clipper2-rust 1.0.3

Pure Rust port of the Clipper2 polygon clipping and offsetting library
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
/*******************************************************************************
* Comprehensive tests for the offset module                                   *
* Direct port of C++ test cases from TestOffsets.cpp and                       *
* TestOffsetOrientation.cpp                                                   *
* Date: 2025                                                                  *
*******************************************************************************/

use crate::core::{area, constants, Path64, Paths64, Point64};
use crate::offset::{ClipperOffset, EndType, JoinType};

// ---------------------------------------------------------------------------
// Helper functions
// ---------------------------------------------------------------------------

/// Create a simple square path centered at origin
fn make_square(size: i64) -> Path64 {
    vec![
        Point64::new(-size, -size),
        Point64::new(size, -size),
        Point64::new(size, size),
        Point64::new(-size, size),
    ]
}

/// Create a diamond (rotated square) path
fn make_diamond(size: i64) -> Path64 {
    vec![
        Point64::new(0, -size),
        Point64::new(size, 0),
        Point64::new(0, size),
        Point64::new(-size, 0),
    ]
}

/// Create a triangle path
fn make_triangle(size: i64) -> Path64 {
    vec![
        Point64::new(0, -size),
        Point64::new(size, size),
        Point64::new(-size, size),
    ]
}

/// Create a simple open path (horizontal line)
fn make_open_line(length: i64) -> Path64 {
    vec![Point64::new(0, 0), Point64::new(length, 0)]
}

/// Create an L-shaped open path
fn make_l_path(size: i64) -> Path64 {
    vec![
        Point64::new(0, 0),
        Point64::new(size, 0),
        Point64::new(size, size),
    ]
}

// ---------------------------------------------------------------------------
// Basic enum tests
// ---------------------------------------------------------------------------

#[test]
fn test_join_type_values() {
    // Verify all JoinType variants exist and are distinct
    let types = [
        JoinType::Square,
        JoinType::Bevel,
        JoinType::Round,
        JoinType::Miter,
    ];
    for i in 0..types.len() {
        for j in (i + 1)..types.len() {
            assert_ne!(types[i], types[j]);
        }
    }
}

#[test]
fn test_end_type_values() {
    // Verify all EndType variants exist and are distinct
    let types = [
        EndType::Polygon,
        EndType::Joined,
        EndType::Butt,
        EndType::Square,
        EndType::Round,
    ];
    for i in 0..types.len() {
        for j in (i + 1)..types.len() {
            assert_ne!(types[i], types[j]);
        }
    }
}

// ---------------------------------------------------------------------------
// Construction and configuration tests
// ---------------------------------------------------------------------------

#[test]
fn test_clipper_offset_default_construction() {
    let co = ClipperOffset::new_default();
    assert_eq!(co.error_code(), 0);
    assert_eq!(co.miter_limit(), 2.0);
    assert_eq!(co.arc_tolerance(), 0.0);
    assert!(!co.preserve_collinear());
    assert!(!co.reverse_solution());
}

#[test]
fn test_clipper_offset_custom_construction() {
    let co = ClipperOffset::new(3.0, 0.5, true, true);
    assert_eq!(co.miter_limit(), 3.0);
    assert_eq!(co.arc_tolerance(), 0.5);
    assert!(co.preserve_collinear());
    assert!(co.reverse_solution());
}

#[test]
fn test_clipper_offset_setters() {
    let mut co = ClipperOffset::new_default();

    co.set_miter_limit(5.0);
    assert_eq!(co.miter_limit(), 5.0);

    co.set_arc_tolerance(1.0);
    assert_eq!(co.arc_tolerance(), 1.0);

    co.set_preserve_collinear(true);
    assert!(co.preserve_collinear());

    co.set_reverse_solution(true);
    assert!(co.reverse_solution());
}

#[test]
fn test_clipper_offset_clear() {
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);
    co.clear();

    // After clearing, executing should produce no output
    let mut result = Paths64::new();
    co.execute(10.0, &mut result);
    assert!(result.is_empty());
}

// ---------------------------------------------------------------------------
// Polygon offset tests (closed paths)
// ---------------------------------------------------------------------------

#[test]
fn test_offset_square_inflate_miter() {
    // Inflating a square with miter joins should produce a larger square-like shape
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty(), "Result should not be empty");
    // The inflated area should be larger than the original
    let original_area = area(&square).abs();
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    assert!(
        result_area > original_area,
        "Inflated area {} should be > original area {}",
        result_area,
        original_area
    );
}

#[test]
fn test_offset_square_shrink_miter() {
    // Shrinking a square should produce a smaller square-like shape.
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(-10.0, &mut result);

    assert!(
        !result.is_empty(),
        "Shrinking a square by 10 should produce a non-empty result"
    );
    let original_area = area(&square).abs();
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    assert!(
        result_area < original_area,
        "Shrunk area {} should be < original area {}",
        result_area,
        original_area
    );
}

#[test]
fn test_offset_square_inflate_round() {
    // Inflating with round joins should produce a rounded shape
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Round, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty(), "Result should not be empty");
    let original_area = area(&square).abs();
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    // Round joins produce slightly less area than miter
    assert!(result_area > original_area);
}

#[test]
fn test_offset_square_inflate_bevel() {
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Bevel, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty(), "Result should not be empty");
    let original_area = area(&square).abs();
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    assert!(result_area > original_area);
}

#[test]
fn test_offset_square_inflate_square_join() {
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Square, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty(), "Result should not be empty");
    let original_area = area(&square).abs();
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    assert!(result_area > original_area);
}

#[test]
fn test_offset_triangle_inflate() {
    let mut co = ClipperOffset::new_default();
    let triangle = make_triangle(100);
    co.add_path(&triangle, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
    let original_area = area(&triangle).abs();
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    assert!(result_area > original_area);
}

#[test]
fn test_offset_diamond_inflate() {
    let mut co = ClipperOffset::new_default();
    let diamond = make_diamond(100);
    co.add_path(&diamond, JoinType::Round, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(20.0, &mut result);

    assert!(!result.is_empty());
    let original_area = area(&diamond).abs();
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    assert!(result_area > original_area);
}

// ---------------------------------------------------------------------------
// Shrinking to nothing
// ---------------------------------------------------------------------------

#[test]
fn test_offset_shrink_to_nothing() {
    // Shrinking a small square by more than its half-width should result in nothing
    let mut co = ClipperOffset::new_default();
    let square = make_square(10);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(-20.0, &mut result);

    // The square is 20x20, shrinking by 20 should eliminate it
    assert!(
        result.is_empty(),
        "Over-shrunk square should produce empty result"
    );
}

// ---------------------------------------------------------------------------
// Insignificant delta
// ---------------------------------------------------------------------------

#[test]
fn test_offset_insignificant_delta() {
    // Delta < 0.5 should just return the original paths
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(0.1, &mut result);

    assert_eq!(result.len(), 1, "Should return the original path");
    assert_eq!(result[0].len(), square.len());
}

// ---------------------------------------------------------------------------
// Open path tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_open_path_butt_end() {
    let mut co = ClipperOffset::new_default();
    let line = make_open_line(200);
    co.add_path(&line, JoinType::Miter, EndType::Butt);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty(), "Open path offset should produce result");
    // The result should be a closed polygon surrounding the line
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    // Expected: approximately 200 * 20 = 4000 (line length * 2*delta)
    assert!(
        result_area > 3000.0,
        "Area {} should be approximately 4000",
        result_area
    );
}

#[test]
fn test_offset_open_path_square_end() {
    let mut co = ClipperOffset::new_default();
    let line = make_open_line(200);
    co.add_path(&line, JoinType::Miter, EndType::Square);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    // Square ends extend beyond the line, so area should be > butt
    assert!(result_area > 4000.0);
}

#[test]
fn test_offset_open_path_round_end() {
    let mut co = ClipperOffset::new_default();
    let line = make_open_line(200);
    co.add_path(&line, JoinType::Round, EndType::Round);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    // Round ends add semicircles, area should be > butt but < square
    assert!(result_area > 3000.0);
}

#[test]
fn test_offset_open_path_joined_end() {
    let mut co = ClipperOffset::new_default();
    let line = make_open_line(200);
    co.add_path(&line, JoinType::Miter, EndType::Joined);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
}

// ---------------------------------------------------------------------------
// L-shaped path tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_l_shape_miter() {
    let mut co = ClipperOffset::new_default();
    let l_path = make_l_path(100);
    co.add_path(&l_path, JoinType::Miter, EndType::Butt);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
}

#[test]
fn test_offset_l_shape_round() {
    let mut co = ClipperOffset::new_default();
    let l_path = make_l_path(100);
    co.add_path(&l_path, JoinType::Round, EndType::Round);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
}

// ---------------------------------------------------------------------------
// Single point tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_single_point_round() {
    let mut co = ClipperOffset::new_default();
    let point = vec![Point64::new(100, 100)];
    co.add_path(&point, JoinType::Round, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(20.0, &mut result);

    assert!(
        !result.is_empty(),
        "Single point offset should produce a circle"
    );
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    // Expected: approximately PI * 20^2 = ~1257
    let expected_area = constants::PI * 20.0 * 20.0;
    assert!(
        (result_area - expected_area).abs() < expected_area * 0.15,
        "Circle area {} should be approximately {}",
        result_area,
        expected_area
    );
}

#[test]
fn test_offset_single_point_square() {
    let mut co = ClipperOffset::new_default();
    let point = vec![Point64::new(100, 100)];
    co.add_path(&point, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(20.0, &mut result);

    assert!(
        !result.is_empty(),
        "Single point offset should produce a square"
    );
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    // Expected: approximately (2*20)^2 = 1600
    // The rect is (x-d, y-d) to (x+d, y+d) where d = ceil(20) = 20
    let expected_area = (2.0 * 20.0) * (2.0 * 20.0);
    assert!(
        (result_area - expected_area).abs() < expected_area * 0.15,
        "Square area {} should be approximately {}",
        result_area,
        expected_area
    );
}

#[test]
fn test_offset_single_point_small_delta() {
    // Delta < 1 for single point should produce nothing
    let mut co = ClipperOffset::new_default();
    let point = vec![Point64::new(100, 100)];
    co.add_path(&point, JoinType::Round, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(0.5, &mut result);

    // group_delta < 1 means the single-point path is skipped
    assert!(result.is_empty());
}

// ---------------------------------------------------------------------------
// Multiple paths tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_multiple_paths() {
    let mut co = ClipperOffset::new_default();

    let square1 = vec![
        Point64::new(0, 0),
        Point64::new(100, 0),
        Point64::new(100, 100),
        Point64::new(0, 100),
    ];
    let square2 = vec![
        Point64::new(200, 0),
        Point64::new(300, 0),
        Point64::new(300, 100),
        Point64::new(200, 100),
    ];

    co.add_paths(
        &vec![square1.clone(), square2.clone()],
        JoinType::Miter,
        EndType::Polygon,
    );

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(
        result.len() >= 2,
        "Should produce at least 2 output paths for 2 non-overlapping inputs"
    );
}

#[test]
fn test_offset_add_paths_empty() {
    let mut co = ClipperOffset::new_default();
    let empty: Paths64 = vec![];
    co.add_paths(&empty, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);
    assert!(result.is_empty());
}

// ---------------------------------------------------------------------------
// Miter limit tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_miter_limit() {
    // With a low miter limit, sharp corners should be clipped
    let mut co_low = ClipperOffset::new(1.0, 0.0, false, false);
    let mut co_high = ClipperOffset::new(10.0, 0.0, false, false);

    let triangle = make_triangle(100);
    co_low.add_path(&triangle, JoinType::Miter, EndType::Polygon);
    co_high.add_path(&triangle, JoinType::Miter, EndType::Polygon);

    let mut result_low = Paths64::new();
    let mut result_high = Paths64::new();
    co_low.execute(10.0, &mut result_low);
    co_high.execute(10.0, &mut result_high);

    assert!(!result_low.is_empty());
    assert!(!result_high.is_empty());
    // Higher miter limit allows sharper corners = larger area
    let area_low: f64 = result_low.iter().map(|p| area(p).abs()).sum();
    let area_high: f64 = result_high.iter().map(|p| area(p).abs()).sum();
    assert!(
        area_high >= area_low,
        "Higher miter limit should produce >= area: high={}, low={}",
        area_high,
        area_low
    );
}

// ---------------------------------------------------------------------------
// Orientation tests (matching TestOffsetOrientation.cpp)
// ---------------------------------------------------------------------------

#[test]
fn test_offset_orientation_positive() {
    // Counter-clockwise polygon (positive area) should stay counter-clockwise after inflate
    let mut co = ClipperOffset::new_default();
    let ccw_square = vec![
        Point64::new(0, 0),
        Point64::new(100, 0),
        Point64::new(100, 100),
        Point64::new(0, 100),
    ];
    let original_area = area(&ccw_square);
    co.add_path(&ccw_square, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
    // The result orientation should match the input orientation
    let result_area = area(&result[0]);
    assert!(
        (original_area > 0.0) == (result_area > 0.0),
        "Orientation should be preserved: original={}, result={}",
        original_area,
        result_area
    );
}

#[test]
fn test_offset_orientation_negative() {
    // Clockwise polygon (negative area) should stay clockwise after inflate
    let mut co = ClipperOffset::new_default();
    let cw_square = vec![
        Point64::new(0, 100),
        Point64::new(100, 100),
        Point64::new(100, 0),
        Point64::new(0, 0),
    ];
    let original_area = area(&cw_square);
    co.add_path(&cw_square, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
    let result_area = area(&result[0]);
    assert!(
        (original_area > 0.0) == (result_area > 0.0),
        "Orientation should be preserved: original={}, result={}",
        original_area,
        result_area
    );
}

// ---------------------------------------------------------------------------
// Two-point path tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_two_point_polygon() {
    // A two-point "polygon" should be treated as a line with 180-degree joins
    let mut co = ClipperOffset::new_default();
    let line = vec![Point64::new(0, 0), Point64::new(100, 0)];
    co.add_path(&line, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    // Should produce a shape around the line
    assert!(!result.is_empty());
}

#[test]
fn test_offset_two_point_joined() {
    // Two-point path with Joined end type
    let mut co = ClipperOffset::new_default();
    let line = vec![Point64::new(0, 0), Point64::new(100, 0)];
    co.add_path(&line, JoinType::Miter, EndType::Joined);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
}

// ---------------------------------------------------------------------------
// Arc tolerance tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_arc_tolerance_small() {
    // Small arc tolerance should produce more vertices (smoother curves)
    let mut co_small = ClipperOffset::new(2.0, 0.5, false, false);
    let mut co_large = ClipperOffset::new(2.0, 5.0, false, false);

    let square = make_square(100);
    co_small.add_path(&square, JoinType::Round, EndType::Polygon);
    co_large.add_path(&square, JoinType::Round, EndType::Polygon);

    let mut result_small = Paths64::new();
    let mut result_large = Paths64::new();
    co_small.execute(20.0, &mut result_small);
    co_large.execute(20.0, &mut result_large);

    assert!(!result_small.is_empty());
    assert!(!result_large.is_empty());

    // Smaller arc tolerance -> more vertices (smoother curves)
    let verts_small: usize = result_small.iter().map(|p| p.len()).sum();
    let verts_large: usize = result_large.iter().map(|p| p.len()).sum();
    assert!(
        verts_small >= verts_large,
        "Small arc tolerance should produce more vertices: {} vs {}",
        verts_small,
        verts_large
    );
}

// ---------------------------------------------------------------------------
// Reverse solution tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_reverse_solution() {
    let mut co_normal = ClipperOffset::new_default();
    let mut co_reversed = ClipperOffset::new(2.0, 0.0, false, true);

    let square = make_square(100);
    co_normal.add_path(&square, JoinType::Miter, EndType::Polygon);
    co_reversed.add_path(&square, JoinType::Miter, EndType::Polygon);

    let mut result_normal = Paths64::new();
    let mut result_reversed = Paths64::new();
    co_normal.execute(10.0, &mut result_normal);
    co_reversed.execute(10.0, &mut result_reversed);

    assert!(!result_normal.is_empty());
    assert!(!result_reversed.is_empty());

    // Reversed solution should have opposite orientation
    let area_normal = area(&result_normal[0]);
    let area_reversed = area(&result_reversed[0]);
    assert!(
        area_normal * area_reversed < 0.0,
        "Reversed solution should have opposite orientation: normal={}, reversed={}",
        area_normal,
        area_reversed
    );
}

// ---------------------------------------------------------------------------
// Delta callback (variable offset) tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_with_delta_callback() {
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);

    // Constant callback - should behave the same as fixed delta
    type DeltaCallback = Box<dyn Fn(&Path64, &crate::core::PathD, usize, usize) -> f64>;
    let cb: DeltaCallback = Box::new(|_path, _norms, _j, _k| 10.0);

    let mut result = Paths64::new();
    co.execute_with_callback(cb, &mut result);

    assert!(!result.is_empty());
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    let original_area = area(&square).abs();
    assert!(result_area > original_area);
}

// ---------------------------------------------------------------------------
// Edge case tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_empty_path() {
    let mut co = ClipperOffset::new_default();
    let empty: Path64 = vec![];
    co.add_path(&empty, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    // Empty path after stripping duplicates should produce nothing meaningful
    assert!(result.is_empty());
}

#[test]
fn test_offset_no_groups() {
    let mut co = ClipperOffset::new_default();
    let mut result = Paths64::new();
    co.execute(10.0, &mut result);
    assert!(result.is_empty());
}

#[test]
fn test_offset_zero_delta() {
    // Zero delta should return original paths
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(0.0, &mut result);

    assert_eq!(result.len(), 1);
}

// ---------------------------------------------------------------------------
// Large offset tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_large_inflate() {
    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(500.0, &mut result);

    assert!(!result.is_empty());
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    // The result should be much larger
    assert!(result_area > 1_000_000.0);
}

// ---------------------------------------------------------------------------
// Concave polygon tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_concave_polygon() {
    // An L-shaped polygon (concave)
    let mut co = ClipperOffset::new_default();
    let l_shape = vec![
        Point64::new(0, 0),
        Point64::new(200, 0),
        Point64::new(200, 100),
        Point64::new(100, 100),
        Point64::new(100, 200),
        Point64::new(0, 200),
    ];
    co.add_path(&l_shape, JoinType::Miter, EndType::Polygon);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    assert!(!result.is_empty());
    let original_area = area(&l_shape).abs();
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    assert!(result_area > original_area);
}

// ---------------------------------------------------------------------------
// PolyTree output test
// ---------------------------------------------------------------------------

#[test]
fn test_offset_execute_tree() {
    use crate::engine_public::PolyTree64;

    let mut co = ClipperOffset::new_default();
    let square = make_square(100);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);

    let mut tree = PolyTree64::new();
    co.execute_tree(10.0, &mut tree);

    // The tree should contain the result
    assert!(
        !tree.nodes.is_empty(),
        "PolyTree should have at least one node"
    );
}

// ---------------------------------------------------------------------------
// Preserve collinear test
// ---------------------------------------------------------------------------

#[test]
fn test_offset_preserve_collinear() {
    let mut co_preserve = ClipperOffset::new(2.0, 0.0, true, false);
    let mut co_no_preserve = ClipperOffset::new(2.0, 0.0, false, false);

    // Square with an extra collinear point on one edge
    let square_with_collinear = vec![
        Point64::new(0, 0),
        Point64::new(50, 0), // collinear with 0,0 -> 100,0
        Point64::new(100, 0),
        Point64::new(100, 100),
        Point64::new(0, 100),
    ];

    co_preserve.add_path(&square_with_collinear, JoinType::Miter, EndType::Polygon);
    co_no_preserve.add_path(&square_with_collinear, JoinType::Miter, EndType::Polygon);

    let mut result_preserve = Paths64::new();
    let mut result_no_preserve = Paths64::new();
    co_preserve.execute(10.0, &mut result_preserve);
    co_no_preserve.execute(10.0, &mut result_no_preserve);

    assert!(!result_preserve.is_empty());
    assert!(!result_no_preserve.is_empty());
}

// ---------------------------------------------------------------------------
// Mixed join/end type tests
// ---------------------------------------------------------------------------

#[test]
fn test_offset_mixed_groups() {
    let mut co = ClipperOffset::new_default();

    // Add a closed polygon
    let square = make_square(100);
    co.add_path(&square, JoinType::Miter, EndType::Polygon);

    // Add an open path
    let line = make_open_line(200);
    co.add_path(&line, JoinType::Round, EndType::Round);

    let mut result = Paths64::new();
    co.execute(10.0, &mut result);

    // Should produce non-empty result (the union step may merge paths)
    assert!(
        !result.is_empty(),
        "Mixed groups should produce output paths"
    );
}