ezu_features/ops/
convert.rs1use crate::Polygon;
11
12pub type Path = Vec<[f64; 2]>;
14pub type FloatShape = Vec<Path>;
18
19#[inline]
20pub fn pt_to_f(p: (i32, i32)) -> [f64; 2] {
21 [p.0 as f64, p.1 as f64]
22}
23
24#[inline]
25pub fn pt_to_i(p: [f64; 2]) -> (i32, i32) {
26 (p[0].round() as i32, p[1].round() as i32)
27}
28
29pub fn line_to_f(line: &[(i32, i32)]) -> Path {
30 line.iter().copied().map(pt_to_f).collect()
31}
32
33pub fn line_to_i(path: &[[f64; 2]]) -> Vec<(i32, i32)> {
34 path.iter().copied().map(pt_to_i).collect()
35}
36
37pub fn polygon_to_f(p: &Polygon) -> FloatShape {
41 let mut shape = Vec::with_capacity(1 + p.holes.len());
42 shape.push(line_to_f(&p.exterior));
43 for h in &p.holes {
44 shape.push(line_to_f(h));
45 }
46 shape
47}
48
49pub fn polygon_from_f(shape: &[Path]) -> Option<Polygon> {
52 let (exterior, holes) = shape.split_first()?;
53 Some(Polygon {
54 exterior: line_to_i(exterior),
55 holes: holes.iter().map(|h| line_to_i(h)).collect(),
56 })
57}
58
59pub fn polygons_from_shapes(shapes: &[FloatShape]) -> Vec<Polygon> {
62 shapes.iter().filter_map(|s| polygon_from_f(s)).collect()
63}