From 49474a511cd1126603e3294be3dc42c3e3c046c9 Mon Sep 17 00:00:00 2001 From: Sameer Puri Date: Sat, 21 Nov 2026 17:39:15 -0800 Subject: [PATCH 1/3] Tracking entering tangent in boundary loop calculation Became wrong after extracting loops with touching boundary points --- star/src/turtle/elements/fill.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/star/src/turtle/elements/fill.rs b/star/src/turtle/elements/fill.rs index 72b60e5..a703572 100644 --- a/star/src/turtle/elements/fill.rs +++ b/star/src/turtle/elements/fill.rs @@ -812,6 +812,7 @@ impl BoundaryGraph { let mut current_edge_idx = start_edge_idx; let mut loop_cmds: Vec = Vec::new(); + let mut loop_edges: Vec = Vec::new(); let mut loop_vertices: Vec = vec![start_edge.from]; let mut vertex_positions: FxHashMap = FxHashMap::from_iter(iter::once((start_edge.from, 0))); @@ -824,11 +825,13 @@ impl BoundaryGraph { break; }; loop_cmds.push(current_edge.command); + loop_edges.push(current_edge_idx); let next_vertex = current_edge.to; // Whenever the walk revisits a vertex on the active stack, extract it. if let Some(&cycle_start) = vertex_positions.get(&next_vertex) { let cycle_cmds: Vec = loop_cmds.drain(cycle_start..).collect(); + loop_edges.truncate(cycle_start); for v in loop_vertices.drain((cycle_start + 1)..) { vertex_positions.remove(&v); } @@ -844,7 +847,14 @@ impl BoundaryGraph { loop_vertices.push(next_vertex); } - let tan_to = current_edge.tan_to; + // Use the tangent of the edge currently entering `next_vertex` on the active stack. + let Some(tan_to) = loop_edges + .last() + .and_then(|&idx| self.edges.get(idx)) + .map(|e| e.tan_to) + else { + break; + }; let outgoing_candidates = self.adjacency_map.get(&next_vertex); // Find the unvisited outgoing edge with the tightest rightward turn From 57ba020058224c28a8c1c4c93c1de1b5c1e36efa Mon Sep 17 00:00:00 2001 From: Sameer Puri Date: Sat, 21 Nov 2026 17:40:28 -0800 Subject: [PATCH 2/3] Use rays instead of offset point checks for fill check --- star/src/turtle/elements/fill.rs | 35 +++++++++++--------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/star/src/turtle/elements/fill.rs b/star/src/turtle/elements/fill.rs index a703572..af784d4 100644 --- a/star/src/turtle/elements/fill.rs +++ b/star/src/turtle/elements/fill.rs @@ -28,11 +28,6 @@ const VERTEX_SNAP_EPSILON_SQ: f64 = VERTEX_SNAP_EPSILON * VERTEX_SNAP_EPSILON; /// Minimum absolute signed area for a boundary loop to be considered non-degenerate. const MIN_LOOP_AREA: f64 = 1e-6; -/// Fraction of segment scale used to offset midpoint samples along the normal. -const SAMPLE_OFFSET_FRACTION: f64 = 0.05; -const MIN_SAMPLE_OFFSET: f64 = 1e-4; -const MAX_SAMPLE_OFFSET: f64 = 0.5; - /// Result of evaluating a point against one or more closed non-overlapping [`Stroke`]s. #[derive(Default)] struct PointRelation { @@ -346,12 +341,12 @@ impl Stroke { } } -/// Queries the crossing count and winding number at `point` relative to `strokes`. -fn query_point(strokes: &[Stroke], point: Point) -> PointRelation { +/// Evaluates crossings and winding strictly ahead of `point` along unit direction `ray_dir`. +fn query_ray(strokes: &[Stroke], point: Point, ray_dir: Vector) -> PointRelation { strokes .iter() .fold(PointRelation::default(), |mut total, stroke| { - total += stroke.point_relationship(point); + total += stroke.ray_relationship(point, ray_dir); total }) } @@ -723,23 +718,17 @@ impl SubdividedSegments<'_> { continue; } - // Sample a point on either side of the segment to classify it. + // Cast rays from `midpoint` to the left/right half-planes, tilted by + // `RAY_SLOPE` to avoid rational vertices. + let tangent = segment.tangent(0.5); let normal = segment.normal(0.5); - let bbox = segment.bounding_box(); - let seg_scale = bbox.width().max(bbox.height()); - let eps = - (seg_scale * SAMPLE_OFFSET_FRACTION).clamp(MIN_SAMPLE_OFFSET, MAX_SAMPLE_OFFSET); - - // Consider a line segment l. The line segment k through p_left and p_right - // has length 2*eps, is perpendicular to l, and passes through the midpoint of l. - let p_left = midpoint + normal * eps; - let p_right = midpoint - normal * eps; - - let left_query = query_point(self.input_strokes, p_left); - let right_query = query_point(self.input_strokes, p_right); + let left_dir = (normal + tangent * RAY_SLOPE).normalize(); + let right_dir = (-normal + tangent * RAY_SLOPE).normalize(); - let left_filled = left_query.is_filled(self.fill_rule); - let right_filled = right_query.is_filled(self.fill_rule); + let left_filled = + query_ray(self.input_strokes, midpoint, left_dir).is_filled(self.fill_rule); + let right_filled = + query_ray(self.input_strokes, midpoint, right_dir).is_filled(self.fill_rule); // Not a boundary, fully within or fully outside the filled region. if left_filled == right_filled { From 73437a6ac027b9f5d6072af9efcc97757d30eedd Mon Sep 17 00:00:00 2001 From: Sameer Puri Date: Sat, 21 Nov 2026 18:00:58 -0800 Subject: [PATCH 3/3] Handle collinear line segments when calculating LineSegment intersections --- star/src/turtle/elements/intersect.rs | 45 ++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/star/src/turtle/elements/intersect.rs b/star/src/turtle/elements/intersect.rs index c66f943..c04f720 100644 --- a/star/src/turtle/elements/intersect.rs +++ b/star/src/turtle/elements/intersect.rs @@ -30,7 +30,50 @@ pub trait Intersections { impl Intersections> for LineSegment { fn intersections(&self, rhs: &LineSegment) -> SmallVec<[(f64, f64); 3]> { - self.intersection_t(rhs).into_iter().collect() + if let Some(intersection) = self.intersection_t(rhs) { + return smallvec![intersection]; + } + + // Degenerate + if self.square_length() <= f64::EPSILON || rhs.square_length() <= f64::EPSILON { + return smallvec![]; + } + + // Parallel + let line1 = self.to_line(); + let line2 = rhs.to_line(); + if !(line1.distance_to_point(&rhs.from) <= PARAM_EPSILON + && line1.distance_to_point(&rhs.to) <= PARAM_EPSILON + && line2.distance_to_point(&self.from) <= PARAM_EPSILON + && line2.distance_to_point(&self.to) <= PARAM_EPSILON) + { + return smallvec![]; + } + + let param_on = |seg: &LineSegment, p: Point| { + (p - seg.from).dot(seg.to_vector()) / seg.square_length() + }; + + let [t_from, t_to] = [rhs.from, rhs.to].map(|p| param_on(self, p)); + let arr @ [t_min, t_max] = [t_from.min(t_to), t_from.max(t_to)]; + + // Disjoint collinear + if t_min > 1.0 + PARAM_EPSILON || t_max < -PARAM_EPSILON { + return smallvec![]; + } + let arr @ [t_low, t_high] = arr.map(|t| t.clamp(0., 1.)); + + let [u_low, u_high] = arr + .map(|t| param_on(rhs, self.sample(t))) + .map(|u| u.clamp(0.0, 1.0)); + + if t_high - t_low <= PARAM_EPSILON { + // Single shared + smallvec![(t_low, u_low)] + } else { + // Overlapping collinear interval + smallvec![(t_low, u_low), (t_high, u_high)] + } } }