2 #include <msp/core/maputils.h>
3 #include <msp/datafile/collection.h>
4 #include <msp/interpolate/linearspline.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, 1.0f, 1.0f);
52 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float slope)
54 add_keyframe(t, kf, slope, slope);
57 void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame &kf, float ss, float es)
59 RefPtr<const KeyFrame> kfr(&kf);
61 add_keyframe(t, kfr, ss, es);
65 void Animation::add_keyframe(const Time::TimeDelta &t, const RefPtr<const KeyFrame> &kf, float ss, float es)
67 if(keyframes.empty() && t!=Time::zero)
68 throw invalid_argument("Animation::add_keyframe");
69 if(!keyframes.empty() && t<keyframes.back().time)
70 throw invalid_argument("Animation::add_keyframe");
71 if(kf->get_pose() && armature && kf->get_pose()->get_armature()!=armature)
72 throw invalid_argument("Animation::add_keyframe");
74 const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
75 for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
77 KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
78 if(j!=kf_uniforms.end() && j->second.size!=i->size)
79 throw invalid_argument("Animation::add_keyframe");
82 if(kf->get_pose() && !armature)
83 armature = kf->get_pose()->get_armature();
91 keyframes.push_back(tkf);
93 for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
96 for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
97 found = (j->name==i->first);
100 uniforms.push_back(UniformInfo(i->first, i->second.size));
104 void Animation::create_curves()
106 for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
110 typedef ValueCurve<3>::Knot Knot;
111 vector<Knot> positions;
114 for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
116 positions.push_back(Knot(i->time/Time::sec, i->keyframe->get_transform().get_position()));
117 const Transform::AngleVector3 &euler = i->keyframe->get_transform().get_euler();
118 eulers.push_back(Knot(i->time/Time::sec, Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians())));
119 scales.push_back(Knot(i->time/Time::sec, i->keyframe->get_transform().get_scale()));
122 curves.reserve(3+uniforms.size());
123 curves.push_back(new ValueCurve<3>(POSITION, positions));
124 curves.push_back(new ValueCurve<3>(EULER, eulers));
125 curves.push_back(new ValueCurve<3>(SCALE, scales));
127 for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
130 create_uniform_curve<1>(i->name);
132 create_uniform_curve<2>(i->name);
134 create_uniform_curve<3>(i->name);
136 create_uniform_curve<4>(i->name);
141 void Animation::create_uniform_curve(const string &name)
143 typedef typename ValueCurve<N>::Knot Knot;
146 for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
148 const KeyFrame::UniformMap &kf_uniforms = i->keyframe->get_uniforms();
149 const KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(name);
150 if(j!=kf_uniforms.end())
151 knots.push_back(Knot(i->time/Time::sec, Interpolate::SplineValue<float, N>::make(j->second.values)));
154 curves.push_back(new ValueCurve<N>(UNIFORM, knots));
157 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
163 events.push_back(event);
166 const Time::TimeDelta &Animation::get_duration() const
168 if(keyframes.empty())
171 return keyframes.back().time;
174 void Animation::set_looping(bool l)
180 Animation::Curve::Curve(CurveTarget t):
186 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
188 spline(Interpolate::LinearSpline<float, N>(k))
192 void Animation::ValueCurve<N>::apply(float, Matrix &) const
194 throw invalid_operation("ValueCurve::apply");
198 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
200 Vector3 value = spline(x);
202 matrix.translate(value);
203 else if(target==EULER)
205 matrix.rotate(value.z, Vector3(0, 0, 1));
206 matrix.rotate(value.y, Vector3(0, 1, 0));
207 matrix.rotate(value.x, Vector3(1, 0, 0));
209 else if(target==SCALE)
212 throw invalid_operation("ValueCurve::apply");
216 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
219 typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
220 for(unsigned i=0; i<N; ++i)
221 uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
225 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
231 Animation::Iterator::Iterator(const Animation &a):
233 event_iter(animation->events.begin()),
238 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
240 const Time::TimeDelta &duration = animation->get_duration();
245 if(animation->looping)
247 while(elapsed>=duration)
250 else if(elapsed>=duration)
259 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
261 for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
262 observer.animation_event(0, event_iter->name, event_iter->value);
265 Matrix Animation::Iterator::get_matrix() const
268 for(unsigned i=0; i<3; ++i)
269 animation->curves[i]->apply(elapsed/Time::sec, matrix);
273 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
275 if(i>=animation->uniforms.size())
276 throw out_of_range("Animation::Iterator::get_uniform");
278 KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
279 animation->curves[3+i]->apply(elapsed/Time::sec, uni);
283 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
285 if(!animation->armature)
286 throw invalid_operation("Animation::Iterator::get_pose_matrix");
287 if(link>animation->armature->get_max_link_index())
288 throw out_of_range("Animation::Iterator::get_pose_matrix");
290 throw logic_error("pose animations are currently unimplemented");
294 Animation::Loader::Loader(Animation &a):
295 DataFile::CollectionObjectLoader<Animation>(a, 0)
300 Animation::Loader::Loader(Animation &a, Collection &c):
301 DataFile::CollectionObjectLoader<Animation>(a, &c)
306 void Animation::Loader::init()
310 add("armature", &Animation::armature);
311 add("event", &Loader::event);
312 add("event", &Loader::event1i);
313 add("event", &Loader::event1f);
314 add("event", &Loader::event2f);
315 add("event", &Loader::event3f);
316 add("event", &Loader::event4f);
317 add("interval", &Loader::interval);
318 add("keyframe", &Loader::keyframe);
319 add("keyframe", &Loader::keyframe_inline);
320 add("looping", &Animation::looping);
321 add("slopes", &Loader::slopes);
324 void Animation::Loader::finish()
329 void Animation::Loader::event(const string &n)
331 obj.add_event(current_time, n);
334 void Animation::Loader::event1i(const string &n, int v)
336 obj.add_event(current_time, n, v);
339 void Animation::Loader::event1f(const string &n, float v)
341 obj.add_event(current_time, n, v);
344 void Animation::Loader::event2f(const string &n, float v0, float v1)
346 obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
349 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
351 obj.add_event(current_time, n, Vector3(v0, v1, v2));
354 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
356 obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
359 void Animation::Loader::interval(float t)
361 current_time += t*Time::sec;
364 void Animation::Loader::keyframe(const string &n)
366 obj.add_keyframe(current_time, get_collection().get<KeyFrame>(n), start_slope, end_slope);
367 start_slope = end_slope;
371 void Animation::Loader::keyframe_inline()
373 RefPtr<KeyFrame> kf = new KeyFrame;
375 load_sub(*kf, get_collection());
379 obj.add_keyframe(current_time, kf, start_slope, end_slope);
380 start_slope = end_slope;
384 void Animation::Loader::slopes(float s, float e)