knipsa 0.2.1

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
//! Public operation contracts for integer and floating-point paths.

use crate::{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 integer inputs to a boolean operation.
///
/// Every non-empty path is interpreted as a closed ring. The first point does
/// not need to be repeated at the end.
#[derive(Clone, Copy, Debug)]
pub struct BooleanRequest<'a> {
    /// Subject paths, the left-hand side of the operation.
    pub subjects: &'a [Path64],
    /// Clip paths, the right-hand side of the operation.
    pub clips: &'a [Path64],
    /// Boolean operation to perform.
    pub clip_type: ClipType,
    /// Fill rule for both path sets.
    pub fill_rule: FillRule,
}

impl<'a> BooleanRequest<'a> {
    /// Creates a boolean request from borrowed subject and clip paths.
    #[must_use]
    pub const fn new(
        subjects: &'a [Path64],
        clips: &'a [Path64],
        clip_type: ClipType,
        fill_rule: FillRule,
    ) -> Self {
        Self { subjects, clips, clip_type, fill_rule }
    }
}

/// Borrowed floating-point inputs to a boolean operation.
///
/// This has the same topology contract as [`BooleanRequest`], but accepts
/// finite [`f64`](f64) coordinates.
#[derive(Clone, Copy, Debug)]
pub struct BooleanRequestD<'a> {
    /// Subject paths, the left-hand side of the operation.
    pub subjects: &'a [PathD],
    /// Clip paths, the right-hand side of the operation.
    pub clips: &'a [PathD],
    /// Boolean operation to perform.
    pub clip_type: ClipType,
    /// Fill rule for both path sets.
    pub fill_rule: FillRule,
}

impl<'a> BooleanRequestD<'a> {
    /// Creates a floating-point boolean request from borrowed paths.
    #[must_use]
    pub const fn new(
        subjects: &'a [PathD],
        clips: &'a [PathD],
        clip_type: ClipType,
        fill_rule: FillRule,
    ) -> Self {
        Self { subjects, clips, clip_type, fill_rule }
    }
}

/// Validates a boolean request without executing it.
///
/// Use this when a caller wants to report malformed input before doing any
/// work. [`boolean_op`] performs the same validation automatically.
///
/// # Errors
///
/// Returns [`Error::InvalidPath`] when a non-empty subject or clip path is too
/// short to describe a closed region.
pub fn validate_request(request: &BooleanRequest<'_>) -> Result<(), Error> {
    for path in request.subjects.iter().chain(request.clips) {
        validate_path64(path, PathKind::Closed)?;
    }
    Ok(())
}

/// Validates a floating-point boolean request without executing it.
///
/// In addition to path shape, this rejects NaN and infinite coordinates.
/// [`boolean_op_d`] performs the same validation automatically.
///
/// # Errors
///
/// Returns [`Error::InvalidPath`] for a too-short path and
/// [`Error::NonFiniteCoordinate`] for a non-finite coordinate.
pub fn validate_request_d(request: &BooleanRequestD<'_>) -> Result<(), Error> {
    for path in request.subjects.iter().chain(request.clips) {
        validate_path_d(path, PathKind::Closed)?;
    }
    Ok(())
}

/// 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.len(), 1);
/// assert!(!result[0].is_empty());
/// ```
///
/// # Errors
///
/// 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<'_>) -> Result<Paths64, Error> {
    validate_request(&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::InvalidPath`] or [`Error::NonFiniteCoordinate`] for invalid
/// input and [`Error::TopologyFailure`] if the arrangement cannot be closed.
pub fn boolean_op_d(request: BooleanRequestD<'_>) -> Result<PathsD, Error> {
    validate_request_d(&request)?;
    crate::boolean::boolean_op_d(request)
}

/// 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(BooleanRequest::new(subjects, clips, ClipType::Intersection, FillRule::EvenOdd))
}

/// 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(BooleanRequest::new(subjects, clips, ClipType::Union, FillRule::EvenOdd))
}

/// 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(BooleanRequest::new(subjects, clips, ClipType::Difference, FillRule::EvenOdd))
}

/// 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(BooleanRequest::new(subjects, clips, ClipType::Xor, FillRule::EvenOdd))
}

/// 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(BooleanRequestD::new(subjects, clips, ClipType::Intersection, FillRule::EvenOdd))
}

/// 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(BooleanRequestD::new(subjects, clips, ClipType::Union, FillRule::EvenOdd))
}

/// 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(BooleanRequestD::new(subjects, clips, ClipType::Difference, FillRule::EvenOdd))
}

/// 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(BooleanRequestD::new(subjects, clips, ClipType::Xor, FillRule::EvenOdd))
}

/// 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.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 double_subject = vec![crate::PointD::new(0.0, 0.0)];
        let double_clip = vec![crate::PointD::new(1.0, 1.0)];
        let double_request = BooleanRequestD::new(
            std::slice::from_ref(&double_subject),
            std::slice::from_ref(&double_clip),
            ClipType::Xor,
            FillRule::Negative,
        );
        assert!(std::ptr::eq(double_request.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 validates_then_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 {
            subjects: &[square],
            clips: &[],
            clip_type: ClipType::Union,
            fill_rule: FillRule::NonZero,
        };
        assert_eq!(validate_request(&request), Ok(()));
        assert_eq!(boolean_op(request).expect("valid request").len(), 1);
    }

    #[test]
    fn rejects_invalid_boolean_input_before_kernel() {
        let request = BooleanRequest {
            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 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 = BooleanRequestD {
            subjects: &[square],
            clips: &[],
            clip_type: ClipType::Union,
            fill_rule: FillRule::NonZero,
        };
        assert!(validate_request_d(&request).is_ok());
        assert_eq!(crate::boolean_op_d(request).expect("valid request").len(), 1);
        let bad = BooleanRequestD {
            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!(validate_request_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));
    }
}