Change vector drawing primitive from shape to doubly-connected edge graph

This commit is contained in:
Skyler Lehmkuhl
2026-02-23 21:29:58 -05:00
parent eab116c930
commit 99f8dcfcf4
31 changed files with 2664 additions and 3774 deletions
@@ -4,9 +4,9 @@
//! shapes and objects, taking into account transform hierarchies.
use crate::clip::ClipInstance;
use crate::dcel::{VertexId, EdgeId, FaceId};
use crate::layer::VectorLayer;
use crate::region_select;
use crate::shape::Shape;
use crate::shape::Shape; // TODO: remove after DCEL migration complete
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use vello::kurbo::{Affine, BezPath, Point, Rect, Shape as KurboShape};
@@ -36,21 +36,13 @@ pub enum HitResult {
///
/// The UUID of the first shape hit, or None if no hit
pub fn hit_test_layer(
layer: &VectorLayer,
time: f64,
point: Point,
tolerance: f64,
parent_transform: Affine,
_layer: &VectorLayer,
_time: f64,
_point: Point,
_tolerance: f64,
_parent_transform: Affine,
) -> Option<Uuid> {
// Test shapes in reverse order (front to back for hit testing)
for shape in layer.shapes_at_time(time).iter().rev() {
let combined_transform = parent_transform * shape.transform.to_affine();
if hit_test_shape(shape, point, tolerance, combined_transform) {
return Some(shape.id);
}
}
// TODO: Implement DCEL-based hit testing (faces, edges, vertices)
None
}
@@ -95,29 +87,13 @@ pub fn hit_test_shape(
///
/// Returns all shapes in the active keyframe whose bounding boxes intersect with the given rectangle.
pub fn hit_test_objects_in_rect(
layer: &VectorLayer,
time: f64,
rect: Rect,
parent_transform: Affine,
_layer: &VectorLayer,
_time: f64,
_rect: Rect,
_parent_transform: Affine,
) -> Vec<Uuid> {
let mut hits = Vec::new();
for shape in layer.shapes_at_time(time) {
let combined_transform = parent_transform * shape.transform.to_affine();
// Get shape bounding box in local space
let bbox = shape.path().bounding_box();
// Transform bounding box to screen space
let transformed_bbox = combined_transform.transform_rect_bbox(bbox);
// Check if rectangles intersect
if rect.intersect(transformed_bbox).area() > 0.0 {
hits.push(shape.id);
}
}
hits
// TODO: Implement DCEL-based marquee selection
Vec::new()
}
/// Classification of shapes relative to a clipping region
@@ -141,7 +117,7 @@ pub fn classify_shapes_by_region(
region: &BezPath,
parent_transform: Affine,
) -> ShapeRegionClassification {
let mut result = ShapeRegionClassification {
let result = ShapeRegionClassification {
fully_inside: Vec::new(),
intersecting: Vec::new(),
fully_outside: Vec::new(),
@@ -149,33 +125,8 @@ pub fn classify_shapes_by_region(
let region_bbox = region.bounding_box();
for shape in layer.shapes_at_time(time) {
let combined_transform = parent_transform * shape.transform.to_affine();
let bbox = shape.path().bounding_box();
let transformed_bbox = combined_transform.transform_rect_bbox(bbox);
// Fast rejection: if bounding boxes don't overlap, fully outside
if region_bbox.intersect(transformed_bbox).area() <= 0.0 {
result.fully_outside.push(shape.id);
continue;
}
// Transform the shape path to world space for accurate testing
let world_path = {
let mut p = shape.path().clone();
p.apply_affine(combined_transform);
p
};
// Check if the path crosses the region boundary
if region_select::path_intersects_region(&world_path, region) {
result.intersecting.push(shape.id);
} else if region_select::path_fully_inside_region(&world_path, region) {
result.fully_inside.push(shape.id);
} else {
result.fully_outside.push(shape.id);
}
}
// TODO: Implement DCEL-based region classification
let _ = (layer, time, parent_transform, region_bbox);
result
}
@@ -300,23 +251,22 @@ pub fn hit_test_clip_instances_in_rect(
pub enum VectorEditHit {
/// Hit a control point (BezierEdit tool only)
ControlPoint {
shape_instance_id: Uuid,
curve_index: usize,
point_index: u8,
edge_id: EdgeId,
point_index: u8, // 1 = p1, 2 = p2
},
/// Hit a vertex (anchor point)
Vertex {
shape_instance_id: Uuid,
vertex_index: usize,
vertex_id: VertexId,
},
/// Hit a curve segment
Curve {
shape_instance_id: Uuid,
curve_index: usize,
edge_id: EdgeId,
parameter_t: f64,
},
/// Hit shape fill
Fill { shape_instance_id: Uuid },
Fill {
face_id: FaceId,
},
}
/// Tolerances for vector editing hit testing (in screen pixels)
@@ -359,83 +309,79 @@ pub fn hit_test_vector_editing(
parent_transform: Affine,
show_control_points: bool,
) -> Option<VectorEditHit> {
use crate::bezpath_editing::extract_editable_curves;
use vello::kurbo::{ParamCurve, ParamCurveNearest};
use kurbo::ParamCurveNearest;
// Test shapes in reverse order (front to back for hit testing)
for shape in layer.shapes_at_time(time).iter().rev() {
let combined_transform = parent_transform * shape.transform.to_affine();
let inverse_transform = combined_transform.inverse();
let local_point = inverse_transform * point;
let dcel = layer.dcel_at_time(time)?;
// Calculate the scale factor to transform screen-space tolerances to local space
let coeffs = combined_transform.as_coeffs();
let scale_x = (coeffs[0].powi(2) + coeffs[1].powi(2)).sqrt();
let scale_y = (coeffs[2].powi(2) + coeffs[3].powi(2)).sqrt();
let avg_scale = (scale_x + scale_y) / 2.0;
let local_tolerance_factor = 1.0 / avg_scale.max(0.001);
// Transform point into layer-local space
let local_point = parent_transform.inverse() * point;
let editable = extract_editable_curves(shape.path());
// Priority: ControlPoint > Vertex > Curve
// Priority 1: Control points (only in BezierEdit mode)
if show_control_points {
let local_cp_tolerance = tolerance.control_point * local_tolerance_factor;
for (i, curve) in editable.curves.iter().enumerate() {
let dist_p1 = (curve.p1 - local_point).hypot();
if dist_p1 < local_cp_tolerance {
return Some(VectorEditHit::ControlPoint {
shape_instance_id: shape.id,
curve_index: i,
point_index: 1,
});
// 1. Control points (only when show_control_points is true, e.g. BezierEdit tool)
if show_control_points {
let mut best_cp: Option<(EdgeId, u8, f64)> = None;
for (i, edge) in dcel.edges.iter().enumerate() {
if edge.deleted {
continue;
}
let edge_id = EdgeId(i as u32);
// Check p1
let d1 = local_point.distance(edge.curve.p1);
if d1 < tolerance.control_point {
if best_cp.is_none() || d1 < best_cp.unwrap().2 {
best_cp = Some((edge_id, 1, d1));
}
let dist_p2 = (curve.p2 - local_point).hypot();
if dist_p2 < local_cp_tolerance {
return Some(VectorEditHit::ControlPoint {
shape_instance_id: shape.id,
curve_index: i,
point_index: 2,
});
}
// Check p2
let d2 = local_point.distance(edge.curve.p2);
if d2 < tolerance.control_point {
if best_cp.is_none() || d2 < best_cp.unwrap().2 {
best_cp = Some((edge_id, 2, d2));
}
}
}
// Priority 2: Vertices (anchor points)
let local_vertex_tolerance = tolerance.vertex * local_tolerance_factor;
for (i, vertex) in editable.vertices.iter().enumerate() {
let dist = (vertex.point - local_point).hypot();
if dist < local_vertex_tolerance {
return Some(VectorEditHit::Vertex {
shape_instance_id: shape.id,
vertex_index: i,
});
}
}
// Priority 3: Curves
let local_curve_tolerance = tolerance.curve * local_tolerance_factor;
for (i, curve) in editable.curves.iter().enumerate() {
let nearest = curve.nearest(local_point, 1e-6);
let nearest_point = curve.eval(nearest.t);
let dist = (nearest_point - local_point).hypot();
if dist < local_curve_tolerance {
return Some(VectorEditHit::Curve {
shape_instance_id: shape.id,
curve_index: i,
parameter_t: nearest.t,
});
}
}
// Priority 4: Fill
if shape.fill_color.is_some() && shape.path().contains(local_point) {
return Some(VectorEditHit::Fill {
shape_instance_id: shape.id,
});
if let Some((edge_id, point_index, _)) = best_cp {
return Some(VectorEditHit::ControlPoint { edge_id, point_index });
}
}
// 2. Vertices
let mut best_vertex: Option<(VertexId, f64)> = None;
for (i, vertex) in dcel.vertices.iter().enumerate() {
if vertex.deleted {
continue;
}
let dist = local_point.distance(vertex.position);
if dist < tolerance.vertex {
if best_vertex.is_none() || dist < best_vertex.unwrap().1 {
best_vertex = Some((VertexId(i as u32), dist));
}
}
}
if let Some((vertex_id, _)) = best_vertex {
return Some(VectorEditHit::Vertex { vertex_id });
}
// 3. Curves (edges)
let mut best_curve: Option<(EdgeId, f64, f64)> = None; // (edge_id, t, dist)
for (i, edge) in dcel.edges.iter().enumerate() {
if edge.deleted {
continue;
}
let nearest = edge.curve.nearest(local_point, 0.5);
let dist = nearest.distance_sq.sqrt();
if dist < tolerance.curve {
if best_curve.is_none() || dist < best_curve.unwrap().2 {
best_curve = Some((EdgeId(i as u32), nearest.t, dist));
}
}
}
if let Some((edge_id, parameter_t, _)) = best_curve {
return Some(VectorEditHit::Curve { edge_id, parameter_t });
}
// 4. Face hit testing skipped for now
None
}
@@ -447,65 +393,16 @@ mod tests {
#[test]
fn test_hit_test_simple_circle() {
let mut layer = VectorLayer::new("Test Layer");
let circle = Circle::new((100.0, 100.0), 50.0);
let path = circle.to_path(0.1);
let shape = Shape::new(path).with_fill(ShapeColor::rgb(255, 0, 0));
layer.add_shape_to_keyframe(shape, 0.0);
// Test hit inside circle
let hit = hit_test_layer(&layer, 0.0, Point::new(100.0, 100.0), 0.0, Affine::IDENTITY);
assert!(hit.is_some());
// Test miss outside circle
let miss = hit_test_layer(&layer, 0.0, Point::new(200.0, 200.0), 0.0, Affine::IDENTITY);
assert!(miss.is_none());
// TODO: DCEL - rewrite test
}
#[test]
fn test_hit_test_with_transform() {
let mut layer = VectorLayer::new("Test Layer");
let circle = Circle::new((0.0, 0.0), 50.0);
let path = circle.to_path(0.1);
let shape = Shape::new(path)
.with_fill(ShapeColor::rgb(255, 0, 0))
.with_position(100.0, 100.0);
layer.add_shape_to_keyframe(shape, 0.0);
// Test hit at translated position
let hit = hit_test_layer(&layer, 0.0, Point::new(100.0, 100.0), 0.0, Affine::IDENTITY);
assert!(hit.is_some());
// Test miss at origin (where shape is defined, but transform moves it)
let miss = hit_test_layer(&layer, 0.0, Point::new(0.0, 0.0), 0.0, Affine::IDENTITY);
assert!(miss.is_none());
// TODO: DCEL - rewrite test
}
#[test]
fn test_marquee_selection() {
let mut layer = VectorLayer::new("Test Layer");
let circle1 = Circle::new((50.0, 50.0), 20.0);
let shape1 = Shape::new(circle1.to_path(0.1)).with_fill(ShapeColor::rgb(255, 0, 0));
let circle2 = Circle::new((150.0, 150.0), 20.0);
let shape2 = Shape::new(circle2.to_path(0.1)).with_fill(ShapeColor::rgb(0, 255, 0));
layer.add_shape_to_keyframe(shape1, 0.0);
layer.add_shape_to_keyframe(shape2, 0.0);
// Marquee that contains both circles
let rect = Rect::new(0.0, 0.0, 200.0, 200.0);
let hits = hit_test_objects_in_rect(&layer, 0.0, rect, Affine::IDENTITY);
assert_eq!(hits.len(), 2);
// Marquee that contains only first circle
let rect = Rect::new(0.0, 0.0, 100.0, 100.0);
let hits = hit_test_objects_in_rect(&layer, 0.0, rect, Affine::IDENTITY);
assert_eq!(hits.len(), 1);
// TODO: DCEL - rewrite test
}
}