8#ifndef META_OCEAN_MATH_QUATERNION_H
9#define META_OCEAN_MATH_QUATERNION_H
21template <
typename T>
class RotationT;
24template <
typename T>
class EulerT;
27template <
typename T>
class SquareMatrixT3;
30template <
typename T>
class HomogenousMatrixT4;
33template <
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;
500 ocean_assert(isValid() ==
true);
509 ocean_assert(isValid() ==
false);
517 values_[0] = T(quaternion[0]);
518 values_[1] = T(quaternion[1]);
519 values_[2] = T(quaternion[2]);
520 values_[3] = T(quaternion[3]);
537 const T angleValue = angle * T(0.5);
541 values_[1] = sinValue * axis[0];
542 values_[2] = sinValue * axis[1];
543 values_[3] = sinValue * axis[2];
545 ocean_assert_accuracy(isValid());
558 ocean_assert(rotation.
isValid());
560 const T angle = rotation.
angle() * T(0.5);
564 values_[1] = sinValue * rotation[0];
565 values_[2] = sinValue * rotation[1];
566 values_[3] = sinValue * rotation[2];
568 ocean_assert(isValid());
576 const T roll = euler.
roll() * T(0.5);
577 const T pitch = euler.
pitch() * T(0.5);
578 const T yaw = euler.
yaw() * T(0.5);
589 const T cc = cosRoll * cosYaw;
590 const T cs = cosRoll * sinYaw;
592 const T sc = sinRoll * cosYaw;
593 const T ss = sinRoll * sinYaw;
595 values_[0] = cosPitch * cc + sinPitch * ss;
596 values_[1] = cosPitch * ss + sinPitch * cc;
597 values_[2] = cosPitch * cs - sinPitch * sc;
598 values_[3] = cosPitch * sc - sinPitch * cs;
601 ocean_assert(isValid());
610 const T trace = matrix.
trace() + T(1.0);
617 const T factor = T(0.25) / values_[0];
619 values_[1] = (matrix(2, 1) - matrix(1, 2)) * factor;
620 values_[2] = (matrix(0, 2) - matrix(2, 0)) * factor;
621 values_[3] = (matrix(1, 0) - matrix(0, 1)) * factor;
625 if (matrix(0, 0) > matrix(1, 1) && matrix(0, 0) > matrix(2, 2))
627 values_[1] = T(0.5) *
NumericT<T>::sqrt(matrix(0, 0) - matrix(1, 1) - matrix(2, 2) + T(1.0));
629 const T factor = T(0.25) / values_[1];
631 values_[0] = (matrix(2, 1) - matrix(1, 2)) * factor;
632 values_[2] = (matrix(0, 1) + matrix(1, 0)) * factor;
633 values_[3] = (matrix(0, 2) + matrix(2, 0)) * factor;
635 else if (matrix(1, 1) > matrix(2, 2))
637 values_[2] = T(0.5) *
NumericT<T>::sqrt(matrix(1, 1) - matrix(0, 0) - matrix(2, 2) + T(1.0));
639 const T factor = T(0.25) / values_[2];
641 values_[0] = (matrix(0, 2) - matrix(2, 0)) * factor;
642 values_[1] = (matrix(0, 1) + matrix(1, 0)) * factor;
643 values_[3] = (matrix(1, 2) + matrix(2, 1)) * factor;
647 values_[3] = T(0.5) *
NumericT<T>::sqrt(matrix(2, 2) - matrix(0, 0) - matrix(1, 1) + T(1.0));
649 const T factor = T(0.25) / values_[3];
651 values_[0] = (matrix(1, 0) - matrix(0, 1)) * factor;
652 values_[1] = (matrix(0, 2) + matrix(2, 0)) * factor;
653 values_[2] = (matrix(1, 2) + matrix(2, 1)) * factor;
658 ocean_assert(isValid());
665 ocean_assert(isValid());
671 memcpy(values_, vector(),
sizeof(T) * 4);
677 ocean_assert(arrayValue);
678 memcpy(values_, arrayValue,
sizeof(T) * 4);
684 const T normValue = norm();
691 const T factor = T(1.0) / normValue;
693 return QuaternionT<T>(values_[0] * factor, values_[1] * factor, values_[2] * factor, values_[3] * factor);
699 const T normValue = norm();
706 const T factor = T(1.0) / normValue;
708 values_[0] *= factor;
709 values_[1] *= factor;
710 values_[2] *= factor;
711 values_[3] *= factor;
719 const T normValue = norm();
726 const T factor = T(1.0) / normValue;
728 normalizedQuaternion.
values_[0] = values_[0] * factor;
729 normalizedQuaternion.
values_[1] = values_[1] * factor;
730 normalizedQuaternion.
values_[2] = values_[2] * factor;
731 normalizedQuaternion.
values_[3] = values_[3] * factor;
739 const T square =
sqr();
746 const T factor = T(1) / square;
748 return QuaternionT<T>(values_[0] * factor, -values_[1] * factor, -values_[2] * factor, -values_[3] * factor);
754 const T square =
sqr();
761 const T factor = T(1.0) / square;
763 values_[0] *= factor;
764 values_[1] *= -factor;
765 values_[2] *= -factor;
766 values_[3] *= -factor;
774 const T square =
sqr();
781 const T factor = T(1.0) / square;
783 invertedQuaternion.
values_[0] = values_[0] * factor;
784 invertedQuaternion.
values_[1] = values_[1] * -factor;
785 invertedQuaternion.
values_[2] = values_[2] * -factor;
786 invertedQuaternion.
values_[3] = values_[3] * -factor;
794 return QuaternionT<T>(values_[0], -values_[1], -values_[2], -values_[3]);
800 return NumericT<T>::sqrt(values_[0] * values_[0] + values_[1] * values_[1] + values_[2] * values_[2] + values_[3] * values_[3]);
806 return values_[0] * values_[0] + values_[1] * values_[1] + values_[2] * values_[2] + values_[3] * values_[3];
812 return values_[0] * quaternion.
values_[0] + values_[1] * quaternion.
values_[1] + values_[2] * quaternion.
values_[2] + values_[3] * quaternion.
values_[3];
818 ocean_assert(isValid());
826 ocean_assert(isValid() && quaternion.
isValid());
828 return (inverted() * quaternion).angle();
834 ocean_assert(isValid() && quaternion.
isValid());
842 ocean_assert(isValid() && quaternion.
isValid());
844 return (inverted() * quaternion).w();
850 ocean_assert(factor >= 0 && factor <= T(1.0));
851 ocean_assert(isValid() && quaternion.
isValid());
853 T sigma =
minmax(T(-1), w() * quaternion.
w() + x() * quaternion.
x() + y() * quaternion.
y() + z() * quaternion.
z(), T(1));
860 adjustedQuaternion =
QuaternionT<T>(-quaternion.
w(), -quaternion.
x(), -quaternion.
y(), -quaternion.
z());
865 T factorA = T(1.0) - factor;
878 return QuaternionT<T>(factorA * w() + factorB * adjustedQuaternion.
w(), factorA * x() + factorB * adjustedQuaternion.
x(), factorA * y() + factorB * adjustedQuaternion.
y(), factorA * z() + factorB * adjustedQuaternion.
z()).
normalized();
960 ocean_assert(isValid());
962 const QuaternionT<T> quaternion(0, vector[0], vector[1], vector[2]);
971 return !(*
this == right);
983 *
this = *
this * right;
997 ocean_assert(index < 4u);
998 return values_[index];
1001template <
typename T>
1004 ocean_assert(index < 4u);
1005 return values_[index];
1008template <
typename T>
1011 ocean_assert(index < 4u);
1012 return values_[index];
1015template <
typename T>
1018 ocean_assert(index < 4u);
1019 return values_[index];
1022template <
typename T>
1028template <
typename T>
1034template <
typename T>
1046 else if (left == -right)
1050 return QuaternionT<T>(T(0), perpendicular.
x(), perpendicular.
y(), perpendicular.
z());
1055 const T w = T(1) + right * left;
1060 ocean_assert(result.
isValid());
1065template <
typename T>
1068 stream <<
"[" << quaternion.
w() <<
", " << quaternion.
x() <<
", " << quaternion.
y() <<
", " << quaternion.
z() <<
"]";
1073template <
bool tActive,
typename T>
1074MessageObject<tActive>&
operator<<(MessageObject<tActive>& messageObject,
const QuaternionT<T>& quaternion)
1076 return messageObject <<
"[" << quaternion.w() <<
", " << quaternion.x() <<
", " << quaternion.y() <<
", " << quaternion.z() <<
"]";
1079template <
bool tActive,
typename T>
1080MessageObject<tActive>&
operator<<(MessageObject<tActive>&& messageObject,
const QuaternionT<T>& quaternion)
1082 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:82
const T & yaw() const
Returns the yaw angle.
Definition Euler.h:324
const T & roll() const
Returns the roll angle.
Definition Euler.h:348
bool isValid() const
Returns whether the euler rotation holds valid parameters.
Definition Euler.h:360
const T & pitch() const
Returns the pitch angle.
Definition Euler.h:336
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:1770
static T sin(const T value)
Returns the sine of a given value.
Definition Numeric.h:1572
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:2581
static T sqrt(const T value)
Returns the square root of a given value.
Definition Numeric.h:1537
static bool isEqual(const T first, const T second)
Returns whether two values are equal up to a small epsilon.
Definition Numeric.h:2395
static T cos(const T value)
Returns the cosine of a given value.
Definition Numeric.h:1588
static T acos(const T value)
Returns the arccosine of a given value.
Definition Numeric.h:2916
T & w()
Returns the w value of the quaternion.
Definition Quaternion.h:894
T Type
Definition of the used data type.
Definition Quaternion.h:106
T cos2(const QuaternionT< T > &quaternion) const
Returns the cosine value of the half angle between two quaternion rotations.
Definition Quaternion.h:840
T sqr() const
Returns the square of the quaternion norm.
Definition Quaternion.h:804
T & operator()(const unsigned int index)
Element operator.
Definition Quaternion.h:1016
QuaternionT(const T *arrayValue)
Creates a new quaternion by an array with at least four elements.
Definition Quaternion.h:675
T * operator()()
Access operator.
Definition Quaternion.h:1029
bool normalize()
Normalizes the quaternion in place.
Definition Quaternion.h:697
QuaternionT(const VectorT3< T > &reference, const VectorT3< T > &offset)
Creates a quaternion object based on two given unit vectors.
Definition Quaternion.h:549
QuaternionT(const EulerT< T > &euler)
Creates a new quaternion by a given Euler rotation.
Definition Quaternion.h:572
QuaternionT(const QuaternionT< U > &quaternion)
Copies a quaternion with different element data type than T.
Definition Quaternion.h:515
bool operator==(const QuaternionT< T > &right) const
Returns whether two quaternions are identical up to a small epsilon.
Definition Quaternion.h:943
T & operator[](const unsigned int index)
Element operator.
Definition Quaternion.h:1002
const T & x() const
Returns the x value of the quaternion.
Definition Quaternion.h:900
T norm() const
Returns the norm of this quaternion.
Definition Quaternion.h:798
T dot(const QuaternionT< T > &quaternion) const
Returns the dot product between this quaternion and a second quaternion.
Definition Quaternion.h:810
T angle(const QuaternionT< T > &quaternion) const
Returns the angle between two quaternion rotations.
Definition Quaternion.h:824
T & x()
Returns the x value of the quaternion.
Definition Quaternion.h:906
bool normalize(QuaternionT< T > &normalizedQuaternion) const
Normalizes the quaternion and returns the result as parameter.
Definition Quaternion.h:717
QuaternionT(const SquareMatrixT3< T > &matrix)
Creates a new quaternion by a given 3x3 rotation matrix.
Definition Quaternion.h:605
bool isValid() const
Returns whether this quaternion is a valid unit quaternion.
Definition Quaternion.h:882
const T & w() const
Returns the w value of the quaternion.
Definition Quaternion.h:888
QuaternionT< T > normalized() const
Returns the normalized quaternion.
Definition Quaternion.h:682
bool invert(QuaternionT< T > &invertedQuaternion) const
Inverts this quaternion and returns the result as parameter.
Definition Quaternion.h:772
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:832
QuaternionT(const bool toIdentity)
Creates a new quaternion with either default values (representation an identity rotation) or an inval...
Definition Quaternion.h:491
T operator()(const unsigned int index) const
Element operator.
Definition Quaternion.h:1009
T angle() const
Returns the rotation angle defined by the quaternion.
Definition Quaternion.h:816
QuaternionT< T > & operator*=(const RotationT< T > &right)
Combines and assigns a quaternion with a rotation.
Definition Quaternion.h:988
bool isEqual(const QuaternionT< T > &quaternion, const T eps) const
Returns whether two quaternions are equal up to a specified epsilon.
Definition Quaternion.h:936
QuaternionT< T > slerp(const QuaternionT< T > &quaternion, T factor) const
Spherical linear interpolation between two quaternions.
Definition Quaternion.h:848
QuaternionT(const VectorT3< T > &axis, const T angle)
Creates a new quaternion by a given axis and rotation angle.
Definition Quaternion.h:533
T & z()
Returns the z value of the quaternion.
Definition Quaternion.h:930
const T * operator()() const
Access operator.
Definition Quaternion.h:1023
bool invert()
Inverts this quaternion in place.
Definition Quaternion.h:752
bool operator!=(const QuaternionT< T > &right) const
Returns whether two quaternions are not identical up to a small epsilon.
Definition Quaternion.h:969
QuaternionT(const RotationT< T > &rotation)
Creates a new quaternion by a given angle-axis rotation.
Definition Quaternion.h:556
T values_[4]
The four values of the quaternion.
Definition Quaternion.h:487
QuaternionT(const VectorT4< T > &vector)
Creates a new quaternion by a 4D vector.
Definition Quaternion.h:669
QuaternionT< T > conjugate() const
Returns the conjugate of this quaternion.
Definition Quaternion.h:792
T & y()
Returns the y value of the quaternion.
Definition Quaternion.h:918
QuaternionT< T > inverted() const
Returns the inverted quaternion.
Definition Quaternion.h:737
static QuaternionT< T > left_Q_right(const VectorT3< T > &left, const VectorT3< T > &right)
Returns a quaternion object based on two given unit vectors.
Definition Quaternion.h:1035
const T & y() const
Returns the y value of the quaternion.
Definition Quaternion.h:912
const T & z() const
Returns the z value of the quaternion.
Definition Quaternion.h:924
QuaternionT(const T w, const T x, const T y, const T z)
Creates a new quaternion by four given values.
Definition Quaternion.h:524
QuaternionT< T > operator*(const RotationT< T > &right) const
Combines a angle-axis rotation with this quaternion.
Definition Quaternion.h:975
QuaternionT< T > & operator*=(const QuaternionT< T > &right)
Combines and assigns two quaternions.
Definition Quaternion.h:981
QuaternionT< T > operator*(const QuaternionT< T > &right) const
Combines two quaternion to a new combined rotation.
Definition Quaternion.h:949
QuaternionT(const HomogenousMatrixT4< T > &transformation)
Creates a new quaternion by a given 4x4 homogeneous transformation matrix.
Definition Quaternion.h:662
T operator[](const unsigned int index) const
Element operator.
Definition Quaternion.h:995
VectorT3< T > operator*(const VectorT3< T > &vector) const
Rotates a 3D vector by this quaternion.
Definition Quaternion.h:958
This class implements a axis-angle rotation using floating point values.
Definition Rotation.h:73
T angle() const
Returns the angle of the rotation.
Definition Rotation.h:649
bool isValid() const
Returns whether this rotation has valid parameters.
Definition Rotation.h:547
This class implements a 3x3 square matrix.
Definition SquareMatrix3.h:89
T determinant() const
Returns the determinant of the matrix.
Definition SquareMatrix3.h:1284
bool isSingular() const
Returns whether this matrix is singular (and thus cannot be inverted).
Definition SquareMatrix3.h:1342
T trace() const
Returns the trace of the matrix which is the sum of the diagonal elements.
Definition SquareMatrix3.h:1292
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:873
VectorT3< T > perpendicular() const
Returns a vector that is perpendicular to this vector.
Definition Vector3.h:768
const T & y() const noexcept
Returns the y value.
Definition Vector3.h:824
VectorT3< T > cross(const VectorT3< T > &vector) const
Returns the cross product of two vectors.
Definition Vector3.h:609
const T & x() const noexcept
Returns the x value.
Definition Vector3.h:812
const T & z() const noexcept
Returns the z value.
Definition Vector3.h:836
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:973
unsigned int sqr(const char value)
Returns the square value of a given value.
Definition base/Utilities.h:1099
std::vector< QuaternionF > QuaternionsF
Definition of a vector holding quaternion objects with single precision float data type.
Definition Quaternion.h:76
std::vector< Quaternion > Quaternions
Definition of a vector holding quaternion objects.
Definition Quaternion.h:69
std::vector< QuaternionT< T > > QuaternionsT
Definition of a typename alias for vectors with QuaternionT objects.
Definition Quaternion.h:62
std::vector< QuaternionD > QuaternionsD
Definition of a vector holding quaternion objects with double precision float data type.
Definition Quaternion.h:83
The namespace covering the entire Ocean framework.
Definition Accessor.h:15
std::ostream & operator<<(std::ostream &stream, const HighPerformanceStatistic &highPerformanceStatistic)
Definition HighPerformanceTimer.h:963