]> git.tdb.fi Git - libs/gl.git/blob - source/animation.cpp
Refactor KeyFrame ownership management in Animation
[libs/gl.git] / source / animation.cpp
1 #include <cmath>
2 #include <msp/core/maputils.h>
3 #include <msp/datafile/collection.h>
4 #include <msp/interpolate/bezierspline.h>
5 #include "animation.h"
6 #include "animationeventobserver.h"
7 #include "armature.h"
8 #include "error.h"
9 #include "pose.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Animation::Animation():
17         armature(0),
18         looping(false)
19 { }
20
21 // Avoid synthesizing ~RefPtr in files including animation.h
22 Animation::~Animation()
23 { }
24
25 void Animation::set_armature(const Armature &a)
26 {
27         if(!keyframes.empty() && &a!=armature)
28                 throw invalid_operation("Animation::set_armature");
29         armature = &a;
30 }
31
32 unsigned Animation::get_slot_for_uniform(const string &n) const
33 {
34         for(unsigned i=0; i<uniforms.size(); ++i)
35                 if(uniforms[i].name==n)
36                         return i;
37         throw key_error(n);
38 }
39
40 const string &Animation::get_uniform_name(unsigned i) const
41 {
42         if(i>=uniforms.size())
43                 throw out_of_range("Animation::get_uniform_name");
44         return uniforms[i].name;
45 }
46
47 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf)
48 {
49         add_keyframe(t, &kf, false, false);
50         create_curves();
51 }
52
53 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
54 {
55         add_keyframe(t, kf, slope, slope);
56 }
57
58 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float, float)
59 {
60         add_keyframe(t, kf);
61 }
62
63 void Animation::add_control_keyframe(const KeyFrame &kf)
64 {
65         if(keyframes.empty())
66                 throw invalid_operation("Animation::add_control_keyframe");
67
68         add_keyframe(keyframes.back().time, &kf, true, false);
69 }
70
71 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool c, bool owned)
72 {
73         if(c && keyframes.empty())
74                 throw invalid_argument("Animation::add_keyframe");
75         if(keyframes.empty() && t!=Time::zero)
76                 throw invalid_argument("Animation::add_keyframe");
77         if(!keyframes.empty() && t<keyframes.back().time)
78                 throw invalid_argument("Animation::add_keyframe");
79         if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
80                 throw invalid_argument("Animation::add_keyframe");
81
82         const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
83         for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
84         {
85                 KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
86                 if(j!=kf_uniforms.end() && j->second.size!=i->size)
87                         throw invalid_argument("Animation::add_keyframe");
88         }
89
90         if(kf->get_pose() && !armature)
91                 armature = kf->get_pose()->get_armature();
92
93         TimedKeyFrame tkf;
94         tkf.time = t;
95         tkf.keyframe = kf;
96         if(!owned)
97                 tkf.keyframe.keep();
98         tkf.control = c;
99
100         keyframes.push_back(tkf);
101
102         for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
103         {
104                 bool found = false;
105                 for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
106                         found = (j->name==i->first);
107
108                 if(!found)
109                         uniforms.push_back(UniformInfo(i->first, i->second.size));
110         }
111 }
112
113 void Animation::create_curves()
114 {
115         for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
116                 delete *i;
117         curves.clear();
118
119         curves.reserve(3+uniforms.size());
120         create_curve<3>(POSITION, &extract_position);
121         create_curve<3>(EULER, &extract_euler);
122         create_curve<3>(SCALE, &extract_scale);
123
124         for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
125         {
126                 if(i->size==1)
127                         create_curve<1>(UNIFORM, ExtractUniform<1>(i->name));
128                 else if(i->size==2)
129                         create_curve<2>(UNIFORM, ExtractUniform<2>(i->name));
130                 else if(i->size==3)
131                         create_curve<3>(UNIFORM, ExtractUniform<3>(i->name));
132                 else if(i->size==4)
133                         create_curve<4>(UNIFORM, ExtractUniform<4>(i->name));
134         }
135 }
136
137 template<unsigned N, typename T>
138 void Animation::create_curve(CurveTarget target, const T &extract)
139 {
140         typedef typename ValueCurve<N>::Knot Knot;
141
142         vector<Knot> knots;
143         unsigned n_control = 0;
144         for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
145         {
146                 if(i->control && knots.empty())
147                         continue;
148
149                 typename Interpolate::SplineValue<float, N>::Type value;
150                 if(extract(*i->keyframe, value))
151                 {
152                         float x = i->time/Time::sec;
153                         if(i->control)
154                         {
155                                 ++n_control;
156                                 if(n_control>2)
157                                         throw logic_error("too many control keyframes");
158                         }
159                         else
160                         {
161                                 if(n_control==1)
162                                 {
163                                         typename Knot::Value cv = knots.back().y;
164                                         knots.back().y = (knots[knots.size()-2].y+cv*2.0f)/3.0f;
165                                         knots.push_back(Knot(x, (value+cv*2.0f)/3.0f));
166                                 }
167                                 else if(n_control==0 && !knots.empty())
168                                 {
169                                         typename Knot::Value prev = knots.back().y;
170                                         knots.push_back(Knot(knots.back().x, (prev*2.0f+value)/3.0f));
171                                         knots.push_back(Knot(x, (prev+value*2.0f)/3.0f));
172                                 }
173                                 n_control = 0;
174                         }
175                         knots.push_back(Knot(x, value));
176                 }
177         }
178         
179         while(n_control--)
180                 knots.pop_back();
181
182         curves.push_back(new ValueCurve<N>(target, knots));
183 }
184
185 bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
186 {
187         value = kf.get_transform().get_position();
188         return true;
189 }
190
191 bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
192 {
193         const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
194         value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
195         return true;
196 }
197
198 bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
199 {
200         value = kf.get_transform().get_scale();
201         return true;
202 }
203
204 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
205 {
206         Event event;
207         event.time = t;
208         event.name = n;
209         event.value = v;
210         events.push_back(event);
211 }
212
213 const Time::TimeDelta &Animation::get_duration() const
214 {
215         if(keyframes.empty())
216                 return Time::zero;
217
218         return keyframes.back().time;
219 }
220
221 void Animation::set_looping(bool l)
222 {
223         looping = l;
224 }
225
226
227 Animation::Curve::Curve(CurveTarget t):
228         target(t)
229 { }
230
231
232 template<unsigned N>
233 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
234         Curve(t),
235         spline(Interpolate::BezierSpline<float, 3, N>(k))
236 { }
237
238 template<unsigned N>
239 void Animation::ValueCurve<N>::apply(float, Matrix &) const
240 {
241         throw invalid_operation("ValueCurve::apply");
242 }
243
244 template<>
245 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
246 {
247         Vector3 value = spline(x);
248         if(target==POSITION)
249                 matrix.translate(value);
250         else if(target==EULER)
251         {
252                 matrix.rotate(value.z, Vector3(0, 0, 1));
253                 matrix.rotate(value.y, Vector3(0, 1, 0));
254                 matrix.rotate(value.x, Vector3(1, 0, 0));
255         }
256         else if(target==SCALE)
257                 matrix.scale(value);
258         else
259                 throw invalid_operation("ValueCurve::apply");
260 }
261
262 template<unsigned N>
263 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
264 {
265         uni.size = N;
266         typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
267         for(unsigned i=0; i<N; ++i)
268                 uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
269 }
270
271
272 template<unsigned N>
273 bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
274 {
275         const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
276         const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
277         if(i==kf_uniforms.end())
278                 return false;
279
280         value = Interpolate::SplineValue<float, N>::make(i->second.values);
281         return true;
282 }
283
284
285 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
286         name(n),
287         size(s)
288 { }
289
290
291 Animation::Iterator::Iterator(const Animation &a):
292         animation(&a),
293         event_iter(animation->events.begin()),
294         end(false)
295 {
296 }
297
298 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
299 {
300         const Time::TimeDelta &duration = animation->get_duration();
301         if(!duration)
302                 return *this;
303
304         elapsed += t;
305         if(animation->looping)
306         {
307                 while(elapsed>=duration)
308                         elapsed -= duration;
309         }
310         else if(elapsed>=duration)
311         {
312                 end = true;
313                 elapsed = duration;
314         }
315
316         return *this;
317 }
318
319 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
320 {
321         for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
322                 observer.animation_event(0, event_iter->name, event_iter->value);
323 }
324
325 Matrix Animation::Iterator::get_matrix() const
326 {
327         Matrix matrix;
328         for(unsigned i=0; i<3; ++i)
329                 animation->curves[i]->apply(elapsed/Time::sec, matrix);
330         return matrix;
331 }
332
333 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
334 {
335         if(i>=animation->uniforms.size())
336                 throw out_of_range("Animation::Iterator::get_uniform");
337
338         KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
339         animation->curves[3+i]->apply(elapsed/Time::sec, uni);
340         return uni;
341 }
342
343 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
344 {
345         if(!animation->armature)
346                 throw invalid_operation("Animation::Iterator::get_pose_matrix");
347         if(link>animation->armature->get_max_link_index())
348                 throw out_of_range("Animation::Iterator::get_pose_matrix");
349
350         throw logic_error("pose animations are currently unimplemented");
351 }
352
353
354 Animation::Loader::Loader(Animation &a):
355         DataFile::CollectionObjectLoader<Animation>(a, 0)
356 {
357         init();
358 }
359
360 Animation::Loader::Loader(Animation &a, Collection &c):
361         DataFile::CollectionObjectLoader<Animation>(a, &c)
362 {
363         init();
364 }
365
366 void Animation::Loader::init()
367 {
368         start_slope = 1;
369         end_slope = 1;
370         add("armature", &Animation::armature);
371         add("control_keyframe", &Loader::control_keyframe);
372         add("control_keyframe", &Loader::control_keyframe_inline);
373         add("event", &Loader::event);
374         add("event", &Loader::event1i);
375         add("event", &Loader::event1f);
376         add("event", &Loader::event2f);
377         add("event", &Loader::event3f);
378         add("event", &Loader::event4f);
379         add("interval", &Loader::interval);
380         add("keyframe", &Loader::keyframe);
381         add("keyframe", &Loader::keyframe_inline);
382         add("looping", &Animation::looping);
383         add("slopes", &Loader::slopes);
384 }
385
386 void Animation::Loader::finish()
387 {
388         obj.create_curves();
389 }
390
391 void Animation::Loader::load_kf(const string &n, bool c)
392 {
393         obj.add_keyframe(current_time, &get_collection().get<KeyFrame>(n), c, false);
394 }
395
396 void Animation::Loader::load_kf_inline(bool c)
397 {
398         RefPtr<KeyFrame> kf = new KeyFrame;
399         if(coll)
400                 load_sub(*kf, get_collection());
401         else
402                 load_sub(*kf);
403
404         obj.add_keyframe(current_time, *kf, c, true);
405         kf.release();
406 }
407
408 void Animation::Loader::control_keyframe(const string &n)
409 {
410         load_kf(n, true);
411 }
412
413 void Animation::Loader::control_keyframe_inline()
414 {
415         load_kf_inline(true);
416 }
417
418 void Animation::Loader::event(const string &n)
419 {
420         obj.add_event(current_time, n);
421 }
422
423 void Animation::Loader::event1i(const string &n, int v)
424 {
425         obj.add_event(current_time, n, v);
426 }
427
428 void Animation::Loader::event1f(const string &n, float v)
429 {
430         obj.add_event(current_time, n, v);
431 }
432
433 void Animation::Loader::event2f(const string &n, float v0, float v1)
434 {
435         obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
436 }
437
438 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
439 {
440         obj.add_event(current_time, n, Vector3(v0, v1, v2));
441 }
442
443 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
444 {
445         obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
446 }
447
448 void Animation::Loader::interval(float t)
449 {
450         current_time += t*Time::sec;
451 }
452
453 void Animation::Loader::keyframe(const string &n)
454 {
455         load_kf(n, false);
456 }
457
458 void Animation::Loader::keyframe_inline()
459 {
460         load_kf_inline(false);
461 }
462
463 void Animation::Loader::slopes(float s, float e)
464 {
465         start_slope = s;
466         end_slope = e;
467 }
468
469 } // namespace GL
470 } // namespace Msp