work on vector graph

This commit is contained in:
Skyler Lehmkuhl
2026-03-22 18:16:17 -04:00
parent 8acac71d86
commit f16e651610
21 changed files with 1317 additions and 843 deletions
@@ -1,17 +1,20 @@
//! Add shape action — inserts strokes into the DCEL.
//! Add shape action — inserts strokes into the VectorGraph.
//!
//! Converts a BezPath into cubic segments and inserts them via
//! `Dcel::insert_stroke()`. Undo is handled by snapshotting the DCEL.
//! `VectorGraph::insert_stroke()`. Undo is handled by snapshotting the graph.
use crate::action::Action;
use crate::dcel::{bezpath_to_cubic_segments, Dcel, FaceId, DEFAULT_SNAP_EPSILON};
use crate::vector_graph::bezpath_to_cubic_segments;
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::shape::{ShapeColor, StrokeStyle};
use kurbo::BezPath;
use crate::shape::{FillRule, ShapeColor, StrokeStyle};
use crate::vector_graph::VectorGraph;
use kurbo::{BezPath, Shape as _};
use uuid::Uuid;
/// Action that inserts a drawn path into a vector layer's DCEL keyframe.
const DEFAULT_SNAP_EPSILON: f64 = 0.5;
/// Action that inserts a drawn path into a vector layer's VectorGraph keyframe.
pub struct AddShapeAction {
layer_id: Uuid,
time: f64,
@@ -21,8 +24,8 @@ pub struct AddShapeAction {
fill_color: Option<ShapeColor>,
is_closed: bool,
description_text: String,
/// Snapshot of the DCEL before insertion (for undo).
dcel_before: Option<Dcel>,
/// Snapshot of the graph before insertion (for undo).
graph_before: Option<VectorGraph>,
}
impl AddShapeAction {
@@ -44,7 +47,7 @@ impl AddShapeAction {
fill_color,
is_closed,
description_text: "Add shape".to_string(),
dcel_before: None,
graph_before: None,
}
}
@@ -66,10 +69,10 @@ impl Action for AddShapeAction {
};
let keyframe = vl.ensure_keyframe_at(self.time);
let dcel = &mut keyframe.dcel;
let graph = &mut keyframe.graph;
// Snapshot for undo
self.dcel_before = Some(dcel.clone());
self.graph_before = Some(graph.clone());
let subpaths = bezpath_to_cubic_segments(&self.path);
@@ -77,41 +80,27 @@ impl Action for AddShapeAction {
if segments.is_empty() {
continue;
}
let result = dcel.insert_stroke(
let _new_edges = graph.insert_stroke(
segments,
self.stroke_style.clone(),
self.stroke_color.clone(),
DEFAULT_SNAP_EPSILON,
);
// Apply fill to new faces if this is a closed shape with fill
// Apply fill if this is a closed shape with fill
if self.is_closed {
if let Some(ref fill) = self.fill_color {
if !result.new_faces.is_empty() {
for face_id in &result.new_faces {
dcel.face_mut(*face_id).fill_color = Some(fill.clone());
}
} else if let Some(&first_edge) = result.new_edges.first() {
// Closed shape in F0 — no face was auto-created.
// One half-edge of the first new edge is on the interior cycle.
// Pick the side with positive signed area (CCW winding).
let [he_a, he_b] = dcel.edge(first_edge).half_edges;
let interior_he = if dcel.cycle_signed_area(he_a) > 0.0 {
he_a
} else {
he_b
};
if dcel.half_edge(interior_he).face == FaceId(0) {
let face_id = dcel.create_face_at_cycle(interior_he);
dcel.face_mut(face_id).fill_color = Some(fill.clone());
}
}
// Compute centroid of the path's bounding box and paint-bucket fill
let bbox = self.path.bounding_box();
let centroid = kurbo::Point::new(
(bbox.x0 + bbox.x1) / 2.0,
(bbox.y0 + bbox.y1) / 2.0,
);
graph.paint_bucket(centroid, fill.clone(), FillRule::NonZero, 0.0);
}
}
}
dcel.rebuild_spatial_index();
Ok(())
}
@@ -126,10 +115,10 @@ impl Action for AddShapeAction {
};
let keyframe = vl.ensure_keyframe_at(self.time);
keyframe.dcel = self
.dcel_before
keyframe.graph = self
.graph_before
.take()
.ok_or_else(|| "No DCEL snapshot for undo".to_string())?;
.ok_or_else(|| "No graph snapshot for undo".to_string())?;
Ok(())
}
@@ -40,7 +40,7 @@ pub use add_clip_instance::AddClipInstanceAction;
pub use add_effect::AddEffectAction;
pub use add_layer::AddLayerAction;
pub use add_shape::AddShapeAction;
pub use modify_shape_path::ModifyDcelAction;
pub use modify_shape_path::ModifyGraphAction;
pub use move_clip_instances::MoveClipInstancesAction;
pub use paint_bucket::PaintBucketAction;
pub use remove_effect::RemoveEffectAction;
@@ -1,45 +1,45 @@
//! Modify DCEL action — snapshot-based undo for DCEL editing
//! Modify graph action — snapshot-based undo for VectorGraph editing
use crate::action::Action;
use crate::dcel::Dcel;
use crate::vector_graph::VectorGraph;
use crate::document::Document;
use crate::layer::AnyLayer;
use uuid::Uuid;
/// Action that captures a before/after DCEL snapshot for undo/redo.
/// Action that captures a before/after VectorGraph snapshot for undo/redo.
///
/// Used by vertex editing, curve editing, and control point editing.
/// The caller provides both snapshots (taken before and after the edit).
pub struct ModifyDcelAction {
pub struct ModifyGraphAction {
layer_id: Uuid,
time: f64,
dcel_before: Option<Dcel>,
dcel_after: Option<Dcel>,
graph_before: Option<VectorGraph>,
graph_after: Option<VectorGraph>,
description_text: String,
}
impl ModifyDcelAction {
impl ModifyGraphAction {
pub fn new(
layer_id: Uuid,
time: f64,
dcel_before: Dcel,
dcel_after: Dcel,
graph_before: VectorGraph,
graph_after: VectorGraph,
description: impl Into<String>,
) -> Self {
Self {
layer_id,
time,
dcel_before: Some(dcel_before),
dcel_after: Some(dcel_after),
graph_before: Some(graph_before),
graph_after: Some(graph_after),
description_text: description.into(),
}
}
}
impl Action for ModifyDcelAction {
impl Action for ModifyGraphAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let dcel_after = self.dcel_after.as_ref()
.ok_or("ModifyDcelAction: no dcel_after snapshot")?
let graph_after = self.graph_after.as_ref()
.ok_or("ModifyGraphAction: no graph_after snapshot")?
.clone();
let layer = document.get_layer_mut(&self.layer_id)
@@ -47,7 +47,7 @@ impl Action for ModifyDcelAction {
if let AnyLayer::Vector(vl) = layer {
if let Some(kf) = vl.keyframe_at_mut(self.time) {
kf.dcel = dcel_after;
kf.graph = graph_after;
Ok(())
} else {
Err(format!("No keyframe at time {}", self.time))
@@ -58,8 +58,8 @@ impl Action for ModifyDcelAction {
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let dcel_before = self.dcel_before.as_ref()
.ok_or("ModifyDcelAction: no dcel_before snapshot")?
let graph_before = self.graph_before.as_ref()
.ok_or("ModifyGraphAction: no graph_before snapshot")?
.clone();
let layer = document.get_layer_mut(&self.layer_id)
@@ -67,7 +67,7 @@ impl Action for ModifyDcelAction {
if let AnyLayer::Vector(vl) = layer {
if let Some(kf) = vl.keyframe_at_mut(self.time) {
kf.dcel = dcel_before;
kf.graph = graph_before;
Ok(())
} else {
Err(format!("No keyframe at time {}", self.time))
@@ -1,23 +1,21 @@
//! Paint bucket fill action — sets fill_color on a DCEL face.
//! Paint bucket fill action — creates a fill region in a VectorGraph.
use crate::action::Action;
use crate::dcel::FaceId;
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::shape::ShapeColor;
use crate::shape::{FillRule, ShapeColor};
use crate::vector_graph::FillId;
use uuid::Uuid;
use vello::kurbo::Point;
/// Action that performs a paint bucket fill on a DCEL face.
/// Action that performs a paint bucket fill on a VectorGraph region.
pub struct PaintBucketAction {
layer_id: Uuid,
time: f64,
click_point: Point,
fill_color: ShapeColor,
/// The face that was hit (resolved during execute)
hit_face: Option<FaceId>,
/// Previous fill color for undo
old_fill_color: Option<Option<ShapeColor>>,
/// The fill that was created (resolved during execute)
hit_fill: Option<FillId>,
}
impl PaintBucketAction {
@@ -32,8 +30,7 @@ impl PaintBucketAction {
time,
click_point,
fill_color,
hit_face: None,
old_fill_color: None,
hit_fill: None,
}
}
}
@@ -50,45 +47,19 @@ impl Action for PaintBucketAction {
};
let keyframe = vl.ensure_keyframe_at(self.time);
let dcel = &mut keyframe.dcel;
let graph = &mut keyframe.graph;
// Record for debug test generation (if recording is active)
dcel.record_paint_point(self.click_point);
let fill_id = graph
.paint_bucket(self.click_point, self.fill_color.clone(), FillRule::NonZero, 2.0)
.ok_or("No fillable region at click point")?;
// Find the enclosing cycle for the click point
let query = dcel.find_face_at_point(self.click_point);
// Dump cumulative test to stderr after every paint click (if recording)
if dcel.is_recording() {
eprintln!("\n--- DCEL debug test (cumulative, face={:?}) ---", query.face);
dcel.debug_recorder.as_ref().unwrap().dump_test("test_recorded");
eprintln!("--- end test ---\n");
}
if query.cycle_he.is_none() {
// No edges at all — nothing to fill
return Err("No face at click point".to_string());
}
// If the cycle is in F0 (no face created yet), create one now
let face_id = if query.face.0 == 0 {
dcel.create_face_at_cycle(query.cycle_he)
} else {
query.face
};
// Store for undo
self.hit_face = Some(face_id);
self.old_fill_color = Some(dcel.face(face_id).fill_color.clone());
// Apply fill
dcel.face_mut(face_id).fill_color = Some(self.fill_color.clone());
self.hit_fill = Some(fill_id);
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let face_id = self.hit_face.ok_or("No face to undo")?;
let fill_id = self.hit_fill.ok_or("No fill to undo")?;
let layer = document
.get_layer_mut(&self.layer_id)
@@ -100,9 +71,9 @@ impl Action for PaintBucketAction {
};
let keyframe = vl.ensure_keyframe_at(self.time);
let dcel = &mut keyframe.dcel;
let graph = &mut keyframe.graph;
dcel.face_mut(face_id).fill_color = self.old_fill_color.take().unwrap_or(None);
graph.free_fill(fill_id);
Ok(())
}
@@ -1,30 +1,30 @@
//! Action that changes the fill of one or more DCEL faces.
//! Action that changes the fill of one or more VectorGraph fills.
//!
//! Handles both solid-colour and gradient fills, clearing the other type so they
//! don't coexist on a face.
//! don't coexist on a fill.
use crate::action::Action;
use crate::dcel::FaceId;
use crate::vector_graph::FillId;
use crate::document::Document;
use crate::gradient::ShapeGradient;
use crate::layer::AnyLayer;
use crate::shape::ShapeColor;
use uuid::Uuid;
/// Snapshot of one face's fill state (both types) for undo.
/// Snapshot of one fill's state (both types) for undo.
#[derive(Clone)]
struct OldFill {
face_id: FaceId,
fill_id: FillId,
color: Option<ShapeColor>,
gradient: Option<ShapeGradient>,
}
/// Action that sets a solid-colour *or* gradient fill on a set of faces,
/// Action that sets a solid-colour *or* gradient fill on a set of fills,
/// clearing the other fill type.
pub struct SetFillPaintAction {
layer_id: Uuid,
time: f64,
face_ids: Vec<FaceId>,
fill_ids: Vec<FillId>,
new_color: Option<ShapeColor>,
new_gradient: Option<ShapeGradient>,
old_fills: Vec<OldFill>,
@@ -32,17 +32,17 @@ pub struct SetFillPaintAction {
}
impl SetFillPaintAction {
/// Set a solid fill (clears any gradient on the same faces).
/// Set a solid fill (clears any gradient on the same fills).
pub fn solid(
layer_id: Uuid,
time: f64,
face_ids: Vec<FaceId>,
fill_ids: Vec<FillId>,
color: Option<ShapeColor>,
) -> Self {
Self {
layer_id,
time,
face_ids,
fill_ids,
new_color: color,
new_gradient: None,
old_fills: Vec::new(),
@@ -50,17 +50,17 @@ impl SetFillPaintAction {
}
}
/// Set a gradient fill (clears any solid colour on the same faces).
/// Set a gradient fill (clears any solid colour on the same fills).
pub fn gradient(
layer_id: Uuid,
time: f64,
face_ids: Vec<FaceId>,
fill_ids: Vec<FillId>,
gradient: Option<ShapeGradient>,
) -> Self {
Self {
layer_id,
time,
face_ids,
fill_ids,
new_color: None,
new_gradient: gradient,
old_fills: Vec::new(),
@@ -68,17 +68,17 @@ impl SetFillPaintAction {
}
}
fn get_dcel_mut<'a>(
fn get_graph_mut<'a>(
document: &'a mut Document,
layer_id: &Uuid,
time: f64,
) -> Result<&'a mut crate::dcel::Dcel, String> {
) -> Result<&'a mut crate::vector_graph::VectorGraph, String> {
let layer = document
.get_layer_mut(layer_id)
.ok_or_else(|| format!("Layer {} not found", layer_id))?;
match layer {
AnyLayer::Vector(vl) => vl
.dcel_at_time_mut(time)
.graph_at_time_mut(time)
.ok_or_else(|| format!("No keyframe at time {}", time)),
_ => Err("Not a vector layer".to_string()),
}
@@ -87,36 +87,36 @@ impl SetFillPaintAction {
impl Action for SetFillPaintAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let dcel = Self::get_dcel_mut(document, &self.layer_id, self.time)?;
let graph = Self::get_graph_mut(document, &self.layer_id, self.time)?;
self.old_fills.clear();
for &fid in &self.face_ids {
let face = dcel.face(fid);
for &fid in &self.fill_ids {
let fill = graph.fill(fid);
self.old_fills.push(OldFill {
face_id: fid,
color: face.fill_color,
gradient: face.gradient_fill.clone(),
fill_id: fid,
color: fill.color,
gradient: fill.gradient_fill.clone(),
});
let face_mut = dcel.face_mut(fid);
let fill_mut = graph.fill_mut(fid);
// Setting a gradient clears solid colour and vice-versa.
if self.new_gradient.is_some() || self.new_color.is_none() {
face_mut.fill_color = self.new_color;
face_mut.gradient_fill = self.new_gradient.clone();
fill_mut.color = self.new_color;
fill_mut.gradient_fill = self.new_gradient.clone();
} else {
face_mut.fill_color = self.new_color;
face_mut.gradient_fill = None;
fill_mut.color = self.new_color;
fill_mut.gradient_fill = None;
}
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let dcel = Self::get_dcel_mut(document, &self.layer_id, self.time)?;
let graph = Self::get_graph_mut(document, &self.layer_id, self.time)?;
for old in &self.old_fills {
let face = dcel.face_mut(old.face_id);
face.fill_color = old.color;
face.gradient_fill = old.gradient.clone();
let fill = graph.fill_mut(old.fill_id);
fill.color = old.color;
fill.gradient_fill = old.gradient.clone();
}
Ok(())
}
@@ -1,24 +1,24 @@
//! Set shape properties action — operates on DCEL edge/face IDs.
//! Set shape properties action — operates on VectorGraph edge/fill IDs.
use crate::action::Action;
use crate::dcel::{EdgeId, FaceId};
use crate::vector_graph::{EdgeId, FillId};
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::shape::ShapeColor;
use uuid::Uuid;
/// Action that sets fill/stroke properties on DCEL elements.
/// Action that sets fill/stroke properties on VectorGraph elements.
pub struct SetShapePropertiesAction {
layer_id: Uuid,
time: f64,
change: PropertyChange,
old_edge_values: Vec<(EdgeId, Option<ShapeColor>, Option<f64>)>,
old_face_values: Vec<(FaceId, Option<ShapeColor>)>,
old_fill_values: Vec<(FillId, Option<ShapeColor>)>,
}
enum PropertyChange {
FillColor {
face_ids: Vec<FaceId>,
fill_ids: Vec<FillId>,
color: Option<ShapeColor>,
},
StrokeColor {
@@ -35,15 +35,15 @@ impl SetShapePropertiesAction {
pub fn set_fill_color(
layer_id: Uuid,
time: f64,
face_ids: Vec<FaceId>,
fill_ids: Vec<FillId>,
color: Option<ShapeColor>,
) -> Self {
Self {
layer_id,
time,
change: PropertyChange::FillColor { face_ids, color },
change: PropertyChange::FillColor { fill_ids, color },
old_edge_values: Vec::new(),
old_face_values: Vec::new(),
old_fill_values: Vec::new(),
}
}
@@ -58,7 +58,7 @@ impl SetShapePropertiesAction {
time,
change: PropertyChange::StrokeColor { edge_ids, color },
old_edge_values: Vec::new(),
old_face_values: Vec::new(),
old_fill_values: Vec::new(),
}
}
@@ -73,15 +73,15 @@ impl SetShapePropertiesAction {
time,
change: PropertyChange::StrokeWidth { edge_ids, width },
old_edge_values: Vec::new(),
old_face_values: Vec::new(),
old_fill_values: Vec::new(),
}
}
fn get_dcel_mut<'a>(
fn get_graph_mut<'a>(
document: &'a mut Document,
layer_id: &Uuid,
time: f64,
) -> Result<&'a mut crate::dcel::Dcel, String> {
) -> Result<&'a mut crate::vector_graph::VectorGraph, String> {
let layer = document
.get_layer_mut(layer_id)
.ok_or_else(|| format!("Layer {} not found", layer_id))?;
@@ -89,40 +89,40 @@ impl SetShapePropertiesAction {
AnyLayer::Vector(vl) => vl,
_ => return Err("Not a vector layer".to_string()),
};
vl.dcel_at_time_mut(time)
vl.graph_at_time_mut(time)
.ok_or_else(|| format!("No keyframe at time {}", time))
}
}
impl Action for SetShapePropertiesAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let dcel = Self::get_dcel_mut(document, &self.layer_id, self.time)?;
let graph = Self::get_graph_mut(document, &self.layer_id, self.time)?;
match &self.change {
PropertyChange::FillColor { face_ids, color } => {
self.old_face_values.clear();
for &fid in face_ids {
let face = dcel.face(fid);
self.old_face_values.push((fid, face.fill_color));
dcel.face_mut(fid).fill_color = *color;
PropertyChange::FillColor { fill_ids, color } => {
self.old_fill_values.clear();
for &fid in fill_ids {
let fill = graph.fill(fid);
self.old_fill_values.push((fid, fill.color));
graph.fill_mut(fid).color = *color;
}
}
PropertyChange::StrokeColor { edge_ids, color } => {
self.old_edge_values.clear();
for &eid in edge_ids {
let edge = dcel.edge(eid);
let edge = graph.edge(eid);
let old_width = edge.stroke_style.as_ref().map(|s| s.width);
self.old_edge_values.push((eid, edge.stroke_color, old_width));
dcel.edge_mut(eid).stroke_color = *color;
graph.edge_mut(eid).stroke_color = *color;
}
}
PropertyChange::StrokeWidth { edge_ids, width } => {
self.old_edge_values.clear();
for &eid in edge_ids {
let edge = dcel.edge(eid);
let edge = graph.edge(eid);
let old_width = edge.stroke_style.as_ref().map(|s| s.width);
self.old_edge_values.push((eid, edge.stroke_color, old_width));
if let Some(ref mut style) = dcel.edge_mut(eid).stroke_style {
if let Some(ref mut style) = graph.edge_mut(eid).stroke_style {
style.width = *width;
}
}
@@ -133,23 +133,23 @@ impl Action for SetShapePropertiesAction {
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let dcel = Self::get_dcel_mut(document, &self.layer_id, self.time)?;
let graph = Self::get_graph_mut(document, &self.layer_id, self.time)?;
match &self.change {
PropertyChange::FillColor { .. } => {
for &(fid, old_color) in &self.old_face_values {
dcel.face_mut(fid).fill_color = old_color;
for &(fid, old_color) in &self.old_fill_values {
graph.fill_mut(fid).color = old_color;
}
}
PropertyChange::StrokeColor { .. } => {
for &(eid, old_color, _) in &self.old_edge_values {
dcel.edge_mut(eid).stroke_color = old_color;
graph.edge_mut(eid).stroke_color = old_color;
}
}
PropertyChange::StrokeWidth { .. } => {
for &(eid, _, old_width) in &self.old_edge_values {
if let Some(w) = old_width {
if let Some(ref mut style) = dcel.edge_mut(eid).stroke_style {
if let Some(ref mut style) = graph.edge_mut(eid).stroke_style {
style.width = w;
}
}