use super::curve::Curve;
use super::path::Path;
use super::shape::Shape;
use super::shape_utils;
use crate::math::{Box2, Color, Vector2};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FillRule {
#[default]
NonZero,
EvenOdd,
}
#[derive(Clone, Debug, Default)]
pub struct ShapePath {
pub color: Color,
pub sub_paths: Vec<Path>,
current_path: Option<usize>,
pub fill_rule: FillRule,
}
impl ShapePath {
pub fn new() -> Self {
Self::default()
}
pub fn type_name(&self) -> &'static str {
"ShapePath"
}
fn current_path(&mut self) -> &mut Path {
let index = self
.current_path
.expect("three-rs: ShapePath: a drawing call before moveTo (three.js throws on the null currentPath)");
&mut self.sub_paths[index]
}
pub fn move_to(&mut self, x: f64, y: f64) -> &mut Self {
let mut path = Path::new();
path.move_to(x, y);
self.sub_paths.push(path);
self.current_path = Some(self.sub_paths.len() - 1);
self
}
pub fn line_to(&mut self, x: f64, y: f64) -> &mut Self {
self.current_path().line_to(x, y);
self
}
pub fn quadratic_curve_to(
&mut self,
a_c_px: f64,
a_c_py: f64,
a_x: f64,
a_y: f64,
) -> &mut Self {
self.current_path()
.quadratic_curve_to(a_c_px, a_c_py, a_x, a_y);
self
}
pub fn bezier_curve_to(
&mut self,
a_cp1x: f64,
a_cp1y: f64,
a_cp2x: f64,
a_cp2y: f64,
a_x: f64,
a_y: f64,
) -> &mut Self {
self.current_path()
.bezier_curve_to(a_cp1x, a_cp1y, a_cp2x, a_cp2y, a_x, a_y);
self
}
pub fn spline_thru(&mut self, pts: &[Vector2]) -> &mut Self {
self.current_path().spline_thru(pts);
self
}
pub fn to_shapes(&self) -> Vec<Shape> {
fn point_in_polygon(p: &Vector2, polygon: &[Vector2]) -> bool {
let mut inside = false;
let n = polygon.len();
let mut j = n.wrapping_sub(1);
for i in 0..n {
let a = &polygon[i];
let b = &polygon[j];
if (a.y > p.y) != (b.y > p.y) && p.x < (b.x - a.x) * (p.y - a.y) / (b.y - a.y) + a.x
{
inside = !inside;
}
j = i;
}
inside
}
fn get_interior_point(polygon: &[Vector2], bounding_box: &Box2) -> Vector2 {
let mut point = bounding_box.get_center();
if point_in_polygon(&point, polygon) {
return point;
}
let y = point.y;
let mut intercepts: Vec<f64> = Vec::new();
let n = polygon.len();
for i in 0..n {
let a = &polygon[i];
let b = &polygon[(i + 1) % n];
if (a.y > y) != (b.y > y) {
let x = a.x + (y - a.y) * (b.x - a.x) / (b.y - a.y);
intercepts.push(x);
}
}
if intercepts.len() > 1 {
intercepts.sort_by(|a, b| js_compare(a - b));
point.x = (intercepts[0] + intercepts[1]) / 2.0;
}
point
}
let fill_rule = self.fill_rule;
let is_inside = |w: i64| match fill_rule {
FillRule::NonZero => w != 0,
FillRule::EvenOdd => (w & 1) != 0,
};
#[derive(Clone, Copy, PartialEq)]
enum Role {
Outer,
Hole,
}
struct Entry<'a> {
sub_path: &'a Path,
points: Vec<Vector2>,
bounding_box: Box2,
interior_point: Vector2,
abs_area: f64,
winding: i64,
container: Option<usize>,
exclude: bool,
role: Option<Role>,
}
let mut entries: Vec<Entry> = Vec::new();
for sub_path in &self.sub_paths {
let points = sub_path.get_points(12);
if points.len() < 3 {
continue;
}
let area = shape_utils::area(&points);
if area == 0.0 {
continue;
}
let mut bounding_box = Box2::default();
for point in &points {
bounding_box.expand_by_point(point);
}
let interior_point = get_interior_point(&points, &bounding_box);
entries.push(Entry {
sub_path,
points,
bounding_box,
interior_point,
abs_area: area.abs(),
winding: if area < 0.0 { -1 } else { 1 },
container: None,
exclude: false,
role: None,
});
}
entries.sort_by(|a, b| js_compare(b.abs_area - a.abs_area));
for i in 0..entries.len() {
let mut container_winding = 0;
for j in (0..i).rev() {
let candidate = &entries[j];
if !candidate
.bounding_box
.contains_box(&entries[i].bounding_box)
{
continue;
}
if !point_in_polygon(&entries[i].interior_point, &candidate.points) {
continue;
}
let container = if candidate.exclude {
candidate.container
} else {
Some(j)
};
container_winding = candidate.winding;
let entry = &mut entries[i];
entry.container = container;
entry.winding += container_winding;
break;
}
if is_inside(entries[i].winding) == is_inside(container_winding) {
entries[i].exclude = true;
}
}
for i in 0..entries.len() {
if entries[i].exclude {
continue;
}
let role = match entries[i].container {
None => Role::Outer,
Some(c) if entries[c].role == Some(Role::Hole) => Role::Outer,
Some(_) => Role::Hole,
};
entries[i].role = Some(role);
}
let mut shapes: Vec<Shape> = Vec::new();
let mut shape_by_entry: Vec<Option<usize>> = vec![None; entries.len()];
for (i, entry) in entries.iter().enumerate() {
if entry.exclude || entry.role != Some(Role::Outer) {
continue;
}
let mut shape = Shape::new();
shape.curves = entry.sub_path.curves.clone();
shapes.push(shape);
shape_by_entry[i] = Some(shapes.len() - 1);
}
for entry in &entries {
if entry.exclude || entry.role != Some(Role::Hole) {
continue;
}
let Some(shape) = entry.container.and_then(|c| shape_by_entry[c]) else {
continue;
};
let mut hole = Path::new();
hole.curves = entry.sub_path.curves.clone();
shapes[shape].holes.push(hole);
}
shapes
}
}
fn js_compare(result: f64) -> std::cmp::Ordering {
if result < 0.0 {
std::cmp::Ordering::Less
} else if result > 0.0 {
std::cmp::Ordering::Greater
} else {
std::cmp::Ordering::Equal
}
}