Skip to main content

mathverse_geometry/
shapes3d.rs

1//! 3D shapes: point, sphere, cube, cylinder, cone, plane, line.
2
3use mathverse_core::constants::PI;
4
5/// 3D point.
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub struct Point3 {
8    pub x: f64,
9    pub y: f64,
10    pub z: f64,
11}
12
13impl Point3 {
14    pub fn new(x: f64, y: f64, z: f64) -> Self {
15        Point3 { x, y, z }
16    }
17    pub fn distance_to(self, other: Point3) -> f64 {
18        let dx = self.x - other.x;
19        let dy = self.y - other.y;
20        let dz = self.z - other.z;
21        (dx * dx + dy * dy + dz * dz).sqrt()
22    }
23    pub fn translate(self, dx: f64, dy: f64, dz: f64) -> Point3 {
24        Point3::new(self.x + dx, self.y + dy, self.z + dz)
25    }
26    pub fn scale(self, s: f64) -> Point3 {
27        Point3::new(self.x * s, self.y * s, self.z * s)
28    }
29}
30
31fn dot(a: Point3, b: Point3) -> f64 {
32    a.x * b.x + a.y * b.y + a.z * b.z
33}
34
35fn cross(a: Point3, b: Point3) -> Point3 {
36    Point3::new(
37        a.y * b.z - a.z * b.y,
38        a.z * b.x - a.x * b.z,
39        a.x * b.y - a.y * b.x,
40    )
41}
42
43fn norm(v: Point3) -> f64 {
44    dot(v, v).sqrt()
45}
46
47/// Sphere.
48#[derive(Debug, Clone, Copy, PartialEq)]
49pub struct Sphere {
50    pub center: Point3,
51    pub radius: f64,
52}
53
54impl Sphere {
55    /// Panics if `radius < 0`.
56    pub fn new(center: Point3, radius: f64) -> Sphere {
57        assert!(radius >= 0.0, "sphere radius must be non-negative");
58        Sphere { center, radius }
59    }
60    pub fn volume(&self) -> f64 {
61        4.0 / 3.0 * PI * self.radius.powi(3)
62    }
63    pub fn surface_area(&self) -> f64 {
64        4.0 * PI * self.radius.powi(2)
65    }
66    pub fn contains(&self, p: Point3) -> bool {
67        self.center.distance_to(p) <= self.radius
68    }
69    /// Sphere-sphere collision (touch counts as collision).
70    pub fn collides_with(&self, other: &Sphere) -> bool {
71        self.center.distance_to(other.center) <= self.radius + other.radius
72    }
73}
74
75/// Cube with a center and side length.
76#[derive(Debug, Clone, Copy, PartialEq)]
77pub struct Cube {
78    pub center: Point3,
79    pub side: f64,
80}
81
82impl Cube {
83    /// Panics if `side < 0`.
84    pub fn new(center: Point3, side: f64) -> Cube {
85        assert!(side >= 0.0, "cube side must be non-negative");
86        Cube { center, side }
87    }
88    pub fn volume(&self) -> f64 {
89        self.side.powi(3)
90    }
91    pub fn surface_area(&self) -> f64 {
92        6.0 * self.side.powi(2)
93    }
94}
95
96/// Right circular cylinder, `height` along the z axis.
97#[derive(Debug, Clone, Copy, PartialEq)]
98pub struct Cylinder {
99    pub center: Point3,
100    pub radius: f64,
101    pub height: f64,
102}
103
104impl Cylinder {
105    /// Panics if `radius < 0` or `height < 0`.
106    pub fn new(center: Point3, radius: f64, height: f64) -> Cylinder {
107        assert!(radius >= 0.0 && height >= 0.0, "cylinder dimensions must be non-negative");
108        Cylinder { center, radius, height }
109    }
110    pub fn volume(&self) -> f64 {
111        PI * self.radius.powi(2) * self.height
112    }
113    /// Lateral surface plus both caps.
114    pub fn surface_area(&self) -> f64 {
115        2.0 * PI * self.radius * (self.radius + self.height)
116    }
117}
118
119/// Right circular cone, `height` along the z axis.
120#[derive(Debug, Clone, Copy, PartialEq)]
121pub struct Cone {
122    pub center: Point3,
123    pub radius: f64,
124    pub height: f64,
125}
126
127impl Cone {
128    /// Panics if `radius < 0` or `height < 0`.
129    pub fn new(center: Point3, radius: f64, height: f64) -> Cone {
130        assert!(radius >= 0.0 && height >= 0.0, "cone dimensions must be non-negative");
131        Cone { center, radius, height }
132    }
133    pub fn volume(&self) -> f64 {
134        PI * self.radius.powi(2) * self.height / 3.0
135    }
136    /// Lateral surface plus base.
137    pub fn surface_area(&self) -> f64 {
138        PI * self.radius * (self.radius + (self.radius.powi(2) + self.height.powi(2)).sqrt())
139    }
140}
141
142/// Plane `normal ยท p + d = 0`.
143#[derive(Debug, Clone, Copy, PartialEq)]
144pub struct Plane {
145    pub normal: Point3,
146    pub d: f64,
147}
148
149impl Plane {
150    /// Panics if `normal` is the zero vector.
151    pub fn new(normal: Point3, d: f64) -> Plane {
152        assert!(norm(normal) > 0.0, "plane normal must be non-zero");
153        Plane { normal, d }
154    }
155    /// Signed distance (positive on the side the normal points to).
156    pub fn signed_distance(&self, p: Point3) -> f64 {
157        (dot(self.normal, p) + self.d) / norm(self.normal)
158    }
159    /// Absolute distance.
160    pub fn distance(&self, p: Point3) -> f64 {
161        self.signed_distance(p).abs()
162    }
163    /// Orthogonal projection of `p` onto the plane.
164    pub fn project(&self, p: Point3) -> Point3 {
165        let n = self.normal.scale(1.0 / norm(self.normal));
166        p.translate(-n.x * self.signed_distance(p), -n.y * self.signed_distance(p), -n.z * self.signed_distance(p))
167    }
168}
169
170/// Line through `point` with direction `dir`.
171#[derive(Debug, Clone, Copy, PartialEq)]
172pub struct Line3 {
173    pub point: Point3,
174    pub dir: Point3,
175}
176
177impl Line3 {
178    /// Panics if `dir` is the zero vector.
179    pub fn new(point: Point3, dir: Point3) -> Line3 {
180        assert!(norm(dir) > 0.0, "line direction must be non-zero");
181        Line3 { point, dir }
182    }
183    /// Distance from a point to the line.
184    pub fn distance(&self, p: Point3) -> f64 {
185        let v = p.translate(-self.point.x, -self.point.y, -self.point.z);
186        norm(cross(v, self.dir)) / norm(self.dir)
187    }
188    /// Orthogonal projection of `p` onto the line.
189    pub fn project(&self, p: Point3) -> Point3 {
190        let v = p.translate(-self.point.x, -self.point.y, -self.point.z);
191        let t = dot(v, self.dir) / dot(self.dir, self.dir);
192        self.point.translate(self.dir.x * t, self.dir.y * t, self.dir.z * t)
193    }
194}
195
196#[cfg(test)]
197mod tests {
198    use super::*;
199
200    #[test]
201    fn sphere() {
202        let s = Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0);
203        assert!((s.volume() - 4.0 / 3.0 * PI).abs() < 1e-12);
204        assert!((s.surface_area() - 4.0 * PI).abs() < 1e-12);
205        assert!(s.contains(Point3::new(0.5, 0.5, 0.5)));
206        assert!(!s.contains(Point3::new(1.0, 1.0, 1.0)));
207        let a = Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0);
208        let b = Sphere::new(Point3::new(1.5, 0.0, 0.0), 1.0);
209        assert!(a.collides_with(&b));
210        let c = Sphere::new(Point3::new(3.0, 0.0, 0.0), 1.0);
211        assert!(!a.collides_with(&c));
212    }
213
214    #[test]
215    fn cube() {
216        let c = Cube::new(Point3::new(0.0, 0.0, 0.0), 2.0);
217        assert_eq!(c.volume(), 8.0);
218        assert_eq!(c.surface_area(), 24.0);
219    }
220
221    #[test]
222    fn cylinder_and_cone() {
223        let cy = Cylinder::new(Point3::new(0.0, 0.0, 0.0), 1.0, 3.0);
224        assert!((cy.volume() - 3.0 * PI).abs() < 1e-12);
225        let co = Cone::new(Point3::new(0.0, 0.0, 0.0), 1.0, 3.0);
226        assert!((co.volume() - PI).abs() < 1e-12); // exactly 1/3 of the cylinder
227    }
228
229    #[test]
230    fn plane() {
231        let p = Plane::new(Point3::new(0.0, 0.0, 1.0), -2.0); // z = 2
232        assert_eq!(p.distance(Point3::new(1.0, 1.0, 5.0)), 3.0);
233        assert_eq!(p.project(Point3::new(1.0, 1.0, 5.0)), Point3::new(1.0, 1.0, 2.0));
234        assert_eq!(p.signed_distance(Point3::new(0.0, 0.0, 0.0)), -2.0);
235    }
236
237    #[test]
238    fn line() {
239        // x axis through origin
240        let l = Line3::new(Point3::new(0.0, 0.0, 0.0), Point3::new(1.0, 0.0, 0.0));
241        assert_eq!(l.distance(Point3::new(0.0, 3.0, 4.0)), 5.0);
242        assert_eq!(l.project(Point3::new(7.0, 3.0, 4.0)), Point3::new(7.0, 0.0, 0.0));
243    }
244}