use crate::Polygon;
pub type Path = Vec<[f64; 2]>;
pub type FloatShape = Vec<Path>;
#[inline]
pub fn pt_to_f(p: (i32, i32)) -> [f64; 2] {
[p.0 as f64, p.1 as f64]
}
#[inline]
pub fn pt_to_i(p: [f64; 2]) -> (i32, i32) {
(p[0].round() as i32, p[1].round() as i32)
}
pub fn line_to_f(line: &[(i32, i32)]) -> Path {
line.iter().copied().map(pt_to_f).collect()
}
pub fn line_to_i(path: &[[f64; 2]]) -> Vec<(i32, i32)> {
path.iter().copied().map(pt_to_i).collect()
}
pub fn polygon_to_f(p: &Polygon) -> FloatShape {
let mut shape = Vec::with_capacity(1 + p.holes.len());
shape.push(line_to_f(&p.exterior));
for h in &p.holes {
shape.push(line_to_f(h));
}
shape
}
pub fn polygon_from_f(shape: &[Path]) -> Option<Polygon> {
let (exterior, holes) = shape.split_first()?;
Some(Polygon {
exterior: line_to_i(exterior),
holes: holes.iter().map(|h| line_to_i(h)).collect(),
})
}
pub fn polygons_from_shapes(shapes: &[FloatShape]) -> Vec<Polygon> {
shapes.iter().filter_map(|s| polygon_from_f(s)).collect()
}