1use crate::SampleContext;
7use crate::animation::{sample_paint, sample_path, sample_point, sample_scalar, sample_vec2};
8use alpha2_geometry::kurbo::{Point, Vec2};
9use alpha2_geometry::{EvaluatedGeometry, EvaluatedItem, EvaluatedTransform};
10use alpha2_model::{
11 Animated, AnimatedPoint, EllipseGeometry, PathGeometry, PolystarGeometry, RectangleGeometry,
12 ShapeGeometry, ShapeItem, ShapeTransform,
13};
14use alpha2_time::MediaTime;
15use std::sync::Arc;
16
17#[derive(Clone, Debug, PartialEq)]
19pub struct ShapesPlan {
20 items: Vec<Planned>,
21 fixed: Option<Arc<[EvaluatedItem]>>,
23}
24
25#[derive(Clone, Debug, PartialEq)]
26enum Planned {
27 Fixed(EvaluatedItem),
28 Group {
30 items: Vec<Planned>,
31 fixed: Option<Arc<[EvaluatedItem]>>,
32 transform: ShapeTransform,
33 },
34 Live(ShapeItem),
35}
36
37impl ShapesPlan {
38 #[must_use]
40 pub fn new(items: &[ShapeItem]) -> Self {
41 let items: Vec<Planned> = items.iter().map(plan).collect();
42 let fixed = all_fixed(&items);
43 Self { items, fixed }
44 }
45
46 #[must_use]
48 pub fn evaluate(&self, time: MediaTime, aspect: f64) -> Arc<[EvaluatedItem]> {
49 if let Some(fixed) = &self.fixed {
50 return Arc::clone(fixed);
51 }
52 evaluate_planned(&self.items, time, aspect)
53 }
54
55 #[must_use]
57 pub fn is_animated(&self) -> bool {
58 self.fixed.is_none()
59 }
60}
61
62fn plan(item: &ShapeItem) -> Planned {
63 if !item.is_animated() {
64 return Planned::Fixed(evaluate_item(item, MediaTime::ZERO, 1.0));
65 }
66 match item {
67 ShapeItem::Group { items, transform } => {
68 let items: Vec<Planned> = items.iter().map(plan).collect();
69 let fixed = all_fixed(&items);
70 Planned::Group {
71 items: if fixed.is_some() { Vec::new() } else { items },
72 fixed,
73 transform: transform.clone(),
74 }
75 }
76 _ => Planned::Live(item.clone()),
77 }
78}
79
80fn all_fixed(items: &[Planned]) -> Option<Arc<[EvaluatedItem]>> {
81 items
82 .iter()
83 .map(|item| match item {
84 Planned::Fixed(item) => Some(item.clone()),
85 _ => None,
86 })
87 .collect::<Option<Vec<_>>>()
88 .map(Arc::from)
89}
90
91fn evaluate_planned(items: &[Planned], time: MediaTime, aspect: f64) -> Arc<[EvaluatedItem]> {
92 items
93 .iter()
94 .map(|item| match item {
95 Planned::Fixed(item) => item.clone(),
96 Planned::Group {
97 items,
98 fixed,
99 transform,
100 } => EvaluatedItem::Group {
101 items: fixed
102 .clone()
103 .unwrap_or_else(|| evaluate_planned(items, time, aspect)),
104 transform: evaluate_transform(transform, time, aspect),
105 },
106 Planned::Live(item) => evaluate_item(item, time, aspect),
107 })
108 .collect()
109}
110
111#[must_use]
113pub fn evaluate_items(items: &[ShapeItem], time: MediaTime, aspect: f64) -> Arc<[EvaluatedItem]> {
114 items
115 .iter()
116 .map(|item| evaluate_item(item, time, aspect))
117 .collect()
118}
119
120const NO_DATA: &SampleContext<'static> = &SampleContext::NONE;
122
123fn scalar(animation: &Animated<f32>, time: MediaTime, context: &SampleContext<'_>) -> f64 {
124 f64::from(sample_scalar(animation, time, context))
125}
126
127fn point(
128 animation: &AnimatedPoint,
129 time: MediaTime,
130 aspect: f64,
131 context: &SampleContext<'_>,
132) -> Point {
133 let value = sample_point(animation, time, aspect, context);
134 Point::new(f64::from(value.x), f64::from(value.y))
135}
136
137fn vector(
138 animation: &Animated<alpha2_model::Vec2>,
139 time: MediaTime,
140 context: &SampleContext<'_>,
141) -> Vec2 {
142 let value = sample_vec2(animation, time, context);
143 Vec2::new(f64::from(value.x), f64::from(value.y))
144}
145
146#[must_use]
149pub fn evaluate_item(item: &ShapeItem, time: MediaTime, aspect: f64) -> EvaluatedItem {
150 let context = NO_DATA;
151 let unit = |value: f64| value.clamp(0.0, 1.0);
152 match item {
153 ShapeItem::Group { items, transform } => EvaluatedItem::Group {
154 items: evaluate_items(items, time, aspect),
155 transform: evaluate_transform(transform, time, aspect),
156 },
157 ShapeItem::Rectangle(geometry) => EvaluatedItem::Geometry(evaluate_geometry(
158 &ShapeGeometry::Rectangle(geometry.clone()),
159 time,
160 aspect,
161 context,
162 )),
163 ShapeItem::Ellipse(geometry) => {
164 EvaluatedItem::Geometry(ellipse(geometry, time, aspect, context))
165 }
166 ShapeItem::Polystar(geometry) => {
167 EvaluatedItem::Geometry(polystar(geometry, time, aspect, context))
168 }
169 ShapeItem::Path(geometry) => EvaluatedItem::Geometry(path(geometry, time)),
170 ShapeItem::Fill {
171 paint,
172 opacity,
173 rule,
174 } => EvaluatedItem::Fill {
175 paint: sample_paint(paint, time),
176 opacity: unit(scalar(opacity, time, context)),
177 rule: *rule,
178 },
179 ShapeItem::Stroke(stroke) => EvaluatedItem::Stroke {
180 paint: sample_paint(&stroke.paint, time),
181 width: scalar(&stroke.width, time, context).max(0.0),
182 opacity: unit(scalar(&stroke.opacity, time, context)),
183 join: stroke.join,
184 miter_limit: f64::from(stroke.miter_limit),
185 cap: stroke.cap,
186 dash: stroke.dash.as_ref().map_or_else(Vec::new, |dash| {
187 dash.pattern
188 .iter()
189 .map(|entry| scalar(entry, time, context).max(0.0))
190 .collect()
191 }),
192 dash_offset: stroke
193 .dash
194 .as_ref()
195 .map_or(0.0, |dash| scalar(&dash.offset, time, context)),
196 },
197 ShapeItem::Trim(trim) => EvaluatedItem::Trim {
198 start: unit(scalar(&trim.start, time, context)),
199 end: unit(scalar(&trim.end, time, context)),
200 offset: scalar(&trim.offset, time, context),
201 mode: trim.mode,
202 },
203 }
204}
205
206#[must_use]
208pub fn evaluate_geometry(
209 geometry: &ShapeGeometry,
210 time: MediaTime,
211 aspect: f64,
212 context: &SampleContext<'_>,
213) -> EvaluatedGeometry {
214 match geometry {
215 ShapeGeometry::Rectangle(RectangleGeometry {
216 center,
217 size,
218 corner_radius,
219 rotation_degrees,
220 }) => EvaluatedGeometry::Rectangle {
221 center: point(center, time, aspect, context),
222 size: vector(size, time, context),
223 corner_radius: scalar(corner_radius, time, context).max(0.0),
224 rotation_degrees: scalar(rotation_degrees, time, context),
225 },
226 ShapeGeometry::Ellipse(geometry) => ellipse(geometry, time, aspect, context),
227 ShapeGeometry::Polystar(geometry) => polystar(geometry, time, aspect, context),
228 ShapeGeometry::Path(geometry) => path(geometry, time),
229 }
230}
231
232fn ellipse(
233 geometry: &EllipseGeometry,
234 time: MediaTime,
235 aspect: f64,
236 context: &SampleContext<'_>,
237) -> EvaluatedGeometry {
238 EvaluatedGeometry::Ellipse {
239 center: point(&geometry.center, time, aspect, context),
240 size: vector(&geometry.size, time, context),
241 rotation_degrees: scalar(&geometry.rotation_degrees, time, context),
242 }
243}
244
245fn polystar(
246 geometry: &PolystarGeometry,
247 time: MediaTime,
248 aspect: f64,
249 context: &SampleContext<'_>,
250) -> EvaluatedGeometry {
251 EvaluatedGeometry::Polystar {
252 kind: geometry.kind,
253 center: point(&geometry.center, time, aspect, context),
254 points: scalar(&geometry.points, time, context).clamp(
255 f64::from(alpha2_model::MIN_POLYSTAR_POINTS),
256 f64::from(alpha2_model::MAX_POLYSTAR_POINTS),
257 ),
258 outer_radius: scalar(&geometry.outer_radius, time, context).max(0.0),
259 inner_radius: scalar(&geometry.inner_radius, time, context).max(0.0),
260 outer_roundness: scalar(&geometry.outer_roundness, time, context).clamp(0.0, 1.0),
261 inner_roundness: scalar(&geometry.inner_roundness, time, context).clamp(0.0, 1.0),
262 rotation_degrees: scalar(&geometry.rotation_degrees, time, context),
263 }
264}
265
266fn path(geometry: &PathGeometry, time: MediaTime) -> EvaluatedGeometry {
267 EvaluatedGeometry::Path(Arc::new(sample_path(&geometry.path, time)))
268}
269
270fn evaluate_transform(
271 transform: &ShapeTransform,
272 time: MediaTime,
273 aspect: f64,
274) -> EvaluatedTransform {
275 let context = NO_DATA;
276 let anchor = vector(&transform.anchor, time, context);
277 EvaluatedTransform {
278 anchor: Point::new(anchor.x, anchor.y),
279 position: point(&transform.position, time, aspect, context),
280 scale: vector(&transform.scale, time, context),
281 rotation_degrees: scalar(&transform.rotation_degrees, time, context),
282 opacity: scalar(&transform.opacity, time, context).clamp(0.0, 1.0),
283 }
284}
285
286#[cfg(test)]
287#[path = "shapes_tests.rs"]
288mod tests;