i_overlay 6.0.0

Boolean Operations for 2D Polygons: Supports intersection, union, difference, xor, and self-intersections for all polygon varieties.
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use crate::core::fill_rule::FillRule;
use crate::core::overlay::ShapeType;
use crate::core::relate::PredicateOverlay;
use crate::core::solver::Solver;
use i_float::adapter::FloatPointAdapter;
use i_float::float::compatible::FloatPointCompatible;
use i_shape::source::resource::ShapeResource;

/// Float-coordinate wrapper for spatial predicate evaluation.
///
/// `FloatPredicateOverlay` handles conversion from floating-point coordinates to
/// the internal integer representation, then delegates to [`PredicateOverlay`](crate::core::relate::PredicateOverlay)
/// for efficient predicate evaluation.
///
/// # Example
///
/// ```
/// use i_overlay::float::relate::FloatPredicateOverlay;
///
/// let subject = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
/// let clip = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];
///
/// let mut overlay = FloatPredicateOverlay::with_subj_and_clip(&subject, &clip);
/// assert!(overlay.intersects());
/// ```
///
/// For a more ergonomic API, see the [`FloatRelate`] trait which provides
/// methods directly on shape types.
pub struct FloatPredicateOverlay<P: FloatPointCompatible> {
    pub(crate) overlay: PredicateOverlay,
    pub(crate) adapter: FloatPointAdapter<P>,
}

impl<P: FloatPointCompatible> FloatPredicateOverlay<P> {
    /// Creates a new predicate overlay with a pre-configured adapter.
    ///
    /// Use this when you need fixed-scale precision via `FloatPointAdapter::with_scale()`.
    ///
    /// # Arguments
    /// * `adapter` - A `FloatPointAdapter` instance for coordinate conversion.
    /// * `capacity` - Initial capacity for storing segments.
    #[inline]
    pub fn with_adapter(adapter: FloatPointAdapter<P>, capacity: usize) -> Self {
        Self {
            overlay: PredicateOverlay::new(capacity),
            adapter,
        }
    }

    /// Creates a new predicate overlay with a pre-configured adapter, fill rule, and solver.
    ///
    /// Use this when you need fixed-scale precision with custom overlay settings.
    ///
    /// # Arguments
    /// * `adapter` - A `FloatPointAdapter` instance for coordinate conversion.
    /// * `fill_rule` - Fill rule to determine filled areas.
    /// * `solver` - Type of solver to use.
    /// * `capacity` - Initial capacity for storing segments.
    #[inline]
    pub fn with_adapter_custom(
        adapter: FloatPointAdapter<P>,
        fill_rule: FillRule,
        solver: Solver,
        capacity: usize,
    ) -> Self {
        let mut overlay = PredicateOverlay::new(capacity);
        overlay.fill_rule = fill_rule;
        overlay.solver = solver;
        Self { overlay, adapter }
    }

    /// Creates a new predicate overlay from subject and clip shapes.
    pub fn with_subj_and_clip<R0, R1>(subj: &R0, clip: &R1) -> Self
    where
        R0: ShapeResource<P> + ?Sized,
        R1: ShapeResource<P> + ?Sized,
    {
        let iter = subj.iter_paths().chain(clip.iter_paths()).flatten();
        let adapter = FloatPointAdapter::with_iter(iter);
        let subj_capacity = subj.iter_paths().fold(0, |s, c| s + c.len());
        let clip_capacity = clip.iter_paths().fold(0, |s, c| s + c.len());

        let mut result = Self {
            overlay: PredicateOverlay::new(subj_capacity + clip_capacity),
            adapter,
        };
        result.add_source(subj, ShapeType::Subject);
        result.add_source(clip, ShapeType::Clip);
        result
    }

    /// Creates a new predicate overlay with custom solver and fill rule.
    pub fn with_subj_and_clip_custom<R0, R1>(
        subj: &R0,
        clip: &R1,
        fill_rule: FillRule,
        solver: Solver,
    ) -> Self
    where
        R0: ShapeResource<P> + ?Sized,
        R1: ShapeResource<P> + ?Sized,
    {
        let iter = subj.iter_paths().chain(clip.iter_paths()).flatten();
        let adapter = FloatPointAdapter::with_iter(iter);
        let subj_capacity = subj.iter_paths().fold(0, |s, c| s + c.len());
        let clip_capacity = clip.iter_paths().fold(0, |s, c| s + c.len());

        let mut overlay = PredicateOverlay::new(subj_capacity + clip_capacity);
        overlay.fill_rule = fill_rule;
        overlay.solver = solver;

        let mut result = Self { overlay, adapter };
        result.add_source(subj, ShapeType::Subject);
        result.add_source(clip, ShapeType::Clip);
        result
    }

    /// Adds a shape resource as subject or clip.
    ///
    /// # Arguments
    /// * `resource` - A `ShapeResource` specifying the geometry to add.
    /// * `shape_type` - Whether to add as `Subject` or `Clip`.
    pub fn add_source<R: ShapeResource<P> + ?Sized>(&mut self, resource: &R, shape_type: ShapeType) {
        for contour in resource.iter_paths() {
            self.overlay
                .add_path_iter(contour.iter().map(|p| self.adapter.float_to_int(p)), shape_type);
        }
    }

    /// Clears segments for reuse with new geometry.
    #[inline]
    pub fn clear(&mut self) {
        self.overlay.clear();
    }

    /// Returns `true` if the subject and clip shapes intersect.
    ///
    /// Uses early-exit optimization - returns immediately when the first
    /// intersection is found.
    #[inline]
    pub fn intersects(&mut self) -> bool {
        self.overlay.intersects()
    }

    /// Returns `true` if the interiors of subject and clip shapes overlap.
    #[inline]
    pub fn interiors_intersect(&mut self) -> bool {
        self.overlay.interiors_intersect()
    }

    /// Returns `true` if subject and clip shapes touch (boundaries intersect but interiors don't).
    #[inline]
    pub fn touches(&mut self) -> bool {
        self.overlay.touches()
    }

    /// Returns `true` if subject and clip shapes intersect by point coincidence only.
    #[inline]
    pub fn point_intersects(&mut self) -> bool {
        self.overlay.point_intersects()
    }

    /// Returns `true` if subject is completely within clip.
    #[inline]
    pub fn within(&mut self) -> bool {
        self.overlay.within()
    }
}

/// Ergonomic trait for spatial predicate operations on shape resources.
///
/// This trait provides convenient methods for testing spatial relationships
/// directly on contours, shapes, and shape collections without explicit
/// overlay construction.
///
/// # Example
///
/// ```
/// use i_overlay::float::relate::FloatRelate;
///
/// let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
/// let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];
///
/// // Overlapping shapes
/// assert!(square.intersects(&other));
/// assert!(square.interiors_intersect(&other));
/// assert!(!square.touches(&other));
///
/// let distant = vec![[100.0, 100.0], [100.0, 110.0], [110.0, 110.0], [110.0, 100.0]];
///
/// // Non-overlapping shapes (fast bounding-box rejection)
/// assert!(!square.intersects(&distant));
/// assert!(square.disjoint(&distant));
/// ```
///
/// # Supported Types
///
/// This trait is implemented for any type implementing `ShapeResource`, including:
/// - `Vec<[f64; 2]>` - single contour
/// - `Vec<Vec<[f64; 2]>>` - multiple contours (shape with holes)
/// - `Vec<Vec<Vec<[f64; 2]>>>` - multiple shapes
pub trait FloatRelate<R1, P>
where
    R1: ShapeResource<P> + ?Sized,
    P: FloatPointCompatible,
{
    /// Returns `true` if this shape intersects with another (shares any point).
    ///
    /// This method uses bounding-box rejection for fast negative results and
    /// early-exit for fast positive results. It's significantly more efficient
    /// than computing a full intersection when you only need a boolean answer.
    ///
    /// Matches the DE-9IM definition: returns `true` for both interior
    /// overlap and boundary contact (shapes sharing an edge).
    fn intersects(&self, other: &R1) -> bool;

    /// Returns `true` if the interiors of this shape and another overlap.
    ///
    /// Unlike `intersects()`, this returns `false` for shapes that only share
    /// boundary points (edges or vertices) without interior overlap.
    fn interiors_intersect(&self, other: &R1) -> bool;

    /// Returns `true` if this shape touches another (boundaries intersect but interiors don't).
    ///
    /// Returns `true` when shapes share boundary points but their interiors don't overlap.
    fn touches(&self, other: &R1) -> bool;

    /// Returns `true` if this shape intersects another by point coincidence only.
    fn point_intersects(&self, other: &R1) -> bool;

    /// Returns `true` if this shape is completely within another.
    ///
    /// Subject is within clip if everywhere the subject has fill, the clip
    /// also has fill on the same side.
    fn within(&self, other: &R1) -> bool;

    /// Returns `true` if this shape does not intersect with another (no shared points).
    ///
    /// This is the negation of `intersects()`.
    fn disjoint(&self, other: &R1) -> bool;

    /// Returns `true` if this shape completely covers another.
    ///
    /// `covers(A, B)` is equivalent to `within(B, A)`.
    fn covers(&self, other: &R1) -> bool;
}

impl<R0, R1, P> FloatRelate<R1, P> for R0
where
    R0: ShapeResource<P> + ?Sized,
    R1: ShapeResource<P> + ?Sized,
    P: FloatPointCompatible,
{
    #[inline]
    fn intersects(&self, other: &R1) -> bool {
        FloatPredicateOverlay::with_subj_and_clip(self, other).intersects()
    }

    #[inline]
    fn interiors_intersect(&self, other: &R1) -> bool {
        FloatPredicateOverlay::with_subj_and_clip(self, other).interiors_intersect()
    }

    #[inline]
    fn touches(&self, other: &R1) -> bool {
        FloatPredicateOverlay::with_subj_and_clip(self, other).touches()
    }

    #[inline]
    fn point_intersects(&self, other: &R1) -> bool {
        FloatPredicateOverlay::with_subj_and_clip(self, other).point_intersects()
    }

    #[inline]
    fn within(&self, other: &R1) -> bool {
        FloatPredicateOverlay::with_subj_and_clip(self, other).within()
    }

    #[inline]
    fn disjoint(&self, other: &R1) -> bool {
        !self.intersects(other)
    }

    #[inline]
    fn covers(&self, other: &R1) -> bool {
        other.within(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec;
    use alloc::vec::Vec;

    #[test]
    fn test_intersects_overlapping() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        assert!(square.intersects(&other));
        assert!(other.intersects(&square));
    }

    #[test]
    fn test_intersects_disjoint() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [30.0, 20.0]];

        assert!(!square.intersects(&other));
        assert!(!other.intersects(&square));
    }

    #[test]
    fn test_intersects_contained() {
        let outer = vec![[0.0, 0.0], [0.0, 20.0], [20.0, 20.0], [20.0, 0.0]];
        let inner = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        assert!(outer.intersects(&inner));
        assert!(inner.intersects(&outer));
    }

    #[test]
    fn test_intersects_touching_edge() {
        let left = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let right = vec![[10.0, 0.0], [10.0, 10.0], [20.0, 10.0], [20.0, 0.0]];

        // Shapes sharing an edge intersect (boundary contact) per DE-9IM
        assert!(left.intersects(&right));
        assert!(right.intersects(&left));
    }

    #[test]
    fn test_intersects_empty() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let empty: Vec<[f64; 2]> = vec![];

        assert!(!square.intersects(&empty));
        assert!(!empty.intersects(&square));
    }

    #[test]
    fn test_disjoint() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [30.0, 20.0]];

        assert!(square.disjoint(&other));
        assert!(other.disjoint(&square));

        let overlapping = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];
        assert!(!square.disjoint(&overlapping));
    }

    #[test]
    fn test_interiors_intersect_overlapping() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        assert!(square.interiors_intersect(&other));
        assert!(other.interiors_intersect(&square));
    }

    #[test]
    fn test_interiors_intersect_touching_edge() {
        let left = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let right = vec![[10.0, 0.0], [10.0, 10.0], [20.0, 10.0], [20.0, 0.0]];

        // Shapes sharing an edge don't have interior overlap
        assert!(!left.interiors_intersect(&right));
        assert!(!right.interiors_intersect(&left));
    }

    #[test]
    fn test_interiors_intersect_disjoint() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [30.0, 20.0]];

        assert!(!square.interiors_intersect(&other));
    }

    #[test]
    fn test_touches_edge_sharing() {
        let left = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let right = vec![[10.0, 0.0], [10.0, 10.0], [20.0, 10.0], [20.0, 0.0]];

        // Shapes sharing an edge touch
        assert!(left.touches(&right));
        assert!(right.touches(&left));
    }

    #[test]
    fn test_touches_overlapping() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        // Overlapping shapes don't touch (interiors intersect)
        assert!(!square.touches(&other));
        assert!(!other.touches(&square));
    }

    #[test]
    fn test_touches_disjoint() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [30.0, 20.0]];

        // Disjoint shapes don't touch
        assert!(!square.touches(&other));
    }

    #[test]
    fn test_within_contained() {
        let outer = vec![[0.0, 0.0], [0.0, 20.0], [20.0, 20.0], [20.0, 0.0]];
        let inner = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        assert!(inner.within(&outer));
        assert!(!outer.within(&inner));
    }

    #[test]
    fn test_within_overlapping() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        // Overlapping shapes - neither is within the other
        assert!(!square.within(&other));
        assert!(!other.within(&square));
    }

    #[test]
    fn test_within_disjoint() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [30.0, 20.0]];

        assert!(!square.within(&other));
        assert!(!other.within(&square));
    }

    #[test]
    fn test_within_empty() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let empty: Vec<[f64; 2]> = vec![];

        // Empty is not within anything
        assert!(!empty.within(&square));
        // Non-empty is not within empty
        assert!(!square.within(&empty));
    }

    #[test]
    fn test_covers_contained() {
        let outer = vec![[0.0, 0.0], [0.0, 20.0], [20.0, 20.0], [20.0, 0.0]];
        let inner = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        assert!(outer.covers(&inner));
        assert!(!inner.covers(&outer));
    }

    #[test]
    fn test_covers_overlapping() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        // Neither covers the other
        assert!(!square.covers(&other));
        assert!(!other.covers(&square));
    }

    #[test]
    fn test_covers_empty() {
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let empty: Vec<[f64; 2]> = vec![];

        // Anything covers empty (empty is within anything)
        // But empty.within(square) is false, so square.covers(empty) = empty.within(square) = false
        assert!(!square.covers(&empty));
    }

    #[test]
    fn test_predicate_consistency() {
        // Test that predicates are consistent for various scenarios

        // Overlapping squares
        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        assert!(square.intersects(&other));
        assert!(!square.disjoint(&other));
        assert!(square.interiors_intersect(&other));
        assert!(!square.touches(&other));
        assert!(!square.within(&other));
        assert!(!square.covers(&other));

        // Disjoint squares
        let distant = vec![[100.0, 100.0], [100.0, 110.0], [110.0, 110.0], [110.0, 100.0]];

        assert!(!square.intersects(&distant));
        assert!(square.disjoint(&distant));
        assert!(!square.interiors_intersect(&distant));
        assert!(!square.touches(&distant));
        assert!(!square.within(&distant));
        assert!(!square.covers(&distant));

        // Contained
        let outer = vec![[0.0, 0.0], [0.0, 20.0], [20.0, 20.0], [20.0, 0.0]];
        let inner = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        assert!(outer.intersects(&inner));
        assert!(!outer.disjoint(&inner));
        assert!(outer.interiors_intersect(&inner));
        assert!(!outer.touches(&inner));
        assert!(!outer.within(&inner));
        assert!(outer.covers(&inner));

        assert!(inner.intersects(&outer));
        assert!(inner.interiors_intersect(&outer));
        assert!(inner.within(&outer));
        assert!(!inner.covers(&outer));

        // Edge-sharing only
        let left = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let right = vec![[10.0, 0.0], [10.0, 10.0], [20.0, 10.0], [20.0, 0.0]];

        assert!(left.intersects(&right));
        assert!(!left.disjoint(&right));
        assert!(!left.interiors_intersect(&right));
        assert!(left.touches(&right));
        assert!(!left.within(&right));
        assert!(!left.covers(&right));
    }

    #[test]
    fn test_predicate_overlay_with_adapter() {
        use crate::core::overlay::ShapeType;
        use i_float::adapter::FloatPointAdapter;

        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        let iter = square.iter().chain(other.iter());
        let adapter = FloatPointAdapter::with_iter(iter);

        let mut overlay = FloatPredicateOverlay::with_adapter(adapter, 16);
        overlay.add_source(&square, ShapeType::Subject);
        overlay.add_source(&other, ShapeType::Clip);
        assert!(overlay.intersects());
    }

    #[test]
    fn test_predicate_overlay_with_adapter_custom() {
        use crate::core::fill_rule::FillRule;
        use crate::core::overlay::ShapeType;
        use crate::core::solver::Solver;
        use i_float::adapter::FloatPointAdapter;

        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        let iter = square.iter().chain(other.iter());
        let adapter = FloatPointAdapter::with_iter(iter);

        let mut overlay =
            FloatPredicateOverlay::with_adapter_custom(adapter, FillRule::NonZero, Solver::default(), 16);
        overlay.add_source(&square, ShapeType::Subject);
        overlay.add_source(&other, ShapeType::Clip);
        assert!(overlay.intersects());
    }

    #[test]
    fn test_predicate_overlay_with_subj_and_clip_custom() {
        use crate::core::fill_rule::FillRule;
        use crate::core::solver::Solver;

        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        let mut overlay = FloatPredicateOverlay::with_subj_and_clip_custom(
            &square,
            &other,
            FillRule::NonZero,
            Solver::default(),
        );
        assert!(overlay.intersects());
    }

    #[test]
    fn test_predicate_overlay_clear() {
        use crate::core::overlay::ShapeType;

        let square = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let other = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        let mut overlay = FloatPredicateOverlay::with_subj_and_clip(&square, &other);
        assert!(overlay.intersects());

        overlay.clear();

        // Use coordinates within the original adapter bounds
        let touching = vec![[10.0, 0.0], [10.0, 10.0], [15.0, 10.0], [15.0, 0.0]];
        overlay.add_source(&square, ShapeType::Subject);
        overlay.add_source(&touching, ShapeType::Clip);
        // After clear and re-add, shapes touch but don't overlap interiors
        assert!(overlay.intersects());
    }

    #[test]
    fn test_predicate_overlay_all_predicates() {
        let outer = vec![[0.0, 0.0], [0.0, 20.0], [20.0, 20.0], [20.0, 0.0]];
        let inner = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];

        let mut overlay = FloatPredicateOverlay::with_subj_and_clip(&inner, &outer);
        assert!(overlay.intersects());

        overlay.clear();
        overlay.add_source(&inner, crate::core::overlay::ShapeType::Subject);
        overlay.add_source(&outer, crate::core::overlay::ShapeType::Clip);
        assert!(overlay.interiors_intersect());

        overlay.clear();
        overlay.add_source(&inner, crate::core::overlay::ShapeType::Subject);
        overlay.add_source(&outer, crate::core::overlay::ShapeType::Clip);
        assert!(!overlay.touches());

        overlay.clear();
        overlay.add_source(&inner, crate::core::overlay::ShapeType::Subject);
        overlay.add_source(&outer, crate::core::overlay::ShapeType::Clip);
        assert!(overlay.within());
    }

    #[test]
    fn test_point_intersects_trait() {
        // Two squares touching at a single corner point (10, 10)
        let square1 = vec![[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [10.0, 0.0]];
        let square2 = vec![[10.0, 10.0], [10.0, 20.0], [20.0, 20.0], [20.0, 10.0]];

        // Point-only intersection → true
        assert!(square1.point_intersects(&square2));
        assert!(square2.point_intersects(&square1));

        // Edge-sharing squares (not point-only)
        let square3 = vec![[10.0, 0.0], [10.0, 10.0], [20.0, 10.0], [20.0, 0.0]];
        assert!(
            !square1.point_intersects(&square3),
            "edge sharing is not point-only"
        );
        // But they do touch
        assert!(square1.touches(&square3));

        // Overlapping squares (not point-only)
        let square4 = vec![[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0]];
        assert!(
            !square1.point_intersects(&square4),
            "overlapping is not point-only"
        );

        // Disjoint squares (no intersection)
        let square5 = vec![[100.0, 100.0], [100.0, 110.0], [110.0, 110.0], [110.0, 100.0]];
        assert!(
            !square1.point_intersects(&square5),
            "disjoint has no point intersection"
        );
    }
}