cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctAxisAngleRotation3.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-01-13
8 
9  (C) Copyright 2005-2012 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 _vctAxisAngleRotation3_h
23 #define _vctAxisAngleRotation3_h
24 
31 
37 #include <cisstVector/vctExport.h>
38 
39 #ifndef DOXYGEN
40 #ifndef SWIG
41 
42 template <class _elementType, class _containerType>
43 void
45  const vctQuaternionRotation3Base<_containerType> & quaternionRotation);
46 
47 template <class _elementType, class _containerType>
48 void
50  const vctMatrixRotation3Base<_containerType> & matrixRotation);
51 
52 template <class _elementType, class _containerType>
53 void
55  const vctRodriguezRotation3Base<_containerType> & rodriguezRotation);
56 
57 #ifdef CISST_COMPILER_IS_MSVC
58 template CISST_EXPORT void
60  const vctQuaternionRotation3Base<vctFixedSizeVector<double, 4> > & quaternionRotation);
61 template CISST_EXPORT void
63  const vctQuaternionRotation3Base<vctFixedSizeVector<float, 4> > & quaternionRotation);
64 template CISST_EXPORT void
67 template CISST_EXPORT void
70 template CISST_EXPORT void
72  const vctRodriguezRotation3Base<vctFixedSizeVector<double, 3> > & rodriguezRotation);
73 template CISST_EXPORT void
75  const vctRodriguezRotation3Base<vctFixedSizeVector<float, 3> > & rodriguezRotation);
76 #endif
77 #endif // SWIG
78 #endif // DOXYGEN
79 
80 
93 template <class _elementType>
95 {
96 public:
97  /* no need to document, inherit doxygen documentation from vctFixedSizeVectorBase */
98  VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType);
99  enum {DIMENSION = 3};
103 
104 protected:
105  AxisType AxisMember;
106  AngleType AngleMember;
107 
108 
110  inline void ThrowUnlessIsNormalized(void) const throw(std::runtime_error) {
111  if (! IsNormalized()) {
112  cmnThrow(std::runtime_error("vctAxisAngleRotation3: This rotation is not normalized"));
113  }
114  }
115 
120  template <class _inputType>
121  inline void ThrowUnlessIsNormalized(const _inputType & input) const throw(std::runtime_error) {
122  if (! input.IsNormalized()) {
123  cmnThrow(std::runtime_error("vctAxisAngleRotation3: Input is not normalized"));
124  }
125  }
126 
127 public:
128 
131  AxisMember(1.0, 0.0, 0.0),
132  AngleMember(0.0)
133  {}
134 
135 
137  inline ThisType & Assign(const ThisType & other) {
138  AxisMember.Assign(other.Axis());
139  AngleMember = other.Angle();
140  return *this;
141  }
142 
143 
157 
158 
163  template <stride_type __stride, class __dataPtrType>
165  const AngleType angle)
166  throw(std::runtime_error)
167  {
168  From(axis, angle);
169  }
170 
175  template <class _vectorOwnerType>
177  AngleType angle)
178  throw(std::runtime_error)
179  {
180  From(axis, angle);
181  }
182 
184  template <class _containerType>
185  explicit inline
187  throw(std::runtime_error)
188  {
189  From(quaternionRotation);
190  }
191 
193  template <class _containerType>
194  explicit inline
196  throw(std::runtime_error)
197  {
198  From(matrixRotation);
199  }
200 
202  template <class _containerType>
203  explicit inline
205  throw(std::runtime_error)
206  {
207  From(rodriguezRotation);
208  }
209 
211 
212 
234 
240  template <stride_type __stride, class __dataPtrType>
242  const AngleType angle,
243  bool normalizeInput)
244  {
245  if (normalizeInput) {
246  FromNormalized(axis, angle);
247  } else {
248  FromRaw(axis, angle);
249  }
250  }
251 
257  template <class _vectorOwnerType>
259  AngleType angle,
260  bool normalizeInput)
261  {
262  if (normalizeInput) {
263  FromNormalized(axis, angle);
264  } else {
265  FromRaw(axis, angle);
266  }
267  }
268 
270  template <class _containerType>
272  bool normalizeInput)
273  {
274  if (normalizeInput) {
275  FromNormalized(quaternionRotation);
276  } else {
277  FromRaw(quaternionRotation);
278  }
279  }
280 
282  template <class _containerType>
284  bool normalizeInput)
285  {
286  if (normalizeInput) {
287  FromNormalized(matrixRotation);
288  } else {
289  FromRaw(matrixRotation);
290  }
291  }
292 
294  template <class _containerType>
296  bool normalizeInput)
297  {
298  if (normalizeInput) {
299  FromNormalized(rodriguezRotation);
300  } else {
301  FromRaw(rodriguezRotation);
302  }
303  }
304 
306 
307 
308 
311  static CISST_EXPORT const ThisType & Identity();
312 
314  inline const AxisType & Axis(void) const {
315  return AxisMember;
316  }
317 
319  inline AxisType & Axis(void) {
320  return AxisMember;
321  }
322 
324  inline const AngleType & Angle(void) const {
325  return AngleMember;
326  }
327 
329  inline AngleType & Angle(void) {
330  return AngleMember;
331  }
332 
333 
347 
349  template <stride_type __stride, class __dataPtrType>
351  const AngleType angle)
352  throw(std::runtime_error)
353  {
354  FromRaw(axis, angle);
355  ThrowUnlessIsNormalized();
356  return *this;
357  }
358 
360  template <class _vectorOwnerType>
362  AngleType angle)
363  throw(std::runtime_error)
364  {
365  AxisMember[0] = axis[0];
366  AxisMember[1] = axis[1];
367  AxisMember[2] = axis[2];
368  AngleMember = angle;
369  ThrowUnlessIsNormalized();
370  return *this;
371  }
372 
374  template <class _containerType>
375  inline ThisType & From(const vctQuaternionRotation3Base<_containerType> & quaternionRotation)
376  throw(std::runtime_error)
377  {
378  ThrowUnlessIsNormalized(quaternionRotation);
379  return FromRaw(quaternionRotation);
380  }
381 
383  template <class _containerType>
384  inline ThisType & From(const vctMatrixRotation3Base<_containerType> & matrixRotation)
385  throw(std::runtime_error)
386  {
387  ThrowUnlessIsNormalized(matrixRotation);
388  return FromRaw(matrixRotation);
389  }
390 
392  template <class _containerType>
393  inline ThisType & From(const vctRodriguezRotation3Base<_containerType> & rodriguezRotation)
394  throw(std::runtime_error)
395  {
396  ThrowUnlessIsNormalized(rodriguezRotation);
397  return FromRaw(rodriguezRotation);
398  }
400 
401 
402 
403 
413 
415  template <stride_type __stride, class __dataPtrType>
417  const AngleType angle)
418  {
419  FromRaw(axis, angle);
420  NormalizedSelf();
421  return *this;
422  }
423 
425  template <class _containerType>
426  inline ThisType & FromNormalized(const vctQuaternionRotation3Base<_containerType> & quaternionRotation) {
427  return FromRaw(quaternionRotation.Normalized());
428  }
429 
431  template <class _containerType>
432  inline ThisType & FromNormalized(const vctMatrixRotation3Base<_containerType> & matrixRotation) {
433  return FromRaw(matrixRotation.Normalized());
434  }
435 
437  template <class _containerType>
438  inline ThisType & FromNormalized(const vctRodriguezRotation3Base<_containerType> & rodriguezRotation) {
439  return FromRaw(rodriguezRotation.Normalized());
440  }
442 
443 
444 
454 
456  template <stride_type __stride, class __dataPtrType>
458  const AngleType angle)
459  {
460  AxisMember.Assign(axis);
461  AngleMember = angle;
462  return *this;
463  }
464 
466  template <class _containerType>
467  inline ThisType & FromRaw(const vctQuaternionRotation3Base<_containerType> & quaternionRotation) {
468  vctAxisAngleRotation3FromRaw(*this, quaternionRotation);
469  return *this;
470  }
471 
473  template <class _containerType>
474  inline ThisType & FromRaw(const vctMatrixRotation3Base<_containerType> & matrixRotation) {
475  vctAxisAngleRotation3FromRaw(*this, matrixRotation);
476  return *this;
477  }
478 
480  template <class _containerType>
481  inline ThisType & FromRaw(const vctRodriguezRotation3Base<_containerType> & rodriguezRotation) {
482  vctAxisAngleRotation3FromRaw(*this, rodriguezRotation);
483  return *this;
484  }
485 
487 
488 
493  inline ThisType & InverseSelf(void) {
494  AngleMember = 2.0 * cmnPI - AngleMember;
495  return *this;
496  }
497 
500  inline ThisType & InverseOf(const ThisType & otherRotation) {
501  AxisMember.Assign(otherRotation.Axis());
502  AngleMember = 2.0 * cmnPI - otherRotation.Angle();
503  return *this;
504  }
505 
509  inline ThisType Inverse(void) const {
510  ThisType result;
511  result.Angle() = 2 * cmnPI - this->Angle();
512  return result;
513  }
514 
518  inline ThisType & NormalizedSelf(void) {
519  AxisMember.NormalizedSelf();
520  AngleMember = fmod(AngleMember, 2.0 * cmnPI);
521  if (AngleMember < 0.0) {
522  AngleMember += (2.0 * cmnPI);
523  }
524  return *this;
525  }
526 
532  inline ThisType & NormalizedOf(const ThisType & otherRotation) {
533  AxisMember.Assign(otherRotation.Axis());
534  AngleMember = otherRotation.Angle();
535  NormalizedSelf();
536  return *this;
537  }
538 
542  inline ThisType Normalized(void) const {
543  ThisType result(*this);
544  result.NormalizedSelf();
545  return result;
546  }
547 
548 
554  inline bool IsNormalized(value_type tolerance = TypeTraits::Tolerance()) const {
555  return AxisMember.IsNormalized(tolerance);
556  }
557 
558 
566  inline bool Equal(const ThisType & other) const {
567  return ((this->Angle() == other.Angle()) && (this->Axis().Equal(other.Axis())));
568  }
569 
570  inline bool operator==(const ThisType & other) const {
571  return this->Equal(other);
572  }
574 
575 
585  inline bool AlmostEqual(const ThisType & other,
586  value_type tolerance = TypeTraits::Tolerance()) const {
587  const AngleType angleDiff = this->Angle() - other.Angle();
588  const AngleType angle = (angleDiff < 0) ? -angleDiff : angleDiff;
589 
590  return ((angle <= tolerance) && (this->Axis().AlmostEqual(other.Axis(), tolerance)));
591  }
592 
593 
605  inline bool AlmostEquivalent(const ThisType & other,
606  value_type tolerance = TypeTraits::Tolerance()) const {
607  const AngleType angleThis = this->Angle();
608  const AngleType angleOther = other.Angle();
609  // two quasi null rotations
610  if ((angleThis <= tolerance) && (angleOther <= tolerance)) {
611  return true;
612  }
613  // one rotation is almost null
614  if ((angleThis <= tolerance) || (angleOther <= tolerance)) {
615  return false;
616  }
617 
618  // both rotations are non null
619  const AngleType dotProduct = AngleType(this->Axis().Normalized().DotProduct(other.Axis().Normalized()));
620  const AngleType absDotProduct = dotProduct > value_type(0.0) ? dotProduct : -dotProduct;
621  // axis don't seem aligned
622  if ((absDotProduct <= (AngleType(1.0) - tolerance))
623  || (absDotProduct >= (AngleType(1.0) + tolerance))) {
624  return false;
625  }
626  // axis are aligned
627  const AngleType angleDifference = angleThis - dotProduct * angleOther;
628  AngleType angleRemain = fmod(angleDifference, 2.0 * cmnPI);
629  if (angleRemain > cmnPI) {
630  angleRemain -= (2.0 * cmnPI);
631  } else if (angleRemain < -cmnPI) {
632  angleRemain += (2.0 *cmnPI);
633  }
634  if ((angleRemain > -tolerance) && (angleRemain < tolerance)) {
635  return true;
636  }
637  return false;
638  }
639 
640 
641  std::string ToString(void) const {
642  std::stringstream outputStream;
643  ToStream(outputStream);
644  return outputStream.str();
645  }
646 
648  void ToStream(std::ostream & outputStream) const {
649  outputStream << "axis: "
650  << std::endl
651  << this->Axis()
652  << std::endl
653  << "angle: "
654  << std::endl
655  << this->Angle();
656  }
657 
659  void ToStreamRaw(std::ostream & outputStream, const char delimiter = ' ',
660  bool headerOnly = false, const std::string & headerPrefix = "") const {
661  this->Axis().ToStreamRaw(outputStream, delimiter, headerOnly, headerPrefix + "axis-");
662  outputStream << delimiter;
663  if (headerOnly) {
664  outputStream << headerPrefix << "angle";
665  } else {
666  outputStream << this->Angle();
667  }
668  }
669 
671  void SerializeRaw(std::ostream & outputStream) const
672  {
673  this->Axis().SerializeRaw(outputStream);
674  cmnSerializeRaw(outputStream, this->Angle());
675  }
676 
678  void DeSerializeRaw(std::istream & inputStream)
679  {
680  this->Axis().DeSerializeRaw(inputStream);
681  cmnDeSerializeRaw(inputStream, this->Angle());
682  }
683 
684 };
685 
686 
688 template<class _elementType>
689 std::ostream & operator << (std::ostream & output,
690  const vctAxisAngleRotation3<_elementType> & axisAngle) {
691  axisAngle.ToStream(output);
692  return output;
693 }
694 
695 
696 #endif // _vctAxisAngleRotation3_h
vctFixedSizeVector< value_type, DIMENSION > AxisType
Definition: vctAxisAngleRotation3.h:101
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
vctAxisAngleRotation3(const vctMatrixRotation3Base< _containerType > &matrixRotation, bool normalizeInput)
Definition: vctAxisAngleRotation3.h:283
bool AlmostEquivalent(const ThisType &other, value_type tolerance=TypeTraits::Tolerance()) const
Definition: vctAxisAngleRotation3.h:605
vctAxisAngleRotation3(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation, bool normalizeInput)
Definition: vctAxisAngleRotation3.h:295
ThisType & Assign(const ThisType &other)
Definition: vctAxisAngleRotation3.h:137
const AxisType & Axis(void) const
Definition: vctAxisAngleRotation3.h:314
vctAxisAngleRotation3(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle)
Definition: vctAxisAngleRotation3.h:164
RotationValueType Normalized(void) const
Definition: vctMatrixRotation3.h:389
ThisType & From(const vctQuaternionRotation3Base< _containerType > &quaternionRotation)
Definition: vctAxisAngleRotation3.h:375
std::string ToString(void) const
Definition: vctAxisAngleRotation3.h:641
AngleType & Angle(void)
Definition: vctAxisAngleRotation3.h:329
AngleType AngleMember
Definition: vctAxisAngleRotation3.h:106
ThisType & FromNormalized(const vctQuaternionRotation3Base< _containerType > &quaternionRotation)
Definition: vctAxisAngleRotation3.h:426
ThisType & FromRaw(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle)
Definition: vctAxisAngleRotation3.h:457
cmnTypeTraits< _elementType > TypeTraits
Definition: vctAxisAngleRotation3.h:102
vctAxisAngleRotation3()
Definition: vctAxisAngleRotation3.h:130
ThisType & FromRaw(const vctMatrixRotation3Base< _containerType > &matrixRotation)
Definition: vctAxisAngleRotation3.h:474
void SerializeRaw(std::ostream &outputStream) const
Definition: vctAxisAngleRotation3.h:671
ThisType & NormalizedSelf(void)
Definition: vctFixedSizeVectorBase.h:1363
ThisType & FromNormalized(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle)
Definition: vctAxisAngleRotation3.h:416
vctAxisAngleRotation3(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle, bool normalizeInput)
Definition: vctAxisAngleRotation3.h:241
AxisType & Axis(void)
Definition: vctAxisAngleRotation3.h:319
vctAxisAngleRotation3(const vctMatrixRotation3Base< _containerType > &matrixRotation)
Definition: vctAxisAngleRotation3.h:195
ThisType & From(const vctMatrixRotation3Base< _containerType > &matrixRotation)
Definition: vctAxisAngleRotation3.h:384
Declaration of various constants.
#define VCT_CONTAINER_TRAITS_TYPEDEFS(type)
Definition: vctContainerTraits.h:50
ThisType & Assign(const vctFixedSizeConstVectorBase< _size, __stride, __elementType, __dataPtrType > &other)
Definition: vctFixedSizeVectorBase.h:274
vctAxisAngleRotation3< value_type > ThisType
Definition: vctAxisAngleRotation3.h:100
ThisType & From(const vctDynamicConstVectorBase< _vectorOwnerType, value_type > &axis, AngleType angle)
Definition: vctAxisAngleRotation3.h:361
ThisType Normalized(void) const
Definition: vctQuaternionRotation3Base.h:529
ThisType & From(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle)
Definition: vctAxisAngleRotation3.h:350
ThisType & FromNormalized(const vctMatrixRotation3Base< _containerType > &matrixRotation)
Definition: vctAxisAngleRotation3.h:432
Declaration of vctDynamicVector.
Define a rotation based on an axis and an angle for a space of dimension 3.
Definition: vctAxisAngleRotation3.h:94
ThisType & NormalizedSelf(void)
Definition: vctAxisAngleRotation3.h:518
bool Equal(const ThisType &other) const
Definition: vctAxisAngleRotation3.h:566
ThisType Inverse(void) const
Definition: vctAxisAngleRotation3.h:509
Define a rotation matrix for a space of dimension 3.
Definition: vctForwardDeclarations.h:199
ThisType & From(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation)
Definition: vctAxisAngleRotation3.h:393
ThisType Normalized(void) const
Definition: vctAxisAngleRotation3.h:542
Declaration of vctFixedSizeVector.
void cmnDeSerializeRaw(std::istream &inputStream, _elementType &data)
Definition: cmnDeSerializer.h:82
vctAxisAngleRotation3(const vctDynamicConstVectorBase< _vectorOwnerType, value_type > &axis, AngleType angle)
Definition: vctAxisAngleRotation3.h:176
ThisType Normalized(void) const
Definition: vctRodriguezRotation3Base.h:304
std::ostream & operator<<(std::ostream &output, const vctAxisAngleRotation3< _elementType > &axisAngle)
Definition: vctAxisAngleRotation3.h:689
void cmnSerializeRaw(std::ostream &outputStream, const _elementType &data)
Definition: cmnSerializer.h:78
Declaration of vctRodriguezRotation3Base.
vctAxisAngleRotation3(const vctQuaternionRotation3Base< _containerType > &quaternionRotation, bool normalizeInput)
Definition: vctAxisAngleRotation3.h:271
vctAxisAngleRotation3(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation)
Definition: vctAxisAngleRotation3.h:204
ThisType & FromNormalized(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation)
Definition: vctAxisAngleRotation3.h:438
void ThrowUnlessIsNormalized(void) const
Definition: vctAxisAngleRotation3.h:110
#define cmnThrow(a)
Definition: MinimalCmn.h:4
void DeSerializeRaw(std::istream &inputStream)
Definition: vctAxisAngleRotation3.h:678
void ToStream(std::ostream &outputStream) const
Definition: vctAxisAngleRotation3.h:648
void ToStreamRaw(std::ostream &outputStream, const char delimiter= ' ', bool headerOnly=false, const std::string &headerPrefix="") const
Definition: vctAxisAngleRotation3.h:659
void vctAxisAngleRotation3FromRaw(vctAxisAngleRotation3< _elementType > &axisAngleRotation, const vctQuaternionRotation3Base< _containerType > &quaternionRotation)
ThisType & FromRaw(const vctQuaternionRotation3Base< _containerType > &quaternionRotation)
Definition: vctAxisAngleRotation3.h:467
vctAxisAngleRotation3(const vctDynamicConstVectorBase< _vectorOwnerType, value_type > &axis, AngleType angle, bool normalizeInput)
Definition: vctAxisAngleRotation3.h:258
bool AlmostEqual(const ThisType &other, value_type tolerance=TypeTraits::Tolerance()) const
Definition: vctAxisAngleRotation3.h:585
const AngleType & Angle(void) const
Definition: vctAxisAngleRotation3.h:324
Declaration of vctMatrixRotation3Base.
VectorValueType Normalized(void) const
Definition: vctFixedSizeVector.h:569
bool operator==(const ThisType &other) const
Definition: vctAxisAngleRotation3.h:570
Definition: vctDynamicConstVectorBase.h:77
A template for a fixed length vector with fixed spacing in memory.
Definition: vctFixedSizeConstVectorBase.h:107
ThisType & FromRaw(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation)
Definition: vctAxisAngleRotation3.h:481
ThisType & InverseOf(const ThisType &otherRotation)
Definition: vctAxisAngleRotation3.h:500
bool IsNormalized(_elementType tolerance=cmnTypeTraits< _elementType >::Tolerance()) const
Definition: vctFixedSizeConstVectorBase.h:461
void ThrowUnlessIsNormalized(const _inputType &input) const
Definition: vctAxisAngleRotation3.h:121
Declaration of vctQuaternionRotation3Base.
Define a rotation quaternion for a space of dimension 3.
Definition: vctForwardDeclarations.h:208
AxisType AxisMember
Definition: vctAxisAngleRotation3.h:105
Macros to export the symbols of cisstVector (in a Dll).
bool IsNormalized(value_type tolerance=TypeTraits::Tolerance()) const
Definition: vctAxisAngleRotation3.h:554
A collection of useful information about the C++ basic types, represented in a generic programming wa...
Definition: cmnTypeTraits.h:155
vctAxisAngleRotation3(const vctQuaternionRotation3Base< _containerType > &quaternionRotation)
Definition: vctAxisAngleRotation3.h:186
const double cmnPI
Definition: cmnConstants.h:38
Define a rotation based on the rodriguez representation for a space of dimension 3.
Definition: vctForwardDeclarations.h:214
ThisType & NormalizedOf(const ThisType &otherRotation)
Definition: vctAxisAngleRotation3.h:532
ThisType & InverseSelf(void)
Definition: vctAxisAngleRotation3.h:493