deckcraft_render/
morph_path.rs1use deckcraft_geom::Xfrm;
5use deckcraft_geom::morph::{Fade, GeometryMorph};
6use deckcraft_geom::preset::Geometry;
7use deckcraft_model::{Geom, Shape, ShapeId};
8use kurbo::Vec2;
9
10use crate::shape_geometry;
11
12#[derive(Clone, Debug, PartialEq)]
15pub struct PathMorph {
16 pub id: ShapeId,
17 pub from: Geom,
19 pub from_box: Xfrm,
21 pub to_box: Xfrm,
22 pub t: f64,
24}
25
26fn ratio(now: f64, own: f64) -> f64 {
28 let r = now / own;
29 if own.abs() > 1e-9 && r.is_finite() {
30 r
31 } else {
32 1.0
33 }
34}
35
36pub(crate) fn geometry(pm: &PathMorph, s: &Shape, w: f64, h: f64) -> (Geometry, Vec<Fade>) {
40 let a = shape_geometry(
41 &Shape {
42 geom: pm.from.clone(),
43 ..Shape::default()
44 },
45 pm.from_box.w,
46 pm.from_box.h,
47 );
48 let b = shape_geometry(s, pm.to_box.w, pm.to_box.h);
49 let scale = |x: Xfrm| Vec2::new(ratio(w, x.w), ratio(h, x.h));
50 GeometryMorph::new(&a, &b).at(pm.t, scale(pm.from_box), scale(pm.to_box))
51}