66 static const char rcsid[] =
"$Id: quaternion.cpp,v 1.18 2005/11/15 19:25:58 gourdeau Exp $";
73 using namespace NEWMAT;
90 cerr <<
"Quaternion::Quaternion, size of axis != 3" << endl;
99 cerr <<
"Quaternion::Quaternion(angle, axis), axis is not unit" << endl;
100 cerr <<
"Make the axis unit." << endl;
101 v_ = sin(angle/2) * axis/norm_axis;
104 v_ = sin(angle/2) * axis;
165 Real tmp = fabs(R(1,1) + R(2,2) + R(3,3) + 1);
172 v_(1) = (R(3,2)-R(2,3))/(4*s_);
173 v_(2) = (R(1,3)-R(3,1))/(4*s_);
174 v_(3) = (R(2,1)-R(1,2))/(4*s_);
179 static int s_iNext[3] = { 2, 3, 1 };
181 if ( R(2,2) > R(1,1) )
183 if ( R(3,3) > R(2,2) )
185 int j = s_iNext[i-1];
186 int k = s_iNext[j-1];
188 Real fRoot = sqrt(R(i,i)-R(j,j)-R(k,k) + 1.0);
190 Real *tmp[3] = { &v_(1), &v_(2), &v_(3) };
191 *tmp[i-1] = 0.5*fRoot;
193 s_ = (R(k,j)-R(j,k))*fRoot;
194 *tmp[j-1] = (R(j,i)+R(i,j))*fRoot;
195 *tmp[k-1] = (R(k,i)+R(i,k))*fRoot;
200 cerr <<
"Quaternion::Quaternion: matrix input is not 3x3 or 4x4" << endl;
270 return *
this*rhs.
i();
280 cerr <<
"Quaternion::set_v: input has a wrong size." << endl;
333 return conjugate()/norm();
347 sin_theta = sin(theta);
350 if ( fabs(sin_theta) >
EPSILON)
351 q.
v_ = v_*sin_theta/theta;
377 Real theta = acos(s_),
378 sin_theta = sin(theta);
380 if ( fabs(sin_theta) >
EPSILON)
381 q.
v_ = v_/sin_theta*theta;
455 return (s_*q.s_ + v_(1)*q.v_(1) + v_(2)*q.v_(2) + v_(3)*q.v_(3));
591 cerr <<
"Integ_Trap(quat1, quat2, dt): dt < 0. dt is set to 0." << endl;
598 dquat_present.
set_s(dquat_present.
s() );
599 dquat_present.
set_v(dquat_present.
v() );
604 dquat_past.
set_s(dquat_present.
s());
605 dquat_past.
set_v(dquat_present.
v());
616 Real integ = 0.5*(present.
s()+past.
s())*dt;
683 if( (t < 0) || (t > 1) )
684 cerr <<
"Slerp(q0, q1, t): t < 0 or t > 1. t is set to 0." << endl;
687 return q0*((q0.
i()*q1).power(t));
689 return q0*((q0.
i()*-1*q1).power(t));
716 if( (t < 0) || (t > 1) )
717 cerr <<
"Slerp_prime(q0, q1, t): t < 0 or t > 1. t is set to 0." << endl;
720 return Slerp(q0, q1, t)*(q0.
i()*q1).Log();
722 return Slerp(q0, q1, t)*(q0.
i()*-1*q1).Log();
745 if( (t < 0) || (t > 1) )
746 cerr <<
"Squad(p,a,b,q, t): t < 0 or t > 1. t is set to 0." << endl;
794 if( (t < 0) || (t > 1) )
795 cerr <<
"Squad_prime(p,a,b,q, t): t < 0 or t > 1. t is set to 0." << endl;
801 U_prime = U*(p.
i()*q).Log(),
802 V_prime = V*(a.
i()*b).Log(),
803 W_prime = U.
i()*V_prime - U.
power(-2)*U_prime*V;
805 q_squad = U*( W.power(2*t*(1-t))*W.Log()*(2-4*t) + W.power(2*t*(1-t)-1)*W_prime*2*t*(1-t) )
806 + U_prime*( W.power(2*t*(1-t)) );
Quaternion class definition.
ReturnMatrix R() const
Rotation matrix from a unit quaternion.
ReturnMatrix v() const
Return vector part.
Quaternion exp() const
Exponential of a quaternion.
Real DotProduct(const Matrix &A, const Matrix &B)
ReturnMatrix x_prod_matrix(const ColumnVector &x)
Cross product matrix.
Quaternion operator/(const Real c, const Quaternion &q)
Overload / operator, division by a scalar.
Quaternion Slerp(const Quaternion &q0, const Quaternion &q1, const Real t)
Spherical Linear Interpolation.
static const char rcsid[]
RCS/CVS version.
Real s_
Quaternion scalar part.
short Integ_quat(Quaternion &dquat_present, Quaternion &dquat_past, Quaternion &quat, const Real dt)
Trapezoidal quaternion integration.
Real s() const
Return scalar part.
ReturnMatrix Integ_Trap_quat_v(const Quaternion &present, Quaternion &past, const Real dt)
Trapezoidal quaternion vector part integration.
Real Integ_Trap_quat_s(const Quaternion &present, Quaternion &past, const Real dt)
Trapezoidal quaternion scalar part integration.
Quaternion power(const Real t) const
void set_s(const Real s)
Set scalar part.
Quaternion & unit()
Normalize a quaternion.
void set_v(const ColumnVector &v)
Set vector part.
Quaternion conjugate() const
Conjugate.
TransposedMatrix t() const
Quaternion Squad_prime(const Quaternion &p, const Quaternion &a, const Quaternion &b, const Quaternion &q, const Real t)
Spherical Cubic Interpolation derivative.
Quaternion operator+(const Quaternion &q) const
Overload + operator.
Quaternion Slerp_prime(const Quaternion &q0, const Quaternion &q1, const Real t)
Spherical Linear Interpolation derivative.
ReturnMatrix E(const short sign) const
Matrix E.
Matrix CrossProduct(const Matrix &A, const Matrix &B)
Quaternion Log() const
Logarithm of a unit quaternion.
The usual rectangular matrix.
Quaternion dot(const ColumnVector &w, const short sign) const
Quaternion time derivative.
FloatVector FloatVector * a
Real dot_prod(const Quaternion &q) const
Quaternion dot product.
Quaternion Squad(const Quaternion &p, const Quaternion &a, const Quaternion &b, const Quaternion &q, const Real t)
Spherical Cubic Interpolation.
void QRZ(Matrix &X, UpperTriangularMatrix &U)
ColumnVector v_
Quaternion vector part.
Real norm() const
Return the quaternion norm.
Quaternion i() const
Quaternion inverse. where and are the quaternion conjugate and the quaternion norm respectively...
GetSubMatrix SubMatrix(int fr, int lr, int fc, int lc) const
Quaternion operator*(const Real c, const Quaternion &q)
Overload * operator, multiplication by a scalar.
Quaternion operator/(const Quaternion &q) const
Overload / operator.
Real threebythreeident[]
Used to initialize a matrix.
Real fourbyfourident[]
Used to initialize a matrix.
Quaternion operator*(const Quaternion &q) const
Overload * operator.
ReturnMatrix T() const
Transformation matrix from a quaternion.
ReturnMatrix Omega(const Quaternion &q, const Quaternion &q_dot)
Return angular velocity from a quaternion and it's time derivative.
Quaternion operator-(const Quaternion &q) const
Overload - operator.