2 #include <msp/core/algorithm.h>
3 #include <msp/core/maputils.h>
4 #include <msp/datafile/collection.h>
5 #include <msp/fs/utils.h>
6 #include <msp/interpolate/bezierspline.h>
7 #include <msp/strings/format.h>
9 #include "animationeventobserver.h"
19 Animation::Animation():
24 Animation::~Animation()
26 for(TimedKeyFrame &k: keyframes)
33 void Animation::set_armature(const Armature &a)
35 if(!keyframes.empty() && &a!=armature)
36 throw invalid_operation("Animation::set_armature");
40 unsigned Animation::get_slot_for_uniform(const string &n) const
42 for(unsigned i=0; i<uniforms.size(); ++i)
43 if(uniforms[i].name==n)
48 const string &Animation::get_uniform_name(unsigned i) const
50 if(i>=uniforms.size())
51 throw out_of_range("Animation::get_uniform_name");
52 return uniforms[i].name;
55 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf)
57 add_keyframe(t, &kf, false, false);
61 void Animation::add_keyframe_owned(const Time::TimeDelta &t, const KeyFrame *kf)
63 add_keyframe(t, kf, false, true);
67 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
69 add_keyframe(t, &kf, slope, slope, false);
73 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float ss, float es)
75 add_keyframe(t, &kf, ss, es, false);
79 void Animation::add_control_keyframe(const KeyFrame &kf)
82 throw invalid_operation("Animation::add_control_keyframe");
84 add_keyframe(keyframes.back().time, &kf, true, false);
87 void Animation::add_control_keyframe_owned(const KeyFrame *kf)
90 throw invalid_operation("Animation::add_control_keyframe_owned");
92 add_keyframe(keyframes.back().time, kf, true, true);
95 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float ss, float es, bool owned)
98 return add_keyframe(t, kf, false, owned);
100 if(keyframes.back().control)
101 throw invalid_operation("Animation::add_keyframe");
103 const KeyFrame &last = *keyframes.back().keyframe;
104 const Transform &trn = kf->get_transform();
105 const Transform &last_trn = last.get_transform();
106 const KeyFrame::UniformMap &kf_unis = kf->get_uniforms();
107 const KeyFrame::UniformMap &last_unis = last.get_uniforms();
108 for(unsigned i=1; i<=2; ++i)
110 float x = (i==1 ? ss/3 : 1-es/3);
111 KeyFrame *ckf = new KeyFrame;
113 ctrn.set_position(last_trn.get_position()*(1-x)+trn.get_position()*x);
114 const Transform::AngleVector3 &e1 = last_trn.get_euler();
115 const Transform::AngleVector3 &e2 = trn.get_euler();
116 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));
117 ctrn.set_scale(last_trn.get_scale()*(1-x)+trn.get_scale()*x);
118 ckf->set_transform(ctrn);
120 for(const auto &kvp: kf_unis)
122 auto k = last_unis.find(kvp.first);
123 if(k==last_unis.end())
126 KeyFrame::AnimatedUniform uni(kvp.second.size, 0.0f);
127 for(unsigned c=0; c<uni.size; ++c)
128 uni.values[c] = k->second.values[c]*(1-x)+kvp.second.values[c]*x;
130 ckf->set_uniform(kvp.first, uni);
133 add_keyframe(t, ckf, true, true);
136 add_keyframe(t, kf, false, owned);
139 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool c, bool owned)
141 if(c && keyframes.empty())
142 throw invalid_argument("Animation::add_keyframe");
143 if(keyframes.empty() && t!=Time::zero)
144 throw invalid_argument("Animation::add_keyframe");
145 if(!keyframes.empty() && t<keyframes.back().time)
146 throw invalid_argument("Animation::add_keyframe");
147 if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
148 throw invalid_argument("Animation::add_keyframe");
150 const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
151 for(const UniformInfo &u: uniforms)
153 auto j = kf_uniforms.find(u.name);
154 if(j!=kf_uniforms.end() && j->second.size!=u.size)
155 throw invalid_argument("Animation::add_keyframe");
158 if(kf->get_pose() && !armature)
159 armature = kf->get_pose()->get_armature();
167 keyframes.push_back(tkf);
169 for(const auto &kvp: kf_uniforms)
171 if(find_member(uniforms, kvp.first, &UniformInfo::name)==uniforms.end())
172 uniforms.push_back(UniformInfo(kvp.first, kvp.second.size));
176 void Animation::create_curves()
178 for(Curve *c: curves)
182 curves.reserve(6+uniforms.size());
183 create_curve(POSITION, Transform::POSITION, &extract_position);
184 create_curve(EULER, Transform::EULER, &extract_euler);
185 create_curve(SCALE, Transform::SCALE, &extract_scale);
187 uniform_curve_offset = curves.size();
188 for(const UniformInfo &u: uniforms)
191 create_curve<1>(UNIFORM, -1, ExtractUniform<1>(u.name));
193 create_curve<2>(UNIFORM, -1, ExtractUniform<2>(u.name));
195 create_curve<3>(UNIFORM, -1, ExtractUniform<3>(u.name));
197 create_curve<4>(UNIFORM, -1, ExtractUniform<4>(u.name));
201 void Animation::create_curve(CurveTarget target, Transform::ComponentMask mask, ExtractComponent::Extract extract)
203 Transform::ComponentMask all = mask;
204 Transform::ComponentMask any = Transform::NONE;
205 for(const TimedKeyFrame &k: keyframes)
207 all = all&k.keyframe->get_transform().get_mask();
208 any = any|k.keyframe->get_transform().get_mask();
212 create_curve<3>(target, -1, extract);
215 unsigned low_bit = mask&(mask>>2);
216 for(unsigned i=3; i-->0; )
218 Transform::ComponentMask bit = static_cast<Transform::ComponentMask>(low_bit<<i);
220 create_curve<1>(target, i, ExtractComponent(extract, i, bit));
225 template<unsigned N, typename T>
226 void Animation::create_curve(CurveTarget target, int component, const T &extract)
228 typedef typename ValueCurve<N>::Knot Knot;
231 unsigned n_control = 0;
232 for(const TimedKeyFrame &k: keyframes)
234 if(k.control && knots.empty())
237 typename Interpolate::SplineValue<float, N>::Type value;
238 if(extract(*k.keyframe, value))
240 typename Knot::Value dvalue = value;
241 float x = k.time/Time::sec;
246 throw logic_error("too many control keyframes");
252 typename Knot::Value cv = knots.back().y;
253 knots.back().y = (knots[knots.size()-2].y+cv*2.0)/3.0;
254 knots.push_back(Knot(x, (dvalue+cv*2.0)/3.0));
256 else if(n_control==0 && !knots.empty())
258 typename Knot::Value prev = knots.back().y;
259 knots.push_back(Knot(knots.back().x, (prev*2.0+dvalue)/3.0));
260 knots.push_back(Knot(x, (prev+dvalue*2.0)/3.0));
264 knots.push_back(Knot(x, value));
273 knots.push_back(knots.back());
274 knots.push_back(knots.back());
276 knots.push_back(knots.back());
279 curves.push_back(new ValueCurve<N>(target, component, knots));
282 bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
284 value = kf.get_transform().get_position();
288 bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
290 const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
291 value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
295 bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
297 value = kf.get_transform().get_scale();
301 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
307 events.push_back(event);
310 const Time::TimeDelta &Animation::get_duration() const
312 if(keyframes.empty())
315 return keyframes.back().time;
318 void Animation::set_looping(bool l)
324 Animation::Curve::Curve(CurveTarget t, int c):
331 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, int c, const vector<Knot> &k):
333 spline(Interpolate::BezierSpline<double, 3, N>(k))
337 void Animation::ValueCurve<N>::apply(float, Matrix &) const
339 throw invalid_operation("ValueCurve::apply");
343 void Animation::ValueCurve<1>::apply(float x, Matrix &matrix) const
345 float value = spline(x);
346 if(target==POSITION || target==SCALE)
351 vec[component] = value;
352 matrix.translate(vec);
356 Vector3 vec(1.0f, 1.0f, 1.0f);
357 vec[component] = value;
361 else if(target==EULER)
364 vec[component] = 1.0f;
365 matrix.rotate(Geometry::Angle<float>::from_radians(value), vec);
368 throw invalid_operation("ValueCurve::apply");
372 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
374 Vector3 value = spline(x);
376 matrix.translate(value);
377 else if(target==EULER)
379 matrix.rotate(Geometry::Angle<float>::from_radians(value.z), Vector3(0, 0, 1));
380 matrix.rotate(Geometry::Angle<float>::from_radians(value.y), Vector3(0, 1, 0));
381 matrix.rotate(Geometry::Angle<float>::from_radians(value.x), Vector3(1, 0, 0));
383 else if(target==SCALE)
386 throw invalid_operation("ValueCurve::apply");
390 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
393 typename Interpolate::Spline<double, 3, N>::Value value = spline(x);
394 for(unsigned i=0; i<N; ++i)
395 uni.values[i] = Interpolate::SplineValue<double, N>::get(value, i);
399 bool Animation::ExtractComponent::operator()(const KeyFrame &kf, float &value) const
402 if(!extract(kf, vec))
406 return kf.get_transform().get_mask()&mask;
411 bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
413 const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
414 auto i = kf_uniforms.find(name);
415 if(i==kf_uniforms.end())
418 value = Interpolate::SplineValue<float, N>::make(i->second.values);
423 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
429 Animation::Iterator::Iterator(const Animation &a):
431 event_iter(animation->events.begin()),
436 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
438 const Time::TimeDelta &duration = animation->get_duration();
443 if(animation->looping)
445 while(elapsed>=duration)
448 else if(elapsed>=duration)
457 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
459 for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
460 observer.animation_event(0, event_iter->name, event_iter->value);
463 Matrix Animation::Iterator::get_matrix() const
466 for(unsigned i=0; i<animation->uniform_curve_offset; ++i)
467 animation->curves[i]->apply(elapsed/Time::sec, matrix);
471 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
473 if(i>=animation->uniforms.size())
474 throw out_of_range("Animation::Iterator::get_uniform");
476 KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
477 animation->curves[animation->uniform_curve_offset+i]->apply(elapsed/Time::sec, uni);
481 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
483 if(!animation->armature)
484 throw invalid_operation("Animation::Iterator::get_pose_matrix");
485 if(link>animation->armature->get_max_link_index())
486 throw out_of_range("Animation::Iterator::get_pose_matrix");
488 throw logic_error("pose animations are currently unimplemented");
492 Animation::Loader::Loader(Animation &a):
493 DataFile::CollectionObjectLoader<Animation>(a, 0)
498 Animation::Loader::Loader(Animation &a, Collection &c):
499 DataFile::CollectionObjectLoader<Animation>(a, &c)
504 void Animation::Loader::init()
509 add("armature", &Animation::armature);
510 add("control_keyframe", &Loader::control_keyframe);
511 add("control_keyframe", &Loader::control_keyframe_inline);
512 add("event", &Loader::event);
513 add("event", &Loader::event1i);
514 add("event", &Loader::event1f);
515 add("event", &Loader::event2f);
516 add("event", &Loader::event3f);
517 add("event", &Loader::event4f);
518 add("interval", &Loader::interval);
519 add("keyframe", &Loader::keyframe);
520 add("keyframe", &Loader::keyframe_inline);
521 add("looping", &Animation::looping);
522 add("slopes", &Loader::slopes);
525 void Animation::Loader::finish()
530 void Animation::Loader::check_slopes_and_control(bool s, bool c)
533 throw logic_error("can't use both slopes and control keyframes in same segment");
536 void Animation::Loader::add_kf(const KeyFrame *kf, bool c, bool owned)
539 obj.add_keyframe(current_time, kf, start_slope, end_slope, owned);
541 obj.add_keyframe(current_time, kf, c, owned);
543 start_slope = end_slope;
545 slopes_set = (slopes_set<<1)&3;
548 void Animation::Loader::load_kf(const string &n, bool c)
550 add_kf(&get_collection().get<KeyFrame>(n), c, false);
553 void Animation::Loader::load_kf_inline(bool c)
555 RefPtr<KeyFrame> kf = new KeyFrame;
558 KeyFrame::Loader ldr(*kf, get_collection());
559 ldr.set_inline_base_name(format("%s/%d.kframe", FS::basename(get_source()), obj.keyframes.size()));
565 add_kf(kf.get(), c, true);
569 void Animation::Loader::control_keyframe(const string &n)
572 check_slopes_and_control(slopes_set, true);
576 void Animation::Loader::control_keyframe_inline()
579 check_slopes_and_control(slopes_set, true);
580 load_kf_inline(true);
583 void Animation::Loader::event(const string &n)
585 obj.add_event(current_time, n);
588 void Animation::Loader::event1i(const string &n, int v)
590 obj.add_event(current_time, n, v);
593 void Animation::Loader::event1f(const string &n, float v)
595 obj.add_event(current_time, n, v);
598 void Animation::Loader::event2f(const string &n, float v0, float v1)
600 obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
603 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
605 obj.add_event(current_time, n, Vector3(v0, v1, v2));
608 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
610 obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
613 void Animation::Loader::interval(float t)
615 current_time += t*Time::sec;
618 void Animation::Loader::keyframe(const string &n)
623 void Animation::Loader::keyframe_inline()
625 load_kf_inline(false);
628 void Animation::Loader::slopes(float s, float e)
630 check_slopes_and_control(true, (!obj.keyframes.empty() && obj.keyframes.back().control));