mod clipper;
pub use clipper::*;
mod path;
pub use path::*;
mod paths;
pub use paths::*;
use crate::cxx_bridge::clipper2_sys_cxx;
use crate::cxx_bridge::clipper2_sys_cxx::ClipperFillRule;
use crate::paths_blob::{path64_to_blob, paths64_to_blob};
use crate::{EndType, JoinType, LazyPathsD, PointInPolygonResult};
pub type Point64 = crate::cxx_bridge::clipper2_sys_cxx::P64;
impl Point64 {
pub fn new(x: i64, y: i64) -> Self {
Self { x, y }
}
}
impl Path64 {
pub fn simplify(&self, epsilon: f64, is_open_path: bool) -> LazyPaths64 {
let out = clipper2_sys_cxx::cxx_path64_simplify(
&path64_to_blob(self),
epsilon,
is_open_path,
);
LazyPaths64::from_blob(out)
}
pub fn point_in_polygon(&self, point: Point64) -> PointInPolygonResult {
clipper2_sys_cxx::cxx_point_in_path64(&path64_to_blob(self), point.x, point.y).into()
}
pub fn to_pathd(&self) -> LazyPathsD {
let b = clipper2_sys_cxx::cxx_path64_to_pathd(&path64_to_blob(self));
LazyPathsD::from_blob(b)
}
}
impl Paths64 {
pub fn simplify(&self, epsilon: f64, is_open_path: bool) -> LazyPaths64 {
let out = clipper2_sys_cxx::cxx_paths64_simplify(
&paths64_to_blob(self),
epsilon,
is_open_path,
);
LazyPaths64::from_blob(out)
}
pub fn inflate(
&self,
delta: f64,
join_type: JoinType,
end_type: EndType,
miter_limit: f64,
) -> LazyPaths64 {
let out = clipper2_sys_cxx::cxx_paths64_inflate(
&paths64_to_blob(self),
delta,
join_type.into(),
end_type.into(),
miter_limit,
);
LazyPaths64::from_blob(out)
}
pub fn to_pathsd(&self) -> LazyPathsD {
let b = clipper2_sys_cxx::cxx_paths64_to_pathsd(&paths64_to_blob(self));
LazyPathsD::from_blob(b)
}
}
impl Path64 {
pub fn minkowski_sum(&self, pattern: &Path64, is_closed: bool) -> LazyPaths64 {
LazyPaths64::from_blob(clipper2_sys_cxx::cxx_path64_minkowski_sum(
&path64_to_blob(pattern),
&path64_to_blob(self),
is_closed,
))
}
pub fn minkowski_diff(&self, pattern: &Path64, is_closed: bool) -> LazyPaths64 {
LazyPaths64::from_blob(clipper2_sys_cxx::cxx_path64_minkowski_diff(
&path64_to_blob(pattern),
&path64_to_blob(self),
is_closed,
))
}
}
impl Paths64 {
pub fn minkowski_sum(
&self,
pattern: &Path64,
is_closed: bool,
fillrule: ClipperFillRule,
) -> LazyPaths64 {
LazyPaths64::from_blob(clipper2_sys_cxx::cxx_paths64_minkowski_sum(
&path64_to_blob(pattern),
&paths64_to_blob(self),
is_closed,
fillrule,
))
}
pub fn minkowski_diff(
&self,
pattern: &Path64,
is_closed: bool,
fillrule: ClipperFillRule,
) -> LazyPaths64 {
LazyPaths64::from_blob(clipper2_sys_cxx::cxx_paths64_minkowski_diff(
&path64_to_blob(pattern),
&paths64_to_blob(self),
is_closed,
fillrule,
))
}
}
impl Path64 {
pub fn area(&self) -> f64 {
clipper2_sys_cxx::cxx_path64_area(&path64_to_blob(self))
}
}
impl Paths64 {
pub fn area(&self) -> f64 {
clipper2_sys_cxx::cxx_paths64_area(&paths64_to_blob(self))
}
}
#[cfg(test)]
mod tests {
use super::{Path64, Point64, PointInPolygonResult};
#[test]
fn path64_area_unit_square() {
let p = Path64::new(vec![
Point64::new(0, 0),
Point64::new(10, 0),
Point64::new(10, 10),
Point64::new(0, 10),
]);
assert!((p.area().abs() - 100.0).abs() < 1e-6);
}
#[test]
fn point_in_polygon_center_inside_square() {
let p = Path64::new(vec![
Point64::new(0, 0),
Point64::new(10, 0),
Point64::new(10, 10),
Point64::new(0, 10),
]);
let r = p.point_in_polygon(Point64::new(5, 5));
assert!(matches!(r, PointInPolygonResult::Inside));
}
}