32 #ifndef Quaternion_INCLUDE_ONCE 33 #define Quaternion_INCLUDE_ONCE 43 template<
typename T_Scalar>
64 mXYZW.x() = (T_Scalar)q.
x();
65 mXYZW.y() = (T_Scalar)q.
y();
66 mXYZW.z() = (T_Scalar)q.
z();
67 mXYZW.w() = (T_Scalar)q.
w();
88 mXYZW.x() = (T_Scalar)v.
x();
89 mXYZW.y() = (T_Scalar)v.
y();
90 mXYZW.z() = (T_Scalar)v.
z();
91 mXYZW.w() = (T_Scalar)v.
w();
107 mXYZW.x() = (T_Scalar)v.
x();
108 mXYZW.y() = (T_Scalar)v.
y();
109 mXYZW.z() = (T_Scalar)v.
z();
110 mXYZW.w() = (T_Scalar)v.
w();
116 return x() == q.
x() &&
y() == q.
y() &&
z() == q.
z() &&
w() == q.
w();
127 if (
x() != other.
x())
128 return x() < other.
x();
129 if (
y() != other.
y())
130 return y() < other.
y();
131 if (
z() != other.
z())
132 return z() < other.
z();
134 return w() < other.
w();
151 const T_Scalar&
x()
const {
return mXYZW.x(); }
153 const T_Scalar&
y()
const {
return mXYZW.y(); }
155 const T_Scalar&
z()
const {
return mXYZW.z(); }
157 const T_Scalar&
w()
const {
return mXYZW.w(); }
181 val = (T_Scalar)1.0 / val;
191 val = (T_Scalar)1.0 / val;
338 return Quaternion().setFromEulerXYZ(degX, degY, degZ);
350 return Quaternion().setFromEulerZYX(degZ, degY, degX);
362 return Quaternion().setFromAxisAngle(axis, degrees);
388 return x()*q.
x() +
y()*q.
y() +
z()*q.
z() +
w()*q.
w();
409 return x()*
x() +
y()*
y() +
z()*
z() +
w()*
w();
475 out = a + (b - a) * t;
485 template<
typename T_Scalar>
491 template<
typename T_Scalar>
495 q.
x() = q1.
w() * q2.
x() + q1.
x() * q2.
w() + q1.
y() * q2.
z() - q1.
z() * q2.
y();
496 q.
y() = q1.
w() * q2.
y() + q1.
y() * q2.
w() + q1.
z() * q2.
x() - q1.
x() * q2.
z();
497 q.
z() = q1.
w() * q2.
z() + q1.
z() * q2.
w() + q1.
x() * q2.
y() - q1.
y() * q2.
x();
498 q.
w() = q1.
w() * q2.
w() - q1.
x() * q2.
x() - q1.
y() * q2.
y() - q1.
z() * q2.
z();
503 template<
typename T_Scalar>
507 T_Scalar x2 = q.
x() * q.
x();
508 T_Scalar y2 = q.
y() * q.
y();
509 T_Scalar z2 = q.
z() * q.
z();
510 T_Scalar xy = q.
x() * q.
y();
511 T_Scalar xz = q.
x() * q.
z();
512 T_Scalar yz = q.
y() * q.
z();
513 T_Scalar wx = q.
w() * q.
x();
514 T_Scalar wy = q.
w() * q.
y();
515 T_Scalar wz = q.
w() * q.
z();
518 r.
x() = ( v.
x()*(1.0f - 2.0f * (y2 + z2)) + v.
y()*(2.0f * (xy - wz)) + v.
z()*(2.0f * (xz + wy)) );
519 r.
y() = ( v.
x()*(2.0f * (xy + wz)) + v.
y()*(1.0f - 2.0f * (x2 + z2)) + v.
z()*(2.0f * (yz - wx)) );
520 r.
z() = ( v.
x()*(2.0f * (xz - wy)) + v.
y()*(2.0f * (yz + wx)) + v.
z()*(1.0f - 2.0f * (x2 + y2)) );
525 template<
typename T_Scalar>
531 template<
typename T_Scalar>
538 template<
typename T_Scalar>
545 template<
typename T_Scalar>
551 template<
typename T_Scalar>
554 T_Scalar tr, s, q[4];
556 int next[3] = {1, 2, 0};
558 tr = m.
e(0,0) + m.
e(1,1) + m.
e(2,2);
561 if (tr + (T_Scalar)1.0 > 0.0)
564 w() = s / (T_Scalar)2.0;
565 s = (T_Scalar)0.5 / s;
566 x() = (m.
e(2,1) - m.
e(1,2)) * s;
567 y() = (m.
e(0,2) - m.
e(2,0)) * s;
568 z() = (m.
e(1,0) - m.
e(0,1)) * s;
575 if (m.
e(1,1) > m.
e(0,0)) i = 1;
576 if (m.
e(2,2) > m.
e(i,i)) i = 2;
580 s =
vl::sqrt((m.
e(i,i) - (m.
e(j,j) + m.
e(k,k))) + (T_Scalar)1.0);
582 q[i] = s * (T_Scalar)0.5;
585 s = (T_Scalar)0.5 / s;
587 q[3] = (m.
e(k,j) - m.
e(j,k)) * s;
588 q[j] = (m.
e(j,i) + m.
e(i,j)) * s;
589 q[k] = (m.
e(k,i) + m.
e(i,k)) * s;
600 template<
typename T_Scalar>
604 T_Scalar sa2 =
sin(degrees * (T_Scalar)0.5);
610 mXYZW.w() =
cos(degrees * (T_Scalar)0.5);
614 template<
typename T_Scalar>
617 T_Scalar iscale =
sqrt(
x()*
x() +
y()*
y() +
z()*
z() );
627 iscale = T_Scalar(1.0) / iscale;
628 axis.
x() =
x() * iscale;
629 axis.
y() =
y() * iscale;
630 axis.
z() =
z() * iscale;
632 T_Scalar tw =
clamp(
w(),(T_Scalar)-1.0,(T_Scalar)+1.0);
637 template<
typename T_Scalar>
640 T_Scalar x2 =
x() *
x();
641 T_Scalar y2 =
y() *
y();
642 T_Scalar z2 =
z() *
z();
643 T_Scalar xy =
x() *
y();
644 T_Scalar xz =
x() *
z();
645 T_Scalar yz =
y() *
z();
646 T_Scalar wx =
w() *
x();
647 T_Scalar wy =
w() *
y();
648 T_Scalar wz =
w() *
z();
651 (1.0f - 2.0f * (y2 + z2)), (2.0f * (xy - wz)), (2.0f * (xz + wy)), 0.0f,
652 (2.0f * (xy + wz)), (1.0f - 2.0f * (x2 + z2)), (2.0f * (yz - wx)), 0.0f,
653 (2.0f * (xz - wy)), (2.0f * (yz + wx)), (1.0f - 2.0f * (x2 + y2)), 0.0f,
654 0.0f, 0.0f, 0.0f, 1.0f );
657 template<
typename T_Scalar>
664 template<
typename T_Scalar>
667 T_Scalar x2 =
x() *
x();
668 T_Scalar y2 =
y() *
y();
669 T_Scalar z2 =
z() *
z();
670 T_Scalar xy =
x() *
y();
671 T_Scalar xz =
x() *
z();
672 T_Scalar yz =
y() *
z();
673 T_Scalar wx =
w() *
x();
674 T_Scalar wy =
w() *
y();
675 T_Scalar wz =
w() *
z();
678 (1.0f - 2.0f * (y2 + z2)), (2.0f * (xy + wz)), (2.0f * (xz - wy)),
679 (2.0f * (xy - wz)), (1.0f - 2.0f * (x2 + z2)), (2.0f * (yz + wx)),
680 (2.0f * (xz + wy)), (2.0f * (yz - wx)), (1.0f - 2.0f * (x2 + y2)) );
683 template<
typename T_Scalar>
690 template<
typename T_Scalar>
698 template<
typename T_Scalar>
701 T_Scalar scale_a, scale_b, omega, sinom;
702 T_Scalar cosom = a.
dot(b);
713 cosom = cosom > (T_Scalar)1 ? (T_Scalar)1 : cosom;
715 if( cosom < (T_Scalar)1.0 )
720 scale_a =
sin(((T_Scalar)1.0-t) * omega) / sinom;
721 scale_b =
sin(t * omega) / sinom;
726 scale_a = (T_Scalar)1.0 - t;
730 return out = (a*scale_a) + (b2*scale_b);
733 template<
typename T_Scalar>
747 cosa =
clamp(cosa, (T_Scalar)-1.0, (T_Scalar)+1.0);
748 T_Scalar alpha =
acos( cosa );
static Quaternion getFromMatrix(const Matrix4< T_Scalar > &m)
Converts the given rotation matrix into a quaternion.
const T_Scalar & z() const
float clamp(float x, float minval, float maxval)
Quaternion operator/(T_Scalar val) const
const Vector3 & normalize(T_Scalar *len=NULL)
Quaternion & operator+=(const Quaternion &q)
Quaternion & operator*=(T_Scalar val)
const T_Scalar & x() const
Quaternion(const Quaternion &quat)
Copy-constructor.
const T_Scalar & w() const
static Quaternion getZero()
Returns the zero quaternion.
static Quaternion & getNoRotation(Quaternion &q)
Returns the no-rotation quaternion, i.e. Quaternion(0,0,0,1).
Quaternion & setFromEulerXYZ(T_Scalar degX, T_Scalar degY, T_Scalar degZ)
Quaternion(const Quaternion< T > &q)
const T_Scalar & z() const
static Quaternion getFromEulerXYZ(T_Scalar degX, T_Scalar degY, T_Scalar degZ)
void toAxisAngle(Vector3< T_Scalar > &axis, T_Scalar °rees) const
Converts a quaternion to an axis-angle representation.
bool operator!=(const Quaternion &q) const
Quaternion & setFromAxisAngle(const Vector3< T_Scalar > &axis, T_Scalar degrees)
Quaternion & setZero()
Sets all the components of the quaternion to zero.
Quaternion & setNoRotation()
Set the quaternion to no-rotation, i.e. Quaternion(0,0,0,1).
Quaternion< float > fquat
Vector4< T_Scalar > & xyzw()
Returns the internal vec4 used to contain the xyzw the quaternion components.
static Quaternion & getFromMatrix(Quaternion &q, const Matrix3< T_Scalar > &m)
Converts the given rotation matrix into a quaternion.
Quaternion< double > dquat
T_Scalar lengthSquared() const
Returns the squared length of a quaternion.
Vector3< T_Scalar > xyz() const
const T_Scalar & e(int i, int j) const
Quaternion operator*(T_Scalar val) const
const double dRAD_TO_DEG
Constant to convert radian into degree using double precision.
Quaternion & getInverse(Quaternion &q) const
Returns the inverse of a quaternion.
The Vector4 class is a template class that implements a generic 4 components vector, see also vl::fvec4, vl::dvec4, vl::uvec4, vl::ivec4, vl::svec4, vl::usvec4, vl::bvec4, vl::ubvec4.
fvec3 cross(const fvec3 &v1, const fvec3 &v2)
Quaternion & setFromMatrix(const Matrix4< T_Scalar > &m)
Creates a quaternion representing the given rotation matrix.
const T_Scalar & x() const
static Quaternion getFromEulerZYX(T_Scalar degZ, T_Scalar degY, T_Scalar degX)
Quaternion(T_Scalar x, T_Scalar y, T_Scalar z, T_Scalar w)
Constructor.
static Quaternion & getZero(Quaternion &q)
Returns the zero quaternion.
const Vector4< T_Scalar > & xyzw() const
Returns the internal vec4 used to contain the xyzw the quaternion components.
Quaternion(const Vector4< T_Scalar > &v)
Constructor from vec4.
bool operator<(const Quaternion &other) const
Lexicographic ordering.
Implements a Quaternion usually used to represent rotations and orientations.
Visualization Library main namespace.
float dot(float a, float b)
const double dDEG_TO_RAD
Constant to convert degree into radian using double precision.
Quaternion getNormalized(T_Scalar *len=NULL) const
Returns the normalized version of a quaternion.
Quaternion & normalize(T_Scalar *len=NULL)
Normalizes a quaternion.
The Matrix3 class is a template class that implements a generic 3x3 matrix, see also vl::dmat3...
static Quaternion getNoRotation()
Returns the no-rotation quaternion, i.e. Quaternion(0,0,0,1).
Matrix3< T_Scalar > get3x3() const
static Quaternion getFromVectors(const Vector3< T_Scalar > &from, const Vector3< T_Scalar > &to)
Sets the quaternion to represent the rotation transforming from into to.
static Quaternion & getFromVectors(Quaternion &q, const Vector3< T_Scalar > &from, const Vector3< T_Scalar > &to)
Sets the quaternion to represent the rotation transforming from into to.
Quaternion operator+(const Quaternion &q) const
The Vector3 class is a template class that implements a generic 3 components vector, see also vl::fvec3, vl::dvec3, vl::uvec3, vl::ivec3, vl::svec3, vl::usvec3, vl::bvec3, vl::ubvec3.
T_Scalar length() const
Returns the length of a quaternion.
Quaternion & getNormalized(Quaternion &q, T_Scalar *len=NULL) const
Returns the normalized version of a quaternion.
const T_Scalar & z() const
static Quaternion & getFromEulerZYX(Quaternion &q, T_Scalar degZ, T_Scalar degY, T_Scalar degX)
bool operator==(const Quaternion &q) const
Quaternion(T_Scalar degrees, const Vector3< T_Scalar > &axis)
Axis-angle constructor.
Matrix4< T_Scalar > toMatrix4() const
Converts a quaternion to a 4x4 rotation matrix.
Implements the OpenGL Shading Language convenience functions for scalar and vector operations...
static Quaternion & getNlerp(Quaternion &out, T_Scalar t, const Quaternion &a, const Quaternion &b)
Normalized spherical interpolation of two quaternions.
static Quaternion getFromAxisAngle(const Vector3< T_Scalar > &axis, T_Scalar degrees)
const T_Scalar & y() const
static Quaternion & getFromMatrix(Quaternion &q, const Matrix4< T_Scalar > &m)
Converts the given rotation matrix into a quaternion.
const T_Scalar & y() const
Quaternion & setFromEulerZYX(T_Scalar degZ, T_Scalar degY, T_Scalar degX)
static Quaternion getSquad(T_Scalar t, const Quaternion &a, const Quaternion &p, const Quaternion &q, const Quaternion &b)
Spherical cubic interpolation of two quaternions.
Quaternion operator-(const Quaternion &q) const
Quaternion & setFromVectors(const Vector3< T_Scalar > &from, const Vector3< T_Scalar > &to)
Sets the quaternion to represent the rotation transforming from into to.
Quaternion getConjugate() const
Returns the conjugate of a quaternion.
Quaternion & operator-=(const Quaternion &q)
The Matrix4 class is a template class that implements a generic 4x4 matrix, see also vl::dmat4...
const T_Scalar & y() const
Quaternion & getConjugate(Quaternion &q) const
Returns the conjugate of a quaternion.
Vector4< T_Scalar > mXYZW
Matrix3< T_Scalar > toMatrix3() const
Converts a quaternion to a 3x3 rotation matrix.
Quaternion & operator/=(T_Scalar val)
static Quaternion & getSquad(Quaternion &out, T_Scalar t, const Quaternion &a, const Quaternion &p, const Quaternion &q, const Quaternion &b)
Spherical cubic interpolation of two quaternions.
const T_Scalar & x() const
Quaternion & operator=(const Quaternion &q)
Assignment operator.
static Quaternion getFromMatrix(const Matrix3< T_Scalar > &m)
Converts the given rotation matrix into a quaternion.
static Quaternion getNlerp(T_Scalar t, const Quaternion &a, const Quaternion &b)
Normalized spherical interpolation of two quaternions.
static Quaternion & getFromAxisAngle(Quaternion &q, const Vector3< T_Scalar > &axis, T_Scalar degrees)
T_Scalar dot(const Quaternion &q) const
Returns the dot product between a quaternion and the given quaternion.
static Quaternion & getFromEulerXYZ(Quaternion &q, T_Scalar degX, T_Scalar degY, T_Scalar degZ)
Quaternion operator-() const
Returns the negated quaternion.
Quaternion & operator=(const Vector4< T_Scalar > &v)
Assignment operator for vec4.
const T_Scalar & w() const
static Quaternion getSlerp(T_Scalar t, const Quaternion &a, const Quaternion &b)
Spherical linear interpolation of two quaternions.
Quaternion getInverse() const
Returns the inverse of a quaternion.