mod ear_clipping;
mod fan;
pub use ear_clipping::{earclip_cell_countercw, earclip_cell_cw};
pub use fan::process_cell as fan_cell;
pub use fan::process_convex_cell as fan_convex_cell;
use honeycomb_core::cmap::SewError;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum TriangulateError {
#[error("face is already a triangle")]
AlreadyTriangulated,
#[error("no ear found in the polygon to triangulate")]
NoEar,
#[error("no star in the polygon to triangulate")]
NonFannable,
#[error("not enough darts were passed to the triangulation function - missing `{0}`")]
NotEnoughDarts(usize),
#[error("too many darts were passed to the triangulation function - missing `{0}`")]
TooManyDarts(usize),
#[error("face isn't defined correctly - {0}")]
UndefinedFace(&'static str),
#[error("an internal operation failed - {0}")]
OpFailed(#[from] SewError),
}
#[allow(clippy::missing_errors_doc)]
#[allow(
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::cast_abs_to_unsigned
)]
pub fn check_requirements(
n_darts_face: usize,
n_darts_allocated: usize,
) -> Result<(), TriangulateError> {
match n_darts_face {
1 | 2 => {
return Err(TriangulateError::UndefinedFace("less than 3 vertices"));
}
3 => {
return Err(TriangulateError::AlreadyTriangulated);
}
_ => {}
}
match n_darts_allocated as isize - (n_darts_face as isize - 3) * 2 {
diff @ ..0 => {
return Err(TriangulateError::NotEnoughDarts(diff.abs() as usize));
}
0 => {}
diff @ 1.. => {
return Err(TriangulateError::TooManyDarts(diff as usize));
}
}
Ok(())
}
#[cfg(test)]
mod tests;