InterpolationAnimationT.hh 9.61 KB
Newer Older
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

Dirk Wilden's avatar
Dirk Wilden committed
43 44 45 46 47 48 49 50 51 52
#ifndef INTERPOLATIONANIMATIONT_HH
#define INTERPOLATIONANIMATIONT_HH

#include <vector>
#include <map>

#include "../PoseT.hh"
#include "InterpolationT.hh"
#include "InterpolationMatrixManipulatorT.hh"

53
template<class PointT>
Dirk Wilden's avatar
Dirk Wilden committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
class InterpolationAnimationT : public AnimationT<PointT>
{ 
  template<typename>
  friend class SkeletonT;

  public:
    typedef PointT                                          Point;
    typedef typename Point::value_type                      Scalar;
    typedef PoseT<PointT>                                   Pose;
    typedef InterpolationMatrixManipulatorT<Scalar>         MatrixManipulator;
    typedef InterpolationT<Scalar>                          Interpolator;
    typedef std::vector<Scalar>                             TargetType;
  
  public:
    InterpolationAnimationT(const InterpolationAnimationT<PointT> &_other);
    InterpolationAnimationT(Skeleton* _skeleton, MatrixManipulator *_matrixManipulator);
    virtual ~InterpolationAnimationT();
    
    virtual AnimationT<PointT>* copy();
    
    virtual void updateFromGlobal(unsigned int _index);
    
    virtual bool getMinInput(Scalar& _result);
    virtual bool getMaxInput(Scalar& _result);

  protected:

81 82 83 84 85 86 87 88 89 90 91 92 93
    Skeleton* skeleton_;

    std::vector<int> influencedJoints_;
    std::vector< Interpolator* > interpolators_;
    //Hier muss es einen Mapper geben, der weiß, wie er auf die Matrix aus der Pose die Werte, die der Interpolator
    //erzeugt, anwendet.
    std::map < Interpolator*, std::vector < TargetType > > precalculations_;
    MatrixManipulator* matrixManipulator_;
    unsigned int frames_;

    std::map < unsigned int, Pose* > interpolatedPoses_;

    unsigned int calcAbsoluteMaxForInterpolator(uint _index);
Dirk Wilden's avatar
Dirk Wilden committed
94 95
  
  public:
96 97
    static const int FPS = 60;

Dirk Wilden's avatar
Dirk Wilden committed
98
  public:
Jan Möbius's avatar
Jan Möbius committed
99 100 101 102 103 104 105 106

    //===========================================================================
    /** @name Frame access
     *
     * There is one pose per frame.
     * @{ */
    //===========================================================================

Jan Möbius's avatar
Jan Möbius committed
107 108 109 110 111
    /** \brief Returns a pointer to the pose calculated for the given frame
      *
      * @param _iFrame    The frame number for which the pose should be calculated.
      *                   This is always from 0..frames even if the animation starts with an input value other than 0.
      */
112
    virtual Pose* pose(unsigned int _iFrame);
Jan Möbius's avatar
Jan Möbius committed
113 114 115 116 117 118 119 120

    /**
     * @brief Returns a pointer to the pose calculated for the given frame
     *
     * @param _iFrame    The frame number for which the pose should be calculated.
     *                   This is always from 0..frames even if the animation starts with an input value other than 0.
     * @param _reference Reference pose
     */
121
    virtual Pose* pose(unsigned int _iFrame, Pose* _reference);
Jan Möbius's avatar
Jan Möbius committed
122 123 124 125

    /**
     * @brief Returns the number of frames stored in this pose
     */
126
    inline unsigned int frameCount();
Dirk Wilden's avatar
Dirk Wilden committed
127

Jan Möbius's avatar
Jan Möbius committed
128 129 130 131 132 133 134 135 136 137
    /** @} */


    //===========================================================================
    /** @name Synchronization
     *
     * Use these methods to keep the poses in sync with the number (and indices) of the joints.
     * @{ */
    //===========================================================================

Jan Möbius's avatar
Jan Möbius committed
138 139 140 141 142 143
    /** \brief Called by the skeleton as a new joint is inserted
     *
     * The call is dispatched to all poses stored in the animation. See BaseNode::insert_at for more information.
     *
     * @param _index The new joint is inserted at this position. Insert new joints at the end by passing SkeletonT::joints_.size as parameter.
     */
144
    virtual void insertJointAt(unsigned int _index);
Jan Möbius's avatar
Jan Möbius committed
145 146 147 148 149 150 151

    /** \brief Called by the skeleton as a joint is deleted
     *
     * The call is dispatched to all poses stored in this animation. See BasePoseT::remove_at for more information.
     *
     * @param _index The index of the joint that is being deleted.
     */
152
    virtual void removeJointAt(unsigned int _index);
Jan Möbius's avatar
Jan Möbius committed
153 154

    /** @} */
155
    
Jan Möbius's avatar
Jan Möbius committed
156 157 158 159 160 161 162

    //===========================================================================
    /** @name Interpolators access
     *
     * We use only interpolators with time as input.
     * @{ */
    //===========================================================================
Jan Möbius's avatar
Jan Möbius committed
163 164 165 166 167

    /** \brief Add an interpolator
     *
     * @param _interpolator New interpolator
     */
168
    void          addInterpolator(InterpolationT<double> *_interpolator);
Jan Möbius's avatar
Jan Möbius committed
169 170 171 172 173 174

    /** \brief Get the i-th interpolator
     *
     * @param _index Number of the interpolator
     * @return Interpolator
     */
175
    Interpolator* interpolator(unsigned int _index);
Jan Möbius's avatar
Jan Möbius committed
176 177 178 179 180

    /** \brief Get the number of interpolators
     *
     * @return Number of interpolators
     */
181
    unsigned int  interpolatorCount();
Jan Möbius's avatar
Jan Möbius committed
182
    /** @} */
183

Jan Möbius's avatar
Jan Möbius committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    //===========================================================================
    /** @name InfluencedJoints access
     *
     * @{ */
    //===========================================================================

    void addInfluencedJoint(int _joint)
    {
      influencedJoints_.push_back(_joint);
    }

    bool isInfluenced(int _joint);

    std::vector<int>& influencedJoints();

    /** @} */

    //===========================================================================
    /** @name MatrixManipulator access
     *
     * @{ */
    //===========================================================================

Jan Möbius's avatar
Jan Möbius committed
207 208 209 210
    /** \brief Get the matrix manipulator
     *
     * @return MatrixManipulator
     */
Jan Möbius's avatar
Jan Möbius committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    MatrixManipulator* matrixManipulator() { return matrixManipulator_; }

    /** @} */

    Pose* getReference() { return pose(0); }

    virtual void clearPoseCache()
    {
      if (interpolatedPoses_.size() == 1 && interpolatedPoses_.find(0) != interpolatedPoses_.end())
        return;

      if (interpolatedPoses_.find(0) != interpolatedPoses_.end()) {
        //  Pose* frame0 = (interpolatedPoses_.find(0)->second);

        if (interpolatedPoses_.size() > 1) {
          typename std::map<unsigned int, Pose*>::iterator ip_it = interpolatedPoses_.begin();
          ++ip_it;
          for (; ip_it != interpolatedPoses_.end(); ++ip_it) {
            delete ip_it->second;
            interpolatedPoses_.erase(ip_it);
Dirk Wilden's avatar
Dirk Wilden committed
231 232
          }
        }
Jan Möbius's avatar
Jan Möbius committed
233 234 235 236 237 238 239 240

        // interpolatedPoses_.insert(std::pair<unsigned int, Pose>(0, frame0));
      } else {
        interpolatedPoses_.clear();
      }
    }


Dirk Wilden's avatar
Dirk Wilden committed
241 242 243 244 245 246 247 248 249
};

//=============================================================================
#if defined(INCLUDE_TEMPLATES) && !defined(INTERPOLATIONANIMATIONT_C)
#define INTERPOLATIONANIMATIONT_TEMPLATES
#include "InterpolationAnimationT.cc"
#endif
//=============================================================================

Jan Möbius's avatar
Jan Möbius committed
250
#endif //INTERPOLATIONANIMATIONT_HH