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()
21 for(TimedKeyFrame &k: keyframes)
28 void Animation::set_armature(const Armature &a)
30 if(!keyframes.empty() && &a!=armature)
31 throw invalid_operation("Animation::set_armature");
35 unsigned Animation::get_slot_for_uniform(const string &n) const
37 for(unsigned i=0; i<uniforms.size(); ++i)
38 if(uniforms[i].name==n)
43 const string &Animation::get_uniform_name(unsigned i) const
45 if(i>=uniforms.size())
46 throw out_of_range("Animation::get_uniform_name");
47 return uniforms[i].name;
50 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf)
52 add_keyframe(t, &kf, false, false);
56 void Animation::add_keyframe_owned(const Time::TimeDelta &t, const KeyFrame *kf)
58 add_keyframe(t, kf, false, true);
62 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
64 add_keyframe(t, &kf, slope, slope, false);
68 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float ss, float es)
70 add_keyframe(t, &kf, ss, es, false);
74 void Animation::add_control_keyframe(const KeyFrame &kf)
77 throw invalid_operation("Animation::add_control_keyframe");
79 add_keyframe(keyframes.back().time, &kf, true, false);
82 void Animation::add_control_keyframe_owned(const KeyFrame *kf)
85 throw invalid_operation("Animation::add_control_keyframe_owned");
87 add_keyframe(keyframes.back().time, kf, true, true);
90 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float ss, float es, bool owned)
93 return add_keyframe(t, kf, false, owned);
95 if(keyframes.back().control)
96 throw invalid_operation("Animation::add_keyframe");
98 const KeyFrame &last = *keyframes.back().keyframe;
99 const Transform &trn = kf->get_transform();
100 const Transform &last_trn = last.get_transform();
101 const KeyFrame::UniformMap &kf_unis = kf->get_uniforms();
102 const KeyFrame::UniformMap &last_unis = last.get_uniforms();
103 for(unsigned i=1; i<=2; ++i)
105 float x = (i==1 ? ss/3 : 1-es/3);
106 KeyFrame *ckf = new KeyFrame;
108 ctrn.set_position(last_trn.get_position()*(1-x)+trn.get_position()*x);
109 const Transform::AngleVector3 &e1 = last_trn.get_euler();
110 const Transform::AngleVector3 &e2 = trn.get_euler();
111 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));
112 ctrn.set_scale(last_trn.get_scale()*(1-x)+trn.get_scale()*x);
113 ckf->set_transform(ctrn);
115 for(const auto &kvp: kf_unis)
117 auto k = last_unis.find(kvp.first);
118 if(k==last_unis.end())
121 KeyFrame::AnimatedUniform uni(kvp.second.size, 0.0f);
122 for(unsigned c=0; c<uni.size; ++c)
123 uni.values[c] = k->second.values[c]*(1-x)+kvp.second.values[c]*x;
125 ckf->set_uniform(kvp.first, uni);
128 add_keyframe(t, ckf, true, true);
131 add_keyframe(t, kf, false, owned);
134 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool c, bool owned)
136 if(c && keyframes.empty())
137 throw invalid_argument("Animation::add_keyframe");
138 if(keyframes.empty() && t!=Time::zero)
139 throw invalid_argument("Animation::add_keyframe");
140 if(!keyframes.empty() && t<keyframes.back().time)
141 throw invalid_argument("Animation::add_keyframe");
142 if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
143 throw invalid_argument("Animation::add_keyframe");
145 const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
146 for(const UniformInfo &u: uniforms)
148 auto j = kf_uniforms.find(u.name);
149 if(j!=kf_uniforms.end() && j->second.size!=u.size)
150 throw invalid_argument("Animation::add_keyframe");
153 if(kf->get_pose() && !armature)
154 armature = kf->get_pose()->get_armature();
162 keyframes.push_back(tkf);
164 for(const auto &kvp: kf_uniforms)
166 if(find_member(uniforms, kvp.first, &UniformInfo::name)==uniforms.end())
167 uniforms.push_back(UniformInfo(kvp.first, kvp.second.size));
171 void Animation::create_curves()
173 for(Curve *c: curves)
177 curves.reserve(6+uniforms.size());
178 create_curve(POSITION, Transform::POSITION, &extract_position);
179 create_curve(EULER, Transform::EULER, &extract_euler);
180 create_curve(SCALE, Transform::SCALE, &extract_scale);
182 uniform_curve_offset = curves.size();
183 for(const UniformInfo &u: uniforms)
186 create_curve<1>(UNIFORM, -1, ExtractUniform<1>(u.name));
188 create_curve<2>(UNIFORM, -1, ExtractUniform<2>(u.name));
190 create_curve<3>(UNIFORM, -1, ExtractUniform<3>(u.name));
192 create_curve<4>(UNIFORM, -1, ExtractUniform<4>(u.name));
196 void Animation::create_curve(CurveTarget target, Transform::ComponentMask mask, ExtractComponent::Extract extract)
198 Transform::ComponentMask all = mask;
199 Transform::ComponentMask any = Transform::NONE;
200 for(const TimedKeyFrame &k: keyframes)
202 all = all&k.keyframe->get_transform().get_mask();
203 any = any|k.keyframe->get_transform().get_mask();
207 create_curve<3>(target, -1, extract);
210 unsigned low_bit = mask&(mask>>2);
211 for(unsigned i=3; i-->0; )
213 Transform::ComponentMask bit = static_cast<Transform::ComponentMask>(low_bit<<i);
215 create_curve<1>(target, i, ExtractComponent(extract, i, bit));
220 template<unsigned N, typename T>
221 void Animation::create_curve(CurveTarget target, int component, const T &extract)
223 typedef typename ValueCurve<N>::Knot Knot;
226 unsigned n_control = 0;
227 for(const TimedKeyFrame &k: keyframes)
229 if(k.control && knots.empty())
232 typename Interpolate::SplineValue<float, N>::Type value;
233 if(extract(*k.keyframe, value))
235 typename Knot::Value dvalue = value;
236 float x = k.time/Time::sec;
241 throw logic_error("too many control keyframes");
247 typename Knot::Value cv = knots.back().y;
248 knots.back().y = (knots[knots.size()-2].y+cv*2.0)/3.0;
249 knots.push_back(Knot(x, (dvalue+cv*2.0)/3.0));
251 else if(n_control==0 && !knots.empty())
253 typename Knot::Value prev = knots.back().y;
254 knots.push_back(Knot(knots.back().x, (prev*2.0+dvalue)/3.0));
255 knots.push_back(Knot(x, (prev+dvalue*2.0)/3.0));
259 knots.push_back(Knot(x, value));
268 knots.push_back(knots.back());
269 knots.push_back(knots.back());
271 knots.push_back(knots.back());
274 curves.push_back(new ValueCurve<N>(target, component, knots));
277 bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
279 value = kf.get_transform().get_position();
283 bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
285 const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
286 value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
290 bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
292 value = kf.get_transform().get_scale();
296 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
302 events.push_back(event);
305 const Time::TimeDelta &Animation::get_duration() const
307 if(keyframes.empty())
310 return keyframes.back().time;
313 void Animation::set_looping(bool l)
319 Animation::Curve::Curve(CurveTarget t, int c):
326 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, int c, const vector<Knot> &k):
328 spline(Interpolate::BezierSpline<double, 3, N>(k))
332 void Animation::ValueCurve<N>::apply(float, Matrix &) const
334 throw invalid_operation("ValueCurve::apply");
338 void Animation::ValueCurve<1>::apply(float x, Matrix &matrix) const
340 float value = spline(x);
341 if(target==POSITION || target==SCALE)
346 vec[component] = value;
347 matrix.translate(vec);
351 Vector3 vec(1.0f, 1.0f, 1.0f);
352 vec[component] = value;
356 else if(target==EULER)
359 vec[component] = 1.0f;
360 matrix.rotate(Geometry::Angle<float>::from_radians(value), vec);
363 throw invalid_operation("ValueCurve::apply");
367 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
369 Vector3 value = spline(x);
371 matrix.translate(value);
372 else if(target==EULER)
374 matrix.rotate(Geometry::Angle<float>::from_radians(value.z), Vector3(0, 0, 1));
375 matrix.rotate(Geometry::Angle<float>::from_radians(value.y), Vector3(0, 1, 0));
376 matrix.rotate(Geometry::Angle<float>::from_radians(value.x), Vector3(1, 0, 0));
378 else if(target==SCALE)
381 throw invalid_operation("ValueCurve::apply");
385 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
388 typename Interpolate::Spline<double, 3, N>::Value value = spline(x);
389 for(unsigned i=0; i<N; ++i)
390 uni.values[i] = Interpolate::SplineValue<double, N>::get(value, i);
394 bool Animation::ExtractComponent::operator()(const KeyFrame &kf, float &value) const
397 if(!extract(kf, vec))
401 return kf.get_transform().get_mask()&mask;
406 bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
408 const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
409 auto i = kf_uniforms.find(name);
410 if(i==kf_uniforms.end())
413 value = Interpolate::SplineValue<float, N>::make(i->second.values);
418 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
424 Animation::Iterator::Iterator(const Animation &a):
426 event_iter(animation->events.begin()),
431 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
433 const Time::TimeDelta &duration = animation->get_duration();
438 if(animation->looping)
440 while(elapsed>=duration)
443 else if(elapsed>=duration)
452 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
454 for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
455 observer.animation_event(0, event_iter->name, event_iter->value);
458 Matrix Animation::Iterator::get_matrix() const
461 for(unsigned i=0; i<animation->uniform_curve_offset; ++i)
462 animation->curves[i]->apply(elapsed/Time::sec, matrix);
466 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
468 if(i>=animation->uniforms.size())
469 throw out_of_range("Animation::Iterator::get_uniform");
471 KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
472 animation->curves[animation->uniform_curve_offset+i]->apply(elapsed/Time::sec, uni);
476 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
478 if(!animation->armature)
479 throw invalid_operation("Animation::Iterator::get_pose_matrix");
480 if(link>animation->armature->get_max_link_index())
481 throw out_of_range("Animation::Iterator::get_pose_matrix");
483 throw logic_error("pose animations are currently unimplemented");
487 Animation::Loader::Loader(Animation &a, Collection *c):
488 DataFile::CollectionObjectLoader<Animation>(a, c),
493 add("armature", &Animation::armature);
494 add("control_keyframe", &Loader::control_keyframe);
495 add("control_keyframe", &Loader::control_keyframe_inline);
496 add("event", &Loader::event);
497 add("event", &Loader::event1i);
498 add("event", &Loader::event1f);
499 add("event", &Loader::event2f);
500 add("event", &Loader::event3f);
501 add("event", &Loader::event4f);
502 add("interval", &Loader::interval);
503 add("keyframe", &Loader::keyframe);
504 add("keyframe", &Loader::keyframe_inline);
505 add("looping", &Animation::looping);
506 add("slopes", &Loader::slopes);
509 void Animation::Loader::finish()
514 void Animation::Loader::check_slopes_and_control(bool s, bool c)
517 throw logic_error("can't use both slopes and control keyframes in same segment");
520 void Animation::Loader::add_kf(const KeyFrame *kf, bool c, bool owned)
523 obj.add_keyframe(current_time, kf, start_slope, end_slope, owned);
525 obj.add_keyframe(current_time, kf, c, owned);
527 start_slope = end_slope;
529 slopes_set = (slopes_set<<1)&3;
532 void Animation::Loader::load_kf(const string &n, bool c)
534 add_kf(&get_collection().get<KeyFrame>(n), c, false);
537 void Animation::Loader::load_kf_inline(bool c)
539 RefPtr<KeyFrame> kf = new KeyFrame;
542 KeyFrame::Loader ldr(*kf, get_collection());
543 ldr.set_inline_base_name(format("%s/%d.kframe", FS::basename(get_source()), obj.keyframes.size()));
549 add_kf(kf.get(), c, true);
553 void Animation::Loader::control_keyframe(const string &n)
556 check_slopes_and_control(slopes_set, true);
560 void Animation::Loader::control_keyframe_inline()
563 check_slopes_and_control(slopes_set, true);
564 load_kf_inline(true);
567 void Animation::Loader::event(const string &n)
569 obj.add_event(current_time, n);
572 void Animation::Loader::event1i(const string &n, int v)
574 obj.add_event(current_time, n, v);
577 void Animation::Loader::event1f(const string &n, float v)
579 obj.add_event(current_time, n, v);
582 void Animation::Loader::event2f(const string &n, float v0, float v1)
584 obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
587 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
589 obj.add_event(current_time, n, Vector3(v0, v1, v2));
592 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
594 obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
597 void Animation::Loader::interval(float t)
599 current_time += t*Time::sec;
602 void Animation::Loader::keyframe(const string &n)
607 void Animation::Loader::keyframe_inline()
609 load_kf_inline(false);
612 void Animation::Loader::slopes(float s, float e)
614 check_slopes_and_control(true, (!obj.keyframes.empty() && obj.keyframes.back().control));