7 #ifndef CAFU_MATH_QUATERNION_HPP_INCLUDED
8 #define CAFU_MATH_QUATERNION_HPP_INCLUDED
10 #include "Vector3.hpp"
18 template<
class T>
class AnglesT;
19 template<
class T>
class Matrix3x3T;
36 : x(x_), y(y_), z(z_), w(w_) { }
63 const T ww = T(1.0) - Vec.
x*Vec.
x - Vec.
y*Vec.
y - Vec.
z*Vec.
z;
85 return w<0 ? Vector3T<T>(x, y, z) :
Vector3T<T>(-x, -y, -z);
99 T
lengthSqr()
const {
return x*x + y*y + z*z + w*w; }
105 return fabs(
length()-T(1.0)) <= Epsilon;
144 return x==B.x && y==B.y && z==B.z && w==B.w;
152 return x!=B.x || y!=B.y || z!=B.z || w!=B.w;
170 x+=B.x; y+=B.y; z+=B.z; w+=B.w;
183 x-=B.x; y-=B.y; z-=B.z; w-=B.w;
191 w*B.x + x*B.w + y*B.z - z*B.y,
192 w*B.y + y*B.w + z*B.x - x*B.z,
193 w*B.z + z*B.w + x*B.y - y*B.x,
194 w*B.w - x*B.x - y*B.y - z*B.z);
206 x*=s; y*=s; z*=s; w*=s;
219 x/=s; y/=s; z/=s; w/=s;
225 typedef QuaternionT<float> QuaternionfT;
226 typedef QuaternionT<double> QuaterniondT;
249 const T len=length(A);
261 const T len=length(A);
272 T CosOmega=dot(P, Q);
274 if (CosOmega<0 && ShortPath)
280 CosOmega=std::min(CosOmega, T( 1.0));
281 CosOmega=std::max(CosOmega, T(-1.0));
284 const T Omega =acos(CosOmega);
285 const T SinOmega=sin (Omega);
287 if (SinOmega > T(0.01))
291 const T tp=sin((T(1.0)-t)*Omega)/SinOmega;
292 const T tq=sin( t *Omega)/SinOmega;
301 return normalizeOr0(P*(T(1.0)-t) + Q*t);
309 const T PI=T(3.14159265358979323846);
320 const int sigdigits=std::numeric_limits<T>::digits10 + 3;
322 std::ostringstream out;
324 out << std::setprecision(sigdigits) <<
"(" << A.x <<
", " << A.y <<
", " << A.z <<
", " << A.w <<
")";
330 template<
class T>
inline std::ostream& operator << (std::ostream& os, const cf::math::QuaternionT<T>& A)
332 return os <<
"(" << A.x <<
", " << A.y <<
", " << A.z <<
", " << A.w <<
")";
QuaternionT< T > operator*(const QuaternionT< T > &B) const
Returns the quaternion Q that expresses the combined rotation of this quaternion and B...
Definition: Quaternion.hpp:188
QuaternionT< T > operator-() const
The unary minus operator.
Definition: Quaternion.hpp:156
static QuaternionT FromXYZ(const Vector3T< T > &Vec)
Constructs a quaternion from the first three components (x, y, z) of a unit quaternion.
Definition: Quaternion.hpp:61
QuaternionT< T > & operator/=(const T s)
Divides this quaternion by s, assuming that s is not 0.
Definition: Quaternion.hpp:217
This class represents a triple of angles.
Definition: Angles.hpp:71
QuaternionT(T x_=0, T y_=0, T z_=0, T w_=1)
The default constructor. It initializes the quaternion from the given x_, y_, z_ and w_ respectively...
Definition: Quaternion.hpp:35
bool operator!=(const QuaternionT< T > &B) const
Returns whether this quaternion and B are not equal (bit-wise).
Definition: Quaternion.hpp:150
T lengthSqr() const
Returns the square of the length of this quaternion.
Definition: Quaternion.hpp:99
QuaternionT< T > operator/(const T s) const
Returns a copy of this quaternion divided by s, assuming that s is not 0.
Definition: Quaternion.hpp:211
T y
The y-component of this vector.
Definition: Vector3.hpp:41
static QuaternionT Euler(const T Pitch, const T Yaw, const T Roll)
Constructs a quaternion from three Euler angles.
Definition: Quaternion.cpp:99
QuaternionT< T > & operator*=(const T s)
Scales this quaternion by s.
Definition: Quaternion.hpp:204
This class represents a polymorphic 3-dimensional vector.
Definition: Misc.hpp:11
T length() const
Returns the length of this quaternion.
Definition: Quaternion.hpp:96
T & operator[](unsigned int Index)
Component access by index number (0 to 3) rather than by name.
Definition: Quaternion.hpp:112
bool IsNormal(const T Epsilon=0) const
Returns if this quaternion is normal, i.e.
Definition: Quaternion.hpp:103
QuaternionT< T > GetConjugate() const
Returns the conjugate of this quaternion.
Definition: Quaternion.hpp:90
Invalid operation (invalid use of method, etc.).
Definition: Errors.hpp:33
QuaternionT< T > & operator+=(const QuaternionT< T > &B)
Adds B to this quaternion.
Definition: Quaternion.hpp:168
bool operator==(const QuaternionT< T > &B) const
Returns whether this quaternion and B are (bit-wise) identical.
Definition: Quaternion.hpp:142
Vector3T< T > GetXYZ() const
Returns the x, y and z components as a Vector3T<T>.
Definition: Quaternion.hpp:81
QuaternionT< T > operator+(const QuaternionT< T > &B) const
Returns the sum of this quaternion and B.
Definition: Quaternion.hpp:162
T dot(const QuaternionT< T > &B) const
Returns the dot product of this quaternion and B.
Definition: Quaternion.hpp:93
QuaternionT(const C Values[])
Constructs a quaternion from an array of (at least) four values.
Definition: Quaternion.hpp:39
T z
The z-component of this vector.
Definition: Vector3.hpp:42
This class represents a quaternion.
Definition: Angles.hpp:18
T x
The x-component of this vector.
Definition: Vector3.hpp:40
QuaternionT< T > & operator-=(const QuaternionT< T > &B)
Subtracts B from this quaternion.
Definition: Quaternion.hpp:181
This class represents a generic 3x3 matrix.
Definition: Angles.hpp:17
Division by zero error.
Definition: Errors.hpp:24