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 add_keyframe(t, &kf, false, false);
53 void Animation::add_keyframe_owned(const Time::TimeDelta &t, const KeyFrame *kf)
55 add_keyframe(t, kf, false, true);
59 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
61 add_keyframe(t, &kf, slope, slope, false);
65 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float ss, float es)
67 add_keyframe(t, &kf, ss, es, false);
71 void Animation::add_control_keyframe(const KeyFrame &kf)
74 throw invalid_operation("Animation::add_control_keyframe");
76 add_keyframe(keyframes.back().time, &kf, true, false);
79 void Animation::add_control_keyframe_owned(const KeyFrame *kf)
82 throw invalid_operation("Animation::add_control_keyframe_owned");
84 add_keyframe(keyframes.back().time, kf, true, true);
87 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float ss, float es, bool owned)
90 return add_keyframe(t, kf, false, owned);
92 if(keyframes.back().control)
93 throw invalid_operation("Animation::add_keyframe");
95 const KeyFrame &last = *keyframes.back().keyframe;
96 const Transform &trn = kf->get_transform();
97 const Transform &last_trn = last.get_transform();
98 const KeyFrame::UniformMap &kf_unis = kf->get_uniforms();
99 const KeyFrame::UniformMap &last_unis = last.get_uniforms();
100 for(unsigned i=1; i<=2; ++i)
102 float x = (i==1 ? ss/3 : 1-es/3);
103 KeyFrame *ckf = new KeyFrame;
105 ctrn.set_position(last_trn.get_position()*(1-x)+trn.get_position()*x);
106 const Transform::AngleVector3 &e1 = last_trn.get_euler();
107 const Transform::AngleVector3 &e2 = trn.get_euler();
108 ctrn.set_euler(Transform::AngleVector3(e1.x*(1-x)+e2.x*x, e1.y*(1-x)+e2.y*x, e1.z*(1-x)+e2.z*x));
109 ctrn.set_scale(last_trn.get_scale()*(1-x)+trn.get_scale()*x);
110 ckf->set_transform(ctrn);
112 for(KeyFrame::UniformMap::const_iterator j=kf_unis.begin(); j!=kf_unis.end(); ++j)
114 KeyFrame::UniformMap::const_iterator k = last_unis.find(j->first);
115 if(k==last_unis.end())
118 KeyFrame::AnimatedUniform uni(j->second.size, 0.0f);
119 for(unsigned c=0; c<uni.size; ++c)
120 uni.values[c] = k->second.values[c]*(1-x)+j->second.values[c]*x;
122 ckf->set_uniform(j->first, uni);
125 add_keyframe(t, ckf, true, true);
128 add_keyframe(t, kf, false, owned);
131 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool c, bool owned)
133 if(c && keyframes.empty())
134 throw invalid_argument("Animation::add_keyframe");
135 if(keyframes.empty() && t!=Time::zero)
136 throw invalid_argument("Animation::add_keyframe");
137 if(!keyframes.empty() && t<keyframes.back().time)
138 throw invalid_argument("Animation::add_keyframe");
139 if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
140 throw invalid_argument("Animation::add_keyframe");
142 const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
143 for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
145 KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
146 if(j!=kf_uniforms.end() && j->second.size!=i->size)
147 throw invalid_argument("Animation::add_keyframe");
150 if(kf->get_pose() && !armature)
151 armature = kf->get_pose()->get_armature();
160 keyframes.push_back(tkf);
162 for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
165 for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
166 found = (j->name==i->first);
169 uniforms.push_back(UniformInfo(i->first, i->second.size));
173 void Animation::create_curves()
175 for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
179 curves.reserve(6+uniforms.size());
180 create_curve(POSITION, Transform::POSITION, &extract_position);
181 create_curve(EULER, Transform::EULER, &extract_euler);
182 create_curve(SCALE, Transform::SCALE, &extract_scale);
184 uniform_curve_offset = curves.size();
185 for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
188 create_curve<1>(UNIFORM, -1, ExtractUniform<1>(i->name));
190 create_curve<2>(UNIFORM, -1, ExtractUniform<2>(i->name));
192 create_curve<3>(UNIFORM, -1, ExtractUniform<3>(i->name));
194 create_curve<4>(UNIFORM, -1, ExtractUniform<4>(i->name));
198 void Animation::create_curve(CurveTarget target, Transform::ComponentMask mask, ExtractComponent::Extract extract)
200 Transform::ComponentMask all = mask;
201 Transform::ComponentMask any = Transform::NONE;
202 for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
204 all = all&i->keyframe->get_transform().get_mask();
205 any = any|i->keyframe->get_transform().get_mask();
209 create_curve<3>(target, -1, extract);
212 unsigned low_bit = mask&(mask>>2);
213 for(unsigned i=3; i-->0; )
215 Transform::ComponentMask bit = static_cast<Transform::ComponentMask>(low_bit<<i);
217 create_curve<1>(target, i, ExtractComponent(extract, i, bit));
222 template<unsigned N, typename T>
223 void Animation::create_curve(CurveTarget target, int component, const T &extract)
225 typedef typename ValueCurve<N>::Knot Knot;
228 unsigned n_control = 0;
229 for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
231 if(i->control && knots.empty())
234 typename Interpolate::SplineValue<float, N>::Type value;
235 if(extract(*i->keyframe, value))
237 float x = i->time/Time::sec;
242 throw logic_error("too many control keyframes");
248 typename Knot::Value cv = knots.back().y;
249 knots.back().y = (knots[knots.size()-2].y+cv*2.0f)/3.0f;
250 knots.push_back(Knot(x, (value+cv*2.0f)/3.0f));
252 else if(n_control==0 && !knots.empty())
254 typename Knot::Value prev = knots.back().y;
255 knots.push_back(Knot(knots.back().x, (prev*2.0f+value)/3.0f));
256 knots.push_back(Knot(x, (prev+value*2.0f)/3.0f));
260 knots.push_back(Knot(x, value));
267 curves.push_back(new ValueCurve<N>(target, component, knots));
270 bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
272 value = kf.get_transform().get_position();
276 bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
278 const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
279 value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
283 bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
285 value = kf.get_transform().get_scale();
289 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
295 events.push_back(event);
298 const Time::TimeDelta &Animation::get_duration() const
300 if(keyframes.empty())
303 return keyframes.back().time;
306 void Animation::set_looping(bool l)
312 Animation::Curve::Curve(CurveTarget t, int c):
319 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, int c, const vector<Knot> &k):
321 spline(Interpolate::BezierSpline<float, 3, N>(k))
325 void Animation::ValueCurve<N>::apply(float, Matrix &) const
327 throw invalid_operation("ValueCurve::apply");
331 void Animation::ValueCurve<1>::apply(float x, Matrix &matrix) const
333 float value = spline(x);
334 if(target==POSITION || target==SCALE)
337 vec[component] = value;
339 matrix.translate(vec);
343 else if(target==EULER)
346 vec[component] = 1.0f;
347 matrix.rotate(Geometry::Angle<float>::from_radians(value), vec);
350 throw invalid_operation("ValueCurve::apply");
354 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
356 Vector3 value = spline(x);
358 matrix.translate(value);
359 else if(target==EULER)
361 matrix.rotate(Geometry::Angle<float>::from_radians(value.z), Vector3(0, 0, 1));
362 matrix.rotate(Geometry::Angle<float>::from_radians(value.y), Vector3(0, 1, 0));
363 matrix.rotate(Geometry::Angle<float>::from_radians(value.x), Vector3(1, 0, 0));
365 else if(target==SCALE)
368 throw invalid_operation("ValueCurve::apply");
372 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
375 typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
376 for(unsigned i=0; i<N; ++i)
377 uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
381 bool Animation::ExtractComponent::operator()(const KeyFrame &kf, float &value) const
384 if(!extract(kf, vec))
388 return kf.get_transform().get_mask()&mask;
393 bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
395 const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
396 const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
397 if(i==kf_uniforms.end())
400 value = Interpolate::SplineValue<float, N>::make(i->second.values);
405 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
411 Animation::Iterator::Iterator(const Animation &a):
413 event_iter(animation->events.begin()),
418 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
420 const Time::TimeDelta &duration = animation->get_duration();
425 if(animation->looping)
427 while(elapsed>=duration)
430 else if(elapsed>=duration)
439 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
441 for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
442 observer.animation_event(0, event_iter->name, event_iter->value);
445 Matrix Animation::Iterator::get_matrix() const
448 for(unsigned i=0; i<animation->uniform_curve_offset; ++i)
449 animation->curves[i]->apply(elapsed/Time::sec, matrix);
453 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
455 if(i>=animation->uniforms.size())
456 throw out_of_range("Animation::Iterator::get_uniform");
458 KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
459 animation->curves[animation->uniform_curve_offset+i]->apply(elapsed/Time::sec, uni);
463 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
465 if(!animation->armature)
466 throw invalid_operation("Animation::Iterator::get_pose_matrix");
467 if(link>animation->armature->get_max_link_index())
468 throw out_of_range("Animation::Iterator::get_pose_matrix");
470 throw logic_error("pose animations are currently unimplemented");
474 Animation::Loader::Loader(Animation &a):
475 DataFile::CollectionObjectLoader<Animation>(a, 0)
480 Animation::Loader::Loader(Animation &a, Collection &c):
481 DataFile::CollectionObjectLoader<Animation>(a, &c)
486 void Animation::Loader::init()
491 add("armature", &Animation::armature);
492 add("control_keyframe", &Loader::control_keyframe);
493 add("control_keyframe", &Loader::control_keyframe_inline);
494 add("event", &Loader::event);
495 add("event", &Loader::event1i);
496 add("event", &Loader::event1f);
497 add("event", &Loader::event2f);
498 add("event", &Loader::event3f);
499 add("event", &Loader::event4f);
500 add("interval", &Loader::interval);
501 add("keyframe", &Loader::keyframe);
502 add("keyframe", &Loader::keyframe_inline);
503 add("looping", &Animation::looping);
504 add("slopes", &Loader::slopes);
507 void Animation::Loader::finish()
512 void Animation::Loader::check_slopes_and_control(bool s, bool c)
515 throw logic_error("can't use both slopes and control keyframes in same segment");
518 void Animation::Loader::add_kf(const KeyFrame *kf, bool c, bool owned)
521 obj.add_keyframe(current_time, kf, start_slope, end_slope, owned);
523 obj.add_keyframe(current_time, kf, c, owned);
525 start_slope = end_slope;
527 slopes_set = (slopes_set<<1)&3;
530 void Animation::Loader::load_kf(const string &n, bool c)
532 add_kf(&get_collection().get<KeyFrame>(n), c, false);
535 void Animation::Loader::load_kf_inline(bool c)
537 RefPtr<KeyFrame> kf = new KeyFrame;
539 load_sub(*kf, get_collection());
543 add_kf(kf.get(), c, true);
547 void Animation::Loader::control_keyframe(const string &n)
550 check_slopes_and_control(slopes_set, true);
554 void Animation::Loader::control_keyframe_inline()
557 check_slopes_and_control(slopes_set, true);
558 load_kf_inline(true);
561 void Animation::Loader::event(const string &n)
563 obj.add_event(current_time, n);
566 void Animation::Loader::event1i(const string &n, int v)
568 obj.add_event(current_time, n, v);
571 void Animation::Loader::event1f(const string &n, float v)
573 obj.add_event(current_time, n, v);
576 void Animation::Loader::event2f(const string &n, float v0, float v1)
578 obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
581 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
583 obj.add_event(current_time, n, Vector3(v0, v1, v2));
586 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
588 obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
591 void Animation::Loader::interval(float t)
593 current_time += t*Time::sec;
596 void Animation::Loader::keyframe(const string &n)
601 void Animation::Loader::keyframe_inline()
603 load_kf_inline(false);
606 void Animation::Loader::slopes(float s, float e)
608 check_slopes_and_control(true, (!obj.keyframes.empty() && obj.keyframes.back().control));