Implement shape tweens (same-topology lerp)
`tween_after == Shape` was stored on keyframes but never read. Now the render path morphs geometry across a shape-tween span: - VectorGraph::interpolated(other, t): same-topology lerp of vertex positions, edge curves, stroke widths and stroke/fill colours. Returns None when topology differs (counts, deleted flags, edge endpoints, fill boundaries), so the caller holds the source keyframe. - VectorLayer::tweened_graph_at(time): returns an owned morphed graph for a shape-tween span whose two keyframes share topology, else borrows the held keyframe. Editing still uses graph_at_time (the held keyframe). - Renderer (Vello + CPU paths) renders via tweened_graph_at. - SetTweenAction + wired the previously-stubbed "Add Shape Tween" menu. The typical workflow — keyframe, duplicate it (same topology), move vertices, Add Shape Tween — now morphs between the two. Non-matching topology falls back to a hold.
This commit is contained in:
@@ -417,6 +417,67 @@ impl VectorGraph {
|
||||
self.boundary_to_bezpath(&fill.boundary)
|
||||
}
|
||||
|
||||
/// Interpolate toward `other` by `t` ∈ [0,1] for a same-topology shape tween.
|
||||
///
|
||||
/// Returns `None` if the two graphs don't share identical topology — same vertex,
|
||||
/// edge and fill structure (counts, deleted flags, edge endpoints, fill boundaries).
|
||||
/// In that case the caller should hold the source keyframe instead of morphing.
|
||||
/// Vertex positions, edge curves, stroke widths and stroke/fill colours are lerped.
|
||||
pub fn interpolated(&self, other: &VectorGraph, t: f64) -> Option<VectorGraph> {
|
||||
if self.vertices.len() != other.vertices.len()
|
||||
|| self.edges.len() != other.edges.len()
|
||||
|| self.fills.len() != other.fills.len()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
for (a, b) in self.vertices.iter().zip(&other.vertices) {
|
||||
if a.deleted != b.deleted {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
for (a, b) in self.edges.iter().zip(&other.edges) {
|
||||
if a.deleted != b.deleted || a.vertices != b.vertices {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
for (a, b) in self.fills.iter().zip(&other.fills) {
|
||||
if a.deleted != b.deleted || a.boundary != b.boundary {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
let lf = |x: f64, y: f64| x + (y - x) * t;
|
||||
let lp = |p: Point, q: Point| Point::new(lf(p.x, q.x), lf(p.y, q.y));
|
||||
let lc = |a: Option<ShapeColor>, b: Option<ShapeColor>| match (a, b) {
|
||||
(Some(a), Some(b)) => {
|
||||
let c = |x: u8, y: u8| (lf(x as f64, y as f64)).round().clamp(0.0, 255.0) as u8;
|
||||
Some(ShapeColor::new(c(a.r, b.r), c(a.g, b.g), c(a.b, b.b), c(a.a, b.a)))
|
||||
}
|
||||
(a, _) => a,
|
||||
};
|
||||
|
||||
let mut g = self.clone();
|
||||
for (i, v) in g.vertices.iter_mut().enumerate() {
|
||||
v.position = lp(self.vertices[i].position, other.vertices[i].position);
|
||||
}
|
||||
for (i, e) in g.edges.iter_mut().enumerate() {
|
||||
let (a, b) = (self.edges[i].curve, other.edges[i].curve);
|
||||
e.curve = CubicBez::new(lp(a.p0, b.p0), lp(a.p1, b.p1), lp(a.p2, b.p2), lp(a.p3, b.p3));
|
||||
if let (Some(s), Some(sa), Some(sb)) = (
|
||||
e.stroke_style.as_mut(),
|
||||
self.edges[i].stroke_style.as_ref(),
|
||||
other.edges[i].stroke_style.as_ref(),
|
||||
) {
|
||||
s.width = lf(sa.width, sb.width);
|
||||
}
|
||||
e.stroke_color = lc(self.edges[i].stroke_color, other.edges[i].stroke_color);
|
||||
}
|
||||
for (i, f) in g.fills.iter_mut().enumerate() {
|
||||
f.color = lc(self.fills[i].color, other.fills[i].color);
|
||||
}
|
||||
Some(g)
|
||||
}
|
||||
|
||||
/// 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
|
||||
|
||||
@@ -12,3 +12,5 @@ mod gap_close;
|
||||
mod region;
|
||||
#[cfg(test)]
|
||||
mod region_cut_select;
|
||||
#[cfg(test)]
|
||||
mod tween;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
//! Tests for same-topology shape-tween interpolation (`VectorGraph::interpolated`).
|
||||
|
||||
use super::super::*;
|
||||
use kurbo::{CubicBez, Point};
|
||||
|
||||
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,
|
||||
)
|
||||
}
|
||||
|
||||
/// Triangle (3 verts, 3 edges, 1 fill) offset by (ox, oy).
|
||||
fn triangle(ox: f64, oy: f64) -> VectorGraph {
|
||||
let mut g = VectorGraph::new();
|
||||
let p = [
|
||||
Point::new(ox, oy),
|
||||
Point::new(ox + 100.0, oy),
|
||||
Point::new(ox + 50.0, oy + 100.0),
|
||||
];
|
||||
let v: Vec<_> = p.iter().map(|&pt| g.alloc_vertex(pt)).collect();
|
||||
let style = StrokeStyle { width: 1.0, ..Default::default() };
|
||||
let mut boundary = Vec::new();
|
||||
for i in 0..3 {
|
||||
let e = g.alloc_edge(
|
||||
line(p[i], p[(i + 1) % 3]),
|
||||
v[i],
|
||||
v[(i + 1) % 3],
|
||||
Some(style.clone()),
|
||||
Some(ShapeColor::rgb(0, 0, 0)),
|
||||
);
|
||||
boundary.push((e, Direction::Forward));
|
||||
}
|
||||
g.alloc_fill(boundary, ShapeColor::rgb(255, 0, 0), FillRule::NonZero);
|
||||
g
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interpolate_same_topology_lerps_positions() {
|
||||
let a = triangle(0.0, 0.0);
|
||||
let b = triangle(100.0, 50.0);
|
||||
|
||||
let mid = a.interpolated(&b, 0.5).expect("same topology should interpolate");
|
||||
// Vertex 0: (0,0) and (100,50) → (50,25). Curve endpoints follow.
|
||||
assert!((mid.vertices[0].position.x - 50.0).abs() < 1e-6);
|
||||
assert!((mid.vertices[0].position.y - 25.0).abs() < 1e-6);
|
||||
assert!((mid.edges[0].curve.p0.x - 50.0).abs() < 1e-6);
|
||||
|
||||
// Endpoints: t=0 is `a`, t=1 is `b`.
|
||||
assert!((a.interpolated(&b, 0.0).unwrap().vertices[0].position.x - 0.0).abs() < 1e-6);
|
||||
assert!((a.interpolated(&b, 1.0).unwrap().vertices[0].position.x - 100.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interpolate_lerps_fill_color() {
|
||||
let mut a = triangle(0.0, 0.0);
|
||||
let mut b = triangle(0.0, 0.0);
|
||||
a.fills[0].color = Some(ShapeColor::rgb(0, 0, 0));
|
||||
b.fills[0].color = Some(ShapeColor::rgb(100, 200, 40));
|
||||
let mid = a.interpolated(&b, 0.5).unwrap();
|
||||
let c = mid.fills[0].color.unwrap();
|
||||
assert_eq!((c.r, c.g, c.b), (50, 100, 20));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interpolate_topology_mismatch_returns_none() {
|
||||
let a = triangle(0.0, 0.0);
|
||||
let mut more_verts = triangle(0.0, 0.0);
|
||||
more_verts.alloc_vertex(Point::new(999.0, 999.0));
|
||||
assert!(a.interpolated(&more_verts, 0.5).is_none(), "different vertex count");
|
||||
|
||||
// Same counts but a moved edge endpoint (different vertices) is still a mismatch.
|
||||
let mut rewired = triangle(0.0, 0.0);
|
||||
rewired.edges[0].vertices = [VertexId(2), VertexId(1)];
|
||||
assert!(a.interpolated(&rewired, 0.5).is_none(), "different edge endpoints");
|
||||
}
|
||||
Reference in New Issue
Block a user