i_curve 0.1.2

Boolean operations on closed paths of lines, Bezier curves, and rational elliptic arcs
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
use crate::int::CURVE_COORDINATE_SAFETY_BITS;
use crate::int::CurveInt;
use crate::int::bool::approximate::CurveApproximator;
use crate::int::bool::bounds::CurveBoundsBuffer;
use crate::int::bool::data::{CurveEdgeData, CurveEdgeDataStore, CurveSourceSpan};
use crate::int::bool::edge::CurveEdge;
use crate::int::bool::planarize::CurvePlanarizer;
use crate::int::bool::recompose::CurveRecomposer;
use crate::int::bool::refine::CurveContainmentRefiner;
use crate::int::bool::source::{CurveId, CurveSource};
use crate::int::curve::shape::CurveShape;
use crate::kernel::int::curve::arc::RationalArcError;
use crate::kernel::int::curve::chord::Chord;
use crate::kernel::int::normalization::canonical::{PushCanonicalSimpleParametricSegment, PushSimpleSegment};
use alloc::vec::Vec;
use i_overlay::core::edge_overlay::{EdgeOverlay, InputEdge};
use i_overlay::core::fill_rule::FillRule;
use i_overlay::core::overlay::ShapeType;
use i_overlay::core::overlay_rule::OverlayRule;
use i_overlay::core::solver::Solver;
use i_overlay::i_float::int::number::wide_int::WideIntNumber;
use i_overlay::vector::edge::DataVectorShape;

/// Structural error in an integer curve input.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CurveInputError {
    /// A shape has no contours.
    EmptyShape,
    /// A contour has no segments.
    EmptyContour {
        /// Zero-based contour index within the rejected shape.
        contour: usize,
    },
    /// The last endpoint of a contour does not equal its start point.
    UnclosedContour {
        /// Zero-based contour index within the rejected shape.
        contour: usize,
    },
    /// A rational arc does not start at the preceding segment endpoint.
    DisconnectedArc {
        /// Zero-based contour index within the rejected shape.
        contour: usize,
        /// Zero-based segment index within the rejected contour.
        segment: usize,
    },
    /// A rational arc violates an integer kernel invariant.
    InvalidArc {
        /// Zero-based contour index within the rejected shape.
        contour: usize,
        /// Zero-based segment index within the rejected contour.
        segment: usize,
        /// Specific invalid arc invariant.
        error: RationalArcError,
    },
}

impl core::fmt::Display for CurveInputError {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match *self {
            Self::EmptyShape => formatter.write_str("curve shape has no contours"),
            Self::EmptyContour { contour } => {
                write!(formatter, "curve contour {contour} has no segments")
            }
            Self::UnclosedContour { contour } => {
                write!(formatter, "curve contour {contour} is not closed")
            }
            Self::DisconnectedArc { contour, segment } => write!(
                formatter,
                "rational arc {segment} in contour {contour} is disconnected"
            ),
            Self::InvalidArc {
                contour,
                segment,
                error: _,
            } => write!(
                formatter,
                "rational arc {segment} in contour {contour} is invalid"
            ),
        }
    }
}

impl core::error::Error for CurveInputError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::InvalidArc { error, .. } => Some(error),
            _ => None,
        }
    }
}

/// Invalid [`CurveOverlayOptions`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CurveOverlayOptionsError {
    /// The requested subdivision limit exceeds the library safety ceiling.
    MaxApproximationDepthTooLarge {
        /// Value supplied by the caller.
        requested: u32,
        /// Largest value accepted by this version of the library.
        maximum: u32,
    },
    /// The requested refinement subdivision power exceeds the safety ceiling.
    RefinementSubdivisionPowerTooLarge {
        /// Value supplied by the caller.
        requested: u32,
        /// Largest value accepted by this version of the library.
        maximum: u32,
    },
    /// The requested refinement iteration count exceeds the safety ceiling.
    MaxRefinementIterationsTooLarge {
        /// Value supplied by the caller.
        requested: u32,
        /// Largest value accepted by this version of the library.
        maximum: u32,
    },
}

impl core::fmt::Display for CurveOverlayOptionsError {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match *self {
            Self::MaxApproximationDepthTooLarge { requested, maximum } => write!(
                formatter,
                "maximum approximation depth {requested} exceeds the safety limit {maximum}"
            ),
            Self::RefinementSubdivisionPowerTooLarge { requested, maximum } => write!(
                formatter,
                "refinement subdivision power {requested} exceeds the safety limit {maximum}"
            ),
            Self::MaxRefinementIterationsTooLarge { requested, maximum } => write!(
                formatter,
                "maximum refinement iteration count {requested} exceeds the safety limit {maximum}"
            ),
        }
    }
}

impl core::error::Error for CurveOverlayOptionsError {}

/// Controls the chord approximation used to determine boolean topology.
///
/// These values are expressed in the integer coordinate system. The default
/// is intended for general-purpose input; change it only after selecting the
/// integer or float conversion scale deliberately.
///
/// Construct this non-exhaustive configuration from [`Default`] and override
/// only the values your application needs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct CurveOverlayOptions {
    /// Chords shorter than `2^min_chord_length_power` are accepted without
    /// further approximation subdivision.
    pub min_chord_length_power: u32,
    /// A segment is accepted when its endpoint handles are collinear with its
    /// chord within a sine tolerance of approximately `2^-angle_tolerance_power`.
    pub angle_tolerance_power: u32,
    /// Hard safety limit for local approximation subdivision. Values above
    /// [`MAX_APPROXIMATION_DEPTH`](Self::MAX_APPROXIMATION_DEPTH) are rejected.
    pub max_approximation_depth: u32,
    /// Power-of-two subdivision applied to a curve piece whose convex hull
    /// contains another chord endpoint. The default value of `3` produces
    /// eight pieces.
    pub refinement_subdivision_power: u32,
    /// A curve piece flatter than the sine tolerance
    /// `2^-refinement_angle_tolerance_power` is not containment-refined. The
    /// default value is `8`.
    pub refinement_angle_tolerance_power: u32,
    /// Maximum number of containment-refinement passes. Zero disables the
    /// additional refinement stage.
    pub max_refinement_iterations: u32,
}

impl Default for CurveOverlayOptions {
    fn default() -> Self {
        Self {
            min_chord_length_power: 4,
            angle_tolerance_power: 3,
            max_approximation_depth: Self::MAX_APPROXIMATION_DEPTH,
            refinement_subdivision_power: 3,
            refinement_angle_tolerance_power: 8,
            max_refinement_iterations: 2,
        }
    }
}

impl CurveOverlayOptions {
    /// Absolute safety ceiling for local approximation subdivision.
    pub const MAX_APPROXIMATION_DEPTH: u32 = 16;
    /// Absolute safety ceiling for the power-of-two refinement subdivision.
    pub const MAX_REFINEMENT_SUBDIVISION_POWER: u32 = 4;
    /// Absolute safety ceiling for containment-refinement passes.
    pub const MAX_REFINEMENT_ITERATIONS: u32 = 4;

    /// Sets the minimum accepted chord length power.
    #[must_use]
    pub const fn with_min_chord_length_power(mut self, power: u32) -> Self {
        self.min_chord_length_power = power;
        self
    }

    /// Sets the angle tolerance power.
    #[must_use]
    pub const fn with_angle_tolerance_power(mut self, power: u32) -> Self {
        self.angle_tolerance_power = power;
        self
    }

    /// Sets the local approximation subdivision limit.
    #[must_use]
    pub const fn with_max_approximation_depth(mut self, depth: u32) -> Self {
        self.max_approximation_depth = depth;
        self
    }

    /// Sets the power-of-two subdivision used by containment refinement.
    #[must_use]
    pub const fn with_refinement_subdivision_power(mut self, power: u32) -> Self {
        self.refinement_subdivision_power = power;
        self
    }

    /// Sets the near-linear angle threshold used by containment refinement.
    #[must_use]
    pub const fn with_refinement_angle_tolerance_power(mut self, power: u32) -> Self {
        self.refinement_angle_tolerance_power = power;
        self
    }

    /// Sets the maximum number of containment-refinement passes.
    #[must_use]
    pub const fn with_max_refinement_iterations(mut self, iterations: u32) -> Self {
        self.max_refinement_iterations = iterations;
        self
    }

    /// Validates the computational safety limits of this configuration.
    pub fn validate(&self) -> Result<(), CurveOverlayOptionsError> {
        if self.max_approximation_depth > Self::MAX_APPROXIMATION_DEPTH {
            return Err(CurveOverlayOptionsError::MaxApproximationDepthTooLarge {
                requested: self.max_approximation_depth,
                maximum: Self::MAX_APPROXIMATION_DEPTH,
            });
        }
        if self.refinement_subdivision_power > Self::MAX_REFINEMENT_SUBDIVISION_POWER {
            return Err(CurveOverlayOptionsError::RefinementSubdivisionPowerTooLarge {
                requested: self.refinement_subdivision_power,
                maximum: Self::MAX_REFINEMENT_SUBDIVISION_POWER,
            });
        }
        if self.max_refinement_iterations > Self::MAX_REFINEMENT_ITERATIONS {
            return Err(CurveOverlayOptionsError::MaxRefinementIterationsTooLarge {
                requested: self.max_refinement_iterations,
                maximum: Self::MAX_REFINEMENT_ITERATIONS,
            });
        }

        Ok(())
    }
}

/// Incremental Boolean overlay for integer curve shapes.
///
/// Shapes are validated when passed to [`add_subject`](Self::add_subject),
/// [`add_clip`](Self::add_clip), or [`add_shape`](Self::add_shape). Add any
/// number of inputs, configure the solver and approximation, then consume the
/// builder with [`overlay`](Self::overlay).
pub struct IntCurveOverlay<I: CurveInt> {
    solver: Solver,
    options: CurveOverlayOptions,
    pub(crate) curve_sources: Vec<CurveSource<I>>,
    pub(crate) curve_edges: Vec<CurveEdge<I>>,
}

impl<I: CurveInt> IntCurveOverlay<I> {
    /// Creates an empty overlay.
    pub fn new() -> Self {
        Self::with_capacity(0)
    }

    /// Creates an empty overlay with an input-segment allocation hint.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            solver: Solver::default(),
            options: CurveOverlayOptions::default(),
            curve_sources: Vec::with_capacity(capacity),
            curve_edges: Vec::with_capacity(capacity),
        }
    }

    /// Sets the polygon solver strategy and precision.
    #[must_use]
    pub fn with_solver(mut self, solver: Solver) -> Self {
        self.solver = solver;
        self
    }

    /// Validates and sets the curve approximation options.
    pub fn try_with_options(
        mut self,
        options: CurveOverlayOptions,
    ) -> Result<Self, CurveOverlayOptionsError> {
        options.validate()?;
        self.options = options;
        Ok(self)
    }

    /// Returns the polygon solver configuration.
    #[inline]
    pub fn solver(&self) -> Solver {
        self.solver
    }

    /// Returns the curve approximation configuration.
    #[inline]
    pub fn options(&self) -> CurveOverlayOptions {
        self.options
    }

    /// Adds a subject shape.
    #[inline]
    pub fn add_subject(&mut self, shape: CurveShape<I>) -> Result<(), CurveInputError> {
        self.add_shape(shape, ShapeType::Subject)
    }

    /// Adds a clip shape.
    #[inline]
    pub fn add_clip(&mut self, shape: CurveShape<I>) -> Result<(), CurveInputError> {
        self.add_shape(shape, ShapeType::Clip)
    }

    /// Validates and adds a shape as a subject or clip operand.
    pub fn add_shape(&mut self, shape: CurveShape<I>, shape_type: ShapeType) -> Result<(), CurveInputError> {
        validate_shape(&shape)?;
        let mut simple_curves = Vec::new();
        let mut canonical_curves = Vec::new();

        for contour in shape.contours {
            let mut current = contour.start;

            for segment in contour.segments {
                let (curve, end) = segment.into_kernel_segment(current);
                simple_curves.clear();
                simple_curves.push_simple(curve);

                for simple_curve in simple_curves.drain(..) {
                    let curve_id = CurveId(self.curve_sources.len());
                    canonical_curves.clear();
                    canonical_curves.push_canonical_simple_parametric(simple_curve);

                    self.curve_sources
                        .push(CurveSource::new(simple_curve, shape_type));

                    self.curve_edges
                        .extend(canonical_curves.drain(..).map(|canonical| {
                            CurveEdge::new(canonical.curve, curve_id, canonical.start, canonical.end)
                        }));
                }

                current = end;
            }
        }

        Ok(())
    }

    fn prepare(&mut self) {
        // Build a bounded local chord approximation, run one curve-aware
        // planarization pass, then apply bounded containment refinement.
        CurveApproximator::new().approximate(&mut self.curve_edges, self.options);

        let mut bounds = CurveBoundsBuffer::new();
        let mut planarizer = CurvePlanarizer::new();
        let cross_radius = self.initial_snap_radius();
        planarizer.planarize(&mut self.curve_edges, cross_radius, &mut bounds);
        CurveContainmentRefiner::new().refine(
            &mut self.curve_edges,
            self.options.refinement_subdivision_power,
            self.options.max_refinement_iterations,
            self.options.min_chord_length_power,
            self.options.refinement_angle_tolerance_power,
            &mut bounds,
        );
    }

    #[inline]
    fn initial_snap_radius(&self) -> I::Wide {
        let coordinate_bits = I::BITS - CURVE_COORDINATE_SAFETY_BITS;
        let max_exponent = 2 * coordinate_bits;
        I::Wide::ONE << (self.solver.precision.start as u32).min(max_exponent)
    }

    fn build_vector_shapes(
        &self,
        overlay_rule: OverlayRule,
        fill_rule: FillRule,
    ) -> (Vec<DataVectorShape<I, CurveEdgeData>>, CurveEdgeDataStore) {
        let mut edge_overlay = EdgeOverlay::new(self.curve_edges.len());
        edge_overlay.solver = self.solver;

        for edge in &self.curve_edges {
            let chord = edge.curve.chord();
            let curve_source = &self.curve_sources[edge.curve_id.0];
            edge_overlay.add_edge(
                InputEdge {
                    a: chord.a,
                    b: chord.b,
                    data: CurveEdgeData::Single(CurveSourceSpan::from_edge(*edge)),
                },
                curve_source.shape_type,
            );
        }

        let shapes = edge_overlay.build_vector_shapes(overlay_rule, fill_rule);
        let data_store = edge_overlay.into_data_store();

        (shapes, data_store)
    }

    /// Resolves the configured Boolean operation and returns reconstructed curves.
    #[inline]
    pub fn overlay(mut self, overlay_rule: OverlayRule, fill_rule: FillRule) -> Vec<CurveShape<I>> {
        self.prepare();

        // Resolve the boolean topology while preserving CurveId provenance.
        let (vector_shapes, data_store) = self.build_vector_shapes(overlay_rule, fill_rule);

        // Restore maximal runs from their source curves and parameter spans.
        CurveRecomposer::new().recompose(vector_shapes, &data_store, &self.curve_sources)
    }
}

impl<I: CurveInt> Default for IntCurveOverlay<I> {
    fn default() -> Self {
        Self::new()
    }
}

/// Performs a boolean operation on one subject and one clip shape.
///
/// Use [`IntCurveOverlay`] when an operation has more inputs or needs custom
/// approximation or polygon solver options.
pub fn overlay<I>(
    subject: CurveShape<I>,
    clip: CurveShape<I>,
    overlay_rule: OverlayRule,
    fill_rule: FillRule,
) -> Result<Vec<CurveShape<I>>, CurveInputError>
where
    I: CurveInt,
{
    let capacity = segment_count(&subject).saturating_add(segment_count(&clip));
    let mut overlay = IntCurveOverlay::with_capacity(capacity);
    overlay.add_subject(subject)?;
    overlay.add_clip(clip)?;
    Ok(overlay.overlay(overlay_rule, fill_rule))
}

fn segment_count<I: CurveInt>(shape: &CurveShape<I>) -> usize {
    shape.contours.iter().map(|contour| contour.segments.len()).sum()
}

pub(crate) fn validate_shape<I: CurveInt>(shape: &CurveShape<I>) -> Result<(), CurveInputError> {
    if shape.contours.is_empty() {
        return Err(CurveInputError::EmptyShape);
    }

    for (contour_index, contour) in shape.contours.iter().enumerate() {
        if contour.segments.is_empty() {
            return Err(CurveInputError::EmptyContour {
                contour: contour_index,
            });
        }

        let mut current = contour.start;
        for (segment_index, segment) in contour.segments.iter().enumerate() {
            current = match segment {
                crate::int::curve::segment::CurveSegment::Line { to }
                | crate::int::curve::segment::CurveSegment::Quad { to, .. }
                | crate::int::curve::segment::CurveSegment::Cubic { to, .. } => *to,
                crate::int::curve::segment::CurveSegment::Arc { arc } => {
                    if arc.control_points[0] != current {
                        return Err(CurveInputError::DisconnectedArc {
                            contour: contour_index,
                            segment: segment_index,
                        });
                    }
                    arc.validate().map_err(|error| CurveInputError::InvalidArc {
                        contour: contour_index,
                        segment: segment_index,
                        error,
                    })?;
                    arc.control_points[2]
                }
            };
        }

        if current != contour.start {
            return Err(CurveInputError::UnclosedContour {
                contour: contour_index,
            });
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::int::curve::path::CurvePath;
    use crate::int::curve::segment::CurveSegment;
    use crate::kernel::int::curve::arc::{ArcDirection, ArcPhase, ArcSegment, ArcVector, EllipseFrame};
    use crate::kernel::int::curve::segment::Segment;
    use alloc::vec;
    use i_overlay::i_float::int::number::fixed_scale::FixedScale;
    use i_overlay::i_shape::int::IntPoint;

    fn circle(center: IntPoint<i32>) -> CurveShape<i32> {
        let one = FixedScale::<i32>::DENOMINATOR as i32;
        let ellipse = EllipseFrame {
            center,
            axis_x: ArcVector { x: 100, y: 0 },
            axis_y: ArcVector { x: 0, y: 100 },
        };
        let phases = [
            ArcPhase { cos: one, sin: 0 },
            ArcPhase { cos: 0, sin: one },
            ArcPhase { cos: -one, sin: 0 },
            ArcPhase { cos: 0, sin: -one },
            ArcPhase { cos: one, sin: 0 },
        ];
        let points = [
            IntPoint::new(center.x + 100, center.y),
            IntPoint::new(center.x, center.y + 100),
            IntPoint::new(center.x - 100, center.y),
            IntPoint::new(center.x, center.y - 100),
            IntPoint::new(center.x + 100, center.y),
        ];
        let controls = [
            IntPoint::new(center.x + 100, center.y + 100),
            IntPoint::new(center.x - 100, center.y + 100),
            IntPoint::new(center.x - 100, center.y - 100),
            IntPoint::new(center.x + 100, center.y - 100),
        ];
        let segments = (0..4)
            .map(|index| CurveSegment::Arc {
                arc: ArcSegment {
                    ellipse,
                    control_points: [points[index], controls[index], points[index + 1]],
                    weights: [one, 759_250_125, one],
                    start_phase: phases[index],
                    end_phase: phases[index + 1],
                    direction: ArcDirection::CounterClockwise,
                },
            })
            .collect();

        CurveShape {
            contours: vec![CurvePath {
                start: points[0],
                segments,
            }],
        }
    }

    #[test]
    fn add_shape_builds_curve_edges_from_contour_start() {
        let p0 = IntPoint::new(2, 3);
        let p1 = IntPoint::new(8, 5);
        let shape = CurveShape {
            contours: vec![CurvePath {
                start: p0,
                segments: vec![CurveSegment::Line { to: p1 }, CurveSegment::Line { to: p0 }],
            }],
        };
        let mut overlay = IntCurveOverlay {
            solver: Solver::default(),
            options: CurveOverlayOptions::default(),
            curve_sources: Vec::new(),
            curve_edges: Vec::new(),
        };

        overlay.add_shape(shape, ShapeType::Subject).unwrap();

        assert_eq!(overlay.curve_sources.len(), 2);
        assert_eq!(overlay.curve_edges.len(), 2);
        assert_eq!(overlay.curve_edges[0].curve_id, CurveId(0));
        assert_eq!(overlay.curve_edges[1].curve_id, CurveId(1));
        assert_eq!(overlay.curve_sources[0].shape_type, ShapeType::Subject);
        match overlay.curve_edges[0].curve {
            Segment::Line(line) => assert_eq!(line.control_points, [p0, p1]),
            _ => panic!("expected line segment"),
        }
        match overlay.curve_edges[1].curve {
            Segment::Line(line) => assert_eq!(line.control_points, [p1, p0]),
            _ => panic!("expected line segment"),
        }
    }

    #[test]
    fn add_shape_canonicalizes_segments_and_preserves_shape_type() {
        let p0 = IntPoint::new(0, 0);
        let p1 = IntPoint::new(4, 0);
        let shape = CurveShape {
            contours: vec![CurvePath {
                start: p0,
                segments: vec![
                    CurveSegment::Line { to: p0 },
                    CurveSegment::Quad {
                        ctrl: IntPoint::new(2, 0),
                        to: p1,
                    },
                    CurveSegment::Line { to: p0 },
                ],
            }],
        };
        let mut overlay = IntCurveOverlay {
            solver: Solver::default(),
            options: CurveOverlayOptions::default(),
            curve_sources: Vec::new(),
            curve_edges: Vec::new(),
        };

        overlay.add_shape(shape, ShapeType::Clip).unwrap();

        assert_eq!(overlay.curve_sources.len(), 2);
        assert_eq!(overlay.curve_edges.len(), 2);
        for edge in &overlay.curve_edges {
            assert_eq!(overlay.curve_sources[edge.curve_id.0].shape_type, ShapeType::Clip);
            assert!(matches!(edge.curve, Segment::Line(_)));
        }
    }

    #[test]
    fn canonical_edges_keep_their_simple_curve_id() {
        let p0 = IntPoint::new(0, 0);
        let p1 = IntPoint::new(0, -2);
        let shape = CurveShape {
            contours: vec![CurvePath {
                start: p0,
                segments: vec![
                    CurveSegment::Quad {
                        ctrl: IntPoint::new(2, 1),
                        to: p1,
                    },
                    CurveSegment::Line { to: p0 },
                ],
            }],
        };
        let mut overlay = IntCurveOverlay {
            solver: Solver::default(),
            options: CurveOverlayOptions::default(),
            curve_sources: Vec::new(),
            curve_edges: Vec::new(),
        };

        overlay.add_shape(shape, ShapeType::Subject).unwrap();

        assert_eq!(overlay.curve_sources.len(), 2);
        let quad_edges: Vec<_> = overlay
            .curve_edges
            .iter()
            .filter(|edge| edge.curve_id == CurveId(0))
            .collect();
        assert_eq!(quad_edges.len(), 2);
        assert!(
            quad_edges
                .iter()
                .all(|edge| matches!(edge.curve, Segment::Quad(_)))
        );
        assert_eq!(quad_edges[0].start_param.value(), 0_i64);
        assert!(quad_edges[0].end_param.value() <= quad_edges[1].start_param.value());
        assert_eq!(
            quad_edges[1].end_param.value(),
            crate::kernel::int::curve::param::SegmentParam::<i32>::DENOMINATOR
        );
        assert_eq!(overlay.curve_edges.last().unwrap().curve_id, CurveId(1));
    }

    #[test]
    fn cusp_pieces_get_distinct_curve_ids() {
        let p0 = IntPoint::new(0, 0);
        let p3 = IntPoint::new(100, 0);
        let shape = CurveShape {
            contours: vec![CurvePath {
                start: p0,
                segments: vec![
                    CurveSegment::Cubic {
                        ctrl0: IntPoint::new(100, 100),
                        ctrl1: IntPoint::new(0, 100),
                        to: p3,
                    },
                    CurveSegment::Line { to: p0 },
                ],
            }],
        };
        let mut overlay = IntCurveOverlay {
            solver: Solver::default(),
            options: CurveOverlayOptions::default(),
            curve_sources: Vec::new(),
            curve_edges: Vec::new(),
        };

        overlay.add_shape(shape, ShapeType::Subject).unwrap();

        assert_eq!(overlay.curve_sources.len(), 3);
        assert!(matches!(overlay.curve_sources[0].curve, Segment::Cubic(_)));
        assert!(matches!(overlay.curve_sources[1].curve, Segment::Cubic(_)));
        assert!(matches!(overlay.curve_sources[2].curve, Segment::Line(_)));
        for id in 0..overlay.curve_sources.len() {
            assert!(
                overlay
                    .curve_edges
                    .iter()
                    .any(|edge| edge.curve_id == CurveId(id))
            );
        }
    }

    #[test]
    fn self_intersection_pieces_get_distinct_curve_ids() {
        let p0 = IntPoint::new(0, 0);
        let p3 = IntPoint::new(-14, -14);
        let shape = CurveShape {
            contours: vec![CurvePath {
                start: p0,
                segments: vec![
                    CurveSegment::Cubic {
                        ctrl0: IntPoint::new(-21, -21),
                        ctrl1: IntPoint::new(-21, -14),
                        to: p3,
                    },
                    CurveSegment::Line { to: p0 },
                ],
            }],
        };
        let mut overlay = IntCurveOverlay {
            solver: Solver::default(),
            options: CurveOverlayOptions::default(),
            curve_sources: Vec::new(),
            curve_edges: Vec::new(),
        };

        overlay.add_shape(shape, ShapeType::Clip).unwrap();

        assert_eq!(overlay.curve_sources.len(), 3);
        assert!(matches!(overlay.curve_sources[2].curve, Segment::Line(_)));
        for id in 0..overlay.curve_sources.len() {
            assert!(
                overlay
                    .curve_edges
                    .iter()
                    .any(|edge| edge.curve_id == CurveId(id))
            );
        }
    }

    #[test]
    fn containment_refine_keeps_identical_operands_equivalent() {
        let start = IntPoint::new(67, 142);
        let shape = CurveShape {
            contours: vec![CurvePath {
                start,
                segments: vec![
                    CurveSegment::Quad {
                        ctrl: IntPoint::new(-833, -1500),
                        to: IntPoint::new(-833, -1500),
                    },
                    CurveSegment::Quad {
                        ctrl: IntPoint::new(2, -2),
                        to: IntPoint::new(-831, -1499),
                    },
                    CurveSegment::Cubic {
                        ctrl0: IntPoint::new(67_078, -313_947),
                        ctrl1: IntPoint::new(-381, -671),
                        to: start,
                    },
                ],
            }],
        };
        let mut overlay = IntCurveOverlay::new();
        overlay.add_subject(shape.clone()).unwrap();
        overlay.add_clip(shape).unwrap();

        let result = overlay.overlay(OverlayRule::Difference, FillRule::NonZero);

        assert!(result.is_empty(), "identical operands produced {result:#?}");
    }

    #[test]
    fn path_segments_restore_all_control_points() {
        let p0 = IntPoint::new(1, 2);
        let p1 = IntPoint::new(3, 4);
        let p2 = IntPoint::new(5, 6);
        let p3 = IntPoint::new(7, 8);

        let (quad, end) = CurveSegment::Quad { ctrl: p1, to: p2 }.into_kernel_segment(p0);
        assert_eq!(end, p2);
        match quad {
            Segment::Quad(quad) => assert_eq!(quad.control_points, [p0, p1, p2]),
            _ => panic!("expected quad segment"),
        }

        let (cubic, end) = CurveSegment::Cubic {
            ctrl0: p1,
            ctrl1: p2,
            to: p3,
        }
        .into_kernel_segment(p0);
        assert_eq!(end, p3);
        match cubic {
            Segment::Cubic(cubic) => assert_eq!(cubic.control_points, [p0, p1, p2, p3]),
            _ => panic!("expected cubic segment"),
        }
    }

    #[test]
    fn coincident_chords_merge_curve_ids_in_edge_overlay() {
        fn square() -> CurveShape<i32> {
            let p0 = IntPoint::new(0, 0);
            CurveShape {
                contours: vec![CurvePath {
                    start: p0,
                    segments: vec![
                        CurveSegment::Line {
                            to: IntPoint::new(10, 0),
                        },
                        CurveSegment::Line {
                            to: IntPoint::new(10, 10),
                        },
                        CurveSegment::Line {
                            to: IntPoint::new(0, 10),
                        },
                        CurveSegment::Line { to: p0 },
                    ],
                }],
            }
        }

        let mut overlay = IntCurveOverlay {
            solver: Solver::default(),
            options: CurveOverlayOptions::default(),
            curve_sources: Vec::new(),
            curve_edges: Vec::new(),
        };
        overlay.add_shape(square(), ShapeType::Subject).unwrap();
        overlay.add_shape(square(), ShapeType::Clip).unwrap();
        overlay.prepare();

        let (shapes, store) = overlay.build_vector_shapes(OverlayRule::Intersect, FillRule::NonZero);

        assert_eq!(shapes.len(), 1);
        assert_eq!(shapes[0].len(), 1);
        assert_eq!(shapes[0][0].len(), 4);

        let mut spans = Vec::new();
        for edge in &shapes[0][0] {
            store.spans(edge.data, &mut spans);
            assert_eq!(spans.len(), 2);
            assert_ne!(spans[0].curve_id, spans[1].curve_id);

            let first_type = overlay.curve_sources[spans[0].curve_id.0].shape_type;
            let second_type = overlay.curve_sources[spans[1].curve_id.0].shape_type;
            assert_ne!(first_type, second_type);
        }

        let result = CurveRecomposer::new().recompose(shapes, &store, &overlay.curve_sources);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].contours.len(), 1);
        assert_eq!(result[0].contours[0].segments.len(), 4);

        let public_result = overlay.overlay(OverlayRule::Intersect, FillRule::NonZero);
        assert_eq!(public_result.len(), 1);
        assert_eq!(public_result[0].contours[0].segments.len(), 4);
    }

    #[test]
    fn identical_circles_survive_boolean_intersection_as_arcs() {
        let mut overlay = IntCurveOverlay::with_capacity(8);
        overlay
            .add_shape(circle(IntPoint::new(0, 0)), ShapeType::Subject)
            .unwrap();
        overlay
            .add_shape(circle(IntPoint::new(0, 0)), ShapeType::Clip)
            .unwrap();

        let result = overlay.overlay(OverlayRule::Intersect, FillRule::NonZero);

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].contours.len(), 1);
        assert_eq!(result[0].contours[0].segments.len(), 4);
        assert!(
            result[0].contours[0]
                .segments
                .iter()
                .all(|segment| matches!(segment, CurveSegment::Arc { .. }))
        );
    }

    #[test]
    fn overlapping_circles_recompose_split_boundaries_as_arcs() {
        let mut overlay = IntCurveOverlay::with_capacity(8);
        overlay
            .add_shape(circle(IntPoint::new(0, 0)), ShapeType::Subject)
            .unwrap();
        overlay
            .add_shape(circle(IntPoint::new(100, 0)), ShapeType::Clip)
            .unwrap();

        let result = overlay.overlay(OverlayRule::Intersect, FillRule::NonZero);

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].contours.len(), 1);
        assert_eq!(result[0].contours[0].segments.len(), 4);
        assert!(
            result[0].contours[0]
                .segments
                .iter()
                .all(|segment| matches!(segment, CurveSegment::Arc { .. }))
        );
    }

    #[test]
    fn with_solver_preserves_precision_settings() {
        use i_overlay::core::solver::Precision;

        let solver = Solver::with_precision(Precision::LOW);
        let overlay = IntCurveOverlay::<i32>::with_capacity(16).with_solver(solver);

        assert_eq!(overlay.solver.precision, Precision::LOW);
        assert_eq!(overlay.curve_edges.capacity(), 16);
        assert_eq!(overlay.curve_sources.capacity(), 16);
    }

    #[test]
    fn try_with_options_preserves_approximation_settings() {
        let options = CurveOverlayOptions {
            min_chord_length_power: 6,
            angle_tolerance_power: 5,
            max_approximation_depth: 12,
            ..Default::default()
        };

        let overlay = IntCurveOverlay::<i32>::with_capacity(4)
            .try_with_options(options)
            .unwrap();

        assert_eq!(overlay.options, options);
        assert_eq!(overlay.curve_edges.capacity(), 4);
    }
}