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
use super::*;

// ============================================================================
// RectClip64 tests
// ============================================================================

#[test]
fn test_rectclip64_new() {
    let rect = Rect64::new(10, 10, 100, 100);
    let rc = RectClip64::new(rect);
    assert_eq!(rc.rect, rect);
    assert_eq!(rc.rect_as_path.len(), 4);
    assert_eq!(rc.rect_mp, Point64::new(55, 55));
}

#[test]
fn test_rectclip64_empty_rect() {
    let rect = Rect64::new(0, 0, 0, 0);
    let mut rc = RectClip64::new(rect);
    let paths = vec![vec![
        Point64::new(10, 10),
        Point64::new(20, 10),
        Point64::new(20, 20),
    ]];
    let result = rc.execute(&paths);
    assert!(result.is_empty());
}

#[test]
fn test_rectclip64_path_fully_inside() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rc = RectClip64::new(rect);
    let paths = vec![vec![
        Point64::new(10, 10),
        Point64::new(20, 10),
        Point64::new(20, 20),
        Point64::new(10, 20),
    ]];
    let result = rc.execute(&paths);
    assert_eq!(result.len(), 1);
    assert_eq!(result[0], paths[0]); // path returned unchanged
}

#[test]
fn test_rectclip64_path_fully_outside() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rc = RectClip64::new(rect);
    let paths = vec![vec![
        Point64::new(200, 200),
        Point64::new(300, 200),
        Point64::new(300, 300),
        Point64::new(200, 300),
    ]];
    let result = rc.execute(&paths);
    assert!(result.is_empty());
}

#[test]
fn test_rectclip64_path_partially_inside() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rc = RectClip64::new(rect);
    // Triangle that extends beyond the right side
    let paths = vec![vec![
        Point64::new(50, 10),
        Point64::new(150, 50),
        Point64::new(50, 90),
    ]];
    let result = rc.execute(&paths);
    assert!(!result.is_empty());
    // All result points should be within or on the rect boundary
    for path in &result {
        for pt in path {
            assert!(
                pt.x >= 0 && pt.x <= 100 && pt.y >= 0 && pt.y <= 100,
                "Point {:?} is outside rect",
                pt
            );
        }
    }
}

#[test]
fn test_rectclip64_path_containing_rect() {
    let rect = Rect64::new(20, 20, 80, 80);
    let mut rc = RectClip64::new(rect);
    // Large square that fully contains the rect
    let paths = vec![vec![
        Point64::new(0, 0),
        Point64::new(100, 0),
        Point64::new(100, 100),
        Point64::new(0, 100),
    ]];
    let result = rc.execute(&paths);
    assert!(!result.is_empty());
    // The result should be approximately the rect itself
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].len(), 4);
}

#[test]
fn test_rectclip64_skip_small_paths() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rc = RectClip64::new(rect);
    // Path with less than 3 points should be skipped
    let paths = vec![vec![Point64::new(10, 10), Point64::new(20, 20)]];
    let result = rc.execute(&paths);
    assert!(result.is_empty());
}

#[test]
fn test_rectclip64_multiple_paths() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rc = RectClip64::new(rect);
    let paths = vec![
        // Fully inside
        vec![
            Point64::new(10, 10),
            Point64::new(20, 10),
            Point64::new(20, 20),
            Point64::new(10, 20),
        ],
        // Fully outside
        vec![
            Point64::new(200, 200),
            Point64::new(300, 200),
            Point64::new(300, 300),
        ],
        // Partially inside
        vec![
            Point64::new(50, 50),
            Point64::new(150, 50),
            Point64::new(150, 150),
            Point64::new(50, 150),
        ],
    ];
    let result = rc.execute(&paths);
    assert!(result.len() >= 2); // at least the fully-inside and partially-inside paths
}

// ============================================================================
// RectClipLines64 tests
// ============================================================================

#[test]
fn test_rectcliplines64_new() {
    let rect = Rect64::new(10, 10, 100, 100);
    let rcl = RectClipLines64::new(rect);
    assert_eq!(rcl.rect, rect);
}

#[test]
fn test_rectcliplines64_empty_rect() {
    let rect = Rect64::new(0, 0, 0, 0);
    let mut rcl = RectClipLines64::new(rect);
    let paths = vec![vec![Point64::new(10, 10), Point64::new(20, 20)]];
    let result = rcl.execute(&paths);
    assert!(result.is_empty());
}

#[test]
fn test_rectcliplines64_line_fully_inside() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rcl = RectClipLines64::new(rect);
    let paths = vec![vec![
        Point64::new(10, 10),
        Point64::new(20, 20),
        Point64::new(30, 10),
    ]];
    let result = rcl.execute(&paths);
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].len(), 3);
}

#[test]
fn test_rectcliplines64_line_fully_outside() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rcl = RectClipLines64::new(rect);
    let paths = vec![vec![Point64::new(200, 200), Point64::new(300, 300)]];
    let result = rcl.execute(&paths);
    assert!(result.is_empty());
}

#[test]
fn test_rectcliplines64_line_crossing() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rcl = RectClipLines64::new(rect);
    // Line that enters and exits the rect
    let paths = vec![vec![Point64::new(-50, 50), Point64::new(150, 50)]];
    let result = rcl.execute(&paths);
    assert!(!result.is_empty());
    // The clipped line should start at x=0 and end at x=100
    for path in &result {
        for pt in path {
            assert!(
                pt.x >= 0 && pt.x <= 100 && pt.y >= 0 && pt.y <= 100,
                "Point {:?} is outside rect",
                pt
            );
        }
    }
}

#[test]
fn test_rectcliplines64_line_exiting() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rcl = RectClipLines64::new(rect);
    // Line starting inside, exiting
    let paths = vec![vec![Point64::new(50, 50), Point64::new(150, 50)]];
    let result = rcl.execute(&paths);
    assert!(!result.is_empty());
}

#[test]
fn test_rectcliplines64_line_entering() {
    let rect = Rect64::new(0, 0, 100, 100);
    let mut rcl = RectClipLines64::new(rect);
    // Line starting outside, entering
    let paths = vec![vec![Point64::new(-50, 50), Point64::new(50, 50)]];
    let result = rcl.execute(&paths);
    assert!(!result.is_empty());
}

// ============================================================================
// Helper function tests
// ============================================================================

#[test]
fn test_heading_clockwise() {
    assert!(heading_clockwise(Location::Left, Location::Top));
    assert!(heading_clockwise(Location::Top, Location::Right));
    assert!(heading_clockwise(Location::Right, Location::Bottom));
    assert!(heading_clockwise(Location::Bottom, Location::Left));
    assert!(!heading_clockwise(Location::Left, Location::Bottom));
    assert!(!heading_clockwise(Location::Top, Location::Left));
}

#[test]
fn test_are_opposites() {
    assert!(are_opposites(Location::Left, Location::Right));
    assert!(are_opposites(Location::Right, Location::Left));
    assert!(are_opposites(Location::Top, Location::Bottom));
    assert!(are_opposites(Location::Bottom, Location::Top));
    assert!(!are_opposites(Location::Left, Location::Top));
    assert!(!are_opposites(Location::Left, Location::Bottom));
}

#[test]
fn test_get_adjacent_location() {
    assert_eq!(get_adjacent_location(Location::Left, true), Location::Top);
    assert_eq!(get_adjacent_location(Location::Top, true), Location::Right);
    assert_eq!(
        get_adjacent_location(Location::Right, true),
        Location::Bottom
    );
    assert_eq!(
        get_adjacent_location(Location::Bottom, true),
        Location::Left
    );

    assert_eq!(
        get_adjacent_location(Location::Left, false),
        Location::Bottom
    );
    assert_eq!(get_adjacent_location(Location::Top, false), Location::Left);
    assert_eq!(get_adjacent_location(Location::Right, false), Location::Top);
    assert_eq!(
        get_adjacent_location(Location::Bottom, false),
        Location::Right
    );
}

#[test]
fn test_get_edges_for_pt() {
    let rect = Rect64::new(10, 10, 100, 100);
    assert_eq!(get_edges_for_pt(&Point64::new(10, 50), &rect), 1); // left
    assert_eq!(get_edges_for_pt(&Point64::new(100, 50), &rect), 4); // right
    assert_eq!(get_edges_for_pt(&Point64::new(50, 10), &rect), 2); // top
    assert_eq!(get_edges_for_pt(&Point64::new(50, 100), &rect), 8); // bottom
    assert_eq!(get_edges_for_pt(&Point64::new(10, 10), &rect), 3); // left+top
    assert_eq!(get_edges_for_pt(&Point64::new(100, 100), &rect), 12); // right+bottom
    assert_eq!(get_edges_for_pt(&Point64::new(50, 50), &rect), 0); // inside
}

#[test]
fn test_start_locs_are_clockwise() {
    // Clockwise: Left -> Top -> Right -> Bottom
    let locs = vec![
        Location::Left,
        Location::Top,
        Location::Right,
        Location::Bottom,
    ];
    assert!(start_locs_are_clockwise(&locs));

    // Counter-clockwise: Left -> Bottom -> Right -> Top
    let locs = vec![
        Location::Left,
        Location::Bottom,
        Location::Right,
        Location::Top,
    ];
    assert!(!start_locs_are_clockwise(&locs));
}

#[test]
fn test_path1_contains_path2() {
    let outer = vec![
        Point64::new(0, 0),
        Point64::new(100, 0),
        Point64::new(100, 100),
        Point64::new(0, 100),
    ];
    let inner = vec![
        Point64::new(20, 20),
        Point64::new(80, 20),
        Point64::new(80, 80),
        Point64::new(20, 80),
    ];
    assert!(path1_contains_path2(&outer, &inner));
    assert!(!path1_contains_path2(&inner, &outer));
}

#[test]
fn test_get_segment_intersection_crossing() {
    let mut ip = Point64::new(0, 0);
    // Two crossing segments
    let result = get_segment_intersection(
        Point64::new(0, 0),
        Point64::new(10, 10),
        Point64::new(0, 10),
        Point64::new(10, 0),
        &mut ip,
    );
    assert!(result);
    assert_eq!(ip, Point64::new(5, 5));
}

#[test]
fn test_get_segment_intersection_collinear() {
    let mut ip = Point64::new(0, 0);
    // Collinear segments
    let result = get_segment_intersection(
        Point64::new(0, 0),
        Point64::new(10, 0),
        Point64::new(5, 0),
        Point64::new(15, 0),
        &mut ip,
    );
    // Collinear with res1==0 and res2==0 should return false
    assert!(!result);
}

#[test]
fn test_get_segment_intersection_parallel() {
    let mut ip = Point64::new(0, 0);
    // Parallel but not touching
    let result = get_segment_intersection(
        Point64::new(0, 0),
        Point64::new(10, 0),
        Point64::new(0, 5),
        Point64::new(10, 5),
        &mut ip,
    );
    assert!(!result);
}

#[test]
fn test_rectclip64_triangle_touching_corner_should_be_empty() {
    // Triangle only touches the rect at corner point (410,310) which is bottom-right
    // rect = (390, 290, 410, 310)
    // triangle = (430,290), (470,330), (390,330)
    // C++ returns empty (0 paths) because the triangle is outside the rect,
    // only touching at the corner point (410,310).
    let rect = Rect64::new(390, 290, 410, 310);
    let mut rc = RectClip64::new(rect);
    let triangle = vec![
        Point64::new(430, 290),
        Point64::new(470, 330),
        Point64::new(390, 330),
    ];
    let result = rc.execute(&vec![triangle]);
    assert!(
        result.is_empty(),
        "Expected empty result for triangle that only touches rect at a corner, got {} paths: {:?}",
        result.len(),
        result
    );
}

// Fix 5: get_segment_intersection must use i128 cross_product_sign, not f64 cross_product
#[test]
fn test_get_segment_intersection_large_coords_i128() {
    // Test with large coordinates where f64 loses precision.
    // p1=(0,0), p2=(1,1) segment, p3=(0,0), p4=(100000000,100000001) segment
    // cross_product_three_points(p2, p3, p4) with f64 gives 0.0 (wrong, should be -1)
    // cross_product_sign(p2, p3, p4) with i128 gives -1 (correct)
    //
    // We test this through the public RectClip64 API since get_segment_intersection is private.
    // We construct a scenario where a polygon edge has large coordinates and clips against a rect.
    // The rect is (0,0)-(100000000,100000001) and the polygon crosses the rect boundary.
    //
    // Simpler approach: test that rect clipping works correctly with large coordinate polygons.
    let rect = Rect64::new(0, 0, 50_000_000, 50_000_001);
    let mut rc = RectClip64::new(rect);
    // A large triangle that extends beyond the rect
    let polygon = vec![
        Point64::new(-1, -1),
        Point64::new(100_000_000, 100_000_001),
        Point64::new(100_000_000, -1),
    ];
    let result = rc.execute(&vec![polygon]);
    // The triangle should clip against the rect and produce a non-empty result
    assert!(
        !result.is_empty(),
        "Rect clipping with large coordinates should produce a non-empty result"
    );
    // Verify the result has reasonable area (not degenerate)
    for path in &result {
        assert!(
            path.len() >= 3,
            "Clipped polygon should have at least 3 vertices, got {}",
            path.len()
        );
    }
}