Unify selection systems and make region/lasso cut robust
Collapse the two parallel selection systems into one. The RegionSelect tool (rect + lasso) now cuts the geometry along the region outline and selects the resulting sub-pieces into the standard `Selection` ID-sets, exactly like every other tool. The vestigial floating `RegionSelection` (drag never wired; commit/delete/copy were stubbed) and all its plumbing are removed, so Group, Convert-to-Movie-Clip, Delete, and Properties all operate uniformly from lasso, rect, marquee, and click selections. Region cutting is reworked onto a robust planar arrangement: - Replace fragile incremental "split a fill by one cut edge" logic with planar face re-tracing (`retrace_fills_after_cut` + `trace_faces`), which correctly handles arbitrary holed/concave fills. - `extract_subgraph` no longer frees vertices still referenced by kept boundary edges (fixed Group leaving freed-but-referenced vertices that a later alloc reused and corrupted). - `split_fill_by_*` direction fix (was producing disconnected boundaries rendered as stray diagonals). - `fill_interior_point` (area-centroid + inward-step fallback) for reliable inside/outside classification of non-convex pieces. - Coincident-edge dedupe + degenerate-fill removal (edge-adjacent shapes no longer make zero-area sliver fills). - Dangling-edge pruning, near-coincident endpoint welding, induced- subgraph expansion, and tracking of `split_edge` sub-edges, so self-intersecting freehand lassos cut correctly. Region-select capture is available behind LIGHTNINGBEAM_DUMP_REGION=1 for turning a misbehaving cut into a deterministic test. Extensive regression tests added in vector_graph/tests/region_cut_select.rs.
This commit is contained in:
@@ -417,6 +417,91 @@ impl VectorGraph {
|
||||
self.boundary_to_bezpath(&fill.boundary)
|
||||
}
|
||||
|
||||
/// A point guaranteed to lie inside the fill — for point-in-region classification
|
||||
/// (e.g. deciding whether a fill is inside a lasso). Prefers the polygon area-centroid,
|
||||
/// but for a non-convex fill (e.g. an L-shape, where the area-centroid can fall in the
|
||||
/// concavity *outside* the shape) it steps just inward from a boundary edge instead.
|
||||
/// The naive average of boundary-edge midpoints is NOT reliable here — it can land
|
||||
/// outside a non-convex fill and misclassify it.
|
||||
pub fn fill_interior_point(&self, fill_id: FillId) -> Point {
|
||||
let boundary = self.fills[fill_id.idx()].boundary.clone();
|
||||
self.boundary_interior_point(&boundary)
|
||||
}
|
||||
|
||||
/// A point guaranteed to lie inside the region enclosed by a `(edge, direction)`
|
||||
/// boundary loop. See [`fill_interior_point`].
|
||||
pub fn boundary_interior_point(&self, boundary: &[(EdgeId, Direction)]) -> Point {
|
||||
use kurbo::{ParamCurve, Shape, Vec2};
|
||||
let path = self.boundary_to_bezpath(boundary);
|
||||
|
||||
// Ordered polygon corners: the directed start point of each boundary edge.
|
||||
let mut pts: Vec<Point> = Vec::new();
|
||||
for &(eid, dir) in boundary {
|
||||
if eid.is_none() {
|
||||
continue;
|
||||
}
|
||||
let c = self.edges[eid.idx()].curve;
|
||||
pts.push(match dir {
|
||||
Direction::Forward => c.p0,
|
||||
Direction::Backward => c.p3,
|
||||
});
|
||||
}
|
||||
if pts.len() < 3 {
|
||||
if pts.is_empty() {
|
||||
return Point::ZERO;
|
||||
}
|
||||
let (sx, sy) = pts.iter().fold((0.0, 0.0), |(x, y), p| (x + p.x, y + p.y));
|
||||
return Point::new(sx / pts.len() as f64, sy / pts.len() as f64);
|
||||
}
|
||||
|
||||
// Shoelace area-centroid.
|
||||
let (mut a2, mut cx, mut cy) = (0.0, 0.0, 0.0);
|
||||
for i in 0..pts.len() {
|
||||
let p0 = pts[i];
|
||||
let p1 = pts[(i + 1) % pts.len()];
|
||||
let cross = p0.x * p1.y - p1.x * p0.y;
|
||||
a2 += cross;
|
||||
cx += (p0.x + p1.x) * cross;
|
||||
cy += (p0.y + p1.y) * cross;
|
||||
}
|
||||
if a2.abs() > 1e-9 {
|
||||
let c = Point::new(cx / (3.0 * a2), cy / (3.0 * a2));
|
||||
if path.winding(c) != 0 {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: step a small distance inward from a boundary edge midpoint.
|
||||
let (mut minx, mut miny, mut maxx, mut maxy) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN);
|
||||
for p in &pts {
|
||||
minx = minx.min(p.x);
|
||||
miny = miny.min(p.y);
|
||||
maxx = maxx.max(p.x);
|
||||
maxy = maxy.max(p.y);
|
||||
}
|
||||
let eps = ((maxx - minx).min(maxy - miny) * 1e-3).max(1e-4);
|
||||
for &(eid, _) in boundary {
|
||||
if eid.is_none() {
|
||||
continue;
|
||||
}
|
||||
let c = self.edges[eid.idx()].curve;
|
||||
let mid = c.eval(0.5);
|
||||
let tangent = c.eval(0.5001) - c.eval(0.4999);
|
||||
let len = tangent.hypot();
|
||||
if len < 1e-12 {
|
||||
continue;
|
||||
}
|
||||
let n = Vec2::new(-tangent.y / len, tangent.x / len);
|
||||
for s in [1.0_f64, -1.0] {
|
||||
let cand = mid + n * (s * eps);
|
||||
if path.winding(cand) != 0 {
|
||||
return cand;
|
||||
}
|
||||
}
|
||||
}
|
||||
pts[0]
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Vertex editing
|
||||
// -------------------------------------------------------------------
|
||||
@@ -653,6 +738,10 @@ impl VectorGraph {
|
||||
}
|
||||
|
||||
let mut all_new_edges = Vec::new();
|
||||
// Sub-edges produced when a stroke segment splits an existing edge (incl. an earlier
|
||||
// segment of this same stroke). Tracked separately so they reach the fill re-tracer
|
||||
// without polluting the returned edge list.
|
||||
let mut split_products: Vec<EdgeId> = Vec::new();
|
||||
let mut prev_end_vertex: Option<VertexId> = None;
|
||||
|
||||
for (seg_idx, seg) in expanded_segments.iter().enumerate() {
|
||||
@@ -706,7 +795,14 @@ impl VectorGraph {
|
||||
let remapped_t = original_edge_t / head_end;
|
||||
let remapped_t = remapped_t.clamp(ENDPOINT_T_MARGIN, 1.0 - ENDPOINT_T_MARGIN);
|
||||
|
||||
let (mid_v, _sub_a, _sub_b) = self.split_edge(eid, remapped_t);
|
||||
let (mid_v, sub_a, sub_b) = self.split_edge(eid, remapped_t);
|
||||
// Track the split products so the fill re-tracer sees them: when a later
|
||||
// stroke segment crosses an earlier one, these sub-edges are part of the
|
||||
// stroke's arrangement but would otherwise be invisible to the re-trace.
|
||||
// (Kept out of `all_new_edges` so the returned edge list stays the stroke's
|
||||
// own edges only.)
|
||||
split_products.push(sub_a);
|
||||
split_products.push(sub_b);
|
||||
// Snap vertex to intersection point
|
||||
self.vertices[mid_v.idx()].position = point;
|
||||
// Merge with nearby existing vertex if within snap distance
|
||||
@@ -791,127 +887,460 @@ impl VectorGraph {
|
||||
prev_end_vertex = Some(end_v);
|
||||
}
|
||||
|
||||
// Fill splitting pass: for each new edge, check if both endpoints
|
||||
// lie on any fill's boundary — if so, split that fill.
|
||||
let edges_to_check = all_new_edges.clone();
|
||||
for &eid in &edges_to_check {
|
||||
let v0 = self.edges[eid.idx()].vertices[0];
|
||||
let v1 = self.edges[eid.idx()].vertices[1];
|
||||
// Weld dangling stroke endpoints onto a near-coincident existing vertex. A
|
||||
// self-intersecting freehand stroke can create an intersection vertex a fraction of
|
||||
// a pixel away from a segment endpoint that should be the same point; if they don't
|
||||
// merge, the stroke's loop is broken by a degree-1 stub and the cut is lost.
|
||||
self.weld_dangling_endpoints(&all_new_edges, snap_epsilon.max(1.0));
|
||||
|
||||
// Find fills where both v0 and v1 appear as boundary vertices
|
||||
let fill_ids: Vec<FillId> = self.fills
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, f)| !f.deleted)
|
||||
.filter(|(_, f)| {
|
||||
let has_v0 = f.boundary.iter().any(|&(be, _)| {
|
||||
let e = &self.edges[be.idx()];
|
||||
e.vertices[0] == v0 || e.vertices[1] == v0
|
||||
});
|
||||
let has_v1 = f.boundary.iter().any(|&(be, _)| {
|
||||
let e = &self.edges[be.idx()];
|
||||
e.vertices[0] == v1 || e.vertices[1] == v1
|
||||
});
|
||||
has_v0 && has_v1
|
||||
})
|
||||
.map(|(i, _)| FillId(i as u32))
|
||||
.collect();
|
||||
// Coincident-edge cleanup: a new edge that lands exactly on an existing edge
|
||||
// between the same two vertices (e.g. drawing a shape whose edge snaps onto an
|
||||
// existing one) must not be duplicated — duplicates produce zero-area "sliver"
|
||||
// fills. Merge such duplicates before splitting/filling.
|
||||
self.dedupe_coincident_new_edges(&all_new_edges);
|
||||
|
||||
for fid in fill_ids {
|
||||
self.split_fill_by_edge(fid, eid);
|
||||
}
|
||||
}
|
||||
// Re-derive the fills touched by the new edges. Rather than incrementally splitting
|
||||
// along single cut edges (which can't handle a lasso whose path is interrupted by a
|
||||
// hole/notch in a non-convex fill), we re-trace the planar faces of the affected
|
||||
// sub-arrangement and rebuild the fills from them. This is robust for arbitrary
|
||||
// holed/concave fills (e.g. cutting across geometry left behind by a prior group).
|
||||
// Include split products so a self-crossing stroke's full arrangement is seen.
|
||||
let mut retrace_edges = all_new_edges.clone();
|
||||
retrace_edges.extend(split_products);
|
||||
self.retrace_fills_after_cut(&retrace_edges);
|
||||
|
||||
// Drop any zero-area fills (e.g. slivers left between coincident edges).
|
||||
self.remove_degenerate_fills();
|
||||
|
||||
all_new_edges
|
||||
}
|
||||
|
||||
/// When a new edge splits a fill (both endpoints on the fill's boundary),
|
||||
/// split the fill into two fills.
|
||||
pub fn split_fill_by_edge(
|
||||
&mut self,
|
||||
fill_id: FillId,
|
||||
splitting_edge: EdgeId,
|
||||
) -> Option<(FillId, FillId)> {
|
||||
let fill = &self.fills[fill_id.idx()];
|
||||
if fill.deleted {
|
||||
return None;
|
||||
}
|
||||
|
||||
let split_v0 = self.edges[splitting_edge.idx()].vertices[0];
|
||||
let split_v1 = self.edges[splitting_edge.idx()].vertices[1];
|
||||
|
||||
// Find the positions in the boundary where the splitting edge's
|
||||
// endpoint vertices appear as the "arrival" vertex of a directed edge.
|
||||
let boundary = fill.boundary.clone();
|
||||
|
||||
// Helper: get the "end" vertex of a directed boundary edge
|
||||
let end_vertex = |eid: EdgeId, dir: Direction| -> VertexId {
|
||||
match dir {
|
||||
Direction::Forward => self.edges[eid.idx()].vertices[1],
|
||||
Direction::Backward => self.edges[eid.idx()].vertices[0],
|
||||
/// Merge edges from the latest stroke that are geometrically coincident with an
|
||||
/// existing edge between the same two vertices (drawing a shape whose edge lands exactly
|
||||
/// on an existing edge). Keeps the existing edge, redirects fill references, and frees
|
||||
/// the duplicate — preventing zero-area "sliver" fills between the two copies.
|
||||
/// Weld degree-1 endpoints of freshly inserted edges onto a near-coincident existing
|
||||
/// vertex. A self-intersecting freehand stroke can create an intersection vertex a
|
||||
/// fraction of a pixel from a segment endpoint that should be the same point; if they
|
||||
/// don't merge, the stroke's loop is broken by a degree-1 stub and the cut is lost.
|
||||
fn weld_dangling_endpoints(&mut self, new_edges: &[EdgeId], eps: f64) {
|
||||
// Repeatedly weld the closest dangling new endpoint to its nearest neighbour.
|
||||
loop {
|
||||
let mut to_merge: Option<(VertexId, VertexId)> = None; // (keep, merge)
|
||||
'scan: for &e in new_edges {
|
||||
if self.edges[e.idx()].deleted {
|
||||
continue;
|
||||
}
|
||||
for &v in &self.edges[e.idx()].vertices {
|
||||
if self.vertices[v.idx()].deleted || self.edges_at_vertex(v).len() != 1 {
|
||||
continue;
|
||||
}
|
||||
let pv = self.vertices[v.idx()].position;
|
||||
let mut best: Option<(f64, VertexId)> = None;
|
||||
for ui in 0..self.vertices.len() {
|
||||
if ui == v.idx() || self.vertices[ui].deleted {
|
||||
continue;
|
||||
}
|
||||
let pu = self.vertices[ui].position;
|
||||
let d = (pu.x - pv.x).hypot(pu.y - pv.y);
|
||||
if d < eps && best.map_or(true, |(bd, _)| d < bd) {
|
||||
best = Some((d, VertexId(ui as u32)));
|
||||
}
|
||||
}
|
||||
if let Some((_, keep)) = best {
|
||||
to_merge = Some((keep, v));
|
||||
break 'scan;
|
||||
}
|
||||
}
|
||||
}
|
||||
match to_merge {
|
||||
Some((keep, merge)) => self.merge_vertices(keep, merge),
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
// Drop any edges that collapsed to zero length (both endpoints welded together).
|
||||
let degenerate: Vec<EdgeId> = self
|
||||
.edges
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, e)| !e.deleted && e.vertices[0] == e.vertices[1])
|
||||
.map(|(i, _)| EdgeId(i as u32))
|
||||
.collect();
|
||||
for e in degenerate {
|
||||
for fill in &mut self.fills {
|
||||
if !fill.deleted {
|
||||
fill.boundary.retain(|&(fe, _)| fe != e);
|
||||
}
|
||||
}
|
||||
self.free_edge(e);
|
||||
}
|
||||
}
|
||||
|
||||
fn dedupe_coincident_new_edges(&mut self, new_edges: &[EdgeId]) {
|
||||
for &ne in new_edges {
|
||||
if self.edges[ne.idx()].deleted {
|
||||
continue;
|
||||
}
|
||||
let va = self.edges[ne.idx()].vertices[0];
|
||||
let vb = self.edges[ne.idx()].vertices[1];
|
||||
let candidates: Vec<EdgeId> = self
|
||||
.edges_at_vertex(va)
|
||||
.into_iter()
|
||||
.filter(|&e| e != ne && !self.edges[e.idx()].deleted)
|
||||
.filter(|&e| {
|
||||
let v = self.edges[e.idx()].vertices;
|
||||
(v[0] == va && v[1] == vb) || (v[0] == vb && v[1] == va)
|
||||
})
|
||||
.collect();
|
||||
for c in candidates {
|
||||
if self.edges[c.idx()].deleted {
|
||||
continue;
|
||||
}
|
||||
if self.curves_coincident(ne, c) {
|
||||
self.redirect_edge_in_fills(ne, c);
|
||||
self.free_edge(ne);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether two edges (already known to share both endpoints) trace the same path.
|
||||
/// Coincident duplicates in practice are straight collinear segments (a shape edge
|
||||
/// snapping onto an existing edge), so we treat "both are straight lines between the
|
||||
/// same endpoints" as coincident. Comparing curve `eval(t)` directly is unreliable —
|
||||
/// split sub-edges are non-uniformly parameterised, so equal `t` ≠ equal point.
|
||||
fn curves_coincident(&self, a: EdgeId, b: EdgeId) -> bool {
|
||||
let is_straight = |c: kurbo::CubicBez| {
|
||||
let chord = c.p3 - c.p0;
|
||||
let len = chord.hypot();
|
||||
if len < 1e-9 {
|
||||
return true; // zero-length chord — degenerate, treat as coincident
|
||||
}
|
||||
// Perpendicular distance of each control point from the p0→p3 chord.
|
||||
let dist = |p: Point| ((p - c.p0).cross(chord)).abs() / len;
|
||||
dist(c.p1) < 1e-2 && dist(c.p2) < 1e-2
|
||||
};
|
||||
is_straight(self.edges[a.idx()].curve) && is_straight(self.edges[b.idx()].curve)
|
||||
}
|
||||
|
||||
// Find positions where boundary edges arrive at split_v0 and split_v1
|
||||
let mut pos_v0: Option<usize> = None;
|
||||
let mut pos_v1: Option<usize> = None;
|
||||
|
||||
for (i, &(eid, dir)) in boundary.iter().enumerate() {
|
||||
let ev = end_vertex(eid, dir);
|
||||
if ev == split_v0 && pos_v0.is_none() {
|
||||
pos_v0 = Some(i);
|
||||
/// Replace every `from` boundary reference with `to`, preserving traversal direction.
|
||||
fn redirect_edge_in_fills(&mut self, from: EdgeId, to: EdgeId) {
|
||||
let f = self.edges[from.idx()].vertices;
|
||||
let t = self.edges[to.idx()].vertices;
|
||||
let same_dir = t[0] == f[0] && t[1] == f[1];
|
||||
for fill in &mut self.fills {
|
||||
if fill.deleted {
|
||||
continue;
|
||||
}
|
||||
if ev == split_v1 && pos_v1.is_none() {
|
||||
pos_v1 = Some(i);
|
||||
for entry in &mut fill.boundary {
|
||||
if entry.0 == from {
|
||||
entry.1 = match (entry.1, same_dir) {
|
||||
(Direction::Forward, true) | (Direction::Backward, false) => {
|
||||
Direction::Forward
|
||||
}
|
||||
(Direction::Backward, true) | (Direction::Forward, false) => {
|
||||
Direction::Backward
|
||||
}
|
||||
};
|
||||
entry.0 = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let pos_v0 = pos_v0?;
|
||||
let pos_v1 = pos_v1?;
|
||||
/// Drop fills that enclose ~zero area (degenerate slivers from coincident edges).
|
||||
fn remove_degenerate_fills(&mut self) {
|
||||
for i in 0..self.fills.len() {
|
||||
if self.fills[i].deleted {
|
||||
continue;
|
||||
}
|
||||
let mut pts: Vec<Point> = Vec::new();
|
||||
for &(eid, dir) in &self.fills[i].boundary {
|
||||
if eid.is_none() {
|
||||
continue;
|
||||
}
|
||||
let c = self.edges[eid.idx()].curve;
|
||||
pts.push(match dir {
|
||||
Direction::Forward => c.p0,
|
||||
Direction::Backward => c.p3,
|
||||
});
|
||||
}
|
||||
let area = if pts.len() < 3 {
|
||||
0.0
|
||||
} else {
|
||||
let mut a2 = 0.0;
|
||||
for k in 0..pts.len() {
|
||||
let p0 = pts[k];
|
||||
let p1 = pts[(k + 1) % pts.len()];
|
||||
a2 += p0.x * p1.y - p1.x * p0.y;
|
||||
}
|
||||
(a2 * 0.5).abs()
|
||||
};
|
||||
if area < 1e-6 {
|
||||
self.fills[i].deleted = true;
|
||||
self.free_fills.push(i as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure we have two distinct positions
|
||||
if pos_v0 == pos_v1 {
|
||||
return None;
|
||||
/// Directed end vertex of a `(edge, direction)` boundary entry.
|
||||
#[inline]
|
||||
fn entry_end_vertex(&self, eid: EdgeId, dir: Direction) -> VertexId {
|
||||
match dir {
|
||||
Direction::Forward => self.edges[eid.idx()].vertices[1],
|
||||
Direction::Backward => self.edges[eid.idx()].vertices[0],
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-derive the fills touched by a freshly inserted stroke by re-tracing the planar
|
||||
/// faces of the affected sub-arrangement. This replaces incremental "split a fill by a
|
||||
/// cut edge" logic, which can't handle a cut whose path is interrupted by a hole/notch
|
||||
/// in a non-convex fill. Each affected fill is deleted and rebuilt from the traced
|
||||
/// faces that lie inside it (inheriting its colour/rule); faces outside it — or in a
|
||||
/// hole — are dropped.
|
||||
fn retrace_fills_after_cut(&mut self, new_edges: &[EdgeId]) {
|
||||
use kurbo::{ParamCurve, Shape};
|
||||
let new_set: HashSet<EdgeId> = new_edges
|
||||
.iter()
|
||||
.filter(|&&e| !e.is_none() && !self.edges[e.idx()].deleted)
|
||||
.copied()
|
||||
.collect();
|
||||
if new_set.is_empty() {
|
||||
return;
|
||||
}
|
||||
let new_verts: HashSet<VertexId> =
|
||||
new_set.iter().flat_map(|&e| self.edges[e.idx()].vertices).collect();
|
||||
|
||||
// Affected fills: any non-deleted fill that shares a vertex with a new edge.
|
||||
let affected: Vec<FillId> = (0..self.fills.len())
|
||||
.filter(|&i| !self.fills[i].deleted)
|
||||
.filter(|&i| {
|
||||
self.fills[i].boundary.iter().any(|&(e, _)| {
|
||||
!e.is_none()
|
||||
&& self.edges[e.idx()].vertices.iter().any(|v| new_verts.contains(v))
|
||||
})
|
||||
})
|
||||
.map(|i| FillId(i as u32))
|
||||
.collect();
|
||||
if affected.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Walk boundary in two halves:
|
||||
// Half A: from pos_v0+1 to pos_v1 (inclusive), then splitting_edge Forward
|
||||
// Half B: from pos_v1+1 to pos_v0 (wrapping), then splitting_edge Backward
|
||||
let n = boundary.len();
|
||||
let color = fill.color;
|
||||
let fill_rule = fill.fill_rule;
|
||||
// Snapshot each affected fill's path + attributes before we delete them.
|
||||
let originals: Vec<(kurbo::BezPath, Option<ShapeColor>, FillRule)> = affected
|
||||
.iter()
|
||||
.map(|&f| {
|
||||
(
|
||||
self.fill_to_bezpath(f),
|
||||
self.fills[f.idx()].color,
|
||||
self.fills[f.idx()].fill_rule,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut half_a = Vec::new();
|
||||
let mut idx = (pos_v0 + 1) % n;
|
||||
// Edge set for the local arrangement: every affected fill's boundary edges plus the
|
||||
// ENTIRE inserted stroke. We include the whole stroke (not just the segments whose
|
||||
// midpoint is inside a fill) because a wiggly freehand lasso has segments that dip
|
||||
// just outside the fill; excluding them would break the inside-arc chain into
|
||||
// dangling fragments that then get pruned away, losing the cut entirely. The stroke
|
||||
// forms closed loops, so it contributes no dangling edges; faces that end up outside
|
||||
// every affected fill are discarded by the classification below.
|
||||
let mut edge_set: HashSet<EdgeId> = HashSet::new();
|
||||
for &f in &affected {
|
||||
for &(e, _) in &self.fills[f.idx()].boundary {
|
||||
if !e.is_none() && !self.edges[e.idx()].deleted {
|
||||
edge_set.insert(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
edge_set.extend(new_set.iter().copied());
|
||||
|
||||
// Expand to the induced subgraph on the covered vertices. A self-intersecting
|
||||
// freehand stroke splits its own edges via `split_edge`, whose sub-edges aren't in
|
||||
// `new_edges`; without them the local arrangement has gaps and the stroke's loop
|
||||
// looks like dangling fragments. Adding every edge whose endpoints are both already
|
||||
// covered closes those gaps (the sub-edges connect already-covered stroke vertices).
|
||||
loop {
|
||||
half_a.push(boundary[idx]);
|
||||
if idx == pos_v1 {
|
||||
let verts: HashSet<VertexId> = edge_set
|
||||
.iter()
|
||||
.flat_map(|&e| self.edges[e.idx()].vertices)
|
||||
.collect();
|
||||
let added: Vec<EdgeId> = (0..self.edges.len())
|
||||
.map(|i| EdgeId(i as u32))
|
||||
.filter(|&e| !self.edges[e.idx()].deleted && !edge_set.contains(&e))
|
||||
.filter(|&e| {
|
||||
let [a, b] = self.edges[e.idx()].vertices;
|
||||
verts.contains(&a) && verts.contains(&b)
|
||||
})
|
||||
.collect();
|
||||
if added.is_empty() {
|
||||
break;
|
||||
}
|
||||
idx = (idx + 1) % n;
|
||||
edge_set.extend(added);
|
||||
}
|
||||
half_a.push((splitting_edge, Direction::Forward));
|
||||
|
||||
let mut half_b = Vec::new();
|
||||
idx = (pos_v1 + 1) % n;
|
||||
// Prune dangling edges (a vertex with degree < 2 in the local arrangement). They
|
||||
// form spikes, never real face boundaries — a freehand lasso that wiggles or nearly
|
||||
// self-touches leaves such stubs, and tracing them produces a face that runs out and
|
||||
// back along the same edge (a degenerate self-touching boundary). Iterate, since
|
||||
// removing one stub can expose another.
|
||||
loop {
|
||||
half_b.push(boundary[idx]);
|
||||
if idx == pos_v0 {
|
||||
let mut degree: HashMap<VertexId, usize> = HashMap::new();
|
||||
for &e in &edge_set {
|
||||
for &v in &self.edges[e.idx()].vertices {
|
||||
*degree.entry(v).or_default() += 1;
|
||||
}
|
||||
}
|
||||
let dangling: Vec<EdgeId> = edge_set
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|&e| {
|
||||
let [a, b] = self.edges[e.idx()].vertices;
|
||||
a == b || degree[&a] < 2 || degree[&b] < 2
|
||||
})
|
||||
.collect();
|
||||
if dangling.is_empty() {
|
||||
break;
|
||||
}
|
||||
idx = (idx + 1) % n;
|
||||
for e in dangling {
|
||||
edge_set.remove(&e);
|
||||
}
|
||||
}
|
||||
half_b.push((splitting_edge, Direction::Backward));
|
||||
|
||||
// Delete the original fill
|
||||
self.fills[fill_id.idx()].deleted = true;
|
||||
self.free_fills.push(fill_id.0);
|
||||
let faces = self.trace_faces(&edge_set);
|
||||
|
||||
// Create two new fills
|
||||
let fill_a = self.alloc_fill(half_a, color, fill_rule);
|
||||
let fill_b = self.alloc_fill(half_b, color, fill_rule);
|
||||
// Replace the affected fills with the re-traced bounded faces that fall inside them.
|
||||
for &f in &affected {
|
||||
self.fills[f.idx()].deleted = true;
|
||||
self.free_fills.push(f.0);
|
||||
}
|
||||
for face in faces {
|
||||
// Only bounded (counter-clockwise, positive-area) faces are real regions; the
|
||||
// outer face is clockwise/negative.
|
||||
if self.face_signed_area(&face) <= 1e-6 {
|
||||
continue;
|
||||
}
|
||||
let sample = self.boundary_interior_point(&face);
|
||||
if let Some((_, color, rule)) =
|
||||
originals.iter().find(|(p, _, _)| p.winding(sample) != 0)
|
||||
{
|
||||
self.alloc_fill(face, *color, *rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some((fill_a, fill_b))
|
||||
/// Trace all faces of the planar arrangement formed by `edge_set`, using the standard
|
||||
/// angular next-edge rule (turn to the clockwise-adjacent dart of the twin at each
|
||||
/// vertex). Returns each face as an ordered `(edge, direction)` loop. Bounded faces
|
||||
/// come out counter-clockwise (positive signed area); the outer face clockwise.
|
||||
fn trace_faces(&self, edge_set: &HashSet<EdgeId>) -> Vec<Vec<(EdgeId, Direction)>> {
|
||||
// Outgoing darts per vertex, sorted by outgoing angle (CCW).
|
||||
let mut out: HashMap<VertexId, Vec<(f64, (EdgeId, Direction))>> = HashMap::new();
|
||||
for &e in edge_set {
|
||||
if self.edges[e.idx()].deleted {
|
||||
continue;
|
||||
}
|
||||
let [a, b] = self.edges[e.idx()].vertices;
|
||||
out.entry(a)
|
||||
.or_default()
|
||||
.push((self.dart_angle(e, Direction::Forward), (e, Direction::Forward)));
|
||||
out.entry(b)
|
||||
.or_default()
|
||||
.push((self.dart_angle(e, Direction::Backward), (e, Direction::Backward)));
|
||||
}
|
||||
for darts in out.values_mut() {
|
||||
darts.sort_by(|x, y| x.0.partial_cmp(&y.0).unwrap_or(std::cmp::Ordering::Equal));
|
||||
}
|
||||
|
||||
let mut visited: HashSet<(EdgeId, Direction)> = HashSet::new();
|
||||
let mut faces: Vec<Vec<(EdgeId, Direction)>> = Vec::new();
|
||||
let cap = edge_set.len() * 2 + 4;
|
||||
for &e in edge_set {
|
||||
if self.edges[e.idx()].deleted {
|
||||
continue;
|
||||
}
|
||||
for dir in [Direction::Forward, Direction::Backward] {
|
||||
let start = (e, dir);
|
||||
if visited.contains(&start) {
|
||||
continue;
|
||||
}
|
||||
let mut face: Vec<(EdgeId, Direction)> = Vec::new();
|
||||
let mut d = start;
|
||||
loop {
|
||||
visited.insert(d);
|
||||
face.push(d);
|
||||
// Next dart: at the end vertex, the dart clockwise-adjacent to the twin.
|
||||
let end_v = self.entry_end_vertex(d.0, d.1);
|
||||
let twin = (
|
||||
d.0,
|
||||
match d.1 {
|
||||
Direction::Forward => Direction::Backward,
|
||||
Direction::Backward => Direction::Forward,
|
||||
},
|
||||
);
|
||||
let darts = match out.get(&end_v) {
|
||||
Some(d) => d,
|
||||
None => break,
|
||||
};
|
||||
let Some(idx) = darts.iter().position(|&(_, dd)| dd == twin) else {
|
||||
break;
|
||||
};
|
||||
let next = darts[(idx + darts.len() - 1) % darts.len()].1;
|
||||
if next == start {
|
||||
break;
|
||||
}
|
||||
if visited.contains(&next) || face.len() > cap {
|
||||
break;
|
||||
}
|
||||
d = next;
|
||||
}
|
||||
if face.len() >= 3 {
|
||||
faces.push(face);
|
||||
}
|
||||
}
|
||||
}
|
||||
faces
|
||||
}
|
||||
|
||||
/// Outgoing direction angle of a dart at its start vertex.
|
||||
fn dart_angle(&self, e: EdgeId, dir: Direction) -> f64 {
|
||||
let c = self.edges[e.idx()].curve;
|
||||
let (base, cands) = match dir {
|
||||
Direction::Forward => (c.p0, [c.p1, c.p2, c.p3]),
|
||||
Direction::Backward => (c.p3, [c.p2, c.p1, c.p0]),
|
||||
};
|
||||
for cand in cands {
|
||||
let d = cand - base;
|
||||
if d.hypot() > 1e-9 {
|
||||
return d.y.atan2(d.x);
|
||||
}
|
||||
}
|
||||
0.0
|
||||
}
|
||||
|
||||
/// Signed area of a face given as an ordered `(edge, direction)` loop (CCW positive).
|
||||
fn face_signed_area(&self, face: &[(EdgeId, Direction)]) -> f64 {
|
||||
let pts: Vec<Point> = face
|
||||
.iter()
|
||||
.map(|&(e, dir)| {
|
||||
let c = self.edges[e.idx()].curve;
|
||||
match dir {
|
||||
Direction::Forward => c.p0,
|
||||
Direction::Backward => c.p3,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
if pts.len() < 3 {
|
||||
return 0.0;
|
||||
}
|
||||
let mut a2 = 0.0;
|
||||
for i in 0..pts.len() {
|
||||
let p0 = pts[i];
|
||||
let p1 = pts[(i + 1) % pts.len()];
|
||||
a2 += p0.x * p1.y - p1.x * p0.y;
|
||||
}
|
||||
a2 * 0.5
|
||||
}
|
||||
|
||||
/// Merge two fills that share a boundary edge (e.g., after edge deletion).
|
||||
@@ -1479,6 +1908,32 @@ impl VectorGraph {
|
||||
let mut vtx_map: HashMap<VertexId, VertexId> = HashMap::new();
|
||||
let mut edge_map: HashMap<EdgeId, EdgeId> = HashMap::new();
|
||||
|
||||
// Augment the inside set with every boundary edge of an extracted fill. A
|
||||
// selection might not enumerate them all (e.g. lasso/region selection populates
|
||||
// edges differently than `select_fill`); without this an extracted fill would be
|
||||
// copied with `EdgeId::NONE` standing in for a missing edge, and that NONE later
|
||||
// panics any code that indexes `fill.boundary` (e.g. `insert_stroke`).
|
||||
let inside_edges: HashSet<EdgeId> = {
|
||||
let mut s = inside_edges.clone();
|
||||
for &fid in inside_fills {
|
||||
if fid.is_none() {
|
||||
continue;
|
||||
}
|
||||
if let Some(fill) = self.fills.get(fid.idx()) {
|
||||
if fill.deleted {
|
||||
continue;
|
||||
}
|
||||
for &(eid, _) in &fill.boundary {
|
||||
if !eid.is_none() {
|
||||
s.insert(eid);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
s
|
||||
};
|
||||
let inside_edges = &inside_edges;
|
||||
|
||||
// Boundary = `explicit_boundary` (e.g. a region/lasso cut the caller knows about)
|
||||
// UNION any inside edge still referenced by a fill we're NOT extracting (a shared
|
||||
// DCEL edge — must be duplicated, not moved, or that fill dangles). Deriving the
|
||||
@@ -1511,16 +1966,21 @@ impl VectorGraph {
|
||||
}
|
||||
}
|
||||
|
||||
// Determine which vertices are interior (exclusively owned by the
|
||||
// extracted subgraph) vs boundary (shared with remaining geometry).
|
||||
// A vertex is interior if ALL of its incident edges are either in
|
||||
// inside_edges or boundary_edge_ids.
|
||||
// Determine which vertices are safe to free from `self`. A vertex can only be
|
||||
// freed if EVERY one of its incident edges is actually being removed from `self`,
|
||||
// i.e. is an inside edge that is NOT a boundary edge. Boundary edges are kept
|
||||
// (duplicated) in `self`, so a vertex touching one is still referenced and must
|
||||
// remain — otherwise it becomes a freed-but-referenced vertex whose slot a later
|
||||
// `alloc_vertex` reuses, corrupting the remaining fill.
|
||||
let mut interior_vertices: HashSet<VertexId> = HashSet::new();
|
||||
let mut boundary_vertices: HashSet<VertexId> = HashSet::new();
|
||||
for &vid in &referenced_vids {
|
||||
let incident = self.edges_at_vertex(vid);
|
||||
let all_inside = incident.iter().all(|&eid| edges_to_copy.contains(&eid));
|
||||
if all_inside {
|
||||
let all_removed = !incident.is_empty()
|
||||
&& incident
|
||||
.iter()
|
||||
.all(|&eid| inside_edges.contains(&eid) && !boundary_edge_ids.contains(&eid));
|
||||
if all_removed {
|
||||
interior_vertices.insert(vid);
|
||||
} else {
|
||||
boundary_vertices.insert(vid);
|
||||
|
||||
@@ -10,3 +10,5 @@ mod editing;
|
||||
mod gap_close;
|
||||
#[cfg(test)]
|
||||
mod region;
|
||||
#[cfg(test)]
|
||||
mod region_cut_select;
|
||||
|
||||
@@ -0,0 +1,609 @@
|
||||
//! Reproduces the unified region-select behaviour at the graph level: cutting a shape
|
||||
//! along a lasso outline (`insert_stroke`) and classifying which resulting fills/edges
|
||||
//! fall inside the lasso — the same logic the editor's `execute_region_select` runs.
|
||||
//!
|
||||
//! The shape is built exactly as the editor builds a rectangle: `insert_stroke` of a
|
||||
//! closed rect loop, then `paint_bucket` to create the fill (NOT a hand-rolled
|
||||
//! `alloc_fill`), because the bug is in how that traced fill's boundary survives being
|
||||
//! split by the lasso cut.
|
||||
|
||||
use super::super::*;
|
||||
use kurbo::{BezPath, CubicBez, ParamCurve, Point, Shape};
|
||||
|
||||
/// Straight-line cubic from a to b.
|
||||
fn line(a: Point, b: Point) -> CubicBez {
|
||||
CubicBez::new(
|
||||
a,
|
||||
Point::new(a.x + (b.x - a.x) / 3.0, a.y + (b.y - a.y) / 3.0),
|
||||
Point::new(a.x + 2.0 * (b.x - a.x) / 3.0, a.y + 2.0 * (b.y - a.y) / 3.0),
|
||||
b,
|
||||
)
|
||||
}
|
||||
|
||||
fn closed_rect_path(min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> BezPath {
|
||||
let mut p = BezPath::new();
|
||||
p.move_to((min_x, min_y));
|
||||
p.line_to((max_x, min_y));
|
||||
p.line_to((max_x, max_y));
|
||||
p.line_to((min_x, max_y));
|
||||
p.close_path();
|
||||
p
|
||||
}
|
||||
|
||||
/// Draw a filled rectangle into an existing graph the way the editor's rectangle tool does.
|
||||
fn draw_filled_rect(g: &mut VectorGraph, min_x: f64, min_y: f64, max_x: f64, max_y: f64) {
|
||||
let style = StrokeStyle { width: 1.0, ..Default::default() };
|
||||
let color = ShapeColor::rgb(0, 0, 0);
|
||||
for segs in bezpath_to_cubic_segments(&closed_rect_path(min_x, min_y, max_x, max_y)) {
|
||||
g.insert_stroke(&segs, Some(style.clone()), Some(color), 0.5);
|
||||
}
|
||||
let centroid = Point::new((min_x + max_x) / 2.0, (min_y + max_y) / 2.0);
|
||||
g.paint_bucket(centroid, ShapeColor::rgb(255, 0, 0), FillRule::NonZero, 0.0)
|
||||
.expect("paint_bucket should create a fill");
|
||||
}
|
||||
|
||||
/// Build a filled rectangle the way the editor's rectangle tool does.
|
||||
fn editor_filled_rect(min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> VectorGraph {
|
||||
let mut g = VectorGraph::new();
|
||||
draw_filled_rect(&mut g, min_x, min_y, max_x, max_y);
|
||||
g
|
||||
}
|
||||
|
||||
/// Closed rectangular lasso: cubic segments (for insert_stroke) + BezPath (for winding).
|
||||
fn rect_lasso(min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> (Vec<CubicBez>, BezPath) {
|
||||
let p = [
|
||||
Point::new(min_x, min_y),
|
||||
Point::new(max_x, min_y),
|
||||
Point::new(max_x, max_y),
|
||||
Point::new(min_x, max_y),
|
||||
];
|
||||
let segs = vec![line(p[0], p[1]), line(p[1], p[2]), line(p[2], p[3]), line(p[3], p[0])];
|
||||
let mut path = BezPath::new();
|
||||
path.move_to(p[0]);
|
||||
for pt in &p[1..] {
|
||||
path.line_to(*pt);
|
||||
}
|
||||
path.close_path();
|
||||
(segs, path)
|
||||
}
|
||||
|
||||
// Classify-against-lasso uses the same guaranteed-interior probe point the editor uses.
|
||||
fn fill_centroid(g: &VectorGraph, fid: FillId) -> Point {
|
||||
g.fill_interior_point(fid)
|
||||
}
|
||||
|
||||
/// Directed start/end points of a boundary entry.
|
||||
fn dir_start(g: &VectorGraph, eid: EdgeId, dir: Direction) -> Point {
|
||||
let c = g.edge(eid).curve;
|
||||
match dir {
|
||||
Direction::Forward => c.p0,
|
||||
Direction::Backward => c.p3,
|
||||
}
|
||||
}
|
||||
fn dir_end(g: &VectorGraph, eid: EdgeId, dir: Direction) -> Point {
|
||||
let c = g.edge(eid).curve;
|
||||
match dir {
|
||||
Direction::Forward => c.p3,
|
||||
Direction::Backward => c.p0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Describe a fill's boundary as an ordered list of (start → end) segments for diagnostics.
|
||||
fn describe_boundary(g: &VectorGraph, fid: FillId) -> Vec<(EdgeId, Point, Point)> {
|
||||
g.fill(fid)
|
||||
.boundary
|
||||
.iter()
|
||||
.filter(|(e, _)| !e.is_none())
|
||||
.map(|&(e, d)| (e, dir_start(g, e, d), dir_end(g, e, d)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Assert no fill boundary uses the same edge twice (the signature of a degenerate
|
||||
/// "spike" where the trace runs out and back along a dangling edge).
|
||||
fn assert_no_spikes(g: &VectorGraph) {
|
||||
for (fi, f) in g.fills.iter().enumerate() {
|
||||
if f.deleted { continue; }
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for &(e, _) in &f.boundary {
|
||||
if e.is_none() { continue; }
|
||||
assert!(seen.insert(e.0), "fill {fi} uses edge {} twice (spike)", e.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Assert every non-deleted fill's boundary is a connected closed loop.
|
||||
fn assert_all_fills_connected(g: &VectorGraph) {
|
||||
for (i, f) in g.fills.iter().enumerate() {
|
||||
if f.deleted {
|
||||
continue;
|
||||
}
|
||||
let fid = FillId(i as u32);
|
||||
let b = describe_boundary(g, fid);
|
||||
if b.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let n = b.len();
|
||||
for k in 0..n {
|
||||
let (_, _, end) = b[k];
|
||||
let (_, next_start, _) = b[(k + 1) % n];
|
||||
assert!(
|
||||
(end.x - next_start.x).abs() < 1e-6 && (end.y - next_start.y).abs() < 1e-6,
|
||||
"fill {i} boundary disconnected between edge {k} (ends {end:?}) and \
|
||||
edge {} (starts {next_start:?}); boundary = {b:#?}",
|
||||
(k + 1) % n
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_vertical_cut_produces_two_connected_fills() {
|
||||
// Rectangle (0,0)-(200,100), one vertical cut at x=100.
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 200.0, 100.0);
|
||||
let cut = vec![line(Point::new(100.0, -10.0), Point::new(100.0, 110.0))];
|
||||
g.insert_stroke(&cut, None, None, 1.0);
|
||||
|
||||
let live = g.fills.iter().filter(|f| !f.deleted).count();
|
||||
assert_eq!(live, 2, "one cut should split the rect into 2 fills, got {live}");
|
||||
assert_all_fills_connected(&g);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lasso_over_second_of_two_rects_splits_that_rect() {
|
||||
// Two separate rectangles; lasso a vertical strip over the SECOND one.
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 200.0, 100.0);
|
||||
draw_filled_rect(&mut g, 0.0, 150.0, 200.0, 250.0);
|
||||
assert_eq!(g.fills.iter().filter(|f| !f.deleted).count(), 2, "two rects → two fills");
|
||||
|
||||
let (segs, lasso_path) = rect_lasso(50.0, 120.0, 150.0, 300.0); // crosses rect B only
|
||||
g.insert_stroke(&segs, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
|
||||
// Rect B should now be split into 3 fills (left/middle/right strips); rect A is untouched.
|
||||
// So 1 (rect A) + 3 (rect B pieces) = 4 fills total.
|
||||
let live = g.fills.iter().filter(|f| !f.deleted).count();
|
||||
let inside_fills: Vec<FillId> = g
|
||||
.fills
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, f)| !f.deleted)
|
||||
.map(|(i, _)| FillId(i as u32))
|
||||
.filter(|&fid| lasso_path.winding(fill_centroid(&g, fid)) != 0)
|
||||
.collect();
|
||||
|
||||
let dump = || {
|
||||
g.fills
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, f)| !f.deleted)
|
||||
.map(|(i, _)| (i, fill_centroid(&g, FillId(i as u32))))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
assert_eq!(live, 4, "rect B should split into 3 (total 4 fills); got {live}: {:#?}", dump());
|
||||
assert_eq!(
|
||||
inside_fills.len(),
|
||||
1,
|
||||
"exactly the center strip of rect B should be inside; got {:#?}",
|
||||
dump()
|
||||
);
|
||||
assert_all_fills_connected(&g);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lasso_strip_through_side_by_side_second_rect() {
|
||||
// Two rectangles side by side; lasso a vertical strip through the SECOND (right) one.
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 100.0, 100.0);
|
||||
draw_filled_rect(&mut g, 150.0, 0.0, 250.0, 100.0);
|
||||
|
||||
let (segs, lasso_path) = rect_lasso(180.0, -50.0, 220.0, 150.0);
|
||||
g.insert_stroke(&segs, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
assert_all_fills_connected(&g);
|
||||
|
||||
let inside: Vec<FillId> = g.fills.iter().enumerate()
|
||||
.filter(|(_, f)| !f.deleted).map(|(i, _)| FillId(i as u32))
|
||||
.filter(|&fid| lasso_path.winding(fill_centroid(&g, fid)) != 0).collect();
|
||||
let dump: Vec<_> = g.fills.iter().enumerate().filter(|(_, f)| !f.deleted)
|
||||
.map(|(i, _)| (i, fill_centroid(&g, FillId(i as u32)), describe_boundary(&g, FillId(i as u32)).len())).collect();
|
||||
assert_eq!(inside.len(), 1, "one strip of the right rect should be inside; got {dump:#?}");
|
||||
}
|
||||
|
||||
/// No live fill may reference a freed edge or vertex (whose slot a later alloc reuses).
|
||||
fn assert_no_freed_but_referenced(g: &VectorGraph) {
|
||||
let freed_v: std::collections::HashSet<u32> = g.free_vertices.iter().copied().collect();
|
||||
let freed_e: std::collections::HashSet<u32> = g.free_edges.iter().copied().collect();
|
||||
for (fi, f) in g.fills.iter().enumerate() {
|
||||
if f.deleted { continue; }
|
||||
for &(e, _) in &f.boundary {
|
||||
if e.is_none() { continue; }
|
||||
assert!(!freed_e.contains(&e.0), "live fill {fi} references freed edge {}", e.0);
|
||||
for &v in &g.edge(e).vertices {
|
||||
assert!(!freed_v.contains(&v.0), "live fill {fi} references freed vertex {}", v.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_after_cut_keeps_remaining_fill_intact() {
|
||||
// Cut a rectangle's corner with a lasso, then extract (Group) the clipped corner. The
|
||||
// extraction must not free vertices/edges still referenced by the L-shaped remainder —
|
||||
// previously it freed the shared cut vertices, and a later `alloc_vertex` reused those
|
||||
// slots and corrupted the remainder (the "second lasso deletes all faces" bug).
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 200.0, 100.0);
|
||||
let (segs, lasso) = rect_lasso(100.0, -50.0, 300.0, 50.0); // clips the top-right corner
|
||||
g.insert_stroke(&segs, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
|
||||
// Pick the fill inside the lasso (the clipped corner) and extract it.
|
||||
let inside: Vec<FillId> = g.fills.iter().enumerate()
|
||||
.filter(|(_, f)| !f.deleted).map(|(i, _)| FillId(i as u32))
|
||||
.filter(|&fid| lasso.winding(g.fill_interior_point(fid)) != 0).collect();
|
||||
assert_eq!(inside.len(), 1, "one corner fill should be inside the lasso");
|
||||
let inside_fills: HashSet<FillId> = inside.iter().copied().collect();
|
||||
let inside_edges: HashSet<EdgeId> = g.fill(inside[0]).boundary.iter()
|
||||
.filter_map(|&(e, _)| (!e.is_none()).then_some(e)).collect();
|
||||
|
||||
let _ = g.extract_subgraph(&inside_edges, &inside_fills, &HashSet::new());
|
||||
|
||||
assert_no_freed_but_referenced(&g);
|
||||
assert_all_fills_connected(&g);
|
||||
|
||||
// Simulate the next lasso allocating vertices, then re-validate: a corrupted (freed but
|
||||
// referenced) vertex would now be at a bogus position.
|
||||
let (segs2, _) = rect_lasso(20.0, 20.0, 60.0, 80.0);
|
||||
g.insert_stroke(&segs2, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
assert_all_fills_connected(&g);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn second_stroke_crossing_first_splits_into_quadrants() {
|
||||
// A later stroke that crosses an earlier stroke's edge inside a fill triggers an
|
||||
// edge-domain `split_edge`, whose sub-edges aren't tracked in the second stroke's new
|
||||
// edges. The retrace must still see them (induced-subgraph expansion) or the cut breaks
|
||||
// and unravels. Two crossing cuts through a rectangle must yield four connected quadrants.
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 100.0, 100.0);
|
||||
g.insert_stroke(&[line(Point::new(-10.0, 50.0), Point::new(110.0, 50.0))], None, None, 1.0); // horizontal
|
||||
assert_eq!(g.fills.iter().filter(|f| !f.deleted).count(), 2, "horizontal cut → 2");
|
||||
g.insert_stroke(&[line(Point::new(50.0, -10.0), Point::new(50.0, 110.0))], None, None, 1.0); // vertical, crosses it
|
||||
g.gc_invisible_edges();
|
||||
|
||||
let live: Vec<FillId> = g.fills.iter().enumerate()
|
||||
.filter(|(_, f)| !f.deleted).map(|(i, _)| FillId(i as u32)).collect();
|
||||
assert_eq!(live.len(), 4, "two crossing cuts → four quadrants; got {}", live.len());
|
||||
assert_all_fills_connected(&g);
|
||||
assert_no_spikes(&g);
|
||||
for &fid in &live {
|
||||
assert!((polygon_area(&g, fid) - 2500.0).abs() < 1.0, "each quadrant is 50x50, got {}", polygon_area(&g, fid));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stroke_dead_ending_inside_fill_does_not_spike() {
|
||||
// A stroke (e.g. a freehand lasso that wiggles or nearly self-touches) can leave a
|
||||
// dangling stub: an edge whose inner endpoint has degree 1. Re-tracing must not run out
|
||||
// and back along it (a self-touching "spike" boundary); the fill stays a clean loop.
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 200.0, 100.0);
|
||||
// Open stroke entering the top edge and dead-ending inside at (100,50).
|
||||
let stub = vec![line(Point::new(50.0, -20.0), Point::new(100.0, 50.0))];
|
||||
g.insert_stroke(&stub, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
|
||||
let live: Vec<FillId> = g.fills.iter().enumerate()
|
||||
.filter(|(_, f)| !f.deleted).map(|(i, _)| FillId(i as u32)).collect();
|
||||
assert_eq!(live.len(), 1, "a dead-end stub must not split the fill; got {}", live.len());
|
||||
assert_all_fills_connected(&g);
|
||||
assert_no_spikes(&g);
|
||||
// The fill is still the full rectangle.
|
||||
assert!((polygon_area(&g, live[0]) - 20000.0).abs() < 1.0,
|
||||
"fill should remain the 200x100 rectangle, area {}", polygon_area(&g, live[0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn groupfail_two_notched_fills_second_lasso() {
|
||||
// Faithful reconstruction of the captured post-group state (dump_1): two adjacent
|
||||
// fills sharing the x=337 column, each with an invisible notch (the hole left by the
|
||||
// first lasso+group). A second lasso (398,249)-(495,327) clips fill 3's corner.
|
||||
use std::collections::HashMap;
|
||||
let mut g = VectorGraph::new();
|
||||
let vp = [
|
||||
(130.0, 232.0), (337.0, 232.0), (337.0, 362.0), (130.0, 362.0), (337.0, 297.0),
|
||||
(535.0, 297.0), (535.0, 417.0), (337.0, 417.0), (0.0, 0.0), (337.0, 315.0),
|
||||
(220.0, 315.0), (456.0, 315.0), (456.0, 345.0), (337.0, 345.0), (220.0, 345.0),
|
||||
];
|
||||
let vs: Vec<_> = vp.iter().map(|&(x, y)| g.alloc_vertex(Point::new(x, y))).collect();
|
||||
let style = StrokeStyle { width: 3.0, ..Default::default() };
|
||||
let color = ShapeColor::rgb(0, 0, 0);
|
||||
// (name, v_start, v_end, visible)
|
||||
let edge_defs: &[(&str, usize, usize, bool)] = &[
|
||||
("e0", 0, 1, true), ("e1", 4, 5, true), ("e2", 2, 3, true), ("e3", 3, 0, true),
|
||||
("e4", 1, 4, true), ("e5", 7, 2, true), ("e6", 5, 6, true), ("e7", 6, 7, true),
|
||||
("e8", 10, 9, false), ("e9", 12, 13, false), ("e11", 4, 9, true), ("e12", 9, 11, false),
|
||||
("e13", 11, 12, false), ("e15", 13, 2, true), ("e16", 13, 14, false), ("e17", 14, 10, false),
|
||||
];
|
||||
let mut em: HashMap<&str, EdgeId> = HashMap::new();
|
||||
for &(name, a, b, vis) in edge_defs {
|
||||
let (ss, sc) = if vis { (Some(style.clone()), Some(color)) } else { (None, None) };
|
||||
let e = g.alloc_edge(line(vp[a].into(), vp[b].into()), vs[a], vs[b], ss, sc);
|
||||
em.insert(name, e);
|
||||
}
|
||||
let mk = |spec: &[(&str, Direction)]| -> Vec<(EdgeId, Direction)> {
|
||||
spec.iter().map(|&(n, d)| (em[n], d)).collect()
|
||||
};
|
||||
use Direction::{Backward as B, Forward as F};
|
||||
g.alloc_fill(mk(&[("e11", B), ("e4", B), ("e0", B), ("e3", B), ("e2", B), ("e15", B), ("e16", F), ("e17", F), ("e8", F)]),
|
||||
ShapeColor::rgb(100, 100, 255), FillRule::NonZero);
|
||||
g.alloc_fill(mk(&[("e15", F), ("e5", B), ("e7", B), ("e6", B), ("e1", B), ("e11", F), ("e12", F), ("e13", F), ("e9", F)]),
|
||||
ShapeColor::rgb(100, 100, 255), FillRule::NonZero);
|
||||
|
||||
assert_eq!(g.fills.iter().filter(|f| !f.deleted).count(), 2, "starts with 2 fills");
|
||||
let (segs, lasso) = rect_lasso(398.0, 249.0, 495.0, 327.0);
|
||||
g.insert_stroke(&segs, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
|
||||
let live = g.fills.iter().filter(|f| !f.deleted).count();
|
||||
let inside: Vec<FillId> = g.fills.iter().enumerate()
|
||||
.filter(|(_, f)| !f.deleted).map(|(i, _)| FillId(i as u32))
|
||||
.filter(|&fid| lasso.winding(g.fill_interior_point(fid)) != 0).collect();
|
||||
eprintln!("groupfail: {live} live fills, {} inside lasso", inside.len());
|
||||
for (i, f) in g.fills.iter().enumerate() {
|
||||
if f.deleted { continue; }
|
||||
let fid = FillId(i as u32);
|
||||
eprintln!(" fill {i}: bbox {:?} area {:.0}", fill_bbox(&g, fid), polygon_area(&g, fid));
|
||||
}
|
||||
assert_all_fills_connected(&g);
|
||||
// The two original shapes must survive (no faces wrongly deleted); the lasso adds a cut.
|
||||
assert!(live >= 2, "must keep both shapes plus the cut; got {live}");
|
||||
assert_eq!(inside.len(), 1, "exactly the clipped corner of fill 3 should be selected");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lasso_across_notched_fill_never_corrupts_boundaries() {
|
||||
// A rectangle with a rectangular notch bitten out of its top edge — the shape a fill
|
||||
// is left as after grouping a lasso selection (the notch is the extracted region's
|
||||
// hole). A second lasso crossing the notch must never produce disconnected/corrupt
|
||||
// fill boundaries (it previously incorporated the lasso's out-of-shape edges, drawing
|
||||
// stray diagonals). It may under-select on such complex geometry, but must stay valid.
|
||||
let mut g = VectorGraph::new();
|
||||
let pts = [
|
||||
Point::new(0.0, 0.0), Point::new(80.0, 0.0), Point::new(80.0, 40.0),
|
||||
Point::new(120.0, 40.0), Point::new(120.0, 0.0), Point::new(200.0, 0.0),
|
||||
Point::new(200.0, 100.0), Point::new(0.0, 100.0),
|
||||
];
|
||||
let style = StrokeStyle { width: 1.0, ..Default::default() };
|
||||
let color = ShapeColor::rgb(0, 0, 0);
|
||||
let v: Vec<_> = pts.iter().map(|&p| g.alloc_vertex(p)).collect();
|
||||
let mut boundary = Vec::new();
|
||||
for i in 0..pts.len() {
|
||||
let e = g.alloc_edge(line(pts[i], pts[(i + 1) % pts.len()]), v[i], v[(i + 1) % pts.len()],
|
||||
Some(style.clone()), Some(color));
|
||||
boundary.push((e, Direction::Forward));
|
||||
}
|
||||
g.alloc_fill(boundary, ShapeColor::rgb(255, 0, 0), FillRule::NonZero);
|
||||
|
||||
// Lasso a rectangle that straddles the notch and the shape body. Inside the fill it
|
||||
// covers (60..140, 0..60) minus the notch (80..120, 0..40) — an arch shape — which the
|
||||
// cut must isolate as one connected fill, with the remainder outside.
|
||||
let (segs, lasso) = rect_lasso(60.0, -20.0, 140.0, 60.0);
|
||||
g.insert_stroke(&segs, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
|
||||
// Every resulting fill is a valid connected loop (never corrupt).
|
||||
assert_all_fills_connected(&g);
|
||||
|
||||
// Exactly one fill lies inside the lasso, and it is the arch (area = 80*60 - 40*40).
|
||||
let inside: Vec<FillId> = g.fills.iter().enumerate()
|
||||
.filter(|(_, f)| !f.deleted).map(|(i, _)| FillId(i as u32))
|
||||
.filter(|&fid| lasso.winding(g.fill_interior_point(fid)) != 0).collect();
|
||||
assert_eq!(inside.len(), 1, "the arch (lasso ∩ notched fill) should be one selected fill");
|
||||
let area = polygon_area(&g, inside[0]);
|
||||
assert!((area - 3200.0).abs() < 1.0, "arch area should be 3200, got {area}");
|
||||
}
|
||||
|
||||
/// Absolute polygon area of a fill from its boundary corner points.
|
||||
fn polygon_area(g: &VectorGraph, fid: FillId) -> f64 {
|
||||
let pts: Vec<Point> = g.fill(fid).boundary.iter()
|
||||
.filter(|(e, _)| !e.is_none())
|
||||
.map(|&(e, d)| dir_start(g, e, d))
|
||||
.collect();
|
||||
if pts.len() < 3 { return 0.0; }
|
||||
let mut a2 = 0.0;
|
||||
for i in 0..pts.len() {
|
||||
let p0 = pts[i]; let p1 = pts[(i + 1) % pts.len()];
|
||||
a2 += p0.x * p1.y - p1.x * p0.y;
|
||||
}
|
||||
(a2 * 0.5).abs()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_edge_adjacent_rects_make_two_clean_fills() {
|
||||
// Two rectangles sharing the x=100 edge. The second's left edge lands exactly on the
|
||||
// first's right edge; without coincident-edge cleanup this produced duplicate edges and
|
||||
// zero-area "sliver" fills (4 fills instead of 2) before any lasso.
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 100.0, 100.0);
|
||||
draw_filled_rect(&mut g, 100.0, 0.0, 200.0, 100.0);
|
||||
let live: Vec<_> = g.fills.iter().enumerate().filter(|(_, f)| !f.deleted)
|
||||
.map(|(i, _)| (i, fill_bbox(&g, FillId(i as u32)))).collect();
|
||||
assert_eq!(live.len(), 2, "two adjacent rects should make exactly two fills; got {live:?}");
|
||||
assert_all_fills_connected(&g);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lasso_strip_through_adjacent_second_rect() {
|
||||
// Two rectangles sharing an edge (snapped adjacent), lasso a strip through the second.
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 100.0, 100.0);
|
||||
draw_filled_rect(&mut g, 100.0, 0.0, 200.0, 100.0); // shares the x=100 edge with the first
|
||||
|
||||
let (segs, lasso_path) = rect_lasso(130.0, -50.0, 170.0, 150.0); // strip through 2nd
|
||||
g.insert_stroke(&segs, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
assert_all_fills_connected(&g);
|
||||
|
||||
let inside: Vec<FillId> = g.fills.iter().enumerate()
|
||||
.filter(|(_, f)| !f.deleted).map(|(i, _)| FillId(i as u32))
|
||||
.filter(|&fid| lasso_path.winding(fill_centroid(&g, fid)) != 0).collect();
|
||||
let dump: Vec<_> = g.fills.iter().enumerate().filter(|(_, f)| !f.deleted)
|
||||
.map(|(i, _)| (i, fill_centroid(&g, FillId(i as u32)), describe_boundary(&g, FillId(i as u32)).len())).collect();
|
||||
assert_eq!(inside.len(), 1, "one strip of the 2nd rect should be inside; got {dump:#?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lasso_strip_through_overlapping_second_rect() {
|
||||
// Second rectangle overlaps the first; lasso a strip through the second's free part.
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 100.0, 100.0);
|
||||
draw_filled_rect(&mut g, 60.0, 60.0, 200.0, 160.0);
|
||||
|
||||
let (segs, _lasso) = rect_lasso(120.0, 40.0, 160.0, 180.0);
|
||||
g.insert_stroke(&segs, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
assert_all_fills_connected(&g);
|
||||
}
|
||||
|
||||
/// Bounding box of a fill's boundary edge endpoints.
|
||||
fn fill_bbox(g: &VectorGraph, fid: FillId) -> (f64, f64, f64, f64) {
|
||||
let (mut minx, mut miny, mut maxx, mut maxy) = (f64::MAX, f64::MAX, f64::MIN, f64::MIN);
|
||||
for &(eid, _) in &g.fill(fid).boundary {
|
||||
if eid.is_none() {
|
||||
continue;
|
||||
}
|
||||
for &vid in &g.edge(eid).vertices {
|
||||
let p = g.vertex(vid).position;
|
||||
minx = minx.min(p.x);
|
||||
miny = miny.min(p.y);
|
||||
maxx = maxx.max(p.x);
|
||||
maxy = maxy.max(p.y);
|
||||
}
|
||||
}
|
||||
(minx, miny, maxx, maxy)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lasso_corner_clip_of_second_rect_splits_off_corner() {
|
||||
// Captured repro (/tmp dump): two separate rects; the lasso clips the top-left CORNER
|
||||
// of the second rect, so the cut path turns a corner at a vertex INTERIOR to the rect.
|
||||
let mut g = editor_filled_rect(128.37, 255.97, 290.91, 336.55); // rect 1
|
||||
draw_filled_rect(&mut g, 417.39, 311.62, 620.25, 442.55); // rect 2
|
||||
let (segs, lasso) = rect_lasso(360.12, 277.92, 537.32, 397.14); // clips rect-2 corner
|
||||
g.insert_stroke(&segs, None, None, 1.0);
|
||||
g.gc_invisible_edges();
|
||||
assert_all_fills_connected(&g);
|
||||
|
||||
let inside: Vec<FillId> = g.fills.iter().enumerate()
|
||||
.filter(|(_, f)| !f.deleted).map(|(i, _)| FillId(i as u32))
|
||||
.filter(|&fid| lasso.winding(fill_centroid(&g, fid)) != 0).collect();
|
||||
let dump: Vec<_> = g.fills.iter().enumerate().filter(|(_, f)| !f.deleted)
|
||||
.map(|(i, _)| (i, fill_centroid(&g, FillId(i as u32)), fill_bbox(&g, FillId(i as u32)), describe_boundary(&g, FillId(i as u32)).len())).collect();
|
||||
assert_eq!(inside.len(), 1, "exactly the clipped corner should be inside; fills = {dump:#?}");
|
||||
|
||||
// The selected fill must be the clipped CORNER, not the whole rect 2.
|
||||
let (minx, miny, maxx, maxy) = fill_bbox(&g, inside[0]);
|
||||
let eps = 1.0;
|
||||
assert!(
|
||||
(minx - 417.39).abs() < eps && (miny - 311.62).abs() < eps
|
||||
&& (maxx - 537.32).abs() < eps && (maxy - 397.14).abs() < eps,
|
||||
"expected clipped-corner bbox (417.4,311.6)-(537.3,397.1), got ({minx:.1},{miny:.1})-({maxx:.1},{maxy:.1}) \
|
||||
— whole rect2 is (417,311)-(620,442); the fill wasn't split"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lasso_over_middle_of_rect_selects_clean_center_strip() {
|
||||
// Rectangle (0,0)-(200,100); lasso a vertical strip (50,-50)-(150,150).
|
||||
let mut g = editor_filled_rect(0.0, 0.0, 200.0, 100.0);
|
||||
let (segs, lasso_path) = rect_lasso(50.0, -50.0, 150.0, 150.0);
|
||||
|
||||
g.insert_stroke(&segs, None, None, 1.0);
|
||||
// The editor runs this right after the cut: the lasso's top/bottom edges and the
|
||||
// out-of-shape extensions of its sides are invisible and unreferenced — they must be
|
||||
// collected so they can't be selected/edited later.
|
||||
g.gc_invisible_edges();
|
||||
for (i, e) in g.edges.iter().enumerate() {
|
||||
if e.deleted || e.stroke_style.is_some() || e.stroke_color.is_some() {
|
||||
continue;
|
||||
}
|
||||
let eid = EdgeId(i as u32);
|
||||
let in_a_fill = g
|
||||
.fills
|
||||
.iter()
|
||||
.any(|f| !f.deleted && f.boundary.iter().any(|&(fe, _)| fe == eid));
|
||||
assert!(
|
||||
in_a_fill,
|
||||
"stray invisible edge {i} ({:?}) survived gc — not part of any fill",
|
||||
e.curve
|
||||
);
|
||||
}
|
||||
|
||||
// Classify fills by centroid winding (the editor's logic).
|
||||
let inside_fills: Vec<FillId> = g
|
||||
.fills
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, f)| !f.deleted)
|
||||
.map(|(i, _)| FillId(i as u32))
|
||||
.filter(|&fid| lasso_path.winding(fill_centroid(&g, fid)) != 0)
|
||||
.collect();
|
||||
|
||||
let dump = || {
|
||||
g.fills
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, f)| !f.deleted)
|
||||
.map(|(i, _)| {
|
||||
let fid = FillId(i as u32);
|
||||
(i, fill_centroid(&g, fid), describe_boundary(&g, fid))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
inside_fills.len(),
|
||||
1,
|
||||
"expected exactly one inside fill (the center strip); live fills = {:#?}",
|
||||
dump()
|
||||
);
|
||||
|
||||
let fid = inside_fills[0];
|
||||
let boundary = describe_boundary(&g, fid);
|
||||
|
||||
// The center strip is a rectangle: it must have 4 boundary edges that form a
|
||||
// *connected closed loop* (each edge's directed end == the next edge's directed start).
|
||||
// Before the fix, the split produced a 2-edge boundary (left cut + top segment) whose
|
||||
// bbox is still (50,0)-(150,100) but which `fill_to_bezpath` renders as left+top edges
|
||||
// plus a diagonal close — the artifact the user reported.
|
||||
assert_eq!(
|
||||
boundary.len(),
|
||||
4,
|
||||
"center strip should have 4 boundary edges, got {}: {:#?}",
|
||||
boundary.len(),
|
||||
boundary
|
||||
);
|
||||
|
||||
let eps = 1e-6;
|
||||
let n = boundary.len();
|
||||
for i in 0..n {
|
||||
let (_, _, end) = boundary[i];
|
||||
let (_, next_start, _) = boundary[(i + 1) % n];
|
||||
assert!(
|
||||
(end.x - next_start.x).abs() < eps && (end.y - next_start.y).abs() < eps,
|
||||
"boundary is disconnected between edge {i} (ends {end:?}) and edge {} \
|
||||
(starts {next_start:?}); full boundary = {boundary:#?}",
|
||||
(i + 1) % n
|
||||
);
|
||||
}
|
||||
|
||||
// And the loop should visit the four expected corners.
|
||||
let corners = [
|
||||
Point::new(50.0, 0.0),
|
||||
Point::new(150.0, 0.0),
|
||||
Point::new(150.0, 100.0),
|
||||
Point::new(50.0, 100.0),
|
||||
];
|
||||
for c in corners {
|
||||
let hit = boundary
|
||||
.iter()
|
||||
.any(|&(_, s, _)| (s.x - c.x).abs() < 0.5 && (s.y - c.y).abs() < 0.5);
|
||||
assert!(hit, "center strip boundary should pass through corner {c:?}: {boundary:#?}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user