Compare commits

...
2 changed files with 170 additions and 10 deletions

No files matched your search

@@ -50,7 +50,7 @@ pub(crate) fn bezpath_intersections(bezpath1: &BezPath, bezpath2: &BezPath, accu
}
/// Calculates the intersection points the segment has with another given segment and returns a list of parametric `t`-values with given accuracy.
fn segment_intersections(segment1: PathSeg, segment2: PathSeg, accuracy: Option<f64>) -> Vec<(f64, f64)> {
pub(crate) fn segment_intersections(segment1: PathSeg, segment2: PathSeg, accuracy: Option<f64>) -> Vec<(f64, f64)> {
let accuracy = accuracy.unwrap_or(DEFAULT_ACCURACY);
match (segment1, segment2) {
@@ -135,15 +135,11 @@ fn segment_intersections_inner(segment1: PathSeg, min_t1: f64, max_t1: f64, segm
return;
}
// Split curves in half
let (seg11, seg12) = segment1.subdivide();
let (seg21, seg22) = segment2.subdivide();
// Repeat checking the intersection with the combinations of the two halves of each curve
segment_intersections_inner(seg11, min_t1, mid_t1, seg21, min_t2, mid_t2, accuracy, intersections);
segment_intersections_inner(seg11, min_t1, mid_t1, seg22, mid_t2, max_t2, accuracy, intersections);
segment_intersections_inner(seg12, mid_t1, max_t1, seg21, min_t2, mid_t2, accuracy, intersections);
segment_intersections_inner(seg12, mid_t1, max_t1, seg22, mid_t2, max_t2, accuracy, intersections);
// Repeat checking the intersection with the combinations of the two halves of each curve's parameter range
segment_intersections_inner(segment1, min_t1, mid_t1, segment2, min_t2, mid_t2, accuracy, intersections);
segment_intersections_inner(segment1, min_t1, mid_t1, segment2, mid_t2, max_t2, accuracy, intersections);
segment_intersections_inner(segment1, mid_t1, max_t1, segment2, min_t2, mid_t2, accuracy, intersections);
segment_intersections_inner(segment1, mid_t1, max_t1, segment2, mid_t2, max_t2, accuracy, intersections);
}
}
@@ -496,4 +492,20 @@ mod tests {
.all()
);
}
// Pairs other than line or cubic-cubic take the subdivision path, which must narrow both curves' parameter ranges
#[test]
fn quadratic_crossing_cubic() {
let quadratic = PathSeg::Quad(QuadBez::new((195., 210.), (125., 235.), (65., 205.)));
let cubic = PathSeg::Cubic(CubicBez::new((87., 220.), (49., 194.), (49., 135.), (64., 96.)));
let expected = super::segment_intersections(PathSeg::Cubic(quadratic.to_cubic()), cubic, None);
assert_eq!(expected.len(), 1);
let intersections = super::segment_intersections(quadratic, cubic, None);
assert!(!intersections.is_empty());
for (quadratic_t, cubic_t) in intersections {
assert!(compare_points(quadratic.eval(quadratic_t), cubic.eval(cubic_t)));
assert!(compare_points(quadratic.eval(quadratic_t), quadratic.eval(expected[0].0)));
}
}
}
@@ -1,4 +1,5 @@
use crate::vector::addressing::{self, Span};
use crate::vector::algorithms::intersection::segment_intersections;
use crate::vector::contour::contours;
use crate::vector::misc::{ManipulatorGroup, Tangent, point_to_dvec2, segment_to_handles};
use crate::vector::path::{CurveType, Path, append_segment, continues_previous_path, curve_type_at, curve_types_of};
@@ -318,6 +319,16 @@ fn tangent_at_endpoint(segment: PathSeg, at_start: bool) -> DVec2 {
}
}
/// Moves the segment's end anchors onto the given positions, so pieces split from different segments meet exactly.
fn snap_endpoints(segment: PathSeg, start: DVec2, end: DVec2) -> PathSeg {
let [start, end] = [start, end].map(|position| kurbo::Point::new(position.x, position.y));
match segment {
PathSeg::Line(_) => PathSeg::Line(kurbo::Line::new(start, end)),
PathSeg::Quad(quad) => PathSeg::Quad(kurbo::QuadBez::new(start, quad.p1, end)),
PathSeg::Cubic(cubic) => PathSeg::Cubic(kurbo::CubicBez::new(start, cubic.p1, cubic.p2, end)),
}
}
impl SegmentGraph {
/// Walks every face orbit of the segment graph and traces each one's path and signed area.
fn face_orbits(&self) -> FaceOrbits {
@@ -465,6 +476,130 @@ impl SegmentGraph {
/// reverse-wound contour still punches its hole when it shares no points with what surrounds it.
pub fn construct_faces(&self) -> Vec<kurbo::BezPath> {
let orbits = self.face_orbits();
if self.has_extra_silhouette(&orbits)
&& let Some(planar) = self.split_at_crossings()
{
return planar.faces_from_orbits(&planar.face_orbits());
}
self.faces_from_orbits(&orbits)
}
/// Whether a connected component has more than one positive-area orbit, which a planar embedding never has.
/// It reveals segments crossing without a shared point, since the face walk then leaves the real faces.
fn has_extra_silhouette(&self, orbits: &FaceOrbits) -> bool {
fn root(component: &mut [usize], mut point: usize) -> usize {
while component[point] != point {
component[point] = component[component[point]];
point = component[point];
}
point
}
let mut component = (0..self.point_count).collect::<Vec<_>>();
for (&start, &end) in self.start_point.iter().zip(&self.end_point) {
let (start_root, end_root) = (root(&mut component, start), root(&mut component, end));
component[start_root] = end_root;
}
let mut silhouette_components = HashSet::new();
(0..orbits.face_count()).filter(|&orbit_index| orbits.areas[orbit_index] > 0.).any(|orbit_index| {
let Some(first_side) = orbits.face_sides(orbit_index).first() else { return false };
let component_root = root(&mut component, self.start_point[first_side.segment_index]);
!silhouette_components.insert(component_root)
})
}
/// A copy of the graph with every segment split where it crosses another, joining the pieces at a shared point.
/// Returns `None` if no segments cross.
fn split_at_crossings(&self) -> Option<Self> {
const T_TOLERANCE: f64 = 1e-6;
const POSITION_TOLERANCE: f64 = 1e-2;
let mut positions = vec![DVec2::NAN; self.point_count];
for (segment_index, segment) in self.segments.iter().enumerate() {
positions[self.start_point[segment_index]] = point_to_dvec2(segment.start());
positions[self.end_point[segment_index]] = point_to_dvec2(segment.end());
}
let bounding_boxes: Vec<kurbo::Rect> = self.segments.iter().map(kurbo::ParamCurveExtrema::bounding_box).collect();
let mut splits: Vec<Vec<(f64, usize)>> = vec![Vec::new(); self.len()];
for first in 0..self.len() {
for second in first + 1..self.len() {
if !bounding_boxes[first].overlaps(bounding_boxes[second]) {
continue;
}
for (first_t, second_t) in segment_intersections(self.segments[first], self.segments[second], None) {
let position = point_to_dvec2(self.segments[first].eval(first_t));
let endpoint_near = |segment_index: usize| {
[self.start_point[segment_index], self.end_point[segment_index]]
.into_iter()
.find(|&point| positions[point].distance(position) < POSITION_TOLERANCE)
};
let (first_endpoint, second_endpoint) = (endpoint_near(first), endpoint_near(second));
let point = match (first_endpoint, second_endpoint) {
(Some(_), Some(_)) => continue,
(Some(point), None) | (None, Some(point)) => point,
(None, None) => {
let existing = (self.point_count..positions.len()).find(|&point| positions[point].distance(position) < POSITION_TOLERANCE);
existing.unwrap_or_else(|| {
positions.push(position);
positions.len() - 1
})
}
};
if first_endpoint.is_none() {
splits[first].push((first_t, point));
}
if second_endpoint.is_none() {
splits[second].push((second_t, point));
}
}
}
}
if splits.iter().all(Vec::is_empty) {
return None;
}
let mut planar = Self {
spans: Vec::new(),
ids: Vec::new(),
start_point: Vec::new(),
end_point: Vec::new(),
segments: Vec::new(),
point_count: positions.len(),
};
for (segment_index, mut segment_splits) in splits.into_iter().enumerate() {
segment_splits.sort_by(|a, b| a.0.total_cmp(&b.0));
segment_splits.dedup_by_key(|&mut (_, point)| point);
let boundaries = std::iter::once((0., self.start_point[segment_index]))
.chain(segment_splits)
.chain(std::iter::once((1., self.end_point[segment_index])))
.collect::<Vec<_>>();
for window in boundaries.windows(2) {
let [(start_t, start), (end_t, end)] = [window[0], window[1]];
if end_t - start_t < T_TOLERANCE {
continue;
}
let piece = snap_endpoints(self.segments[segment_index].subsegment(start_t..end_t), positions[start], positions[end]);
planar.spans.push(self.spans[segment_index]);
planar.ids.push(self.ids[segment_index]);
planar.start_point.push(start);
planar.end_point.push(end);
planar.segments.push(piece);
}
}
Some(planar)
}
fn faces_from_orbits(&self, orbits: &FaceOrbits) -> Vec<kurbo::BezPath> {
let (core_degree, ..) = self.two_core();
// Split the bounded orbits (negative area) into fillable faces and deliberate negative space
@@ -1546,4 +1681,17 @@ mod tests {
assert_eq!(orbits.face_count(), 1);
assert_eq!(orbits.face_sides(0).len(), 6);
}
// An extruded square overlaps its offset copy, so its walls cross without sharing a point
#[test]
fn crossing_segments_split_into_the_regions_they_bound() {
let square = [DVec2::new(0., 0.), DVec2::new(100., 0.), DVec2::new(100., 100.), DVec2::new(0., 100.)];
let points: Vec<DVec2> = square.iter().copied().chain(square.iter().map(|point| *point + DVec2::new(30., 30.))).collect();
let segments = [(0, 1), (1, 2), (2, 3), (3, 0), (4, 5), (5, 6), (6, 7), (7, 4), (1, 5), (3, 7)];
let (vector, paths) = build_vector(&points, &segments);
let mut face_areas: Vec<f64> = vector.construct_faces(&paths).iter().map(|face| face.area().abs()).collect();
face_areas.sort_by(f64::total_cmp);
assert_eq!(face_areas, vec![450., 450., 4900., 5100., 5100.]);
}
}