]> git.tdb.fi Git - libs/gl.git/blobdiff - source/animation/animation.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / animation / animation.cpp
index b62f5dd9c74e035ed7fe0ab39ef69c46b3cbf714..a79e1f8d9860d9e31cdde6baf083cb3423b6c392 100644 (file)
@@ -1,4 +1,5 @@
 #include <cmath>
+#include <msp/core/algorithm.h>
 #include <msp/core/maputils.h>
 #include <msp/datafile/collection.h>
 #include <msp/fs/utils.h>
@@ -15,18 +16,13 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-Animation::Animation():
-       armature(0),
-       looping(false)
-{ }
-
 Animation::~Animation()
 {
-       for(vector<TimedKeyFrame>::iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
-               if(i->owned)
-                       delete i->keyframe;
-       for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
-               delete *i;
+       for(TimedKeyFrame &k: keyframes)
+               if(k.owned)
+                       delete k.keyframe;
+       for(Curve *c: curves)
+               delete c;
 }
 
 void Animation::set_armature(const Armature &a)
@@ -116,17 +112,17 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float
                ctrn.set_scale(last_trn.get_scale()*(1-x)+trn.get_scale()*x);
                ckf->set_transform(ctrn);
 
-               for(KeyFrame::UniformMap::const_iterator j=kf_unis.begin(); j!=kf_unis.end(); ++j)
+               for(const auto &kvp: kf_unis)
                {
-                       KeyFrame::UniformMap::const_iterator k = last_unis.find(j->first);
+                       auto k = last_unis.find(kvp.first);
                        if(k==last_unis.end())
                                continue;
 
-                       KeyFrame::AnimatedUniform uni(j->second.size, 0.0f);
+                       KeyFrame::AnimatedUniform uni(kvp.second.size, 0.0f);
                        for(unsigned c=0; c<uni.size; ++c)
-                               uni.values[c] = k->second.values[c]*(1-x)+j->second.values[c]*x;
+                               uni.values[c] = k->second.values[c]*(1-x)+kvp.second.values[c]*x;
 
-                       ckf->set_uniform(j->first, uni);
+                       ckf->set_uniform(kvp.first, uni);
                }
 
                add_keyframe(t, ckf, true, true);
@@ -147,10 +143,10 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool
                throw invalid_argument("Animation::add_keyframe");
 
        const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
-       for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+       for(const UniformInfo &u: uniforms)
        {
-               KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
-               if(j!=kf_uniforms.end() && j->second.size!=i->size)
+               auto j = kf_uniforms.find(u.name);
+               if(j!=kf_uniforms.end() && j->second.size!=u.size)
                        throw invalid_argument("Animation::add_keyframe");
        }
 
@@ -165,21 +161,17 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool
 
        keyframes.push_back(tkf);
 
-       for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
+       for(const auto &kvp: kf_uniforms)
        {
-               bool found = false;
-               for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
-                       found = (j->name==i->first);
-
-               if(!found)
-                       uniforms.push_back(UniformInfo(i->first, i->second.size));
+               if(find_member(uniforms, kvp.first, &UniformInfo::name)==uniforms.end())
+                       uniforms.emplace_back(kvp.first, kvp.second.size);
        }
 }
 
 void Animation::create_curves()
 {
-       for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
-               delete *i;
+       for(Curve *c: curves)
+               delete c;
        curves.clear();
 
        curves.reserve(6+uniforms.size());
@@ -188,16 +180,16 @@ void Animation::create_curves()
        create_curve(SCALE, Transform::SCALE, &extract_scale);
 
        uniform_curve_offset = curves.size();
-       for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+       for(const UniformInfo &u: uniforms)
        {
-               if(i->size==1)
-                       create_curve<1>(UNIFORM, -1, ExtractUniform<1>(i->name));
-               else if(i->size==2)
-                       create_curve<2>(UNIFORM, -1, ExtractUniform<2>(i->name));
-               else if(i->size==3)
-                       create_curve<3>(UNIFORM, -1, ExtractUniform<3>(i->name));
-               else if(i->size==4)
-                       create_curve<4>(UNIFORM, -1, ExtractUniform<4>(i->name));
+               if(u.size==1)
+                       create_curve<1>(UNIFORM, -1, ExtractUniform<1>(u.name));
+               else if(u.size==2)
+                       create_curve<2>(UNIFORM, -1, ExtractUniform<2>(u.name));
+               else if(u.size==3)
+                       create_curve<3>(UNIFORM, -1, ExtractUniform<3>(u.name));
+               else if(u.size==4)
+                       create_curve<4>(UNIFORM, -1, ExtractUniform<4>(u.name));
        }
 }
 
@@ -205,10 +197,10 @@ void Animation::create_curve(CurveTarget target, Transform::ComponentMask mask,
 {
        Transform::ComponentMask all = mask;
        Transform::ComponentMask any = Transform::NONE;
-       for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
+       for(const TimedKeyFrame &k: keyframes)
        {
-               all = all&i->keyframe->get_transform().get_mask();
-               any = any|i->keyframe->get_transform().get_mask();
+               all = all&k.keyframe->get_transform().get_mask();
+               any = any|k.keyframe->get_transform().get_mask();
        }
 
        if(all==mask)
@@ -232,17 +224,17 @@ void Animation::create_curve(CurveTarget target, int component, const T &extract
 
        vector<Knot> knots;
        unsigned n_control = 0;
-       for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
+       for(const TimedKeyFrame &k: keyframes)
        {
-               if(i->control && knots.empty())
+               if(k.control && knots.empty())
                        continue;
 
                typename Interpolate::SplineValue<float, N>::Type value;
-               if(extract(*i->keyframe, value))
+               if(extract(*k.keyframe, value))
                {
                        typename Knot::Value dvalue = value;
-                       float x = i->time/Time::sec;
-                       if(i->control)
+                       float x = k.time/Time::sec;
+                       if(k.control)
                        {
                                ++n_control;
                                if(n_control>2)
@@ -254,17 +246,17 @@ void Animation::create_curve(CurveTarget target, int component, const T &extract
                                {
                                        typename Knot::Value cv = knots.back().y;
                                        knots.back().y = (knots[knots.size()-2].y+cv*2.0)/3.0;
-                                       knots.push_back(Knot(x, (dvalue+cv*2.0)/3.0));
+                                       knots.emplace_back(x, (dvalue+cv*2.0/3.0));
                                }
                                else if(n_control==0 && !knots.empty())
                                {
                                        typename Knot::Value prev = knots.back().y;
-                                       knots.push_back(Knot(knots.back().x, (prev*2.0+dvalue)/3.0));
-                                       knots.push_back(Knot(x, (prev+dvalue*2.0)/3.0));
+                                       knots.emplace_back(knots.back().x, (prev*2.0+dvalue)/3.0);
+                                       knots.emplace_back(x, (prev+dvalue*2.0/3.0));
                                }
                                n_control = 0;
                        }
-                       knots.push_back(Knot(x, value));
+                       knots.emplace_back(x, value);
                }
        }
        
@@ -414,7 +406,7 @@ 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);
+       auto i = kf_uniforms.find(name);
        if(i==kf_uniforms.end())
                return false;
 
@@ -492,23 +484,12 @@ Matrix Animation::Iterator::get_pose_matrix(unsigned link) const
 }
 
 
-Animation::Loader::Loader(Animation &a):
-       DataFile::CollectionObjectLoader<Animation>(a, 0)
-{
-       init();
-}
-
-Animation::Loader::Loader(Animation &a, Collection &c):
-       DataFile::CollectionObjectLoader<Animation>(a, &c)
-{
-       init();
-}
-
-void Animation::Loader::init()
+Animation::Loader::Loader(Animation &a, Collection *c):
+       DataFile::CollectionObjectLoader<Animation>(a, c),
+       start_slope(1),
+       end_slope(1),
+       slopes_set(0)
 {
-       start_slope = 1;
-       end_slope = 1;
-       slopes_set = 0;
        add("armature", &Animation::armature);
        add("control_keyframe", &Loader::control_keyframe);
        add("control_keyframe", &Loader::control_keyframe_inline);