hypercurve 0.3.0

Hyperreal-backed planar curves, contours, and regions for CAD topology
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
//! Directed boolean boundary traversal and loop reconstruction.
//!
//! This module owns the graph-facing part of boolean construction: selected
//! fragments are already classified, oriented, and ready to be connected into
//! chains. It deliberately stops before material/hole role assignment.

use crate::boolean::{
    BooleanFragmentClassification, validate_boolean_fragment_classification_boundary_action,
};
use crate::classify::is_zero;
use crate::{
    Classification, Contour2, CurveError, CurvePolicy, CurveResult, FillRule, RegionContourKey,
    RegionContourRole, RegionSide, Segment2,
};

/// A selected fragment with geometry already oriented for result traversal.
#[derive(Clone, Debug, PartialEq)]
pub struct DirectedBooleanFragment {
    /// Source keyed contour.
    pub key: crate::RegionContourKey,
    /// Index within [`crate::RegionContourFragments::fragments`].
    pub fragment_index: usize,
    /// Segment geometry in result traversal direction.
    pub segment: Segment2,
}

/// Boundary fragments selected by a boolean operation.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BooleanBoundaryFragmentSet {
    directed_fragments: Vec<DirectedBooleanFragment>,
    unresolved_boundaries: Vec<BooleanFragmentClassification>,
}

impl BooleanBoundaryFragmentSet {
    /// Constructs a boundary-fragment set from preclassified pieces.
    pub fn new(
        directed_fragments: Vec<DirectedBooleanFragment>,
        unresolved_boundaries: Vec<BooleanFragmentClassification>,
    ) -> CurveResult<Self> {
        validate_boolean_boundary_fragment_set(&directed_fragments, &unresolved_boundaries)?;
        Ok(Self {
            directed_fragments,
            unresolved_boundaries,
        })
    }

    /// Returns fragments that can be passed to graph traversal immediately.
    pub fn directed_fragments(&self) -> &[DirectedBooleanFragment] {
        &self.directed_fragments
    }

    /// Returns shared-boundary fragments that still need overlap resolution.
    pub fn unresolved_boundaries(&self) -> &[BooleanFragmentClassification] {
        &self.unresolved_boundaries
    }

    /// Returns true when no directed fragments or unresolved fragments exist.
    pub fn is_empty(&self) -> bool {
        self.directed_fragments.is_empty() && self.unresolved_boundaries.is_empty()
    }

    /// Returns true when this set contains no unresolved shared-boundary work.
    pub fn is_ready_for_traversal(&self) -> bool {
        self.unresolved_boundaries.is_empty()
    }

    /// Number of immediately directed fragments.
    pub fn directed_len(&self) -> usize {
        self.directed_fragments.len()
    }

    /// Number of unresolved shared-boundary fragments.
    pub fn unresolved_len(&self) -> usize {
        self.unresolved_boundaries.len()
    }

    /// Assembles directed boundary fragments into endpoint-connected chains.
    ///
    /// This is the first graph-traversal scaffold, not final loop extraction.
    /// It requires every directed fragment endpoint to have at most one outgoing
    /// and one incoming neighbor. That mirrors the regularized traversal graph
    /// assumed after polygon clipping has inserted and classified intersections
    /// (G. Greiner and K. Hormann, "Efficient clipping of arbitrary polygons,"
    /// ACM Transactions on Graphics 17(2), 71-83, 1998). Branch points and
    /// unresolved overlaps are intentionally returned as uncertainty here
    /// because Vatti-style scanline algorithms resolve those cases with fill
    /// state and event ordering, not by choosing an arbitrary local successor
    /// (B. R. Vatti, "A generic solution to polygon clipping," Communications
    /// of the ACM 35(7), 56-63, 1992).
    pub fn assemble_chains(&self, policy: &CurvePolicy) -> Classification<BooleanBoundaryChainSet> {
        if !self.unresolved_boundaries.is_empty() {
            return Classification::Uncertain(crate::UncertaintyReason::Boundary);
        }

        let (successors, predecessors) = match endpoint_adjacency(&self.directed_fragments, policy)
        {
            Classification::Decided(adjacency) => adjacency,
            Classification::Uncertain(reason) => return Classification::Uncertain(reason),
        };

        let mut used = vec![false; self.directed_fragments.len()];
        let mut chains = Vec::new();

        for index in 0..self.directed_fragments.len() {
            if predecessors[index].is_none() && !used[index] {
                let chain =
                    match follow_chain(index, &self.directed_fragments, &successors, &mut used) {
                        Classification::Decided(chain) => chain,
                        Classification::Uncertain(reason) => {
                            return Classification::Uncertain(reason);
                        }
                    };
                chains.push(chain);
            }
        }

        for index in 0..self.directed_fragments.len() {
            if !used[index] {
                let chain =
                    match follow_chain(index, &self.directed_fragments, &successors, &mut used) {
                        Classification::Decided(chain) => chain,
                        Classification::Uncertain(reason) => {
                            return Classification::Uncertain(reason);
                        }
                    };
                chains.push(chain);
            }
        }

        decided_boolean_boundary_chain_set(chains)
    }
}

/// One endpoint-connected directed boundary chain.
#[derive(Clone, Debug, PartialEq)]
pub struct BooleanBoundaryChain {
    fragments: Vec<DirectedBooleanFragment>,
    closed: bool,
}

impl BooleanBoundaryChain {
    /// Constructs a boundary chain from already-ordered fragments.
    pub fn new(fragments: Vec<DirectedBooleanFragment>, closed: bool) -> CurveResult<Self> {
        validate_directed_boolean_fragments(&fragments, "boolean boundary chain")?;
        validate_boolean_boundary_chain_geometry(&fragments, closed)?;
        Ok(Self { fragments, closed })
    }

    /// Returns fragments in traversal order.
    pub fn fragments(&self) -> &[DirectedBooleanFragment] {
        &self.fragments
    }

    /// Consumes the chain and returns fragments in traversal order.
    pub fn into_fragments(self) -> Vec<DirectedBooleanFragment> {
        self.fragments
    }

    /// Returns true when the chain starts and ends at the same point.
    pub const fn is_closed(&self) -> bool {
        self.closed
    }

    /// Returns true when this chain contains no fragments.
    pub fn is_empty(&self) -> bool {
        self.fragments.is_empty()
    }

    /// Returns the number of fragments in this chain.
    pub fn len(&self) -> usize {
        self.fragments.len()
    }
}

/// Endpoint-connected boundary chains.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BooleanBoundaryChainSet {
    chains: Vec<BooleanBoundaryChain>,
}

impl BooleanBoundaryChainSet {
    /// Constructs a chain set from already-assembled chains.
    pub fn new(chains: Vec<BooleanBoundaryChain>) -> CurveResult<Self> {
        validate_boolean_boundary_chains(&chains)?;
        Ok(Self { chains })
    }

    /// Returns chains in assembly order.
    pub fn chains(&self) -> &[BooleanBoundaryChain] {
        &self.chains
    }

    /// Consumes the set and returns the chains.
    pub fn into_chains(self) -> Vec<BooleanBoundaryChain> {
        self.chains
    }

    /// Returns true when no chains were assembled.
    pub fn is_empty(&self) -> bool {
        self.chains.is_empty()
    }

    /// Returns the number of assembled chains.
    pub fn len(&self) -> usize {
        self.chains.len()
    }

    /// Counts closed chains.
    pub fn closed_count(&self) -> usize {
        self.chains.iter().filter(|chain| chain.is_closed()).count()
    }

    /// Extracts closed chains as boolean boundary loops.
    ///
    /// This is intentionally only loop extraction. It does not decide which
    /// loops are material contours or holes; that nesting/role pass needs
    /// signed containment and overlap-aware traversal. Greiner and Hormann
    /// treat closed result polygons as the product of classified traversal
    /// (G. Greiner and K. Hormann, "Efficient clipping of arbitrary polygons,"
    /// ACM Transactions on Graphics 17(2), 71-83, 1998), while Vatti's
    /// scanline algorithm determines output contours from fill-state
    /// transitions (B. R. Vatti, "A generic solution to polygon clipping,"
    /// Communications of the ACM 35(7), 56-63, 1992). Keeping this conversion
    /// separate avoids assigning hole/material roles before the graph is fully
    /// resolved.
    pub fn closed_loops(&self) -> Classification<BooleanBoundaryLoopSet> {
        if self.chains.iter().any(|chain| !chain.is_closed()) {
            return Classification::Uncertain(crate::UncertaintyReason::Unsupported);
        }

        let loops = match self
            .chains
            .iter()
            .map(|chain| BooleanBoundaryLoop::new(chain.fragments.clone()))
            .collect::<CurveResult<Vec<_>>>()
        {
            Ok(loops) => loops,
            Err(_) => return Classification::Uncertain(crate::UncertaintyReason::Unsupported),
        };
        decided_boolean_boundary_loop_set(loops)
    }

    /// Consumes the chain set and extracts closed chains as boundary loops.
    pub fn into_closed_loops(self) -> Classification<BooleanBoundaryLoopSet> {
        if self.chains.iter().any(|chain| !chain.is_closed()) {
            return Classification::Uncertain(crate::UncertaintyReason::Unsupported);
        }

        let loops = match self
            .chains
            .into_iter()
            .map(|chain| BooleanBoundaryLoop::new(chain.fragments))
            .collect::<CurveResult<Vec<_>>>()
        {
            Ok(loops) => loops,
            Err(_) => return Classification::Uncertain(crate::UncertaintyReason::Unsupported),
        };
        decided_boolean_boundary_loop_set(loops)
    }
}

/// One closed boolean result boundary loop.
///
/// A loop is a stronger result than a chain: all fragments are ordered in
/// traversal direction and the final endpoint reconnects to the first start
/// point. The loop may later become either a material contour or a hole after a
/// nesting pass.
#[derive(Clone, Debug, PartialEq)]
pub struct BooleanBoundaryLoop {
    fragments: Vec<DirectedBooleanFragment>,
}

impl BooleanBoundaryLoop {
    /// Constructs a loop from already-ordered directed fragments.
    pub fn new(fragments: Vec<DirectedBooleanFragment>) -> CurveResult<Self> {
        validate_directed_boolean_fragments(&fragments, "boolean boundary loop")?;
        validate_boolean_boundary_loop_geometry(&fragments)?;
        Ok(Self { fragments })
    }

    /// Returns directed fragments in traversal order.
    pub fn fragments(&self) -> &[DirectedBooleanFragment] {
        &self.fragments
    }

    /// Consumes the loop and returns its directed fragments.
    pub fn into_fragments(self) -> Vec<DirectedBooleanFragment> {
        self.fragments
    }

    /// Returns true when this loop contains no fragments.
    pub fn is_empty(&self) -> bool {
        self.fragments.is_empty()
    }

    /// Returns the number of directed fragments in the loop.
    pub fn len(&self) -> usize {
        self.fragments.len()
    }

    /// Clones loop geometry into a checked closed contour.
    ///
    /// The checked constructor validates connectivity again instead of trusting
    /// the boolean graph. Foster, Hormann, and Popa emphasize that degenerate
    /// polygon clipping needs explicit validation around boundary coincidences
    /// (E. L. Foster, K. Hormann, and R. T. Popa, "Clipping simple polygons
    /// with degenerate intersections," Computers & Graphics: X 2, 100007,
    /// 2019), so this API keeps geometric validation visible at the conversion
    /// point.
    pub fn to_contour(&self, fill_rule: FillRule) -> CurveResult<Contour2> {
        Contour2::try_new_with_fill_rule(
            self.fragments
                .iter()
                .map(|fragment| fragment.segment.clone())
                .collect(),
            fill_rule,
        )
    }

    /// Consumes loop geometry into a checked closed contour.
    pub fn into_contour(self, fill_rule: FillRule) -> CurveResult<Contour2> {
        Contour2::try_new_with_fill_rule(
            self.fragments
                .into_iter()
                .map(|fragment| fragment.segment)
                .collect(),
            fill_rule,
        )
    }
}

/// Closed boolean boundary loops before material/hole role assignment.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct BooleanBoundaryLoopSet {
    loops: Vec<BooleanBoundaryLoop>,
}

impl BooleanBoundaryLoopSet {
    /// Constructs a loop set from already-extracted loops.
    pub fn new(loops: Vec<BooleanBoundaryLoop>) -> CurveResult<Self> {
        validate_boolean_boundary_loops(&loops)?;
        Ok(Self { loops })
    }

    /// Builds a loop set from already-decided closed contours.
    ///
    /// When higher-level boolean stages have already regularized a degenerate
    /// boundary-contact case to a set of closed contours (for example, when two
    /// boundaries share an edge that is a known full-seam overlap), the
    /// remaining work is structural transfer, not graph reconstruction. This
    /// conversion keeps the topological decision external to contour construction,
    /// matching the graph extraction model used by G. Greiner and K. Hormann while
    /// preserving the contour-only assumptions in `Contour2` as boundary facts
    /// rather than topology claims.
    ///
    /// Greiner and Hormann, "Efficient clipping of arbitrary polygons," ACM TOG 17(2),
    /// 71-83, 1998.
    pub fn from_contours(contours: Vec<Contour2>) -> CurveResult<Self> {
        let mut loops = Vec::with_capacity(contours.len());

        for (index, contour) in contours.into_iter().enumerate() {
            let fragments = contour
                .segments()
                .iter()
                .enumerate()
                .map(|(fragment_index, segment)| DirectedBooleanFragment {
                    key: RegionContourKey::new(
                        RegionSide::First,
                        RegionContourRole::Material,
                        index,
                    ),
                    fragment_index,
                    segment: segment.clone(),
                })
                .collect();
            loops.push(BooleanBoundaryLoop::new(fragments)?);
        }

        Self::new(loops)
    }

    /// Converts a decided contour set into a checked loop set while preserving
    /// upstream uncertainty.
    pub fn from_contour_classification(
        contours: Classification<Vec<Contour2>>,
    ) -> CurveResult<Classification<Self>> {
        match contours {
            Classification::Decided(contours) => {
                Self::from_contours(contours).map(Classification::Decided)
            }
            Classification::Uncertain(reason) => Ok(Classification::Uncertain(reason)),
        }
    }

    /// Returns loops in extraction order.
    pub fn loops(&self) -> &[BooleanBoundaryLoop] {
        &self.loops
    }

    /// Consumes the set and returns loops in extraction order.
    pub fn into_loops(self) -> Vec<BooleanBoundaryLoop> {
        self.loops
    }

    /// Returns true when no loops were extracted.
    pub fn is_empty(&self) -> bool {
        self.loops.is_empty()
    }

    /// Returns the number of closed boundary loops.
    pub fn len(&self) -> usize {
        self.loops.len()
    }

    /// Clones every loop into a checked closed contour.
    pub fn to_contours(&self, fill_rule: FillRule) -> CurveResult<Vec<Contour2>> {
        self.loops
            .iter()
            .map(|boundary_loop| boundary_loop.to_contour(fill_rule))
            .collect()
    }

    /// Consumes every loop into a checked closed contour.
    pub fn into_contours(self, fill_rule: FillRule) -> CurveResult<Vec<Contour2>> {
        self.loops
            .into_iter()
            .map(|boundary_loop| boundary_loop.into_contour(fill_rule))
            .collect()
    }
}

type EndpointAdjacency = (Vec<Option<usize>>, Vec<Option<usize>>);

fn directed_boolean_fragment_owner(
    fragment: &DirectedBooleanFragment,
) -> (RegionContourKey, usize) {
    (fragment.key, fragment.fragment_index)
}

fn validate_directed_boolean_fragments(
    fragments: &[DirectedBooleanFragment],
    owner: &str,
) -> CurveResult<()> {
    if fragments.is_empty() {
        return Err(CurveError::Topology(format!(
            "{owner} must carry at least one directed fragment"
        )));
    }

    let mut fragment_owners = fragments
        .iter()
        .map(directed_boolean_fragment_owner)
        .collect::<Vec<_>>();
    fragment_owners.sort_unstable();
    if fragment_owners
        .windows(2)
        .any(|window| window[0] == window[1])
    {
        return Err(CurveError::Topology(format!(
            "{owner} directed fragment ownership must be unique"
        )));
    }
    validate_directed_boolean_fragment_geometry(fragments, owner)?;
    Ok(())
}

fn validate_directed_boolean_fragment_geometry(
    fragments: &[DirectedBooleanFragment],
    owner: &str,
) -> CurveResult<()> {
    let policy = CurvePolicy::certified();
    for fragment in fragments {
        match is_zero(
            &fragment
                .segment
                .start()
                .distance_squared(fragment.segment.end()),
            &policy,
        ) {
            Some(false) => {}
            Some(true) => {
                return Err(CurveError::Topology(format!(
                    "{owner} directed fragment must carry nonzero geometry"
                )));
            }
            None => {
                return Err(CurveError::Topology(format!(
                    "{owner} directed fragment geometry must be certified nonzero"
                )));
            }
        }
    }
    Ok(())
}

fn validate_boolean_boundary_chain_geometry(
    fragments: &[DirectedBooleanFragment],
    closed: bool,
) -> CurveResult<()> {
    validate_directed_boolean_fragment_connectivity(fragments, "boolean boundary chain")?;
    let (first, last) = directed_fragment_endpoints(fragments, "boolean boundary chain")?;

    let endpoints_close = certified_endpoint_match(last, first, "boolean boundary chain")?;
    if endpoints_close != closed {
        return Err(CurveError::Topology(
            "boolean boundary chain closed flag must match endpoint evidence".to_owned(),
        ));
    }
    Ok(())
}

fn validate_boolean_boundary_loop_geometry(
    fragments: &[DirectedBooleanFragment],
) -> CurveResult<()> {
    validate_directed_boolean_fragment_connectivity(fragments, "boolean boundary loop")?;
    let (first, last) = directed_fragment_endpoints(fragments, "boolean boundary loop")?;

    if !certified_endpoint_match(last, first, "boolean boundary loop")? {
        return Err(CurveError::Topology(
            "boolean boundary loop must close back to its first fragment".to_owned(),
        ));
    }
    Ok(())
}

fn validate_directed_boolean_fragment_connectivity(
    fragments: &[DirectedBooleanFragment],
    owner: &str,
) -> CurveResult<()> {
    for window in fragments.windows(2) {
        if !certified_endpoint_match(&window[0], &window[1], owner)? {
            return Err(CurveError::Topology(format!(
                "{owner} fragments must be endpoint-connected"
            )));
        }
    }
    Ok(())
}

fn directed_fragment_endpoints<'a>(
    fragments: &'a [DirectedBooleanFragment],
    owner: &str,
) -> CurveResult<(&'a DirectedBooleanFragment, &'a DirectedBooleanFragment)> {
    let first = fragments.first().ok_or_else(|| {
        CurveError::Topology(format!("{owner} must carry at least one directed fragment"))
    })?;
    let last = fragments.last().ok_or_else(|| {
        CurveError::Topology(format!("{owner} must carry at least one directed fragment"))
    })?;
    Ok((first, last))
}

fn certified_endpoint_match(
    left: &DirectedBooleanFragment,
    right: &DirectedBooleanFragment,
    owner: &str,
) -> CurveResult<bool> {
    let policy = CurvePolicy::certified();
    match points_match(left.segment.end(), right.segment.start(), &policy) {
        Classification::Decided(matches) => Ok(matches),
        Classification::Uncertain(reason) => Err(CurveError::Topology(format!(
            "{owner} endpoint equality could not be certified: {reason:?}"
        ))),
    }
}

fn validate_boolean_boundary_chains(chains: &[BooleanBoundaryChain]) -> CurveResult<()> {
    let mut fragment_owners = Vec::new();
    for chain in chains {
        validate_directed_boolean_fragments(chain.fragments(), "boolean boundary chain")?;
        fragment_owners.extend(
            chain
                .fragments()
                .iter()
                .map(directed_boolean_fragment_owner),
        );
    }
    validate_unique_boolean_fragment_owners(
        fragment_owners,
        "boolean boundary chain set must not reuse directed fragment ownership",
    )
}

fn validate_boolean_boundary_loops(loops: &[BooleanBoundaryLoop]) -> CurveResult<()> {
    let mut fragment_owners = Vec::new();
    for boundary_loop in loops {
        validate_directed_boolean_fragments(boundary_loop.fragments(), "boolean boundary loop")?;
        fragment_owners.extend(
            boundary_loop
                .fragments()
                .iter()
                .map(directed_boolean_fragment_owner),
        );
    }
    validate_unique_boolean_fragment_owners(
        fragment_owners,
        "boolean boundary loop set must not reuse directed fragment ownership",
    )
}

fn validate_unique_boolean_fragment_owners(
    mut fragment_owners: Vec<(RegionContourKey, usize)>,
    message: &str,
) -> CurveResult<()> {
    fragment_owners.sort_unstable();
    if fragment_owners
        .windows(2)
        .any(|window| window[0] == window[1])
    {
        return Err(CurveError::Topology(message.to_owned()));
    }
    Ok(())
}

fn validate_boolean_boundary_fragment_set(
    directed_fragments: &[DirectedBooleanFragment],
    unresolved_boundaries: &[BooleanFragmentClassification],
) -> CurveResult<()> {
    validate_directed_boolean_fragment_geometry(
        directed_fragments,
        "boolean boundary fragment set",
    )?;
    for unresolved in unresolved_boundaries {
        validate_boolean_fragment_classification_boundary_action(unresolved)?;
    }

    let mut fragment_owners = directed_fragments
        .iter()
        .map(directed_boolean_fragment_owner)
        .collect::<Vec<_>>();
    fragment_owners.extend(
        unresolved_boundaries
            .iter()
            .map(|classification| (classification.key, classification.fragment_index)),
    );
    validate_unique_boolean_fragment_owners(
        fragment_owners,
        "boolean boundary fragment set must not contain duplicate source fragment ownership",
    )
}

fn decided_boolean_boundary_chain(
    fragments: Vec<DirectedBooleanFragment>,
    closed: bool,
) -> Classification<BooleanBoundaryChain> {
    match BooleanBoundaryChain::new(fragments, closed) {
        Ok(chain) => Classification::Decided(chain),
        Err(_) => Classification::Uncertain(crate::UncertaintyReason::Unsupported),
    }
}

fn decided_boolean_boundary_chain_set(
    chains: Vec<BooleanBoundaryChain>,
) -> Classification<BooleanBoundaryChainSet> {
    match BooleanBoundaryChainSet::new(chains) {
        Ok(chain_set) => Classification::Decided(chain_set),
        Err(_) => Classification::Uncertain(crate::UncertaintyReason::Unsupported),
    }
}

fn decided_boolean_boundary_loop_set(
    loops: Vec<BooleanBoundaryLoop>,
) -> Classification<BooleanBoundaryLoopSet> {
    match BooleanBoundaryLoopSet::new(loops) {
        Ok(loop_set) => Classification::Decided(loop_set),
        Err(_) => Classification::Uncertain(crate::UncertaintyReason::Unsupported),
    }
}

fn endpoint_adjacency(
    fragments: &[DirectedBooleanFragment],
    policy: &CurvePolicy,
) -> Classification<EndpointAdjacency> {
    let mut successors = vec![None; fragments.len()];
    let mut predecessors = vec![None; fragments.len()];

    for (left_index, left) in fragments.iter().enumerate() {
        for (right_index, right) in fragments.iter().enumerate() {
            if left_index == right_index {
                continue;
            }

            let matches = match points_match(left.segment.end(), right.segment.start(), policy) {
                Classification::Decided(matches) => matches,
                Classification::Uncertain(reason) => return Classification::Uncertain(reason),
            };
            if !matches {
                continue;
            }

            if successors[left_index].replace(right_index).is_some()
                || predecessors[right_index].replace(left_index).is_some()
            {
                return Classification::Uncertain(crate::UncertaintyReason::Unsupported);
            }
        }
    }

    Classification::Decided((successors, predecessors))
}

fn follow_chain(
    start: usize,
    fragments: &[DirectedBooleanFragment],
    successors: &[Option<usize>],
    used: &mut [bool],
) -> Classification<BooleanBoundaryChain> {
    let mut chain = Vec::new();
    let mut current = start;
    let mut closed = false;

    loop {
        if used[current] {
            return Classification::Uncertain(crate::UncertaintyReason::Unsupported);
        }

        used[current] = true;
        chain.push(fragments[current].clone());

        let Some(next) = successors[current] else {
            break;
        };

        if next == start {
            closed = true;
            break;
        }
        if used[next] {
            return Classification::Uncertain(crate::UncertaintyReason::Unsupported);
        }

        current = next;
    }

    decided_boolean_boundary_chain(chain, closed)
}

fn points_match(
    left: &crate::Point2,
    right: &crate::Point2,
    policy: &CurvePolicy,
) -> Classification<bool> {
    let distance = left.distance_squared(right);
    match is_zero(&distance, policy) {
        Some(matches) => Classification::Decided(matches),
        None => Classification::Uncertain(crate::UncertaintyReason::RealSign),
    }
}