use crate::paths::{self, Cap, HobbyOptions, Join, StrokeStyle, Vec2};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
type XY = (f64, f64);
fn v(p: XY) -> Vec2 {
Vec2::new(p.0, p.1)
}
fn xy(p: Vec2) -> XY {
(p.x, p.y)
}
fn vs(ps: Vec<XY>) -> Vec<Vec2> {
ps.into_iter().map(v).collect()
}
fn samples(ps: Vec<paths::Sample>) -> Vec<(f64, XY)> {
ps.into_iter().map(|(t, p)| (t, xy(p))).collect()
}
#[pyfunction]
fn _path_bezier(py: Python<'_>, points: Vec<XY>, tolerance: f64) -> PyResult<Vec<(f64, XY)>> {
Ok(samples(
py.detach(|| paths::bezier(&vs(points), tolerance))?,
))
}
#[pyfunction]
fn _path_arc(
py: Python<'_>,
start: XY,
radii: XY,
initial: f64,
end: f64,
rotation: f64,
tolerance: f64,
) -> PyResult<Vec<(f64, XY)>> {
Ok(samples(py.detach(|| {
paths::arc(v(start), v(radii), initial, end, rotation, tolerance)
})?))
}
#[pyfunction]
fn _path_hobby(
py: Python<'_>,
points: Vec<XY>,
angles: Vec<Option<f64>>,
tension_in: Vec<f64>,
tension_out: Vec<f64>,
curl: (f64, f64),
cycle: bool,
) -> PyResult<Vec<(XY, XY)>> {
let controls = py.detach(|| {
paths::hobby_controls(
&vs(points),
&HobbyOptions {
angles: &angles,
tension_in: &tension_in,
tension_out: &tension_out,
initial_curl: curl.0,
final_curl: curl.1,
cycle,
},
)
})?;
Ok(controls.into_iter().map(|[a, b]| (xy(a), xy(b))).collect())
}
#[pyfunction]
fn _path_grid(points: Vec<XY>) -> PyResult<Vec<(i64, i64)>> {
Ok(paths::on_grid(&vs(points))?
.into_iter()
.map(|p| (p.x, p.y))
.collect())
}
#[pyfunction]
fn _path_polygon(points: Vec<XY>) -> PyResult<Vec<(i64, i64)>> {
Ok(paths::grid_outline(&vs(points))?
.into_iter()
.map(|p| (p.x, p.y))
.collect())
}
#[pyfunction]
fn _path_spine(points: Vec<XY>, offsets: Vec<f64>) -> PyResult<Vec<XY>> {
Ok(paths::offset_spine(&vs(points), &offsets)?
.into_iter()
.map(xy)
.collect())
}
#[pyfunction]
fn _path_fillet(a: XY, b: XY, c: XY, radius: f64, tol: f64) -> PyResult<Vec<XY>> {
Ok(paths::fillet(v(a), v(b), v(c), radius, tol)?
.into_iter()
.map(xy)
.collect())
}
type PySection = (Vec<XY>, Vec<f64>, Vec<f64>);
fn outline(
points: Vec<XY>,
widths: &[f64],
offsets: &[f64],
style: StrokeStyle,
sections: Option<Vec<PySection>>,
max_evals: usize,
callback: &mut dyn FnMut(paths::Junction) -> crate::Result<Vec<Vec2>>,
) -> crate::Result<Vec<Vec2>> {
match sections {
Some(input) => {
let data: Vec<_> = input
.into_iter()
.map(|(pts, widths, offsets)| paths::StrokeSection {
points: vs(pts),
widths,
offsets,
})
.collect();
paths::robust_outline(&data, style, max_evals, callback)
}
None => paths::stroke_outline(&vs(points), widths, offsets, style, callback),
}
}
#[pyfunction]
#[allow(clippy::too_many_arguments)]
fn _path_stroke(
py: Python<'_>,
points: Vec<XY>,
widths: Vec<f64>,
offsets: Vec<f64>,
join: &str,
cap: &str,
extensions: (f64, f64),
tolerance: f64,
miter_limit: f64,
max_evals: usize,
join_fn: Option<&Bound<'_, PyAny>>,
cap_fn: Option<&Bound<'_, PyAny>>,
sections: Option<Vec<PySection>>,
) -> PyResult<Vec<XY>> {
let join = match join {
"callback" => Join::Callback,
"miter" => Join::Miter,
"natural" => Join::Natural,
"bevel" => Join::Bevel,
"round" => Join::Round,
"smooth" => Join::Smooth,
_ => return Err(PyValueError::new_err("unknown path join")),
};
let cap = match cap {
"callback" => Cap::Callback,
"flush" => Cap::Flush,
"extended" => Cap::Extended,
"round" => Cap::Round,
"smooth" => Cap::Smooth,
"custom" => Cap::Custom(extensions.0, extensions.1),
_ => return Err(PyValueError::new_err("unknown path cap")),
};
let style = StrokeStyle {
join,
cap,
tolerance,
miter_limit,
};
let mut raised = None;
let mut callback = |j: paths::Junction| {
let pts: PyResult<Vec<XY>> = (|| {
let f = if j.is_cap { cap_fn } else { join_fn }
.ok_or_else(|| PyValueError::new_err("missing boundary callback"))?;
let values = if j.is_cap {
f.call1((xy(j.a), xy(j.u), xy(j.b), xy(j.v)))?
} else {
f.call1((xy(j.a), xy(j.u), xy(j.b), xy(j.v), xy(j.center), j.width))?
};
values.extract()
})();
pts.map(vs).map_err(|err| {
raised = Some(err);
crate::Error::invalid("path callback failed")
})
};
let polygons = if join_fn.is_none() && cap_fn.is_none() {
py.detach(|| {
outline(
points,
&widths,
&offsets,
style,
sections,
max_evals,
&mut |_| Err(crate::Error::invalid("missing path callback")),
)
})
} else {
outline(
points,
&widths,
&offsets,
style,
sections,
max_evals,
&mut callback,
)
};
if let Some(err) = raised {
return Err(err);
}
let p = polygons?;
Ok(p.into_iter().map(|q| (q.x, q.y)).collect())
}
pub(crate) fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(_path_bezier, m)?)?;
m.add_function(wrap_pyfunction!(_path_arc, m)?)?;
m.add_function(wrap_pyfunction!(_path_hobby, m)?)?;
m.add_function(wrap_pyfunction!(_path_stroke, m)?)?;
m.add_function(wrap_pyfunction!(_path_grid, m)?)?;
m.add_function(wrap_pyfunction!(_path_polygon, m)?)?;
m.add_function(wrap_pyfunction!(_path_spine, m)?)?;
m.add_function(wrap_pyfunction!(_path_fillet, m)?)?;
Ok(())
}