work on vector graph
This commit is contained in:
@@ -15,6 +15,7 @@ pub mod tests;
|
||||
|
||||
use kurbo::{CubicBez, ParamCurve, Point};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fmt;
|
||||
|
||||
use crate::curve_intersections::find_curve_intersections;
|
||||
@@ -95,15 +96,19 @@ pub struct Edge {
|
||||
pub deleted: bool,
|
||||
}
|
||||
|
||||
/// A fill: an explicit boundary referencing edges, with a color.
|
||||
/// A fill: an explicit boundary referencing edges, with visual properties.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Fill {
|
||||
/// Ordered cycle of directed edge references forming the boundary.
|
||||
/// `EdgeId::NONE` entries act as separators between outer contour and hole contours.
|
||||
pub boundary: Vec<(EdgeId, Direction)>,
|
||||
pub color: ShapeColor,
|
||||
pub color: Option<ShapeColor>,
|
||||
pub fill_rule: FillRule,
|
||||
#[serde(default)]
|
||||
pub gradient_fill: Option<crate::gradient::ShapeGradient>,
|
||||
#[serde(default)]
|
||||
pub image_fill: Option<uuid::Uuid>,
|
||||
pub deleted: bool,
|
||||
// TODO: gradient_fill, image_fill
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -198,13 +203,15 @@ impl VectorGraph {
|
||||
pub fn alloc_fill(
|
||||
&mut self,
|
||||
boundary: Vec<(EdgeId, Direction)>,
|
||||
color: ShapeColor,
|
||||
color: impl Into<Option<ShapeColor>>,
|
||||
fill_rule: FillRule,
|
||||
) -> FillId {
|
||||
let fill = Fill {
|
||||
boundary,
|
||||
color,
|
||||
color: color.into(),
|
||||
fill_rule,
|
||||
gradient_fill: None,
|
||||
image_fill: None,
|
||||
deleted: false,
|
||||
};
|
||||
if let Some(idx) = self.free_fills.pop() {
|
||||
@@ -344,6 +351,61 @@ impl VectorGraph {
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Fill / hit-test queries
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
/// Find a fill whose boundary encloses the given point.
|
||||
/// Returns the smallest (by area) enclosing fill.
|
||||
pub fn find_fill_at_point(&self, point: Point) -> Option<FillId> {
|
||||
let mut best: Option<(FillId, f64)> = None;
|
||||
for (i, fill) in self.fills.iter().enumerate() {
|
||||
if fill.deleted {
|
||||
continue;
|
||||
}
|
||||
let fid = FillId(i as u32);
|
||||
let path = self.fill_to_bezpath(fid);
|
||||
if kurbo::Shape::winding(&path, point) != 0 {
|
||||
let area = kurbo::Shape::area(&path).abs();
|
||||
if best.is_none() || area < best.unwrap().1 {
|
||||
best = Some((fid, area));
|
||||
}
|
||||
}
|
||||
}
|
||||
best.map(|(fid, _)| fid)
|
||||
}
|
||||
|
||||
/// Get the distinct edge IDs from a fill's boundary (skipping NONE separators).
|
||||
pub fn fill_boundary_edges(&self, fill_id: FillId) -> Vec<EdgeId> {
|
||||
let fill = &self.fills[fill_id.idx()];
|
||||
let mut edges = Vec::new();
|
||||
for &(eid, _) in &fill.boundary {
|
||||
if !eid.is_none() && !edges.contains(&eid) {
|
||||
edges.push(eid);
|
||||
}
|
||||
}
|
||||
edges
|
||||
}
|
||||
|
||||
/// Get the distinct vertex IDs from a fill's boundary edges.
|
||||
pub fn fill_boundary_vertices(&self, fill_id: FillId) -> Vec<VertexId> {
|
||||
let mut verts = Vec::new();
|
||||
for eid in self.fill_boundary_edges(fill_id) {
|
||||
let e = &self.edges[eid.idx()];
|
||||
for &vid in &e.vertices {
|
||||
if !verts.contains(&vid) {
|
||||
verts.push(vid);
|
||||
}
|
||||
}
|
||||
}
|
||||
verts
|
||||
}
|
||||
|
||||
/// Alias for `delete_edge_by_user` — removes an edge, handling fill merging/invisibility.
|
||||
pub fn remove_edge(&mut self, id: EdgeId) {
|
||||
self.delete_edge_by_user(id);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Fill boundary → BezPath (for rendering)
|
||||
// -------------------------------------------------------------------
|
||||
@@ -1393,6 +1455,223 @@ impl VectorGraph {
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
// ── Region selection: extract / merge subgraph ──────────────────────
|
||||
|
||||
/// Extract a subgraph containing `inside_edges` and `inside_fills`.
|
||||
///
|
||||
/// Boundary edges (`boundary_edge_ids`) are **duplicated** — they exist in
|
||||
/// both the returned graph and `self`, so both sides have closed fill
|
||||
/// boundaries when the selection is moved.
|
||||
///
|
||||
/// Returns `(new_graph, vertex_map, edge_map)` where the maps go from
|
||||
/// old (self) IDs to new (returned graph) IDs.
|
||||
pub fn extract_subgraph(
|
||||
&mut self,
|
||||
inside_edges: &HashSet<EdgeId>,
|
||||
inside_fills: &HashSet<FillId>,
|
||||
boundary_edge_ids: &HashSet<EdgeId>,
|
||||
) -> (VectorGraph, HashMap<VertexId, VertexId>, HashMap<EdgeId, EdgeId>) {
|
||||
let mut new_graph = VectorGraph::new();
|
||||
let mut vtx_map: HashMap<VertexId, VertexId> = HashMap::new();
|
||||
let mut edge_map: HashMap<EdgeId, EdgeId> = HashMap::new();
|
||||
|
||||
// Collect all edge IDs we need to copy into the new graph
|
||||
let edges_to_copy: HashSet<EdgeId> = inside_edges
|
||||
.union(boundary_edge_ids)
|
||||
.copied()
|
||||
.collect();
|
||||
|
||||
// Collect all vertices referenced by edges we're copying
|
||||
let mut referenced_vids: HashSet<VertexId> = HashSet::new();
|
||||
for &eid in &edges_to_copy {
|
||||
if eid.is_none() || self.edges[eid.idx()].deleted {
|
||||
continue;
|
||||
}
|
||||
for &vid in &self.edges[eid.idx()].vertices {
|
||||
referenced_vids.insert(vid);
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
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 {
|
||||
interior_vertices.insert(vid);
|
||||
} else {
|
||||
boundary_vertices.insert(vid);
|
||||
}
|
||||
}
|
||||
|
||||
// Allocate vertices in new graph
|
||||
for &vid in &referenced_vids {
|
||||
let pos = self.vertices[vid.idx()].position;
|
||||
let new_vid = new_graph.alloc_vertex(pos);
|
||||
vtx_map.insert(vid, new_vid);
|
||||
}
|
||||
|
||||
// Copy edges into new graph
|
||||
for &eid in &edges_to_copy {
|
||||
if eid.is_none() || self.edges[eid.idx()].deleted {
|
||||
continue;
|
||||
}
|
||||
let edge = &self.edges[eid.idx()];
|
||||
let new_v0 = vtx_map[&edge.vertices[0]];
|
||||
let new_v1 = vtx_map[&edge.vertices[1]];
|
||||
let new_eid = new_graph.alloc_edge(
|
||||
edge.curve,
|
||||
new_v0,
|
||||
new_v1,
|
||||
edge.stroke_style.clone(),
|
||||
edge.stroke_color.clone(),
|
||||
);
|
||||
edge_map.insert(eid, new_eid);
|
||||
}
|
||||
|
||||
// Copy inside fills into new graph
|
||||
for &fid in inside_fills {
|
||||
if fid.is_none() || self.fills[fid.idx()].deleted {
|
||||
continue;
|
||||
}
|
||||
let fill = &self.fills[fid.idx()];
|
||||
let new_boundary: Vec<(EdgeId, Direction)> = fill
|
||||
.boundary
|
||||
.iter()
|
||||
.map(|&(eid, dir)| {
|
||||
if eid.is_none() {
|
||||
(EdgeId::NONE, dir)
|
||||
} else if let Some(&new_eid) = edge_map.get(&eid) {
|
||||
(new_eid, dir)
|
||||
} else {
|
||||
// Edge referenced by fill but not in edges_to_copy —
|
||||
// shouldn't happen if classification is correct, but
|
||||
// skip gracefully.
|
||||
(EdgeId::NONE, dir)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let new_fid = new_graph.alloc_fill(
|
||||
new_boundary,
|
||||
fill.color,
|
||||
fill.fill_rule,
|
||||
);
|
||||
// Copy gradient and image fill
|
||||
new_graph.fills[new_fid.idx()].gradient_fill = fill.gradient_fill.clone();
|
||||
new_graph.fills[new_fid.idx()].image_fill = fill.image_fill;
|
||||
}
|
||||
|
||||
// Remove inside_edges from self (but NOT boundary edges — those are duplicated)
|
||||
for &eid in inside_edges {
|
||||
if !eid.is_none() && !self.edges[eid.idx()].deleted {
|
||||
self.free_edge(eid);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove inside fills from self
|
||||
for &fid in inside_fills {
|
||||
if !fid.is_none() && !self.fills[fid.idx()].deleted {
|
||||
self.free_fill(fid);
|
||||
}
|
||||
}
|
||||
|
||||
// Free interior vertices (they have no remaining edges in self)
|
||||
for &vid in &interior_vertices {
|
||||
self.free_vertex(vid);
|
||||
}
|
||||
|
||||
(new_graph, vtx_map, edge_map)
|
||||
}
|
||||
|
||||
/// Merge another graph back into `self`, applying `transform` to all geometry.
|
||||
///
|
||||
/// `boundary_vertex_map` maps vertex IDs in `other` to existing vertex IDs in
|
||||
/// `self` (shared boundary vertices that should reconnect rather than duplicate).
|
||||
///
|
||||
/// `boundary_edge_map` maps edge IDs in `other` to existing edge IDs in `self`
|
||||
/// (duplicated boundary edges that should be skipped — `self` already has them).
|
||||
pub fn merge_subgraph(
|
||||
&mut self,
|
||||
other: &VectorGraph,
|
||||
transform: kurbo::Affine,
|
||||
boundary_vertex_map: &HashMap<VertexId, VertexId>,
|
||||
boundary_edge_map: &HashMap<EdgeId, EdgeId>,
|
||||
) {
|
||||
let mut vtx_map: HashMap<VertexId, VertexId> = HashMap::new();
|
||||
let mut edge_map: HashMap<EdgeId, EdgeId> = HashMap::new();
|
||||
|
||||
// Map or allocate vertices
|
||||
for (i, vertex) in other.vertices.iter().enumerate() {
|
||||
let other_vid = VertexId(i as u32);
|
||||
if vertex.deleted {
|
||||
continue;
|
||||
}
|
||||
if let Some(&self_vid) = boundary_vertex_map.get(&other_vid) {
|
||||
vtx_map.insert(other_vid, self_vid);
|
||||
} else {
|
||||
let pos = transform * vertex.position;
|
||||
let new_vid = self.alloc_vertex(pos);
|
||||
vtx_map.insert(other_vid, new_vid);
|
||||
}
|
||||
}
|
||||
|
||||
// Map or allocate edges
|
||||
for (i, edge) in other.edges.iter().enumerate() {
|
||||
let other_eid = EdgeId(i as u32);
|
||||
if edge.deleted {
|
||||
continue;
|
||||
}
|
||||
if let Some(&self_eid) = boundary_edge_map.get(&other_eid) {
|
||||
edge_map.insert(other_eid, self_eid);
|
||||
} else {
|
||||
let new_v0 = vtx_map[&edge.vertices[0]];
|
||||
let new_v1 = vtx_map[&edge.vertices[1]];
|
||||
// Transform the curve control points
|
||||
let curve = CubicBez::new(
|
||||
transform * edge.curve.p0,
|
||||
transform * edge.curve.p1,
|
||||
transform * edge.curve.p2,
|
||||
transform * edge.curve.p3,
|
||||
);
|
||||
let new_eid = self.alloc_edge(
|
||||
curve,
|
||||
new_v0,
|
||||
new_v1,
|
||||
edge.stroke_style.clone(),
|
||||
edge.stroke_color.clone(),
|
||||
);
|
||||
edge_map.insert(other_eid, new_eid);
|
||||
}
|
||||
}
|
||||
|
||||
// Copy fills
|
||||
for (_i, fill) in other.fills.iter().enumerate() {
|
||||
if fill.deleted {
|
||||
continue;
|
||||
}
|
||||
let new_boundary: Vec<(EdgeId, Direction)> = fill
|
||||
.boundary
|
||||
.iter()
|
||||
.map(|&(eid, dir)| {
|
||||
if eid.is_none() {
|
||||
(EdgeId::NONE, dir)
|
||||
} else if let Some(&new_eid) = edge_map.get(&eid) {
|
||||
(new_eid, dir)
|
||||
} else {
|
||||
(EdgeId::NONE, dir)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let new_fid = self.alloc_fill(new_boundary, fill.color, fill.fill_rule);
|
||||
self.fills[new_fid.idx()].gradient_fill = fill.gradient_fill.clone();
|
||||
self.fills[new_fid.idx()].image_fill = fill.image_fill;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -1566,3 +1845,65 @@ fn nearest_point_on_cubic(curve: &CubicBez, point: Point) -> (f64, f64) {
|
||||
let dy = p.y - point.y;
|
||||
(best_t, (dx * dx + dy * dy).sqrt())
|
||||
}
|
||||
|
||||
/// Convert a BezPath into groups of cubic Bézier segments (one group per subpath).
|
||||
pub fn bezpath_to_cubic_segments(path: &kurbo::BezPath) -> Vec<Vec<CubicBez>> {
|
||||
use kurbo::PathEl;
|
||||
|
||||
let mut result: Vec<Vec<CubicBez>> = Vec::new();
|
||||
let mut current: Vec<CubicBez> = Vec::new();
|
||||
let mut subpath_start = Point::ZERO;
|
||||
let mut cursor = Point::ZERO;
|
||||
|
||||
for el in path.elements() {
|
||||
match *el {
|
||||
PathEl::MoveTo(p) => {
|
||||
if !current.is_empty() {
|
||||
result.push(std::mem::take(&mut current));
|
||||
}
|
||||
subpath_start = p;
|
||||
cursor = p;
|
||||
}
|
||||
PathEl::LineTo(p) => {
|
||||
let c1 = lerp_point(cursor, p, 1.0 / 3.0);
|
||||
let c2 = lerp_point(cursor, p, 2.0 / 3.0);
|
||||
current.push(CubicBez::new(cursor, c1, c2, p));
|
||||
cursor = p;
|
||||
}
|
||||
PathEl::QuadTo(p1, p2) => {
|
||||
let cp1 = Point::new(
|
||||
cursor.x + (2.0 / 3.0) * (p1.x - cursor.x),
|
||||
cursor.y + (2.0 / 3.0) * (p1.y - cursor.y),
|
||||
);
|
||||
let cp2 = Point::new(
|
||||
p2.x + (2.0 / 3.0) * (p1.x - p2.x),
|
||||
p2.y + (2.0 / 3.0) * (p1.y - p2.y),
|
||||
);
|
||||
current.push(CubicBez::new(cursor, cp1, cp2, p2));
|
||||
cursor = p2;
|
||||
}
|
||||
PathEl::CurveTo(p1, p2, p3) => {
|
||||
current.push(CubicBez::new(cursor, p1, p2, p3));
|
||||
cursor = p3;
|
||||
}
|
||||
PathEl::ClosePath => {
|
||||
let dist = ((cursor.x - subpath_start.x).powi(2)
|
||||
+ (cursor.y - subpath_start.y).powi(2))
|
||||
.sqrt();
|
||||
if dist > 1e-9 {
|
||||
let c1 = lerp_point(cursor, subpath_start, 1.0 / 3.0);
|
||||
let c2 = lerp_point(cursor, subpath_start, 2.0 / 3.0);
|
||||
current.push(CubicBez::new(cursor, c1, c2, subpath_start));
|
||||
}
|
||||
cursor = subpath_start;
|
||||
if !current.is_empty() {
|
||||
result.push(std::mem::take(&mut current));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !current.is_empty() {
|
||||
result.push(current);
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ fn alloc_fill_with_boundary() {
|
||||
let fid = g.alloc_fill(boundary, fill_color, FillRule::NonZero);
|
||||
|
||||
assert_eq!(g.fill(fid).boundary.len(), 3);
|
||||
assert_eq!(g.fill(fid).color, fill_color);
|
||||
assert_eq!(g.fill(fid).color, Some(fill_color));
|
||||
}
|
||||
|
||||
// ── Adjacency ────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -51,7 +51,7 @@ fn paint_bucket_fills_rectangle() {
|
||||
let fid = fid.unwrap();
|
||||
let fill = g.fill(fid);
|
||||
assert_eq!(fill.boundary.len(), 4, "rectangle boundary should have 4 edges");
|
||||
assert_eq!(fill.color, ShapeColor::rgb(255, 0, 0));
|
||||
assert_eq!(fill.color, Some(ShapeColor::rgb(255, 0, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -148,7 +148,7 @@ fn draw_line_across_fill_splits_it() {
|
||||
|
||||
// Both fills should inherit the original color
|
||||
for (_, fill) in &live_fills {
|
||||
assert_eq!(fill.color, ShapeColor::rgb(255, 0, 0));
|
||||
assert_eq!(fill.color, Some(ShapeColor::rgb(255, 0, 0)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,3 +8,5 @@ mod fill;
|
||||
mod editing;
|
||||
#[cfg(test)]
|
||||
mod gap_close;
|
||||
#[cfg(test)]
|
||||
mod region;
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
//! Tests for extract_subgraph and merge_subgraph (region selection support).
|
||||
|
||||
use super::super::*;
|
||||
use kurbo::{Affine, CubicBez, Point};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// Helper: create a straight-line cubic Bézier 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,
|
||||
)
|
||||
}
|
||||
|
||||
/// Build a triangle graph: 3 vertices, 3 edges, 1 fill.
|
||||
/// Returns (graph, [v0,v1,v2], [e0,e1,e2], fid).
|
||||
fn triangle_graph() -> (VectorGraph, [VertexId; 3], [EdgeId; 3], FillId) {
|
||||
let mut g = VectorGraph::new();
|
||||
let p0 = Point::new(0.0, 0.0);
|
||||
let p1 = Point::new(100.0, 0.0);
|
||||
let p2 = Point::new(50.0, 100.0);
|
||||
|
||||
let v0 = g.alloc_vertex(p0);
|
||||
let v1 = g.alloc_vertex(p1);
|
||||
let v2 = g.alloc_vertex(p2);
|
||||
|
||||
let style = StrokeStyle { width: 1.0, ..Default::default() };
|
||||
let color = ShapeColor::rgb(0, 0, 0);
|
||||
let e0 = g.alloc_edge(line(p0, p1), v0, v1, Some(style.clone()), Some(color));
|
||||
let e1 = g.alloc_edge(line(p1, p2), v1, v2, Some(style.clone()), Some(color));
|
||||
let e2 = g.alloc_edge(line(p2, p0), v2, v0, Some(style), Some(color));
|
||||
|
||||
let boundary = vec![
|
||||
(e0, Direction::Forward),
|
||||
(e1, Direction::Forward),
|
||||
(e2, Direction::Forward),
|
||||
];
|
||||
let fid = g.alloc_fill(boundary, ShapeColor::rgb(255, 0, 0), FillRule::NonZero);
|
||||
|
||||
(g, [v0, v1, v2], [e0, e1, e2], fid)
|
||||
}
|
||||
|
||||
// ── extract_subgraph ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn extract_empty_region_returns_empty_graph() {
|
||||
let (mut g, _, _, _) = triangle_graph();
|
||||
let orig_edge_count = g.edges.iter().filter(|e| !e.deleted).count();
|
||||
|
||||
let (new_g, vtx_map, edge_map) = g.extract_subgraph(
|
||||
&HashSet::new(),
|
||||
&HashSet::new(),
|
||||
&HashSet::new(),
|
||||
);
|
||||
|
||||
// New graph should be empty
|
||||
assert_eq!(new_g.edges.iter().filter(|e| !e.deleted).count(), 0);
|
||||
assert_eq!(new_g.fills.iter().filter(|f| !f.deleted).count(), 0);
|
||||
assert!(vtx_map.is_empty());
|
||||
assert!(edge_map.is_empty());
|
||||
|
||||
// Original should be unchanged
|
||||
assert_eq!(g.edges.iter().filter(|e| !e.deleted).count(), orig_edge_count);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_single_edge_removes_from_original() {
|
||||
let mut g = VectorGraph::new();
|
||||
let v0 = g.alloc_vertex(Point::new(0.0, 0.0));
|
||||
let v1 = g.alloc_vertex(Point::new(100.0, 0.0));
|
||||
let v2 = g.alloc_vertex(Point::new(200.0, 0.0));
|
||||
|
||||
let style = StrokeStyle { width: 1.0, ..Default::default() };
|
||||
let color = ShapeColor::rgb(0, 0, 0);
|
||||
let e0 = g.alloc_edge(line(Point::new(0.0, 0.0), Point::new(100.0, 0.0)), v0, v1, Some(style.clone()), Some(color));
|
||||
let e1 = g.alloc_edge(line(Point::new(100.0, 0.0), Point::new(200.0, 0.0)), v1, v2, Some(style), Some(color));
|
||||
|
||||
let mut inside = HashSet::new();
|
||||
inside.insert(e0);
|
||||
|
||||
let (new_g, vtx_map, edge_map) = g.extract_subgraph(
|
||||
&inside,
|
||||
&HashSet::new(),
|
||||
&HashSet::new(),
|
||||
);
|
||||
|
||||
// e0 should be extracted
|
||||
assert!(g.edge(e0).deleted, "extracted edge should be freed from original");
|
||||
assert!(!g.edge(e1).deleted, "non-extracted edge should remain");
|
||||
|
||||
// v0 is interior (only connected to e0), should be freed
|
||||
assert!(g.vertex(v0).deleted, "interior vertex should be freed");
|
||||
// v1 is shared (connected to e1 too), should NOT be freed
|
||||
assert!(!g.vertex(v1).deleted, "shared vertex should remain");
|
||||
|
||||
// New graph should have the edge
|
||||
assert_eq!(new_g.edges.iter().filter(|e| !e.deleted).count(), 1);
|
||||
assert!(edge_map.contains_key(&e0));
|
||||
|
||||
// New graph should have 2 vertices (v0 and v1 mapped)
|
||||
assert_eq!(new_g.vertices.iter().filter(|v| !v.deleted).count(), 2);
|
||||
assert!(vtx_map.contains_key(&v0));
|
||||
assert!(vtx_map.contains_key(&v1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_fill_duplicates_boundary_edges() {
|
||||
let (mut g, verts, edges, fid) = triangle_graph();
|
||||
|
||||
// Pretend e0 is a boundary edge (from region selection insert_stroke)
|
||||
let mut boundary_edges = HashSet::new();
|
||||
boundary_edges.insert(edges[0]);
|
||||
|
||||
// e1 and e2 are "inside"
|
||||
let mut inside_edges = HashSet::new();
|
||||
inside_edges.insert(edges[1]);
|
||||
inside_edges.insert(edges[2]);
|
||||
|
||||
let mut inside_fills = HashSet::new();
|
||||
inside_fills.insert(fid);
|
||||
|
||||
let (new_g, vtx_map, edge_map) = g.extract_subgraph(
|
||||
&inside_edges,
|
||||
&inside_fills,
|
||||
&boundary_edges,
|
||||
);
|
||||
|
||||
// Boundary edge e0 should still exist in original (duplicated, not removed)
|
||||
assert!(!g.edge(edges[0]).deleted, "boundary edge should remain in original");
|
||||
|
||||
// Inside edges should be removed from original
|
||||
assert!(g.edge(edges[1]).deleted, "inside edge should be freed from original");
|
||||
assert!(g.edge(edges[2]).deleted, "inside edge should be freed from original");
|
||||
|
||||
// New graph should have 3 edges: e0 (boundary copy) + e1 + e2
|
||||
assert_eq!(new_g.edges.iter().filter(|e| !e.deleted).count(), 3);
|
||||
|
||||
// New graph should have 1 fill
|
||||
assert_eq!(new_g.fills.iter().filter(|f| !f.deleted).count(), 1);
|
||||
|
||||
// The fill's boundary in new graph should reference remapped edges
|
||||
let new_fill = &new_g.fills[0];
|
||||
assert_eq!(new_fill.boundary.len(), 3);
|
||||
for &(eid, _) in &new_fill.boundary {
|
||||
assert!(!eid.is_none(), "fill boundary should have valid edge IDs");
|
||||
}
|
||||
|
||||
// Fill color should be preserved
|
||||
assert_eq!(new_fill.color, Some(ShapeColor::rgb(255, 0, 0)));
|
||||
}
|
||||
|
||||
// ── merge_subgraph ───────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn merge_round_trip_identity_restores_edges() {
|
||||
let mut g = VectorGraph::new();
|
||||
let v0 = g.alloc_vertex(Point::new(0.0, 0.0));
|
||||
let v1 = g.alloc_vertex(Point::new(100.0, 0.0));
|
||||
let v2 = g.alloc_vertex(Point::new(200.0, 0.0));
|
||||
|
||||
let style = StrokeStyle { width: 1.0, ..Default::default() };
|
||||
let color = ShapeColor::rgb(0, 0, 0);
|
||||
let e0 = g.alloc_edge(line(Point::new(0.0, 0.0), Point::new(100.0, 0.0)), v0, v1, Some(style.clone()), Some(color));
|
||||
let _e1 = g.alloc_edge(line(Point::new(100.0, 0.0), Point::new(200.0, 0.0)), v1, v2, Some(style), Some(color));
|
||||
|
||||
let mut inside = HashSet::new();
|
||||
inside.insert(e0);
|
||||
|
||||
let (new_g, vtx_map, edge_map) = g.extract_subgraph(
|
||||
&inside,
|
||||
&HashSet::new(),
|
||||
&HashSet::new(),
|
||||
);
|
||||
|
||||
// Build boundary vertex map (reverse of vtx_map, only for non-deleted vertices in g)
|
||||
let boundary_vtx_map: HashMap<VertexId, VertexId> = vtx_map.iter()
|
||||
.filter(|(&old, _)| !g.vertex(old).deleted)
|
||||
.map(|(&old, &new)| (new, old))
|
||||
.collect();
|
||||
|
||||
// Merge back with identity transform
|
||||
g.merge_subgraph(&new_g, Affine::IDENTITY, &boundary_vtx_map, &HashMap::new());
|
||||
|
||||
// Should have 2 non-deleted edges again
|
||||
let live_edges = g.edges.iter().filter(|e| !e.deleted).count();
|
||||
assert_eq!(live_edges, 2, "should have 2 edges after merge-back");
|
||||
|
||||
// Should have 3 vertices (v0 was freed then re-added)
|
||||
let live_verts = g.vertices.iter().filter(|v| !v.deleted).count();
|
||||
assert_eq!(live_verts, 3, "should have 3 vertices after merge-back");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merge_with_translation_moves_geometry() {
|
||||
let mut g = VectorGraph::new();
|
||||
let v0 = g.alloc_vertex(Point::new(0.0, 0.0));
|
||||
let v1 = g.alloc_vertex(Point::new(100.0, 0.0));
|
||||
|
||||
let style = StrokeStyle { width: 1.0, ..Default::default() };
|
||||
let color = ShapeColor::rgb(0, 0, 0);
|
||||
let e0 = g.alloc_edge(line(Point::new(0.0, 0.0), Point::new(100.0, 0.0)), v0, v1, Some(style), Some(color));
|
||||
|
||||
let mut inside = HashSet::new();
|
||||
inside.insert(e0);
|
||||
|
||||
let (new_g, _vtx_map, _edge_map) = g.extract_subgraph(
|
||||
&inside,
|
||||
&HashSet::new(),
|
||||
&HashSet::new(),
|
||||
);
|
||||
|
||||
// Merge back with a translation of (50, 50)
|
||||
let transform = Affine::translate((50.0, 50.0));
|
||||
g.merge_subgraph(&new_g, transform, &HashMap::new(), &HashMap::new());
|
||||
|
||||
// The merged edge's vertices should be at (50,50) and (150,50)
|
||||
let merged_edge = g.edges.iter().find(|e| !e.deleted).unwrap();
|
||||
let v0_pos = g.vertices[merged_edge.vertices[0].idx()].position;
|
||||
let v1_pos = g.vertices[merged_edge.vertices[1].idx()].position;
|
||||
|
||||
assert!((v0_pos.x - 50.0).abs() < 0.01 && (v0_pos.y - 50.0).abs() < 0.01,
|
||||
"v0 should be at (50, 50), got ({}, {})", v0_pos.x, v0_pos.y);
|
||||
assert!((v1_pos.x - 150.0).abs() < 0.01 && (v1_pos.y - 50.0).abs() < 0.01,
|
||||
"v1 should be at (150, 50), got ({}, {})", v1_pos.x, v1_pos.y);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_and_merge_fill_round_trip() {
|
||||
let (mut g, _verts, edges, fid) = triangle_graph();
|
||||
|
||||
// Treat e0 as boundary, e1+e2 as inside, fill as inside
|
||||
let mut boundary_edges = HashSet::new();
|
||||
boundary_edges.insert(edges[0]);
|
||||
let mut inside_edges = HashSet::new();
|
||||
inside_edges.insert(edges[1]);
|
||||
inside_edges.insert(edges[2]);
|
||||
let mut inside_fills = HashSet::new();
|
||||
inside_fills.insert(fid);
|
||||
|
||||
let (new_g, vtx_map, edge_map) = g.extract_subgraph(
|
||||
&inside_edges,
|
||||
&inside_fills,
|
||||
&boundary_edges,
|
||||
);
|
||||
|
||||
// Build maps for merge-back
|
||||
let boundary_vtx_map: HashMap<VertexId, VertexId> = vtx_map.iter()
|
||||
.filter(|(&old, _)| !g.vertex(old).deleted)
|
||||
.map(|(&old, &new)| (new, old))
|
||||
.collect();
|
||||
let boundary_edge_map_for_merge: HashMap<EdgeId, EdgeId> = edge_map.iter()
|
||||
.filter(|(old_eid, _)| boundary_edges.contains(old_eid))
|
||||
.map(|(&old, &new)| (new, old))
|
||||
.collect();
|
||||
|
||||
// Before merge: original has 1 edge (boundary), extracted has 3 edges + 1 fill
|
||||
assert_eq!(g.edges.iter().filter(|e| !e.deleted).count(), 1);
|
||||
assert_eq!(g.fills.iter().filter(|f| !f.deleted).count(), 0);
|
||||
assert_eq!(new_g.edges.iter().filter(|e| !e.deleted).count(), 3);
|
||||
assert_eq!(new_g.fills.iter().filter(|f| !f.deleted).count(), 1);
|
||||
|
||||
// Merge back
|
||||
g.merge_subgraph(&new_g, Affine::IDENTITY, &boundary_vtx_map, &boundary_edge_map_for_merge);
|
||||
|
||||
// After merge: should have 3 edges (boundary + 2 merged) and 1 fill
|
||||
assert_eq!(g.edges.iter().filter(|e| !e.deleted).count(), 3);
|
||||
assert_eq!(g.fills.iter().filter(|f| !f.deleted).count(), 1);
|
||||
}
|
||||
Reference in New Issue
Block a user