Implements the quaternion operations. More...
#include <geometry/quaternion.h>
Public Member Functions | |
double | angle () const |
Returns the angle of the rotation corresponding to this quaternion. | |
Point3d | axis () const |
Returns the axis of the rotation corresponding to this quaternion. | |
Quaternion | conjugate () const |
Return the conjugate of the current quaternion. | |
Quaternion | inverse () const |
Return the quaternion corresponding to the inverse transform. | |
Point3d | inverseRotate (const Point3d &v) const |
Apply the inverse of the rotation contained in this quaternion on the vector. | |
Quaternion | operator* (const Quaternion &other) const |
Quaternion multiplication. | |
Quaternion & | operator*= (double s) |
In-place multiplication of a quaternion by a scalar. | |
Quaternion & | operator*= (const Quaternion &other) |
Quaternion in-place multiplication. | |
Quaternion | operator+ (const Quaternion &other) const |
Quaternion addition. | |
Quaternion & | operator+= (const Quaternion &other) |
Quaternion in-place addition. | |
Quaternion | operator/ (double v) const |
Division of a quaternion by a real number. | |
Quaternion & | operator/= (double v) |
Division of a quaternion by a real number. | |
Quaternion & | operator= (const Quaternion &other) |
Assignment operator for quaternions. | |
Quaternion (const Matrix3d &m) | |
Creates the quaternion corresponding to the rotation matrix m . | |
Quaternion (const Point3d &from, const Point3d &to) | |
Creates the quaternion corresponding to the rotation transforming from into to . | |
Quaternion (const Point3d &axis, double angle) | |
Creates a Quaternion corresponding to an axis rotation. | |
Quaternion (const Quaternion &other) | |
Copy constructor. | |
Quaternion (double x, double y, double z, double w) | |
Creates a quaternion specified by its components. | |
Quaternion () | |
Default constructor. | |
Point3d | rotate (const Point3d &v) const |
Apply the rotation contained in this quaternion on the vector. | |
void | setAxisAngle (const Point3d &axis, double angle) |
Set the quaternion to the described rotation. | |
void | setMatrix (Matrix4d &m) const |
Fill the matrix as argument from the quaternion. | |
void | setMatrix (Matrix3d &m) const |
Fill the matrix as argument from the quaternion. | |
const double & | w () const |
Accessing the real part of the quaternion. | |
double & | w () |
Accessing the real part of the quaternion. |
Implements the quaternion operations.
Definition at line 17 of file quaternion.h.
geometry::Quaternion::Quaternion | ( | ) | [inline] |
Default constructor.
Provides an identity quaternion
Definition at line 24 of file quaternion.h.
Referenced by conjugate(), inverse(), and Quaternion().
00025 : Point4d(0,0,0,1) 00026 {}
geometry::Quaternion::Quaternion | ( | double | x, | |
double | y, | |||
double | z, | |||
double | w | |||
) | [inline] |
geometry::Quaternion::Quaternion | ( | const Quaternion & | other | ) | [inline] |
Quaternion::Quaternion | ( | const Point3d & | axis, | |
double | angle | |||
) |
Creates a Quaternion corresponding to an axis rotation.
axis | Axis of the rotation. It needs not be normalized before hand. If it is null, then the Quaternion will correspond to the identity matrix. | |
angle | Angle of the rotation. |
Definition at line 55 of file quaternion.cpp.
00059 : Point4d()
Creates the quaternion corresponding to the rotation transforming from
into to
.
Definition at line 4 of file quaternion.cpp.
References Quaternion().
00006 { 00007 Quaternion::Quaternion(const Point3d& from, const Point3d& to) 00008 : Point4d(0,0,0,1) 00009 { 00010 const double fromSqNorm = util::normsq(from); 00011 const double toSqNorm = util::normsq(to); 00012 // Identity Quaternion when one vector is null 00013 if ((fromSqNorm < VVE_EPSILON) || (toSqNorm < VVE_EPSILON)) 00014 { 00015 return; 00016 } 00017 else 00018 { 00019 Point3d axis = from ^ to; 00020 const double axisSqNorm = util::normsq(axis); 00021 00022 // Aligned vectors, pick any axis, not aligned with from or to 00023 if (axisSqNorm < VVE_EPSILON) 00024 axis = util::orthogonal(from); 00025 00026 double angle = std::asin(std::sqrt(axisSqNorm / (fromSqNorm * toSqNorm))); 00027 00028 if (from*to < 0.0) 00029 angle = M_PI-angle; 00030
Quaternion::Quaternion | ( | const Matrix3d & | m | ) |
Creates the quaternion corresponding to the rotation matrix m
.
Definition at line 81 of file quaternion.cpp.
00085 { 00086 // Compute one plus the trace of the matrix 00087 const double onePlusTrace = 1.0 + m.trace(); 00088 if(onePlusTrace > VVE_EPSILON) 00089 { 00090 const double s = std::sqrt(onePlusTrace)*2.0; 00091 x() = (m(2,1) - m(1,2))/s; 00092 y() = (m(0,2) - m(2,0))/s; 00093 z() = (m(1,0) - m(0,1))/s; 00094 w() = 0.25*s; 00095 } 00096 else 00097 { 00098 // Computation depends on major diagonal term 00099 if(m(0,0) > m(1,1) and m(0,0) > m(2,2)) 00100 { 00101 const double s = std::sqrt(1+m(0,0)-m(1,1)-m(2,2))*2.0; 00102 x() = 0.25*s; 00103 y() = (m(1,0)+m(0,1))/s; 00104 z() = (m(2,0)+m(0,2))/s; 00105 w() = (m(1,2)-m(2,1))/s; 00106 } 00107 else if(m(1,1) > m(2,2)) 00108 { 00109 const double s = std::sqrt(1+m(1,1)-m(0,0)-m(2,2))*2.0; 00110 x() = (m(0,1)+m(1,0))/s; 00111 y() = 0.25*s; 00112 z() = (m(1,2)+m(2,1))/s; 00113 w() = (m(0,2)-m(2,0))/s; 00114 } 00115 else 00116 { 00117 const double s = std::sqrt(1+m(2,2)-m(0,0)-m(1,1))*2.0; 00118 x() = (m(0,2)+m(0,0))/s; 00119 y() = (m(1,2)+m(2,1))/s; 00120 z() = 0.25*s; 00121 w() = (m(0,1)-m(1,0))/s; 00122 }
double Quaternion::angle | ( | ) | const |
Returns the angle of the rotation corresponding to this quaternion.
Definition at line 174 of file quaternion.cpp.
00174 :-res; 00175 } 00176 00177 double Quaternion::angle() const 00178 {
Point3d Quaternion::axis | ( | ) | const |
Quaternion geometry::Quaternion::conjugate | ( | ) | const [inline] |
Return the conjugate of the current quaternion.
Definition at line 126 of file quaternion.h.
References Quaternion(), w(), util::Vector< dim, T >::x(), util::Vector< dim, T >::y(), and util::Vector< dim, T >::z().
00126 { return Quaternion(-x(), -y(), -z(), w()); }
Quaternion geometry::Quaternion::inverse | ( | ) | const [inline] |
Return the quaternion corresponding to the inverse transform.
Definition at line 121 of file quaternion.h.
References util::Vector< dim, T >::normsq(), Quaternion(), w(), util::Vector< dim, T >::x(), util::Vector< dim, T >::y(), and util::Vector< dim, T >::z().
00121 { double n = util::normsq(*this); return Quaternion(-x()/n, -y()/n, -z()/n, w()/n); }
Apply the inverse of the rotation contained in this quaternion on the vector.
It is identical to calling this->inverse()
.rotate()
Definition at line 204 of file quaternion.cpp.
Quaternion Quaternion::operator* | ( | const Quaternion & | other | ) | const |
Quaternion multiplication.
Definition at line 38 of file quaternion.cpp.
Quaternion& geometry::Quaternion::operator*= | ( | double | s | ) | [inline] |
In-place multiplication of a quaternion by a scalar.
Definition at line 115 of file quaternion.h.
References util::Vector< dim, T >::i().
Quaternion & Quaternion::operator*= | ( | const Quaternion & | other | ) |
Quaternion in-place multiplication.
Definition at line 48 of file quaternion.cpp.
00052 { 00053 Quaternion res = *this * other;
Quaternion geometry::Quaternion::operator+ | ( | const Quaternion & | other | ) | const [inline] |
Quaternion addition.
Definition at line 96 of file quaternion.h.
00097 { 00098 Quaternion tmp(*this); 00099 tmp += other; 00100 return tmp; 00101 }
Quaternion& geometry::Quaternion::operator+= | ( | const Quaternion & | other | ) | [inline] |
Quaternion in-place addition.
Definition at line 87 of file quaternion.h.
References util::Vector< dim, T >::i().
Quaternion geometry::Quaternion::operator/ | ( | double | v | ) | const [inline] |
Division of a quaternion by a real number.
Definition at line 140 of file quaternion.h.
00141 { 00142 Quaternion tmp(*this); 00143 tmp /= v; 00144 return tmp; 00145 }
Quaternion& geometry::Quaternion::operator/= | ( | double | v | ) | [inline] |
Division of a quaternion by a real number.
Definition at line 131 of file quaternion.h.
References util::Vector< dim, T >::i().
Quaternion & Quaternion::operator= | ( | const Quaternion & | other | ) |
Apply the rotation contained in this quaternion on the vector.
Definition at line 180 of file quaternion.cpp.
00180 : 2.0*M_PI-a; 00181 } 00182 00183 Point3d Quaternion::rotate(const Point3d& v) const 00184 { 00185 /* 00186 Quaternion q(v.x(), v.y(), v.z(), 0); 00187 Quaternion result = *this * q * conjugate(); 00188 return Point3d(result.x(), result.y(), result.z()); 00189 */ 00190 00191 const double xx = 2.0l*x()*x(); 00192 const double yy = 2.0l*y()*y(); 00193 const double zz = 2.0l*z()*z(); 00194 const double xy = 2.0l*x()*y(); 00195 const double xz = 2.0l*x()*z(); 00196 const double yz = 2.0l*y()*z(); 00197 00198 const double wx = 2.0l*w()*x(); 00199 const double wy = 2.0l*w()*y(); 00200 const double wz = 2.0l*w()*z(); 00201 00202 return Point3d((1.0-yy-zz)*v.x() + ( xy-wz)*v.y() + ( xz+wy)*v.z(),
void Quaternion::setAxisAngle | ( | const Point3d & | axis, | |
double | angle | |||
) |
Set the quaternion to the described rotation.
axis | Axis of the rotation | |
angle | Angle of the rotation |
Definition at line 61 of file quaternion.cpp.
00065 { 00066 const double norm = util::norm(axis); 00067 if(norm < VVE_EPSILON) 00068 { 00069 x() = 0; 00070 y() = 0; 00071 z() = 0; 00072 w() = 1; 00073 } 00074 else 00075 { 00076 const double sin_half_angle = std::sin(angle/2.0); 00077 x() = sin_half_angle*axis.x()/norm; 00078 y() = sin_half_angle*axis.y()/norm; 00079 z() = sin_half_angle*axis.z()/norm;
void geometry::Quaternion::setMatrix | ( | Matrix4d & | m | ) | const |
Fill the matrix as argument from the quaternion.
Multiplying with this matrix is equivalent to performing a rotation with this quaternion.
void Quaternion::setMatrix | ( | Matrix3d & | m | ) | const |
Fill the matrix as argument from the quaternion.
Multiplying with this matrix is equivalent to performing a rotation with this quaternion.
Definition at line 149 of file quaternion.cpp.
const double& geometry::Quaternion::w | ( | ) | const [inline] |
Accessing the real part of the quaternion.
Definition at line 82 of file quaternion.h.
double& geometry::Quaternion::w | ( | ) | [inline] |
Accessing the real part of the quaternion.
Definition at line 78 of file quaternion.h.
Referenced by conjugate(), and inverse().