Developer Documentation
InterpolationAnimationT_impl.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 #define INTERPOLATIONANIMATIONT_C
45 
46 #include "AnimationT.hh"
47 
48 #include <vector>
49 #include <cassert>
50 #include <cmath>
51 //-----------------------------------------------------------------------------------------------------
52 
61 template<class PointT>
63  AnimationT<PointT>(_other.name_),
64  skeleton_(_other.skeleton_),
65  matrixManipulator_(_other.matrixManipulator_),
66  frames_(0)
67 {
68 
69 }
70 
71 //-----------------------------------------------------------------------------------------------------
72 
79 template<class PointT>
81  skeleton_(_skeleton),
82  matrixManipulator_(_matrixManipulator),
83  frames_(0)
84 {
85 
86 }
87 
88 //-----------------------------------------------------------------------------------------------------
89 
90 template<class PointT>
92 {
93  clearPoseCache();
94 }
95 
96 //-----------------------------------------------------------------------------------------------------
97 
98 template<class PointT>
100  return new InterpolationAnimationT<PointT>(*this);
101 }
102 
103 //-----------------------------------------------------------------------------------------------------
104 
105 template<class PointT>
107 {
108  return pose(_iFrame, skeleton_->referencePose());
109 }
110 
111 //-----------------------------------------------------------------------------------------------------
112 
113 template<class PointT>
114 PoseT<PointT>* InterpolationAnimationT<PointT>::pose(unsigned int _iFrame, Pose* _reference)
115 {
116 // std::cerr << "Frame " << _iFrame << ": ";
117 
118  if (interpolatedPoses_.find(_iFrame) != interpolatedPoses_.end()) {
119 // std::cerr << "(from cache)" << std::endl;
120  return (interpolatedPoses_[_iFrame]);
121  } else {
122 
123  if (_iFrame == 0) {
124  interpolatedPoses_.insert( std::make_pair(0, new Pose(*_reference)) );
125 // std::cerr << "Insert reference to posecache. &_reference: " << _reference << ", &cacheref: " << getPose(_iFrame, _reference) << std::endl;
126  return pose(_iFrame, _reference);
127  } else {
128  //Find the correct interpolator
129  Interpolator* interpolator = NULL;
130  unsigned long min = 0;
131 
132  uint i;
133  for (i=0; i<interpolators_.size(); ++i) {
134  min = (i==0 ? 0.0 : calcAbsoluteMaxForInterpolator(i-1) + 1);
135  const unsigned long max = calcAbsoluteMaxForInterpolator(i);
136  if (_iFrame >= min && _iFrame <= max) {
137  interpolator = interpolators_[i];
138  break;
139  }
140  }
141 
142  if (interpolator == NULL) {
143 // std::cerr << "No appropriate interpolator found for this frame!" << std::endl;
144  return _reference;
145  }
146 
147 // std::cerr << "Using interpolator " << i << " - ";
148 
149  //Create a new pose that is a copy of the reference and apply the interpolated transformations to it
150  Pose *generatedPose = new Pose(*_reference);
151 
152  for (uint i=0; i<influencedJoints_.size(); ++i) {
153  ACG::GLMatrixT<Scalar> transformation(generatedPose->globalMatrix(influencedJoints_[i]).raw());
154  //The frames for each interpolator are stored from 0..max, i.e. in "interpolator local time space".
155  // So, they have to be mapped to the global space here.
156  TargetType precalcVal = precalculations_[interpolator][_iFrame - min];
157 
158  matrixManipulator_->doManipulation(transformation, precalcVal);
159  generatedPose->setGlobalMatrix(influencedJoints_[i], transformation, false);
160  }
161 
162 // std::cerr << std::endl;
163 
164  interpolatedPoses_.insert(std::pair<unsigned long, Pose*>(_iFrame, generatedPose));
165  return (interpolatedPoses_.find(_iFrame)->second);
166  }
167 
168 
169  }
170 }
171 
172 //-----------------------------------------------------------------------------------------------------
173 
174 template<class PointT>
176 {
177  return frames_;
178 }
179 
180 //-----------------------------------------------------------------------------------------------------
181 
182 template<class PointT>
184 {
185  //NOOP
186 }
187 
188 //-----------------------------------------------------------------------------------------------------
189 
190 
191 template<class PointT>
193 {
194  //NOOP
195 }
196 
197 //-----------------------------------------------------------------------------------------------------
198 
207 template<class PointT>
209 {
210  //NOOP
211 }
212 
213 //-----------------------------------------------------------------------------------------------------
214 
215 template<class PointT>
217  if (_interpolator == NULL)
218  return;
219 
220  interpolators_.push_back(_interpolator);
221 // std::cerr << "Precalc for interpolator " << interpolators_.size()-1 << ":" << std::endl;
222 
223  std::vector < TargetType > valueVector;
224 
225  //Precalc values for this interpolator
226  uint i=0;
227  for (i=_interpolator->getMinInput()*FPS;i<=(_interpolator->getMaxInput()) * FPS;++i) {
228  TargetType precalcValue;
229  double input = ((double)i) / ((double)FPS);
230  precalcValue = _interpolator->getValue(input);
231  valueVector.push_back(precalcValue);
232 
233 // std::cerr << "Frame " << i << "(t=" << input << "): " << precalcValue[0] << std::endl;
234  }
235 
236 // std::cerr << std::endl;
237 
238  precalculations_.insert( std::pair< Interpolator*, std::vector < TargetType > >(_interpolator, valueVector) );
239 
240  frames_ = std::max<long unsigned int>(frames_, i+1);
241 }
242 
243 //-----------------------------------------------------------------------------------------------------
244 
245 template<class PointT>
248 {
249  if ( _index < interpolators_.size() )
250  return interpolators_[ _index ];
251  else
252  return 0;
253 }
254 
255 //-----------------------------------------------------------------------------------------------------
256 
257 template<class PointT>
259 {
260  return interpolators_.size();
261 }
262 
263 //-----------------------------------------------------------------------------------------------------
264 
268 template<class PointT>
270  assert (_index < interpolators_.size());
271 
272  if (_index == 0) {
273  return precalculations_[interpolators_[_index]].size() - 1;
274  } else {
275  return precalculations_[interpolators_[_index]].size() + calcAbsoluteMaxForInterpolator(_index - 1);
276  }
277 }
278 
279 //-----------------------------------------------------------------------------------------------------
280 
281 template<class PointT>
282 bool InterpolationAnimationT<PointT>::getMinInput(Scalar& _result) {
283  if (interpolators_.size() == 0)
284  return false;
285  else
286  _result = interpolators_[0]->getMinInput();
287 
288  for (uint i=0;i<interpolators_.size();++i) {
289  if (interpolators_[i]->getMinInput() < _result)
290  _result = interpolators_[i]->getMinInput();
291  }
292 
293  return true;
294 }
295 
296 //-----------------------------------------------------------------------------------------------------
297 
298 template<class PointT>
299 bool InterpolationAnimationT<PointT>::getMaxInput(Scalar& _result) {
300  if (interpolators_.size() == 0)
301  return false;
302  else
303  _result = interpolators_[0]->getMaxInput();
304 
305  for (uint i=0;i<interpolators_.size();++i) {
306  if (interpolators_[i]->getMaxInput() > _result)
307  _result = interpolators_[i]->getMaxInput();
308  }
309 
310  return true;
311 }
312 
313 //-----------------------------------------------------------------------------------------------------
314 
315 template<class PointT>
317  for (uint i=0; i<influencedJoints_.size(); ++i)
318  if ( influencedJoints_[i] == _joint )
319  return true;
320 
321  return false;
322 }
323 
324 //-----------------------------------------------------------------------------------------------------
325 
326 template<class PointT>
328  return influencedJoints_;
329 }
330 
331 //-----------------------------------------------------------------------------------------------------
332 
Knows how to apply the values generated by an interpolator to a matrix. When playing back an Interpol...
virtual void insertJointAt(unsigned int _index)
Called by the skeleton as a new joint is inserted.
virtual void doManipulation(GLMatrixT &_matrix, std::vector< Scalar > _value)=0
void setGlobalMatrix(unsigned int _joint, const Matrix &_global, bool _keepGlobalChildPositions=true)
Sets the global coordinate system.
Definition: PoseT_impl.hh:211
InterpolationAnimationT(const InterpolationAnimationT< PointT > &_other)
Copy constructor.
virtual void removeJointAt(unsigned int _index)
Called by the skeleton as a joint is deleted.
Stores a single animation.
Definition: AnimationT.hh:58
A general pose, used to store the frames of the animation.
Definition: PoseT.hh:58
unsigned int calcAbsoluteMaxForInterpolator(uint _index)
Calculates the last frame that interpolator _index is responsible for.
unsigned int frameCount()
Returns the number of frames stored in this pose.
Pose * referencePose()
Returns a pointer to the reference pose.
4x4 matrix implementing OpenGL commands.
Definition: GLMatrixT.hh:79
Interpolator * interpolator(unsigned int _index)
Get the i-th interpolator.
unsigned int interpolatorCount()
Get the number of interpolators.
void addInterpolator(InterpolationT< double > *_interpolator)
Add an interpolator.
const Matrix & globalMatrix(unsigned int _joint) const
Returns the global matrix for the given joint.
Definition: PoseT_impl.hh:193
virtual Pose * pose(unsigned int _iFrame)
Returns a pointer to the pose calculated for the given frame.
virtual void updateFromGlobal(unsigned int _index)
Updates the local matrix using the global matrix.