use crate::geometry::{self as geom, Axis, BooleanOp, Join, Region, Ring};
use crate::{Error, Point, Transform};
use pyo3::prelude::*;
type XY = (i64, i64);
type PyRing = Vec<XY>;
type PyRegion = Vec<PyRing>;
fn point((x, y): XY) -> Point {
Point { x, y }
}
fn ring(points: PyRing) -> Ring {
points.into_iter().map(point).collect()
}
fn region(rings: PyRegion) -> Region {
rings.into_iter().map(ring).collect()
}
fn py_ring(ring: Ring) -> PyRing {
ring.into_iter().map(|p| (p.x, p.y)).collect()
}
fn py_region(region: Region) -> PyRegion {
region.into_iter().map(py_ring).collect()
}
#[pyfunction]
fn boolean(
py: Python<'_>,
subject: PyRegion,
clip: PyRegion,
operation: &str,
) -> PyResult<PyRegion> {
let op = operation.parse::<BooleanOp>()?;
Ok(py.detach(|| geom::boolean(®ion(subject), ®ion(clip), op).map(py_region))?)
}
#[pyfunction(signature=(polygons, distance, *, join="miter", miter_limit=2.0, tolerance=0.25))]
fn offset(
py: Python<'_>,
polygons: PyRegion,
distance: f64,
join: &str,
miter_limit: f64,
tolerance: f64,
) -> PyResult<PyRegion> {
let join = match join {
"miter" => Join::Miter,
"round" => Join::Round,
"bevel" => Join::Bevel,
"square" => Join::Square,
_ => return Err(Error::invalid("join must be miter, round, bevel or square").into()),
};
Ok(py.detach(|| {
geom::offset(®ion(polygons), distance, join, miter_limit, tolerance).map(py_region)
})?)
}
#[pyfunction(signature=(polygons, positions, axis="x"))]
fn slice(
py: Python<'_>,
polygons: PyRegion,
positions: Vec<i64>,
axis: &str,
) -> PyResult<Vec<PyRegion>> {
let axis = match axis {
"x" => Axis::X,
"y" => Axis::Y,
_ => return Err(Error::invalid("axis must be x or y").into()),
};
Ok(py.detach(|| {
geom::slice(®ion(polygons), &positions, axis)
.map(|bins| bins.into_iter().map(py_region).collect())
})?)
}
#[pyfunction(signature=(points, polygons, *, include_boundary=true))]
fn inside(
py: Python<'_>,
points: PyRing,
polygons: PyRegion,
include_boundary: bool,
) -> PyResult<Vec<bool>> {
Ok(py.detach(|| geom::inside(&ring(points), ®ion(polygons), include_boundary))?)
}
#[pyfunction(signature=(polygons, max_points=199))]
fn fracture(py: Python<'_>, polygons: PyRegion, max_points: usize) -> PyResult<PyRegion> {
Ok(py.detach(|| geom::fracture(®ion(polygons), max_points).map(py_region))?)
}
#[pyfunction(signature=(points, *, origin=(0,0), translation=(0,0), angle_deg=0.0, magnification=1.0, reflect_x=false))]
fn transform_points(
py: Python<'_>,
points: PyRing,
origin: XY,
translation: XY,
angle_deg: f64,
magnification: f64,
reflect_x: bool,
) -> PyResult<PyRing> {
let transform = Transform {
angle_deg,
magnification,
reflect_x,
};
Ok(py.detach(|| {
geom::transform_points(&ring(points), point(origin), point(translation), transform)
.map(py_ring)
})?)
}
#[pyfunction(signature=(points, radius, *, tolerance=0.25))]
fn fillet(
py: Python<'_>,
points: PyRing,
radius: &Bound<'_, PyAny>,
tolerance: f64,
) -> PyResult<PyRing> {
let radii = if let Ok(scalar) = radius.extract::<f64>() {
vec![scalar]
} else {
radius.extract::<Vec<f64>>()?
};
Ok(py.detach(|| geom::fillet_radii(&ring(points), &radii, tolerance).map(py_ring))?)
}
#[pyfunction(signature=(points,sx,sy=None,*,origin=(0,0)))]
fn scale_points(points: PyRing, sx: f64, sy: Option<f64>, origin: XY) -> PyResult<PyRing> {
Ok(py_ring(geom::scale_points(
&ring(points),
point(origin),
sx,
sy.unwrap_or(sx),
)?))
}
#[pyfunction]
fn mirror_points(points: PyRing, a: XY, b: XY) -> PyResult<PyRing> {
Ok(py_ring(geom::mirror_points(
&ring(points),
point(a),
point(b),
)?))
}
#[pyfunction(signature=(center,outer,*,inner=None,start_deg=0.0,sweep_deg=360.0,tolerance=None,vertices=None))]
fn elliptic_sector(
center: XY,
outer: (f64, f64),
inner: Option<(f64, f64)>,
start_deg: f64,
sweep_deg: f64,
tolerance: Option<f64>,
vertices: Option<usize>,
) -> PyResult<PyRegion> {
let sampling = match (tolerance, vertices) {
(Some(_), Some(_)) => return Err(Error::invalid("choose vertices or tolerance").into()),
(_, Some(n)) => geom::Sampling::Vertices(n),
(tol, None) => geom::Sampling::Tolerance(tol.unwrap_or(0.25)),
};
Ok(py_region(geom::elliptic_sector(
point(center),
outer,
inner,
start_deg,
sweep_deg,
sampling,
)?))
}
#[pyfunction]
fn rectangle(corner1: XY, corner2: XY) -> PyResult<PyRing> {
Ok(py_ring(geom::rectangle(point(corner1), point(corner2))?))
}
#[pyfunction(signature=(center, radius_x, radius_y, *, tolerance=0.25))]
fn ellipse(
py: Python<'_>,
center: XY,
radius_x: f64,
radius_y: f64,
tolerance: f64,
) -> PyResult<PyRing> {
Ok(py.detach(|| geom::ellipse(point(center), radius_x, radius_y, tolerance).map(py_ring))?)
}
#[pyfunction(signature=(center, radius, sides, *, angle_deg=0.0))]
fn regular_polygon(
py: Python<'_>,
center: XY,
radius: f64,
sides: usize,
angle_deg: f64,
) -> PyResult<PyRing> {
Ok(py.detach(|| geom::regular_polygon(point(center), radius, sides, angle_deg).map(py_ring))?)
}
pub(crate) fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(boolean, m)?)?;
m.add_function(wrap_pyfunction!(scale_points, m)?)?;
m.add_function(wrap_pyfunction!(mirror_points, m)?)?;
m.add_function(wrap_pyfunction!(elliptic_sector, m)?)?;
m.add_function(wrap_pyfunction!(offset, m)?)?;
m.add_function(wrap_pyfunction!(slice, m)?)?;
m.add_function(wrap_pyfunction!(inside, m)?)?;
m.add_function(wrap_pyfunction!(fracture, m)?)?;
m.add_function(wrap_pyfunction!(transform_points, m)?)?;
m.add_function(wrap_pyfunction!(fillet, m)?)?;
m.add_function(wrap_pyfunction!(rectangle, m)?)?;
m.add_function(wrap_pyfunction!(ellipse, m)?)?;
m.add_function(wrap_pyfunction!(regular_polygon, m)?)?;
Ok(())
}