2 #include <msp/core/maputils.h>
3 #include <msp/datafile/collection.h>
4 #include <msp/interpolate/bezierspline.h>
6 #include "animationeventobserver.h"
16 Animation::Animation():
21 // Avoid synthesizing ~RefPtr in files including animation.h
22 Animation::~Animation()
25 void Animation::set_armature(const Armature &a)
27 if(!keyframes.empty() && &a!=armature)
28 throw invalid_operation("Animation::set_armature");
32 unsigned Animation::get_slot_for_uniform(const string &n) const
34 for(unsigned i=0; i<uniforms.size(); ++i)
35 if(uniforms[i].name==n)
40 const string &Animation::get_uniform_name(unsigned i) const
42 if(i>=uniforms.size())
43 throw out_of_range("Animation::get_uniform_name");
44 return uniforms[i].name;
47 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf)
49 RefPtr<const KeyFrame> kfr(&kf);
51 add_keyframe(t, kfr, false);
55 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
57 add_keyframe(t, kf, slope, slope);
60 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float, float)
65 void Animation::add_control_keyframe(const KeyFrame &kf)
68 throw invalid_operation("Animation::add_control_keyframe");
70 RefPtr<const KeyFrame> kfr(&kf);
72 add_keyframe(keyframes.back().time, kfr, true);
75 void Animation::add_keyframe(const Time::TimeDelta &t, const RefPtr<const KeyFrame> &kf, bool c)
77 if(c && keyframes.empty())
78 throw invalid_argument("Animation::add_keyframe");
79 if(keyframes.empty() && t!=Time::zero)
80 throw invalid_argument("Animation::add_keyframe");
81 if(!keyframes.empty() && t<keyframes.back().time)
82 throw invalid_argument("Animation::add_keyframe");
83 if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
84 throw invalid_argument("Animation::add_keyframe");
86 const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
87 for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
89 KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
90 if(j!=kf_uniforms.end() && j->second.size!=i->size)
91 throw invalid_argument("Animation::add_keyframe");
94 if(kf->get_pose() && !armature)
95 armature = kf->get_pose()->get_armature();
102 keyframes.push_back(tkf);
104 for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
107 for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
108 found = (j->name==i->first);
111 uniforms.push_back(UniformInfo(i->first, i->second.size));
115 void Animation::create_curves()
117 for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
121 curves.reserve(3+uniforms.size());
122 create_curve<3>(POSITION, &extract_position);
123 create_curve<3>(EULER, &extract_euler);
124 create_curve<3>(SCALE, &extract_scale);
126 for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
129 create_curve<1>(UNIFORM, ExtractUniform<1>(i->name));
131 create_curve<2>(UNIFORM, ExtractUniform<2>(i->name));
133 create_curve<3>(UNIFORM, ExtractUniform<3>(i->name));
135 create_curve<4>(UNIFORM, ExtractUniform<4>(i->name));
139 template<unsigned N, typename T>
140 void Animation::create_curve(CurveTarget target, const T &extract)
142 typedef typename ValueCurve<N>::Knot Knot;
145 unsigned n_control = 0;
146 for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
148 if(i->control && knots.empty())
151 typename Interpolate::SplineValue<float, N>::Type value;
152 if(extract(*i->keyframe, value))
154 float x = i->time/Time::sec;
159 throw logic_error("too many control keyframes");
165 typename Knot::Value cv = knots.back().y;
166 knots.back().y = (knots[knots.size()-2].y+cv*2.0f)/3.0f;
167 knots.push_back(Knot(x, (value+cv*2.0f)/3.0f));
169 else if(n_control==0 && !knots.empty())
171 typename Knot::Value prev = knots.back().y;
172 knots.push_back(Knot(knots.back().x, (prev*2.0f+value)/3.0f));
173 knots.push_back(Knot(x, (prev+value*2.0f)/3.0f));
177 knots.push_back(Knot(x, value));
184 curves.push_back(new ValueCurve<N>(target, knots));
187 bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
189 value = kf.get_transform().get_position();
193 bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
195 const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
196 value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
200 bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
202 value = kf.get_transform().get_scale();
206 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
212 events.push_back(event);
215 const Time::TimeDelta &Animation::get_duration() const
217 if(keyframes.empty())
220 return keyframes.back().time;
223 void Animation::set_looping(bool l)
229 Animation::Curve::Curve(CurveTarget t):
235 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
237 spline(Interpolate::BezierSpline<float, 3, N>(k))
241 void Animation::ValueCurve<N>::apply(float, Matrix &) const
243 throw invalid_operation("ValueCurve::apply");
247 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
249 Vector3 value = spline(x);
251 matrix.translate(value);
252 else if(target==EULER)
254 matrix.rotate(value.z, Vector3(0, 0, 1));
255 matrix.rotate(value.y, Vector3(0, 1, 0));
256 matrix.rotate(value.x, Vector3(1, 0, 0));
258 else if(target==SCALE)
261 throw invalid_operation("ValueCurve::apply");
265 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
268 typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
269 for(unsigned i=0; i<N; ++i)
270 uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
275 bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
277 const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
278 const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
279 if(i==kf_uniforms.end())
282 value = Interpolate::SplineValue<float, N>::make(i->second.values);
287 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
293 Animation::Iterator::Iterator(const Animation &a):
295 event_iter(animation->events.begin()),
300 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
302 const Time::TimeDelta &duration = animation->get_duration();
307 if(animation->looping)
309 while(elapsed>=duration)
312 else if(elapsed>=duration)
321 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
323 for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
324 observer.animation_event(0, event_iter->name, event_iter->value);
327 Matrix Animation::Iterator::get_matrix() const
330 for(unsigned i=0; i<3; ++i)
331 animation->curves[i]->apply(elapsed/Time::sec, matrix);
335 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
337 if(i>=animation->uniforms.size())
338 throw out_of_range("Animation::Iterator::get_uniform");
340 KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
341 animation->curves[3+i]->apply(elapsed/Time::sec, uni);
345 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
347 if(!animation->armature)
348 throw invalid_operation("Animation::Iterator::get_pose_matrix");
349 if(link>animation->armature->get_max_link_index())
350 throw out_of_range("Animation::Iterator::get_pose_matrix");
352 throw logic_error("pose animations are currently unimplemented");
356 Animation::Loader::Loader(Animation &a):
357 DataFile::CollectionObjectLoader<Animation>(a, 0)
362 Animation::Loader::Loader(Animation &a, Collection &c):
363 DataFile::CollectionObjectLoader<Animation>(a, &c)
368 void Animation::Loader::init()
372 add("armature", &Animation::armature);
373 add("control_keyframe", &Loader::control_keyframe);
374 add("control_keyframe", &Loader::control_keyframe_inline);
375 add("event", &Loader::event);
376 add("event", &Loader::event1i);
377 add("event", &Loader::event1f);
378 add("event", &Loader::event2f);
379 add("event", &Loader::event3f);
380 add("event", &Loader::event4f);
381 add("interval", &Loader::interval);
382 add("keyframe", &Loader::keyframe);
383 add("keyframe", &Loader::keyframe_inline);
384 add("looping", &Animation::looping);
385 add("slopes", &Loader::slopes);
388 void Animation::Loader::finish()
393 void Animation::Loader::load_kf(const string &n, bool c)
395 obj.add_keyframe(current_time, get_collection().get<KeyFrame>(n), c);
398 void Animation::Loader::load_kf_inline(bool c)
400 RefPtr<KeyFrame> kf = new KeyFrame;
402 load_sub(*kf, get_collection());
406 obj.add_keyframe(current_time, kf, c);
409 void Animation::Loader::control_keyframe(const string &n)
414 void Animation::Loader::control_keyframe_inline()
416 load_kf_inline(true);
419 void Animation::Loader::event(const string &n)
421 obj.add_event(current_time, n);
424 void Animation::Loader::event1i(const string &n, int v)
426 obj.add_event(current_time, n, v);
429 void Animation::Loader::event1f(const string &n, float v)
431 obj.add_event(current_time, n, v);
434 void Animation::Loader::event2f(const string &n, float v0, float v1)
436 obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
439 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
441 obj.add_event(current_time, n, Vector3(v0, v1, v2));
444 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
446 obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
449 void Animation::Loader::interval(float t)
451 current_time += t*Time::sec;
454 void Animation::Loader::keyframe(const string &n)
459 void Animation::Loader::keyframe_inline()
461 load_kf_inline(false);
464 void Animation::Loader::slopes(float s, float e)