13#ifndef ML_QUATERNION_H
14#define ML_QUATERNION_H
112 qx =
static_cast<DT
>(
q2.qx);
113 qy =
static_cast<DT
>(
q2.qy);
114 qz =
static_cast<DT
>(
q2.qz);
115 qw =
static_cast<DT
>(
q2.qw);
217 inline void set(
const DT x,
const DT y,
const DT z,
const DT w)
238 printTemplateError(
"TQuaternion::getAsMat4() const, quaternion is not convertible to 4x4 matrix",
261 const DT
qxqx = qx * qx;
262 const DT
qyqy = qy * qy;
263 const DT
qzqz = qz * qz;
265 const DT
qxqy = qx * qy;
266 const DT
qxqz = qx * qz;
267 const DT
qxqw = qx * qw;
269 const DT
qyqz = qy * qz;
270 const DT
qyqw = qy * qw;
272 const DT
qzqw = qz * qw;
449 b.
qz != 0 ? qz / b.
qz : qz, b.
qw != 0 ? qw / b.
qw : qw)
474 return qx * p.
qx + qy * p.
qy + qz * p.
qz + qw * p.
qw;
489 qw * p.
qw - qx * p.
qx - qy * p.
qy - qz * p.
qz);
503 return conjugate().mult(
q);
509 return (euclideanMult(
q) -
q.euclideanMult(*
this)) / 2;
529 "Returning unchanged quaternion");
530 retVal.set(qx, qy, qz, qw);
535 retVal.set(qx, qy, qz, qw);
586 return qx * qx + qy * qy + qz * qz + qw * qw;
594 return static_cast<DT
>(
::sqrt(qx * qx + qy * qy + qz * qz + qw * qw));
601 return static_cast<DT
>(
::sqrt(norm()));
612 const DT
n = norm2();
618 "Returning default quaternion");
646 const DT
n = norm2();
652 "Returning default quaternion");
677 const DT
absVal = absoluteValue();
710 "Returning default quaternion");
721 retVal = conjugate().mult(1 /
n);
736 const DT
n2 = norm2();
737 const DT
lqv = qv().length();
743 "Returning default quaternion");
774 const DT
len = qv().length();
790 const DT
n2 = norm2();
791 const DT
qvLen = qv().length();
806 return ln().mult(
quat).exp();
824 ::sin(qw) * ::cosh(
len));
837 ?
static_cast<DT
>(-1) *
static_cast<DT
>(::sin(qw)) * qv() /
len *
static_cast<DT
>(::sinh(
len))
839 ::cos(qw) * ::cosh(
len));
846 return sin().div(cos(),
isError);
852 return cos().div(sin(),
isError);
870 ::sinh(qw) * ::cos(
len));
884 ::cosh(qw) * ::cos(
len));
891 return sinh().div(cosh(),
isError);
897 return cosh().div(sinh(),
isError);
934#define _ML_QUATERNION_CALC_CHECKED(FUNC_NAME, CALC_EXPR) \
935 TQuaternion<DT> retVal; \
937 const Tvec3<DT>& v = qv(); \
938 const DT len = v.length(); \
939 if (MLValueIs0WOM(len)) { \
941 if (isError == NULL) { \
943 printTemplateError("TQuaternion::" FUNC_NAME "() const, " FUNC_NAME " of quaternion cannot be calculated", \
944 ML_BAD_PARAMETER, "Returning default quaternion"); \
953 if (isError != NULL) { \
956 retVal = CALC_EXPR; \
963#define _ML_QUAT_CALC_EXP TQuaternion<DT>(-v / len, 0).mult(mult(TQuaternion<DT>(-v / len, 0)).arcsinh());
973#undef _ML_QUAT_CALC_EXP
977#define _ML_QUAT_CALC_EXP TQuaternion<DT>(-v / len, 0).mult(arccosh());
983#undef _ML_QUAT_CALC_EXP
987#define _ML_QUAT_CALC_EXP TQuaternion<DT>(-v / len, 0).mult(mult(TQuaternion<DT>(-v / len, 0)).arctanh());
993#undef _ML_QUAT_CALC_EXP
997#undef _ML_QUATERNION_CALC_CHECKED
1030#ifdef _ML_IMPLEMENT_GLOBAL_QUATERNION_OPERATORS
1031#ifndef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCS_AND_OPS_IN_GLOBAL_NAMESPACE
1039template <
typename DT>
1040inline ML_LA_NAMESPACE::TQuaternion<DT> operator+(DT d,
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1042 return ML_LA_NAMESPACE::TQuaternion<DT>(
q.qx,
q.qy,
q.qz,
q.qw + d);
1046template <
typename DT>
1047inline ML_LA_NAMESPACE::TQuaternion<DT> operator+(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q, DT d)
1049 return ML_LA_NAMESPACE::TQuaternion<DT>(
q.qx,
q.qy,
q.qz,
q.qw + d);
1053template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT>
operator+(
const ML_LA_NAMESPACE::TQuaternion<DT>&
qa,
1054 const ML_LA_NAMESPACE::TQuaternion<DT>&
qb)
1056 return qa.operator+(
qb);
1064template <
typename DT>
1065inline ML_LA_NAMESPACE::TQuaternion<DT>
operator-(DT d,
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1067 return ML_LA_NAMESPACE::TQuaternion<DT>(-
q.qx, -
q.qy, -
q.qz, d -
q.qw);
1071template <
typename DT>
1072inline ML_LA_NAMESPACE::TQuaternion<DT>
operator-(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q, DT d)
1074 return ML_LA_NAMESPACE::TQuaternion<DT>(
q.qx,
q.qy,
q.qz,
q.qw - d);
1078template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT>
operator-(
const ML_LA_NAMESPACE::TQuaternion<DT>&
qa,
1079 const ML_LA_NAMESPACE::TQuaternion<DT>&
qb)
1081 return qa.operator-(
qb);
1086template <
typename DT>
1087inline ML_LA_NAMESPACE::TQuaternion<DT>
operator*(DT d,
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1093template <
typename DT>
1094inline ML_LA_NAMESPACE::TQuaternion<DT>
operator*(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q, DT d)
1100template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT>
operator*(
const ML_LA_NAMESPACE::TQuaternion<DT>&
qa,
1101 const ML_LA_NAMESPACE::TQuaternion<DT>&
qb)
1103 return qa.operator*(
qb);
1107#ifndef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCS_AND_OPS_IN_GLOBAL_NAMESPACE
1122#ifdef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCTIONS
1123#ifndef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCS_AND_OPS_IN_GLOBAL_NAMESPACE
1129#ifdef _ML_IMPLEMENT_QUATERNION_SQRT
1130template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT>
sqrt(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1137template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT>
exp(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1143template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> ln(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1149template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> sin(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1155template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> cos(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1161template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> tan(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1167template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> cotan(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1173template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> sinh(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1179template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> cosh(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1185template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> tanh(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1191template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> cotanh(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1197template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> arcsinh(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1203template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> arccosh(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1209template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> arctanh(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1215template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> arcsin(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1221template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> arccos(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1227template <
typename DT>
inline ML_LA_NAMESPACE::TQuaternion<DT> arctan(
const ML_LA_NAMESPACE::TQuaternion<DT>&
q)
1233#ifndef _ML_IMPLEMENT_GLOBAL_QUATERNION_FUNCS_AND_OPS_IN_GLOBAL_NAMESPACE
1248 template <
typename DT>
inline ostream&
operator<<(ostream& s,
const ML_NAMESPACE::TQuaternion<DT>& v)
1250 return s <<
"(" << v.qx <<
"," << v.qy <<
"," << v.qz <<
"," << v.qw <<
")";
Template class for vector arithmetic with floating point datatypes.
T length() const
Returns the length of the vector, i.e., norm2().
Declaration of complex type traits.
TQuaternion< DT > arctan(bool *isError=nullptr) const
Inverse tangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
TQuaternion(DT d)
Builds a quaternion from a scalar by setting all elements to 0 and qw to the scalar; use explicitly t...
TQuaternion< DT > arccos(bool *isError=nullptr) const
Inverse cosine; for error handling see _ML_QUATERNION_CALC_CHECKED.
TQuaternion< DT > euclideanMult(const TQuaternion< DT > &q) const
Euclidean product of this quaternion with another one (which is non-commutative).
bool operator!=(const TQuaternion< DT > &b) const
Inequality operator: Returns whether any component differs from its corresponding component of b.
DT dot(const TQuaternion< DT > &p) const
Dot product, equivalent to four component vector dot product.
TQuaternion< DT > arg(bool *isError=nullptr) const
Computes the quaternion argument.
TQuaternion< DT > ln() const
Computes the natural logarithm.
TQuaternion(DT x, DT y, DT z, DT w)
Creates a new TQuaternion with the DT values given.
TQuaternion< DT > arccosh() const
Computes the inverse hyperbolic cosine.
DT qx
First element of imaginary part.
TQuaternion< DT > operator/(const TQuaternion< DT > &q) const
Divides by a quaternion by another.
TQuaternion< DT > sinh() const
Hyperbolic functions: Hyperbolic sine, hyperbolic cosine, hyperbolic tangent https://en....
TQuaternion< DT > operator/(DT d) const
Divides by a scalar d. The caller must guarantee that d is non-zero.
DT absoluteValue() const
Returns the absolute value of a quaternion, which is the scalar quantity that determines the length o...
TQuaternion< DT > tan(bool *isError=nullptr) const
Computes the tangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
TQuaternion< DT > & operator+=(const TQuaternion< DT > &b)
Adds the corresponding component from b to each component.
TQuaternion< DT > add(const TQuaternion< DT > &b) const
Adds another quaternion b to this one and returns the sum.
TQuaternion< DT > tanh(bool *isError=nullptr) const
Computes the hyperbolic tangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
FloatingPointVector< DT, 3, Vector3DataContainer< DT > > getImaginaryPart() const
Returns a copy of the quaternion's imaginary part.
TQuaternion< DT > sqrt(bool *isError=nullptr) const
Computes the square root of a quaternion.
void set(const FloatingPointVector< DT, 3, Vector3DataContainer< DT > > &v, const DT w)
Sets all quaternion elements from a vector and DT parameter.
TQuaternion< DT > operator*(const TQuaternion< DT > &b) const
Multiplies each components with its corresponding components from b.
TQuaternion< DT > div(const TQuaternion< DT > &d, bool *isError=nullptr) const
Divides a quaternion by another quaternion.
TQuaternion< DT > operator+(const TQuaternion< DT > &b) const
Adds the corresponding component from b to each component.
TQuaternion< DT > sgn(bool *isError=nullptr) const
Returns the sign of the quaternion.
const FloatingPointVector< DT, 3, Vector3DataContainer< DT > > & qv() const
Returns a constant reference to the quaternion's imaginary part as vector object.
TQuaternion< DT > cotan(bool *isError=nullptr) const
Computes the cotangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
FloatingPointVector< DT, 3, Vector3DataContainer< DT > > & qv()
Returns a modifiable reference to the quaternion's imaginary part as vector object,...
TQuaternion< DT > sin() const
Trigonometric functions: sine, cosine, tangent, cotangent.
TQuaternion< DT > cosh() const
Computes the hyperbolic cosine.
TQuaternion< DT > arcsin(bool *isError=nullptr) const
Inverse trigonometric functions: Inverse sine, inverse cosine, inverse tangent.
bool operator==(const TQuaternion< DT > &b) const
Equality operator: Returns whether each component of this is equal to its corresponding component of ...
TQuaternion< DT > exp() const
Exponential and logarithmic functions: natural exponential, natural logarithm, power.
TQuaternion< DT > negate() const
Computes the negation.
TQuaternion< DT > & operator-=(const TQuaternion< DT > &b)
Subtracts the corresponding component from b from each component.
DT norm() const
Computes the d-norm.
TQuaternion< DT > arctanh() const
Computes the inverse hyperbolic tangent.
TQuaternion< DT > arcsinh() const
Inverse hyperbolic function: arcsinh, arccosh, arctanh.
TQuaternion< DT > mult(const TQuaternion< DT > &q) const
Multiplies this quaternion with another one (which is non-commutative). It is also called Grassman pr...
TQuaternion< DT > compDiv(const TQuaternion< DT > &b, bool *isError=nullptr) const
Divides each component by its corresponding component from b.
TQuaternion()
Standard constructor that is setting all elements to 0.
TQuaternion< DT > inverse(bool *isInvertible=nullptr) const
Computes and returns the inverse.
DT value_type
Scalar type used for qx, qy, qz, and qw.
DT & operator[](const size_t i)
Indexing operator.
TQuaternion< DT > & operator/=(DT d)
Divides by a scalar d. The caller must guarantee that d is non-zero.
Tmat4< DT > getAsMat4(bool *isConvertible=nullptr) const
Converts this quaternion to a 4x4 matrix.
TQuaternion< DT > operator-(const TQuaternion< DT > &b) const
Subtracts the corresponding component from b from each component.
TQuaternion< DT > & operator=(const TQuaternion< DT > &q)
Assignment operator.
TQuaternion< DT > add(DT s) const
Adds a scalar s to the quaternion and returns the sum.
void set(const DT x, const DT y, const DT z, const DT w)
Sets all quaternion elements qx, qy, qz, and qw from parameters x, y, z, and w, respectively.
DT norm2() const
Computes the magnitude of the quaternion.
TQuaternion< DT > & operator*=(const TQuaternion< DT > &b)
Multiplies each components with its corresponding components from b.
TQuaternion< DT > odd(const TQuaternion< DT > &p) const
Odd product, also known as cross-product or Grassman outer product.
TQuaternion< DT > normalize(bool *isError=nullptr) const
Returns the normalization of the quaternion.
TQuaternion(const TQuaternion< DT2 > &q2)
Copy constructor from another TQuaternion of same or different type.
TQuaternion< DT > conjugate() const
Computes the conjugate.
DT qz
Third element of imaginary part.
TQuaternion< DT > mult(DT s) const
Multiplies this quaternion with a scalar factor s.
TQuaternion(const FloatingPointVector< DT, 4 > &v)
Creates a new TQuaternion from the four-dimensional parameter vector.
TQuaternion< DT > cos() const
Computes the cosine.
DT operator[](const size_t i) const
Constant indexing operator.
TQuaternion< DT > cotanh(bool *isError=nullptr) const
Computes the hyperbolic cotangent; for error handling see _ML_QUATERNION_CALC_CHECKED.
TQuaternion< DT > pow(const TQuaternion< DT > &quat) const
Computes the power of a quaternion.
TQuaternion(const FloatingPointVector< DT, 3, Vector3DataContainer< DT > > &v, DT w)
Constructor with a vector and DT parameter.
TQuaternion(const TQuaternion &q2)=default
DT getRealPart() const
Returns a copy of the quaternion's real part, also called scalar() of a quaternion.
DT qy
Second element of imaginary part.
TQuaternion< DT > even(const TQuaternion< DT > &p) const
Even product, also known as Grassman inner product.
TQuaternion< DT > outer(const TQuaternion< DT > &q) const
Euclidean outer product of this quaternion with another one.
A 4x4 matrix class consisting of four row vectors.
Forward declarations to resolve header file dependencies.
Specialized base class for the FloatingPointVectorDataContainerBase.
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0; otherwise, it returns false.
bool MLValuesAreEqualWOM(MLint8 a, MLint8 b)
Returns true if values a and b are equal; otherwise, it returns false.
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm,...
#define ML_CHECK_FLOAT(x)
#define _ML_QUAT_CALC_EXP
Internal helper macro - do not use.
#define _ML_QUATERNION_CALC_CHECKED(FUNC_NAME, CALC_EXPR)
Defines a code fragment that checks and handles operations for safe expression calculations.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
ScopeGuard< Functor > operator+(ScopeGuardOnExit, Functor &&fn)
void ML_UTILS_EXPORT printTemplateError(const char *location, MLErrorCode reason, const std::string_view &handling)
bool operator==(const Tmat2< DT > &a, const Tmat2< DT > &b)
a == b ? Returns true if yes.
T operator*(const FloatingPointVector< T, size, DataContainer > &a, const FloatingPointVector< T, size, DataContainer > &b)
Dot product, returns a.dot(b).
FloatingPointVector< T, size, DataContainer > operator-(FloatingPointVector< T, size, DataContainer > lhs, const FloatingPointVector< T, size, DataContainer > &rhs)
Return value is the component-wise subtraction of rhs from lhs.
MLEXPORT std::ostream & operator<<(std::ostream &s, const ml::Field &v)
Overloads the operator '<<' for stream output of Field objects.