8 #include "Quaternion.h"
18 Quaternion::Quaternion()
24 Quaternion::Quaternion(qreal w, qreal x, qreal y, qreal z)
32 Quaternion Quaternion::fromSpherical(qreal lon, qreal lat)
35 const qreal x = cos(lat) * sin(lon);
36 const qreal y = sin(lat);
37 const qreal z = cos(lat) * cos(lon);
39 return Quaternion( w, x, y, z );
42 void Quaternion::getSpherical(qreal &lon, qreal &lat)
const
52 if(v[Q_X] * v[Q_X] + v[Q_Z] * v[Q_Z] > 0.00005)
53 lon = atan2(v[Q_X], v[Q_Z]);
58 void Quaternion::normalize()
60 (*this) *= 1.0 / length();
63 qreal Quaternion::length()
const
65 return sqrt(v[Q_W] * v[Q_W] + v[Q_X] * v[Q_X] + v[Q_Y] * v[Q_Y] + v[Q_Z] * v[Q_Z]);
68 Quaternion& Quaternion::operator*=(qreal mult)
70 (*this) = (*this) * mult;
75 Quaternion Quaternion::inverse()
const
77 Quaternion inverse( v[Q_W], -v[Q_X], -v[Q_Y], -v[Q_Z] );
83 Quaternion Quaternion::log()
const
85 double const qlen = length();
86 double const vlen = sqrt(v[Q_X]*v[Q_X] + v[Q_Y]*v[Q_Y] + v[Q_Z]*v[Q_Z]);
87 double const a = acos(v[Q_W]/qlen) / vlen;
88 return Quaternion(std::log(qlen), v[Q_X] * a, v[Q_Y] * a, v[Q_Z] * a);
91 Quaternion Quaternion::exp()
const
93 double const vlen = sqrt(v[Q_X]*v[Q_X] + v[Q_Y]*v[Q_Y] + v[Q_Z]*v[Q_Z]);
94 double const s = std::exp(v[Q_W]);
95 double const a = s * sin(vlen) / vlen;
96 return Quaternion(s * cos(vlen), v[Q_X] * a, v[Q_Y] * a, v[Q_Z] * a);
99 Quaternion Quaternion::fromEuler(qreal pitch, qreal yaw, qreal roll)
101 const qreal cPhi = cos(0.5 * pitch);
102 const qreal cThe = cos(0.5 * yaw);
103 const qreal cPsi = cos(0.5 * roll);
105 const qreal sPhi = sin(0.5 * pitch);
106 const qreal sThe = sin(0.5 * yaw);
107 const qreal sPsi = sin(0.5 * roll);
109 const qreal w = cPhi * cThe * cPsi + sPhi * sThe * sPsi;
110 const qreal x = sPhi * cThe * cPsi - cPhi * sThe * sPsi;
111 const qreal y = cPhi * sThe * cPsi + sPhi * cThe * sPsi;
112 const qreal z = cPhi * cThe * sPsi - sPhi * sThe * cPsi;
114 return Quaternion( w, x, y, z );
117 qreal Quaternion::pitch() const
119 return atan2( 2.0*(v[Q_X]*v[Q_W]-v[Q_Y]*v[Q_Z]),
120 ( 1.0 - 2.0*(v[Q_X]*v[Q_X]+v[Q_Z]*v[Q_Z]) ) );
123 qreal Quaternion::yaw() const
125 return atan2( 2.0*(v[Q_Y]*v[Q_W]-v[Q_X]*v[Q_Z]),
126 ( 1.0 - 2.0*(v[Q_Y]*v[Q_Y]+v[Q_Z]*v[Q_Z]) ) );
129 qreal Quaternion::roll() const
131 return asin(2.0*(v[Q_X]*v[Q_Y]+v[Q_Z]*v[Q_W]));
134 #ifndef QT_NO_DEBUG_STREAM
137 QString quatdisplay =
QString(
"Quaternion: w= %1, x= %2, y= %3, z= %4, |q|= %5" )
138 .
arg(q.v[Q_W]).
arg(q.v[Q_X]).
arg(q.v[Q_Y]).
arg(q.v[Q_Z]).
arg(q.length());
140 debug << quatdisplay;
146 Quaternion& Quaternion::operator*=(
const Quaternion &q)
148 (*this) = (*this) * q;
153 bool Quaternion::operator==(
const Quaternion &q)
const
156 return ( v[Q_W] == q.v[Q_W]
157 && v[Q_X] == q.v[Q_X]
158 && v[Q_Y] == q.v[Q_Y]
159 && v[Q_Z] == q.v[Q_Z] );
162 Quaternion Quaternion::operator*(
const Quaternion &q)
const
164 const qreal w = v[Q_W] * q.v[Q_W] - v[Q_X] * q.v[Q_X] - v[Q_Y] * q.v[Q_Y] - v[Q_Z] * q.v[Q_Z];
165 const qreal x = v[Q_W] * q.v[Q_X] + v[Q_X] * q.v[Q_W] + v[Q_Y] * q.v[Q_Z] - v[Q_Z] * q.v[Q_Y];
166 const qreal y = v[Q_W] * q.v[Q_Y] - v[Q_X] * q.v[Q_Z] + v[Q_Y] * q.v[Q_W] + v[Q_Z] * q.v[Q_X];
167 const qreal z = v[Q_W] * q.v[Q_Z] + v[Q_X] * q.v[Q_Y] - v[Q_Y] * q.v[Q_X] + v[Q_Z] * q.v[Q_W];
169 return Quaternion( w, x, y, z );
172 Quaternion Quaternion::operator+(
const Quaternion &q)
const
174 return Quaternion(v[Q_W] + q.v[Q_W],
180 Quaternion Quaternion::operator*(qreal factor)
const
182 return Quaternion( v[Q_W] * factor, v[Q_X] * factor, v[Q_Y] * factor, v[Q_Z] * factor );
185 void Quaternion::rotateAroundAxis(
const Quaternion &q)
187 const qreal w = + v[Q_X] * q.v[Q_X] + v[Q_Y] * q.v[Q_Y] + v[Q_Z] * q.v[Q_Z];
188 const qreal x = + v[Q_X] * q.v[Q_W] - v[Q_Y] * q.v[Q_Z] + v[Q_Z] * q.v[Q_Y];
189 const qreal y = + v[Q_X] * q.v[Q_Z] + v[Q_Y] * q.v[Q_W] - v[Q_Z] * q.v[Q_X];
190 const qreal z = - v[Q_X] * q.v[Q_Y] + v[Q_Y] * q.v[Q_X] + v[Q_Z] * q.v[Q_W];
192 (*this) = q * Quaternion( w, x, y, z );
195 Quaternion Quaternion::slerp(
const Quaternion &q1,
const Quaternion &q2, qreal t)
201 qreal cosAlpha = ( q1.v[Q_X] * q2.v[Q_X]
202 + q1.v[Q_Y] * q2.v[Q_Y]
203 + q1.v[Q_Z] * q2.v[Q_Z]
204 + q1.v[Q_W] * q2.v[Q_W] );
205 qreal alpha = acos( cosAlpha );
206 qreal sinAlpha = sin( alpha );
208 if ( sinAlpha > 0.0 ) {
209 p1 = sin( ( 1.0 - t ) * alpha ) / sinAlpha;
210 p2 = sin( t * alpha ) / sinAlpha;
217 const qreal w = p1 * q1.v[Q_W] + p2 * q2.v[Q_W];
218 const qreal x = p1 * q1.v[Q_X] + p2 * q2.v[Q_X];
219 const qreal y = p1 * q1.v[Q_Y] + p2 * q2.v[Q_Y];
220 const qreal z = p1 * q1.v[Q_Z] + p2 * q2.v[Q_Z];
222 return Quaternion( w, x, y, z );
225 Quaternion Quaternion::nlerp(
const Quaternion &q1,
const Quaternion &q2, qreal t)
227 const qreal p1 = 1.0 - t;
229 const qreal w = p1 * q1.v[Q_W] + t * q2.v[Q_W];
230 const qreal x = p1 * q1.v[Q_X] + t * q2.v[Q_X];
231 const qreal y = p1 * q1.v[Q_Y] + t * q2.v[Q_Y];
232 const qreal z = p1 * q1.v[Q_Z] + t * q2.v[Q_Z];
234 Quaternion result( w, x, y, z );
240 void Quaternion::toMatrix(matrix &m)
const
243 const qreal xy = v[Q_X] * v[Q_Y], xz = v[Q_X] * v[Q_Z];
244 const qreal yy = v[Q_Y] * v[Q_Y], yw = v[Q_Y] * v[Q_W];
245 const qreal zw = v[Q_Z] * v[Q_W], zz = v[Q_Z] * v[Q_Z];
247 m[0][0] = 1.0 - 2.0 * (yy + zz);
248 m[0][1] = 2.0 * (xy + zw);
249 m[0][2] = 2.0 * (xz - yw);
252 const qreal xx = v[Q_X] * v[Q_X];
253 const qreal xw = v[Q_X] * v[Q_W];
254 const qreal yz = v[Q_Y] * v[Q_Z];
256 m[1][0] = 2.0 * (xy - zw);
257 m[1][1] = 1.0 - 2.0 * (xx + zz);
258 m[1][2] = 2.0 * (yz + xw);
261 m[2][0] = 2.0 * (xz + yw);
262 m[2][1] = 2.0 * (yz - xw);
263 m[2][2] = 1.0 - 2.0 * (xx + yy);
267 void Quaternion::rotateAroundAxis(
const matrix &m)
269 const qreal x = m[0][0] * v[Q_X] + m[1][0] * v[Q_Y] + m[2][0] * v[Q_Z];
270 const qreal y = m[0][1] * v[Q_X] + m[1][1] * v[Q_Y] + m[2][1] * v[Q_Z];
271 const qreal z = m[0][2] * v[Q_X] + m[1][2] * v[Q_Y] + m[2][2] * v[Q_Z];