Files
figcraft/crates/pathops/src/loose.rs
T
Livio Sidler 1d18f795d0 M4, #2731: a vector's lines are placed by what is filled beside them, and trimmed only where they end on the outline
From the review of #2732:
- A segment is on the outline when the fill lies on one side of it, in the fill or outside it
  when on both or neither. The loops alone don't say: a loop inside another that turns the
  same way bounds nothing under the non-zero rule, and was stroked as outline.
- An inside stroke cut every line in the fill by the fill. Now only what shows past the
  outline around a point where such a line ends on it: a line that goes through the fill
  keeps its parts outside, and one close to the edge its whole stroke.
- A segment's middle comes from its own curve, not from a copy of the network.
- The stroke cache's key takes in the segments and the points' places: the loops name
  segments by number, and two networks can have the same path with other numbers.
2026-11-29 12:34:14 +01:00

150 lines
6.9 KiB
Rust

//! Inside and outside strokes of a vector network that has more segments than its filled
//! outline: a line drawn across the fill, an edge two regions share, a tail that leaves the
//! shape. Only the outline has an inside and an outside. Its stroke is aligned as asked for; the
//! other segments are stroked on their centre at the same weight, so every line of the network
//! reads equally thick.
use figcraft_doc::{StrokeAlign, StrokeProps, VectorNetwork, WindingRule};
use figcraft_geom::kurbo::{BezPath, Circle, ParamCurve as _, ParamCurveDeriv as _, Point, Shape as _, Vec2};
/// Where a segment lies relative to the network's fill.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Place {
/// On the fill's outline: filled on one side.
Edge,
/// In the fill: filled on both sides (shared by regions, a line across one, a loop the fill
/// rule doesn't cut out).
Within,
/// Outside the fill: filled on neither side.
Beyond,
}
/// The stroke of `net` aligned `align` (Inside or Outside) at `props`' weight, or `None` when every
/// segment is on the fill's outline (the plain clip does it).
pub(crate) fn outline(net: &VectorNetwork, props: &StrokeProps, align: StrokeAlign) -> Option<BezPath> {
let even_odd = net.regions.iter().any(|r| r.winding == WindingRule::EvenOdd);
// Every segment in the loops once: the outline, when the loops say what is filled (one loop,
// or the even-odd rule). Under the non-zero rule a loop inside another may bound nothing.
let by_loops = even_odd || net.regions.iter().map(|r| r.loops.len()).sum::<usize>() <= 1;
if by_loops && loop_uses(net).iter().all(|u| u % 2 == 1) {
return None;
}
// Rounded points are drawn in on the whole network: a part of it would round other points.
let rounded = net.rounded();
let net = rounded.as_ref().unwrap_or(net);
let fill = net.fill_path();
let filled = |p: Point| {
let w = fill.winding(p);
if even_odd { w % 2 != 0 } else { w != 0 }
};
// A segment is placed by what is filled beside its middle, a little way off to either side:
// the loops alone don't say (a loop inside another of the same direction bounds nothing).
let bounds = fill.bounding_box();
let off = (bounds.width().max(bounds.height()) * 1e-5).max(1e-9);
let places: Vec<Place> = (0..net.segments.len())
.map(|i| {
let Some(c) = net.cubic(i) else { return Place::Beyond };
let (at, mut along) = (c.eval(0.5), c.deriv().eval(0.5).to_vec2());
if along.hypot2() == 0.0 {
along = c.p3 - c.p0;
}
let side = Vec2::new(-along.y, along.x).normalize() * off;
if !side.is_finite() {
return if filled(at) { Place::Within } else { Place::Beyond };
}
match (filled(at + side), filled(at - side)) {
(true, true) => Place::Within,
(false, false) => Place::Beyond,
_ => Place::Edge,
}
})
.collect();
if places.iter().all(|p| *p == Place::Edge) {
return None;
}
let part = |keep: &dyn Fn(Place) -> bool| -> VectorNetwork {
let mut vertices = net.vertices.clone();
for v in &mut vertices {
v.radius = 0.0;
}
let segments = net.segments.iter().zip(&places).filter(|(_, p)| keep(**p)).map(|(s, _)| *s).collect();
VectorNetwork { vertices, segments, regions: Vec::new() }
};
let w = props.weight;
let centred = |net: &VectorNetwork, width: f64| {
if net.segments.is_empty() {
return BezPath::new();
}
let styled = crate::points::Styled::new(net, props);
crate::centred(&net.stroke_path(), styled.as_ref(), width, props)
};
let op = if align == StrokeAlign::Inside { linesweeper::BinaryOp::Intersection } else { linesweeper::BinaryOp::Difference };
let edge = crate::clip(centred(&part(&|p| p == Place::Edge), w * 2.0), &fill, even_odd, op);
let lines = if align == StrokeAlign::Inside {
let within = centred(&part(&|p| p == Place::Within), w);
// Where a line in the fill ends on the outline, an inside stroke must not show past it.
// Only there: a line may leave the fill further on, or run close to its edge.
let mut on_edge = vec![false; net.vertices.len()];
for (s, _) in net.segments.iter().zip(&places).filter(|(_, p)| **p == Place::Edge) {
for v in [s.a, s.b] {
if let Some(e) = on_edge.get_mut(v) {
*e = true;
}
}
}
// As far as a cap or a mitre reaches from the point.
let reach = w * props.miter_limit.clamp(2.0, 16.0) / 2.0;
let mut ends = BezPath::new();
for (s, _) in net.segments.iter().zip(&places).filter(|(_, p)| **p == Place::Within) {
for v in [s.a, s.b] {
if on_edge.get(v) == Some(&true)
&& let Some(p) = net.vertices.get(v)
{
ends.extend(Circle::new((p.x, p.y), reach).to_path(0.01));
if let Some(e) = on_edge.get_mut(v) {
*e = false;
}
}
}
}
let trimmed = if ends.elements().is_empty() {
Ok(within.clone())
} else {
let past = if even_odd {
crate::simplify(&within).and_then(|s| crate::binary_by(&s, &fill, linesweeper::FillRule::EvenOdd, linesweeper::BinaryOp::Difference))
} else {
crate::binary(&within, &fill, linesweeper::BinaryOp::Difference)
};
past.and_then(|past| crate::binary(&past, &ends, linesweeper::BinaryOp::Intersection))
.and_then(|past| crate::binary(&within, &past, linesweeper::BinaryOp::Difference))
};
// Should that fail: all of it cut by the fill, as the outline's stroke is.
let mut lines = trimmed.unwrap_or_else(|_| crate::clip(within, &fill, even_odd, linesweeper::BinaryOp::Intersection));
lines.extend(centred(&part(&|p| p == Place::Beyond), w).elements().iter().copied());
lines
} else {
centred(&part(&|p| p != Place::Edge), w)
};
// One area without overlaps, as the even-odd rule the aligned strokes are filled by needs.
let joined =
crate::simplify(&lines).and_then(|lines| crate::binary_by(&edge, &lines, linesweeper::FillRule::EvenOdd, linesweeper::BinaryOp::Union));
Some(joined.unwrap_or_else(|_| {
let mut all = edge;
all.extend(lines.elements().iter().copied());
all
}))
}
/// How often each segment is in the loops of the regions.
fn loop_uses(net: &VectorNetwork) -> Vec<usize> {
let mut uses = vec![0usize; net.segments.len()];
for lp in net.regions.iter().flat_map(|r| &r.loops) {
for i in lp {
if let Some(u) = uses.get_mut(*i) {
*u += 1;
}
}
}
uses
}