Developer Documentation
PoseT_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 POSET_C
45 
46 #include <cassert>
47 
48 //-----------------------------------------------------------------------------
49 
58 template<typename PointT>
60 {
61  assert(_skeleton != 0);
62 
63  // add joints until we have the same size as the reference pose
64  while( skeleton_->jointCount() > local_.size() )
65  insertJointAt( local_.size() );
66 }
67 
68 //-----------------------------------------------------------------------------
69 
75 template<typename PointT>
77 {
78  local_.insert(local_.begin(), _other.local_.begin(), _other.local_.end());
79  global_.insert(global_.begin(), _other.global_.begin(), _other.global_.end());
80  unified_.insert(unified_.begin(), _other.unified_.begin(), _other.unified_.end());
82 }
83 
84 //-----------------------------------------------------------------------------
85 
90 template<typename PointT>
92 {
93 
94 }
95 
96 //-----------------------------------------------------------------------------
97 
103 template<typename PointT>
104 inline const typename PoseT<PointT>::Matrix& PoseT<PointT>::localMatrix(unsigned int _joint) const
105 {
106  return local_[_joint];
107 }
108 
109 //-----------------------------------------------------------------------------
110 
119 template<typename PointT>
120 void PoseT<PointT>::setLocalMatrix(unsigned int _joint, const Matrix& _local, bool _keepLocalChildPositions)
121 {
122  local_[_joint] = _local;
123 
124  updateFromLocal(_joint, _keepLocalChildPositions);
125 }
126 
127 //-----------------------------------------------------------------------------
128 
138 template<typename PointT>
139 inline typename PoseT<PointT>::Vector PoseT<PointT>::localTranslation(unsigned int _joint)
140 {
141  Vector ret;
142  Matrix &mat = local_[_joint];
143  for(int i = 0; i < 3; ++i)
144  ret[i] = mat(i, 3);
145  return ret;
146 }
147 
148 //-----------------------------------------------------------------------------
149 
160 template<typename PointT>
161 void PoseT<PointT>::setLocalTranslation(unsigned int _joint, const Vector &_position, bool _keepLocalChildPositions)
162 {
163  Matrix &mat = local_[_joint];
164  for(int i = 0; i < 3; ++i)
165  mat(i, 3) = _position[i];
166 
167  updateFromLocal(_joint, _keepLocalChildPositions);
168 }
169 
170 //-----------------------------------------------------------------------------
171 
175 template<typename PointT>
176 typename PoseT<PointT>::Matrix PoseT<PointT>::localMatrixInv(unsigned int _joint) const
177 {
178  Matrix ret = local_[_joint];
179  ret.invert();
180  return ret;
181 }
182 
183 //-----------------------------------------------------------------------------
184 
192 template<typename PointT>
193 inline const typename PoseT<PointT>::Matrix& PoseT<PointT>::globalMatrix(unsigned int _joint) const
194 {
195  return global_[_joint];
196 }
197 
198 //-----------------------------------------------------------------------------
199 
210 template<typename PointT>
211 void PoseT<PointT>::setGlobalMatrix(unsigned int _joint, const Matrix &_global, bool _keepGlobalChildPositions)
212 {
213  global_[_joint] = _global;
214 
215  updateFromGlobal(_joint, _keepGlobalChildPositions);
216 }
217 
218 //-----------------------------------------------------------------------------
219 
226 template<typename PointT>
227 inline typename PoseT<PointT>::Vector PoseT<PointT>::globalTranslation(unsigned int _joint)
228 {
229  Vector ret;
230  Matrix &mat = global_[_joint];
231  for(int i = 0; i < 3; ++i)
232  ret[i] = mat(i, 3);
233  return ret;
234 }
235 
236 //-----------------------------------------------------------------------------
237 
248 template<typename PointT>
249 void PoseT<PointT>::setGlobalTranslation(unsigned int _joint, const Vector &_position, bool _keepGlobalChildPositions)
250 {
251  Matrix &mat = global_[_joint];
252  for(int i = 0; i < 3; ++i)
253  mat(i, 3) = _position[i];
254 
255  updateFromGlobal(_joint, _keepGlobalChildPositions);
256 }
257 
258 //-----------------------------------------------------------------------------
259 
266 template<typename PointT>
267 typename PoseT<PointT>::Matrix PoseT<PointT>::globalMatrixInv(unsigned int _joint) const
268 {
269  if (_joint >= global_.size()) {
270  std::cerr << "Illegal joint number: " << _joint << std::endl;
271  return global_[0];
272  }
273  Matrix ret = global_[_joint];
274  ret.invert();
275  return ret;
276 }
277 
278 //-----------------------------------------------------------------------------
279 
280 template<typename PointT>
281 void PoseT<PointT>::insertJointAt(size_t _index)
282 {
283  Matrix id;
284  id.identity();
285 
286  DualQuaternion idDQ;
287  idDQ.identity();
288 
289  local_.insert(local_.begin() + _index, id);
290  global_.insert(global_.begin() + _index, id);
291  unified_.insert(unified_.begin() + _index, id);
292  unifiedDualQuaternion_.insert(unifiedDualQuaternion_.begin() + _index, idDQ);
293 }
294 
295 //-----------------------------------------------------------------------------
296 
297 template<typename PointT>
298 void PoseT<PointT>::removeJointAt(size_t _index)
299 {
300  local_.erase(local_.begin() + _index);
301  global_.erase(global_.begin() + _index);
302  unified_.erase(unified_.begin() + _index);
303  unifiedDualQuaternion_.erase(unifiedDualQuaternion_.begin() + _index);
304 }
305 
306 //-----------------------------------------------------------------------------
307 
314 template<typename PointT>
315 void PoseT<PointT>::updateFromLocal(size_t _joint, bool _keepChildPositions)
316 {
317  // first update the global coordinate system
318  if(skeleton_->parent(_joint) == -1)
319  global_[_joint] = local_[_joint];
320  else
321  global_[_joint] = globalMatrix(skeleton_->parent(_joint)) * localMatrix(_joint);
322 
323  // update the unified matrices
324  Matrix matRefGlobalInv = skeleton_->referencePose()->globalMatrix(_joint);
325  matRefGlobalInv.invert();
326 
327  unified_[_joint] = globalMatrix(_joint) * matRefGlobalInv;
329 
330  // update children
331  if (_keepChildPositions) {
332  // finally update all children as well
333  for(size_t i = 0; i < skeleton_->childCount(_joint); ++i) {
334  updateFromLocal(skeleton_->child(_joint, i));
335  }
336  } else {
337  updateFromGlobal(_joint, true); //this will adjust the childrens' positions according to the _joint position.
338  }
339 }
340 
341 //-----------------------------------------------------------------------------
342 
352 template<typename PointT>
353 void PoseT<PointT>::updateFromGlobal(size_t _joint, bool _keepChildPositions)
354 {
355  // first update the local coordinate system
356  if(skeleton_->parent(_joint) == -1)
357  local_[_joint] = global_[_joint];
358  else
359  local_[_joint] = globalMatrixInv(skeleton_->parent(_joint)) * globalMatrix(_joint);
360 
361  // update the unified matrices
362  Matrix matRefGlobalInv = skeleton_->referencePose()->globalMatrix(_joint);
363  matRefGlobalInv.invert();
364 
365  unified_[_joint] = globalMatrix(_joint) * matRefGlobalInv;
367 
368  // update children
369  if (_keepChildPositions) {
370  for(size_t i = 0; i < skeleton_->childCount(_joint); ++i) {
371  updateFromGlobal(skeleton_->child(_joint, i));
372  }
373  } else {
374  updateFromLocal(_joint, true); //this will adjust the childrens' positions according to the _joint position.
375  }
376 }
377 
378 //-----------------------------------------------------------------------------
379 
396 template<typename PointT>
397 inline const typename PoseT<PointT>::Matrix& PoseT<PointT>::unifiedMatrix(size_t _joint)
398 {
399  return unified_[_joint];
400 }
401 
402 //-----------------------------------------------------------------------------
403 
414 template<typename PointT>
415 inline const typename PoseT<PointT>::Quaternion& PoseT<PointT>::unifiedRotation(size_t _joint)
416 {
417  return unifiedDualQuaternion_[_joint].real();
418 }
419 
420 //-----------------------------------------------------------------------------
421 
430 template<typename PointT>
432 {
433  return unifiedDualQuaternion_[_joint];
434 }
435 
436 //-----------------------------------------------------------------------------
437 
virtual Matrix globalMatrixInv(unsigned int _joint) const
Simply returns the inverse of the global matrix.
Definition: PoseT_impl.hh:267
virtual ~PoseT()
Destructor.
Definition: PoseT_impl.hh:91
Matrix localMatrixInv(unsigned int _joint) const
Simply returns the inverse of the local matrix.
Definition: PoseT_impl.hh:176
void setGlobalMatrix(unsigned int _joint, const Matrix &_global, bool _keepGlobalChildPositions=true)
Sets the global coordinate system.
Definition: PoseT_impl.hh:211
const Matrix & unifiedMatrix(size_t _joint)
Returns the unified matrix.
Definition: PoseT_impl.hh:397
void setGlobalTranslation(unsigned int _joint, const Vector &_position, bool _keepGlobalChildPositions=true)
Sets the global translation vector.
Definition: PoseT_impl.hh:249
const Quaternion & unifiedRotation(size_t _joint)
Returns a quaternion holding the rotational part of the unified matrix.
Definition: PoseT_impl.hh:415
void updateFromLocal(size_t _joint, bool _keepChildPositions=true)
This method propagates the change in the local coordinate system to the global system and all childre...
Definition: PoseT_impl.hh:315
Skeleton * skeleton_
Pointer to associated skeleton.
std::vector< DualQuaternion > unifiedDualQuaternion_
Definition: PoseT.hh:190
DualQuaternion class for representing rigid motions in 3d.
void identity()
setup an identity matrix
A general pose, used to store the frames of the animation.
Definition: PoseT.hh:58
static DualQuaternion identity()
identity dual quaternion [ R(1, 0, 0, 0), D(0,0,0,0) ]
void setLocalTranslation(unsigned int _joint, const Vector &_position, bool _keepLocalChildPositions=true)
Sets the local translation vector.
Definition: PoseT_impl.hh:161
bool invert()
matrix inversion (returns true on success)
PoseT(SkeletonT< Point > *_skeleton)
Constructor.
Definition: PoseT_impl.hh:59
const Matrix & localMatrix(unsigned int _joint) const
Returns the local matrix for the given joint.
Definition: PoseT_impl.hh:104
std::vector< Matrix > unified_
the global pose matrix left-multiplied to the inverse global reference matrix:
Definition: PoseT.hh:186
virtual void insertJointAt(size_t _index)
Called by the skeleton/animation as a new joint is inserted.
Definition: PoseT_impl.hh:281
std::vector< Matrix > local_
the pose in local coordinates
Definition: PoseT.hh:181
SkeletonT< PointT > * skeleton_
a pointer to the skeleton
Definition: PoseT.hh:179
const DualQuaternion & unifiedDualQuaternion(size_t _joint)
Returns a dual quaternion holding the unified matrix represented as dual quaternion.
Definition: PoseT_impl.hh:431
void updateFromGlobal(size_t _joint, bool _keepChildPositions=true)
This method propagates the change in the global coordinate system to the local system and all childre...
Definition: PoseT_impl.hh:353
Vector localTranslation(unsigned int _joint)
Returns the local translation vector.
Definition: PoseT_impl.hh:139
virtual void removeJointAt(size_t _index)
Called by the skeleton/animation as a joint is removed.
Definition: PoseT_impl.hh:298
const Matrix & globalMatrix(unsigned int _joint) const
Returns the global matrix for the given joint.
Definition: PoseT_impl.hh:193
void setLocalMatrix(unsigned int _joint, const Matrix &_local, bool _keepLocalChildPositions=true)
Sets the local coordinate system.
Definition: PoseT_impl.hh:120
std::vector< Matrix > global_
the pose in global coordinates
Definition: PoseT.hh:183
Vector globalTranslation(unsigned int _joint)
Returns the global translation vector.
Definition: PoseT_impl.hh:227