InterpolationAnimationT.cc 12.7 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
#define INTERPOLATIONANIMATIONT_C

#include "AnimationT.hh"

#include <vector>
48 49
#include <cassert>
#include <cmath>
Dirk Wilden's avatar
Dirk Wilden committed
50 51 52 53 54 55 56 57 58 59
//-----------------------------------------------------------------------------------------------------

/**
 * @brief Copy constructor
 *
 * This animation will copy all frames from the given animation. After the call returns they are completely
 * independent.
 *
 * @param _other The animation to copy from
 */
60 61 62 63
template<class PointT>
InterpolationAnimationT<PointT>::InterpolationAnimationT(const InterpolationAnimationT<PointT> &_other) :
        AnimationT<PointT>(_other.name_),
        skeleton_(_other.skeleton_),
Dirk Wilden's avatar
Dirk Wilden committed
64 65 66 67 68 69 70 71 72 73 74
        matrixManipulator_(_other.matrixManipulator_),
        frames_(0)
{
        
}

//-----------------------------------------------------------------------------------------------------

/**
 * @brief Creates a new empty animation
 *
Jan Möbius's avatar
Jan Möbius committed
75 76
 * @param _skeleton           The skeleton that will hold this animation
 * @param _matrixManipulator  Interpolator that translates the interpolation into matrix values
Dirk Wilden's avatar
Dirk Wilden committed
77
 */
78 79 80
template<class PointT>
InterpolationAnimationT<PointT>::InterpolationAnimationT(Skeleton* _skeleton, MatrixManipulator *_matrixManipulator) :
        skeleton_(_skeleton),
Dirk Wilden's avatar
Dirk Wilden committed
81 82 83 84 85 86 87 88
        matrixManipulator_(_matrixManipulator),
        frames_(0)
{

}

//-----------------------------------------------------------------------------------------------------

89 90
template<class PointT>
InterpolationAnimationT<PointT>::~InterpolationAnimationT()
Dirk Wilden's avatar
Dirk Wilden committed
91 92 93 94 95 96
{
  clearPoseCache();
}

//-----------------------------------------------------------------------------------------------------

97 98 99
template<class PointT>
AnimationT<PointT>* InterpolationAnimationT<PointT>::copy() {
  return new InterpolationAnimationT<PointT>(*this);
Dirk Wilden's avatar
Dirk Wilden committed
100 101 102 103
}

//-----------------------------------------------------------------------------------------------------

104 105
template<class PointT>
PoseT<PointT>* InterpolationAnimationT<PointT>::pose(unsigned int _iFrame)
Dirk Wilden's avatar
Dirk Wilden committed
106
{
107
  return pose(_iFrame, skeleton_->referencePose());
Dirk Wilden's avatar
Dirk Wilden committed
108 109 110 111 112 113 114
}

//-----------------------------------------------------------------------------------------------------

/**
 * @brief Returns a pointer to the pose calculated for the given frame
 *
Jan Möbius's avatar
Jan Möbius committed
115 116 117
 * @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
Dirk Wilden's avatar
Dirk Wilden committed
118
 */
119 120
template<class PointT>
PoseT<PointT>* InterpolationAnimationT<PointT>::pose(unsigned int _iFrame, Pose* _reference)
Dirk Wilden's avatar
Dirk Wilden committed
121 122 123 124 125 126 127 128 129
{
//          std::cerr << "Frame " << _iFrame << ": ";
  
        if (interpolatedPoses_.find(_iFrame) != interpolatedPoses_.end()) {
//           std::cerr << "(from cache)" << std::endl;
          return (interpolatedPoses_[_iFrame]);
        } else {
        
          if (_iFrame == 0) {
Jan Möbius's avatar
std fix  
Jan Möbius committed
130
            interpolatedPoses_.insert( std::make_pair(0, new Pose(*_reference)) );
Dirk Wilden's avatar
Dirk Wilden committed
131
//             std::cerr << "Insert reference to posecache. &_reference: " << _reference << ", &cacheref: " << getPose(_iFrame, _reference) << std::endl;
132
            return pose(_iFrame, _reference);
Dirk Wilden's avatar
Dirk Wilden committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
          } else {
            //Find the correct interpolator
            Interpolator* interpolator = NULL;
            unsigned long min = 0, max = 0;
            
            uint i;
            for (i=0; i<interpolators_.size(); ++i) {
              min = (i==0 ? 0.0 : calcAbsoluteMaxForInterpolator(i-1) + 1);
              max = calcAbsoluteMaxForInterpolator(i);
              if (_iFrame >= min && _iFrame <= max) {
                interpolator = interpolators_[i];
                break;
              }
            }
            
            if (interpolator == NULL) {
//               std::cerr << "No appropriate interpolator found for this frame!" << std::endl;
              return _reference;
            }
            
//             std::cerr << "Using interpolator " << i << " - ";
            
            //Create a new pose that is a copy of the reference and apply the interpolated transformations to it
            Pose *generatedPose = new Pose(*_reference);
            
            for (uint i=0; i<influencedJoints_.size(); ++i) {
159
              ACG::GLMatrixT<Scalar> transformation(generatedPose->globalMatrix(influencedJoints_[i]).raw());
Dirk Wilden's avatar
Dirk Wilden committed
160 161 162 163 164
              //The frames for each interpolator are stored from 0..max, i.e. in "interpolator local time space".
              // So, they have to be mapped to the global space here.
              TargetType precalcVal = precalculations_[interpolator][_iFrame - min];
              
              matrixManipulator_->doManipulation(transformation, precalcVal);
165
              generatedPose->setGlobalMatrix(influencedJoints_[i], transformation, false);
Dirk Wilden's avatar
Dirk Wilden committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179
            }
            
//             std::cerr << std::endl;
            
            interpolatedPoses_.insert(std::pair<unsigned long, Pose*>(_iFrame, generatedPose));
            return (interpolatedPoses_.find(_iFrame)->second);
          }
          
          
      }
}

//-----------------------------------------------------------------------------------------------------

180 181
template<class PointT>
unsigned int InterpolationAnimationT<PointT>::frameCount()
Dirk Wilden's avatar
Dirk Wilden committed
182 183 184 185 186 187
{
  return frames_;
}

//-----------------------------------------------------------------------------------------------------

188 189
template<class PointT>
void InterpolationAnimationT<PointT>::insertJointAt(unsigned int _index)
Dirk Wilden's avatar
Dirk Wilden committed
190 191 192 193 194 195
{
  //NOOP
}

//-----------------------------------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
196

197 198
template<class PointT>
void InterpolationAnimationT<PointT>::removeJointAt(unsigned int _index)
Dirk Wilden's avatar
Dirk Wilden committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212
{
  //NOOP
}

//-----------------------------------------------------------------------------------------------------

/**
 * @brief Updates the local matrix using the global matrix
 *
 * Called when a joints parent is changed.
 * Note: Does nothing at the moment
 *
 * @param _index The joints index
 */
213 214
template<class PointT>
void InterpolationAnimationT<PointT>::updateFromGlobal(unsigned int _index)
Dirk Wilden's avatar
Dirk Wilden committed
215 216 217 218 219 220
{
  //NOOP
}

//-----------------------------------------------------------------------------------------------------

221 222
template<class PointT>
void InterpolationAnimationT<PointT>::addInterpolator(InterpolationT<double> *_interpolator) {
Dirk Wilden's avatar
Dirk Wilden committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  if (_interpolator == NULL)
    return;
  
  interpolators_.push_back(_interpolator);
//   std::cerr << "Precalc for interpolator " << interpolators_.size()-1 << ":" << std::endl;
  
  std::vector < TargetType > valueVector;
  
  //Precalc values for this interpolator
  uint i=0;
  for (i=_interpolator->getMinInput()*FPS;i<=(_interpolator->getMaxInput()) * FPS;++i) {
    TargetType precalcValue;
    double input = ((double)i) / ((double)FPS);
    precalcValue = _interpolator->getValue(input);
    valueVector.push_back(precalcValue);
    
//      std::cerr << "Frame " << i << "(t=" << input << "): " << precalcValue[0] << std::endl;
  }
  
//   std::cerr << std::endl;
  
  precalculations_.insert( std::pair< Interpolator*, std::vector < TargetType > >(_interpolator, valueVector) );
  
Jan Möbius's avatar
Jan Möbius committed
246
  frames_ = std::max<long unsigned int>(frames_, i+1);
Dirk Wilden's avatar
Dirk Wilden committed
247 248 249 250
}

//-----------------------------------------------------------------------------------------------------

251 252 253
template<class PointT>
InterpolationT<typename PointT::value_type>*
InterpolationAnimationT<PointT>::interpolator(unsigned int _index)
Dirk Wilden's avatar
Dirk Wilden committed
254 255 256 257 258 259 260 261 262
{
  if ( _index < interpolators_.size() )
    return interpolators_[ _index ];
  else 
    return 0;
}

//-----------------------------------------------------------------------------------------------------

263 264
template<class PointT>
unsigned int InterpolationAnimationT<PointT>::interpolatorCount()
Dirk Wilden's avatar
Dirk Wilden committed
265 266 267 268 269 270 271 272 273
{
  return interpolators_.size();
}

//-----------------------------------------------------------------------------------------------------

/**
* \brief Calculates the last frame that interpolator _index is responsible for
*/
274 275
template<class PointT>
unsigned int InterpolationAnimationT<PointT>::calcAbsoluteMaxForInterpolator(uint _index) {
Dirk Wilden's avatar
Dirk Wilden committed
276 277 278 279 280 281 282 283 284 285 286
  assert (_index < interpolators_.size());
  
  if (_index == 0) {
    return precalculations_[interpolators_[_index]].size() - 1;
  } else {
    return precalculations_[interpolators_[_index]].size() + calcAbsoluteMaxForInterpolator(_index - 1);
  }
}

//-----------------------------------------------------------------------------------------------------

287 288
template<class PointT>
bool InterpolationAnimationT<PointT>::getMinInput(Scalar& _result) {
Dirk Wilden's avatar
Dirk Wilden committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
  if (interpolators_.size() == 0)
    return false;
  else
    _result = interpolators_[0]->getMinInput();
  
  for (uint i=0;i<interpolators_.size();++i) {
    if (interpolators_[i]->getMinInput() < _result)
      _result = interpolators_[i]->getMinInput();
  }
  
  return true;
}

//-----------------------------------------------------------------------------------------------------

304 305
template<class PointT>
bool InterpolationAnimationT<PointT>::getMaxInput(Scalar& _result) {
Dirk Wilden's avatar
Dirk Wilden committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
  if (interpolators_.size() == 0)
    return false;
  else
    _result = interpolators_[0]->getMaxInput();
  
  for (uint i=0;i<interpolators_.size();++i) {
    if (interpolators_[i]->getMaxInput() > _result)
      _result = interpolators_[i]->getMaxInput();
  }
  
  return true;
}

//-----------------------------------------------------------------------------------------------------

321 322
template<class PointT>
bool InterpolationAnimationT<PointT>::isInfluenced(int _joint) {
Dirk Wilden's avatar
Dirk Wilden committed
323 324 325 326 327 328 329 330 331
  for (uint i=0; i<influencedJoints_.size(); ++i) 
    if ( influencedJoints_[i] == _joint )
      return true;
    
  return false;
}

//-----------------------------------------------------------------------------------------------------

332 333
template<class PointT>
std::vector<int>& InterpolationAnimationT<PointT>::influencedJoints() {
Dirk Wilden's avatar
Dirk Wilden committed
334 335 336 337 338
  return influencedJoints_;
}

//-----------------------------------------------------------------------------------------------------