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 curves.reserve(3+uniforms.size());
111 create_curve<3>(POSITION, &extract_position);
112 create_curve<3>(EULER, &extract_euler);
113 create_curve<3>(SCALE, &extract_scale);
115 for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
118 create_curve<1>(UNIFORM, ExtractUniform<1>(i->name));
120 create_curve<2>(UNIFORM, ExtractUniform<2>(i->name));
122 create_curve<3>(UNIFORM, ExtractUniform<3>(i->name));
124 create_curve<4>(UNIFORM, ExtractUniform<4>(i->name));
128 template<unsigned N, typename T>
129 void Animation::create_curve(CurveTarget target, const T &extract)
131 typedef typename ValueCurve<N>::Knot Knot;
134 for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
136 typename Interpolate::SplineValue<float, N>::Type value;
137 if(extract(*i->keyframe, value))
138 knots.push_back(Knot(i->time/Time::sec, value));
141 curves.push_back(new ValueCurve<N>(target, knots));
144 bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
146 value = kf.get_transform().get_position();
150 bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
152 const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
153 value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
157 bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
159 value = kf.get_transform().get_scale();
163 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
169 events.push_back(event);
172 const Time::TimeDelta &Animation::get_duration() const
174 if(keyframes.empty())
177 return keyframes.back().time;
180 void Animation::set_looping(bool l)
186 Animation::Curve::Curve(CurveTarget t):
192 Animation::ValueCurve<N>::ValueCurve(CurveTarget t, const vector<Knot> &k):
194 spline(Interpolate::LinearSpline<float, N>(k))
198 void Animation::ValueCurve<N>::apply(float, Matrix &) const
200 throw invalid_operation("ValueCurve::apply");
204 void Animation::ValueCurve<3>::apply(float x, Matrix &matrix) const
206 Vector3 value = spline(x);
208 matrix.translate(value);
209 else if(target==EULER)
211 matrix.rotate(value.z, Vector3(0, 0, 1));
212 matrix.rotate(value.y, Vector3(0, 1, 0));
213 matrix.rotate(value.x, Vector3(1, 0, 0));
215 else if(target==SCALE)
218 throw invalid_operation("ValueCurve::apply");
222 void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) const
225 typename Interpolate::Spline<float, 3, N>::Value value = spline(x);
226 for(unsigned i=0; i<N; ++i)
227 uni.values[i] = Interpolate::SplineValue<float, N>::get(value, i);
232 bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
234 const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
235 const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
236 if(i==kf_uniforms.end())
239 value = Interpolate::SplineValue<float, N>::make(i->second.values);
244 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
250 Animation::Iterator::Iterator(const Animation &a):
252 event_iter(animation->events.begin()),
257 Animation::Iterator &Animation::Iterator::operator+=(const Time::TimeDelta &t)
259 const Time::TimeDelta &duration = animation->get_duration();
264 if(animation->looping)
266 while(elapsed>=duration)
269 else if(elapsed>=duration)
278 void Animation::Iterator::dispatch_events(AnimationEventObserver &observer)
280 for(; (event_iter!=animation->events.end() && event_iter->time<=elapsed); ++event_iter)
281 observer.animation_event(0, event_iter->name, event_iter->value);
284 Matrix Animation::Iterator::get_matrix() const
287 for(unsigned i=0; i<3; ++i)
288 animation->curves[i]->apply(elapsed/Time::sec, matrix);
292 KeyFrame::AnimatedUniform Animation::Iterator::get_uniform(unsigned i) const
294 if(i>=animation->uniforms.size())
295 throw out_of_range("Animation::Iterator::get_uniform");
297 KeyFrame::AnimatedUniform uni(animation->uniforms[i].size, 0.0f);
298 animation->curves[3+i]->apply(elapsed/Time::sec, uni);
302 Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
304 if(!animation->armature)
305 throw invalid_operation("Animation::Iterator::get_pose_matrix");
306 if(link>animation->armature->get_max_link_index())
307 throw out_of_range("Animation::Iterator::get_pose_matrix");
309 throw logic_error("pose animations are currently unimplemented");
313 Animation::Loader::Loader(Animation &a):
314 DataFile::CollectionObjectLoader<Animation>(a, 0)
319 Animation::Loader::Loader(Animation &a, Collection &c):
320 DataFile::CollectionObjectLoader<Animation>(a, &c)
325 void Animation::Loader::init()
329 add("armature", &Animation::armature);
330 add("event", &Loader::event);
331 add("event", &Loader::event1i);
332 add("event", &Loader::event1f);
333 add("event", &Loader::event2f);
334 add("event", &Loader::event3f);
335 add("event", &Loader::event4f);
336 add("interval", &Loader::interval);
337 add("keyframe", &Loader::keyframe);
338 add("keyframe", &Loader::keyframe_inline);
339 add("looping", &Animation::looping);
340 add("slopes", &Loader::slopes);
343 void Animation::Loader::finish()
348 void Animation::Loader::event(const string &n)
350 obj.add_event(current_time, n);
353 void Animation::Loader::event1i(const string &n, int v)
355 obj.add_event(current_time, n, v);
358 void Animation::Loader::event1f(const string &n, float v)
360 obj.add_event(current_time, n, v);
363 void Animation::Loader::event2f(const string &n, float v0, float v1)
365 obj.add_event(current_time, n, LinAl::Vector<float, 2>(v0, v1));
368 void Animation::Loader::event3f(const string &n, float v0, float v1, float v2)
370 obj.add_event(current_time, n, Vector3(v0, v1, v2));
373 void Animation::Loader::event4f(const string &n, float v0, float v1, float v2, float v3)
375 obj.add_event(current_time, n, Vector4(v0, v1, v2, v3));
378 void Animation::Loader::interval(float t)
380 current_time += t*Time::sec;
383 void Animation::Loader::keyframe(const string &n)
385 obj.add_keyframe(current_time, get_collection().get<KeyFrame>(n), start_slope, end_slope);
386 start_slope = end_slope;
390 void Animation::Loader::keyframe_inline()
392 RefPtr<KeyFrame> kf = new KeyFrame;
394 load_sub(*kf, get_collection());
398 obj.add_keyframe(current_time, kf, start_slope, end_slope);
399 start_slope = end_slope;
403 void Animation::Loader::slopes(float s, float e)