cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctMatrixRotation3Base.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet
7  Created on: 2005-08-19
8 
9  (C) Copyright 2005-2013 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 */
20 
21 #pragma once
22 #ifndef _vctMatrixRotation3Base_h
23 #define _vctMatrixRotation3Base_h
24 
35 #include <cisstVector/vctExport.h>
36 
37 
38 #ifndef DOXYGEN
39 #ifndef SWIG
40 
41 // helper functions for subtemplated methods of a templated class
42 template <class _matrixType, class _quaternionType>
43 void
45  const vctQuaternionRotation3Base<_quaternionType> & quaternionRotation);
46 
47 #endif // SWIG
48 #endif // DOXYGEN
49 
50 
71 template <class _containerType>
72 class vctMatrixRotation3Base: public vctMatrixRotation3ConstBase<_containerType>
73 {
74 public:
75  enum {ROWS = 3, COLS = 3};
76  enum {DIMENSION = 3};
77  typedef _containerType ContainerType;
81 
82  /* no need to document, inherit doxygen documentation from base class */
83  VCT_CONTAINER_TRAITS_TYPEDEFS(typename ContainerType::value_type);
84 
87 
88  // MJ: support for gcc 4.6 compilation (not to have -fpermissive)
90 
104 
106  template <class __containerType>
107  inline ThisType &
109  throw(std::runtime_error)
110  {
111  this->FromRaw(other);
112  this->ThrowUnlessIsNormalized();
113  return *this;
114  }
115 
116 
118  inline ThisType &
119  From(const value_type & element00, const value_type & element01, const value_type & element02,
120  const value_type & element10, const value_type & element11, const value_type & element12,
121  const value_type & element20, const value_type & element21, const value_type & element22)
122  throw(std::runtime_error)
123  {
124  this->FromRaw(element00, element01, element02,
125  element10, element11, element12,
126  element20, element21, element22);
127  this->ThrowUnlessIsNormalized();
128  return *this;
129  }
130 
137  template <stride_type __stride1, class __dataPtrType1,
138  stride_type __stride2, class __dataPtrType2,
139  stride_type __stride3, class __dataPtrType3>
140  inline ThisType &
144  bool vectorsAreColumns = true)
145  throw(std::runtime_error)
146  {
147  this->FromRaw(v1, v2, v3, vectorsAreColumns);
148  this->ThrowUnlessIsNormalized();
149  return *this;
150  }
151 
152 
159  template <class __vectorOwnerType1,
160  class __vectorOwnerType2,
161  class __vectorOwnerType3>
162  inline ThisType &
166  bool vectorsAreColumns = true)
167  throw (std::runtime_error)
168  {
169  this->FromRaw(v1, v2, v3, vectorsAreColumns);
170  this->ThrowUnlessIsNormalized();
171  return *this;
172  }
173 
175  inline ThisType &
176  From(const vctAxisAngleRotation3<value_type> & axisAngleRotation)
177  throw(std::runtime_error)
178  {
179  this->ThrowUnlessIsNormalized(axisAngleRotation);
180  return this->FromRaw(axisAngleRotation);
181  }
182 
184  template <class __containerType>
185  inline ThisType &
187  throw(std::runtime_error)
188  {
189  this->ThrowUnlessIsNormalized(quaternionRotation);
190  return this->FromRaw(quaternionRotation);
191  }
192 
193 
195  template <class __containerType>
196  inline ThisType &
198  throw(std::runtime_error)
199  {
200  this->ThrowUnlessIsNormalized(rodriguezRotation);
201  return this->FromRaw(rodriguezRotation);
202  }
203 
205  template <vctEulerRotation3Order::OrderType __order>
206  inline ThisType &
207  From(const vctEulerRotation3<__order> & eulerRotation)
208  throw(std::runtime_error)
209  {
210  this->ThrowUnlessIsNormalized(eulerRotation);
211  return this->FromRaw(eulerRotation);
212  }
213 
215 
216 
217 
218 
219 
220 
230 
232  template <class __containerType>
233  inline ThisType &
235  throw(std::runtime_error)
236  {
237  this->FromRaw(other);
238  this->NormalizedSelf();
239  return *this;
240  }
241 
242 
244  inline ThisType &
245  FromNormalized(const value_type & element00, const value_type & element01, const value_type & element02,
246  const value_type & element10, const value_type & element11, const value_type & element12,
247  const value_type & element20, const value_type & element21, const value_type & element22)
248  {
249  this->FromRaw(element00, element01, element02,
250  element10, element11, element12,
251  element20, element21, element22);
252  this->NormalizedSelf();
253  return *this;
254  }
255 
262  template <stride_type __stride1, class __dataPtrType1,
263  stride_type __stride2, class __dataPtrType2,
264  stride_type __stride3, class __dataPtrType3>
265  inline ThisType &
269  bool vectorsAreColumns = true)
270  throw(std::runtime_error)
271  {
272  this->FromRaw(v1, v2, v3, vectorsAreColumns);
273  this->NormalizedSelf();
274  return *this;
275  }
276 
277 
284  template <class __vectorOwnerType1,
285  class __vectorOwnerType2,
286  class __vectorOwnerType3>
287  inline ThisType &
291  bool vectorsAreColumns = true)
292  {
293  this->FromRaw(v1, v2, v3, vectorsAreColumns);
294  this->NormalizedSelf();
295  return *this;
296  }
297 
299  inline ThisType &
301  {
302  return this->FromRaw(axisAngleRotation.Normalized());
303  }
304 
310  template <class __containerType>
311  inline ThisType &
313  {
314  return this->FromRaw(quaternionRotation.Normalized());
315  }
316 
317 
319  template <class __containerType>
320  inline ThisType &
322  {
323  return this->FromRaw(rodriguezRotation.Normalized());
324  }
325 
326 
328  template <vctEulerRotation3Order::OrderType __order>
329  inline ThisType &
331  {
332  return this->FromRaw(eulerRotation.Normalized());
333  }
335 
336 
337 
338 
339 
340 
350 
352  template <class __containerType>
353  inline ThisType &
355  throw(std::runtime_error)
356  {
357  this->Assign(other);
358  return *this;
359  }
360 
361 
363  inline ThisType &
364  FromRaw(const value_type & element00, const value_type & element01, const value_type & element02,
365  const value_type & element10, const value_type & element11, const value_type & element12,
366  const value_type & element20, const value_type & element21, const value_type & element22)
367  {
368  this->Assign(element00, element01, element02,
369  element10, element11, element12,
370  element20, element21, element22);
371  return *this;
372  }
373 
374 
381  template <stride_type __stride1, class __dataPtrType1,
382  stride_type __stride2, class __dataPtrType2,
383  stride_type __stride3, class __dataPtrType3>
384  inline ThisType &
388  bool vectorsAreColumns = true)
389  {
390  if (vectorsAreColumns) {
391  this->Column(0).Assign(v1);
392  this->Column(1).Assign(v2);
393  this->Column(2).Assign(v3);
394  } else {
395  this->Row(0).Assign(v1);
396  this->Row(1).Assign(v2);
397  this->Row(2).Assign(v3);
398  }
399  return *this;
400  }
401 
402 
409  template <class __vectorOwnerType1,
410  class __vectorOwnerType2,
411  class __vectorOwnerType3>
412  inline ThisType &
416  bool vectorsAreColumns = true)
417  throw (std::runtime_error)
418  {
419  CMN_ASSERT(v1.size() == DIMENSION);
420  CMN_ASSERT(v2.size() == DIMENSION);
421  CMN_ASSERT(v3.size() == DIMENSION);
422  if (vectorsAreColumns) {
423  this->Column(0).Assign(v1);
424  this->Column(1).Assign(v2);
425  this->Column(2).Assign(v3);
426  } else {
427  this->Row(0).Assign(v1);
428  this->Row(1).Assign(v2);
429  this->Row(2).Assign(v3);
430  }
431  return *this;
432  }
433 
434 
436  ThisType &
437  FromRaw(const vctAxisAngleRotation3<value_type> & axisAngleRotation);
438 
439 
441  template <class __containerType>
442  inline ThisType &
444  vctMatrixRotation3BaseFromRaw(*this, quaternionRotation);
445  return *this;
446  }
447 
448 
450  template <class __containerType>
451  inline ThisType &
453  return this->FromRaw(vctAxisAngleRotation3<value_type>(rodriguezRotation, VCT_DO_NOT_NORMALIZE));
454  }
455 
457  template <vctEulerRotation3Order::OrderType __order>
458  inline ThisType &
459  FromRaw(const vctEulerRotation3<__order> & eulerRotation) {
460  vctEulerToMatrixRotation3(eulerRotation, *this);
461  return *this;
462  }
463 
467  inline ThisType &
468  FromRaw(const ThisType & otherRotation) {
469  return reinterpret_cast<ThisType &>(this->Assign(otherRotation));
470  }
471 
472 
482  template <stride_type __rowStride, stride_type __colStride, class __dataPtrType>
483  inline ThisType &
485  this->Assign(matrix);
486  return *this;
487  }
489 
490 
495  quaternion.FromRaw(*this);
496  quaternion.NormalizedSelf();
497  this->From(quaternion);
498  return *this;
499  }
500 
501 
504  inline ThisType & NormalizedOf(ThisType & otherMatrix) {
505  CMN_ASSERT(otherMatrix.Pointer() != this->Pointer());
506  *this = otherMatrix;
507  this->NormalizedSelf();
508  return *this;
509  }
510 
511 
514  inline ThisType &
515  InverseSelf(void) {
516  // could use the transpose operator but this seems more efficient
517  value_type tmp;
518  tmp = this->Element(0, 1); this->Element(0, 1) = this->Element(1, 0); this->Element(1, 0) = tmp;
519  tmp = this->Element(0, 2); this->Element(0, 2) = this->Element(2, 0); this->Element(2, 0) = tmp;
520  tmp = this->Element(1, 2); this->Element(1, 2) = this->Element(2, 1); this->Element(2, 1) = tmp;
521  return *this;
522  }
523 
524 
527  inline ThisType &
528  InverseOf(const ThisType & otherRotation) {
529  CMN_ASSERT(otherRotation.Pointer() != this->Pointer());
530  this->TransposeOf(otherRotation);
531  return *this;
532  }
533 };
534 
535 
536 
537 template <class _containerType>
540 
541  typedef vctAxisAngleRotation3<value_type> AxisAngleType;
542 
543  const typename AxisAngleType::AngleType angle = axisAngleRotation.Angle();
544  const typename AxisAngleType::AxisType axis = axisAngleRotation.Axis();
545 
546  const AngleType sinAngle = AngleType(sin(angle));
547  const AngleType cosAngle = AngleType(cos(angle));
548  const AngleType CompCosAngle = 1 - cosAngle;
549 
550  // first column
551  this->Element(0, 0) = value_type(axis[0] * axis[0] * CompCosAngle + cosAngle);
552  this->Element(1, 0) = value_type(axis[0] * axis[1] * CompCosAngle + axis[2] * sinAngle);
553  this->Element(2, 0) = value_type(axis[0] * axis[2] * CompCosAngle - axis[1] * sinAngle);
554 
555  // second column
556  this->Element(0, 1) = value_type(axis[1] * axis[0] * CompCosAngle - axis[2] * sinAngle);
557  this->Element(1, 1) = value_type(axis[1] * axis[1] * CompCosAngle + cosAngle);
558  this->Element(2, 1) = value_type(axis[1] * axis[2] * CompCosAngle + axis[0] * sinAngle);
559 
560  // third column
561  this->Element(0, 2) = value_type(axis[2] * axis[0] * CompCosAngle + axis[1] * sinAngle);
562  this->Element(1, 2) = value_type(axis[2] * axis[1] * CompCosAngle - axis[0] * sinAngle);
563  this->Element(2, 2) = value_type(axis[2] * axis[2] * CompCosAngle + cosAngle);
564 
565  return *this;
566 }
567 
568 
569 template <class _matrixType, class _quaternionType>
570 void
572  const vctQuaternionRotation3Base<_quaternionType> & quaternionRotation) {
573 
574  typedef typename _matrixType::value_type value_type;
575 
576  value_type xx = quaternionRotation.X() * quaternionRotation.X();
577  value_type xy = quaternionRotation.X() * quaternionRotation.Y();
578  value_type xz = quaternionRotation.X() * quaternionRotation.Z();
579  value_type xr = quaternionRotation.X() * quaternionRotation.R();
580  value_type yy = quaternionRotation.Y() * quaternionRotation.Y();
581  value_type yz = quaternionRotation.Y() * quaternionRotation.Z();
582  value_type yr = quaternionRotation.Y() * quaternionRotation.R();
583  value_type zz = quaternionRotation.Z() * quaternionRotation.Z();
584  value_type zr = quaternionRotation.Z() * quaternionRotation.R();
585  matrixRotation.Assign(1 - 2 * (yy + zz), 2 * (xy - zr), 2 * (xz + yr),
586  2 * (xy + zr), 1 - 2 * (xx + zz), 2 * (yz - xr),
587  2 * (xz - yr), 2 * (yz + xr), 1 - 2 * (xx + yy));
588 }
589 
590 #endif // _vctMatrixRotation3Base_h
A template for a fixed size matrix with fixed spacing in memory.
Definition: vctFixedSizeConstMatrixBase.h:103
Definition: vctEulerRotation3.h:153
ThisType & FromNormalized(const vctQuaternionRotation3Base< __containerType > &quaternionRotation)
Definition: vctMatrixRotation3Base.h:312
ThisType Normalized(void) const
Definition: vctEulerRotation3.h:242
_containerType ContainerType
Definition: vctMatrixRotation3Base.h:77
const AxisType & Axis(void) const
Definition: vctAxisAngleRotation3.h:314
#define CMN_ASSERT(expr)
Definition: cmnAssert.h:90
ThisType & FromRaw(const vctQuaternionRotation3Base< __containerType > &quaternionRotation)
Definition: vctMatrixRotation3Base.h:443
ThisType & FromRaw(const vctEulerRotation3< __order > &eulerRotation)
Definition: vctMatrixRotation3Base.h:459
ThisType & NormalizedOf(ThisType &otherMatrix)
Definition: vctMatrixRotation3Base.h:504
Declaration of vctMatrixRotation3ConstBase.
ThisType & From(const vctRodriguezRotation3Base< __containerType > &rodriguezRotation)
Definition: vctMatrixRotation3Base.h:197
Definition: vctMatrixRotation3Base.h:76
ThisType & From(const vctAxisAngleRotation3< value_type > &axisAngleRotation)
Definition: vctMatrixRotation3Base.h:176
ThisType & FromRaw(const vctRodriguezRotation3Base< __containerType > &rodriguezRotation)
Definition: vctMatrixRotation3Base.h:452
Declaration of vctAxisAngleRotation3.
vctMatrixRotation3Base()
Definition: vctMatrixRotation3Base.h:89
ThisType & InverseSelf(void)
Definition: vctMatrixRotation3Base.h:515
VCT_CONTAINER_TRAITS_TYPEDEFS(typename ContainerType::value_type)
void ThrowUnlessIsNormalized(void) const
Definition: vctMatrixRotation3ConstBase.h:78
ThisType & FromRaw(const vctDynamicConstVectorBase< __vectorOwnerType1, value_type > &v1, const vctDynamicConstVectorBase< __vectorOwnerType2, value_type > &v2, const vctDynamicConstVectorBase< __vectorOwnerType3, value_type > &v3, bool vectorsAreColumns=true)
Definition: vctMatrixRotation3Base.h:413
ThisType & From(const vctMatrixRotation3Base< __containerType > &other)
Definition: vctMatrixRotation3Base.h:108
ThisType Normalized(void) const
Definition: vctQuaternionRotation3Base.h:529
vctMatrixRotation3ConstBase< ContainerType > BaseType
Definition: vctMatrixRotation3Base.h:78
Definition: vctMatrixRotation3Base.h:75
ThisType & From(const vctQuaternionRotation3Base< __containerType > &quaternionRotation)
Definition: vctMatrixRotation3Base.h:186
Define a rotation based on an axis and an angle for a space of dimension 3.
Definition: vctAxisAngleRotation3.h:94
ThisType & NormalizedSelf(void)
Definition: vctQuaternionRotation3Base.h:519
size_type size(void) const
Definition: vctDynamicConstVectorBase.h:164
A template for a fixed size matrix with fixed spacings in memory.
Definition: vctFixedSizeMatrixBase.h:58
ThisType & FromNormalized(const value_type &element00, const value_type &element01, const value_type &element02, const value_type &element10, const value_type &element11, const value_type &element12, const value_type &element20, const value_type &element21, const value_type &element22)
Definition: vctMatrixRotation3Base.h:245
ThisType & From(const vctDynamicConstVectorBase< __vectorOwnerType1, value_type > &v1, const vctDynamicConstVectorBase< __vectorOwnerType2, value_type > &v2, const vctDynamicConstVectorBase< __vectorOwnerType3, value_type > &v3, bool vectorsAreColumns=true)
Definition: vctMatrixRotation3Base.h:163
ThisType & FromNormalized(const vctRodriguezRotation3Base< __containerType > &rodriguezRotation)
Definition: vctMatrixRotation3Base.h:321
Define a rotation matrix for a space of dimension 3.
Definition: vctForwardDeclarations.h:199
ThisType Normalized(void) const
Definition: vctAxisAngleRotation3.h:542
ThisType & InverseOf(const ThisType &otherRotation)
Definition: vctMatrixRotation3Base.h:528
void Assign(const OtherT &other)
Definition: cisstVectorEigenAddons.h:3
ThisType Normalized(void) const
Definition: vctRodriguezRotation3Base.h:304
ThisType & From(const value_type &element00, const value_type &element01, const value_type &element02, const value_type &element10, const value_type &element11, const value_type &element12, const value_type &element20, const value_type &element21, const value_type &element22)
Definition: vctMatrixRotation3Base.h:119
Define a rotation matrix for a space of dimension 3.
Definition: vctForwardDeclarations.h:198
Declaration of vctRodriguezRotation3Base.
vctMatrixRotation3Base< typename ContainerType::MatrixValueType > RotationValueType
Definition: vctMatrixRotation3Base.h:80
ThisType & FromRaw(const ThisType &otherRotation)
Definition: vctMatrixRotation3Base.h:468
ThisType & FromRaw(const vctMatrixRotation3Base< __containerType > &other)
Definition: vctMatrixRotation3Base.h:354
ThisType & FromNormalized(const vctFixedSizeConstVectorBase< DIMENSION, __stride1, value_type, __dataPtrType1 > &v1, const vctFixedSizeConstVectorBase< DIMENSION, __stride2, value_type, __dataPtrType2 > &v2, const vctFixedSizeConstVectorBase< DIMENSION, __stride3, value_type, __dataPtrType3 > &v3, bool vectorsAreColumns=true)
Definition: vctMatrixRotation3Base.h:266
ThisType & FromRaw(const value_type &element00, const value_type &element01, const value_type &element02, const value_type &element10, const value_type &element11, const value_type &element12, const value_type &element20, const value_type &element21, const value_type &element22)
Definition: vctMatrixRotation3Base.h:364
ThisType & FromNormalized(const vctEulerRotation3< __order > &eulerRotation)
Definition: vctMatrixRotation3Base.h:330
ThisType & FromRaw(const vctFixedSizeMatrixBase< ROWS, COLS, __rowStride, __colStride, value_type, __dataPtrType > &matrix)
Definition: vctMatrixRotation3Base.h:484
const AngleType & Angle(void) const
Definition: vctAxisAngleRotation3.h:324
const_reference R(void) const
Definition: vctQuaternionBase.h:101
ThisType & FromNormalized(const vctAxisAngleRotation3< value_type > &axisAngleRotation)
Definition: vctMatrixRotation3Base.h:300
ThisType & FromRaw(const vctFixedSizeConstVectorBase< DIMENSION, __stride1, value_type, __dataPtrType1 > &v1, const vctFixedSizeConstVectorBase< DIMENSION, __stride2, value_type, __dataPtrType2 > &v2, const vctFixedSizeConstVectorBase< DIMENSION, __stride3, value_type, __dataPtrType3 > &v3, bool vectorsAreColumns=true)
Definition: vctMatrixRotation3Base.h:385
ThisType & From(const vctEulerRotation3< __order > &eulerRotation)
Definition: vctMatrixRotation3Base.h:207
Declaration of vctEulerRotation3.
ptrdiff_t stride_type
Definition: vctContainerTraits.h:37
vctMatrixRotation3Base< ContainerType > ThisType
Definition: vctMatrixRotation3Base.h:79
Definition: vctDynamicConstVectorBase.h:77
A template for a fixed length vector with fixed spacing in memory.
Definition: vctFixedSizeConstVectorBase.h:107
const bool VCT_DO_NOT_NORMALIZE
Definition: vctForwardDeclarations.h:69
ThisType & NormalizedSelf(void)
Definition: vctMatrixRotation3Base.h:493
cmnTypeTraits< value_type > TypeTraits
Definition: vctMatrixRotation3Base.h:86
Declaration of vctQuaternionRotation3Base.
Define a rotation quaternion for a space of dimension 3.
Definition: vctForwardDeclarations.h:208
Macros to export the symbols of cisstVector (in a Dll).
void vctMatrixRotation3BaseFromRaw(vctMatrixRotation3Base< _matrixType > &matrixRotation, const vctQuaternionRotation3Base< _quaternionType > &quaternionRotation)
Definition: vctMatrixRotation3Base.h:571
A collection of useful information about the C++ basic types, represented in a generic programming wa...
Definition: cmnTypeTraits.h:155
ThisType & FromNormalized(const vctDynamicConstVectorBase< __vectorOwnerType1, value_type > &v1, const vctDynamicConstVectorBase< __vectorOwnerType2, value_type > &v2, const vctDynamicConstVectorBase< __vectorOwnerType3, value_type > &v3, bool vectorsAreColumns=true)
Definition: vctMatrixRotation3Base.h:288
Definition: vctMatrixRotation3Base.h:75
ThisType & FromNormalized(const vctMatrixRotation3Base< __containerType > &other)
Definition: vctMatrixRotation3Base.h:234
ThisType & From(const vctFixedSizeConstVectorBase< DIMENSION, __stride1, value_type, __dataPtrType1 > &v1, const vctFixedSizeConstVectorBase< DIMENSION, __stride2, value_type, __dataPtrType2 > &v2, const vctFixedSizeConstVectorBase< DIMENSION, __stride3, value_type, __dataPtrType3 > &v3, bool vectorsAreColumns=true)
Definition: vctMatrixRotation3Base.h:141
Define a rotation based on the rodriguez representation for a space of dimension 3.
Definition: vctForwardDeclarations.h:214
ThisType & FromRaw(const vctQuaternionRotation3Base< __containerType > &other)
Definition: vctQuaternionRotation3Base.h:447