Developer Documentation
PoseT.cc
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #define POSET_C
51 
52 #include <cassert>
53 
54 //-----------------------------------------------------------------------------
55 
64 template<typename PointT>
65 PoseT<PointT>::PoseT(SkeletonT<PointT>* _skeleton) : skeleton_(_skeleton)
66 {
67  assert(_skeleton != 0);
68 
69  // add joints until we have the same size as the reference pose
70  while( skeleton_->jointCount() > local_.size() )
71  insertJointAt( local_.size() );
72 }
73 
74 //-----------------------------------------------------------------------------
75 
81 template<typename PointT>
83 {
84  local_.insert(local_.begin(), _other.local_.begin(), _other.local_.end());
85  global_.insert(global_.begin(), _other.global_.begin(), _other.global_.end());
86  unified_.insert(unified_.begin(), _other.unified_.begin(), _other.unified_.end());
88 }
89 
90 //-----------------------------------------------------------------------------
91 
96 template<typename PointT>
98 {
99 
100 }
101 
102 //-----------------------------------------------------------------------------
103 
109 template<typename PointT>
110 inline const typename PoseT<PointT>::Matrix& PoseT<PointT>::localMatrix(unsigned int _joint) const
111 {
112  return local_[_joint];
113 }
114 
115 //-----------------------------------------------------------------------------
116 
125 template<typename PointT>
126 void PoseT<PointT>::setLocalMatrix(unsigned int _joint, const Matrix& _local, bool _keepLocalChildPositions)
127 {
128  local_[_joint] = _local;
129 
130  updateFromLocal(_joint, _keepLocalChildPositions);
131 }
132 
133 //-----------------------------------------------------------------------------
134 
144 template<typename PointT>
145 inline typename PoseT<PointT>::Vector PoseT<PointT>::localTranslation(unsigned int _joint)
146 {
147  Vector ret;
148  Matrix &mat = local_[_joint];
149  for(int i = 0; i < 3; ++i)
150  ret[i] = mat(i, 3);
151  return ret;
152 }
153 
154 //-----------------------------------------------------------------------------
155 
166 template<typename PointT>
167 void PoseT<PointT>::setLocalTranslation(unsigned int _joint, const Vector &_position, bool _keepLocalChildPositions)
168 {
169  Matrix &mat = local_[_joint];
170  for(int i = 0; i < 3; ++i)
171  mat(i, 3) = _position[i];
172 
173  updateFromLocal(_joint, _keepLocalChildPositions);
174 }
175 
176 //-----------------------------------------------------------------------------
177 
181 template<typename PointT>
182 typename PoseT<PointT>::Matrix PoseT<PointT>::localMatrixInv(unsigned int _joint) const
183 {
184  Matrix ret = local_[_joint];
185  ret.invert();
186  return ret;
187 }
188 
189 //-----------------------------------------------------------------------------
190 
198 template<typename PointT>
199 inline const typename PoseT<PointT>::Matrix& PoseT<PointT>::globalMatrix(unsigned int _joint) const
200 {
201  return global_[_joint];
202 }
203 
204 //-----------------------------------------------------------------------------
205 
216 template<typename PointT>
217 void PoseT<PointT>::setGlobalMatrix(unsigned int _joint, const Matrix &_global, bool _keepGlobalChildPositions)
218 {
219  global_[_joint] = _global;
220 
221  updateFromGlobal(_joint, _keepGlobalChildPositions);
222 }
223 
224 //-----------------------------------------------------------------------------
225 
232 template<typename PointT>
233 inline typename PoseT<PointT>::Vector PoseT<PointT>::globalTranslation(unsigned int _joint)
234 {
235  Vector ret;
236  Matrix &mat = global_[_joint];
237  for(int i = 0; i < 3; ++i)
238  ret[i] = mat(i, 3);
239  return ret;
240 }
241 
242 //-----------------------------------------------------------------------------
243 
254 template<typename PointT>
255 void PoseT<PointT>::setGlobalTranslation(unsigned int _joint, const Vector &_position, bool _keepGlobalChildPositions)
256 {
257  Matrix &mat = global_[_joint];
258  for(int i = 0; i < 3; ++i)
259  mat(i, 3) = _position[i];
260 
261  updateFromGlobal(_joint, _keepGlobalChildPositions);
262 }
263 
264 //-----------------------------------------------------------------------------
265 
272 template<typename PointT>
273 typename PoseT<PointT>::Matrix PoseT<PointT>::globalMatrixInv(unsigned int _joint) const
274 {
275  if (_joint >= global_.size()) {
276  std::cerr << "Illegal joint number: " << _joint << std::endl;
277  return global_[0];
278  }
279  Matrix ret = global_[_joint];
280  ret.invert();
281  return ret;
282 }
283 
284 //-----------------------------------------------------------------------------
285 
286 template<typename PointT>
287 void PoseT<PointT>::insertJointAt(unsigned int _index)
288 {
289  Matrix id;
290  id.identity();
291 
292  DualQuaternion idDQ;
293  idDQ.identity();
294 
295  local_.insert(local_.begin() + _index, id);
296  global_.insert(global_.begin() + _index, id);
297  unified_.insert(unified_.begin() + _index, id);
298  unifiedDualQuaternion_.insert(unifiedDualQuaternion_.begin() + _index, idDQ);
299 }
300 
301 //-----------------------------------------------------------------------------
302 
303 template<typename PointT>
304 void PoseT<PointT>::removeJointAt(unsigned int _index)
305 {
306  local_.erase(local_.begin() + _index);
307  global_.erase(global_.begin() + _index);
308  unified_.erase(unified_.begin() + _index);
309  unifiedDualQuaternion_.erase(unifiedDualQuaternion_.begin() + _index);
310 }
311 
312 //-----------------------------------------------------------------------------
313 
320 template<typename PointT>
321 void PoseT<PointT>::updateFromLocal(unsigned int _joint, bool _keepChildPositions)
322 {
323  // first update the global coordinate system
324  if(skeleton_->parent(_joint) == -1)
325  global_[_joint] = local_[_joint];
326  else
327  global_[_joint] = globalMatrix(skeleton_->parent(_joint)) * localMatrix(_joint);
328 
329  // update the unified matrices
330  Matrix matRefGlobalInv = skeleton_->referencePose()->globalMatrix(_joint);
331  matRefGlobalInv.invert();
332 
333  unified_[_joint] = globalMatrix(_joint) * matRefGlobalInv;
335 
336  // update children
337  if (_keepChildPositions) {
338  // finally update all children as well
339  for(unsigned int i = 0; i < skeleton_->childCount(_joint); ++i) {
340  updateFromLocal(skeleton_->child(_joint, i));
341  }
342  } else {
343  updateFromGlobal(_joint, true); //this will adjust the childrens' positions according to the _joint position.
344  }
345 }
346 
347 //-----------------------------------------------------------------------------
348 
358 template<typename PointT>
359 void PoseT<PointT>::updateFromGlobal(unsigned int _joint, bool _keepChildPositions)
360 {
361  // first update the local coordinate system
362  if(skeleton_->parent(_joint) == -1)
363  local_[_joint] = global_[_joint];
364  else
365  local_[_joint] = globalMatrixInv(skeleton_->parent(_joint)) * globalMatrix(_joint);
366 
367  // update the unified matrices
368  Matrix matRefGlobalInv = skeleton_->referencePose()->globalMatrix(_joint);
369  matRefGlobalInv.invert();
370 
371  unified_[_joint] = globalMatrix(_joint) * matRefGlobalInv;
373 
374  // update children
375  if (_keepChildPositions) {
376  for(unsigned int i = 0; i < skeleton_->childCount(_joint); ++i) {
377  updateFromGlobal(skeleton_->child(_joint, i));
378  }
379  } else {
380  updateFromLocal(_joint, true); //this will adjust the childrens' positions according to the _joint position.
381  }
382 }
383 
384 //-----------------------------------------------------------------------------
385 
402 template<typename PointT>
403 inline const typename PoseT<PointT>::Matrix& PoseT<PointT>::unifiedMatrix(unsigned int _joint)
404 {
405  return unified_[_joint];
406 }
407 
408 //-----------------------------------------------------------------------------
409 
420 template<typename PointT>
421 inline const typename PoseT<PointT>::Quaternion& PoseT<PointT>::unifiedRotation(unsigned int _joint)
422 {
423  return unifiedDualQuaternion_[_joint].real();
424 }
425 
426 //-----------------------------------------------------------------------------
427 
436 template<typename PointT>
437 inline const typename PoseT<PointT>::DualQuaternion& PoseT<PointT>::unifiedDualQuaternion(unsigned int _joint)
438 {
439  return unifiedDualQuaternion_[_joint];
440 }
441 
442 //-----------------------------------------------------------------------------
443 
DualQuaternion class for representing rigid motions in 3d.
virtual void removeJointAt(unsigned int _index)
Called by the skeleton/animation as a joint is removed.
Definition: PoseT.cc:304
PoseT(SkeletonT< Point > *_skeleton)
Constructor.
Definition: PoseT.cc:65
void setGlobalTranslation(unsigned int _joint, const Vector &_position, bool _keepGlobalChildPositions=true)
Sets the global translation vector.
Definition: PoseT.cc:255
void updateFromLocal(unsigned int _joint, bool _keepChildPositions=true)
This method propagates the change in the local coordinate system to the global system and all childre...
Definition: PoseT.cc:321
const Quaternion & unifiedRotation(unsigned int _joint)
Returns a quaternion holding the rotational part of the unified matrix.
Definition: PoseT.cc:421
virtual Matrix globalMatrixInv(unsigned int _joint) const
Simply returns the inverse of the global matrix.
Definition: PoseT.cc:273
void updateFromGlobal(unsigned int _joint, bool _keepChildPositions=true)
This method propagates the change in the global coordinate system to the local system and all childre...
Definition: PoseT.cc:359
std::vector< DualQuaternion > unifiedDualQuaternion_
Definition: PoseT.hh:198
Vector globalTranslation(unsigned int _joint)
Returns the global translation vector.
Definition: PoseT.cc:233
std::vector< Matrix > local_
the pose in local coordinates
Definition: PoseT.hh:189
SkeletonT< PointT > * skeleton_
a pointer to the skeleton
Definition: PoseT.hh:187
const Matrix & globalMatrix(unsigned int _joint) const
Returns the global matrix for the given joint.
Definition: PoseT.cc:199
A general pose, used to store the frames of the animation.
Definition: PoseT.hh:68
const Matrix & localMatrix(unsigned int _joint) const
Returns the local matrix for the given joint.
Definition: PoseT.cc:110
void identity()
setup an identity matrix
Definition: Matrix4x4T.cc:256
Matrix localMatrixInv(unsigned int _joint) const
Simply returns the inverse of the local matrix.
Definition: PoseT.cc:182
Vector localTranslation(unsigned int _joint)
Returns the local translation vector.
Definition: PoseT.cc:145
virtual void insertJointAt(unsigned int _index)
Called by the skeleton/animation as a new joint is inserted.
Definition: PoseT.cc:287
void setLocalTranslation(unsigned int _joint, const Vector &_position, bool _keepLocalChildPositions=true)
Sets the local translation vector.
Definition: PoseT.cc:167
const DualQuaternion & unifiedDualQuaternion(unsigned int _joint)
Returns a dual quaternion holding the unified matrix represented as dual quaternion.
Definition: PoseT.cc:437
std::vector< Matrix > unified_
the global pose matrix left-multiplied to the inverse global reference matrix:
Definition: PoseT.hh:194
bool invert()
matrix inversion (returns true on success)
Definition: Matrix4x4T.cc:297
const Matrix & unifiedMatrix(unsigned int _joint)
Returns the unified matrix.
Definition: PoseT.cc:403
static DualQuaternion identity()
identity dual quaternion [ R(1, 0, 0, 0), D(0,0,0,0) ]
std::vector< Matrix > global_
the pose in global coordinates
Definition: PoseT.hh:191
void setGlobalMatrix(unsigned int _joint, const Matrix &_global, bool _keepGlobalChildPositions=true)
Sets the global coordinate system.
Definition: PoseT.cc:217
virtual ~PoseT()
Destructor.
Definition: PoseT.cc:97
void setLocalMatrix(unsigned int _joint, const Matrix &_local, bool _keepLocalChildPositions=true)
Sets the local coordinate system.
Definition: PoseT.cc:126