32 #ifndef Quaternion_INCLUDE_ONCE 33 #define Quaternion_INCLUDE_ONCE 43 template<
typename T_Scalar>
83 mXYZW.x() = (T_Scalar)v.
x();
84 mXYZW.y() = (T_Scalar)v.
y();
85 mXYZW.z() = (T_Scalar)v.
z();
86 mXYZW.w() = (T_Scalar)v.
w();
102 mXYZW.x() = (T_Scalar)v.
x();
103 mXYZW.y() = (T_Scalar)v.
y();
104 mXYZW.z() = (T_Scalar)v.
z();
105 mXYZW.w() = (T_Scalar)v.
w();
111 return x() == q.
x() &&
y() == q.
y() &&
z() == q.
z() &&
w() == q.
w();
122 if (
x() != other.
x())
123 return x() < other.
x();
124 if (
y() != other.
y())
125 return y() < other.
y();
126 if (
z() != other.
z())
127 return z() < other.
z();
129 return w() < other.
w();
146 const T_Scalar&
x()
const {
return mXYZW.x(); }
148 const T_Scalar&
y()
const {
return mXYZW.y(); }
150 const T_Scalar&
z()
const {
return mXYZW.z(); }
152 const T_Scalar&
w()
const {
return mXYZW.w(); }
176 val = (T_Scalar)1.0 / val;
186 val = (T_Scalar)1.0 / val;
333 return Quaternion().setFromEulerXYZ(degX, degY, degZ);
345 return Quaternion().setFromEulerZYX(degZ, degY, degX);
357 return Quaternion().setFromAxisAngle(axis, degrees);
383 return x()*q.
x() +
y()*q.
y() +
z()*q.
z() +
w()*q.
w();
404 return x()*
x() +
y()*
y() +
z()*
z() +
w()*
w();
470 out = a + (b - a) * t;
480 template<
typename T_Scalar>
486 template<
typename T_Scalar>
490 q.
x() = q1.
w() * q2.
x() + q1.
x() * q2.
w() + q1.
y() * q2.
z() - q1.
z() * q2.
y();
491 q.
y() = q1.
w() * q2.
y() + q1.
y() * q2.
w() + q1.
z() * q2.
x() - q1.
x() * q2.
z();
492 q.
z() = q1.
w() * q2.
z() + q1.
z() * q2.
w() + q1.
x() * q2.
y() - q1.
y() * q2.
x();
493 q.
w() = q1.
w() * q2.
w() - q1.
x() * q2.
x() - q1.
y() * q2.
y() - q1.
z() * q2.
z();
498 template<
typename T_Scalar>
502 T_Scalar x2 = q.
x() * q.
x();
503 T_Scalar y2 = q.
y() * q.
y();
504 T_Scalar z2 = q.
z() * q.
z();
505 T_Scalar xy = q.
x() * q.
y();
506 T_Scalar xz = q.
x() * q.
z();
507 T_Scalar yz = q.
y() * q.
z();
508 T_Scalar wx = q.
w() * q.
x();
509 T_Scalar wy = q.
w() * q.
y();
510 T_Scalar wz = q.
w() * q.
z();
513 r.
x() = ( v.
x()*(1.0f - 2.0f * (y2 + z2)) + v.
y()*(2.0f * (xy - wz)) + v.
z()*(2.0f * (xz + wy)) );
514 r.
y() = ( v.
x()*(2.0f * (xy + wz)) + v.
y()*(1.0f - 2.0f * (x2 + z2)) + v.
z()*(2.0f * (yz - wx)) );
515 r.
z() = ( v.
x()*(2.0f * (xz - wy)) + v.
y()*(2.0f * (yz + wx)) + v.
z()*(1.0f - 2.0f * (x2 + y2)) );
520 template<
typename T_Scalar>
526 template<
typename T_Scalar>
533 template<
typename T_Scalar>
540 template<
typename T_Scalar>
546 template<
typename T_Scalar>
549 T_Scalar tr, s, q[4];
551 int next[3] = {1, 2, 0};
553 tr = m.
e(0,0) + m.
e(1,1) + m.
e(2,2);
556 if (tr + (T_Scalar)1.0 > 0.0)
559 w() = s / (T_Scalar)2.0;
560 s = (T_Scalar)0.5 / s;
561 x() = (m.
e(2,1) - m.
e(1,2)) * s;
562 y() = (m.
e(0,2) - m.
e(2,0)) * s;
563 z() = (m.
e(1,0) - m.
e(0,1)) * s;
570 if (m.
e(1,1) > m.
e(0,0)) i = 1;
571 if (m.
e(2,2) > m.
e(i,i)) i = 2;
575 s =
vl::sqrt((m.
e(i,i) - (m.
e(j,j) + m.
e(k,k))) + (T_Scalar)1.0);
577 q[i] = s * (T_Scalar)0.5;
580 s = (T_Scalar)0.5 / s;
582 q[3] = (m.
e(k,j) - m.
e(j,k)) * s;
583 q[j] = (m.
e(j,i) + m.
e(i,j)) * s;
584 q[k] = (m.
e(k,i) + m.
e(i,k)) * s;
595 template<
typename T_Scalar>
599 T_Scalar sa2 =
sin(degrees * (T_Scalar)0.5);
605 mXYZW.w() =
cos(degrees * (T_Scalar)0.5);
609 template<
typename T_Scalar>
612 T_Scalar iscale =
sqrt(
x()*
x() +
y()*
y() +
z()*
z() );
622 iscale = T_Scalar(1.0) / iscale;
623 axis.
x() =
x() * iscale;
624 axis.
y() =
y() * iscale;
625 axis.
z() =
z() * iscale;
627 T_Scalar tw =
clamp(
w(),(T_Scalar)-1.0,(T_Scalar)+1.0);
632 template<
typename T_Scalar>
635 T_Scalar x2 =
x() *
x();
636 T_Scalar y2 =
y() *
y();
637 T_Scalar z2 =
z() *
z();
638 T_Scalar xy =
x() *
y();
639 T_Scalar xz =
x() *
z();
640 T_Scalar yz =
y() *
z();
641 T_Scalar wx =
w() *
x();
642 T_Scalar wy =
w() *
y();
643 T_Scalar wz =
w() *
z();
646 (1.0f - 2.0f * (y2 + z2)), (2.0f * (xy - wz)), (2.0f * (xz + wy)), 0.0f,
647 (2.0f * (xy + wz)), (1.0f - 2.0f * (x2 + z2)), (2.0f * (yz - wx)), 0.0f,
648 (2.0f * (xz - wy)), (2.0f * (yz + wx)), (1.0f - 2.0f * (x2 + y2)), 0.0f,
649 0.0f, 0.0f, 0.0f, 1.0f );
652 template<
typename T_Scalar>
659 template<
typename T_Scalar>
662 T_Scalar x2 =
x() *
x();
663 T_Scalar y2 =
y() *
y();
664 T_Scalar z2 =
z() *
z();
665 T_Scalar xy =
x() *
y();
666 T_Scalar xz =
x() *
z();
667 T_Scalar yz =
y() *
z();
668 T_Scalar wx =
w() *
x();
669 T_Scalar wy =
w() *
y();
670 T_Scalar wz =
w() *
z();
673 (1.0f - 2.0f * (y2 + z2)), (2.0f * (xy + wz)), (2.0f * (xz - wy)),
674 (2.0f * (xy - wz)), (1.0f - 2.0f * (x2 + z2)), (2.0f * (yz + wx)),
675 (2.0f * (xz + wy)), (2.0f * (yz - wx)), (1.0f - 2.0f * (x2 + y2)) );
678 template<
typename T_Scalar>
685 template<
typename T_Scalar>
693 template<
typename T_Scalar>
696 T_Scalar scale_a, scale_b, omega, sinom;
697 T_Scalar cosom = a.
dot(b);
708 cosom = cosom > (T_Scalar)1 ? (T_Scalar)1 : cosom;
710 if( cosom < (T_Scalar)1.0 )
715 scale_a =
sin(((T_Scalar)1.0-t) * omega) / sinom;
716 scale_b =
sin(t * omega) / sinom;
721 scale_a = (T_Scalar)1.0 - t;
725 return out = (a*scale_a) + (b2*scale_b);
728 template<
typename T_Scalar>
742 cosa =
clamp(cosa, (T_Scalar)-1.0, (T_Scalar)+1.0);
743 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
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)
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
Quaternion(const Quaternion< T > &quat)
Copy-constructor.
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.