hypercurve 0.2.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
//! Prepared region boolean traversal.
//!
//! This module owns the prepared counterpart to the ordinary region boolean
//! pipeline. Prepared region booleans keep the same event/split/classify/emit
//! stages as [`crate::region_boolean`], but route event collection and fragment
//! representative-point classification through [`crate::PreparedRegionView2`]
//! caches.

use crate::prepared::{PreparedContourView2, PreparedRegionView2};
use crate::{
    BooleanBoundaryLoopSet, BooleanFragmentSelection, BooleanOp, Classification, Contour2,
    CurvePolicy, CurveResult, FillRule, Region2, RegionFragmentSet, RegionIntersectionSet,
    RegionPointLocation, RegionSide,
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum PreparedBoundaryContactKind {
    PointOnly,
    Overlap,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum PreparedBoundaryContactResolution {
    BoundaryOnly(PreparedBoundaryContactKind),
    Containment {
        relation: crate::region_boolean::BoundaryContainmentRelation,
        contact: PreparedBoundaryContactKind,
    },
}

pub(crate) fn boolean_boundary_loops_between_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    op: BooleanOp,
    policy: &CurvePolicy,
) -> CurveResult<Classification<BooleanBoundaryLoopSet>> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    let intersections = first.intersect_prepared_region(second, policy)?;

    let fragments = match intersections.split_regions(&first_view, &second_view, policy)? {
        Classification::Decided(fragments) => fragments,
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    };

    // The prepared path keeps the same split/classify/traverse structure as the
    // ordinary Greiner-Hormann/Martinez-Rueda-Feito pipeline, but routes
    // fragment representative-point classification through prepared region
    // caches so repeated boolean-boundary queries do not rebuild contour boxes.
    let selection =
        match classify_fragments_with_prepared_regions(&fragments, first, second, op, policy)? {
            Classification::Decided(selection) => selection,
            Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
        };

    let emitted = selection.emit_boundary_fragments(&fragments)?;
    let chains = match emitted.assemble_chains(policy) {
        Classification::Decided(chains) => chains,
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    };

    Ok(chains.into_closed_loops())
}

pub(crate) fn boolean_boundary_contours_between_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    op: BooleanOp,
    fill_rule: FillRule,
    policy: &CurvePolicy,
) -> CurveResult<Classification<Vec<Contour2>>> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    if crate::region_boolean::same_region_view(&first_view, &second_view) {
        return Ok(Classification::Decided(match op {
            BooleanOp::Union | BooleanOp::Intersection => {
                crate::region_boolean::clone_boundary_contours(&first_view)
            }
            BooleanOp::Difference | BooleanOp::Xor => Vec::new(),
        }));
    }
    if first_view.is_empty() || second_view.is_empty() {
        return Ok(Classification::Decided(
            crate::region_boolean::empty_operand_boundary_contours(&first_view, &second_view, op),
        ));
    }
    match boundary_contact_resolution_prepared(first, second, policy)? {
        Classification::Decided(Some(PreparedBoundaryContactResolution::BoundaryOnly(kind))) => {
            return boundary_contact_boundary_contours_prepared(
                first, second, op, fill_rule, policy, kind,
            );
        }
        Classification::Decided(Some(PreparedBoundaryContactResolution::Containment {
            relation,
            contact,
        })) => {
            if let Some(contours) =
                containment_boundary_contours_prepared(first, second, op, relation)
            {
                return Ok(Classification::Decided(contours));
            }
            if relation == crate::region_boolean::BoundaryContainmentRelation::FirstContainsSecond
                && contact == PreparedBoundaryContactKind::Overlap
                && op == BooleanOp::Difference
            {
                return containment_difference_boundary_contours_prepared(
                    first, second, fill_rule, policy,
                );
            }
        }
        Classification::Decided(None) => {}
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    }
    if op == BooleanOp::Xor {
        return xor_boundary_contours_by_prepared_region(first, second, fill_rule, policy);
    }

    match boolean_boundary_loops_between_prepared(first, second, op, policy)? {
        Classification::Decided(loops) => {
            loops.into_contours(fill_rule).map(Classification::Decided)
        }
        Classification::Uncertain(reason) => Ok(Classification::Uncertain(reason)),
    }
}

pub(crate) fn boolean_region_between_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    op: BooleanOp,
    fill_rule: FillRule,
    policy: &CurvePolicy,
) -> CurveResult<Classification<Region2>> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    if crate::region_boolean::same_region_view(&first_view, &second_view) {
        return Ok(Classification::Decided(match op {
            BooleanOp::Union | BooleanOp::Intersection => {
                crate::region_boolean::clone_region(&first_view)
            }
            BooleanOp::Difference | BooleanOp::Xor => Region2::empty(),
        }));
    }
    if first_view.is_empty() || second_view.is_empty() {
        return Ok(Classification::Decided(
            crate::region_boolean::empty_operand_region(&first_view, &second_view, op),
        ));
    }
    match boundary_contact_resolution_prepared(first, second, policy)? {
        Classification::Decided(Some(PreparedBoundaryContactResolution::BoundaryOnly(kind))) => {
            return boundary_contact_region_prepared(first, second, op, fill_rule, policy, kind);
        }
        Classification::Decided(Some(PreparedBoundaryContactResolution::Containment {
            relation,
            contact,
        })) => {
            if let Some(region) = containment_region_prepared(first, second, op, relation) {
                return Ok(Classification::Decided(region));
            }
            if relation == crate::region_boolean::BoundaryContainmentRelation::FirstContainsSecond
                && contact == PreparedBoundaryContactKind::Overlap
                && op == BooleanOp::Difference
            {
                return match containment_difference_boundary_contours_prepared(
                    first, second, fill_rule, policy,
                )? {
                    Classification::Decided(contours) => {
                        Region2::from_boundary_contours(contours, policy)
                    }
                    Classification::Uncertain(reason) => Ok(Classification::Uncertain(reason)),
                };
            }
        }
        Classification::Decided(None) => {}
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    }
    if op == BooleanOp::Xor {
        return xor_region_by_prepared_difference_union(first, second, fill_rule, policy);
    }

    match boolean_boundary_contours_between_prepared(first, second, op, fill_rule, policy)? {
        Classification::Decided(contours) => Region2::from_boundary_contours(contours, policy),
        Classification::Uncertain(reason) => Ok(Classification::Uncertain(reason)),
    }
}

fn xor_boundary_contours_by_prepared_region(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    fill_rule: FillRule,
    policy: &CurvePolicy,
) -> CurveResult<Classification<Vec<Contour2>>> {
    match xor_region_by_prepared_difference_union(first, second, fill_rule, policy)? {
        Classification::Decided(region) => Ok(Classification::Decided(
            crate::region_boolean::clone_boundary_contours(&region.as_view()),
        )),
        Classification::Uncertain(reason) => Ok(Classification::Uncertain(reason)),
    }
}

fn boundary_contact_resolution_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    policy: &CurvePolicy,
) -> CurveResult<Classification<Option<PreparedBoundaryContactResolution>>> {
    let intersections = first.intersect_prepared_region(second, policy)?;
    if intersections.is_empty() {
        return Ok(Classification::Decided(None));
    }

    let saw_overlap = match crate::region_boolean::boundary_contact_overlap_flag(&intersections) {
        Classification::Decided(Some(saw_overlap)) => saw_overlap,
        Classification::Decided(None) => return Ok(Classification::Decided(None)),
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    };

    // Prepared boundary-contact certification follows the same degeneracy split
    // described by Foster, Hormann, and Popa, "Clipping simple polygons with
    // degenerate intersections" (2019), but reuses cached region classifiers
    // for the interior-disjointness samples.
    let disjoint_interiors = if saw_overlap {
        split_contact_interiors_are_disjoint_prepared(first, second, &intersections, policy)?
    } else {
        unsplit_contact_interiors_are_disjoint_prepared(first, second, policy)?
    };
    match disjoint_interiors {
        Classification::Decided(true) => {}
        Classification::Decided(false) => {
            return match boundary_contact_containment_relation_prepared(first, second, policy)? {
                Classification::Decided(Some(relation)) => Ok(Classification::Decided(Some(
                    PreparedBoundaryContactResolution::Containment {
                        relation,
                        contact: if saw_overlap {
                            PreparedBoundaryContactKind::Overlap
                        } else {
                            PreparedBoundaryContactKind::PointOnly
                        },
                    },
                ))),
                Classification::Decided(None) => Ok(Classification::Decided(None)),
                Classification::Uncertain(reason) => Ok(Classification::Uncertain(reason)),
            };
        }
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    }

    Ok(Classification::Decided(Some(
        PreparedBoundaryContactResolution::BoundaryOnly(if saw_overlap {
            PreparedBoundaryContactKind::Overlap
        } else {
            PreparedBoundaryContactKind::PointOnly
        }),
    )))
}

fn split_contact_interiors_are_disjoint_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    intersections: &RegionIntersectionSet,
    policy: &CurvePolicy,
) -> CurveResult<Classification<bool>> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    let fragments = match intersections.split_regions(&first_view, &second_view, policy)? {
        Classification::Decided(fragments) => fragments,
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    };

    let mut first_has_outside_sample = false;
    let mut second_has_outside_sample = false;
    for contour_fragments in fragments.contours() {
        for fragment in contour_fragments.fragments.fragments() {
            let sample = match fragment.segment.representative_point(policy)? {
                Classification::Decided(sample) => sample,
                Classification::Uncertain(reason) => {
                    return Ok(Classification::Uncertain(reason));
                }
            };
            let location = match contour_fragments.key.side {
                RegionSide::First => second.classify_point(&sample, policy),
                RegionSide::Second => first.classify_point(&sample, policy),
            };
            match location {
                Classification::Decided(RegionPointLocation::Outside) => {
                    match contour_fragments.key.side {
                        RegionSide::First => first_has_outside_sample = true,
                        RegionSide::Second => second_has_outside_sample = true,
                    }
                }
                Classification::Decided(RegionPointLocation::Boundary) => {}
                Classification::Decided(RegionPointLocation::Inside) => {
                    return Ok(Classification::Decided(false));
                }
                Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
            }
        }
    }

    Ok(Classification::Decided(
        first_has_outside_sample && second_has_outside_sample,
    ))
}

fn unsplit_contact_interiors_are_disjoint_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    policy: &CurvePolicy,
) -> CurveResult<Classification<bool>> {
    let mut first_has_outside_sample = false;
    let mut second_has_outside_sample = false;

    match scan_unsplit_prepared_contact_samples(
        first.prepared_material_contours(),
        second,
        &mut first_has_outside_sample,
        policy,
    )? {
        Classification::Decided(true) => {}
        Classification::Decided(false) => return Ok(Classification::Decided(false)),
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    }
    match scan_unsplit_prepared_contact_samples(
        first.prepared_hole_contours(),
        second,
        &mut first_has_outside_sample,
        policy,
    )? {
        Classification::Decided(true) => {}
        Classification::Decided(false) => return Ok(Classification::Decided(false)),
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    }
    match scan_unsplit_prepared_contact_samples(
        second.prepared_material_contours(),
        first,
        &mut second_has_outside_sample,
        policy,
    )? {
        Classification::Decided(true) => {}
        Classification::Decided(false) => return Ok(Classification::Decided(false)),
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    }
    match scan_unsplit_prepared_contact_samples(
        second.prepared_hole_contours(),
        first,
        &mut second_has_outside_sample,
        policy,
    )? {
        Classification::Decided(true) => {}
        Classification::Decided(false) => return Ok(Classification::Decided(false)),
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    }

    Ok(Classification::Decided(
        first_has_outside_sample && second_has_outside_sample,
    ))
}

fn scan_unsplit_prepared_contact_samples(
    contours: &[PreparedContourView2<'_>],
    opposite: &PreparedRegionView2<'_>,
    has_outside_sample: &mut bool,
    policy: &CurvePolicy,
) -> CurveResult<Classification<bool>> {
    for contour in contours {
        for segment in contour.contour().segments() {
            let sample = match segment.representative_point(policy)? {
                Classification::Decided(sample) => sample,
                Classification::Uncertain(reason) => {
                    return Ok(Classification::Uncertain(reason));
                }
            };
            match opposite.classify_point(&sample, policy) {
                Classification::Decided(RegionPointLocation::Outside) => {
                    *has_outside_sample = true;
                }
                Classification::Decided(RegionPointLocation::Boundary) => {}
                Classification::Decided(RegionPointLocation::Inside) => {
                    return Ok(Classification::Decided(false));
                }
                Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
            }
        }
    }

    Ok(Classification::Decided(true))
}

fn boundary_contact_containment_relation_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    policy: &CurvePolicy,
) -> CurveResult<Classification<Option<crate::region_boolean::BoundaryContainmentRelation>>> {
    let first_contains_second =
        match prepared_region_contains_region_boundary_samples(first, second, policy)? {
            Classification::Decided(contains) => contains,
            Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
        };
    let second_contains_first =
        match prepared_region_contains_region_boundary_samples(second, first, policy)? {
            Classification::Decided(contains) => contains,
            Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
        };

    Ok(Classification::Decided(
        match (first_contains_second, second_contains_first) {
            (true, true) => Some(crate::region_boolean::BoundaryContainmentRelation::Equivalent),
            (true, false) => {
                Some(crate::region_boolean::BoundaryContainmentRelation::FirstContainsSecond)
            }
            (false, true) => {
                Some(crate::region_boolean::BoundaryContainmentRelation::SecondContainsFirst)
            }
            (false, false) => None,
        },
    ))
}

fn prepared_region_contains_region_boundary_samples(
    container: &PreparedRegionView2<'_>,
    candidate: &PreparedRegionView2<'_>,
    policy: &CurvePolicy,
) -> CurveResult<Classification<bool>> {
    crate::region_boolean::boundary_contours_inside_or_on_region(
        candidate
            .prepared_material_contours()
            .iter()
            .map(|contour| contour.contour())
            .chain(
                candidate
                    .prepared_hole_contours()
                    .iter()
                    .map(|contour| contour.contour()),
            ),
        |point| container.classify_point(point, policy),
        policy,
    )
}

fn containment_boundary_contours_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    op: BooleanOp,
    relation: crate::region_boolean::BoundaryContainmentRelation,
) -> Option<Vec<Contour2>> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    match (relation, op) {
        (
            crate::region_boolean::BoundaryContainmentRelation::FirstContainsSecond,
            BooleanOp::Union | BooleanOp::Intersection,
        ) => Some(match op {
            BooleanOp::Union => crate::region_boolean::clone_boundary_contours(&first_view),
            BooleanOp::Intersection => crate::region_boolean::clone_boundary_contours(&second_view),
            _ => unreachable!(),
        }),
        (
            crate::region_boolean::BoundaryContainmentRelation::SecondContainsFirst,
            BooleanOp::Union | BooleanOp::Intersection,
        ) => Some(match op {
            BooleanOp::Union => crate::region_boolean::clone_boundary_contours(&second_view),
            BooleanOp::Intersection => crate::region_boolean::clone_boundary_contours(&first_view),
            _ => unreachable!(),
        }),
        (
            crate::region_boolean::BoundaryContainmentRelation::SecondContainsFirst,
            BooleanOp::Difference,
        ) => Some(Vec::new()),
        (
            crate::region_boolean::BoundaryContainmentRelation::Equivalent,
            BooleanOp::Union | BooleanOp::Intersection,
        ) => Some(crate::region_boolean::clone_boundary_contours(&first_view)),
        (
            crate::region_boolean::BoundaryContainmentRelation::Equivalent,
            BooleanOp::Difference | BooleanOp::Xor,
        ) => Some(Vec::new()),
        _ => None,
    }
}

fn containment_region_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    op: BooleanOp,
    relation: crate::region_boolean::BoundaryContainmentRelation,
) -> Option<Region2> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    match (relation, op) {
        (
            crate::region_boolean::BoundaryContainmentRelation::FirstContainsSecond,
            BooleanOp::Union | BooleanOp::Intersection,
        ) => Some(match op {
            BooleanOp::Union => crate::region_boolean::clone_region(&first_view),
            BooleanOp::Intersection => crate::region_boolean::clone_region(&second_view),
            _ => unreachable!(),
        }),
        (
            crate::region_boolean::BoundaryContainmentRelation::SecondContainsFirst,
            BooleanOp::Union | BooleanOp::Intersection,
        ) => Some(match op {
            BooleanOp::Union => crate::region_boolean::clone_region(&second_view),
            BooleanOp::Intersection => crate::region_boolean::clone_region(&first_view),
            _ => unreachable!(),
        }),
        (
            crate::region_boolean::BoundaryContainmentRelation::SecondContainsFirst,
            BooleanOp::Difference,
        ) => Some(Region2::empty()),
        (
            crate::region_boolean::BoundaryContainmentRelation::Equivalent,
            BooleanOp::Union | BooleanOp::Intersection,
        ) => Some(crate::region_boolean::clone_region(&first_view)),
        (
            crate::region_boolean::BoundaryContainmentRelation::Equivalent,
            BooleanOp::Difference | BooleanOp::Xor,
        ) => Some(Region2::empty()),
        _ => None,
    }
}

fn containment_difference_boundary_contours_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    fill_rule: FillRule,
    policy: &CurvePolicy,
) -> CurveResult<Classification<Vec<Contour2>>> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    let intersections = first.intersect_prepared_region(second, policy)?;
    let fragments = match intersections.split_regions(&first_view, &second_view, policy)? {
        Classification::Decided(fragments) => fragments,
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    };
    let selection = match classify_fragments_with_prepared_regions(
        &fragments,
        first,
        second,
        BooleanOp::Difference,
        policy,
    )? {
        Classification::Decided(selection) => selection,
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    };

    crate::region_boolean::boundary_contours_dropping_unresolved(
        &fragments, &selection, fill_rule, policy,
    )
}

fn boundary_contact_boundary_contours_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    op: BooleanOp,
    fill_rule: FillRule,
    policy: &CurvePolicy,
    kind: PreparedBoundaryContactKind,
) -> CurveResult<Classification<Vec<Contour2>>> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    Ok(Classification::Decided(match op {
        BooleanOp::Union | BooleanOp::Xor => match kind {
            PreparedBoundaryContactKind::PointOnly => {
                let mut contours = crate::region_boolean::clone_boundary_contours(&first_view);
                contours.extend(crate::region_boolean::clone_boundary_contours(&second_view));
                contours
            }
            PreparedBoundaryContactKind::Overlap => {
                return boundary_overlap_union_contours_prepared(
                    first, second, op, fill_rule, policy,
                );
            }
        },
        BooleanOp::Intersection => Vec::new(),
        BooleanOp::Difference => crate::region_boolean::clone_boundary_contours(&first_view),
    }))
}

fn boundary_overlap_union_contours_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    op: BooleanOp,
    fill_rule: FillRule,
    policy: &CurvePolicy,
) -> CurveResult<Classification<Vec<Contour2>>> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    let intersections = first.intersect_prepared_region(second, policy)?;
    let fragments = match intersections.split_regions(&first_view, &second_view, policy)? {
        Classification::Decided(fragments) => fragments,
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    };
    let selection =
        match classify_fragments_with_prepared_regions(&fragments, first, second, op, policy)? {
            Classification::Decided(selection) => selection,
            Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
        };

    crate::region_boolean::boundary_contours_dropping_unresolved(
        &fragments, &selection, fill_rule, policy,
    )
}

fn boundary_contact_region_prepared(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    op: BooleanOp,
    fill_rule: FillRule,
    policy: &CurvePolicy,
    kind: PreparedBoundaryContactKind,
) -> CurveResult<Classification<Region2>> {
    let first_view = first.as_region_view();
    let second_view = second.as_region_view();
    Ok(Classification::Decided(match op {
        BooleanOp::Union | BooleanOp::Xor => match kind {
            PreparedBoundaryContactKind::PointOnly => {
                crate::region_boolean::merge_disjoint_region_bins(
                    crate::region_boolean::clone_region(&first_view),
                    crate::region_boolean::clone_region(&second_view),
                )
            }
            PreparedBoundaryContactKind::Overlap => {
                return match boundary_overlap_union_contours_prepared(
                    first, second, op, fill_rule, policy,
                )? {
                    Classification::Decided(contours) => {
                        Region2::from_boundary_contours(contours, policy)
                    }
                    Classification::Uncertain(reason) => Ok(Classification::Uncertain(reason)),
                };
            }
        },
        BooleanOp::Intersection => Region2::empty(),
        BooleanOp::Difference => crate::region_boolean::clone_region(&first_view),
    }))
}

fn classify_fragments_with_prepared_regions(
    fragments: &RegionFragmentSet,
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    op: BooleanOp,
    policy: &CurvePolicy,
) -> CurveResult<Classification<BooleanFragmentSelection>> {
    fragments.classify_for_boolean_with_point_classifier(op, policy, |source_side, sample| {
        match source_side {
            RegionSide::First => second.classify_point(sample, policy),
            RegionSide::Second => first.classify_point(sample, policy),
        }
    })
}

fn xor_region_by_prepared_difference_union(
    first: &PreparedRegionView2<'_>,
    second: &PreparedRegionView2<'_>,
    fill_rule: FillRule,
    policy: &CurvePolicy,
) -> CurveResult<Classification<Region2>> {
    let first_only = match boolean_region_between_prepared(
        first,
        second,
        BooleanOp::Difference,
        fill_rule,
        policy,
    )? {
        Classification::Decided(region) => region,
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    };
    let second_only = match boolean_region_between_prepared(
        second,
        first,
        BooleanOp::Difference,
        fill_rule,
        policy,
    )? {
        Classification::Decided(region) => region,
        Classification::Uncertain(reason) => return Ok(Classification::Uncertain(reason)),
    };

    Ok(Classification::Decided(
        crate::region_boolean::merge_disjoint_region_bins(first_only, second_only),
    ))
}