clipper2-sys 1.0.0

Polygon Clipping and Offsetting (Clipper2 wrapper)
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
//! Shared `macro_rules!` for path geometry, blob iteration, lazy path holders, and poly-tree preorder.
//!
//! 整型 / 双精度路径几何、`PathsBlob*` 迭代、惰性路径包装、多边形树前序等的内部宏(`#[macro_use]` 加载)。

/// Lazy iterator: one [`Path*`] per segment `[path_starts[i], path_starts[i+1])`.
///
/// 惰性迭代:`path_starts` 与 `points` 布局与 C++ 一致,每步产出一条路径。
macro_rules! define_paths_blob_iter {
    (
        $(#[$iter_meta:meta])*
        $vis:vis $Iter:ident,
        point = $Pt:ty,
        path = $Path:ty,
        blob = $Blob:ty,
        conv_slice = $conv:path,
    ) => {
        $(#[$iter_meta])*
        #[derive(Clone, Copy)]
        $vis struct $Iter<'a> {
            points: &'a [$Pt],
            path_starts: &'a [usize],
            idx: usize,
        }

        impl<'a> $Iter<'a> {
            /// Wraps a borrowed blob. / 借用 blob 构造迭代器。
            pub(crate) fn new(blob: &'a $Blob) -> Self {
                Self {
                    points: &blob.points,
                    path_starts: &blob.path_starts,
                    idx: 0,
                }
            }
        }

        impl<'a> Iterator for $Iter<'a> {
            type Item = $Path;

            fn next(&mut self) -> Option<Self::Item> {
                let i = self.idx;
                if i + 1 >= self.path_starts.len() {
                    return None;
                }
                let a = self.path_starts[i];
                let e = self.path_starts[i + 1];
                self.idx += 1;
                Some($conv(&self.points[a..e]))
            }

            fn size_hint(&self) -> (usize, Option<usize>) {
                let total = self.path_starts.len().saturating_sub(1);
                let left = total.saturating_sub(self.idx);
                (left, Some(left))
            }
        }

        impl<'a> ExactSizeIterator for $Iter<'a> {}

        impl<'a> std::iter::FusedIterator for $Iter<'a> {}
    };
}

/// Expands a lazy holder around a `PathsBlob*` plus `IntoIterator`.
///
/// 在 `PathsBlob*` 外封一层惰性包装并实现 `IntoIterator`。
macro_rules! define_lazy_paths {
    (
        $(#[$lazy_meta:meta])*
        $Lazy:ident,
        blob = $Blob:ty,
        paths = $Paths:ty,
        path = $Path:ty,
        iter = $Iter:ident,
        from_blob = $from_blob:path,
    ) => {
        $(#[$lazy_meta])*
        #[derive(Clone, Debug, Default)]
        pub struct $Lazy {
            blob: $Blob,
        }

        impl $Lazy {
            /// Wraps an owned blob (crate-internal). / 拥有 blob(crate 内部构造)。
            pub(crate) fn from_blob(blob: $Blob) -> Self {
                Self { blob }
            }

            /// Borrowing iterator over paths. / 借用迭代每条路径。
            #[inline]
            pub fn iter(&self) -> $Iter<'_> {
                $Iter::new(&self.blob)
            }

            /// True if there is no path segment. / 无任何路径分段。
            #[inline]
            pub fn is_empty(&self) -> bool {
                self.blob.path_starts.len() < 2
            }

            /// Consumes and materializes all paths. / 消耗并物化全部路径。
            pub fn into_paths(self) -> $Paths {
                $from_blob(self.blob)
            }

            /// Clones-by-rebuilding all paths. / 通过迭代收集副本。
            pub fn to_paths(&self) -> $Paths {
                self.iter().collect()
            }

            /// First path or empty (common for single-path ops). / 取首条路径(单路径运算常用)。
            pub fn into_first_path(self) -> $Path {
                self.iter().next().unwrap_or_default()
            }
        }

        impl<'a> IntoIterator for &'a $Lazy {
            type Item = $Path;
            type IntoIter = $Iter<'a>;

            #[inline]
            fn into_iter(self) -> Self::IntoIter {
                self.iter()
            }
        }
    };
}

macro_rules! define_poly_cxx_preorder_64 {
    () => {
        /// One node while traversing a C++ `PolyPath64` in preorder.
        ///
        /// 前序遍历 C++ `PolyPath64` 时的一个结点。
        #[derive(Clone, Debug)]
        pub struct PolyCxxNode64 {
            /// Depth from tree root (0 = root). / 相对根的深度。
            pub depth: usize,
            /// Hole flag reported by Clipper. / Clipper 报告的洞标志。
            pub is_hole: bool,
            /// Single contour for this node. / 该结点对应单条轮廓。
            pub path: crate::Path64,
        }

        /// Owns a native root; yields [`PolyCxxNode64`] in preorder; frees via `cxx_poly64_delete` on drop.
        ///
        /// 拥有 C++ 根指针,前序产出结点;`Drop` 时 `cxx_poly64_delete`。
        pub struct PolyCxxPreorderIter64 {
            stack: Vec<(usize, usize)>,
            root_to_free: Option<usize>,
        }

        impl PolyCxxPreorderIter64 {
            /// `root` is opaque C++ pointer as `usize`; `0` means empty. / `root` 为 C++ 指针数值,`0` 表示空。
            pub(crate) fn new(root: usize) -> Self {
                if root == 0 {
                    return Self {
                        stack: Vec::new(),
                        root_to_free: None,
                    };
                }
                Self {
                    stack: vec![(root, 0)],
                    root_to_free: Some(root),
                }
            }
        }

        impl Iterator for PolyCxxPreorderIter64 {
            type Item = PolyCxxNode64;

            fn next(&mut self) -> Option<Self::Item> {
                use crate::paths_blob::path64_from_single_paths_blob;
                let (n, depth) = self.stack.pop()?;
                let is_hole = crate::cxx_bridge::clipper2_sys_cxx::cxx_poly64_is_hole(n);
                let blob = crate::cxx_bridge::clipper2_sys_cxx::cxx_poly64_polygon(n);
                let path = path64_from_single_paths_blob(blob);
                let nc = crate::cxx_bridge::clipper2_sys_cxx::cxx_poly64_child_count(n) as usize;
                for i in (0..nc).rev() {
                    let c = crate::cxx_bridge::clipper2_sys_cxx::cxx_poly64_child_at(n, i);
                    self.stack.push((c, depth + 1));
                }
                Some(PolyCxxNode64 {
                    depth,
                    is_hole,
                    path,
                })
            }
        }

        impl Drop for PolyCxxPreorderIter64 {
            fn drop(&mut self) {
                if let Some(r) = self.root_to_free.take() {
                    if r != 0 {
                        crate::cxx_bridge::clipper2_sys_cxx::cxx_poly64_delete(r);
                    }
                }
            }
        }
    };
}

macro_rules! define_poly_cxx_preorder_d {
    () => {
        /// Node during preorder on `PolyPathD`. / `PolyPathD` 前序结点。
        #[derive(Clone, Debug)]
        pub struct PolyCxxNodeD {
            /// Depth from root. / 深度。
            pub depth: usize,
            /// Hole flag. / 是否洞。
            pub is_hole: bool,
            /// Scale from `PolyPathD`. / `PolyPathD` 的 scale。
            pub scale: f64,
            /// Contour geometry. / 轮廓。
            pub path: crate::PathD,
        }

        /// Preorder iterator with `cxx_polyd_delete` on drop. / 前序迭代,`Drop` 时 `cxx_polyd_delete`。
        pub struct PolyCxxPreorderIterD {
            stack: Vec<(usize, usize)>,
            root_to_free: Option<usize>,
        }

        impl PolyCxxPreorderIterD {
            /// See [`PolyCxxPreorderIter64::new`]. / 同 64 版 `new` 语义。
            pub(crate) fn new(root: usize) -> Self {
                if root == 0 {
                    return Self {
                        stack: Vec::new(),
                        root_to_free: None,
                    };
                }
                Self {
                    stack: vec![(root, 0)],
                    root_to_free: Some(root),
                }
            }
        }

        impl Iterator for PolyCxxPreorderIterD {
            type Item = PolyCxxNodeD;

            fn next(&mut self) -> Option<Self::Item> {
                use crate::paths_blob::pathd_from_single_paths_blob;
                let (n, depth) = self.stack.pop()?;
                let is_hole = crate::cxx_bridge::clipper2_sys_cxx::cxx_polyd_is_hole(n);
                let scale = crate::cxx_bridge::clipper2_sys_cxx::cxx_polyd_scale(n);
                let blob = crate::cxx_bridge::clipper2_sys_cxx::cxx_polyd_polygon(n);
                let path = pathd_from_single_paths_blob(blob);
                let nc = crate::cxx_bridge::clipper2_sys_cxx::cxx_polyd_child_count(n) as usize;
                for i in (0..nc).rev() {
                    let c = crate::cxx_bridge::clipper2_sys_cxx::cxx_polyd_child_at(n, i);
                    self.stack.push((c, depth + 1));
                }
                Some(PolyCxxNodeD {
                    depth,
                    is_hole,
                    scale,
                    path,
                })
            }
        }

        impl Drop for PolyCxxPreorderIterD {
            fn drop(&mut self) {
                if let Some(r) = self.root_to_free.take() {
                    if r != 0 {
                        crate::cxx_bridge::clipper2_sys_cxx::cxx_polyd_delete(r);
                    }
                }
            }
        }
    };
}

/// Implements geometry helpers on `Path64` or `PathD` (`translate` / `scale` differ by coordinate type).
///
/// 为 `Path64` 或 `PathD` 生成几何方法(平移 / 缩放随坐标类型不同)。
macro_rules! impl_path_geom {
    (int64, $Path:ident, $Point:ident) => {
        impl $Path {
            /// From owned `Vec` of points. / 由点向量构造。
            #[inline]
            pub fn from_vec(points: Vec<$Point>) -> Self {
                Self(points)
            }

            /// From anything that becomes `Vec` (e.g. `vec![…]`). / 由 `Into<Vec>` 构造。
            #[inline]
            pub fn new(points: impl Into<Vec<$Point>>) -> Self {
                Self(points.into())
            }

            #[inline]
            pub fn as_slice(&self) -> &[$Point] {
                &self.0
            }

            #[inline]
            pub fn as_mut_slice(&mut self) -> &mut [$Point] {
                &mut self.0
            }

            #[inline]
            pub fn iter(&self) -> std::slice::Iter<'_, $Point> {
                self.0.iter()
            }

            #[inline]
            pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, $Point> {
                self.0.iter_mut()
            }

            pub fn is_empty(&self) -> bool {
                self.0.is_empty()
            }

            pub fn len(&self) -> usize {
                self.0.len()
            }

            /// Copy of vertex `index`. / 拷贝下标 `index` 的顶点。
            #[inline]
            pub fn get_point(&self, index: usize) -> $Point {
                self.0[index]
            }

            pub fn add_point(&mut self, point: $Point) {
                self.0.push(point)
            }

            /// Translate by integer deltas. / 整数平移。
            pub fn translate(&self, dx: i64, dy: i64) -> Self {
                let n = self.0.len();
                let mut new_points = Vec::with_capacity(n);
                new_points.extend(self.0.iter().map(|p| $Point {
                    x: p.x + dx,
                    y: p.y + dy,
                }));
                Self(new_points)
            }

            /// Scale coordinates (`sx`/`sy` — `0` treated as `1`). / 缩放(0 视为 1)。
            pub fn scale(&self, sx: f64, sy: f64) -> Self {
                let mut _sx = sx;
                if _sx == 0. {
                    _sx = 1.;
                }
                let mut _sy = sy;
                if _sy == 0. {
                    _sy = 1.;
                }
                let n = self.0.len();
                let mut new_points = Vec::with_capacity(n);
                new_points.extend(self.0.iter().map(|p| $Point {
                    x: ((p.x as f64) * _sx) as i64,
                    y: ((p.y as f64) * _sy) as i64,
                }));
                Self(new_points)
            }
        }

        impl<'a> IntoIterator for &'a $Path {
            type Item = &'a $Point;
            type IntoIter = std::slice::Iter<'a, $Point>;

            #[inline]
            fn into_iter(self) -> Self::IntoIter {
                self.iter()
            }
        }

        impl IntoIterator for $Path {
            type Item = $Point;
            type IntoIter = std::vec::IntoIter<$Point>;

            #[inline]
            fn into_iter(self) -> Self::IntoIter {
                self.0.into_iter()
            }
        }
    };
    (float, $Path:ident, $Point:ident) => {
        impl $Path {
            /// See int64 `Path64` methods (float variants). / 与 `Path64` 方法对偶(浮点版)。
            #[inline]
            pub fn from_vec(points: Vec<$Point>) -> Self {
                Self(points)
            }

            #[inline]
            pub fn new(points: impl Into<Vec<$Point>>) -> Self {
                Self(points.into())
            }

            #[inline]
            pub fn as_slice(&self) -> &[$Point] {
                &self.0
            }

            #[inline]
            pub fn as_mut_slice(&mut self) -> &mut [$Point] {
                &mut self.0
            }

            #[inline]
            pub fn iter(&self) -> std::slice::Iter<'_, $Point> {
                self.0.iter()
            }

            #[inline]
            pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, $Point> {
                self.0.iter_mut()
            }

            pub fn is_empty(&self) -> bool {
                self.0.is_empty()
            }

            pub fn len(&self) -> usize {
                self.0.len()
            }

            #[inline]
            pub fn get_point(&self, index: usize) -> $Point {
                self.0[index]
            }

            pub fn add_point(&mut self, point: $Point) {
                self.0.push(point)
            }

            pub fn translate(&self, dx: f64, dy: f64) -> Self {
                let n = self.0.len();
                let mut new_points = Vec::with_capacity(n);
                new_points.extend(self.0.iter().map(|p| $Point {
                    x: p.x + dx,
                    y: p.y + dy,
                }));
                Self(new_points)
            }

            pub fn scale(&self, sx: f64, sy: f64) -> Self {
                let mut _sx = sx;
                if _sx == 0. {
                    _sx = 1.;
                }
                let mut _sy = sy;
                if _sy == 0. {
                    _sy = 1.;
                }
                let n = self.0.len();
                let mut new_points = Vec::with_capacity(n);
                new_points.extend(self.0.iter().map(|p| $Point {
                    x: p.x * _sx,
                    y: p.y * _sy,
                }));
                Self(new_points)
            }
        }

        impl<'a> IntoIterator for &'a $Path {
            type Item = &'a $Point;
            type IntoIter = std::slice::Iter<'a, $Point>;

            #[inline]
            fn into_iter(self) -> Self::IntoIter {
                self.iter()
            }
        }

        impl IntoIterator for $Path {
            type Item = $Point;
            type IntoIter = std::vec::IntoIter<$Point>;

            #[inline]
            fn into_iter(self) -> Self::IntoIter {
                self.0.into_iter()
            }
        }
    };
}

/// Multi-path container: forwards `translate` / `scale` to each path.
///
/// 多路径容器:将 `translate` / `scale` 逐条委托给单路径。
macro_rules! impl_paths_collection {
    (int64, $Paths:ident, $Path:ty) => {
        impl $Paths {
            /// Internal: wrap `Vec`. / 内部:包装 `Vec`。
            #[inline]
            pub(crate) fn from_vec(paths: Vec<$Path>) -> Self {
                Self(paths)
            }

            /// New collection from any `Into<Vec>`. / 由 `Into<Vec>` 构造。
            #[inline]
            pub fn new(paths: impl Into<Vec<$Path>>) -> Self {
                Self(paths.into())
            }

            /// Slice of all paths. / 全部路径切片。
            #[inline]
            pub fn as_slice(&self) -> &[$Path] {
                &self.0
            }

            /// Borrow one path by index. / 按下标借用路径。
            #[inline]
            pub fn path(&self, index: usize) -> &$Path {
                &self.0[index]
            }

            /// True if there are zero paths. / 是否无路径。
            pub fn is_empty(&self) -> bool {
                self.0.is_empty()
            }

            /// Path count. / 路径条数。
            pub fn len(&self) -> usize {
                self.0.len()
            }

            /// Push one path. / 追加一条路径。
            pub fn add_path(&mut self, path: $Path) {
                self.0.push(path)
            }

            /// Extend with many paths. / 追加多条。
            pub fn add_paths(&mut self, paths: impl IntoIterator<Item = $Path>) {
                self.0.extend(paths)
            }

            /// Clone all paths into a vector. / 克隆为 `Vec`。
            pub fn to_vec(&self) -> Vec<$Path> {
                self.0.clone()
            }

            /// Per-path translate with `i64` deltas. / 逐路径平移(`i64`)。
            pub fn translate(&self, dx: i64, dy: i64) -> Self {
                let n = self.0.len();
                let mut new_paths = Vec::with_capacity(n);
                new_paths.extend(self.0.iter().map(|p| p.translate(dx, dy)));
                Self(new_paths)
            }

            /// Per-path scale (`f64` factors, 0 → 1). / 逐路径缩放。
            pub fn scale(&self, sx: f64, sy: f64) -> Self {
                let n = self.0.len();
                let mut new_paths = Vec::with_capacity(n);
                new_paths.extend(self.0.iter().map(|p| p.scale(sx, sy)));
                Self(new_paths)
            }
        }

        impl FromIterator<$Path> for $Paths {
            /// Collect iterator of paths. / 从迭代器收集。
            fn from_iter<T: IntoIterator<Item = $Path>>(iter: T) -> Self {
                Self(iter.into_iter().collect())
            }
        }
    };
    (float, $Paths:ident, $Path:ty) => {
        impl $Paths {
            #[inline]
            pub(crate) fn from_vec(paths: Vec<$Path>) -> Self {
                Self(paths)
            }

            #[inline]
            pub fn new(paths: impl Into<Vec<$Path>>) -> Self {
                Self(paths.into())
            }

            #[inline]
            pub fn as_slice(&self) -> &[$Path] {
                &self.0
            }

            #[inline]
            pub fn path(&self, index: usize) -> &$Path {
                &self.0[index]
            }

            /// See int64 arm. / 同整型版各方法语义。
            pub fn is_empty(&self) -> bool {
                self.0.is_empty()
            }

            pub fn len(&self) -> usize {
                self.0.len()
            }

            pub fn add_path(&mut self, path: $Path) {
                self.0.push(path)
            }

            pub fn add_paths(&mut self, paths: impl IntoIterator<Item = $Path>) {
                self.0.extend(paths)
            }

            pub fn to_vec(&self) -> Vec<$Path> {
                self.0.clone()
            }

            /// Per-path translate (`f64`). / 逐路径平移。
            pub fn translate(&self, dx: f64, dy: f64) -> Self {
                let n = self.0.len();
                let mut new_paths = Vec::with_capacity(n);
                new_paths.extend(self.0.iter().map(|p| p.translate(dx, dy)));
                Self(new_paths)
            }

            pub fn scale(&self, sx: f64, sy: f64) -> Self {
                let n = self.0.len();
                let mut new_paths = Vec::with_capacity(n);
                new_paths.extend(self.0.iter().map(|p| p.scale(sx, sy)));
                Self(new_paths)
            }
        }

        impl FromIterator<$Path> for $Paths {
            fn from_iter<T: IntoIterator<Item = $Path>>(iter: T) -> Self {
                Self(iter.into_iter().collect())
            }
        }
    };
}