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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
//! Checked, allocation-transparent geometry primitives.

use core::cmp::Ordering;
use num_bigint::BigInt;

use crate::{
    BooleanRequest, ClipType, Error, FillRule, PathKind, PathValidationError, boolean_op,
    boolean_op_d,
};

/// An integer point used by the exact-coordinate API.
///
/// Integer operations preserve these coordinates exactly. Paths are ordinary
/// Rust vectors, so they can be built with `vec![]` and passed by slice.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct Point64 {
    /// Horizontal coordinate.
    pub x: i64,
    /// Vertical coordinate.
    pub y: i64,
}

impl Point64 {
    /// Creates an integer point.
    #[must_use]
    pub const fn new(x: i64, y: i64) -> Self {
        Self { x, y }
    }
}

/// A double-precision point used by the floating-point API.
///
/// [`crate::boolean_op_d`] accepts finite `f64` coordinates. The arrangement
/// kernel keeps the input values exact while it computes intersections, then
/// converts the result back to `f64`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct PointD {
    /// Horizontal coordinate.
    pub x: f64,
    /// Vertical coordinate.
    pub y: f64,
}

impl PointD {
    /// Creates a floating-point point.
    #[must_use]
    pub const fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }
}

/// A sequence of points describing one path.
///
/// The path does not need to repeat its first point at the end. Whether the
/// final point connects to the first is selected with [`PathKind`]. Boolean
/// and triangulation inputs are closed paths; open paths are used by line
/// offsets.
pub type Path64 = Vec<Point64>;

/// A collection of integer paths, usually polygon rings.
pub type Paths64 = Vec<Path64>;

/// A floating-point sequence of points.
pub type PathD = Vec<PointD>;

/// A collection of floating-point paths, usually polygon rings.
pub type PathsD = Vec<PathD>;

#[allow(clippy::cast_precision_loss)]
pub(crate) fn paths64_to_local_d(paths: &[Path64]) -> Result<(Point64, PathsD), Error> {
    let origin = paths64_local_origin(paths)?;
    let mut local = Vec::with_capacity(paths.len());
    for path in paths {
        let mut local_path = Vec::with_capacity(path.len());
        for point in path {
            local_path.push(PointD::new(
                (i128::from(point.x) - i128::from(origin.x)) as f64,
                (i128::from(point.y) - i128::from(origin.y)) as f64,
            ));
        }
        local.push(local_path);
    }
    Ok((origin, local))
}

pub(crate) fn paths64_local_origin(paths: &[Path64]) -> Result<Point64, Error> {
    const MAX_EXACT_INTEGER: i128 = 1 << 53;
    let Some(first) = paths.iter().flatten().next().copied() else {
        return Ok(Point64::new(0, 0));
    };
    let (mut min_x, mut min_y, mut max_x, mut max_y) = (first.x, first.y, first.x, first.y);
    for point in paths.iter().flatten() {
        min_x = min_x.min(point.x);
        min_y = min_y.min(point.y);
        max_x = max_x.max(point.x);
        max_y = max_y.max(point.y);
    }
    if i128::from(max_x) - i128::from(min_x) > MAX_EXACT_INTEGER
        || i128::from(max_y) - i128::from(min_y) > MAX_EXACT_INTEGER
    {
        return Err(Error::ArithmeticOverflow);
    }
    Ok(Point64::new(min_x, min_y))
}

/// The orientation of three points in Cartesian coordinates.
///
/// For points `a`, `b`, and `c`, the result describes the turn from `a -> b`
/// to `b -> c`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum Orientation {
    /// The points turn clockwise.
    Clockwise,
    /// The points are collinear.
    Collinear,
    /// The points turn counter-clockwise.
    CounterClockwise,
}

/// The result of testing a point against a closed path.
///
/// [`point_in_polygon`] uses the even-odd rule for this classification.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum PointLocation {
    /// The point is outside the filled path.
    Outside,
    /// The point is inside the filled path.
    Inside,
    /// The point lies on the path boundary.
    Boundary,
}

/// An axis-aligned integer rectangle.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct Rect64 {
    /// Inclusive lower horizontal bound.
    pub min_x: i64,
    /// Inclusive lower vertical bound.
    pub min_y: i64,
    /// Inclusive upper horizontal bound.
    pub max_x: i64,
    /// Inclusive upper vertical bound.
    pub max_y: i64,
}

impl Rect64 {
    /// Creates a rectangle from two opposite corners.
    ///
    /// The bounds are normalized, so the corner order does not matter.
    #[must_use]
    pub const fn new(first_x: i64, first_y: i64, second_x: i64, second_y: i64) -> Self {
        let (min_x, max_x) =
            if first_x <= second_x { (first_x, second_x) } else { (second_x, first_x) };
        let (min_y, max_y) =
            if first_y <= second_y { (first_y, second_y) } else { (second_y, first_y) };
        Self { min_x, min_y, max_x, max_y }
    }

    fn path(self) -> Path64 {
        vec![
            Point64::new(self.min_x, self.min_y),
            Point64::new(self.max_x, self.min_y),
            Point64::new(self.max_x, self.max_y),
            Point64::new(self.min_x, self.max_y),
        ]
    }
}

/// An axis-aligned floating-point rectangle.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct RectD {
    /// Inclusive lower horizontal bound.
    pub min_x: f64,
    /// Inclusive lower vertical bound.
    pub min_y: f64,
    /// Inclusive upper horizontal bound.
    pub max_x: f64,
    /// Inclusive upper vertical bound.
    pub max_y: f64,
}

impl RectD {
    /// Creates a rectangle from two opposite corners.
    ///
    /// The bounds are normalized, so the corner order does not matter.
    #[must_use]
    pub const fn new(first_x: f64, first_y: f64, second_x: f64, second_y: f64) -> Self {
        let (min_x, max_x) =
            if first_x <= second_x { (first_x, second_x) } else { (second_x, first_x) };
        let (min_y, max_y) =
            if first_y <= second_y { (first_y, second_y) } else { (second_y, first_y) };
        Self { min_x, min_y, max_x, max_y }
    }

    fn path(self) -> PathD {
        vec![
            PointD::new(self.min_x, self.min_y),
            PointD::new(self.max_x, self.min_y),
            PointD::new(self.max_x, self.max_y),
            PointD::new(self.min_x, self.max_y),
        ]
    }
}

/// Validates an integer path's shape contract and coordinate type.
///
/// Empty paths are valid and represent no geometry. A closed path requires at
/// least three points; an open path requires at least two.
///
/// # Errors
///
/// Returns [`Error::InvalidPath`] when a non-empty path is shorter than the
/// minimum for its kind.
pub fn validate_path64(path: &[Point64], kind: PathKind) -> Result<(), Error> {
    let minimum_vertices = match kind {
        PathKind::Closed => 3,
        PathKind::Open => 2,
    };
    if path.is_empty() || path.len() >= minimum_vertices {
        Ok(())
    } else {
        Err(Error::InvalidPath { kind, minimum_vertices, actual_vertices: path.len() })
    }
}

/// Validates every integer path in a collection.
///
/// # Errors
///
/// Returns the first path-shape error found.
pub fn validate_paths64(paths: &[Path64], kind: PathKind) -> Result<(), Error> {
    for path in paths {
        validate_path64(path, kind)?;
    }
    Ok(())
}

/// Validates integer paths and reports the index of the first failing path.
///
/// # Errors
///
/// Returns [`PathValidationError`] with the zero-based path index and original
/// [`Error`]. Integer shape errors do not carry a point index.
pub fn validate_paths64_located(
    paths: &[Path64],
    kind: PathKind,
) -> Result<(), PathValidationError> {
    for (path_index, path) in paths.iter().enumerate() {
        validate_path64(path, kind).map_err(|error| PathValidationError::new(path_index, error))?;
    }
    Ok(())
}

/// Validates a floating-point path's shape and finiteness.
///
/// Empty paths are valid. Closed paths require three points and open paths
/// require two. Consecutive duplicate points are accepted here and can be
/// removed with [`normalize_path_d`].
///
/// # Errors
///
/// Returns [`Error::InvalidPath`] for a too-short path or
/// [`Error::NonFiniteCoordinate`] for NaN/infinite coordinates.
pub fn validate_path_d(path: &[PointD], kind: PathKind) -> Result<(), Error> {
    let minimum_vertices = match kind {
        PathKind::Closed => 3,
        PathKind::Open => 2,
    };
    if !path.is_empty() && path.len() < minimum_vertices {
        return Err(Error::InvalidPath { kind, minimum_vertices, actual_vertices: path.len() });
    }
    for (point_index, point) in path.iter().enumerate() {
        if !point.x.is_finite() || !point.y.is_finite() {
            return Err(Error::NonFiniteCoordinate { point_index });
        }
    }
    Ok(())
}

/// Validates every floating-point path in a collection.
///
/// This is the collection counterpart to [`validate_path_d`]. Empty paths are
/// valid and every coordinate in a non-empty path must be finite.
///
/// # Errors
///
/// Returns the first path-shape or coordinate-finiteness error found.
pub fn validate_paths_d(paths: &[PathD], kind: PathKind) -> Result<(), Error> {
    for path in paths {
        validate_path_d(path, kind)?;
    }
    Ok(())
}

/// Validates floating-point paths with stable path and vertex locations.
///
/// # Errors
///
/// Returns [`PathValidationError`] with the zero-based failing path. A
/// non-finite coordinate also records its zero-based point index.
pub fn validate_paths_d_located(
    paths: &[PathD],
    kind: PathKind,
) -> Result<(), PathValidationError> {
    for (path_index, path) in paths.iter().enumerate() {
        validate_path_d(path, kind).map_err(|error| PathValidationError::new(path_index, error))?;
    }
    Ok(())
}

/// Removes consecutive duplicates and, for closed paths, a repeated closing
/// point. The input is never modified.
///
/// This is a shape normalization helper, not a validity check: a normalized
/// path can still be too short or collinear for a particular operation.
#[must_use]
pub fn normalize_path64(path: &[Point64], kind: PathKind) -> Path64 {
    let mut normalized = Vec::with_capacity(path.len());
    for &point in path {
        if normalized.last().copied() != Some(point) {
            normalized.push(point);
        }
    }
    if kind == PathKind::Closed && normalized.len() > 1 && normalized.first() == normalized.last() {
        normalized.pop();
    }
    normalized
}

/// Removes consecutive duplicate floating-point points and a repeated closing
/// point for closed paths.
///
/// This is a shape normalization helper, not a validity check: a normalized
/// path can still be too short or collinear for a particular operation.
#[must_use]
pub fn normalize_path_d(path: &[PointD], kind: PathKind) -> PathD {
    let mut normalized = Vec::with_capacity(path.len());
    for &point in path {
        if normalized.last().copied() != Some(point) {
            normalized.push(point);
        }
    }
    if kind == PathKind::Closed && normalized.len() > 1 && normalized.first() == normalized.last() {
        normalized.pop();
    }
    normalized
}

/// Returns twice the signed area of an integer path using checked `i128`
/// arithmetic.
///
/// A positive result means counter-clockwise winding in the usual Cartesian
/// coordinate system; a negative result means clockwise winding. Paths with
/// fewer than three points have area zero.
///
/// # Errors
///
/// Returns [`Error::ArithmeticOverflow`] if the exact sum does not fit in an
/// `i128`.
pub fn signed_area2(path: &[Point64]) -> Result<i128, Error> {
    if path.len() < 3 {
        return Ok(0);
    }
    let mut area = 0_i128;
    for (a, b) in path.iter().copied().zip(path.iter().copied().cycle().skip(1)).take(path.len()) {
        let term = i128::from(a.x)
            .checked_mul(i128::from(b.y))
            .and_then(|left| {
                i128::from(a.y)
                    .checked_mul(i128::from(b.x))
                    .and_then(|right| left.checked_sub(right))
            })
            .ok_or(Error::ArithmeticOverflow)?;
        area = area.checked_add(term).ok_or(Error::ArithmeticOverflow)?;
    }
    Ok(area)
}

pub(crate) fn signed_area2_d(path: &[PointD]) -> f64 {
    path.iter()
        .zip(path.iter().cycle().skip(1))
        .take(path.len())
        .map(|(start, end)| start.x * end.y - start.y * end.x)
        .sum()
}

/// Returns the orientation of three integer points.
///
/// The common path uses checked `i128` arithmetic. Coordinates spanning more
/// than that representation can hold are compared with arbitrary-precision
/// integers, so every `i64` input has an exact answer.
#[must_use]
pub fn orientation(a: Point64, b: Point64, c: Point64) -> Orientation {
    match cross_ordering(a, b, c) {
        Ordering::Less => Orientation::Clockwise,
        Ordering::Equal => Orientation::Collinear,
        Ordering::Greater => Orientation::CounterClockwise,
    }
}

/// Classifies an integer point against a closed path using the even-odd rule.
///
/// An empty path has no filled region, so every point is [`PointLocation::Outside`].
///
/// # Errors
///
/// Returns [`Error::InvalidPath`] for a non-empty path with fewer than three
/// points. Predicates are exact across the complete `i64` coordinate range.
pub fn point_in_polygon(point: Point64, path: &[Point64]) -> Result<PointLocation, Error> {
    validate_path64(path, PathKind::Closed)?;
    if path.is_empty() {
        return Ok(PointLocation::Outside);
    }

    let mut inside = false;
    for (a, b) in path.iter().copied().zip(path.iter().copied().cycle().skip(1)).take(path.len()) {
        if point_on_segment(point, a, b) {
            return Ok(PointLocation::Boundary);
        }

        if (a.y > point.y) != (b.y > point.y) {
            let cross = cross_ordering(a, b, point);
            let crosses_right =
                if b.y > a.y { cross == Ordering::Greater } else { cross == Ordering::Less };
            if crosses_right {
                inside = !inside;
            }
        }
    }
    Ok(if inside { PointLocation::Inside } else { PointLocation::Outside })
}

/// Returns a reversed copy of an integer path.
#[must_use]
pub fn reverse_path64(path: &[Point64]) -> Path64 {
    path.iter().copied().rev().collect()
}

/// Returns a reversed copy of a floating-point path.
#[must_use]
pub fn reverse_path_d(path: &[PointD]) -> PathD {
    path.iter().copied().rev().collect()
}

/// Translates an integer path with checked coordinate arithmetic.
///
/// # Errors
///
/// Returns [`Error::ArithmeticOverflow`] if any translated coordinate does not
/// fit in `i64`.
pub fn translate_path64(path: &[Point64], dx: i64, dy: i64) -> Result<Path64, Error> {
    path.iter()
        .map(|point| {
            Ok(Point64::new(
                point.x.checked_add(dx).ok_or(Error::ArithmeticOverflow)?,
                point.y.checked_add(dy).ok_or(Error::ArithmeticOverflow)?,
            ))
        })
        .collect()
}

/// Translates a floating-point path.
///
/// # Errors
///
/// Returns [`Error::NonFiniteCoordinate`] when an input or translated
/// coordinate is not finite.
pub fn translate_path_d(path: &[PointD], dx: f64, dy: f64) -> Result<PathD, Error> {
    if !dx.is_finite() || !dy.is_finite() {
        return Err(Error::NonFiniteCoordinate { point_index: 0 });
    }
    path.iter()
        .enumerate()
        .map(|(point_index, point)| {
            let translated = PointD::new(point.x + dx, point.y + dy);
            if point.x.is_finite()
                && point.y.is_finite()
                && translated.x.is_finite()
                && translated.y.is_finite()
            {
                Ok(translated)
            } else {
                Err(Error::NonFiniteCoordinate { point_index })
            }
        })
        .collect()
}

/// Removes redundant collinear vertices from an integer path.
///
/// Consecutive duplicates and a repeated closing point are removed first. For
/// closed paths the first and last vertices are treated as adjacent; for open
/// paths the two endpoints are preserved.
///
/// # Errors
///
/// Returns [`Error::InvalidPath`] for a non-empty path shorter than the
/// minimum for `kind`. Collinearity is exact across the complete `i64` domain.
pub fn trim_collinear64(path: &[Point64], kind: PathKind) -> Result<Path64, Error> {
    validate_path64(path, kind)?;
    let mut points = normalize_path64(path, kind);
    loop {
        if points.len() < 3 {
            return Ok(points);
        }
        let mut changed = false;
        let mut cleaned = Vec::with_capacity(points.len());
        for index in 0..points.len() {
            let removable = match kind {
                PathKind::Closed => {
                    let previous = points[(index + points.len() - 1) % points.len()];
                    let current = points[index];
                    let next = points[(index + 1) % points.len()];
                    collinear_between64(previous, current, next)
                }
                PathKind::Open if index > 0 && index + 1 < points.len() => {
                    collinear_between64(points[index - 1], points[index], points[index + 1])
                }
                PathKind::Open => false,
            };
            if removable {
                changed = true;
            } else {
                cleaned.push(points[index]);
            }
        }
        points = cleaned;
        if !changed {
            return Ok(points);
        }
    }
}

/// Removes redundant collinear vertices from a floating-point path.
///
/// The test is exact in the input `f64` values; nearly-collinear vertices are
/// retained rather than silently changing the shape.
///
/// # Errors
///
/// Returns [`Error::InvalidPath`] or [`Error::NonFiniteCoordinate`] when the
/// input violates the selected path contract.
pub fn trim_collinear_d(path: &[PointD], kind: PathKind) -> Result<PathD, Error> {
    validate_path_d(path, kind)?;
    let mut points = normalize_path_d(path, kind);
    loop {
        if points.len() < 3 {
            return Ok(points);
        }
        let mut changed = false;
        let mut cleaned = Vec::with_capacity(points.len());
        for index in 0..points.len() {
            let removable = match kind {
                PathKind::Closed => {
                    let previous = points[(index + points.len() - 1) % points.len()];
                    let current = points[index];
                    let next = points[(index + 1) % points.len()];
                    collinear_between_d(previous, current, next)
                }
                PathKind::Open if index > 0 && index + 1 < points.len() => {
                    collinear_between_d(points[index - 1], points[index], points[index + 1])
                }
                PathKind::Open => false,
            };
            if removable {
                changed = true;
            } else {
                cleaned.push(points[index]);
            }
        }
        points = cleaned;
        if !changed {
            return Ok(points);
        }
    }
}

/// Simplifies integer paths by applying a union with the selected fill rule.
///
/// This resolves self-intersections and removes internal boundaries. Use
/// [`trim_collinear64`] instead when only vertex cleanup is desired.
///
/// # Errors
///
/// Returns the same validation, arithmetic, and topology errors as
/// [`crate::boolean_op`].
pub fn simplify_paths64(paths: &[Path64], fill_rule: FillRule) -> Result<Paths64, Error> {
    boolean_op(BooleanRequest::new(paths, &[], ClipType::Union, fill_rule))
        .map(|output| output.closed)
}

/// Simplifies floating-point paths by applying a union with the selected fill
/// rule.
///
/// # Errors
///
/// Returns the same validation, non-finite-coordinate, and topology errors as
/// [`crate::boolean_op_d`].
pub fn simplify_paths_d(paths: &[PathD], fill_rule: FillRule) -> Result<PathsD, Error> {
    boolean_op_d(BooleanRequest::new(paths, &[], ClipType::Union, fill_rule))
        .map(|output| output.closed)
}

/// Clips integer paths to an axis-aligned rectangle.
///
/// # Errors
///
/// Returns the same validation, arithmetic, and topology errors as
/// [`crate::boolean_op`].
pub fn clip_to_rect64(
    paths: &[Path64],
    rectangle: Rect64,
    fill_rule: FillRule,
) -> Result<Paths64, Error> {
    let clip = rectangle.path();
    boolean_op(BooleanRequest::new(
        paths,
        std::slice::from_ref(&clip),
        ClipType::Intersection,
        fill_rule,
    ))
    .map(|output| output.closed)
}

/// Clips floating-point paths to an axis-aligned rectangle.
///
/// # Errors
///
/// Returns [`Error::NonFiniteCoordinate`] when any rectangle bound is not
/// finite, in addition to the usual boolean-operation errors.
pub fn clip_to_rect_d(
    paths: &[PathD],
    rectangle: RectD,
    fill_rule: FillRule,
) -> Result<PathsD, Error> {
    if !rectangle.min_x.is_finite()
        || !rectangle.min_y.is_finite()
        || !rectangle.max_x.is_finite()
        || !rectangle.max_y.is_finite()
    {
        return Err(Error::NonFiniteCoordinate { point_index: 0 });
    }
    let clip = rectangle.path();
    boolean_op_d(BooleanRequest::new(
        paths,
        std::slice::from_ref(&clip),
        ClipType::Intersection,
        fill_rule,
    ))
    .map(|output| output.closed)
}

fn collinear_between64(previous: Point64, current: Point64, next: Point64) -> bool {
    if cross_ordering(previous, current, next) != Ordering::Equal {
        return false;
    }
    current.x >= previous.x.min(next.x)
        && current.x <= previous.x.max(next.x)
        && current.y >= previous.y.min(next.y)
        && current.y <= previous.y.max(next.y)
}

fn collinear_between_d(previous: PointD, current: PointD, next: PointD) -> bool {
    let first_x = current.x - previous.x;
    let first_y = current.y - previous.y;
    let second_x = next.x - current.x;
    let second_y = next.y - current.y;
    let cross = first_x * second_y - first_y * second_x;
    let dot = first_x * second_x + first_y * second_y;
    cross == 0.0 && dot >= 0.0
}

fn checked_cross(a: Point64, b: Point64, c: Point64) -> Result<i128, Error> {
    let first_x = i128::from(b.x) - i128::from(a.x);
    let first_y = i128::from(b.y) - i128::from(a.y);
    let second_x = i128::from(c.x) - i128::from(a.x);
    let second_y = i128::from(c.y) - i128::from(a.y);
    first_x
        .checked_mul(second_y)
        .and_then(|left| first_y.checked_mul(second_x).and_then(|right| left.checked_sub(right)))
        .ok_or(Error::ArithmeticOverflow)
}

pub(crate) fn cross_ordering(a: Point64, b: Point64, c: Point64) -> Ordering {
    if let Ok(cross) = checked_cross(a, b, c) {
        return cross.cmp(&0);
    }
    let first_x = BigInt::from(i128::from(b.x) - i128::from(a.x));
    let first_y = BigInt::from(i128::from(b.y) - i128::from(a.y));
    let second_x = BigInt::from(i128::from(c.x) - i128::from(a.x));
    let second_y = BigInt::from(i128::from(c.y) - i128::from(a.y));
    (first_x * second_y - first_y * second_x).cmp(&BigInt::from(0))
}

fn point_on_segment(point: Point64, a: Point64, b: Point64) -> bool {
    if cross_ordering(a, b, point) != Ordering::Equal {
        return false;
    }
    let px = i128::from(point.x);
    let py = i128::from(point.y);
    let min_x = i128::from(a.x).min(i128::from(b.x));
    let max_x = i128::from(a.x).max(i128::from(b.x));
    let min_y = i128::from(a.y).min(i128::from(b.y));
    let max_y = i128::from(a.y).max(i128::from(b.y));
    px >= min_x && px <= max_x && py >= min_y && py <= max_y
}

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

    const A: Point64 = Point64::new(0, 0);
    const B: Point64 = Point64::new(10, 0);
    const C: Point64 = Point64::new(10, 10);

    #[test]
    fn validates_empty_and_minimum_shapes() {
        assert!(validate_path64(&[], PathKind::Closed).is_ok());
        assert!(validate_path64(&[A], PathKind::Closed).is_err());
        assert!(validate_path64(&[A, B], PathKind::Closed).is_err());
        assert!(validate_path64(&[A, B], PathKind::Open).is_ok());
        assert!(validate_path64(&[A], PathKind::Open).is_err());
        assert!(validate_paths64(&[vec![A, B]], PathKind::Open).is_ok());
        assert!(validate_paths64(&[vec![A]], PathKind::Closed).is_err());

        assert!(validate_paths_d(&[], PathKind::Closed).is_ok());
        assert!(
            validate_paths_d(&[vec![PointD::new(0.0, 0.0), PointD::new(1.0, 0.0)]], PathKind::Open)
                .is_ok()
        );
        assert!(validate_paths_d(&[vec![PointD::new(0.0, 0.0)]], PathKind::Closed).is_err());
    }

    #[test]
    fn converts_integer_paths_in_one_exact_local_coordinate_frame() {
        assert_eq!(paths64_to_local_d(&[]), Ok((Point64::new(0, 0), Vec::new())));
        let origin = i64::MAX - 100;
        let paths = [vec![
            Point64::new(origin, origin),
            Point64::new(origin + 10, origin),
            Point64::new(origin + 10, origin + 20),
        ]];
        let (actual_origin, local) = paths64_to_local_d(&paths).unwrap();
        assert_eq!(actual_origin, Point64::new(origin, origin));
        assert_eq!(local[0][2], PointD::new(10.0, 20.0));

        let exact_limit = 1_i64 << 53;
        let ring = vec![
            Point64::new(-exact_limit, 0),
            Point64::new(0, exact_limit),
            Point64::new(exact_limit, 0),
        ];
        for start in 0..ring.len() {
            let mut rotated = ring.clone();
            rotated.rotate_left(start);
            assert_eq!(
                paths64_to_local_d(&[rotated]),
                Err(Error::ArithmeticOverflow),
                "ring start {start}"
            );
        }

        let within_exact_span = [vec![
            Point64::new(-exact_limit / 2, 0),
            Point64::new(0, exact_limit / 2),
            Point64::new(exact_limit / 2, 0),
        ]];
        assert!(paths64_to_local_d(&within_exact_span).is_ok());

        let excessive_span =
            [vec![Point64::new(i64::MIN, 0), Point64::new(i64::MAX, 0), Point64::new(0, 1)]];
        assert_eq!(paths64_to_local_d(&excessive_span), Err(Error::ArithmeticOverflow));
        let excessive_y_span =
            [vec![Point64::new(0, i64::MIN), Point64::new(0, i64::MAX), Point64::new(1, 0)]];
        assert_eq!(paths64_to_local_d(&excessive_y_span), Err(Error::ArithmeticOverflow));
    }

    #[test]
    fn validates_double_finiteness() {
        assert!(validate_path_d(&[], PathKind::Closed).is_ok());
        assert!(validate_path_d(&[PointD::new(0.0, 0.0)], PathKind::Closed).is_err());
        assert!(
            validate_path_d(&[PointD::new(0.0, 0.0), PointD::new(1.0, 0.0)], PathKind::Open)
                .is_ok()
        );
        let error = validate_path_d(
            &[PointD::new(0.0, 0.0), PointD::new(f64::NAN, 0.0), PointD::new(1.0, 1.0)],
            PathKind::Closed,
        )
        .expect_err("NaN must be rejected");
        assert_eq!(error, Error::NonFiniteCoordinate { point_index: 1 });
        assert!(
            validate_path_d(
                &[PointD::new(0.0, 0.0), PointD::new(1.0, 0.0), PointD::new(f64::INFINITY, 1.0),],
                PathKind::Closed,
            )
            .is_err()
        );
        assert!(
            validate_path_d(
                &[PointD::new(0.0, 0.0), PointD::new(1.0, 0.0), PointD::new(0.0, f64::INFINITY)],
                PathKind::Closed,
            )
            .is_err()
        );
    }

    #[test]
    fn located_validation_reports_path_and_point_indices() {
        let integer_error =
            validate_paths64_located(&[vec![A, B, C], vec![A]], PathKind::Closed).unwrap_err();
        assert_eq!(integer_error.path_index(), 1);
        assert_eq!(integer_error.point_index(), None);
        assert!(integer_error.to_string().starts_with("path 1:"));
        assert!(std::error::Error::source(&integer_error).is_some());
        assert!(matches!(integer_error.error(), Error::InvalidPath { .. }));
        assert!(matches!(integer_error.into_error(), Error::InvalidPath { .. }));

        let floating_error = validate_paths_d_located(
            &[
                vec![PointD::new(0.0, 0.0), PointD::new(1.0, 0.0), PointD::new(0.0, 1.0)],
                vec![PointD::new(0.0, 0.0), PointD::new(1.0, 0.0), PointD::new(f64::NAN, 1.0)],
            ],
            PathKind::Closed,
        )
        .unwrap_err();
        assert_eq!(floating_error.path_index(), 1);
        assert_eq!(floating_error.point_index(), Some(2));
        assert!(floating_error.to_string().starts_with("path 1, point 2:"));
        assert!(validate_paths64_located(&[], PathKind::Closed).is_ok());
        assert!(validate_paths_d_located(&[], PathKind::Closed).is_ok());
    }

    #[test]
    fn normalizes_integer_and_double_paths() {
        let input = [A, A, B, B, C, A, A];
        assert_eq!(normalize_path64(&input, PathKind::Closed), vec![A, B, C]);
        assert_eq!(normalize_path64(&[A, B, C], PathKind::Closed), vec![A, B, C]);
        assert_eq!(normalize_path64(&input, PathKind::Open), vec![A, B, C, A]);
        assert!(normalize_path64(&[], PathKind::Closed).is_empty());

        let doubles = [PointD::new(0.0, 0.0), PointD::new(0.0, 0.0), PointD::new(1.0, 0.0)];
        assert_eq!(normalize_path_d(&doubles, PathKind::Open).len(), 2);
        let closed_doubles = [
            PointD::new(0.0, 0.0),
            PointD::new(1.0, 0.0),
            PointD::new(0.0, 1.0),
            PointD::new(0.0, 0.0),
        ];
        assert_eq!(normalize_path_d(&closed_doubles, PathKind::Closed).len(), 3);
        assert_eq!(
            normalize_path_d(
                &[PointD::new(0.0, 0.0), PointD::new(1.0, 0.0), PointD::new(0.0, 1.0)],
                PathKind::Closed,
            )
            .len(),
            3
        );
        assert_eq!(normalize_path_d(&[], PathKind::Closed), Vec::<PointD>::new());
    }

    #[test]
    fn computes_area_and_orientation() {
        let square = vec![A, B, C, Point64::new(0, 10)];
        assert_eq!(signed_area2(&square), Ok(200));
        let mut reverse = square.clone();
        reverse.reverse();
        assert_eq!(signed_area2(&reverse), Ok(-200));
        let square_d = vec![
            PointD::new(0.0, 0.0),
            PointD::new(10.0, 0.0),
            PointD::new(10.0, 10.0),
            PointD::new(0.0, 10.0),
        ];
        assert!((signed_area2_d(&square_d) - 200.0).abs() < f64::EPSILON);
        assert_eq!(signed_area2(&[A, B]), Ok(0));
        assert_eq!(orientation(A, B, C), Orientation::CounterClockwise);
        assert_eq!(orientation(C, B, A), Orientation::Clockwise);
        assert_eq!(orientation(A, B, Point64::new(20, 0)), Orientation::Collinear);
    }

    #[test]
    fn reports_arithmetic_overflow() {
        let extreme = vec![
            Point64::new(i64::MAX, i64::MAX),
            Point64::new(i64::MIN, i64::MAX),
            Point64::new(i64::MIN, i64::MIN),
            Point64::new(i64::MAX, i64::MIN),
        ];
        assert_eq!(signed_area2(&extreme), Err(Error::ArithmeticOverflow));
        assert_eq!(
            signed_area2(&[
                Point64::new(i64::MIN, i64::MIN),
                Point64::new(i64::MIN, 1),
                Point64::new(i64::MAX, i64::MIN),
            ]),
            Err(Error::ArithmeticOverflow)
        );
        assert_eq!(
            orientation(
                Point64::new(i64::MIN, i64::MIN),
                Point64::new(i64::MAX, i64::MAX),
                Point64::new(i64::MIN, i64::MAX)
            ),
            Orientation::CounterClockwise
        );
        assert_eq!(
            checked_cross(
                Point64::new(i64::MIN, i64::MIN),
                Point64::new(i64::MIN, i64::MAX),
                Point64::new(i64::MAX, i64::MIN),
            ),
            Err(Error::ArithmeticOverflow)
        );
        assert_eq!(
            checked_cross(
                Point64::new(i64::MIN, -1),
                Point64::new(-1, i64::MIN),
                Point64::new(i64::MAX, i64::MAX),
            ),
            Err(Error::ArithmeticOverflow)
        );
    }

    #[test]
    fn classifies_points_and_empty_paths() {
        let square = vec![A, B, C, Point64::new(0, 10)];
        assert_eq!(point_in_polygon(Point64::new(5, 5), &square), Ok(PointLocation::Inside));
        assert_eq!(point_in_polygon(Point64::new(15, 5), &square), Ok(PointLocation::Outside));
        assert_eq!(point_in_polygon(Point64::new(20, 0), &square), Ok(PointLocation::Outside));
        assert_eq!(point_in_polygon(Point64::new(-1, 0), &square), Ok(PointLocation::Outside));
        assert_eq!(point_in_polygon(Point64::new(10, 20), &square), Ok(PointLocation::Outside));
        assert_eq!(point_in_polygon(Point64::new(10, -1), &square), Ok(PointLocation::Outside));
        assert_eq!(point_in_polygon(Point64::new(10, 5), &square), Ok(PointLocation::Boundary));
        assert_eq!(point_in_polygon(A, &[]), Ok(PointLocation::Outside));
        assert!(point_in_polygon(A, &[A, B]).is_err());

        let extreme = vec![
            Point64::new(i64::MAX, i64::MAX),
            Point64::new(i64::MIN, i64::MAX),
            Point64::new(i64::MIN, i64::MIN),
            Point64::new(i64::MAX, i64::MIN),
        ];
        assert_eq!(point_in_polygon(Point64::new(0, 0), &extreme), Ok(PointLocation::Inside));
        assert_eq!(
            point_in_polygon(Point64::new(i64::MIN, 0), &extreme),
            Ok(PointLocation::Boundary)
        );
    }

    #[test]
    fn transforms_and_trims_paths() {
        assert!(!collinear_between64(Point64::new(0, 0), Point64::new(0, 2), Point64::new(0, 1),));
        assert!(!collinear_between64(Point64::new(0, 0), Point64::new(0, -1), Point64::new(0, 1),));
        let square = vec![A, B, C, Point64::new(0, 10)];
        let with_collinear = vec![A, Point64::new(5, 0), B, C, Point64::new(0, 10), A];
        assert_eq!(trim_collinear64(&with_collinear, PathKind::Closed), Ok(square.clone()));
        assert_eq!(
            trim_collinear64(&[A, Point64::new(5, 0), B, C], PathKind::Open,),
            Ok(vec![A, B, C])
        );
        assert_eq!(reverse_path64(&square), vec![Point64::new(0, 10), C, B, A]);
        assert_eq!(
            translate_path64(&square, 5, -2),
            Ok(vec![
                Point64::new(5, -2),
                Point64::new(15, -2),
                Point64::new(15, 8),
                Point64::new(5, 8),
            ])
        );
        assert_eq!(
            translate_path64(&[Point64::new(i64::MAX, 0)], 1, 0),
            Err(Error::ArithmeticOverflow)
        );

        let double_path = vec![
            PointD::new(0.0, 0.0),
            PointD::new(10.0, 0.0),
            PointD::new(10.0, 10.0),
            PointD::new(0.0, 10.0),
        ];
        assert_eq!(
            reverse_path_d(&double_path),
            double_path.iter().copied().rev().collect::<Vec<_>>()
        );
        assert_eq!(
            trim_collinear_d(
                &[
                    PointD::new(0.0, 0.0),
                    PointD::new(5.0, 0.0),
                    PointD::new(10.0, 0.0),
                    PointD::new(10.0, 10.0),
                ],
                PathKind::Open,
            )
            .unwrap()
            .len(),
            3
        );
        assert_eq!(
            translate_path_d(&[PointD::new(1.0, 2.0)], 3.0, -1.0),
            Ok(vec![PointD::new(4.0, 1.0)])
        );
        assert!(translate_path_d(&[PointD::new(1.0, 2.0)], f64::INFINITY, 0.0).is_err());
        assert!(translate_path_d(&[PointD::new(1.0, 2.0)], 0.0, f64::INFINITY).is_err());
        assert!(translate_path_d(&[PointD::new(f64::INFINITY, 2.0)], 0.0, 0.0).is_err());
        assert!(translate_path_d(&[PointD::new(1.0, f64::INFINITY)], 0.0, 0.0).is_err());
        assert!(translate_path_d(&[PointD::new(f64::MAX, 0.0)], f64::MAX, 0.0).is_err());
        assert!(translate_path_d(&[PointD::new(0.0, f64::MAX)], 0.0, f64::MAX).is_err());
        let collinear_closed64 = [A, Point64::new(5, 0), B];
        assert_eq!(trim_collinear64(&collinear_closed64, PathKind::Closed), Ok(vec![A, B]));
        let collinear_closed_d =
            [PointD::new(0.0, 0.0), PointD::new(5.0, 0.0), PointD::new(10.0, 0.0)];
        assert_eq!(
            trim_collinear_d(&collinear_closed_d, PathKind::Closed),
            Ok(vec![PointD::new(0.0, 0.0), PointD::new(10.0, 0.0)])
        );
    }

    #[test]
    fn simplifies_and_clips_to_rectangles() {
        let bow_tie = vec![
            Point64::new(0, 0),
            Point64::new(20, 20),
            Point64::new(0, 20),
            Point64::new(20, 0),
        ];
        let simplified = simplify_paths64(&[bow_tie], FillRule::EvenOdd).unwrap();
        assert_eq!(simplified.len(), 2);
        assert_eq!(
            simplified.iter().map(|path| signed_area2(path).unwrap().abs()).sum::<i128>(),
            400
        );

        let subject = vec![
            Point64::new(0, 0),
            Point64::new(20, 0),
            Point64::new(20, 20),
            Point64::new(0, 20),
        ];
        let clipped =
            clip_to_rect64(&[subject], Rect64::new(5, 5, 15, 15), FillRule::EvenOdd).unwrap();
        assert_eq!(clipped.len(), 1);
        assert_eq!(signed_area2(&clipped[0]), Ok(200));
        assert_eq!(Rect64::new(15, 15, 5, 5), Rect64::new(5, 5, 15, 15));

        let subject_d = vec![
            PointD::new(0.0, 0.0),
            PointD::new(20.0, 0.0),
            PointD::new(20.0, 20.0),
            PointD::new(0.0, 20.0),
        ];
        let clipped_d =
            clip_to_rect_d(&[subject_d], RectD::new(5.5, 5.5, 15.5, 15.5), FillRule::EvenOdd)
                .unwrap();
        assert_eq!(clipped_d.len(), 1);
        assert!((signed_area2_d(&clipped_d[0]).abs() - 200.0).abs() < f64::EPSILON);
        assert!(
            clip_to_rect_d(&[], RectD::new(f64::NAN, 0.0, 1.0, 1.0), FillRule::EvenOdd,).is_err()
        );
        for rectangle in [
            RectD::new(0.0, f64::NAN, 1.0, 1.0),
            RectD::new(0.0, 0.0, f64::NAN, 1.0),
            RectD::new(0.0, 0.0, 1.0, f64::NAN),
        ] {
            assert!(clip_to_rect_d(&[], rectangle, FillRule::EvenOdd).is_err());
        }
    }
}