cisst-saw
Loading...
Searching...
No Matches
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 Created on: 2011-05-18
6
7 (C) Copyright 2011-2019 Johns Hopkins University (JHU), All Rights Reserved.
8
9--- begin cisst license - do not edit ---
10
11This software is provided "as is" under an open source license, with
12no warranty. The complete license can be found in license.txt and
13http://www.cisst.org/cisst/license.txt.
14
15--- end cisst license ---
16*/
17
18#ifndef _vctEulerRotation3_h
19#define _vctEulerRotation3_h
20
25
29
30// Always include last!
32
63
64#ifndef SWIG
65// helper functions for subtemplated methods of a templated class
66
67#define VCT_DECLARE_EULER_CONVERSIONS(ORDER) \
68 template <class _matrixType> \
69 void \
70 vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
71 const vctMatrixRotation3Base<_matrixType> & matrixRot); \
72 template <class _matrixType> \
73 void \
74 vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
75 vctMatrixRotation3Base<_matrixType> & matrixRot);
76
81#endif
82
83
87
88
89// This base class may not be necessary; if we keep it, it could be moved to vctEulerRotation3Base.h
91protected:
94
96
98 inline void ThrowUnlessIsNormalized(void) const CISST_THROW(std::runtime_error) {
99 if (! IsNormalized()) {
100 cmnThrow(std::runtime_error("vctEulerRotation3Base: This rotation is not normalized"));
101 }
102 }
103
108 template <class _inputType>
109 inline void ThrowUnlessIsNormalized(const _inputType & input) const CISST_THROW(std::runtime_error) {
110 if (! input.IsNormalized()) {
111 cmnThrow(std::runtime_error("vctEulerRotation3Base: Input is not normalized"));
112 }
113 }
114
115public:
117 inline vctEulerRotation3Base(void) : Angles(0.0, 0.0, 0.0) {}
119 inline vctEulerRotation3Base(double phi, double theta, double psi) : Angles(phi, theta, psi) {}
120 inline vctEulerRotation3Base(double * angles) : Angles(angles) {}
121 inline vctEulerRotation3Base(const vct3 & angles) : Angles(angles) {}
122
124
125 inline double phi(void) const { return Angles[0]; }
126 inline double theta(void) const { return Angles[1]; }
127 inline double psi(void) const { return Angles[2]; }
128
129 inline double alpha(void) const { return Angles[0]; }
130 inline double beta(void) const { return Angles[1]; }
131 inline double gamma(void) const { return Angles[2]; }
132
134 void Assign(double phi, double theta, double psi);
135
138
141
147 bool IsNormalized(double tolerance = TypeTraits::Tolerance()) const;
148
149};
150
151// Euler angle class templated by order convention
152
153template <vctEulerRotation3Order::OrderType _order>
155public:
158
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
165 inline ThisType & operator = (const ThisType & other) {
166 Angles.Assign(other.Angles);
167 return *this;
168 }
169
171 template <class __containerType>
173 CISST_THROW(std::runtime_error)
174 {
175 From(matrixRotation);
176 }
177
179 template <class __containerType>
181 bool normalizeInput)
182 {
183 if (normalizeInput) {
184 FromNormalized(matrixRotation);
185 } else {
186 FromRaw(matrixRotation);
187 }
188 }
189
191
193 template <class _matrixType>
194 ThisType & From(const vctMatrixRotation3Base<_matrixType> & matrixRot) CISST_THROW(std::runtime_error) {
195 ThrowUnlessIsNormalized(matrixRot);
196 return FromRaw(matrixRot);
197 }
198
200 template <class _matrixType>
202 return FromRaw(matrixRot.Normalized());
203 }
204
206 template <class _matrixType>
208 vctEulerFromMatrixRotation3(*this, matrixRotation);
209 return *this;
210 }
211
213 const vct3 & GetAngles(void) const { return Angles; }
214 vct3 & GetAngles(void) { return Angles; }
215
217 vct3 GetAnglesInDegrees(void) const { return (180.0/cmnPI)*Angles; }
218
221 inline ThisType & InverseOf(const ThisType & otherRotation) {
222 *this = otherRotation;
223 InverseSelf();
224 return *this;
225 }
226
228 inline ThisType Inverse(void) const {
229 ThisType result(*this);
230 result.InverseSelf();
231 return result;
232 }
233
239 inline ThisType & NormalizedOf(const ThisType & otherRotation) {
240 Angles = otherRotation.Angles;
242 return *this;
243 }
244
248 inline ThisType Normalized(void) const {
249 ThisType result(*this);
250 result.NormalizedSelf();
251 return result;
252 }
253
261 inline bool Equal(const ThisType & other) const {
262 return (this->Angles == other.Angles);
263 }
264
265 inline bool operator==(const ThisType & other) const {
266 return this->Equal(other);
267 }
268
269
270
280 inline bool AlmostEqual(const ThisType & other,
281 double tolerance = TypeTraits::Tolerance()) const {
282 const vct3 angleDiff(this->Angles - other.Angles);
283 return (angleDiff.MaxAbsElement() < tolerance);
284 }
285
286
295 inline bool AlmostEquivalent(const ThisType & other,
296 double tolerance = TypeTraits::Tolerance()) const {
297 ThisType thisNorm = this->Normalized();
298 return thisNorm.AlmostEqual(other.Normalized(), tolerance);
299 }
300
301 std::string ToString(void) const {
302 std::stringstream outputStream;
303 ToStream(outputStream);
304 return outputStream.str();
305 }
306
308 void ToStream(std::ostream & outputStream) const {
309 outputStream << "Euler " << vctEulerRotation3Order::ToString(_order) << ": " << Angles << std::endl;
310 }
311
313 void ToStreamRaw(std::ostream & outputStream, const char delimiter = ' ',
314 bool headerOnly = false, const std::string & headerPrefix = "") const {
315 this->Angles.ToStreamRaw(outputStream, delimiter, headerOnly, headerPrefix + "angle-");
316 }
317
319 void SerializeRaw(std::ostream & outputStream) const
320 {
321 Angles.SerializeRaw(outputStream);
322 }
323
325 void DeSerializeRaw(std::istream & inputStream)
326 {
327 Angles.DeSerializeRaw(inputStream);
328 }
329
330};
331
334
337
340
343
344#ifndef SWIG
345#ifdef CISST_COMPILER_IS_MSVC
346// declare instances of helper functions
347#define VCT_DECLARE_EULER_CONVERSION_TEMPLATES(ORDER) \
348 template CISST_EXPORT void \
349 vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
350 const vctMatrixRotation3Base<vctFixedSizeMatrix<double, 3, 3, VCT_ROW_MAJOR> > & matrixRot); \
351 template CISST_EXPORT void \
352 vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
353 const vctMatrixRotation3Base<vctFixedSizeMatrix<double, 3, 3, VCT_COL_MAJOR> > & matrixRot); \
354 template CISST_EXPORT void \
355 vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
356 const vctMatrixRotation3Base<vctFixedSizeMatrix<float, 3, 3, VCT_ROW_MAJOR> > & matrixRot); \
357 template CISST_EXPORT void \
358 vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
359 const vctMatrixRotation3Base<vctFixedSizeMatrix<float, 3, 3, VCT_COL_MAJOR> > & matrixRot); \
360 template CISST_EXPORT void \
361 vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
362 const vctMatrixRotation3Base<vctFixedSizeMatrixRef<double, 3, 3, 4, 1> > & matrixRot); \
363 template CISST_EXPORT void \
364 vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
365 const vctMatrixRotation3Base<vctFixedSizeMatrixRef<double, 3, 3, 1, 4> > & matrixRot); \
366 template CISST_EXPORT void \
367 vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
368 const vctMatrixRotation3Base<vctFixedSizeMatrixRef<float, 3, 3, 4, 1> > & matrixRot); \
369 template CISST_EXPORT void \
370 vctEulerFromMatrixRotation3(vctEulerRotation3<ORDER> & eulerRot, \
371 const vctMatrixRotation3Base<vctFixedSizeMatrixRef<float, 3, 3, 1, 4> > & matrixRot); \
372 template CISST_EXPORT void \
373 vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
374 vctMatrixRotation3Base<vctFixedSizeMatrix<double, 3, 3, VCT_ROW_MAJOR> > & matrixRot); \
375 template CISST_EXPORT void \
376 vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
377 vctMatrixRotation3Base<vctFixedSizeMatrix<double, 3, 3, VCT_COL_MAJOR> > & matrixRot); \
378 template CISST_EXPORT void \
379 vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
380 vctMatrixRotation3Base<vctFixedSizeMatrix<float, 3, 3, VCT_ROW_MAJOR> > & matrixRot); \
381 template CISST_EXPORT void \
382 vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
383 vctMatrixRotation3Base<vctFixedSizeMatrix<float, 3, 3, VCT_COL_MAJOR> > & matrixRot); \
384 template CISST_EXPORT void \
385 vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
386 vctMatrixRotation3Base<vctFixedSizeMatrixRef<double, 3, 3, 4, 1> > & matrixRot); \
387 template CISST_EXPORT void \
388 vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
389 vctMatrixRotation3Base<vctFixedSizeMatrixRef<double, 3, 3, 1, 4> > & matrixRot); \
390 template CISST_EXPORT void \
391 vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
392 vctMatrixRotation3Base<vctFixedSizeMatrixRef<float, 3, 3, 4, 1> > & matrixRot); \
393 template CISST_EXPORT void \
394 vctEulerToMatrixRotation3(const vctEulerRotation3<ORDER> & eulerRot, \
395 vctMatrixRotation3Base<vctFixedSizeMatrixRef<float, 3, 3, 1, 4> > & matrixRot);
396
397VCT_DECLARE_EULER_CONVERSION_TEMPLATES(vctEulerRotation3Order::ZYZ)
398VCT_DECLARE_EULER_CONVERSION_TEMPLATES(vctEulerRotation3Order::ZYX)
399VCT_DECLARE_EULER_CONVERSION_TEMPLATES(vctEulerRotation3Order::ZXZ)
400VCT_DECLARE_EULER_CONVERSION_TEMPLATES(vctEulerRotation3Order::YZX)
401#endif // CISST_COMPILER_IS_MSVC
402#endif // !SWIG
403
404#endif // _vctEulerRotation3_h
A collection of useful information about the C++ basic types, represented in a generic programming wa...
Definition cmnTypeTraits.h:155
static Type Tolerance(void)
Definition cmnTypeTraits.h:170
vctEulerRotation3Base(double phi, double theta, double psi)
Definition vctEulerRotation3.h:119
vct3 Angles
Definition vctEulerRotation3.h:95
vctEulerRotation3Base(double *angles)
Definition vctEulerRotation3.h:120
vctEulerRotation3Base(const vct3 &angles)
Definition vctEulerRotation3.h:121
double psi(void) const
Definition vctEulerRotation3.h:127
void ThrowUnlessIsNormalized(const _inputType &input) const CISST_THROW(std
Definition vctEulerRotation3.h:109
void Assign(double phi, double theta, double psi)
double gamma(void) const
Definition vctEulerRotation3.h:131
double beta(void) const
Definition vctEulerRotation3.h:130
double phi(void) const
Definition vctEulerRotation3.h:125
double theta(void) const
Definition vctEulerRotation3.h:126
bool IsNormalized(double tolerance=TypeTraits::Tolerance()) const
void ThrowUnlessIsNormalized(void) const CISST_THROW(std
Definition vctEulerRotation3.h:98
vctEulerRotation3Base(const vctEulerRotation3Base &other)
Definition vctEulerRotation3.h:118
double alpha(void) const
Definition vctEulerRotation3.h:129
~vctEulerRotation3Base()
Definition vctEulerRotation3.h:123
vctEulerRotation3Base(void)
Definition vctEulerRotation3.h:117
vctEulerRotation3Base & InverseSelf(void)
cmnTypeTraits< double > TypeTraits
Definition vctEulerRotation3.h:93
vctEulerRotation3Base & NormalizedSelf(void)
Definition vctEulerRotation3.h:154
ThisType & FromNormalized(const vctMatrixRotation3Base< _matrixType > &matrixRot)
Definition vctEulerRotation3.h:201
ThisType & From(const vctMatrixRotation3Base< _matrixType > &matrixRot) CISST_THROW(std
Definition vctEulerRotation3.h:194
void ToStream(std::ostream &outputStream) const
Definition vctEulerRotation3.h:308
const vct3 & GetAngles(void) const
Definition vctEulerRotation3.h:213
~vctEulerRotation3()
Definition vctEulerRotation3.h:190
ThisType Inverse(void) const
Definition vctEulerRotation3.h:228
bool AlmostEquivalent(const ThisType &other, double tolerance=TypeTraits::Tolerance()) const
Definition vctEulerRotation3.h:295
vctEulerRotation3< _order > ThisType
Definition vctEulerRotation3.h:156
ThisType & FromRaw(const vctMatrixRotation3Base< _matrixType > &matrixRotation)
Definition vctEulerRotation3.h:207
ThisType Normalized(void) const
Definition vctEulerRotation3.h:248
vctEulerRotation3(double *angles)
Definition vctEulerRotation3.h:162
ThisType & operator=(const ThisType &other)
Definition vctEulerRotation3.h:165
std::string ToString(void) const
Definition vctEulerRotation3.h:301
vctEulerRotation3Base BaseType
Definition vctEulerRotation3.h:157
ThisType & InverseOf(const ThisType &otherRotation)
Definition vctEulerRotation3.h:221
void DeSerializeRaw(std::istream &inputStream)
Definition vctEulerRotation3.h:325
vctEulerRotation3()
Definition vctEulerRotation3.h:159
bool Equal(const ThisType &other) const
Definition vctEulerRotation3.h:261
vct3 GetAnglesInDegrees(void) const
Definition vctEulerRotation3.h:217
vctEulerRotation3(const vctMatrixRotation3Base< __containerType > &matrixRotation) CISST_THROW(std
Definition vctEulerRotation3.h:172
vctEulerRotation3(const vct3 &angles)
Definition vctEulerRotation3.h:163
vct3 & GetAngles(void)
Definition vctEulerRotation3.h:214
ThisType & NormalizedOf(const ThisType &otherRotation)
Definition vctEulerRotation3.h:239
vctEulerRotation3(const ThisType &other)
Definition vctEulerRotation3.h:160
vctEulerRotation3(double phi, double theta, double psi)
Definition vctEulerRotation3.h:161
void ToStreamRaw(std::ostream &outputStream, const char delimiter=' ', bool headerOnly=false, const std::string &headerPrefix="") const
Definition vctEulerRotation3.h:313
bool operator==(const ThisType &other) const
Definition vctEulerRotation3.h:265
vctEulerRotation3(const vctMatrixRotation3Base< __containerType > &matrixRotation, bool normalizeInput)
Definition vctEulerRotation3.h:180
bool AlmostEqual(const ThisType &other, double tolerance=TypeTraits::Tolerance()) const
Definition vctEulerRotation3.h:280
void SerializeRaw(std::ostream &outputStream) const
Definition vctEulerRotation3.h:319
void ToStreamRaw(std::ostream &outputStream, const char delimiter=' ', bool headerOnly=false, const std::string &headerPrefix="") const
Definition vctFixedSizeConstVectorBase.h:1118
void SerializeRaw(std::ostream &outputStream) const
Definition vctFixedSizeConstVectorBase.h:1141
value_type MaxAbsElement(void) const
Definition vctFixedSizeConstVectorBase.h:514
ThisType & Assign(const vctFixedSizeConstVectorBase< _size, __stride, __elementType, __dataPtrType > &other)
Definition vctFixedSizeVectorBase.h:274
void DeSerializeRaw(std::istream &inputStream)
Definition vctFixedSizeVector.h:301
Define a rotation matrix for a space of dimension 3.
Definition vctMatrixRotation3Base.h:71
RotationValueType Normalized(void) const
Definition vctMatrixRotation3.h:394
Declaration of various constants.
const double cmnPI
Definition cmnConstants.h:38
#define CISST_EXPORT
Definition cmnExportMacros.h:50
#define CISST_THROW(exceptionParameter)
Somewhat portable compilation warning message. This works with very recent versions of gcc (4....
Definition cmnPortability.h:559
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
Definition vctEulerRotation3.h:84
std::string CISST_EXPORT ToString(vctEulerRotation3Order::OrderType order)
OrderType
Definition vctForwardDeclarations.h:218
@ ZXZ
Definition vctForwardDeclarations.h:218
@ ZYX
Definition vctForwardDeclarations.h:218
@ YZX
Definition vctForwardDeclarations.h:218
@ ZYZ
Definition vctForwardDeclarations.h:218
vctEulerRotation3< vctEulerRotation3Order::ZXZ > vctEulerZXZRotation3
Definition vctEulerRotation3.h:339
vctEulerRotation3< vctEulerRotation3Order::ZYX > vctEulerZYXRotation3
Definition vctEulerRotation3.h:336
vctEulerRotation3< vctEulerRotation3Order::ZYZ > vctEulerZYZRotation3
Definition vctEulerRotation3.h:333
#define VCT_DECLARE_EULER_CONVERSIONS(ORDER)
Define an Euler angle rotation for a space of dimension 3.
Definition vctEulerRotation3.h:67
vctEulerRotation3< vctEulerRotation3Order::YZX > vctEulerYZXRotation3
Definition vctEulerRotation3.h:342
Macros to export the symbols of cisstVector (in a Dll).
Typedef for fixed size vectors.
vctFixedSizeVector< double, 3 > vct3
Definition vctFixedSizeVectorTypes.h:46
Forward declarations and #define for cisstVector.