22 #ifndef _vctRodriguezRotation3Base_h
23 #define _vctRodriguezRotation3Base_h
41 template <
class _rodriguezType,
class _matrixType>
46 template <
class _rodriguezType,
class _matrixType>
68 template <
class _containerType>
86 cmnThrow(std::runtime_error(
"vctRodriguezRotation3Base: This rotation is not normalized"));
94 template <
class _inputType>
96 if (! input.IsNormalized()) {
97 cmnThrow(std::runtime_error(
"vctRodriguezRotation3Base: Input is not normalized"));
111 template <str
ide_type __str
ide,
class __dataPtrType>
125 template <str
ide_type __str
ide,
class __dataPtrType>
127 throw(std::runtime_error)
135 throw(std::runtime_error)
143 template <
class __vectorOwnerType>
145 throw(std::runtime_error)
153 template <
class __containerType>
155 throw(std::runtime_error)
158 return FromRaw(quaternionRotation);
162 template <
class __containerType>
165 return FromRaw(matrixRotation);
171 return FromRaw(axisAngleRotation);
176 template <str
ide_type __str
ide,
class __dataPtrType>
192 template <
class __vectorOwnerType>
194 throw(std::runtime_error)
201 template <
class __containerType>
208 template <
class __containerType>
222 template <str
ide_type __str
ide,
class __dataPtrType>
232 this->
Assign(static_cast<value_type>(x), static_cast<value_type>(y), static_cast<value_type>(z));
237 template <
class __vectorOwnerType>
240 if (vector.
size() != 3) {
241 cmnThrow(std::runtime_error(
"vctRodriguezRotation3Base: From requires a vector of size 3"));
248 template <
class __containerType>
255 template <
class __containerType>
264 this->Multiply(value_type(axisAngleRotation.
Angle()));
270 this->NegationSelf();
275 this->NegationOf(otherRotation);
281 result.NegationOf(*
this);
287 const NormType norm = this->Norm();
288 if (norm > (2.0 *
cmnPI)) {
289 const NormType remainder = fmod(norm, (2.0 *
cmnPI));
291 this->Multiply(value_type(remainder / norm));
298 this->
Assign(otherRotation);
306 result.NormalizedSelf();
334 const AngleType angleThis = this->Norm();
335 const AngleType angleOther = other.
Norm();
336 const AngleType dotProduct = AngleType(this->DotProduct(other));
338 if ((angleThis <= tolerance) && (angleOther <= tolerance)) {
342 if ((angleThis <= tolerance) || (angleOther <= tolerance)) {
346 const AngleType cosAngle = dotProduct / (angleThis * angleOther);
347 const AngleType absCosAngle = cosAngle > value_type(0.0) ? cosAngle : -cosAngle;
349 if ((absCosAngle <= (AngleType(1.0) - tolerance))
350 || (absCosAngle >= (AngleType(1.0) + tolerance))) {
354 const AngleType angleDifference = angleThis - cosAngle * angleOther;
355 AngleType angleRemain = fmod(angleDifference, 2.0 *
cmnPI);
356 if (angleRemain >
cmnPI) {
357 angleRemain -= (2.0 *
cmnPI);
358 }
else if (angleRemain < -
cmnPI) {
359 angleRemain += (2.0 *
cmnPI);
361 if ((angleRemain > -tolerance) && (angleRemain < tolerance)) {
371 template <
class _rodriguezType,
class _quaternionType>
376 typedef typename _rodriguezType::value_type value_type;
377 typedef typename _rodriguezType::AngleType AngleType;
378 typedef typename _rodriguezType::NormType NormType;
379 typedef typename _rodriguezType::TypeTraits TypeTraits;
381 const NormType r = quaternionRotation.
R();
382 const AngleType angle = acos(r) * 2;
383 AngleType oneMinusR2 = 1.0 - r * r;
384 if (oneMinusR2 < TypeTraits::Tolerance())
386 AngleType sinAngle = sqrt(oneMinusR2);
388 rodriguezRotation.X() = (value_type)(quaternionRotation.X() / sinAngle);
389 rodriguezRotation.Y() = (value_type)(quaternionRotation.Y() / sinAngle);
390 rodriguezRotation.Z() = (value_type)(quaternionRotation.Z() / sinAngle);
392 rodriguezRotation.X() = quaternionRotation.X();
393 rodriguezRotation.Y() = quaternionRotation.Y();
394 rodriguezRotation.Z() = quaternionRotation.Z();
396 rodriguezRotation.Multiply((value_type) angle);
400 template <
class _rodriguezType,
class _matrixType>
405 typedef typename _rodriguezType::value_type value_type;
406 typedef typename _rodriguezType::AngleType AngleType;
407 typedef typename _rodriguezType::NormType NormType;
408 typedef typename _rodriguezType::TypeTraits TypeTraits;
410 const NormType normTolerance = TypeTraits::Tolerance();
411 const NormType trace = matrixRotation.Element(0, 0) + matrixRotation.Element(1, 1) + matrixRotation.Element(2, 2);
412 const NormType xSin = matrixRotation.Element(2, 1) - matrixRotation.Element(1, 2);
413 const NormType ySin = matrixRotation.Element(0, 2) - matrixRotation.Element(2, 0);
414 const NormType zSin = matrixRotation.Element(1, 0) - matrixRotation.Element(0, 1);
415 const NormType normSquare = xSin * xSin + ySin * ySin + zSin * zSin;
417 if (normSquare < normTolerance) {
420 norm = sqrt(normSquare);
425 const NormType traceMinus3 = trace - NormType(3);
427 if ( (traceMinus3 > -normTolerance) && (traceMinus3 < normTolerance) ) {
428 rodriguezRotation.SetAll(value_type(0));
433 assert( (trace > (NormType(-1) - normTolerance)) && (trace < (NormType(-1) + normTolerance)) );
436 NormType xSquare = (matrixRotation.Element(0, 0) + 1) / 2;
437 NormType ySquare = (matrixRotation.Element(1, 1) + 1) / 2;
438 NormType zSquare = (matrixRotation.Element(2, 2) + 1) / 2;
439 if (xSquare < normTolerance)
441 if (ySquare < normTolerance)
443 if (zSquare < normTolerance)
445 NormType x = sqrt(xSquare);
446 NormType y = sqrt(ySquare);
447 NormType z = sqrt(zSquare);
450 if (matrixRotation.Element(1, 0) < 0)
452 if (matrixRotation.Element(2, 0) < 0)
455 if (matrixRotation.Element(2, 1) < 0)
461 rodriguezRotation.Assign(value_type(x), value_type(y), value_type(z));
462 rodriguezRotation.Multiply(value_type(
cmnPI));
466 AngleType angle = atan2(norm / 2, (trace - 1) / 2);
467 rodriguezRotation.Assign(value_type(xSin), value_type(ySin), value_type(zSin));
468 rodriguezRotation.Multiply(value_type(angle / norm));
471 #endif // _vctRodriguezRotation3Base_h
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
bool AlmostEquivalent(const ThisType &other, value_type tolerance=TypeTraits::Tolerance()) const
Definition: vctRodriguezRotation3Base.h:332
ThisType & FromRaw(const vctDynamicConstVectorBase< __vectorOwnerType, value_type > &vector)
Definition: vctRodriguezRotation3Base.h:238
const AxisType & Axis(void) const
Definition: vctAxisAngleRotation3.h:314
NormType Norm(void) const
Definition: vctFixedSizeConstVectorBase.h:453
ThisType & From(const vctQuaternionRotation3Base< __containerType > &quaternionRotation)
Definition: vctRodriguezRotation3Base.h:154
RotationValueType Normalized(void) const
Definition: vctMatrixRotation3.h:389
ThisType & FromRaw(const vctFixedSizeConstVectorBase< 3, __stride, value_type, __dataPtrType > &vector)
Definition: vctRodriguezRotation3Base.h:223
ThisType & FromNormalized(value_type x, value_type y, value_type z)
Definition: vctRodriguezRotation3Base.h:184
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
_containerType ContainerType
Definition: vctRodriguezRotation3Base.h:74
static Type Tolerance(void)
Definition: cmnTypeTraits.h:170
ThisType & FromNormalized(const vctQuaternionRotation3Base< __containerType > &quaternionRotation)
Definition: vctRodriguezRotation3Base.h:202
Forward declarations and #define for cisstVector.
ThisType & From(const vctMatrixRotation3Base< __containerType > &matrixRotation)
Definition: vctRodriguezRotation3Base.h:163
ThisType & NormalizedOf(const ThisType &otherRotation)
Definition: vctRodriguezRotation3Base.h:297
ThisType & FromNormalized(const vctMatrixRotation3Base< __containerType > &matrixRotation)
Definition: vctRodriguezRotation3Base.h:209
static CISST_EXPORT const ThisType & Identity()
vctRodriguezRotation3Base(const vctFixedSizeConstVectorBase< DIMENSION, __stride, value_type, __dataPtrType > &axis)
Definition: vctRodriguezRotation3Base.h:112
Definition: vctRodriguezRotation3Base.h:72
void ThrowUnlessIsNormalized(const _inputType &input) const
Definition: vctRodriguezRotation3Base.h:95
ThisType & InverseSelf(void)
Definition: vctRodriguezRotation3Base.h:269
vctRodriguezRotation3Base()
Definition: vctRodriguezRotation3Base.h:104
Declaration of various constants.
ThisType & NormalizedSelf(void)
Definition: vctRodriguezRotation3Base.h:286
ThisType Normalized(void) const
Definition: vctQuaternionRotation3Base.h:529
Define a rotation based on an axis and an angle for a space of dimension 3.
Definition: vctAxisAngleRotation3.h:94
ThisType & From(const vctFixedSizeConstVectorBase< 3, __stride, value_type, __dataPtrType > &vector)
Definition: vctRodriguezRotation3Base.h:126
Define unary operations on an object as classes.
Definition: vctUnaryOperations.h:55
ThisType & FromRaw(value_type x, value_type y, value_type z)
Definition: vctRodriguezRotation3Base.h:230
size_type size(void) const
Definition: vctDynamicConstVectorBase.h:164
ThisType & InverseOf(const ThisType &otherRotation)
Definition: vctRodriguezRotation3Base.h:274
ThisType & From(value_type x, value_type y, value_type z)
Definition: vctRodriguezRotation3Base.h:134
Define a rotation matrix for a space of dimension 3.
Definition: vctForwardDeclarations.h:199
ThisType & FromNormalized(const vctAxisAngleRotation3< value_type > &axisAngleRotation)
Definition: vctRodriguezRotation3Base.h:214
ThisType Normalized(void) const
Definition: vctAxisAngleRotation3.h:542
Declaration of vctFixedSizeVector.
void ThrowUnlessIsNormalized(void) const
Definition: vctRodriguezRotation3Base.h:84
void Assign(const OtherT &other)
Definition: cisstVectorEigenAddons.h:3
ThisType Normalized(void) const
Definition: vctRodriguezRotation3Base.h:304
ThisType & FromRaw(const vctQuaternionRotation3Base< __containerType > &quaternionRotation)
Definition: vctRodriguezRotation3Base.h:249
void vctRodriguezRotation3BaseFromRaw(vctRodriguezRotation3Base< _rodriguezType > &rodriguezRotation, const vctMatrixRotation3Base< _matrixType > &matrixRotation)
Definition: vctRodriguezRotation3Base.h:402
#define cmnThrow(a)
Definition: MinimalCmn.h:4
const AngleType & Angle(void) const
Definition: vctAxisAngleRotation3.h:324
const_reference R(void) const
Definition: vctQuaternionBase.h:101
ThisType & From(const vctDynamicConstVectorBase< __vectorOwnerType, value_type > &vector)
Definition: vctRodriguezRotation3Base.h:144
A template for a fixed length vector with fixed spacing in memory.
Definition: vctFixedSizeVectorBase.h:76
VCT_CONTAINER_TRAITS_TYPEDEFS(typename ContainerType::value_type)
ThisType & FromNormalized(const vctDynamicConstVectorBase< __vectorOwnerType, value_type > &vector)
Definition: vctRodriguezRotation3Base.h:193
ThisType & From(const vctAxisAngleRotation3< value_type > &axisAngleRotation)
Definition: vctRodriguezRotation3Base.h:169
bool IsNormalized(value_type CMN_UNUSED(tolerance)=TypeTraits::Tolerance()) const
Definition: vctRodriguezRotation3Base.h:319
Definition: vctDynamicConstVectorBase.h:77
A template for a fixed length vector with fixed spacing in memory.
Definition: vctFixedSizeConstVectorBase.h:107
ThisType & FromRaw(const vctAxisAngleRotation3< value_type > &axisAngleRotation)
Definition: vctRodriguezRotation3Base.h:262
_containerType BaseType
Definition: vctRodriguezRotation3Base.h:73
Define a rotation quaternion for a space of dimension 3.
Definition: vctForwardDeclarations.h:208
Macros to export the symbols of cisstVector (in a Dll).
A collection of useful information about the C++ basic types, represented in a generic programming wa...
Definition: cmnTypeTraits.h:155
vctRodriguezRotation3Base(value_type x, value_type y, value_type z)
Definition: vctRodriguezRotation3Base.h:117
ThisType & FromNormalized(const vctFixedSizeConstVectorBase< 3, __stride, value_type, __dataPtrType > &vector)
Definition: vctRodriguezRotation3Base.h:177
ThisType & FromRaw(const vctMatrixRotation3Base< __containerType > &matrixRotation)
Definition: vctRodriguezRotation3Base.h:256
const double cmnPI
Definition: cmnConstants.h:38
vctRodriguezRotation3Base< ContainerType > ThisType
Definition: vctRodriguezRotation3Base.h:75
Define a rotation based on the rodriguez representation for a space of dimension 3.
Definition: vctForwardDeclarations.h:214
ThisType Inverse(void) const
Definition: vctRodriguezRotation3Base.h:279
cmnTypeTraits< value_type > TypeTraits
Definition: vctRodriguezRotation3Base.h:79