117 double a_z) : w(a_w), x(a_x), y(a_y), z(a_z) {}
120 cQuaternion(
double const* in) : w(in[0]), x(in[1]), y(in[2]), z(in[3]) {}
130 inline operator double*() {
return &
w;}
133 inline operator double const*()
const {
return &
w;}
138 double neww = w*a_quaternion.
w - x*a_quaternion.
x - y*a_quaternion.
y - z*a_quaternion.
z;
139 double newx = w*a_quaternion.
x + x*a_quaternion.
w + y*a_quaternion.
z - z*a_quaternion.
y;
140 double newy = w*a_quaternion.
y - x*a_quaternion.
z + y*a_quaternion.
w + z*a_quaternion.
x;
141 double newz = w*a_quaternion.
z + x*a_quaternion.
y - y*a_quaternion.
x + z*a_quaternion.
w;
183 return ( (w==a_quaternion.
w) &&
184 (x==a_quaternion.
x) &&
185 (y==a_quaternion.
y) &&
186 (z==a_quaternion.
z) );
197 inline void zero() { w=0.0; x=0.0; y=0.0; z=0.0; }
203 inline double magsq()
const {
return (w*w) + (x*
x) + (y*y) + (z*
z); }
209 inline double mag()
const {
return sqrt(
magsq()); }
250 a_matrix(0,0) = 1.0 - y2 - z2;
251 a_matrix(0,1) = xy - wz;
252 a_matrix(0,2) = xz + wy;
253 a_matrix(1,0) = xy + wz;
254 a_matrix(1,1) = 1.0 - x2 - z2;
255 a_matrix(1,2) = yz - wx;
256 a_matrix(2,0) = xz - wy;
257 a_matrix(2,1) = yz + wx;
258 a_matrix(2,2) = 1.0 - x2 - y2;
276 double trace = 1.0 + a_matrix(0,0) + a_matrix(1,1) + a_matrix(2,2);
280 double s = 2.0*sqrt(trace);
281 x = (a_matrix(2,1) - a_matrix(1,2))/s;
282 y = (a_matrix(0,2) - a_matrix(2,0))/s;
283 z = (a_matrix(1,0) - a_matrix(0,1))/s;
286 else if ((a_matrix(0,0) > a_matrix(1,1)) && (a_matrix(0,0) > a_matrix(2,2)))
289 double s = 2.0*sqrt(1.0+a_matrix(0,0)-a_matrix(1,1)-a_matrix(2,2));
291 y = (a_matrix(1,0) + a_matrix(0,1))/s;
292 z = (a_matrix(0,2) + a_matrix(2,0))/s;
293 w = (a_matrix(2,1) - a_matrix(1,2))/s;
295 else if (a_matrix(1,1) > a_matrix(2,2))
298 double s = 2.0*sqrt(1.0+a_matrix(1,1)-a_matrix(0,0)-a_matrix(2,2));
299 x = (a_matrix(1,0) + a_matrix(0,1))/s;
301 z = (a_matrix(2,1) + a_matrix(1,2))/s;
302 w = (a_matrix(0,2) - a_matrix(2,0))/s;
307 double s = 2.0*sqrt(1.0+a_matrix(2,2)-a_matrix(0,0)-a_matrix(1,1));
308 x = (a_matrix(0,2) + a_matrix(2,0))/s;
309 y = (a_matrix(2,1) + a_matrix(1,2))/s;
311 w = (a_matrix(1,0) - a_matrix(0,1))/s;
334 double sina = sin(a_angleRad / 2.0);
335 double cosa = cos(a_angleRad / 2.0);
337 x = a_axis(0) * sina;
338 y = a_axis(1) * sina;
339 z = a_axis(2) * sina;
361 double cosa = w /
mag();
362 a_angleRad = acos(cosa);
424 double neww = w*a_quaternion.
w - x*a_quaternion.
x - y*a_quaternion.
y - z*a_quaternion.
z;
425 double newx = w*a_quaternion.
x + x*a_quaternion.
w + y*a_quaternion.
z - z*a_quaternion.
y;
426 double newy = w*a_quaternion.
y - x*a_quaternion.
z + y*a_quaternion.
w + z*a_quaternion.
x;
427 double newz = w*a_quaternion.
z + x*a_quaternion.
y - y*a_quaternion.
x + z*a_quaternion.
w;
449 inline void mul(
double a_scale)
476 return (w*a_quaternion.
w + x*a_quaternion.
x + y*a_quaternion.
y + z*a_quaternion.
z);
551 double costheta = a_quaternion0.
dot(a_quaternion1);
552 if ((costheta-1.0) < 1e-4 && (costheta-1.0) > -1e-4)
556 *
this = a_quaternion0;
557 this->
mul(1.0-a_level);
558 a_quaternion1.
mul(a_level);
564 double ratio1, ratio2;
565 if ((costheta+1.0) > -1e-4 && (costheta+1.0) < 1e-4)
569 a_quaternion1.
w = a_quaternion0.
z;
570 a_quaternion1.
x = -a_quaternion0.
y;
571 a_quaternion1.
y = a_quaternion0.
x;
572 a_quaternion1.
z = -a_quaternion0.
w;
573 ratio1 = sin(
C_PI*(0.5-a_level));
574 ratio2 = sin(
C_PI*a_level);
580 costheta = -costheta;
583 double theta = acos(costheta);
584 double sintheta = sin(theta);
586 ratio1 = sin(theta*(1.0-a_level))/sintheta;
587 ratio2 = sin(theta*a_level)/sintheta;
589 *
this = a_quaternion0;
591 a_quaternion1.
mul(ratio2);
611 inline std::string
str(
const unsigned int a_precision = 2)
const 615 cStr(w, a_precision) +
"| " +
616 cStr(x, a_precision) +
", " +
617 cStr(y, a_precision) +
", " +
618 cStr(z, a_precision) +
652 result.
mul(a_quaternion1);
661 result.
add(a_quaternion1);
670 result.
sub(a_quaternion1);
676 static inline std::ostream &operator << (std::ostream &a_os,
cQuaternion const& a_quaternion)
678 a_os << a_quaternion.
str(3);
This class implements a 3D vector.
Definition: CVector3d.h:88
const double C_SMALL
Small value near zero.
Definition: CConstants.h:103
double y
Component y of quaternion.
Definition: CQuaternion.h:98
void fromRotMat(cMatrix3d const &a_matrix)
This method converts a rotation matrix into a quaternion.
Definition: CQuaternion.h:274
void toAxisAngle(cVector3d &a_axis, double &a_angleRad) const
This method converts a quaternion representation into an axis-angle representation.
Definition: CQuaternion.h:359
double lengthsq() const
This method returns the squared value of the quaternion magnitude.
Definition: CQuaternion.h:206
void normalize()
This method normalizes this vector to length 1.
Definition: CVector3d.h:1054
This class implements a quaternion.
Definition: CQuaternion.h:83
cQuaternion operator+(const cQuaternion &a_quaternion0, const cQuaternion &a_quaternion1)
An overloaded + operator for quaternion addition.
Definition: CQuaternion.h:658
void toRotMat(cMatrix3d &a_matrix) const
This method convert this quaternion into a rotation matrix.
Definition: CQuaternion.h:238
cVector3d operator*(const cMatrix3d &a_matrix, const cVector3d &a_vector)
An overloaded * operator for matrix/vector multiplication.
Definition: CMatrix3d.h:2078
std::string cStr(const bool a_value)
This function converts a boolean into a string.
Definition: CString.cpp:189
cQuaternion & operator*=(cQuaternion const &a_quaternion)
*= operator (Grassman product).
Definition: CQuaternion.h:136
void sub(cQuaternion const &a_quaternion)
This method computes the subtraction between this quaternion and another passed as argument...
Definition: CQuaternion.h:517
void invert()
This method inverts this quaternion.
Definition: CQuaternion.h:398
cQuaternion(double a_w, double a_x, double a_y, double a_z)
Constructor of cQuaternion.
Definition: CQuaternion.h:114
const double C_PI
PI constant.
Definition: CConstants.h:88
std::string str(const unsigned int a_precision=2) const
This method converts this quaternion into a string.
Definition: CQuaternion.h:611
This class implements a 3D matrix.
Definition: CMatrix3d.h:97
cQuaternion operator-(const cQuaternion &a_quaternion0, const cQuaternion &a_quaternion1)
An overloaded - operator for quaternion subtraction.
Definition: CQuaternion.h:667
double x
Component x of quaternion.
Definition: CQuaternion.h:95
double magsq() const
This method returns the squared value of the quaternion magnitude.
Definition: CQuaternion.h:203
void conj()
This method computes the conjugate of this quaternion.
Definition: CQuaternion.h:379
void normalize()
This method normalizes this quaternion.
Definition: CQuaternion.h:215
void mul(cQuaternion const &a_quaternion)
This method computes the multiplication of this quaternion with another quaternion.
Definition: CQuaternion.h:422
double dot(cQuaternion const &a_quaternion) const
This method computes the dot product between this quaternion and another quaternion.
Definition: CQuaternion.h:474
void zero()
This method clears this quaternion with zeros.
Definition: CQuaternion.h:197
cQuaternion & operator+=(cQuaternion const &a_quaternion)
+= operator.
Definition: CQuaternion.h:161
double z
Component z of quaternion.
Definition: CQuaternion.h:101
cQuaternion & operator-=(cQuaternion const &a_quaternion)
-= operator.
Definition: CQuaternion.h:171
bool operator==(cQuaternion const &a_quaternion) const
== operator.
Definition: CQuaternion.h:181
void fromAxisAngle(cVector3d a_axis, double a_angleRad)
This method converts an axis-angle representation into a quaternion.
Definition: CQuaternion.h:329
void negate()
This method negates this quaternion.
Definition: CQuaternion.h:200
Definition: CAudioBuffer.cpp:56
double length() const
This method returns the quaternion magnitude.
Definition: CQuaternion.h:212
cQuaternion()
Constructor of cQuaternion.
Definition: CQuaternion.h:111
void mul(double a_scale)
This method multiplies this quaternion by a scalar.
Definition: CQuaternion.h:449
double mag() const
This method returns the quaternion magnitude.
Definition: CQuaternion.h:209
void add(cQuaternion const &a_quaternion)
This method computes the addition between this quaternion and another passed as argument.
Definition: CQuaternion.h:494
void slerp(double a_level, cQuaternion const &a_quaternion0, cQuaternion a_quaternion1)
This method computes a spherical linear interpolation between two quaternions (SLERP).
Definition: CQuaternion.h:545
cQuaternion(double const *in)
Constructor of cQuaternion.
Definition: CQuaternion.h:120
double w
Component w of quaternion.
Definition: CQuaternion.h:92