solstice_2d/d3/
shapes.rs

1mod arc_geometry;
2mod box_geometry;
3mod plane_geometry;
4mod polyhedron_geometry;
5mod sphere_geometry;
6
7pub use arc_geometry::Arc3D;
8pub use box_geometry::Box;
9pub use plane_geometry::Plane;
10pub use polyhedron_geometry::Polyhedron;
11pub use sphere_geometry::Sphere;
12
13#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Default)]
14pub struct Point3D {
15    pub x: f32,
16    pub y: f32,
17    pub z: f32,
18}
19
20impl Point3D {
21    pub fn new(x: f32, y: f32, z: f32) -> Self {
22        Self { x, y, z }
23    }
24
25    pub fn length(&self) -> f32 {
26        (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
27    }
28
29    pub fn lerp(&self, other: &Point3D, ratio: f32) -> Point3D {
30        let mut r = *self;
31        r.x += (other.x - self.x) * ratio;
32        r.y += (other.y - self.y) * ratio;
33        r.z += (other.z - self.z) * ratio;
34        r
35    }
36
37    pub fn normalize(&self) -> Point3D {
38        self.divide_scalar(self.length())
39    }
40
41    pub fn divide_scalar(&self, scalar: f32) -> Point3D {
42        self.multiply_scalar(1.0 / scalar)
43    }
44
45    pub fn multiply_scalar(&self, scalar: f32) -> Point3D {
46        Self {
47            x: self.x * scalar,
48            y: self.y * scalar,
49            z: self.z * scalar,
50        }
51    }
52}
53
54impl From<[f32; 3]> for Point3D {
55    fn from([x, y, z]: [f32; 3]) -> Self {
56        Self { x, y, z }
57    }
58}
59
60impl From<Point3D> for [f32; 3] {
61    fn from(p: Point3D) -> Self {
62        [p.x, p.y, p.z]
63    }
64}