Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions star/src/turtle/elements/fill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<f64>) -> PointRelation {
/// Evaluates crossings and winding strictly ahead of `point` along unit direction `ray_dir`.
fn query_ray(strokes: &[Stroke], point: Point<f64>, ray_dir: Vector<f64>) -> PointRelation {
strokes
.iter()
.fold(PointRelation::default(), |mut total, stroke| {
total += stroke.point_relationship(point);
total += stroke.ray_relationship(point, ray_dir);
total
})
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -812,6 +801,7 @@ impl BoundaryGraph {

let mut current_edge_idx = start_edge_idx;
let mut loop_cmds: Vec<DrawCommand> = Vec::new();
let mut loop_edges: Vec<usize> = Vec::new();
let mut loop_vertices: Vec<usize> = vec![start_edge.from];
let mut vertex_positions: FxHashMap<usize, usize> =
FxHashMap::from_iter(iter::once((start_edge.from, 0)));
Expand All @@ -824,11 +814,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<DrawCommand> = loop_cmds.drain(cycle_start..).collect();
loop_edges.truncate(cycle_start);
for v in loop_vertices.drain((cycle_start + 1)..) {
vertex_positions.remove(&v);
}
Expand All @@ -844,7 +836,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
Expand Down
45 changes: 44 additions & 1 deletion star/src/turtle/elements/intersect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,50 @@ pub trait Intersections<Rhs = Self> {

impl Intersections<LineSegment<f64>> for LineSegment<f64> {
fn intersections(&self, rhs: &LineSegment<f64>) -> 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<f64>, p: Point<f64>| {
(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)]
}
}
}

Expand Down