Make animation curve creation more generic
authorMikko Rasa <tdb@tdb.fi>
Fri, 7 Jun 2019 22:30:11 +0000 (01:30 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 7 Jun 2019 22:30:11 +0000 (01:30 +0300)
source/animation.cpp
source/animation.h

index dbdd05bafd741262910b1832a10a8590dd1ce88b..4be00d73b3299012df11280df62e677f52039852 100644 (file)
@@ -107,51 +107,57 @@ void Animation::create_curves()
                delete *i;
        curves.clear();
 
-       typedef ValueCurve<3>::Knot Knot;
-       vector<Knot> positions;
-       vector<Knot> eulers;
-       vector<Knot> scales;
-       for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
-       {
-               positions.push_back(Knot(i->time/Time::sec, i->keyframe->get_transform().get_position()));
-               const Transform::AngleVector3 &euler = i->keyframe->get_transform().get_euler();
-               eulers.push_back(Knot(i->time/Time::sec, Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians())));
-               scales.push_back(Knot(i->time/Time::sec, i->keyframe->get_transform().get_scale()));
-       }
-
        curves.reserve(3+uniforms.size());
-       curves.push_back(new ValueCurve<3>(POSITION, positions));
-       curves.push_back(new ValueCurve<3>(EULER, eulers));
-       curves.push_back(new ValueCurve<3>(SCALE, scales));
+       create_curve<3>(POSITION, &extract_position);
+       create_curve<3>(EULER, &extract_euler);
+       create_curve<3>(SCALE, &extract_scale);
 
        for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
        {
                if(i->size==1)
-                       create_uniform_curve<1>(i->name);
+                       create_curve<1>(UNIFORM, ExtractUniform<1>(i->name));
                else if(i->size==2)
-                       create_uniform_curve<2>(i->name);
+                       create_curve<2>(UNIFORM, ExtractUniform<2>(i->name));
                else if(i->size==3)
-                       create_uniform_curve<3>(i->name);
+                       create_curve<3>(UNIFORM, ExtractUniform<3>(i->name));
                else if(i->size==4)
-                       create_uniform_curve<4>(i->name);
+                       create_curve<4>(UNIFORM, ExtractUniform<4>(i->name));
        }
 }
 
-template<unsigned N>
-void Animation::create_uniform_curve(const string &name)
+template<unsigned N, typename T>
+void Animation::create_curve(CurveTarget target, const T &extract)
 {
        typedef typename ValueCurve<N>::Knot Knot;
 
        vector<Knot> knots;
        for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
        {
-               const KeyFrame::UniformMap &kf_uniforms = i->keyframe->get_uniforms();
-               const KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(name);
-               if(j!=kf_uniforms.end())
-                       knots.push_back(Knot(i->time/Time::sec, Interpolate::SplineValue<float, N>::make(j->second.values)));
+               typename Interpolate::SplineValue<float, N>::Type value;
+               if(extract(*i->keyframe, value))
+                       knots.push_back(Knot(i->time/Time::sec, value));
        }
 
-       curves.push_back(new ValueCurve<N>(UNIFORM, knots));
+       curves.push_back(new ValueCurve<N>(target, knots));
+}
+
+bool Animation::extract_position(const KeyFrame &kf, Vector3 &value)
+{
+       value = kf.get_transform().get_position();
+       return true;
+}
+
+bool Animation::extract_euler(const KeyFrame &kf, Vector3 &value)
+{
+       const Transform::AngleVector3 &euler = kf.get_transform().get_euler();
+       value = Vector3(euler.x.radians(), euler.y.radians(), euler.z.radians());
+       return true;
+}
+
+bool Animation::extract_scale(const KeyFrame &kf, Vector3 &value)
+{
+       value = kf.get_transform().get_scale();
+       return true;
 }
 
 void Animation::add_event(const Time::TimeDelta &t, const string &n, const Variant &v)
@@ -222,6 +228,19 @@ void Animation::ValueCurve<N>::apply(float x, KeyFrame::AnimatedUniform &uni) co
 }
 
 
+template<unsigned N>
+bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
+{
+       const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
+       const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
+       if(i==kf_uniforms.end())
+               return false;
+
+       value = Interpolate::SplineValue<float, N>::make(i->second.values);
+       return true;
+}
+
+
 Animation::UniformInfo::UniformInfo(const string &n, unsigned s):
        name(n),
        size(s)
index 83b369bfca1f1e975df889de113d9655ca77db80..92c177b685fead92aa15ee34c8721af1e16e4924 100644 (file)
@@ -86,6 +86,16 @@ private:
                virtual void apply(float, KeyFrame::AnimatedUniform &) const;
        };
 
+       template<unsigned N>
+       struct ExtractUniform
+       {
+               const std::string &name;
+
+               ExtractUniform(const std::string &n): name(n) { }
+
+               bool operator()(const KeyFrame &, typename Interpolate::SplineValue<float, N>::Type &) const;
+       };
+
        struct TimedKeyFrame
        {
                Time::TimeDelta time;
@@ -156,8 +166,11 @@ private:
        void add_keyframe(const Time::TimeDelta &, const RefPtr<const KeyFrame> &, float, float);
        void prepare_keyframe(TimedKeyFrame &);
        void create_curves();
-       template<unsigned N>
-       void create_uniform_curve(const std::string &);
+       template<unsigned N, typename T>
+       void create_curve(CurveTarget target, const T &);
+       static bool extract_position(const KeyFrame &, Vector3 &);
+       static bool extract_euler(const KeyFrame &, Vector3 &);
+       static bool extract_scale(const KeyFrame &, Vector3 &);
 public:
        void add_event(const Time::TimeDelta &, const std::string &, const Variant & = Variant());