cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Anton Deguet
6 Created on: 2005-01-13
7
8 (C) Copyright 2005-2018 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19#pragma once
20#ifndef _vctAxisAngleRotation3_h
21#define _vctAxisAngleRotation3_h
22
27
29
36
37#ifndef DOXYGEN
38#ifndef SWIG
39
40template <class _elementType, class _containerType>
41void
43 const vctQuaternionRotation3Base<_containerType> & quaternionRotation);
44
45template <class _elementType, class _containerType>
46void
48 const vctMatrixRotation3Base<_containerType> & matrixRotation);
49
50template <class _elementType, class _containerType>
51void
53 const vctRodriguezRotation3Base<_containerType> & rodriguezRotation);
54
55#ifdef CISST_COMPILER_IS_MSVC
56template CISST_EXPORT void
59template CISST_EXPORT void
61 const vctQuaternionRotation3Base<vctFixedSizeVector<float, 4> > & quaternionRotation);
62template CISST_EXPORT void
65template CISST_EXPORT void
68template CISST_EXPORT void
71template CISST_EXPORT void
74#endif
75#endif // SWIG
76#endif // DOXYGEN
77
78
91template <class _elementType>
93{
94public:
95 /* no need to document, inherit doxygen documentation from vctFixedSizeVectorBase */
97 enum {DIMENSION = 3};
101
102protected:
104 AngleType AngleMember;
105
106
108 inline void ThrowUnlessIsNormalized(void) const CISST_THROW(std::runtime_error) {
109 if (! IsNormalized()) {
110 cmnThrow(std::runtime_error("vctAxisAngleRotation3: This rotation is not normalized"));
111 }
112 }
113
118 template <class _inputType>
119 inline void ThrowUnlessIsNormalized(const _inputType & input) const CISST_THROW(std::runtime_error) {
120 if (! input.IsNormalized()) {
121 cmnThrow(std::runtime_error("vctAxisAngleRotation3: Input is not normalized"));
122 }
123 }
124
125public:
126
129 AxisMember(1.0, 0.0, 0.0),
130 AngleMember(0.0)
131 {}
132
133
135 inline ThisType & Assign(const ThisType & other) {
136 AxisMember.Assign(other.Axis());
137 AngleMember = other.Angle();
138 return *this;
139 }
140
141
155
156
161 template <stride_type __stride, class __dataPtrType>
163 const AngleType angle)
164 CISST_THROW(std::runtime_error)
165 {
166 From(axis, angle);
167 }
168
173 template <class _vectorOwnerType>
175 AngleType angle)
176 CISST_THROW(std::runtime_error)
177 {
178 From(axis, angle);
179 }
180
182 template <class _containerType>
183 explicit inline
185 CISST_THROW(std::runtime_error)
186 {
187 From(quaternionRotation);
188 }
189
191 template <class _containerType>
192 explicit inline
194 CISST_THROW(std::runtime_error)
195 {
196 From(matrixRotation);
197 }
198
200 template <class _containerType>
201 explicit inline
203 CISST_THROW(std::runtime_error)
204 {
205 From(rodriguezRotation);
206 }
207
209
210
232
238 template <stride_type __stride, class __dataPtrType>
240 const AngleType angle,
241 bool normalizeInput)
242 {
243 if (normalizeInput) {
244 FromNormalized(axis, angle);
245 } else {
246 FromRaw(axis, angle);
247 }
248 }
249
255 template <class _vectorOwnerType>
257 AngleType angle,
258 bool normalizeInput)
259 {
260 if (normalizeInput) {
261 FromNormalized(axis, angle);
262 } else {
263 FromRaw(axis, angle);
264 }
265 }
266
268 template <class _containerType>
270 bool normalizeInput)
271 {
272 if (normalizeInput) {
273 FromNormalized(quaternionRotation);
274 } else {
275 FromRaw(quaternionRotation);
276 }
277 }
278
280 template <class _containerType>
282 bool normalizeInput)
283 {
284 if (normalizeInput) {
285 FromNormalized(matrixRotation);
286 } else {
287 FromRaw(matrixRotation);
288 }
289 }
290
292 template <class _containerType>
294 bool normalizeInput)
295 {
296 if (normalizeInput) {
297 FromNormalized(rodriguezRotation);
298 } else {
299 FromRaw(rodriguezRotation);
300 }
301 }
302
304
305
306
310
312 inline const AxisType & Axis(void) const {
313 return AxisMember;
314 }
315
317 inline AxisType & Axis(void) {
318 return AxisMember;
319 }
320
322 inline const AngleType & Angle(void) const {
323 return AngleMember;
324 }
325
327 inline AngleType & Angle(void) {
328 return AngleMember;
329 }
330
331
345
347 template <stride_type __stride, class __dataPtrType>
349 const AngleType angle)
350 CISST_THROW(std::runtime_error)
351 {
352 FromRaw(axis, angle);
354 return *this;
355 }
356
358 template <class _vectorOwnerType>
360 AngleType angle)
361 CISST_THROW(std::runtime_error)
362 {
363 AxisMember[0] = axis[0];
364 AxisMember[1] = axis[1];
365 AxisMember[2] = axis[2];
366 AngleMember = angle;
368 return *this;
369 }
370
372 template <class _containerType>
373 inline ThisType & From(const vctQuaternionRotation3Base<_containerType> & quaternionRotation)
374 CISST_THROW(std::runtime_error)
375 {
376 ThrowUnlessIsNormalized(quaternionRotation);
377 return FromRaw(quaternionRotation);
378 }
379
381 template <class _containerType>
382 inline ThisType & From(const vctMatrixRotation3Base<_containerType> & matrixRotation)
383 CISST_THROW(std::runtime_error)
384 {
385 ThrowUnlessIsNormalized(matrixRotation);
386 return FromRaw(matrixRotation);
387 }
388
390 template <class _containerType>
391 inline ThisType & From(const vctRodriguezRotation3Base<_containerType> & rodriguezRotation)
392 CISST_THROW(std::runtime_error)
393 {
394 ThrowUnlessIsNormalized(rodriguezRotation);
395 return FromRaw(rodriguezRotation);
396 }
397
398
399
400
401
411
413 template <stride_type __stride, class __dataPtrType>
415 const AngleType angle)
416 {
417 FromRaw(axis, angle);
419 return *this;
420 }
421
423 template <class _containerType>
425 return FromRaw(quaternionRotation.Normalized());
426 }
427
429 template <class _containerType>
431 return FromRaw(matrixRotation.Normalized());
432 }
433
435 template <class _containerType>
437 return FromRaw(rodriguezRotation.Normalized());
438 }
439
440
441
442
452
454 template <stride_type __stride, class __dataPtrType>
456 const AngleType angle)
457 {
458 AxisMember.Assign(axis);
459 AngleMember = angle;
460 return *this;
461 }
462
464 template <class _containerType>
465 inline ThisType & FromRaw(const vctQuaternionRotation3Base<_containerType> & quaternionRotation) {
466 vctAxisAngleRotation3FromRaw(*this, quaternionRotation);
467 return *this;
468 }
469
471 template <class _containerType>
472 inline ThisType & FromRaw(const vctMatrixRotation3Base<_containerType> & matrixRotation) {
473 vctAxisAngleRotation3FromRaw(*this, matrixRotation);
474 return *this;
475 }
476
478 template <class _containerType>
479 inline ThisType & FromRaw(const vctRodriguezRotation3Base<_containerType> & rodriguezRotation) {
480 vctAxisAngleRotation3FromRaw(*this, rodriguezRotation);
481 return *this;
482 }
483
485
486
491 inline ThisType & InverseSelf(void) {
492 AngleMember = 2.0 * cmnPI - AngleMember;
493 return *this;
494 }
495
498 inline ThisType & InverseOf(const ThisType & otherRotation) {
499 AxisMember.Assign(otherRotation.Axis());
500 AngleMember = 2.0 * cmnPI - otherRotation.Angle();
501 return *this;
502 }
503
507 inline ThisType Inverse(void) const {
508 ThisType result;
509 result.Angle() = 2 * cmnPI - this->Angle();
510 return result;
511 }
512
516 inline ThisType & NormalizedSelf(void) {
518 AngleMember = fmod(AngleMember, 2.0 * cmnPI);
519 if (AngleMember < 0.0) {
520 AngleMember += (2.0 * cmnPI);
521 }
522 return *this;
523 }
524
530 inline ThisType & NormalizedOf(const ThisType & otherRotation) {
531 AxisMember.Assign(otherRotation.Axis());
532 AngleMember = otherRotation.Angle();
534 return *this;
535 }
536
540 inline ThisType Normalized(void) const {
541 ThisType result(*this);
542 result.NormalizedSelf();
543 return result;
544 }
545
546
552 inline bool IsNormalized(value_type tolerance = TypeTraits::Tolerance()) const {
553 return AxisMember.IsNormalized(tolerance);
554 }
555
556
564 inline bool Equal(const ThisType & other) const {
565 return ((this->Angle() == other.Angle()) && (this->Axis().Equal(other.Axis())));
566 }
567
568 inline bool operator==(const ThisType & other) const {
569 return this->Equal(other);
570 }
571
572
573
583 inline bool AlmostEqual(const ThisType & other,
584 value_type tolerance = TypeTraits::Tolerance()) const {
585 const AngleType angleDiff = this->Angle() - other.Angle();
586 const AngleType angle = (angleDiff < 0) ? -angleDiff : angleDiff;
587
588 return ((angle <= tolerance) && (this->Axis().AlmostEqual(other.Axis(), tolerance)));
589 }
590
591
603 inline bool AlmostEquivalent(const ThisType & other,
604 value_type tolerance = TypeTraits::Tolerance()) const {
605 const AngleType angleThis = this->Angle();
606 const AngleType angleOther = other.Angle();
607 // two quasi null rotations
608 if ((angleThis <= tolerance) && (angleOther <= tolerance)) {
609 return true;
610 }
611 // one rotation is almost null
612 if ((angleThis <= tolerance) || (angleOther <= tolerance)) {
613 return false;
614 }
615
616 // both rotations are non null
617 const AngleType dotProduct = AngleType(this->Axis().Normalized().DotProduct(other.Axis().Normalized()));
618 const AngleType absDotProduct = dotProduct > value_type(0.0) ? dotProduct : -dotProduct;
619 // axis don't seem aligned
620 if ((absDotProduct <= (AngleType(1.0) - tolerance))
621 || (absDotProduct >= (AngleType(1.0) + tolerance))) {
622 return false;
623 }
624 // axis are aligned
625 const AngleType angleDifference = angleThis - dotProduct * angleOther;
626 AngleType angleRemain = fmod(angleDifference, 2.0 * cmnPI);
627 if (angleRemain > cmnPI) {
628 angleRemain -= (2.0 * cmnPI);
629 } else if (angleRemain < -cmnPI) {
630 angleRemain += (2.0 *cmnPI);
631 }
632 if ((angleRemain > -tolerance) && (angleRemain < tolerance)) {
633 return true;
634 }
635 return false;
636 }
637
638
639 std::string ToString(void) const {
640 std::stringstream outputStream;
641 ToStream(outputStream);
642 return outputStream.str();
643 }
644
646 void ToStream(std::ostream & outputStream) const {
647 outputStream << "axis: "
648 << std::endl
649 << this->Axis()
650 << std::endl
651 << "angle: "
652 << std::endl
653 << this->Angle();
654 }
655
657 void ToStreamRaw(std::ostream & outputStream, const char delimiter = ' ',
658 bool headerOnly = false, const std::string & headerPrefix = "") const {
659 this->Axis().ToStreamRaw(outputStream, delimiter, headerOnly, headerPrefix + "axis-");
660 outputStream << delimiter;
661 if (headerOnly) {
662 outputStream << headerPrefix << "angle";
663 } else {
664 outputStream << this->Angle();
665 }
666 }
667
669 void SerializeRaw(std::ostream & outputStream) const
670 {
671 this->Axis().SerializeRaw(outputStream);
672 cmnSerializeRaw(outputStream, this->Angle());
673 }
674
676 void DeSerializeRaw(std::istream & inputStream)
677 {
678 this->Axis().DeSerializeRaw(inputStream);
679 cmnDeSerializeRaw(inputStream, this->Angle());
680 }
681
682};
683
684
686template<class _elementType>
687std::ostream & operator << (std::ostream & output,
688 const vctAxisAngleRotation3<_elementType> & axisAngle) {
689 axisAngle.ToStream(output);
690 return output;
691}
692
693
694#endif // _vctAxisAngleRotation3_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
_elementType value_type
Definition mtsGenericObjectProxy.h:329
Define a rotation based on an axis and an angle for a space of dimension 3.
Definition vctAxisAngleRotation3.h:93
ThisType & FromRaw(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle)
Definition vctAxisAngleRotation3.h:455
vctAxisAngleRotation3(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle, bool normalizeInput)
Definition vctAxisAngleRotation3.h:239
void DeSerializeRaw(std::istream &inputStream)
Definition vctAxisAngleRotation3.h:676
vctAxisAngleRotation3(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation, bool normalizeInput)
Definition vctAxisAngleRotation3.h:293
ThisType & NormalizedSelf(void)
Definition vctAxisAngleRotation3.h:516
ThisType & FromRaw(const vctQuaternionRotation3Base< _containerType > &quaternionRotation)
Definition vctAxisAngleRotation3.h:465
void ThrowUnlessIsNormalized(const _inputType &input) const CISST_THROW(std
Definition vctAxisAngleRotation3.h:119
vctAxisAngleRotation3()
Definition vctAxisAngleRotation3.h:128
ThisType Inverse(void) const
Definition vctAxisAngleRotation3.h:507
vctFixedSizeVector< value_type, DIMENSION > AxisType
Definition vctAxisAngleRotation3.h:99
ThisType & FromNormalized(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle)
Definition vctAxisAngleRotation3.h:414
vctAxisAngleRotation3(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation) CISST_THROW(std
Definition vctAxisAngleRotation3.h:202
ThisType & From(const vctDynamicConstVectorBase< _vectorOwnerType, value_type > &axis, AngleType angle) CISST_THROW(std
Definition vctAxisAngleRotation3.h:359
AngleType & Angle(void)
Definition vctAxisAngleRotation3.h:327
vctAxisAngleRotation3(const vctQuaternionRotation3Base< _containerType > &quaternionRotation, bool normalizeInput)
Definition vctAxisAngleRotation3.h:269
ThisType & From(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation) CISST_THROW(std
Definition vctAxisAngleRotation3.h:391
void ThrowUnlessIsNormalized(void) const CISST_THROW(std
Definition vctAxisAngleRotation3.h:108
void ToStream(std::ostream &outputStream) const
Definition vctAxisAngleRotation3.h:646
bool IsNormalized(value_type tolerance=TypeTraits::Tolerance()) const
Definition vctAxisAngleRotation3.h:552
bool Equal(const ThisType &other) const
Definition vctAxisAngleRotation3.h:564
bool AlmostEqual(const ThisType &other, value_type tolerance=TypeTraits::Tolerance()) const
Definition vctAxisAngleRotation3.h:583
VCT_CONTAINER_TRAITS_TYPEDEFS(_elementType)
std::string ToString(void) const
Definition vctAxisAngleRotation3.h:639
ThisType & FromRaw(const vctMatrixRotation3Base< _containerType > &matrixRotation)
Definition vctAxisAngleRotation3.h:472
vctAxisAngleRotation3(const vctMatrixRotation3Base< _containerType > &matrixRotation) CISST_THROW(std
Definition vctAxisAngleRotation3.h:193
cmnTypeTraits< value_type > TypeTraits
Definition vctAxisAngleRotation3.h:100
ThisType & From(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle) CISST_THROW(std
Definition vctAxisAngleRotation3.h:348
bool AlmostEquivalent(const ThisType &other, value_type tolerance=TypeTraits::Tolerance()) const
Definition vctAxisAngleRotation3.h:603
static CISST_EXPORT const ThisType & Identity()
vctAxisAngleRotation3(const vctQuaternionRotation3Base< _containerType > &quaternionRotation) CISST_THROW(std
Definition vctAxisAngleRotation3.h:184
ThisType & FromRaw(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation)
Definition vctAxisAngleRotation3.h:479
const AxisType & Axis(void) const
Definition vctAxisAngleRotation3.h:312
vctAxisAngleRotation3(const vctMatrixRotation3Base< _containerType > &matrixRotation, bool normalizeInput)
Definition vctAxisAngleRotation3.h:281
ThisType & From(const vctMatrixRotation3Base< _containerType > &matrixRotation) CISST_THROW(std
Definition vctAxisAngleRotation3.h:382
AngleType AngleMember
Definition vctAxisAngleRotation3.h:104
ThisType Normalized(void) const
Definition vctAxisAngleRotation3.h:540
const AngleType & Angle(void) const
Definition vctAxisAngleRotation3.h:322
vctAxisAngleRotation3(const vctDynamicConstVectorBase< _vectorOwnerType, value_type > &axis, AngleType angle) CISST_THROW(std
Definition vctAxisAngleRotation3.h:174
vctAxisAngleRotation3(const vctDynamicConstVectorBase< _vectorOwnerType, value_type > &axis, AngleType angle, bool normalizeInput)
Definition vctAxisAngleRotation3.h:256
ThisType & InverseSelf(void)
Definition vctAxisAngleRotation3.h:491
vctAxisAngleRotation3(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis, const AngleType angle) CISST_THROW(std
Definition vctAxisAngleRotation3.h:162
ThisType & FromNormalized(const vctMatrixRotation3Base< _containerType > &matrixRotation)
Definition vctAxisAngleRotation3.h:430
bool operator==(const ThisType &other) const
Definition vctAxisAngleRotation3.h:568
ThisType & FromNormalized(const vctRodriguezRotation3Base< _containerType > &rodriguezRotation)
Definition vctAxisAngleRotation3.h:436
AxisType & Axis(void)
Definition vctAxisAngleRotation3.h:317
ThisType & NormalizedOf(const ThisType &otherRotation)
Definition vctAxisAngleRotation3.h:530
ThisType & InverseOf(const ThisType &otherRotation)
Definition vctAxisAngleRotation3.h:498
void SerializeRaw(std::ostream &outputStream) const
Definition vctAxisAngleRotation3.h:669
ThisType & FromNormalized(const vctQuaternionRotation3Base< _containerType > &quaternionRotation)
Definition vctAxisAngleRotation3.h:424
@ DIMENSION
Definition vctAxisAngleRotation3.h:97
AxisType AxisMember
Definition vctAxisAngleRotation3.h:103
ThisType & From(const vctQuaternionRotation3Base< _containerType > &quaternionRotation) CISST_THROW(std
Definition vctAxisAngleRotation3.h:373
vctAxisAngleRotation3< value_type > ThisType
Definition vctAxisAngleRotation3.h:98
ThisType & Assign(const ThisType &other)
Definition vctAxisAngleRotation3.h:135
void ToStreamRaw(std::ostream &outputStream, const char delimiter=' ', bool headerOnly=false, const std::string &headerPrefix="") const
Definition vctAxisAngleRotation3.h:657
Definition vctForwardDeclarations.h:119
A template for a fixed length vector with fixed spacing in memory.
Definition vctFixedSizeConstVectorBase.h:108
bool IsNormalized(_elementType tolerance=cmnTypeTraits< _elementType >::Tolerance()) const
Definition vctFixedSizeConstVectorBase.h:461
Implementation of a fixed-size matrix using template metaprogramming.
Definition vctFixedSizeMatrix.h:54
ThisType & Assign(const vctFixedSizeConstVectorBase< _size, __stride, __elementType, __dataPtrType > &other)
Definition vctFixedSizeVectorBase.h:274
ThisType & NormalizedSelf(void) CISST_THROW(std
Definition vctFixedSizeVectorBase.h:1363
Implementation of a fixed-size vector using template metaprogramming.
Definition vctFixedSizeVector.h:54
Define a rotation matrix for a space of dimension 3.
Definition vctMatrixRotation3Base.h:71
RotationValueType Normalized(void) const
Definition vctMatrixRotation3.h:394
Define a rotation quaternion for a space of dimension 3.
Definition vctQuaternionRotation3Base.h:64
ThisType Normalized(void) const
Definition vctQuaternionRotation3Base.h:532
Define a rotation based on the rodriguez representation for a space of dimension 3.
Definition vctRodriguezRotation3Base.h:68
ThisType Normalized(void) const
Definition vctRodriguezRotation3Base.h:302
Declaration of various constants.
const double cmnPI
Definition cmnConstants.h:38
void cmnDeSerializeRaw(std::istream &inputStream, _elementType &data) CISST_THROW(std
Definition cmnDeSerializer.h:82
#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 cmnSerializeRaw(std::ostream &outputStream, const _elementType &data) CISST_THROW(std
Definition cmnSerializer.h:76
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
void vctAxisAngleRotation3FromRaw(vctAxisAngleRotation3< _elementType > &axisAngleRotation, const vctQuaternionRotation3Base< _containerType > &quaternionRotation)
std::ostream & operator<<(std::ostream &output, const vctAxisAngleRotation3< _elementType > &axisAngle)
Definition vctAxisAngleRotation3.h:687
value_type DotProduct(const vctDynamicConstVectorBase< __vectorOwnerType, _elementType > &otherVector) const
Definition vctDynamicConstVectorBase.h:633
Declaration of vctDynamicVector.
Macros to export the symbols of cisstVector (in a Dll).
Declaration of vctFixedSizeVector.
Declaration of vctMatrixRotation3Base.
Declaration of vctQuaternionRotation3Base.
Declaration of vctRodriguezRotation3Base.