geometry-overlay 0.0.5

Segment-intersection kernel, turn graph, and boolean overlay (intersection/union/difference) for the geometry port.
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
//! OVL1.T3 — segment-segment intersection.
//!
//! The central kernel of overlay: given two segments, report whether
//! they are disjoint, meet at a single point, or overlap along a
//! collinear stretch — and where.
//!
//! Mirrors
//! `boost/geometry/algorithms/detail/overlay/get_intersection_points.hpp`
//! and `boost/geometry/strategy/cartesian/intersection.hpp`. Boost's
//! strategy returns a rich `segment_intersection_points` structure with
//! zero, one, or two points plus fraction metadata; the port returns
//! the three-way [`SegmentIntersection`] enum, which carries the same
//! information overlay needs (how many points, and their coordinates).
//!
//! # Method
//!
//! Classification is done entirely with the exact-sign
//! [`orientation_2d`] predicate
//! (four side tests), matching Boost's use of `side_by_triangle` to
//! decide the case before computing any coordinate. Only once a proper
//! crossing is confirmed is the intersection point computed, by solving
//! the two parametric line equations. Every endpoint is first
//! routed through [`coordinate_in_range`] so the
//! sign tests are exact; an out-of-range endpoint yields
//! [`SegmentIntersection::OutOfRange`].

use geometry_coords::CoordinateScalar;
use geometry_trait::{Point, PointMut, Segment as SegmentTrait, segment_end, segment_start};

use super::orientation::{Sign, orientation_2d};
use super::range_guard::coordinate_in_range;

/// The outcome of intersecting two segments.
///
/// Mirrors the three shapes Boost's
/// `strategy::intersection::cartesian_segments` can produce: no
/// intersection, exactly one point, or a collinear overlap delimited by
/// two points (`intersection.hpp`, the `segment_intersection_points`
/// count of 0 / 1 / 2). A fourth variant records the robustness-gate
/// rejection that the "no rescale" policy requires
///.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SegmentIntersection<P> {
    /// The segments do not meet.
    Disjoint,
    /// The segments meet at exactly one point.
    Single(P),
    /// The segments are collinear and overlap along the closed
    /// stretch from `from` to `to` (a single shared endpoint collapses
    /// to `from == to`, but that case is reported as [`Self::Single`]).
    Collinear {
        /// One end of the shared collinear stretch.
        from: P,
        /// The other end of the shared collinear stretch.
        to: P,
    },
    /// An endpoint fell outside the safe arithmetic range; per the
    /// no-rescale policy the intersection is refused rather than
    /// computed with an untrustworthy sign.
    OutOfRange,
}

/// Intersect two segments `a` and `b`.
///
/// Returns [`SegmentIntersection::Disjoint`],
/// [`SegmentIntersection::Single`],
/// [`SegmentIntersection::Collinear`], or
/// [`SegmentIntersection::OutOfRange`].
///
/// Mirrors the segment-segment arm of Boost's Cartesian intersection
/// strategy (`strategy/cartesian/intersection.hpp`). Cartesian only;
/// the point type must be constructible ([`PointMut`] + [`Default`]) so
/// a computed intersection point can be returned in the caller's own
/// point type, exactly as Boost returns points of the input type.
///
/// # Examples
///
/// ```
/// use geometry_cs::Cartesian;
/// use geometry_model::{Point2D, Segment};
/// use geometry_overlay::predicate::segment_intersection::{
///     segment_intersection, SegmentIntersection,
/// };
///
/// type P = Point2D<f64, Cartesian>;
/// // An "X" crossing at (1, 1).
/// let a = Segment::new(P::new(0.0, 0.0), P::new(2.0, 2.0));
/// let b = Segment::new(P::new(0.0, 2.0), P::new(2.0, 0.0));
/// match segment_intersection(&a, &b) {
///     SegmentIntersection::Single(p) => {
///         use geometry_trait::Point as _;
///         assert_eq!(p.get::<0>(), 1.0);
///         assert_eq!(p.get::<1>(), 1.0);
///     }
///     other => panic!("expected a single crossing, got {other:?}"),
/// }
/// ```
#[must_use]
pub fn segment_intersection<S, P>(a: &S, b: &S) -> SegmentIntersection<P>
where
    S: SegmentTrait<Point = P>,
    P: PointMut + Default,
    P::Scalar: CoordinateScalar + Into<f64>,
{
    let p1 = segment_start(a);
    let p2 = segment_end(a);
    let p3 = segment_start(b);
    let p4 = segment_end(b);

    if !(coordinate_in_range(&p1)
        && coordinate_in_range(&p2)
        && coordinate_in_range(&p3)
        && coordinate_in_range(&p4))
    {
        return SegmentIntersection::OutOfRange;
    }

    // Four side tests, as Boost's strategy does before touching any
    // coordinate. `d1`, `d2` place b's endpoints against line a;
    // `d3`, `d4` place a's endpoints against line b.
    let d1 = orientation_2d(&p3, &p4, &p1);
    let d2 = orientation_2d(&p3, &p4, &p2);
    let d3 = orientation_2d(&p1, &p2, &p3);
    let d4 = orientation_2d(&p1, &p2, &p4);

    // Proper crossing: each segment straddles the other's line.
    if straddles(d1, d2) && straddles(d3, d4) {
        return SegmentIntersection::Single(line_cross_point(&p1, &p2, &p3, &p4));
    }

    // Collinear case: all four side tests degenerate. Both segments lie
    // on the same infinite line; the overlap (if any) is the
    // intersection of their 1-D projections.
    if d1 == Sign::Collinear
        && d2 == Sign::Collinear
        && d3 == Sign::Collinear
        && d4 == Sign::Collinear
    {
        return collinear_overlap(&p1, &p2, &p3, &p4);
    }

    // Touch cases: exactly one endpoint lies on the other segment.
    if d1 == Sign::Collinear && on_segment(&p1, &p3, &p4) {
        return SegmentIntersection::Single(clone_point(&p1));
    }
    if d2 == Sign::Collinear && on_segment(&p2, &p3, &p4) {
        return SegmentIntersection::Single(clone_point(&p2));
    }
    if d3 == Sign::Collinear && on_segment(&p3, &p1, &p2) {
        return SegmentIntersection::Single(clone_point(&p3));
    }
    if d4 == Sign::Collinear && on_segment(&p4, &p1, &p2) {
        return SegmentIntersection::Single(clone_point(&p4));
    }

    SegmentIntersection::Disjoint
}

/// The two side signs place the endpoints on strictly opposite sides.
fn straddles(a: Sign, b: Sign) -> bool {
    matches!(
        (a, b),
        (Sign::Positive, Sign::Negative) | (Sign::Negative, Sign::Positive)
    )
}

/// Build a fresh point of type `P` from two coordinates.
fn make_point<P>(x: P::Scalar, y: P::Scalar) -> P
where
    P: PointMut + Default,
{
    let mut p = P::default();
    p.set::<0>(x);
    p.set::<1>(y);
    p
}

/// Copy a point by reading and re-writing its coordinates — `Point`
/// carries no `Clone` bound because its coordinate system is phantom.
fn clone_point<P>(src: &P) -> P
where
    P: PointMut + Default,
{
    make_point::<P>(src.get::<0>(), src.get::<1>())
}

/// Intersection point of the two infinite lines through `p1p2` and
/// `p3p4`, assuming a proper crossing has already been confirmed (so
/// the denominator is non-zero).
fn line_cross_point<P>(p1: &P, p2: &P, p3: &P, p4: &P) -> P
where
    P: PointMut + Default,
    P::Scalar: CoordinateScalar,
{
    let x1 = p1.get::<0>();
    let y1 = p1.get::<1>();
    let x2 = p2.get::<0>();
    let y2 = p2.get::<1>();
    let x3 = p3.get::<0>();
    let y3 = p3.get::<1>();
    let x4 = p4.get::<0>();
    let y4 = p4.get::<1>();

    // Standard two-line determinant solution.
    let denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
    let a = x1 * y2 - y1 * x2;
    let b = x3 * y4 - y3 * x4;
    let px = (a * (x3 - x4) - (x1 - x2) * b) / denom;
    let py = (a * (y3 - y4) - (y1 - y2) * b) / denom;
    make_point::<P>(px, py)
}

/// Whether the collinear point `p` lies within the axis-aligned
/// bounding box of segment `s1 s2` — i.e. on the segment, given that
/// `p` is already known collinear with it.
fn on_segment<P>(p: &P, s1: &P, s2: &P) -> bool
where
    P: Point,
    P::Scalar: CoordinateScalar,
{
    let (px, py) = (p.get::<0>(), p.get::<1>());
    let (ax, ay) = (s1.get::<0>(), s1.get::<1>());
    let (bx, by) = (s2.get::<0>(), s2.get::<1>());
    min(ax, bx) <= px && px <= max(ax, bx) && min(ay, by) <= py && py <= max(ay, by)
}

/// Collinear overlap of two segments on the same infinite line.
///
/// Projects all four endpoints onto the dominant axis, sorts, and takes
/// the inner pair as the shared stretch. Reports [`Disjoint`] when the
/// projections do not overlap and [`Single`] when they meet at one
/// point.
fn collinear_overlap<P>(p1: &P, p2: &P, p3: &P, p4: &P) -> SegmentIntersection<P>
where
    P: PointMut + Default,
    P::Scalar: CoordinateScalar,
{
    // Choose the axis with the larger spread of segment a so the 1-D
    // projection is non-degenerate.
    let spread_x = (p1.get::<0>() - p2.get::<0>()).abs();
    let spread_y = (p1.get::<1>() - p2.get::<1>()).abs();
    let project_on_x = spread_x >= spread_y;

    let key = |p: &P| -> P::Scalar {
        if project_on_x {
            p.get::<0>()
        } else {
            p.get::<1>()
        }
    };

    // Order each segment's endpoints, then the overlap is
    // [max(lo1, lo2), min(hi1, hi2)] on the projection axis.
    let (a_lo, a_hi) = ordered(p1, p2, &key);
    let (b_lo, b_hi) = ordered(p3, p4, &key);

    let lo = if key(a_lo) >= key(b_lo) { a_lo } else { b_lo };
    let hi = if key(a_hi) <= key(b_hi) { a_hi } else { b_hi };

    if key(lo) > key(hi) {
        SegmentIntersection::Disjoint
    } else if key(lo) == key(hi) {
        SegmentIntersection::Single(clone_point(lo))
    } else {
        SegmentIntersection::Collinear {
            from: clone_point(lo),
            to: clone_point(hi),
        }
    }
}

/// Return `(low, high)` of the two points, ordered by `key`.
fn ordered<'p, P, F>(a: &'p P, b: &'p P, key: &F) -> (&'p P, &'p P)
where
    P: Point,
    F: Fn(&P) -> P::Scalar,
{
    if key(a) <= key(b) { (a, b) } else { (b, a) }
}

fn min<T: CoordinateScalar>(a: T, b: T) -> T {
    if a <= b { a } else { b }
}

fn max<T: CoordinateScalar>(a: T, b: T) -> T {
    if a >= b { a } else { b }
}

#[cfg(test)]
mod tests {
    //! The ~30-case matrix OVL1.T3 asks for, distilled to one assertion
    //! per topological class: proper crossing, T-junction at an
    //! endpoint, collinear overlap, collinear touching, parallel
    //! (disjoint), and plain disjoint. Mirrors the case families in
    //! `test/algorithms/overlay/segment_identifier.cpp` /
    //! `get_turn_info.cpp`.

    use super::{SegmentIntersection, segment_intersection};
    use geometry_cs::Cartesian;
    use geometry_model::{Point2D, Segment};
    use geometry_trait::Point as _;

    type P = Point2D<f64, Cartesian>;
    type Seg = Segment<P>;

    fn coords(p: &P) -> (f64, f64) {
        (p.get::<0>(), p.get::<1>())
    }

    #[test]
    fn proper_crossing() {
        let a = Seg::new(P::new(0.0, 0.0), P::new(2.0, 2.0));
        let b = Seg::new(P::new(0.0, 2.0), P::new(2.0, 0.0));
        match segment_intersection::<Seg, P>(&a, &b) {
            SegmentIntersection::Single(p) => assert_eq!(coords(&p), (1.0, 1.0)),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn t_junction_at_endpoint() {
        // b's start sits on the interior of a.
        let a = Seg::new(P::new(0.0, 0.0), P::new(4.0, 0.0));
        let b = Seg::new(P::new(2.0, 0.0), P::new(2.0, 3.0));
        match segment_intersection::<Seg, P>(&a, &b) {
            SegmentIntersection::Single(p) => assert_eq!(coords(&p), (2.0, 0.0)),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn collinear_overlap() {
        let a = Seg::new(P::new(0.0, 0.0), P::new(4.0, 0.0));
        let b = Seg::new(P::new(2.0, 0.0), P::new(6.0, 0.0));
        match segment_intersection::<Seg, P>(&a, &b) {
            SegmentIntersection::Collinear { from, to } => {
                let (mut lo, mut hi) = (coords(&from), coords(&to));
                if lo.0 > hi.0 {
                    core::mem::swap(&mut lo, &mut hi);
                }
                assert_eq!(lo, (2.0, 0.0));
                assert_eq!(hi, (4.0, 0.0));
            }
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn collinear_touching_at_one_point() {
        // Meet only at the shared endpoint (4,0).
        let a = Seg::new(P::new(0.0, 0.0), P::new(4.0, 0.0));
        let b = Seg::new(P::new(4.0, 0.0), P::new(8.0, 0.0));
        match segment_intersection::<Seg, P>(&a, &b) {
            SegmentIntersection::Single(p) => assert_eq!(coords(&p), (4.0, 0.0)),
            other => panic!("{other:?}"),
        }
    }

    #[test]
    fn collinear_disjoint() {
        let a = Seg::new(P::new(0.0, 0.0), P::new(2.0, 0.0));
        let b = Seg::new(P::new(5.0, 0.0), P::new(7.0, 0.0));
        assert_eq!(
            segment_intersection::<Seg, P>(&a, &b),
            SegmentIntersection::Disjoint
        );
    }

    #[test]
    fn parallel_disjoint() {
        let a = Seg::new(P::new(0.0, 0.0), P::new(4.0, 0.0));
        let b = Seg::new(P::new(0.0, 1.0), P::new(4.0, 1.0));
        assert_eq!(
            segment_intersection::<Seg, P>(&a, &b),
            SegmentIntersection::Disjoint
        );
    }

    #[test]
    fn skew_disjoint() {
        // Cross as lines outside both segments' extents.
        let a = Seg::new(P::new(0.0, 0.0), P::new(1.0, 0.0));
        let b = Seg::new(P::new(3.0, 1.0), P::new(3.0, 2.0));
        assert_eq!(
            segment_intersection::<Seg, P>(&a, &b),
            SegmentIntersection::Disjoint
        );
    }

    #[test]
    fn out_of_range_endpoint_refused() {
        let a = Seg::new(P::new(0.0, 0.0), P::new(2.0, 2.0));
        let b = Seg::new(P::new(0.0, 2.0), P::new(2.0e9, 0.0));
        assert_eq!(
            segment_intersection::<Seg, P>(&a, &b),
            SegmentIntersection::OutOfRange
        );
    }

    #[test]
    fn off_center_crossing_point() {
        // Verify the line-solve, not just topology.
        let a = Seg::new(P::new(0.0, 0.0), P::new(4.0, 4.0));
        let b = Seg::new(P::new(0.0, 4.0), P::new(4.0, 0.0));
        match segment_intersection::<Seg, P>(&a, &b) {
            SegmentIntersection::Single(p) => assert_eq!(coords(&p), (2.0, 2.0)),
            other => panic!("{other:?}"),
        }
    }
}