cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctEulerRotation3.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  Created on: 2011-05-18
7 
8  (C) Copyright 2011-2014 Johns Hopkins University (JHU), All Rights Reserved.
9 
10 --- begin cisst license - do not edit ---
11 
12 This software is provided "as is" under an open source license, with
13 no warranty. The complete license can be found in license.txt and
14 http://www.cisst.org/cisst/license.txt.
15 
16 --- end cisst license ---
17 */
18 
19 #ifndef _vctEulerRotation3_h
20 #define _vctEulerRotation3_h
21 
30 
31 // Always include last!
32 #include <cisstVector/vctExport.h>
33 
65 #ifndef SWIG
66 // helper functions for subtemplated methods of a templated class
67 
68 #define VCT_DECLARE_EULER_CONVERSIONS(ORDER) \
69  template <class _matrixType> \
70  void \
71  vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
72  const vctMatrixRotation3Base<_matrixType> & matrixRot); \
73  template <class _matrixType> \
74  void \
75  vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
76  vctMatrixRotation3Base<_matrixType> & matrixRot);
77 
79 VCT_DECLARE_EULER_CONVERSIONS(vctEulerRotation3Order::ZYX)
80 #endif
81 
82 
83 namespace vctEulerRotation3Order {
85 };
86 
87 
88 // This base class may not be necessary; if we keep it, it could be moved to vctEulerRotation3Base.h
90 protected:
93 
95 
97  inline void ThrowUnlessIsNormalized(void) const throw(std::runtime_error) {
98  if (! IsNormalized()) {
99  cmnThrow(std::runtime_error("vctEulerRotation3Base: This rotation is not normalized"));
100  }
101  }
102 
107  template <class _inputType>
108  inline void ThrowUnlessIsNormalized(const _inputType & input) const throw(std::runtime_error) {
109  if (! input.IsNormalized()) {
110  cmnThrow(std::runtime_error("vctEulerRotation3Base: Input is not normalized"));
111  }
112  }
113 
114 public:
116  inline vctEulerRotation3Base(void) : Angles(0.0, 0.0, 0.0) {}
117  inline vctEulerRotation3Base(const vctEulerRotation3Base & other) : Angles(other.Angles) {}
118  inline vctEulerRotation3Base(double phi, double theta, double psi) : Angles(phi, theta, psi) {}
119  inline vctEulerRotation3Base(double * angles) : Angles(angles) {}
120  inline vctEulerRotation3Base(const vct3 & angles) : Angles(angles) {}
121 
123 
124  inline double phi(void) const { return Angles[0]; }
125  inline double theta(void) const { return Angles[1]; }
126  inline double psi(void) const { return Angles[2]; }
127 
128  inline double alpha(void) const { return Angles[0]; }
129  inline double beta(void) const { return Angles[1]; }
130  inline double gamma(void) const { return Angles[2]; }
131 
133  void Assign(double phi, double theta, double psi);
134 
136  vctEulerRotation3Base & InverseSelf(void);
137 
139  vctEulerRotation3Base & NormalizedSelf(void);
140 
146  bool IsNormalized(double tolerance = TypeTraits::Tolerance()) const;
147 
148 };
149 
150 // Euler angle class templated by order convention
151 
152 template <vctEulerRotation3Order::OrderType _order>
156 
157 public:
158 
159  inline vctEulerRotation3() : BaseType() {}
160  inline vctEulerRotation3(const ThisType & other) : BaseType(other) {}
161  inline vctEulerRotation3(double phi, double theta, double psi) : BaseType(phi, theta, psi) {}
162  inline vctEulerRotation3(double * angles) : BaseType(angles) {}
163  inline vctEulerRotation3(const vct3 & angles) : BaseType(angles) {}
164 
166  template <class __containerType>
168  throw(std::runtime_error)
169  {
170  From(matrixRotation);
171  }
172 
174  template <class __containerType>
176  bool normalizeInput)
177  {
178  if (normalizeInput) {
179  FromNormalized(matrixRotation);
180  } else {
181  FromRaw(matrixRotation);
182  }
183  }
184 
186 
188  template <class _matrixType>
189  ThisType & From(const vctMatrixRotation3Base<_matrixType> & matrixRot) throw(std::runtime_error) {
190  ThrowUnlessIsNormalized(matrixRot);
191  return FromRaw(matrixRot);
192  }
193 
195  template <class _matrixType>
197  return FromRaw(matrixRot.Normalized());
198  }
199 
201  template <class _matrixType>
203  vctEulerFromMatrixRotation3(*this, matrixRotation);
204  return *this;
205  }
206 
208  vct3 GetAngles(void) const { return Angles; }
209 
211  vct3 GetAnglesInDegrees(void) const { return (180.0/cmnPI)*Angles; }
212 
215  inline ThisType & InverseOf(const ThisType & otherRotation) {
216  *this = otherRotation;
217  InverseSelf();
218  return *this;
219  }
220 
222  inline ThisType Inverse(void) const {
223  ThisType result(*this);
224  result.InverseSelf();
225  return result;
226  }
227 
233  inline ThisType & NormalizedOf(const ThisType & otherRotation) {
234  Angles = otherRotation.Angles;
235  NormalizedSelf();
236  return *this;
237  }
238 
242  inline ThisType Normalized(void) const {
243  ThisType result(*this);
244  result.NormalizedSelf();
245  return result;
246  }
247 
255  inline bool Equal(const ThisType & other) const {
256  return (this->Angles == other.Angles);
257  }
258 
259  inline bool operator==(const ThisType & other) const {
260  return this->Equal(other);
261  }
263 
264 
274  inline bool AlmostEqual(const ThisType & other,
275  double tolerance = TypeTraits::Tolerance()) const {
276  const vct3 angleDiff(this->Angles - other.Angles);
277  return (angleDiff.MaxAbsElement() < tolerance);
278  }
279 
280 
289  inline bool AlmostEquivalent(const ThisType & other,
290  double tolerance = TypeTraits::Tolerance()) const {
291  ThisType thisNorm = this->Normalized();
292  return thisNorm.AlmostEqual(other.Normalized(), tolerance);
293  }
294 
295  std::string ToString(void) const {
296  std::stringstream outputStream;
297  ToStream(outputStream);
298  return outputStream.str();
299  }
300 
302  void ToStream(std::ostream & outputStream) const {
303  outputStream << "Euler " << vctEulerRotation3Order::ToString(_order) << ": " << Angles << std::endl;
304  }
305 
307  void ToStreamRaw(std::ostream & outputStream, const char delimiter = ' ',
308  bool headerOnly = false, const std::string & headerPrefix = "") const {
309  this->Angles.ToStreamRaw(outputStream, delimiter, headerOnly, headerPrefix + "angle-");
310  }
311 
313  void SerializeRaw(std::ostream & outputStream) const
314  {
315  Angles.SerializeRaw(outputStream);
316  }
317 
319  void DeSerializeRaw(std::istream & inputStream)
320  {
321  Angles.DeSerializeRaw(inputStream);
322  }
323 
324 };
325 
328 
331 
332 #ifndef SWIG
333 #ifdef CISST_COMPILER_IS_MSVC
334 // declare instances of helper functions
335 #define VCT_DECLARE_EULER_CONVERSION_TEMPLATES(ORDER) \
336  template CISST_EXPORT void \
337  vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
338  const vctMatrixRotation3Base<vctFixedSizeMatrix<double, 3, 3, VCT_ROW_MAJOR> > & matrixRot); \
339  template CISST_EXPORT void \
340  vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
341  const vctMatrixRotation3Base<vctFixedSizeMatrix<double, 3, 3, VCT_COL_MAJOR> > & matrixRot); \
342  template CISST_EXPORT void \
343  vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
344  const vctMatrixRotation3Base<vctFixedSizeMatrix<float, 3, 3, VCT_ROW_MAJOR> > & matrixRot); \
345  template CISST_EXPORT void \
346  vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
347  const vctMatrixRotation3Base<vctFixedSizeMatrix<float, 3, 3, VCT_COL_MAJOR> > & matrixRot); \
348  template CISST_EXPORT void \
349  vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
350  const vctMatrixRotation3Base<vctFixedSizeMatrixRef<double, 3, 3, 4, 1> > & matrixRot); \
351  template CISST_EXPORT void \
352  vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
353  const vctMatrixRotation3Base<vctFixedSizeMatrixRef<double, 3, 3, 1, 4> > & matrixRot); \
354  template CISST_EXPORT void \
355  vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
356  const vctMatrixRotation3Base<vctFixedSizeMatrixRef<float, 3, 3, 4, 1> > & matrixRot); \
357  template CISST_EXPORT void \
358  vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
359  const vctMatrixRotation3Base<vctFixedSizeMatrixRef<float, 3, 3, 1, 4> > & matrixRot); \
360  template CISST_EXPORT void \
361  vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
362  vctMatrixRotation3Base<vctFixedSizeMatrix<double, 3, 3, VCT_ROW_MAJOR> > & matrixRot); \
363  template CISST_EXPORT void \
364  vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
365  vctMatrixRotation3Base<vctFixedSizeMatrix<double, 3, 3, VCT_COL_MAJOR> > & matrixRot); \
366  template CISST_EXPORT void \
367  vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
368  vctMatrixRotation3Base<vctFixedSizeMatrix<float, 3, 3, VCT_ROW_MAJOR> > & matrixRot); \
369  template CISST_EXPORT void \
370  vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
371  vctMatrixRotation3Base<vctFixedSizeMatrix<float, 3, 3, VCT_COL_MAJOR> > & matrixRot); \
372  template CISST_EXPORT void \
373  vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
374  vctMatrixRotation3Base<vctFixedSizeMatrixRef<double, 3, 3, 4, 1> > & matrixRot); \
375  template CISST_EXPORT void \
376  vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
377  vctMatrixRotation3Base<vctFixedSizeMatrixRef<double, 3, 3, 1, 4> > & matrixRot); \
378  template CISST_EXPORT void \
379  vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
380  vctMatrixRotation3Base<vctFixedSizeMatrixRef<float, 3, 3, 4, 1> > & matrixRot); \
381  template CISST_EXPORT void \
382  vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
383  vctMatrixRotation3Base<vctFixedSizeMatrixRef<float, 3, 3, 1, 4> > & matrixRot);
384 
385 VCT_DECLARE_EULER_CONVERSION_TEMPLATES(vctEulerRotation3Order::ZYZ)
386 VCT_DECLARE_EULER_CONVERSION_TEMPLATES(vctEulerRotation3Order::ZYX)
387 #endif // CISST_COMPILER_IS_MSVC
388 #endif // !SWIG
389 
390 #endif // _vctEulerRotation3_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
vctEulerRotation3Base(const vct3 &angles)
Definition: vctEulerRotation3.h:120
vctEulerRotation3(const vct3 &angles)
Definition: vctEulerRotation3.h:163
Definition: vctEulerRotation3.h:153
ThisType Normalized(void) const
Definition: vctEulerRotation3.h:242
ThisType Inverse(void) const
Definition: vctEulerRotation3.h:222
bool AlmostEquivalent(const ThisType &other, double tolerance=TypeTraits::Tolerance()) const
Definition: vctEulerRotation3.h:289
RotationValueType Normalized(void) const
Definition: vctMatrixRotation3.h:389
#define VCT_DECLARE_EULER_CONVERSIONS(ORDER)
Define an Euler angle rotation for a space of dimension 3.
Definition: vctEulerRotation3.h:68
ThisType & FromNormalized(const vctMatrixRotation3Base< _matrixType > &matrixRot)
Definition: vctEulerRotation3.h:196
vctEulerRotation3Base(const vctEulerRotation3Base &other)
Definition: vctEulerRotation3.h:117
void ToStreamRaw(std::ostream &outputStream, const char delimiter= ' ', bool headerOnly=false, const std::string &headerPrefix="") const
Definition: vctFixedSizeConstVectorBase.h:1118
static Type Tolerance(void)
Definition: cmnTypeTraits.h:170
void SerializeRaw(std::ostream &outputStream) const
Definition: vctFixedSizeConstVectorBase.h:1141
Forward declarations and #define for cisstVector.
vctEulerRotation3(double phi, double theta, double psi)
Definition: vctEulerRotation3.h:161
Definition: vctEulerRotation3.h:89
vctEulerRotation3Base & NormalizedSelf(void)
std::string ToString(void) const
Definition: vctEulerRotation3.h:295
ThisType & InverseOf(const ThisType &otherRotation)
Definition: vctEulerRotation3.h:215
void SerializeRaw(std::ostream &outputStream) const
Definition: vctEulerRotation3.h:313
void ToStream(std::ostream &outputStream) const
Definition: vctEulerRotation3.h:302
void ToStreamRaw(std::ostream &outputStream, const char delimiter= ' ', bool headerOnly=false, const std::string &headerPrefix="") const
Definition: vctEulerRotation3.h:307
Typedef for fixed size vectors.
double phi(void) const
Definition: vctEulerRotation3.h:124
Declaration of various constants.
Definition: vctForwardDeclarations.h:220
vctEulerRotation3Base(double phi, double theta, double psi)
Definition: vctEulerRotation3.h:118
vctEulerRotation3()
Definition: vctEulerRotation3.h:159
double gamma(void) const
Definition: vctEulerRotation3.h:130
cmnTypeTraits< double > TypeTraits
Definition: vctEulerRotation3.h:92
ThisType & FromRaw(const vctMatrixRotation3Base< _matrixType > &matrixRotation)
Definition: vctEulerRotation3.h:202
Define a rotation matrix for a space of dimension 3.
Definition: vctForwardDeclarations.h:199
std::string CISST_EXPORT ToString(vctEulerRotation3Order::OrderType order)
OrderType
Definition: vctForwardDeclarations.h:220
vctEulerRotation3Base & InverseSelf(void)
void Assign(const OtherT &other)
Definition: cisstVectorEigenAddons.h:3
ThisType & From(const vctMatrixRotation3Base< _matrixType > &matrixRot)
Definition: vctEulerRotation3.h:189
vct3 GetAngles(void) const
Definition: vctEulerRotation3.h:208
void DeSerializeRaw(std::istream &inputStream)
Definition: vctEulerRotation3.h:319
void DeSerializeRaw(std::istream &inputStream)
Definition: vctFixedSizeVector.h:299
vctEulerRotation3Base(void)
Definition: vctEulerRotation3.h:116
double alpha(void) const
Definition: vctEulerRotation3.h:128
double theta(void) const
Definition: vctEulerRotation3.h:125
bool Equal(const ThisType &other) const
Definition: vctEulerRotation3.h:255
vct3 Angles
Definition: vctEulerRotation3.h:94
#define cmnThrow(a)
Definition: MinimalCmn.h:4
vctEulerRotation3Base(double *angles)
Definition: vctEulerRotation3.h:119
void ThrowUnlessIsNormalized(void) const
Definition: vctEulerRotation3.h:97
~vctEulerRotation3Base()
Definition: vctEulerRotation3.h:122
vctEulerRotation3< vctEulerRotation3Order::ZYX > vctEulerZYXRotation3
Definition: vctEulerRotation3.h:330
void ThrowUnlessIsNormalized(const _inputType &input) const
Definition: vctEulerRotation3.h:108
ThisType & NormalizedOf(const ThisType &otherRotation)
Definition: vctEulerRotation3.h:233
vctEulerRotation3< vctEulerRotation3Order::ZYZ > vctEulerZYZRotation3
Definition: vctEulerRotation3.h:327
vctEulerRotation3(const vctMatrixRotation3Base< __containerType > &matrixRotation, bool normalizeInput)
Definition: vctEulerRotation3.h:175
vct3 GetAnglesInDegrees(void) const
Definition: vctEulerRotation3.h:211
vctEulerRotation3(const ThisType &other)
Definition: vctEulerRotation3.h:160
Definition: vctForwardDeclarations.h:220
vctEulerRotation3(const vctMatrixRotation3Base< __containerType > &matrixRotation)
Definition: vctEulerRotation3.h:167
bool operator==(const ThisType &other) const
Definition: vctEulerRotation3.h:259
double psi(void) const
Definition: vctEulerRotation3.h:126
double beta(void) const
Definition: vctEulerRotation3.h:129
Macros to export the symbols of cisstVector (in a Dll).
value_type MaxAbsElement(void) const
Definition: vctFixedSizeConstVectorBase.h:514
A collection of useful information about the C++ basic types, represented in a generic programming wa...
Definition: cmnTypeTraits.h:155
~vctEulerRotation3()
Definition: vctEulerRotation3.h:185
const double cmnPI
Definition: cmnConstants.h:38
vctEulerRotation3(double *angles)
Definition: vctEulerRotation3.h:162
bool AlmostEqual(const ThisType &other, double tolerance=TypeTraits::Tolerance()) const
Definition: vctEulerRotation3.h:274