8 #ifndef META_OCEAN_MATH_QUATERNION_H
9 #define META_OCEAN_MATH_QUATERNION_H
21 template <
typename T>
class RotationT;
24 template <
typename T>
class EulerT;
27 template <
typename T>
class SquareMatrixT3;
30 template <
typename T>
class HomogenousMatrixT4;
33 template <
typename T>
class QuaternionT;
126 template <
typename U>
327 inline const T&
w()
const;
339 inline const T&
x()
const;
351 inline const T&
y()
const;
363 inline const T&
z()
const;
478 template <
typename T>
488 ocean_assert(isValid() ==
true);
497 ocean_assert(isValid() ==
false);
501 template <
typename T>
502 template <
typename U>
505 values_[0] = T(quaternion[0]);
506 values_[1] = T(quaternion[1]);
507 values_[2] = T(quaternion[2]);
508 values_[3] = T(quaternion[3]);
511 template <
typename T>
520 template <
typename T>
525 const T angleValue = angle * T(0.5);
529 values_[1] = sinValue * axis[0];
530 values_[2] = sinValue * axis[1];
531 values_[3] = sinValue * axis[2];
533 ocean_assert_accuracy(isValid());
536 template <
typename T>
542 if (reference == offset)
549 else if (reference == -offset)
554 values_[1] = perpendicular[0];
555 values_[2] = perpendicular[1];
556 values_[3] = perpendicular[2];
562 values_[0] = T(1) + reference * offset;
563 values_[1] = axis.
x();
564 values_[2] = axis.
y();
565 values_[3] = axis.
z();
570 ocean_assert(isValid());
574 template <
typename T>
577 ocean_assert(rotation.
isValid());
579 const T angle = rotation.
angle() * T(0.5);
583 values_[1] = sinValue * rotation[0];
584 values_[2] = sinValue * rotation[1];
585 values_[3] = sinValue * rotation[2];
587 ocean_assert(isValid());
590 template <
typename T>
595 const T roll = euler.
roll() * T(0.5);
596 const T pitch = euler.
pitch() * T(0.5);
597 const T yaw = euler.
yaw() * T(0.5);
608 const T cc = cosRoll * cosYaw;
609 const T cs = cosRoll * sinYaw;
611 const T sc = sinRoll * cosYaw;
612 const T ss = sinRoll * sinYaw;
614 values_[0] = cosPitch * cc + sinPitch * ss;
615 values_[1] = cosPitch * ss + sinPitch * cc;
616 values_[2] = cosPitch * cs - sinPitch * sc;
617 values_[3] = cosPitch * sc - sinPitch * cs;
620 ocean_assert(isValid());
623 template <
typename T>
627 const T trace = matrix.
trace() + T(1.0);
634 const T factor = T(0.25) / values_[0];
636 values_[1] = (matrix(2, 1) - matrix(1, 2)) * factor;
637 values_[2] = (matrix(0, 2) - matrix(2, 0)) * factor;
638 values_[3] = (matrix(1, 0) - matrix(0, 1)) * factor;
642 if (matrix(0, 0) > matrix(1, 1) && matrix(0, 0) > matrix(2, 2))
644 values_[1] = T(0.5) *
NumericT<T>::sqrt(matrix(0, 0) - matrix(1, 1) - matrix(2, 2) + T(1.0));
646 const T factor = T(0.25) / values_[1];
648 values_[0] = (matrix(2, 1) - matrix(1, 2)) * factor;
649 values_[2] = (matrix(0, 1) + matrix(1, 0)) * factor;
650 values_[3] = (matrix(0, 2) + matrix(2, 0)) * factor;
652 else if (matrix(1, 1) > matrix(2, 2))
654 values_[2] = T(0.5) *
NumericT<T>::sqrt(matrix(1, 1) - matrix(0, 0) - matrix(2, 2) + T(1.0));
656 const T factor = T(0.25) / values_[2];
658 values_[0] = (matrix(0, 2) - matrix(2, 0)) * factor;
659 values_[1] = (matrix(0, 1) + matrix(1, 0)) * factor;
660 values_[3] = (matrix(1, 2) + matrix(2, 1)) * factor;
664 values_[3] = T(0.5) *
NumericT<T>::sqrt(matrix(2, 2) - matrix(0, 0) - matrix(1, 1) + T(1.0));
666 const T factor = T(0.25) / values_[3];
668 values_[0] = (matrix(1, 0) - matrix(0, 1)) * factor;
669 values_[1] = (matrix(0, 2) + matrix(2, 0)) * factor;
670 values_[2] = (matrix(1, 2) + matrix(2, 1)) * factor;
675 ocean_assert(isValid());
678 template <
typename T>
682 ocean_assert(isValid());
685 template <
typename T>
688 memcpy(values_, vector(),
sizeof(T) * 4);
691 template <
typename T>
694 ocean_assert(arrayValue);
695 memcpy(values_, arrayValue,
sizeof(T) * 4);
698 template <
typename T>
701 const T normValue = norm();
708 const T factor = T(1.0) / normValue;
710 return QuaternionT<T>(values_[0] * factor, values_[1] * factor, values_[2] * factor, values_[3] * factor);
713 template <
typename T>
716 const T normValue = norm();
723 const T factor = T(1.0) / normValue;
725 values_[0] *= factor;
726 values_[1] *= factor;
727 values_[2] *= factor;
728 values_[3] *= factor;
733 template <
typename T>
736 const T normValue = norm();
743 const T factor = T(1.0) / normValue;
745 normalizedQuaternion.
values_[0] = values_[0] * factor;
746 normalizedQuaternion.
values_[1] = values_[1] * factor;
747 normalizedQuaternion.
values_[2] = values_[2] * factor;
748 normalizedQuaternion.
values_[3] = values_[3] * factor;
753 template <
typename T>
756 const T square =
sqr();
763 const T factor = T(1) / square;
765 return QuaternionT<T>(values_[0] * factor, -values_[1] * factor, -values_[2] * factor, -values_[3] * factor);
768 template <
typename T>
771 const T square =
sqr();
778 const T factor = T(1.0) / square;
780 values_[0] *= factor;
781 values_[1] *= -factor;
782 values_[2] *= -factor;
783 values_[3] *= -factor;
788 template <
typename T>
791 const T square =
sqr();
798 const T factor = T(1.0) / square;
800 invertedQuaternion.
values_[0] = values_[0] * factor;
801 invertedQuaternion.
values_[1] = values_[1] * -factor;
802 invertedQuaternion.
values_[2] = values_[2] * -factor;
803 invertedQuaternion.
values_[3] = values_[3] * -factor;
808 template <
typename T>
811 return QuaternionT<T>(values_[0], -values_[1], -values_[2], -values_[3]);
814 template <
typename T>
817 return NumericT<T>::sqrt(values_[0] * values_[0] + values_[1] * values_[1] + values_[2] * values_[2] + values_[3] * values_[3]);
820 template <
typename T>
823 return values_[0] * values_[0] + values_[1] * values_[1] + values_[2] * values_[2] + values_[3] * values_[3];
826 template <
typename T>
829 return values_[0] * quaternion.
values_[0] + values_[1] * quaternion.
values_[1] + values_[2] * quaternion.
values_[2] + values_[3] * quaternion.
values_[3];
832 template <
typename T>
835 ocean_assert(isValid());
840 template <
typename T>
843 ocean_assert(isValid() && quaternion.
isValid());
845 return (inverted() * quaternion).angle();
848 template <
typename T>
851 ocean_assert(isValid() && quaternion.
isValid());
856 template <
typename T>
859 ocean_assert(isValid() && quaternion.
isValid());
861 return (inverted() * quaternion).w();
864 template <
typename T>
867 ocean_assert(factor >= 0 && factor <= T(1.0));
868 ocean_assert(isValid() && quaternion.
isValid());
870 T sigma =
minmax(T(-1), w() * quaternion.
w() + x() * quaternion.
x() + y() * quaternion.
y() + z() * quaternion.
z(), T(1));
877 adjustedQuaternion =
QuaternionT<T>(-quaternion.
w(), -quaternion.
x(), -quaternion.
y(), -quaternion.
z());
882 T factorA = T(1.0) - factor;
895 return QuaternionT<T>(factorA * w() + factorB * adjustedQuaternion.
w(), factorA * x() + factorB * adjustedQuaternion.
x(), factorA * y() + factorB * adjustedQuaternion.
y(), factorA * z() + factorB * adjustedQuaternion.
z()).
normalized();
898 template <
typename T>
904 template <
typename T>
910 template <
typename T>
916 template <
typename T>
922 template <
typename T>
928 template <
typename T>
934 template <
typename T>
940 template <
typename T>
946 template <
typename T>
952 template <
typename T>
959 template <
typename T>
965 template <
typename T>
974 template <
typename T>
977 ocean_assert(isValid());
979 const QuaternionT<T> quaternion(0, vector[0], vector[1], vector[2]);
985 template <
typename T>
988 return !(*
this == right);
991 template <
typename T>
997 template <
typename T>
1000 *
this = *
this * right;
1004 template <
typename T>
1011 template <
typename T>
1014 ocean_assert(index < 4u);
1015 return values_[index];
1018 template <
typename T>
1021 ocean_assert(index < 4u);
1022 return values_[index];
1025 template <
typename T>
1028 ocean_assert(index < 4u);
1029 return values_[index];
1032 template <
typename T>
1035 ocean_assert(index < 4u);
1036 return values_[index];
1039 template <
typename T>
1045 template <
typename T>
1051 template <
typename T>
1054 stream <<
"[" << quaternion.
w() <<
", " << quaternion.
x() <<
", " << quaternion.
y() <<
", " << quaternion.
z() <<
"]";
1059 template <
bool tActive,
typename T>
1060 MessageObject<tActive>&
operator<<(MessageObject<tActive>& messageObject,
const QuaternionT<T>& quaternion)
1062 return messageObject <<
"[" << quaternion.w() <<
", " << quaternion.x() <<
", " << quaternion.y() <<
", " << quaternion.z() <<
"]";
1065 template <
bool tActive,
typename T>
1066 MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const QuaternionT<T>& quaternion)
1068 return messageObject <<
"[" << quaternion.w() <<
", " << quaternion.x() <<
", " << quaternion.y() <<
", " << quaternion.z() <<
"]";
This class implements an euler rotation with angles: yaw, pitch and roll.
Definition: Euler.h:80
const T & yaw() const
Returns the yaw angle.
Definition: Euler.h:315
const T & roll() const
Returns the roll angle.
Definition: Euler.h:339
bool isValid() const
Returns whether the euler rotation holds valid parameters.
Definition: Euler.h:351
const T & pitch() const
Returns the pitch angle.
Definition: Euler.h:327
This class implements a 4x4 homogeneous transformation matrix using floating point values with the pr...
Definition: HomogenousMatrix4.h:110
SquareMatrixT3< T > orthonormalRotationMatrix() const
Returns the 3x3 orthonormal rotation matrix of the 4x4 transformation (by forcing a orthogonal and no...
Definition: HomogenousMatrix4.h:1538
This class provides basic numeric functionalities.
Definition: Numeric.h:57
static T angleAdjustPositive(const T angle)
Adjusts an arbitrary angle into the range of [0.0, 2PI).
Definition: Numeric.h:1764
static T sin(const T value)
Returns the sine of a given value.
Definition: Numeric.h:1568
static T abs(const T value)
Returns the absolute value of a given value.
Definition: Numeric.h:1220
static bool isWeakEqual(const T first, const T second)
Returns whether two values a equal up to a weak epsilon.
Definition: Numeric.h:2572
static T sqrt(const T value)
Returns the square root of a given value.
Definition: Numeric.h:1533
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition: Numeric.h:2386
static T cos(const T value)
Returns the cosine of a given value.
Definition: Numeric.h:1584
static T acos(const T value)
Returns the arccosine of a given value.
Definition: Numeric.h:2907
T & w()
Returns the w value of the quaternion.
Definition: Quaternion.h:911
T cos2(const QuaternionT< T > &quaternion) const
Returns the cosine value of the half angle between two quaternion rotations.
Definition: Quaternion.h:857
T sqr() const
Returns the square of the quaternion norm.
Definition: Quaternion.h:821
T & operator()(const unsigned int index)
Element operator.
Definition: Quaternion.h:1033
QuaternionT(const T *arrayValue)
Creates a new quaternion by an array with at least four elements.
Definition: Quaternion.h:692
T * operator()()
Access operator.
Definition: Quaternion.h:1046
bool normalize()
Normalizes the quaternion in place.
Definition: Quaternion.h:714
QuaternionT(const VectorT3< T > &reference, const VectorT3< T > &offset)
Creates a quaternion object based on two given unit vectors.
Definition: Quaternion.h:537
QuaternionT(const EulerT< T > &euler)
Creates a new quaternion by a given Euler rotation.
Definition: Quaternion.h:591
QuaternionT(const QuaternionT< U > &quaternion)
Copies a quaternion with different element data type than T.
Definition: Quaternion.h:503
bool operator==(const QuaternionT< T > &right) const
Returns whether two quaternions are identical up to a small epsilon.
Definition: Quaternion.h:960
T & operator[](const unsigned int index)
Element operator.
Definition: Quaternion.h:1019
const T & x() const
Returns the x value of the quaternion.
Definition: Quaternion.h:917
T norm() const
Returns the norm of this quaternion.
Definition: Quaternion.h:815
T dot(const QuaternionT< T > &quaternion) const
Returns the dot product between this quaternion and a second quaternion.
Definition: Quaternion.h:827
T angle(const QuaternionT< T > &quaternion) const
Returns the angle between two quaternion rotations.
Definition: Quaternion.h:841
T & x()
Returns the x value of the quaternion.
Definition: Quaternion.h:923
bool normalize(QuaternionT< T > &normalizedQuaternion) const
Normalizes the quaternion and returns the result as parameter.
Definition: Quaternion.h:734
QuaternionT(const SquareMatrixT3< T > &matrix)
Creates a new quaternion by a given 3x3 rotation matrix.
Definition: Quaternion.h:624
bool isValid() const
Returns whether this quaternion is a valid unit quaternion.
Definition: Quaternion.h:899
const T & w() const
Returns the w value of the quaternion.
Definition: Quaternion.h:905
QuaternionT< T > normalized() const
Returns the normalized quaternion.
Definition: Quaternion.h:699
bool invert(QuaternionT< T > &invertedQuaternion) const
Inverts this quaternion and returns the result as parameter.
Definition: Quaternion.h:789
QuaternionT()=default
Creates a new quaternion with default values, representation an identity rotation.
T smallestAngle(const QuaternionT< T > &quaternion) const
Returns the smallest angle between two quaternion rotations.
Definition: Quaternion.h:849
QuaternionT(const bool toIdentity)
Creates a new quaternion with either default values (representation an identity rotation) or an inval...
Definition: Quaternion.h:479
T operator()(const unsigned int index) const
Element operator.
Definition: Quaternion.h:1026
T angle() const
Returns the rotation angle defined by the quaternion.
Definition: Quaternion.h:833
QuaternionT< T > & operator*=(const RotationT< T > &right)
Combines and assigns a quaternion with a rotation.
Definition: Quaternion.h:1005
bool isEqual(const QuaternionT< T > &quaternion, const T eps) const
Returns whether two quaternions are equal up to a specified epsilon.
Definition: Quaternion.h:953
QuaternionT< T > slerp(const QuaternionT< T > &quaternion, T factor) const
Spherical linear interpolation between two quaternions.
Definition: Quaternion.h:865
QuaternionT(const VectorT3< T > &axis, const T angle)
Creates a new quaternion by a given axis and rotation angle.
Definition: Quaternion.h:521
T & z()
Returns the z value of the quaternion.
Definition: Quaternion.h:947
const T * operator()() const
Access operator.
Definition: Quaternion.h:1040
bool invert()
Inverts this quaternion in place.
Definition: Quaternion.h:769
bool operator!=(const QuaternionT< T > &right) const
Returns whether two quaternions are not identical up to a small epsilon.
Definition: Quaternion.h:986
QuaternionT(const RotationT< T > &rotation)
Creates a new quaternion by a given angle-axis rotation.
Definition: Quaternion.h:575
T Type
Definition of the used data type.
Definition: Quaternion.h:106
T values_[4]
The four values of the quaternion.
Definition: Quaternion.h:475
QuaternionT(const VectorT4< T > &vector)
Creates a new quaternion by a 4D vector.
Definition: Quaternion.h:686
QuaternionT< T > conjugate() const
Returns the conjugate of this quaternion.
Definition: Quaternion.h:809
T & y()
Returns the y value of the quaternion.
Definition: Quaternion.h:935
QuaternionT< T > inverted() const
Returns the inverted quaternion.
Definition: Quaternion.h:754
const T & y() const
Returns the y value of the quaternion.
Definition: Quaternion.h:929
const T & z() const
Returns the z value of the quaternion.
Definition: Quaternion.h:941
QuaternionT(const T w, const T x, const T y, const T z)
Creates a new quaternion by four given values.
Definition: Quaternion.h:512
QuaternionT< T > operator*(const RotationT< T > &right) const
Combines a angle-axis rotation with this quaternion.
Definition: Quaternion.h:992
QuaternionT< T > & operator*=(const QuaternionT< T > &right)
Combines and assigns two quaternions.
Definition: Quaternion.h:998
QuaternionT< T > operator*(const QuaternionT< T > &right) const
Combines two quaternion to a new combined rotation.
Definition: Quaternion.h:966
QuaternionT(const HomogenousMatrixT4< T > &transformation)
Creates a new quaternion by a given 4x4 homogeneous transformation matrix.
Definition: Quaternion.h:679
T operator[](const unsigned int index) const
Element operator.
Definition: Quaternion.h:1012
VectorT3< T > operator*(const VectorT3< T > &vector) const
Rotates a 3D vector by this quaternion.
Definition: Quaternion.h:975
This class implements a axis-angle rotation using floating point values.
Definition: Rotation.h:79
T angle() const
Returns the angle of the rotation.
Definition: Rotation.h:746
bool isValid() const
Returns whether this rotation has valid parameters.
Definition: Rotation.h:665
This class implements a 3x3 square matrix.
Definition: SquareMatrix3.h:88
T determinant() const
Returns the determinant of the matrix.
Definition: SquareMatrix3.h:1283
T trace() const
Returns the trace of the matrix which is the sum of the diagonal elements.
Definition: SquareMatrix3.h:1291
This class implements a vector with three elements.
Definition: Vector3.h:97
bool isUnit(const T eps=NumericT< T >::eps()) const
Returns whether this vector is a unit vector (whether the vector has the length 1).
Definition: Vector3.h:861
VectorT3< T > perpendicular() const
Returns a vector that is perpendicular to this vector.
Definition: Vector3.h:756
const T & y() const noexcept
Returns the y value.
Definition: Vector3.h:812
VectorT3< T > cross(const VectorT3< T > &vector) const
Returns the cross product of two vectors.
Definition: Vector3.h:597
const T & x() const noexcept
Returns the x value.
Definition: Vector3.h:800
const T & z() const noexcept
Returns the z value.
Definition: Vector3.h:824
bool isEqual(const VectorT3< T > &vector, const T eps) const
Returns whether two vectors are equal up to a specified epsilon.
Definition: Vector3.h:867
This class implements a vector with four elements.
Definition: Vector4.h:97
T minmax(const T &lowerBoundary, const T &value, const T &upperBoundary)
This function fits a given parameter into a specified value range.
Definition: base/Utilities.h:903
unsigned int sqr(const char value)
Returns the square value of a given value.
Definition: base/Utilities.h:1029
QuaternionT< Scalar > Quaternion
Definition of the Quaternion object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with sin...
Definition: Quaternion.h:33
std::vector< QuaternionF > QuaternionsF
Definition of a vector holding quaternion objects with single precision float data type.
Definition: Quaternion.h:76
std::vector< QuaternionD > QuaternionsD
Definition of a vector holding quaternion objects with double precision float data type.
Definition: Quaternion.h:83
QuaternionT< float > QuaternionF
Instantiation of the QuaternionT template class using a single precision float data type.
Definition: Quaternion.h:54
std::vector< QuaternionT< T > > QuaternionsT
Definition of a typename alias for vectors with QuaternionT objects.
Definition: Quaternion.h:62
QuaternionT< double > QuaternionD
Instantiation of the QuaternionT template class using a double precision float data type.
Definition: Quaternion.h:47
std::vector< Quaternion > Quaternions
Definition of a vector holding quaternion objects.
Definition: Quaternion.h:69
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15
std::ostream & operator<<(std::ostream &stream, const HighPerformanceStatistic &highPerformanceStatistic)
Definition: HighPerformanceTimer.h:963