knipsa 0.3.0

Polygon booleans, offsets, point queries, and triangulation in Rust
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
//! Public operation contracts for integer and floating-point paths.

use crate::complexity::BooleanComplexity;
use crate::{
    ComplexityLimits, Error, Path64, PathD, PathKind, Paths64, PathsD, validate_path_d,
    validate_path64,
};

/// A boolean operation over filled paths.
///
/// The subject and clip collections may contain multiple rings. Empty
/// collections are valid and make the operation behave like the corresponding
/// set operation with an empty side.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[repr(u8)]
pub enum ClipType {
    /// Keep the region present in both inputs.
    Intersection = 1,
    /// Keep the region present in either input.
    Union = 2,
    /// Keep the subject region outside the clip region.
    Difference = 3,
    /// Keep the region present in exactly one input.
    Xor = 4,
}

/// A winding/fill rule used to interpret input paths.
///
/// `EvenOdd` depends only on crossing parity. The other rules use the signed
/// winding accumulated by a path, which makes ring orientation meaningful.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
#[repr(u8)]
pub enum FillRule {
    /// Alternate parity at every crossing.
    EvenOdd = 0,
    /// Non-zero winding number.
    NonZero = 1,
    /// Keep only positively wound regions.
    Positive = 2,
    /// Keep only negatively wound regions.
    Negative = 3,
}

/// Borrowed paths and options for one bounded Boolean operation.
///
/// Closed subjects and clips define filled regions; clips are always closed.
/// Open subjects are polylines and never contribute winding or affect the
/// closed result. Intersection retains open fragments inside clips, difference
/// and XOR retain fragments outside clips, and union retains fragments outside
/// every filled closed subject and clip. Open paths do not interact with one
/// another. A fragment collinear with a filled boundary follows Clipper2's
/// adjacent-fragment rule; at a polyline end, the one present adjacent
/// fragment decides whether the boundary run remains.
#[derive(Debug)]
pub struct BooleanRequest<'a, P> {
    /// Closed subject paths, the filled left-hand side of the operation.
    pub closed_subjects: &'a [P],
    /// Open subject polylines to clip.
    pub open_subjects: &'a [P],
    /// Closed clip paths, the right-hand side of the operation.
    pub clips: &'a [P],
    /// Boolean operation to perform.
    pub clip_type: ClipType,
    /// Fill rule for both path sets.
    pub fill_rule: FillRule,
    /// Deterministic input-complexity budget.
    pub limits: ComplexityLimits,
}

impl<P> Copy for BooleanRequest<'_, P> {}

impl<P> Clone for BooleanRequest<'_, P> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<'a, P> BooleanRequest<'a, P> {
    /// Creates a closed-subject Boolean request with [`ComplexityLimits::DEFAULT`].
    ///
    /// Set `open_subjects` or `limits` directly when constructing a request
    /// that needs open clipping or a different deterministic budget.
    #[must_use]
    pub const fn new(
        closed_subjects: &'a [P],
        clips: &'a [P],
        clip_type: ClipType,
        fill_rule: FillRule,
    ) -> Self {
        Self {
            closed_subjects,
            open_subjects: &[],
            clips,
            clip_type,
            fill_rule,
            limits: ComplexityLimits::DEFAULT,
        }
    }
}

/// Separate closed and open outputs from a Boolean operation.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BooleanOutput<P> {
    /// Closed polygon rings produced by the operation.
    pub closed: Vec<P>,
    /// Clipped open subject polylines.
    pub open: Vec<P>,
}

fn closed_request<'a, P>(
    subjects: &'a [P],
    clips: &'a [P],
    clip_type: ClipType,
    fill_rule: FillRule,
) -> BooleanRequest<'a, P> {
    BooleanRequest::new(subjects, clips, clip_type, fill_rule)
}

/// Executes an exact integer boolean request.
///
/// # Examples
///
/// ```
/// use knipsa::{boolean_op, BooleanRequest, ClipType, FillRule, Point64};
///
/// let subject = vec![
///     Point64::new(0, 0),
///     Point64::new(10, 0),
///     Point64::new(10, 10),
///     Point64::new(0, 10),
/// ];
/// let clip = vec![
///     Point64::new(5, 5),
///     Point64::new(15, 5),
///     Point64::new(15, 15),
///     Point64::new(5, 15),
/// ];
/// let result = boolean_op(BooleanRequest::new(
///     std::slice::from_ref(&subject),
///     std::slice::from_ref(&clip),
///     ClipType::Intersection,
///     FillRule::EvenOdd,
/// ))
/// .expect("valid polygons close");
///
/// assert_eq!(result.closed.len(), 1);
/// assert!(result.open.is_empty());
/// ```
///
/// # Errors
///
/// Returns [`Error::LimitExceeded`] when the request exceeds its
/// [`ComplexityLimits`] preflight budget. It also returns [`Error::InvalidPath`]
/// for invalid input, [`Error::ArithmeticOverflow`] for checked integer
/// overflow, [`Error::NonIntegralResult`] when an exact result cannot be
/// represented by `i64`, or [`Error::TopologyFailure`] if the arrangement
/// cannot be closed.
pub fn boolean_op(request: BooleanRequest<'_, Path64>) -> Result<BooleanOutput<Path64>, Error> {
    validate_bounded64(&request)?;
    crate::boolean::boolean_op64(&request)
}

/// Executes a floating-point boolean request.
///
/// The input binary floating-point values are treated as exact values during
/// arrangement construction; only the returned coordinates are converted back
/// to `f64`. Ordinary well-conditioned convex input uses the fast path and
/// difficult or ambiguous input uses the exact arrangement fallback.
///
/// # Errors
///
/// Returns [`Error::LimitExceeded`] when the request exceeds its
/// [`ComplexityLimits`] preflight budget, [`Error::InvalidPath`] or
/// [`Error::NonFiniteCoordinate`] for invalid input, and
/// [`Error::TopologyFailure`] if the arrangement cannot be closed.
pub fn boolean_op_d(request: BooleanRequest<'_, PathD>) -> Result<BooleanOutput<PathD>, Error> {
    validate_bounded_d(&request)?;
    crate::boolean::boolean_op_d(&request)
}

fn validate_bounded64(request: &BooleanRequest<'_, Path64>) -> Result<(), Error> {
    let mut validation_error = None;
    let mut complexity = BooleanComplexity::default();
    for path in request.closed_subjects {
        complexity.add_closed_subject(path.len());
        if validation_error.is_none() {
            validation_error = validate_path64(path, PathKind::Closed).err();
        }
    }
    for path in request.open_subjects {
        complexity.add_open_subject(path.len());
        if validation_error.is_none() {
            validation_error = validate_path64(path, PathKind::Open).err();
        }
    }
    for path in request.clips {
        complexity.add_clip(path.len());
        if validation_error.is_none() {
            validation_error = validate_path64(path, PathKind::Closed).err();
        }
    }
    complexity.check(request.limits, request.clip_type == ClipType::Union)?;
    validation_error.map_or(Ok(()), Err)
}

fn validate_bounded_d(request: &BooleanRequest<'_, PathD>) -> Result<(), Error> {
    let mut validation_error = None;
    let mut complexity = BooleanComplexity::default();
    for path in request.closed_subjects {
        complexity.add_closed_subject(path.len());
        if validation_error.is_none() {
            validation_error = validate_path_d(path, PathKind::Closed).err();
        }
    }
    for path in request.open_subjects {
        complexity.add_open_subject(path.len());
        if validation_error.is_none() {
            validation_error = validate_path_d(path, PathKind::Open).err();
        }
    }
    for path in request.clips {
        complexity.add_clip(path.len());
        if validation_error.is_none() {
            validation_error = validate_path_d(path, PathKind::Closed).err();
        }
    }
    complexity.check(request.limits, request.clip_type == ClipType::Union)?;
    validation_error.map_or(Ok(()), Err)
}

/// Intersects integer path collections using the orientation-independent
/// [`FillRule::EvenOdd`] rule.
///
/// Use [`boolean_op`] when a different fill rule is required.
///
/// # Errors
///
/// Propagates validation, arithmetic, representation, and topology errors from
/// [`boolean_op`].
pub fn intersection(subjects: &[Path64], clips: &[Path64]) -> Result<Paths64, Error> {
    boolean_op(closed_request(subjects, clips, ClipType::Intersection, FillRule::EvenOdd))
        .map(|output| output.closed)
}

/// Unites two integer path collections using [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`boolean_op`].
pub fn union(subjects: &[Path64], clips: &[Path64]) -> Result<Paths64, Error> {
    boolean_op(closed_request(subjects, clips, ClipType::Union, FillRule::EvenOdd))
        .map(|output| output.closed)
}

/// Subtracts integer `clips` from `subjects` using [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`boolean_op`].
pub fn difference(subjects: &[Path64], clips: &[Path64]) -> Result<Paths64, Error> {
    boolean_op(closed_request(subjects, clips, ClipType::Difference, FillRule::EvenOdd))
        .map(|output| output.closed)
}

/// Computes integer symmetric difference using [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`boolean_op`].
pub fn xor(subjects: &[Path64], clips: &[Path64]) -> Result<Paths64, Error> {
    boolean_op(closed_request(subjects, clips, ClipType::Xor, FillRule::EvenOdd))
        .map(|output| output.closed)
}

/// Intersects floating-point path collections using [`FillRule::EvenOdd`].
///
/// Use [`boolean_op_d`] when a different fill rule is required.
///
/// # Errors
///
/// Propagates validation, arithmetic, and topology errors from [`boolean_op_d`].
pub fn intersection_d(subjects: &[PathD], clips: &[PathD]) -> Result<PathsD, Error> {
    boolean_op_d(closed_request(subjects, clips, ClipType::Intersection, FillRule::EvenOdd))
        .map(|output| output.closed)
}

/// Unites two floating-point path collections using [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`boolean_op_d`].
pub fn union_d(subjects: &[PathD], clips: &[PathD]) -> Result<PathsD, Error> {
    boolean_op_d(closed_request(subjects, clips, ClipType::Union, FillRule::EvenOdd))
        .map(|output| output.closed)
}

/// Subtracts floating-point `clips` from `subjects` using
/// [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`boolean_op_d`].
pub fn difference_d(subjects: &[PathD], clips: &[PathD]) -> Result<PathsD, Error> {
    boolean_op_d(closed_request(subjects, clips, ClipType::Difference, FillRule::EvenOdd))
        .map(|output| output.closed)
}

/// Computes floating-point symmetric difference using [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`boolean_op_d`].
pub fn xor_d(subjects: &[PathD], clips: &[PathD]) -> Result<PathsD, Error> {
    boolean_op_d(closed_request(subjects, clips, ClipType::Xor, FillRule::EvenOdd))
        .map(|output| output.closed)
}

/// Intersects two integer rings using [`FillRule::EvenOdd`].
///
/// This is the shortest zero-allocation entry point when each side contains
/// exactly one ring. The result can still contain multiple rings.
///
/// # Errors
///
/// Propagates errors from [`intersection`].
pub fn intersection_path(subject: &Path64, clip: &Path64) -> Result<Paths64, Error> {
    intersection(std::slice::from_ref(subject), std::slice::from_ref(clip))
}

/// Unites two integer rings using [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`union`].
pub fn union_path(subject: &Path64, clip: &Path64) -> Result<Paths64, Error> {
    union(std::slice::from_ref(subject), std::slice::from_ref(clip))
}

/// Subtracts one integer ring from another using [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`difference`].
pub fn difference_path(subject: &Path64, clip: &Path64) -> Result<Paths64, Error> {
    difference(std::slice::from_ref(subject), std::slice::from_ref(clip))
}

/// Computes the symmetric difference of two integer rings using
/// [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`xor`].
pub fn xor_path(subject: &Path64, clip: &Path64) -> Result<Paths64, Error> {
    xor(std::slice::from_ref(subject), std::slice::from_ref(clip))
}

/// Intersects two floating-point rings using [`FillRule::EvenOdd`].
///
/// This is the shortest zero-allocation entry point when each side contains
/// exactly one ring. The result can still contain multiple rings.
///
/// # Errors
///
/// Propagates errors from [`intersection_d`].
pub fn intersection_path_d(subject: &PathD, clip: &PathD) -> Result<PathsD, Error> {
    intersection_d(std::slice::from_ref(subject), std::slice::from_ref(clip))
}

/// Unites two floating-point rings using [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`union_d`].
pub fn union_path_d(subject: &PathD, clip: &PathD) -> Result<PathsD, Error> {
    union_d(std::slice::from_ref(subject), std::slice::from_ref(clip))
}

/// Subtracts one floating-point ring from another using [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`difference_d`].
pub fn difference_path_d(subject: &PathD, clip: &PathD) -> Result<PathsD, Error> {
    difference_d(std::slice::from_ref(subject), std::slice::from_ref(clip))
}

/// Computes the symmetric difference of two floating-point rings using
/// [`FillRule::EvenOdd`].
///
/// # Errors
///
/// Propagates errors from [`xor_d`].
pub fn xor_path_d(subject: &PathD, clip: &PathD) -> Result<PathsD, Error> {
    xor_d(std::slice::from_ref(subject), std::slice::from_ref(clip))
}

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

    #[test]
    fn exposes_stable_operation_values() {
        assert_eq!(ClipType::Intersection as u8, 1);
        assert_eq!(ClipType::Union as u8, 2);
        assert_eq!(ClipType::Difference as u8, 3);
        assert_eq!(ClipType::Xor as u8, 4);
        assert_eq!(FillRule::EvenOdd as u8, 0);
        assert_eq!(FillRule::NonZero as u8, 1);
        assert_eq!(FillRule::Positive as u8, 2);
        assert_eq!(FillRule::Negative as u8, 3);
    }

    #[test]
    fn constructors_preserve_borrowed_inputs_and_options() {
        let integer_subject = vec![crate::Point64::new(0, 0)];
        let integer_clip = vec![crate::Point64::new(1, 1)];
        let integer_request = BooleanRequest::new(
            std::slice::from_ref(&integer_subject),
            std::slice::from_ref(&integer_clip),
            ClipType::Difference,
            FillRule::Positive,
        );
        assert!(std::ptr::eq(
            integer_request.closed_subjects[0].as_ptr(),
            integer_subject.as_ptr()
        ));
        assert!(std::ptr::eq(integer_request.clips[0].as_ptr(), integer_clip.as_ptr()));
        assert_eq!(integer_request.clip_type, ClipType::Difference);
        assert_eq!(integer_request.fill_rule, FillRule::Positive);
        let cloned = Clone::clone(&integer_request);
        assert_eq!(cloned.closed_subjects, integer_request.closed_subjects);

        let double_subject = vec![crate::PointD::new(0.0, 0.0)];
        let double_clip = vec![crate::PointD::new(1.0, 1.0)];
        let double_request = BooleanRequest::new(
            std::slice::from_ref(&double_subject),
            std::slice::from_ref(&double_clip),
            ClipType::Xor,
            FillRule::Negative,
        );
        assert!(std::ptr::eq(double_request.closed_subjects[0].as_ptr(), double_subject.as_ptr()));
        assert!(std::ptr::eq(double_request.clips[0].as_ptr(), double_clip.as_ptr()));
        assert_eq!(double_request.clip_type, ClipType::Xor);
        assert_eq!(double_request.fill_rule, FillRule::Negative);
    }

    #[test]
    fn open_subjects_are_charged_only_against_relevant_filled_boundaries() {
        let open = [(0..3_000).map(|x| crate::Point64::new(x, 0)).collect::<Path64>()];
        let output = boolean_op(BooleanRequest {
            closed_subjects: &[],
            open_subjects: &open,
            clips: &[],
            clip_type: ClipType::Difference,
            fill_rule: FillRule::EvenOdd,
            limits: ComplexityLimits::new(1, 3_000, 0),
        })
        .expect("an isolated open polyline has no candidate edge pairs");
        assert_eq!(output.open, open);

        let open_d = [vec![crate::PointD::new(-1.0, 0.5), crate::PointD::new(2.0, 0.5)]];
        let clip_d = [vec![
            crate::PointD::new(0.0, 0.0),
            crate::PointD::new(1.0, 0.0),
            crate::PointD::new(1.0, 1.0),
            crate::PointD::new(0.0, 1.0),
        ]];
        let output = boolean_op_d(BooleanRequest {
            closed_subjects: &[],
            open_subjects: &open_d,
            clips: &clip_d,
            clip_type: ClipType::Intersection,
            fill_rule: FillRule::EvenOdd,
            limits: ComplexityLimits::DEFAULT,
        })
        .expect("valid floating open request");
        assert_eq!(
            output.open,
            vec![vec![crate::PointD::new(0.0, 0.5), crate::PointD::new(1.0, 0.5)]]
        );
    }

    #[test]
    fn executes_union_with_empty_clip() {
        let square =
            vec![crate::Point64::new(0, 0), crate::Point64::new(1, 0), crate::Point64::new(1, 1)];
        let request = BooleanRequest {
            limits: crate::ComplexityLimits::DEFAULT,
            open_subjects: &[],
            closed_subjects: &[square],
            clips: &[],
            clip_type: ClipType::Union,
            fill_rule: FillRule::NonZero,
        };
        assert_eq!(boolean_op(request).expect("valid request").closed.len(), 1);
    }

    #[test]
    fn rejects_invalid_boolean_input_before_kernel() {
        let request = BooleanRequest {
            limits: crate::ComplexityLimits::DEFAULT,
            open_subjects: &[],
            closed_subjects: &[vec![crate::Point64::new(0, 0)]],
            clips: &[],
            clip_type: ClipType::Intersection,
            fill_rule: FillRule::EvenOdd,
        };
        assert!(matches!(boolean_op(request), Err(Error::InvalidPath { .. })));
    }

    #[test]
    fn validation_keeps_scanning_complexity_after_the_first_error() {
        let integer_closed = [
            vec![crate::Point64::new(0, 0)],
            vec![crate::Point64::new(0, 0), crate::Point64::new(2, 0), crate::Point64::new(0, 2)],
        ];
        let integer_open = [vec![crate::Point64::new(0, 0), crate::Point64::new(1, 1)]];
        assert!(matches!(
            boolean_op(BooleanRequest {
                closed_subjects: &integer_closed,
                open_subjects: &integer_open,
                clips: &[],
                clip_type: ClipType::Intersection,
                fill_rule: FillRule::EvenOdd,
                limits: ComplexityLimits::DEFAULT,
            }),
            Err(Error::InvalidPath { .. })
        ));

        let double_closed = [
            vec![crate::PointD::new(0.0, 0.0)],
            vec![
                crate::PointD::new(0.0, 0.0),
                crate::PointD::new(2.0, 0.0),
                crate::PointD::new(0.0, 2.0),
            ],
        ];
        let double_open = [vec![crate::PointD::new(0.0, 0.0), crate::PointD::new(1.0, 1.0)]];
        assert!(matches!(
            boolean_op_d(BooleanRequest {
                closed_subjects: &double_closed,
                open_subjects: &double_open,
                clips: &[],
                clip_type: ClipType::Intersection,
                fill_rule: FillRule::EvenOdd,
                limits: ComplexityLimits::DEFAULT,
            }),
            Err(Error::InvalidPath { .. })
        ));
    }

    #[test]
    fn open_requests_validate_and_preflight_before_geometry_work() {
        let invalid_open = [vec![crate::Point64::new(0, 0)]];
        let validation = BooleanRequest {
            closed_subjects: &[],
            open_subjects: &invalid_open,
            clips: &[],
            clip_type: ClipType::Intersection,
            fill_rule: FillRule::EvenOdd,
            limits: ComplexityLimits::DEFAULT,
        };
        assert!(matches!(
            boolean_op(validation),
            Err(Error::InvalidPath { kind: PathKind::Open, .. })
        ));

        let oversized = [vec![crate::Point64::new(0, 0), crate::Point64::new(1, 1)]];
        let limited = BooleanRequest {
            closed_subjects: &[],
            open_subjects: &oversized,
            clips: &[],
            clip_type: ClipType::Difference,
            fill_rule: FillRule::EvenOdd,
            limits: ComplexityLimits::new(0, 0, 0),
        };
        assert!(matches!(
            boolean_op(limited),
            Err(Error::LimitExceeded { resource: crate::ComplexityResource::Paths, .. })
        ));
    }

    #[test]
    fn executes_and_validates_double_requests() {
        let square = vec![
            crate::PointD::new(0.0, 0.0),
            crate::PointD::new(1.0, 0.0),
            crate::PointD::new(1.0, 1.0),
        ];
        let request = BooleanRequest {
            limits: crate::ComplexityLimits::DEFAULT,
            open_subjects: &[],
            closed_subjects: &[square],
            clips: &[],
            clip_type: ClipType::Union,
            fill_rule: FillRule::NonZero,
        };
        assert_eq!(crate::boolean_op_d(request).expect("valid request").closed.len(), 1);
        let bad = BooleanRequest {
            limits: crate::ComplexityLimits::DEFAULT,
            open_subjects: &[],
            closed_subjects: &[vec![
                crate::PointD::new(0.0, 0.0),
                crate::PointD::new(f64::NAN, 0.0),
            ]],
            clips: &[],
            clip_type: ClipType::Union,
            fill_rule: FillRule::EvenOdd,
        };
        assert!(matches!(boolean_op_d(bad), Err(Error::InvalidPath { .. })));
    }

    #[test]
    #[allow(clippy::cast_precision_loss)]
    fn convenience_operations_use_even_odd_semantics() {
        let subject = vec![
            crate::Point64::new(0, 0),
            crate::Point64::new(10, 0),
            crate::Point64::new(10, 10),
            crate::Point64::new(0, 10),
        ];
        let clip = vec![
            crate::Point64::new(5, 0),
            crate::Point64::new(15, 0),
            crate::Point64::new(15, 10),
            crate::Point64::new(5, 10),
        ];
        let subjects = [subject];
        let clips = [clip];
        assert_eq!(intersection(&subjects, &clips).expect("intersection").len(), 1);
        assert_eq!(union(&subjects, &clips).expect("union").len(), 1);
        assert_eq!(difference(&subjects, &clips).expect("difference").len(), 1);
        assert_eq!(xor(&subjects, &clips).expect("xor").len(), 2);

        let subjects_d = subjects
            .iter()
            .map(|path| {
                path.iter()
                    .map(|point| crate::PointD::new(point.x as f64, point.y as f64))
                    .collect()
            })
            .collect::<Vec<PathD>>();
        let clips_d = clips
            .iter()
            .map(|path| {
                path.iter()
                    .map(|point| crate::PointD::new(point.x as f64, point.y as f64))
                    .collect()
            })
            .collect::<Vec<PathD>>();
        assert_eq!(intersection_d(&subjects_d, &clips_d).expect("intersection_d").len(), 1);
        assert_eq!(union_d(&subjects_d, &clips_d).expect("union_d").len(), 1);
        assert_eq!(difference_d(&subjects_d, &clips_d).expect("difference_d").len(), 1);
        assert_eq!(xor_d(&subjects_d, &clips_d).expect("xor_d").len(), 2);
    }

    #[test]
    fn single_ring_helpers_match_collection_helpers() {
        let integer_subject = vec![
            crate::Point64::new(0, 0),
            crate::Point64::new(10, 0),
            crate::Point64::new(10, 10),
            crate::Point64::new(0, 10),
        ];
        let integer_clip = vec![
            crate::Point64::new(5, 0),
            crate::Point64::new(15, 0),
            crate::Point64::new(15, 10),
            crate::Point64::new(5, 10),
        ];
        let integer_subjects = std::slice::from_ref(&integer_subject);
        let integer_clips = std::slice::from_ref(&integer_clip);
        assert_eq!(
            intersection_path(&integer_subject, &integer_clip),
            intersection(integer_subjects, integer_clips)
        );
        assert_eq!(
            union_path(&integer_subject, &integer_clip),
            union(integer_subjects, integer_clips)
        );
        assert_eq!(
            difference_path(&integer_subject, &integer_clip),
            difference(integer_subjects, integer_clips)
        );
        assert_eq!(xor_path(&integer_subject, &integer_clip), xor(integer_subjects, integer_clips));

        let double_subject = vec![
            crate::PointD::new(0.0, 0.0),
            crate::PointD::new(10.0, 0.0),
            crate::PointD::new(10.0, 10.0),
            crate::PointD::new(0.0, 10.0),
        ];
        let double_clip = vec![
            crate::PointD::new(5.0, 0.0),
            crate::PointD::new(15.0, 0.0),
            crate::PointD::new(15.0, 10.0),
            crate::PointD::new(5.0, 10.0),
        ];
        let double_subjects = std::slice::from_ref(&double_subject);
        let double_clips = std::slice::from_ref(&double_clip);
        assert_eq!(
            intersection_path_d(&double_subject, &double_clip),
            intersection_d(double_subjects, double_clips)
        );
        assert_eq!(
            union_path_d(&double_subject, &double_clip),
            union_d(double_subjects, double_clips)
        );
        assert_eq!(
            difference_path_d(&double_subject, &double_clip),
            difference_d(double_subjects, double_clips)
        );
        assert_eq!(xor_path_d(&double_subject, &double_clip), xor_d(double_subjects, double_clips));
    }
}