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
/*******************************************************************************
* Comprehensive tests for the public API convenience functions                *
* Date: 2025                                                                  *
*******************************************************************************/

use crate::clipper::*;
use crate::core::{area, Path64, PathD, Paths64, Point, Point64, Rect64, RectD};
use crate::engine::ClipType;
use crate::engine_public::{PolyTree64, PolyTreeD};
use crate::offset::{EndType, JoinType};
use crate::FillRule;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn square_64(cx: i64, cy: i64, size: i64) -> Path64 {
    vec![
        Point64::new(cx - size, cy - size),
        Point64::new(cx + size, cy - size),
        Point64::new(cx + size, cy + size),
        Point64::new(cx - size, cy + size),
    ]
}

fn square_d(cx: f64, cy: f64, size: f64) -> PathD {
    vec![
        Point::<f64>::new(cx - size, cy - size),
        Point::<f64>::new(cx + size, cy - size),
        Point::<f64>::new(cx + size, cy + size),
        Point::<f64>::new(cx - size, cy + size),
    ]
}

// ============================================================================
// Boolean operation tests
// ============================================================================

#[test]
fn test_boolean_op_64_intersection() {
    let subjects = vec![square_64(0, 0, 100)];
    let clips = vec![square_64(50, 50, 100)];
    let result = boolean_op_64(ClipType::Intersection, FillRule::NonZero, &subjects, &clips);
    assert!(!result.is_empty(), "Intersection should produce output");
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    assert!(result_area > 0.0);
    // Overlap region is a 150x150 square = area 22500
    assert!(
        (result_area - 22500.0).abs() < 500.0,
        "Expected ~22500, got {}",
        result_area
    );
}

#[test]
fn test_boolean_op_64_union() {
    let subjects = vec![square_64(0, 0, 50)];
    let clips = vec![square_64(50, 0, 50)];
    let result = boolean_op_64(ClipType::Union, FillRule::NonZero, &subjects, &clips);
    assert!(!result.is_empty());
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    // Two adjacent 100x100 squares share one edge, so union = 200x100 = 20000
    // Minus the overlap (50x100 overlap area)
    assert!(result_area > 5000.0);
}

#[test]
fn test_boolean_op_tree_64() {
    let subjects = vec![square_64(0, 0, 100)];
    let clips = vec![square_64(0, 0, 50)]; // smaller square inside
    let mut tree = PolyTree64::new();
    boolean_op_tree_64(
        ClipType::Difference,
        FillRule::NonZero,
        &subjects,
        &clips,
        &mut tree,
    );
    // Should produce a ring shape (outer - inner)
    let root = tree.root();
    // Either root has children or the tree is populated
    assert!(root.count() > 0 || tree.nodes.len() > 1);
}

#[test]
fn test_boolean_op_d_intersection() {
    let subjects = vec![square_d(0.0, 0.0, 100.0)];
    let clips = vec![square_d(50.0, 50.0, 100.0)];
    let result = boolean_op_d(
        ClipType::Intersection,
        FillRule::NonZero,
        &subjects,
        &clips,
        2,
    );
    assert!(!result.is_empty());
}

// ============================================================================
// Convenience boolean function tests
// ============================================================================

#[test]
fn test_intersect_64() {
    let subjects = vec![square_64(0, 0, 100)];
    let clips = vec![square_64(50, 50, 100)];
    let result = intersect_64(&subjects, &clips, FillRule::NonZero);
    assert!(!result.is_empty());
}

#[test]
fn test_intersect_d() {
    let subjects = vec![square_d(0.0, 0.0, 100.0)];
    let clips = vec![square_d(50.0, 50.0, 100.0)];
    let result = intersect_d(&subjects, &clips, FillRule::NonZero, 2);
    assert!(!result.is_empty());
}

#[test]
fn test_union_64() {
    let subjects = vec![square_64(0, 0, 100)];
    let clips = vec![square_64(50, 0, 100)];
    let result = union_64(&subjects, &clips, FillRule::NonZero);
    assert!(!result.is_empty());
}

#[test]
fn test_union_d() {
    let subjects = vec![square_d(0.0, 0.0, 100.0)];
    let clips = vec![square_d(50.0, 0.0, 100.0)];
    let result = union_d(&subjects, &clips, FillRule::NonZero, 2);
    assert!(!result.is_empty());
}

#[test]
fn test_union_subjects_64() {
    let subjects = vec![square_64(0, 0, 100), square_64(50, 0, 100)];
    let result = union_subjects_64(&subjects, FillRule::NonZero);
    assert!(!result.is_empty());
}

#[test]
fn test_union_subjects_d() {
    let subjects = vec![square_d(0.0, 0.0, 100.0), square_d(50.0, 0.0, 100.0)];
    let result = union_subjects_d(&subjects, FillRule::NonZero, 2);
    assert!(!result.is_empty());
}

#[test]
fn test_difference_64() {
    let subjects = vec![square_64(0, 0, 100)];
    let clips = vec![square_64(50, 50, 100)];
    let result = difference_64(&subjects, &clips, FillRule::NonZero);
    assert!(!result.is_empty());
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    let original = area(&subjects[0]).abs();
    assert!(result_area < original, "Difference should reduce area");
}

#[test]
fn test_difference_d() {
    let subjects = vec![square_d(0.0, 0.0, 100.0)];
    let clips = vec![square_d(50.0, 50.0, 100.0)];
    let result = difference_d(&subjects, &clips, FillRule::NonZero, 2);
    assert!(!result.is_empty());
}

#[test]
fn test_xor_64() {
    let subjects = vec![square_64(0, 0, 100)];
    let clips = vec![square_64(50, 50, 100)];
    let result = xor_64(&subjects, &clips, FillRule::NonZero);
    assert!(!result.is_empty());
}

#[test]
fn test_xor_d() {
    let subjects = vec![square_d(0.0, 0.0, 100.0)];
    let clips = vec![square_d(50.0, 50.0, 100.0)];
    let result = xor_d(&subjects, &clips, FillRule::NonZero, 2);
    assert!(!result.is_empty());
}

// ============================================================================
// InflatePaths tests
// ============================================================================

#[test]
fn test_inflate_paths_64() {
    let paths = vec![square_64(0, 0, 100)];
    let result = inflate_paths_64(&paths, 10.0, JoinType::Miter, EndType::Polygon, 2.0, 0.0);
    assert!(!result.is_empty());
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    let original_area = area(&paths[0]).abs();
    assert!(result_area > original_area);
}

#[test]
fn test_inflate_paths_64_zero_delta() {
    let paths = vec![square_64(0, 0, 100)];
    let result = inflate_paths_64(&paths, 0.0, JoinType::Miter, EndType::Polygon, 2.0, 0.0);
    assert_eq!(result.len(), paths.len());
}

#[test]
fn test_inflate_paths_d() {
    let paths = vec![square_d(0.0, 0.0, 100.0)];
    let result = inflate_paths_d(&paths, 10.0, JoinType::Miter, EndType::Polygon, 2.0, 2, 0.0);
    assert!(!result.is_empty());
}

// ============================================================================
// TranslatePath / TranslatePaths tests
// ============================================================================

#[test]
fn test_translate_path_64() {
    let path = square_64(0, 0, 100);
    let translated = translate_path(&path, 50i64, 50i64);
    assert_eq!(translated.len(), path.len());
    for (orig, trans) in path.iter().zip(translated.iter()) {
        assert_eq!(trans.x, orig.x + 50);
        assert_eq!(trans.y, orig.y + 50);
    }
}

#[test]
fn test_translate_path_d() {
    let path = square_d(0.0, 0.0, 100.0);
    let translated = translate_path(&path, 50.0f64, 50.0f64);
    assert_eq!(translated.len(), path.len());
    for (orig, trans) in path.iter().zip(translated.iter()) {
        assert!((trans.x - (orig.x + 50.0)).abs() < 1e-10);
        assert!((trans.y - (orig.y + 50.0)).abs() < 1e-10);
    }
}

#[test]
fn test_translate_paths() {
    let paths = vec![square_64(0, 0, 100), square_64(200, 0, 50)];
    let translated = translate_paths(&paths, 10i64, 20i64);
    assert_eq!(translated.len(), 2);
    assert_eq!(translated[0][0].x, paths[0][0].x + 10);
    assert_eq!(translated[0][0].y, paths[0][0].y + 20);
}

// ============================================================================
// RectClip tests
// ============================================================================

#[test]
fn test_rect_clip_64() {
    let rect = Rect64::new(-50, -50, 50, 50);
    let paths = vec![square_64(0, 0, 100)];
    let result = rect_clip_64(&rect, &paths);
    assert!(!result.is_empty());
    let result_area: f64 = result.iter().map(|p| area(p).abs()).sum();
    // Clipped to 100x100 rect, area should be ~10000
    assert!(
        (result_area - 10000.0).abs() < 500.0,
        "Expected ~10000, got {}",
        result_area
    );
}

#[test]
fn test_rect_clip_path_64() {
    let rect = Rect64::new(-50, -50, 50, 50);
    let path = square_64(0, 0, 100);
    let result = rect_clip_path_64(&rect, &path);
    assert!(!result.is_empty());
}

#[test]
fn test_rect_clip_empty() {
    let rect = Rect64::new(0, 0, 0, 0); // empty rect
    let paths = vec![square_64(0, 0, 100)];
    let result = rect_clip_64(&rect, &paths);
    assert!(result.is_empty());
}

#[test]
fn test_rect_clip_lines_64() {
    let rect = Rect64::new(-50, -50, 50, 50);
    let lines = vec![vec![Point64::new(-200, 0), Point64::new(200, 0)]];
    let result = rect_clip_lines_64(&rect, &lines);
    assert!(!result.is_empty());
}

#[test]
fn test_rect_clip_line_64() {
    let rect = Rect64::new(-50, -50, 50, 50);
    let line = vec![Point64::new(-200, 0), Point64::new(200, 0)];
    let result = rect_clip_line_64(&rect, &line);
    assert!(!result.is_empty());
}

// ============================================================================
// MakePath tests
// ============================================================================

#[test]
fn test_make_path64() {
    let path = make_path64(&[0, 0, 100, 0, 100, 100, 0, 100]);
    assert_eq!(path.len(), 4);
    assert_eq!(path[0], Point64::new(0, 0));
    assert_eq!(path[1], Point64::new(100, 0));
    assert_eq!(path[2], Point64::new(100, 100));
    assert_eq!(path[3], Point64::new(0, 100));
}

#[test]
fn test_make_path64_odd_count() {
    // Odd count should truncate the last coordinate
    let path = make_path64(&[0, 0, 100, 0, 100]);
    assert_eq!(path.len(), 2);
}

#[test]
fn test_make_path_d() {
    let path = make_path_d(&[0.0, 0.0, 100.5, 0.0, 100.5, 100.5, 0.0, 100.5]);
    assert_eq!(path.len(), 4);
    assert!((path[0].x - 0.0).abs() < 1e-10);
    assert!((path[2].x - 100.5).abs() < 1e-10);
}

// ============================================================================
// TrimCollinear tests
// ============================================================================

#[test]
fn test_trim_collinear_closed() {
    // Square with collinear point on bottom edge
    let path = vec![
        Point64::new(0, 0),
        Point64::new(50, 0), // collinear
        Point64::new(100, 0),
        Point64::new(100, 100),
        Point64::new(0, 100),
    ];
    let result = trim_collinear_64(&path, false);
    assert_eq!(result.len(), 4, "Collinear point should be removed");
}

#[test]
fn test_trim_collinear_open() {
    let path = vec![
        Point64::new(0, 0),
        Point64::new(50, 0), // collinear
        Point64::new(100, 0),
    ];
    let result = trim_collinear_64(&path, true);
    assert_eq!(result.len(), 2, "Open collinear should reduce to endpoints");
}

#[test]
fn test_trim_collinear_too_short() {
    let path = vec![Point64::new(0, 0), Point64::new(100, 0)];
    let result = trim_collinear_64(&path, false);
    assert!(result.is_empty(), "Closed path with < 3 points is empty");

    let result_open = trim_collinear_64(&path, true);
    assert_eq!(result_open.len(), 2, "Open 2-point path stays");
}

#[test]
fn test_trim_collinear_d() {
    let path = vec![
        Point::<f64>::new(0.0, 0.0),
        Point::<f64>::new(50.0, 0.0),
        Point::<f64>::new(100.0, 0.0),
        Point::<f64>::new(100.0, 100.0),
        Point::<f64>::new(0.0, 100.0),
    ];
    let result = trim_collinear_d(&path, 2, false);
    assert_eq!(result.len(), 4);
}

// ============================================================================
// Distance / Length tests
// ============================================================================

#[test]
fn test_distance_basic() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(3, 4);
    assert!((distance(p1, p2) - 5.0).abs() < 1e-10);
}

#[test]
fn test_distance_same_point() {
    let p = Point64::new(42, 42);
    assert!((distance(p, p) - 0.0).abs() < 1e-10);
}

#[test]
fn test_path_length_open() {
    let path = vec![
        Point64::new(0, 0),
        Point64::new(100, 0),
        Point64::new(100, 100),
    ];
    let len = path_length(&path, false);
    assert!((len - 200.0).abs() < 1e-10);
}

#[test]
fn test_path_length_closed() {
    let path = vec![
        Point64::new(0, 0),
        Point64::new(100, 0),
        Point64::new(100, 100),
        Point64::new(0, 100),
    ];
    let len = path_length(&path, true);
    assert!((len - 400.0).abs() < 1e-10);
}

#[test]
fn test_path_length_too_short() {
    let path: Path64 = vec![Point64::new(0, 0)];
    assert_eq!(path_length(&path, false), 0.0);
}

// ============================================================================
// NearCollinear tests
// ============================================================================

#[test]
fn test_near_collinear_true() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(50, 1); // nearly collinear
    let p3 = Point64::new(100, 0);
    assert!(near_collinear(p1, p2, p3, 0.01));
}

#[test]
fn test_near_collinear_false() {
    let p1 = Point64::new(0, 0);
    let p2 = Point64::new(50, 50); // definitely not collinear
    let p3 = Point64::new(100, 0);
    assert!(!near_collinear(p1, p2, p3, 0.01));
}

// ============================================================================
// SimplifyPath tests
// ============================================================================

#[test]
fn test_simplify_path_basic() {
    let path = vec![
        Point64::new(0, 0),
        Point64::new(10, 1), // nearly collinear
        Point64::new(20, 0),
        Point64::new(30, 1), // nearly collinear
        Point64::new(40, 0),
    ];
    let result = simplify_path(&path, 5.0, false);
    assert!(result.len() <= path.len());
}

#[test]
fn test_simplify_path_short() {
    let path = vec![
        Point64::new(0, 0),
        Point64::new(50, 0),
        Point64::new(100, 0),
    ];
    // Fewer than 4 points => returned as-is
    let result = simplify_path(&path, 5.0, false);
    assert_eq!(result.len(), 3);
}

#[test]
fn test_simplify_paths() {
    let paths = vec![
        vec![
            Point64::new(0, 0),
            Point64::new(10, 1),
            Point64::new(20, 0),
            Point64::new(20, 20),
            Point64::new(0, 20),
        ],
        vec![
            Point64::new(100, 100),
            Point64::new(110, 101),
            Point64::new(120, 100),
            Point64::new(120, 120),
            Point64::new(100, 120),
        ],
    ];
    let result = simplify_paths(&paths, 5.0, true);
    assert_eq!(result.len(), 2);
}

// ============================================================================
// PolyTree conversion tests
// ============================================================================

#[test]
fn test_poly_tree_to_paths64_empty() {
    let tree = PolyTree64::new();
    let result = poly_tree_to_paths64(&tree);
    assert!(result.is_empty());
}

#[test]
fn test_poly_tree_to_paths64() {
    let mut tree = PolyTree64::new();
    let outer = square_64(0, 0, 100);
    tree.add_child(0, outer.clone());
    let result = poly_tree_to_paths64(&tree);
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].len(), outer.len());
}

#[test]
fn test_poly_tree_to_paths_d_empty() {
    let tree = PolyTreeD::new();
    let result = poly_tree_to_paths_d(&tree);
    assert!(result.is_empty());
}

// Note: path2_contains_path1 is tested in engine_fns tests

// ============================================================================
// Ramer-Douglas-Peucker tests
// ============================================================================

#[test]
fn test_ramer_douglas_peucker_short_path() {
    let path = vec![
        Point64::new(0, 0),
        Point64::new(50, 0),
        Point64::new(100, 0),
        Point64::new(100, 100),
    ];
    // Fewer than 5 points => returned as-is
    let result = ramer_douglas_peucker(&path, 5.0);
    assert_eq!(result.len(), 4);
}

#[test]
fn test_ramer_douglas_peucker_simplifies() {
    let path = vec![
        Point64::new(0, 0),
        Point64::new(25, 1), // nearly collinear
        Point64::new(50, 0),
        Point64::new(75, 1), // nearly collinear
        Point64::new(100, 0),
    ];
    let result = ramer_douglas_peucker(&path, 5.0);
    assert!(
        result.len() < path.len(),
        "RDP should simplify, got {} points",
        result.len()
    );
    // Start and end should be preserved
    assert_eq!(result[0], path[0]);
    assert_eq!(result[result.len() - 1], path[path.len() - 1]);
}

#[test]
fn test_ramer_douglas_peucker_paths() {
    let paths = vec![
        vec![
            Point64::new(0, 0),
            Point64::new(25, 1),
            Point64::new(50, 0),
            Point64::new(75, 1),
            Point64::new(100, 0),
        ],
        vec![
            Point64::new(0, 0),
            Point64::new(0, 50),
            Point64::new(0, 100),
            Point64::new(50, 100),
            Point64::new(100, 100),
        ],
    ];
    let result = ramer_douglas_peucker_paths(&paths, 5.0);
    assert_eq!(result.len(), 2);
}

// ============================================================================
// Edge case tests
// ============================================================================

#[test]
fn test_boolean_op_empty_subjects() {
    let subjects = Paths64::new();
    let clips = vec![square_64(0, 0, 100)];
    let result = boolean_op_64(ClipType::Intersection, FillRule::NonZero, &subjects, &clips);
    assert!(result.is_empty());
}

#[test]
fn test_boolean_op_empty_clips() {
    let subjects = vec![square_64(0, 0, 100)];
    let clips = Paths64::new();
    let result = boolean_op_64(ClipType::Union, FillRule::NonZero, &subjects, &clips);
    // Union with no clips should return the subjects
    // (behavior depends on engine; no panic is the minimum requirement)
    let _ = result;
}

#[test]
fn test_rect_clip_d() {
    let rect = RectD::new(-50.0, -50.0, 50.0, 50.0);
    let paths = vec![square_d(0.0, 0.0, 100.0)];
    let result = rect_clip_d(&rect, &paths, 2);
    assert!(!result.is_empty());
}

#[test]
fn test_rect_clip_lines_d() {
    let rect = RectD::new(-50.0, -50.0, 50.0, 50.0);
    let lines = vec![vec![
        Point::<f64>::new(-200.0, 0.0),
        Point::<f64>::new(200.0, 0.0),
    ]];
    let result = rect_clip_lines_d(&rect, &lines, 2);
    assert!(!result.is_empty());
}

#[test]
fn test_check_polytree_fully_contains_children_empty() {
    let tree = PolyTree64::new();
    assert!(check_polytree_fully_contains_children(&tree));
}