clipper2_sys/double/
poly_tree_d.rs1use crate::{
2 clipper_delete_pathd, clipper_pathd_size, clipper_polytreed_count, clipper_polytreed_get_child,
3 clipper_polytreed_inv_scale, clipper_polytreed_is_hole, clipper_polytreed_polygon, malloc,
4 ClipperPolyTreeD, PathD, PathsD,
5};
6
7#[derive(Debug)]
8pub struct PolyTreeD {
9 pub(crate) childs: Vec<Self>,
10 pub(crate) is_hole: bool,
11 pub(crate) polygon: PathD,
12 pub(crate) scale: f64,
13}
14
15impl PolyTreeD {
16 pub(crate) fn from(ptr: *mut ClipperPolyTreeD) -> Self {
17 let is_hole: bool;
18 let scale: f64;
19 let polygon: PathD;
20 let childs: Vec<Self>;
21 unsafe {
22 is_hole = clipper_polytreed_is_hole(ptr) == 1;
23 scale = clipper_polytreed_inv_scale(ptr);
24 let mem = malloc(clipper_pathd_size());
25 let polygon_prt = clipper_polytreed_polygon(mem, ptr);
26 polygon = PathD::from(polygon_prt);
27 clipper_delete_pathd(polygon_prt);
28 let count = clipper_polytreed_count(ptr);
29 childs = (0..count)
30 .map(|i| {
31 let tree_ptr = clipper_polytreed_get_child(ptr, i);
32 Self::from(tree_ptr as *mut ClipperPolyTreeD)
33 })
34 .collect();
35 }
36 Self {
37 childs: childs,
38 is_hole: is_hole,
39 polygon: polygon,
40 scale: scale,
41 }
42 }
43}
44
45impl PolyTreeD {
46
47 pub fn get_childs(&mut self) -> &mut Vec<Self> {
48 &mut self.childs
49 }
50
51 pub fn is_hole(&self) -> bool {
52 self.is_hole
53 }
54
55 pub fn get_hole_paths(&self) -> PathsD {
56 let mut paths = PathsD::new(&vec![]);
57 for child in &self.childs {
58 if child.is_hole {
59 paths.add_path(child.get_polygon());
60 }
61 }
62 paths
63 }
64
65 pub fn get_polygon(&self) -> PathD {
66 self.polygon.clone()
67 }
68
69 pub fn scale(&self) -> f64 {
70 self.scale
71 }
72}