engeom 0.5.0

Library for working with geometry with a focus on engineering metrology applications.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use crate::common::Line;
use crate::common::points::dist;
use crate::geom3::circle3::Circle3;
use crate::geom3::plane3::Plane3;
use crate::geom3::sphere3::{Sphere3, solve_sphere_quadratic};
use crate::{Iso3, Point3, Vector3};
use std::ops;

/// A parameterized line in 3D space: `P(t) = origin + t * direction`.
///
/// This is one of `engeom`'s 3D geometric primitives.
///
/// This is the three-dimensional specialization of the dimension-generic [`Line`](Line); see that
/// type for the shared constructors and queries (`new`, `new_normalize`, `from_points`, `at`,
/// `closest_point`, `distance_to`, `transformed_by`, and so on). The methods defined directly on
/// `Line3` here are the ones that only make sense in 3D.
///
/// The direction is not required to be normalized; use `new_normalize` for unit-speed
/// parameterization where `t` equals unit length.
pub type Line3 = Line<3>;

impl Line3 {
    pub fn x_axis() -> Self {
        Self::new(Point3::origin(), Vector3::x())
    }
    pub fn y_axis() -> Self {
        Self::new(Point3::origin(), Vector3::y())
    }
    pub fn z_axis() -> Self {
        Self::new(Point3::origin(), Vector3::z())
    }

    /// Intersects the line with a plane, returning the parameter `t` at the intersection, or
    /// `None` if the line is parallel to (or lies within) the plane. The parallelism test is
    /// angular, so it does not depend on the magnitude of the line's direction vector.
    pub fn intersect_plane(&self, plane: &Plane3) -> Option<f64> {
        let denom = plane.normal.dot(&self.direction);
        // The guard scales with the direction's magnitude so it rejects by angle alone; `<=`
        // rather than `<` keeps a zero direction (both sides 0.0) on the `None` path.
        if denom.abs() <= 1e-10 * self.direction.norm() {
            return None;
        }
        Some((plane.d - plane.normal.dot(&self.origin.coords)) / denom)
    }

    /// Intersects the line with a sphere, returning 0, 1, or 2 parameters `t` at which the line
    /// meets the sphere surface.
    pub fn intersect_sphere(&self, sphere: &Sphere3) -> Vec<f64> {
        solve_sphere_quadratic(&self.origin, &self.direction, &sphere.center, sphere.r())
    }

    /// Intersect the line with the plane of a circle and check if the intersection point is within
    /// the circle. Returns `None` if the line does not intersect the circle.
    ///
    /// # Arguments
    ///
    /// * `circle`: a reference to the circle to intersect with.
    ///
    /// returns: Option<f64>
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// ```
    /// Projects this line onto a plane. The projected origin is the closest point on the plane to
    /// `self.origin`, and the projected direction is `self.direction` with its normal component
    /// removed. Returns `None` if the line is perpendicular to the plane (the projection
    /// degenerates to a point).
    pub fn project_onto_plane(&self, plane: &Plane3) -> Option<Self> {
        let projected_direction =
            self.direction - plane.normal.into_inner() * plane.normal.dot(&self.direction);
        if projected_direction.norm_squared() < 1e-20 {
            return None;
        }
        Some(Self::new(
            plane.project_point(&self.origin),
            projected_direction,
        ))
    }

    /// Intersect the line with the plane of a circle and check if the intersection point is within
    /// the circle. Returns `None` if the line does not intersect the circle.
    ///
    /// # Arguments
    ///
    /// * `circle`: a reference to the circle to intersect with.
    ///
    /// returns: Option<f64>
    ///
    /// # Examples
    ///
    /// ```
    ///
    /// ```
    pub fn intersect_circle(&self, circle: &Circle3) -> Option<f64> {
        if let Some(t) = self.intersect_plane(&circle.plane()) {
            let p = self.at(t);
            if dist(&p, &circle.center) <= circle.r() {
                Some(t)
            } else {
                None
            }
        } else {
            None
        }
    }
}

impl ops::Mul<Line3> for Iso3 {
    type Output = Line3;
    fn mul(self, rhs: Line3) -> Line3 {
        rhs.transformed_by(&self)
    }
}

impl ops::Mul<&Line3> for Iso3 {
    type Output = Line3;
    fn mul(self, rhs: &Line3) -> Line3 {
        rhs.transformed_by(&self)
    }
}

impl ops::Mul<Line3> for &Iso3 {
    type Output = Line3;
    fn mul(self, rhs: Line3) -> Line3 {
        rhs.transformed_by(self)
    }
}

impl ops::Mul<&Line3> for &Iso3 {
    type Output = Line3;
    fn mul(self, rhs: &Line3) -> Line3 {
        rhs.transformed_by(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::common::random_geometry::RandomGeometry3;
    use crate::geom3::sphere3::Sphere3;
    use crate::{Plane3, UnitVec3};
    use approx::assert_relative_eq;

    fn x_axis_line() -> Line3 {
        Line3::new(Point3::origin(), Vector3::x())
    }

    #[test]
    fn at_zero_is_origin() {
        let line = x_axis_line();
        assert_relative_eq!(line.at(0.0), Point3::origin(), epsilon = 1e-12);
    }

    #[test]
    fn at_one_is_origin_plus_direction() {
        let line = Line3::new(Point3::new(1.0, 2.0, 3.0), Vector3::new(0.0, 1.0, 0.0));
        assert_relative_eq!(line.at(1.0), Point3::new(1.0, 3.0, 3.0), epsilon = 1e-12);
        assert_relative_eq!(line.at(-1.0), Point3::new(1.0, 1.0, 3.0), epsilon = 1e-12);
    }

    #[test]
    fn new_normalize_gives_unit_direction() {
        let line = Line3::new_normalize(Point3::origin(), Vector3::new(3.0, 0.0, 0.0));
        assert_relative_eq!(line.direction.norm(), 1.0, epsilon = 1e-12);
    }

    #[test]
    fn from_points_direction_is_difference() {
        let p1 = Point3::new(1.0, 0.0, 0.0);
        let p2 = Point3::new(4.0, 0.0, 0.0);
        let line = Line3::from_points(&p1, &p2);
        assert_relative_eq!(line.direction, Vector3::new(3.0, 0.0, 0.0), epsilon = 1e-12);
    }

    #[test]
    fn scalar_project_point_on_line() {
        let line = x_axis_line();
        let pt = Point3::new(5.0, 0.0, 0.0);
        assert_relative_eq!(line.scalar_project(&pt), 5.0, epsilon = 1e-12);
    }

    #[test]
    fn scalar_project_perpendicular_offset() {
        // Point directly above the origin - projection is at t=0
        let line = x_axis_line();
        let pt = Point3::new(0.0, 3.0, 0.0);
        assert_relative_eq!(line.scalar_project(&pt), 0.0, epsilon = 1e-12);
    }

    #[test]
    fn scalar_project_non_unit_direction() {
        // Direction has length 2; at(1) = (2,0,0), at(2) = (4,0,0)
        let line = Line3::new(Point3::origin(), Vector3::new(2.0, 0.0, 0.0));
        let pt = Point3::new(4.0, 7.0, 0.0);
        assert_relative_eq!(line.scalar_project(&pt), 2.0, epsilon = 1e-12);
    }

    #[test]
    fn scalar_project_normalized_equals_arc_length() {
        let line = Line3::new_normalize(Point3::origin(), Vector3::new(1.0, 1.0, 0.0));
        let pt = line.at(3.7);
        assert_relative_eq!(line.scalar_project(&pt), 3.7, epsilon = 1e-12);
    }

    #[test]
    fn closest_point_on_line_is_point_itself() {
        let line = x_axis_line();
        let pt = Point3::new(3.0, 0.0, 0.0);
        assert_relative_eq!(line.closest_point(&pt), pt, epsilon = 1e-12);
    }

    #[test]
    fn closest_point_perpendicular_drop() {
        let line = x_axis_line();
        let pt = Point3::new(4.0, 3.0, 0.0);
        assert_relative_eq!(
            line.closest_point(&pt),
            Point3::new(4.0, 0.0, 0.0),
            epsilon = 1e-12
        );
    }

    #[test]
    fn distance_to_point_on_line_is_zero() {
        let line = x_axis_line();
        assert_relative_eq!(line.distance_to(&line.at(5.0)), 0.0, epsilon = 1e-12);
    }

    #[test]
    fn distance_to_known_value() {
        // Line along X, point at (0, 3, 4) distance = 5
        let line = x_axis_line();
        assert_relative_eq!(
            line.distance_to(&Point3::new(0.0, 3.0, 4.0)),
            5.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn stress_closest_point_is_perpendicular() {
        let mut rg = RandomGeometry3::new();
        for _ in 0..500 {
            let iso = rg.iso3(10.0);
            let line = Line3::new(iso * Point3::origin(), iso.rotation * Vector3::x());
            let pt = rg.point(10.0);
            let cp = line.closest_point(&pt);
            // Vector from closest point to test point must be perpendicular to direction
            assert_relative_eq!((pt - cp).dot(&line.direction), 0.0, epsilon = 1e-10);
        }
    }

    #[test]
    fn intersect_plane_hits() {
        // Line along Z, plane z=5
        let line = Line3::new(Point3::origin(), Vector3::z());
        let plane = Plane3::new(Vector3::z_axis(), 5.0);
        let t = line.intersect_plane(&plane).unwrap();
        assert_relative_eq!(t, 5.0, epsilon = 1e-12);
        assert_relative_eq!(
            plane.signed_distance_to_point(&line.at(t)),
            0.0,
            epsilon = 1e-12
        );
    }

    #[test]
    fn intersect_plane_parallel_returns_none() {
        let line = Line3::new(Point3::origin(), Vector3::x());
        let plane = Plane3::new(Vector3::z_axis(), 1.0); // z=1, parallel to XY
        assert!(line.intersect_plane(&plane).is_none());
    }

    #[test]
    fn intersect_plane_oblique() {
        let line = Line3::new(Point3::new(0.0, 0.0, -3.0), Vector3::new(1.0, 1.0, 1.0));
        let plane = Plane3::xy(); // z=0
        let t = line.intersect_plane(&plane).unwrap();
        let pt = line.at(t);
        assert_relative_eq!(pt.z, 0.0, epsilon = 1e-12);
    }

    #[test]
    fn stress_intersect_plane_result_on_plane() {
        // Seeded so a failure is reproducible and so this can never join the flaky-by-RNG set.
        let mut rg = RandomGeometry3::from_seed(0x11e3_5eed);

        for _ in 0..500 {
            let iso = rg.iso3(10.0);
            let origin = iso * Point3::origin();
            let dir = rg.vector(2.0);
            let line = Line3::new(origin, dir);
            let plane = Plane3::new(UnitVec3::new_normalize(iso.rotation * Vector3::z()), 2.0);

            if let Some(t) = line.intersect_plane(&plane) {
                // A near-grazing intersection legitimately has a huge `t`, and the absolute
                // error of evaluating `at(t)` grows with `|t| * |direction|`, so the tolerance
                // scales with the conditioning of the sample rather than staying fixed.
                let scale = 1.0 + t.abs() * line.direction.norm();
                assert_relative_eq!(
                    plane.signed_distance_to_point(&line.at(t)).abs(),
                    0.0,
                    epsilon = 1e-10 * scale
                );
            }
        }
    }

    #[test]
    fn intersect_plane_zero_direction_returns_none() {
        let line = Line3::new(Point3::origin(), Vector3::zeros());
        let plane = Plane3::new(Vector3::z_axis(), 5.0);
        assert!(line.intersect_plane(&plane).is_none());
    }

    #[test]
    fn intersect_plane_guard_is_scale_invariant() {
        let plane = Plane3::new(Vector3::z_axis(), 5.0);

        // A tiny but well-aligned direction still intersects; t scales with 1/|direction|.
        let tiny = Line3::new(Point3::origin(), Vector3::z() * 1e-8);
        let t = tiny.intersect_plane(&plane).unwrap();
        assert_relative_eq!(t, 5e8, epsilon = 1e-3);

        // A large direction at a below-threshold grazing angle is still rejected.
        let grazing = Line3::new(Point3::origin(), Vector3::new(1e3, 0.0, 1e-8));
        assert!(grazing.intersect_plane(&plane).is_none());
    }

    #[test]
    fn line_through_sphere_two_intersections() {
        let sphere = Sphere3::new(&Point3::origin(), 3.0);
        let line = Line3::new(Point3::new(0.0, 0.0, -10.0), Vector3::z());
        let ts = line.intersect_sphere(&sphere);
        assert_eq!(ts.len(), 2);
        for &t in &ts {
            assert_relative_eq!(
                (line.at(t) - sphere.center).norm(),
                sphere.r(),
                epsilon = 1e-10
            );
        }
        let t_vals: Vec<f64> = {
            let mut v = ts.clone();
            v.sort_by(|a, b| a.partial_cmp(b).unwrap());
            v
        };
        // Origin at z=-10 going +z: hits sphere at z=-3 (t=7) and z=+3 (t=13)
        assert_relative_eq!(t_vals[0], 7.0, epsilon = 1e-10);
        assert_relative_eq!(t_vals[1], 13.0, epsilon = 1e-10);
    }

    #[test]
    fn line_misses_sphere() {
        let sphere = Sphere3::new(&Point3::origin(), 1.0);
        let line = Line3::new(Point3::new(5.0, 0.0, 0.0), Vector3::z());
        assert!(line.intersect_sphere(&sphere).is_empty());
    }

    #[test]
    fn line_tangent_to_sphere() {
        let sphere = Sphere3::new(&Point3::origin(), 1.0);
        // Line at x=1, y=0 along Z is tangent to sphere
        let line = Line3::new(Point3::new(1.0, 0.0, 0.0), Vector3::z());
        let ts = line.intersect_sphere(&sphere);
        assert_eq!(ts.len(), 1);
        let pt = line.at(ts[0]);
        assert_relative_eq!((pt - sphere.center).norm(), sphere.r(), epsilon = 1e-10);
        assert_relative_eq!(pt.z, 0.0, epsilon = 1e-10);
    }

    #[test]
    fn stress_intersect_sphere_points_on_surface() {
        let mut rg = RandomGeometry3::new();
        for _ in 0..1000 {
            let sphere = Sphere3::new(&rg.point(2.0), rg.f64(0.1, 3.0));
            let line = Line3::new(rg.point(3.0), rg.vector(2.0));

            for &t in &line.intersect_sphere(&sphere) {
                let dist = (line.at(t) - sphere.center).norm();
                assert_relative_eq!(dist, sphere.r(), epsilon = 1e-8);
            }
        }
    }

    #[test]
    fn project_onto_plane_origin_lies_on_plane() {
        let mut rg = RandomGeometry3::new();
        for _ in 0..500 {
            let iso = rg.iso3(10.0);
            let plane = Plane3::xy().transformed_by(&iso);
            let line = Line3::new(rg.point(10.0), rg.vector(1.0));
            if let Some(proj) = line.project_onto_plane(&plane) {
                assert_relative_eq!(
                    plane.signed_distance_to_point(&proj.origin).abs(),
                    0.0,
                    epsilon = 1e-10
                );
            }
        }
    }

    #[test]
    fn project_onto_plane_direction_parallel_to_plane() {
        let mut rg = RandomGeometry3::new();
        for _ in 0..500 {
            let iso = rg.iso3(10.0);
            let plane = Plane3::xy().transformed_by(&iso);
            let line = Line3::new(rg.point(10.0), rg.vector(1.0));
            if let Some(proj) = line.project_onto_plane(&plane) {
                // projected direction must be perpendicular to the plane normal
                assert_relative_eq!(plane.normal.dot(&proj.direction), 0.0, epsilon = 1e-10);
            }
        }
    }

    #[test]
    fn project_onto_plane_perpendicular_line_returns_none() {
        // Line along Z projected onto the XY plane: direction collapses to zero
        let plane = Plane3::xy();
        let line = Line3::new(Point3::new(1.0, 2.0, 5.0), Vector3::z());
        assert!(line.project_onto_plane(&plane).is_none());
    }

    #[test]
    fn project_onto_plane_already_in_plane() {
        // A line lying in the XY plane should be unchanged
        let plane = Plane3::xy();
        let line = Line3::new(Point3::new(1.0, 2.0, 0.0), Vector3::x());
        let proj = line.project_onto_plane(&plane).unwrap();
        assert_relative_eq!(proj.origin, line.origin, epsilon = 1e-12);
        assert_relative_eq!(proj.direction, line.direction, epsilon = 1e-12);
    }

    #[test]
    fn transformed_by_isometry_preserves_point_on_line() {
        let mut rg = RandomGeometry3::new();

        for _ in 0..500 {
            let original = Line3::new(rg.point(10.0), rg.vector(1.0));
            let iso = rg.iso3(10.0);
            let transformed = original.transformed_by(&iso);

            for t in [-3.0, -1.0, -0.001, 0.0, 0.001, 1.0, 3.0] {
                let p0 = original.at(t);
                let p1 = transformed.at(t);
                assert_relative_eq!(iso * p0, p1, epsilon = 1e-12);
            }
        }
    }
}