Work on region select

This commit is contained in:
Skyler Lehmkuhl
2026-02-21 06:04:54 -05:00
parent 469849a0d6
commit 2222e68a3e
16 changed files with 2163 additions and 6 deletions
@@ -31,6 +31,7 @@ pub mod remove_shapes;
pub mod set_keyframe;
pub mod group_shapes;
pub mod convert_to_movie_clip;
pub mod region_split;
pub use add_clip_instance::AddClipInstanceAction;
pub use add_effect::AddEffectAction;
@@ -60,3 +61,4 @@ pub use remove_shapes::RemoveShapesAction;
pub use set_keyframe::SetKeyframeAction;
pub use group_shapes::GroupAction;
pub use convert_to_movie_clip::ConvertToMovieClipAction;
pub use region_split::RegionSplitAction;
@@ -0,0 +1,119 @@
//! Region split action
//!
//! Commits a temporary region-based shape split permanently.
//! Replaces original shapes with their inside and outside portions.
use crate::action::Action;
use crate::document::Document;
use crate::layer::AnyLayer;
use crate::shape::Shape;
use uuid::Uuid;
use vello::kurbo::BezPath;
/// One shape split entry for the action
#[derive(Clone, Debug)]
struct SplitEntry {
/// The original shape (for rollback)
original_shape: Shape,
/// The inside portion shape
inside_shape: Shape,
/// The outside portion shape
outside_shape: Shape,
}
/// Action that commits a region split — replacing original shapes with
/// their inside and outside portions.
pub struct RegionSplitAction {
layer_id: Uuid,
time: f64,
splits: Vec<SplitEntry>,
}
impl RegionSplitAction {
/// Create a new region split action.
///
/// Each tuple is (original_shape, inside_path, inside_id, outside_path, outside_id).
pub fn new(
layer_id: Uuid,
time: f64,
split_data: Vec<(Shape, BezPath, Uuid, BezPath, Uuid)>,
) -> Self {
let splits = split_data
.into_iter()
.map(|(original, inside_path, inside_id, outside_path, outside_id)| {
let mut inside_shape = original.clone();
inside_shape.id = inside_id;
inside_shape.versions[0].path = inside_path;
let mut outside_shape = original.clone();
outside_shape.id = outside_id;
outside_shape.versions[0].path = outside_path;
SplitEntry {
original_shape: original,
inside_shape,
outside_shape,
}
})
.collect();
Self {
layer_id,
time,
splits,
}
}
}
impl Action for RegionSplitAction {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
let vector_layer = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Not a vector layer".to_string()),
};
for split in &self.splits {
// Remove original
vector_layer.remove_shape_from_keyframe(&split.original_shape.id, self.time);
// Add inside and outside portions
vector_layer.add_shape_to_keyframe(split.inside_shape.clone(), self.time);
vector_layer.add_shape_to_keyframe(split.outside_shape.clone(), self.time);
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let layer = document
.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
let vector_layer = match layer {
AnyLayer::Vector(vl) => vl,
_ => return Err("Not a vector layer".to_string()),
};
for split in &self.splits {
// Remove inside and outside portions
vector_layer.remove_shape_from_keyframe(&split.inside_shape.id, self.time);
vector_layer.remove_shape_from_keyframe(&split.outside_shape.id, self.time);
// Restore original
vector_layer.add_shape_to_keyframe(split.original_shape.clone(), self.time);
}
Ok(())
}
fn description(&self) -> String {
let count = self.splits.len();
if count == 1 {
"Region split shape".to_string()
} else {
format!("Region split {} shapes", count)
}
}
}
@@ -5,10 +5,11 @@
use crate::clip::ClipInstance;
use crate::layer::VectorLayer;
use crate::region_select;
use crate::shape::Shape;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use vello::kurbo::{Affine, Point, Rect, Shape as KurboShape};
use vello::kurbo::{Affine, BezPath, Point, Rect, Shape as KurboShape};
/// Result of a hit test operation
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -119,6 +120,66 @@ pub fn hit_test_objects_in_rect(
hits
}
/// Classification of shapes relative to a clipping region
#[derive(Debug, Clone)]
pub struct ShapeRegionClassification {
/// Shapes entirely inside the region
pub fully_inside: Vec<Uuid>,
/// Shapes whose paths cross the region boundary
pub intersecting: Vec<Uuid>,
/// Shapes with no overlap with the region
pub fully_outside: Vec<Uuid>,
}
/// Classify shapes in a layer relative to a clipping region.
///
/// Uses bounding box fast-rejection, then checks path-region intersection
/// and containment for accurate classification.
pub fn classify_shapes_by_region(
layer: &VectorLayer,
time: f64,
region: &BezPath,
parent_transform: Affine,
) -> ShapeRegionClassification {
let mut result = ShapeRegionClassification {
fully_inside: Vec::new(),
intersecting: Vec::new(),
fully_outside: Vec::new(),
};
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);
}
}
result
}
/// Get the bounding box of a shape in screen space
pub fn get_shape_bounds(
shape: &Shape,
@@ -489,14 +489,14 @@ impl VectorLayer {
/// Add a shape to the keyframe at the given time.
/// Creates a keyframe if none exists at that time.
pub(crate) fn add_shape_to_keyframe(&mut self, shape: Shape, time: f64) {
pub fn add_shape_to_keyframe(&mut self, shape: Shape, time: f64) {
let kf = self.ensure_keyframe_at(time);
kf.shapes.push(shape);
}
/// Remove a shape from the keyframe at the given time.
/// Returns the removed shape if found.
pub(crate) fn remove_shape_from_keyframe(&mut self, shape_id: &Uuid, time: f64) -> Option<Shape> {
pub fn remove_shape_from_keyframe(&mut self, shape_id: &Uuid, time: f64) -> Option<Shape> {
let kf = self.keyframe_at_mut(time)?;
let idx = kf.shapes.iter().position(|s| &s.id == shape_id)?;
Some(kf.shapes.remove(idx))
@@ -42,3 +42,4 @@ pub mod file_types;
pub mod file_io;
pub mod export;
pub mod clipboard;
pub mod region_select;
File diff suppressed because it is too large Load Diff
@@ -2,8 +2,10 @@
//!
//! Tracks selected shape instances, clip instances, and shapes for editing operations.
use crate::shape::Shape;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use vello::kurbo::BezPath;
/// Selection state for the editor
///
@@ -212,6 +214,43 @@ impl Selection {
}
}
/// Represents a temporary region-based split of shapes.
///
/// When a region select is active, shapes that cross the region boundary
/// are temporarily split into "inside" and "outside" parts. The inside
/// parts are selected. If the user performs an operation, the split is
/// committed; if they deselect, the original shapes are restored.
#[derive(Clone, Debug)]
pub struct RegionSelection {
/// The clipping region as a closed BezPath (polygon or rect)
pub region_path: BezPath,
/// Layer containing the affected shapes
pub layer_id: Uuid,
/// Keyframe time
pub time: f64,
/// Per-shape split results
pub splits: Vec<ShapeSplit>,
/// Shape IDs that were fully inside the region (not split, just selected)
pub fully_inside_ids: Vec<Uuid>,
/// Whether the split has been committed (via an operation on the selection)
pub committed: bool,
}
/// One shape's split result from a region selection
#[derive(Clone, Debug)]
pub struct ShapeSplit {
/// The original shape (stored for reverting)
pub original_shape: Shape,
/// UUID for the "inside" portion shape
pub inside_shape_id: Uuid,
/// The clipped path inside the region
pub inside_path: BezPath,
/// UUID for the "outside" portion shape
pub outside_shape_id: Uuid,
/// The clipped path outside the region
pub outside_path: BezPath,
}
#[cfg(test)]
mod tests {
use super::*;
@@ -33,6 +33,23 @@ pub enum Tool {
BezierEdit,
/// Text tool - add and edit text
Text,
/// Region select tool - select sub-regions of shapes by clipping
RegionSelect,
}
/// Region select mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RegionSelectMode {
/// Rectangular region selection
Rectangle,
/// Freehand lasso region selection
Lasso,
}
impl Default for RegionSelectMode {
fn default() -> Self {
Self::Rectangle
}
}
/// Tool state tracking for interactive operations
@@ -117,6 +134,17 @@ pub enum ToolState {
parameter_t: f64, // Parameter where the drag started (0.0-1.0)
},
/// Drawing a region selection rectangle
RegionSelectingRect {
start: Point,
current: Point,
},
/// Drawing a freehand lasso region selection
RegionSelectingLasso {
points: Vec<Point>,
},
/// Editing a control point (BezierEdit tool only)
EditingControlPoint {
shape_id: Uuid, // Which shape is being edited
@@ -179,6 +207,7 @@ impl Tool {
Tool::Polygon => "Polygon",
Tool::BezierEdit => "Bezier Edit",
Tool::Text => "Text",
Tool::RegionSelect => "Region Select",
}
}
@@ -196,6 +225,7 @@ impl Tool {
Tool::Polygon => "polygon.svg",
Tool::BezierEdit => "bezier_edit.svg",
Tool::Text => "text.svg",
Tool::RegionSelect => "region_select.svg",
}
}
@@ -213,6 +243,7 @@ impl Tool {
Tool::Polygon,
Tool::BezierEdit,
Tool::Text,
Tool::RegionSelect,
]
}
@@ -230,6 +261,7 @@ impl Tool {
Tool::Polygon => "G",
Tool::BezierEdit => "A",
Tool::Text => "T",
Tool::RegionSelect => "S",
}
}
}
@@ -0,0 +1,201 @@
use lightningbeam_core::region_select::*;
use vello::kurbo::{BezPath, Point, Rect, Shape};
#[test]
fn debug_clip_rect_corner() {
// Rectangle from (0,0) to (200,200)
let mut subject = BezPath::new();
subject.move_to(Point::new(0.0, 0.0));
subject.line_to(Point::new(200.0, 0.0));
subject.line_to(Point::new(200.0, 200.0));
subject.line_to(Point::new(0.0, 200.0));
subject.close_path();
// Region: upper-right corner, from (100,0) to (300,100)
// This extends beyond the subject on the right and top
let region = rect_to_path(Rect::new(100.0, 0.0, 300.0, 100.0));
println!("Subject path: {:?}", subject);
println!("Region path: {:?}", region);
let result = clip_path_to_region(&subject, &region);
println!("Inside path: {:?}", result.inside);
println!("Outside path: {:?}", result.outside);
let inside_bb = result.inside.bounding_box();
println!("Inside bounding box: {:?}", inside_bb);
// Expected inside: a rectangle from (100,0) to (200,100)
assert!((inside_bb.x0 - 100.0).abs() < 2.0,
"inside x0 should be ~100, got {}", inside_bb.x0);
assert!((inside_bb.y0 - 0.0).abs() < 2.0,
"inside y0 should be ~0, got {}", inside_bb.y0);
assert!((inside_bb.x1 - 200.0).abs() < 2.0,
"inside x1 should be ~200, got {}", inside_bb.x1);
assert!((inside_bb.y1 - 100.0).abs() < 2.0,
"inside y1 should be ~100, got {}", inside_bb.y1);
// Verify the inside path has the right shape by checking it has ~5 elements
// (MoveTo, 3x LineTo, ClosePath) for a rectangle
let elem_count = result.inside.elements().len();
println!("Inside element count: {}", elem_count);
// Print each element
for (i, el) in result.inside.elements().iter().enumerate() {
println!(" inside[{}]: {:?}", i, el);
}
for (i, el) in result.outside.elements().iter().enumerate() {
println!(" outside[{}]: {:?}", i, el);
}
}
#[test]
fn debug_clip_partial_overlap() {
// When the region is fully contained inside the subject (no edge crossings),
// the path-clipping approach cannot split the subject since no path segments
// cross the region boundary. This is correct — the selection system handles
// this case by classifying the shape as "fully_inside" via hit_test, not
// via path clipping.
let mut subject = BezPath::new();
subject.move_to(Point::new(0.0, 0.0));
subject.line_to(Point::new(200.0, 0.0));
subject.line_to(Point::new(200.0, 200.0));
subject.line_to(Point::new(0.0, 200.0));
subject.close_path();
let region = rect_to_path(Rect::new(50.0, 50.0, 150.0, 150.0));
let result = clip_path_to_region(&subject, &region);
// No intersections found → entire subject classified as outside
assert!(result.inside.elements().is_empty(),
"No edge crossings → inside should be empty (handled by hit_test instead)");
assert!(!result.outside.elements().is_empty());
}
#[test]
fn debug_lasso_extending_beyond_subject() {
// Rectangle subject from (0,0) to (100,100)
let mut subject = BezPath::new();
subject.move_to(Point::new(0.0, 0.0));
subject.line_to(Point::new(100.0, 0.0));
subject.line_to(Point::new(100.0, 100.0));
subject.line_to(Point::new(0.0, 100.0));
subject.close_path();
// Lasso region that extends beyond the subject: a triangle
// covering (20,20) to (150,20) to (80,120)
// This extends beyond the right and bottom of the rectangle
let mut lasso = BezPath::new();
lasso.move_to(Point::new(20.0, 20.0));
lasso.line_to(Point::new(150.0, 20.0));
lasso.line_to(Point::new(80.0, 120.0));
lasso.close_path();
let result = clip_path_to_region(&subject, &lasso);
// The inside should be ONLY the intersection of the rectangle and lasso.
// It should NOT extend beyond the rectangle's bounds.
let inside_bb = result.inside.bounding_box();
println!("Lasso extending beyond: inside bb = {:?}", inside_bb);
for (i, el) in result.inside.elements().iter().enumerate() {
println!(" inside[{}]: {:?}", i, el);
}
// The inside must be contained within the subject rectangle
assert!(inside_bb.x0 >= -1.0, "inside x0={} should be >= 0", inside_bb.x0);
assert!(inside_bb.y0 >= -1.0, "inside y0={} should be >= 0", inside_bb.y0);
assert!(inside_bb.x1 <= 101.0, "inside x1={} should be <= 100", inside_bb.x1);
assert!(inside_bb.y1 <= 101.0, "inside y1={} should be <= 100", inside_bb.y1);
}
#[test]
fn debug_lasso_splits_remainder_into_two() {
// Rectangle subject from (0,0) to (200,200)
let mut subject = BezPath::new();
subject.move_to(Point::new(0.0, 0.0));
subject.line_to(Point::new(200.0, 0.0));
subject.line_to(Point::new(200.0, 200.0));
subject.line_to(Point::new(0.0, 200.0));
subject.close_path();
// Lasso that cuts across the rectangle, with vertices clearly NOT on
// the subject boundary. The lasso is a diamond that extends beyond
// the rect on top and right, splitting the remainder into two pieces:
// upper-left and lower-right.
let mut lasso = BezPath::new();
lasso.move_to(Point::new(-10.0, 100.0)); // left of rect
lasso.line_to(Point::new(100.0, -60.0)); // above rect
lasso.line_to(Point::new(260.0, 100.0)); // right of rect
lasso.line_to(Point::new(100.0, 210.0)); // below rect
lasso.close_path();
let result = clip_path_to_region(&subject, &lasso);
let inside_bb = result.inside.bounding_box();
println!("Lasso splits remainder: inside bb = {:?}", inside_bb);
for (i, el) in result.inside.elements().iter().enumerate() {
println!(" inside[{}]: {:?}", i, el);
}
// The inside must be contained within the subject rectangle bounds
assert!(inside_bb.x0 >= -1.0,
"inside x0={} should be >= 0 (must not extend left of subject)", inside_bb.x0);
assert!(inside_bb.y0 >= -1.0,
"inside y0={} should be >= 0 (must not extend above subject)", inside_bb.y0);
assert!(inside_bb.x1 <= 201.0,
"inside x1={} should be <= 200 (must not extend right of subject)", inside_bb.x1);
assert!(inside_bb.y1 <= 201.0,
"inside y1={} should be <= 200 (must not extend below subject)", inside_bb.y1);
// The outside (remainder) must also be within subject bounds
let outside_bb = result.outside.bounding_box();
println!("Lasso splits remainder: outside bb = {:?}", outside_bb);
for (i, el) in result.outside.elements().iter().enumerate() {
println!(" outside[{}]: {:?}", i, el);
}
assert!(outside_bb.x1 <= 201.0,
"outside x1={} must not extend right of subject (no lasso fill!)", outside_bb.x1);
assert!(outside_bb.y0 >= -1.0,
"outside y0={} must not extend above subject (no lasso fill!)", outside_bb.y0);
// The outside should have multiple separate sub-paths (multiple ClosePath elements)
// instead of one giant path that includes the lasso area
let close_count = result.outside.elements().iter()
.filter(|el| matches!(el, vello::kurbo::PathEl::ClosePath))
.count();
println!("Outside close_path count: {}", close_count);
assert!(close_count >= 2,
"Outside should be split into separate sub-paths, got {} ClosePaths", close_count);
}
#[test]
fn debug_clip_outside_shape_correct() {
// Test the exact scenario the user reported: outside shape should
// correctly include boundary walk points (no diagonal shortcuts)
let mut subject = BezPath::new();
subject.move_to(Point::new(0.0, 0.0));
subject.line_to(Point::new(200.0, 0.0));
subject.line_to(Point::new(200.0, 200.0));
subject.line_to(Point::new(0.0, 200.0));
subject.close_path();
let region = rect_to_path(Rect::new(100.0, 0.0, 300.0, 100.0));
let result = clip_path_to_region(&subject, &region);
// Outside should be an L-shape: (0,0)-(100,0)-(100,100)-(200,100)-(200,200)-(0,200)
let outside_bb = result.outside.bounding_box();
assert!((outside_bb.x0 - 0.0).abs() < 2.0);
assert!((outside_bb.y0 - 0.0).abs() < 2.0);
assert!((outside_bb.x1 - 200.0).abs() < 2.0);
assert!((outside_bb.y1 - 200.0).abs() < 2.0);
// Verify the path includes (200,100) — the critical boundary walk point
let has_200_100 = result.outside.elements().iter().any(|el| {
match *el {
vello::kurbo::PathEl::LineTo(p) => (p.x - 200.0).abs() < 1.0 && (p.y - 100.0).abs() < 1.0,
_ => false,
}
});
assert!(has_200_100, "Outside path must include point (200,100) from boundary walk");
}
@@ -0,0 +1,149 @@
use lightningbeam_core::region_select::*;
use vello::kurbo::{BezPath, Point, Rect, Shape};
#[test]
fn test_rect_to_path() {
let rect = Rect::new(10.0, 20.0, 100.0, 200.0);
let path = rect_to_path(rect);
assert!(path.elements().len() >= 5);
}
#[test]
fn test_lasso_to_path() {
let points = vec![
Point::new(0.0, 0.0),
Point::new(100.0, 0.0),
Point::new(100.0, 100.0),
Point::new(0.0, 100.0),
];
let path = lasso_to_path(&points);
assert!(path.elements().len() >= 5);
}
#[test]
fn test_clip_rect_corner() {
// Rectangle from (0,0) to (100,100)
let mut subject = BezPath::new();
subject.move_to(Point::new(0.0, 0.0));
subject.line_to(Point::new(100.0, 0.0));
subject.line_to(Point::new(100.0, 100.0));
subject.line_to(Point::new(0.0, 100.0));
subject.close_path();
// Clip to upper-right corner: region covers (50,0) to (150,50)
let region = rect_to_path(Rect::new(50.0, 0.0, 150.0, 50.0));
let result = clip_path_to_region(&subject, &region);
// Inside should have elements (the upper-right portion)
assert!(
!result.inside.elements().is_empty(),
"inside path should not be empty"
);
// Outside should have elements (the rest of the rectangle)
assert!(
!result.outside.elements().is_empty(),
"outside path should not be empty"
);
// The inside portion should be a roughly rectangular region
// Its bounding box should be approximately (50,0)-(100,50)
let inside_bb = result.inside.bounding_box();
assert!(
(inside_bb.x0 - 50.0).abs() < 2.0,
"inside x0 should be ~50, got {}",
inside_bb.x0
);
assert!(
(inside_bb.y0 - 0.0).abs() < 2.0,
"inside y0 should be ~0, got {}",
inside_bb.y0
);
assert!(
(inside_bb.x1 - 100.0).abs() < 2.0,
"inside x1 should be ~100, got {}",
inside_bb.x1
);
assert!(
(inside_bb.y1 - 50.0).abs() < 2.0,
"inside y1 should be ~50, got {}",
inside_bb.y1
);
}
#[test]
fn test_clip_fully_inside() {
let mut path = BezPath::new();
path.move_to(Point::new(20.0, 20.0));
path.line_to(Point::new(80.0, 20.0));
path.line_to(Point::new(80.0, 80.0));
path.line_to(Point::new(20.0, 80.0));
path.close_path();
let region = rect_to_path(Rect::new(0.0, 0.0, 100.0, 100.0));
let result = clip_path_to_region(&path, &region);
assert!(!result.inside.elements().is_empty());
assert!(result.outside.elements().is_empty());
}
#[test]
fn test_clip_fully_outside() {
let mut path = BezPath::new();
path.move_to(Point::new(200.0, 200.0));
path.line_to(Point::new(300.0, 200.0));
path.line_to(Point::new(300.0, 300.0));
path.close_path();
let region = rect_to_path(Rect::new(0.0, 0.0, 100.0, 100.0));
let result = clip_path_to_region(&path, &region);
assert!(result.inside.elements().is_empty());
assert!(!result.outside.elements().is_empty());
}
#[test]
fn test_path_intersects_region() {
let mut path = BezPath::new();
path.move_to(Point::new(-50.0, 50.0));
path.line_to(Point::new(150.0, 50.0));
let region = rect_to_path(Rect::new(0.0, 0.0, 100.0, 100.0));
assert!(path_intersects_region(&path, &region));
}
#[test]
fn test_path_fully_inside() {
let mut path = BezPath::new();
path.move_to(Point::new(20.0, 20.0));
path.line_to(Point::new(80.0, 20.0));
path.line_to(Point::new(80.0, 80.0));
path.close_path();
let region = rect_to_path(Rect::new(0.0, 0.0, 100.0, 100.0));
assert!(path_fully_inside_region(&path, &region));
assert!(!path_intersects_region(&path, &region));
}
#[test]
fn test_clip_horizontal_line_crossing() {
// A horizontal line crossing through a region
let mut subject = BezPath::new();
subject.move_to(Point::new(-50.0, 50.0));
subject.line_to(Point::new(150.0, 50.0));
let region = rect_to_path(Rect::new(0.0, 0.0, 100.0, 100.0));
let result = clip_path_to_region(&subject, &region);
// Inside should be the segment from x=0 to x=100 at y=50
let inside_bb = result.inside.bounding_box();
assert!(
(inside_bb.x0 - 0.0).abs() < 2.0,
"inside x0 should be ~0, got {}",
inside_bb.x0
);
assert!(
(inside_bb.x1 - 100.0).abs() < 2.0,
"inside x1 should be ~100, got {}",
inside_bb.x1
);
}