17Quaternion::Quaternion()
23Quaternion::Quaternion(qreal w, qreal x, qreal y, qreal z)
31Quaternion Quaternion::fromSpherical(qreal lon, qreal lat)
34 const qreal x = cos(lat) * sin(lon);
35 const qreal y = sin(lat);
36 const qreal z = cos(lat) * cos(lon);
41void Quaternion::getSpherical(qreal &lon, qreal &lat)
const
51 if (v[Q_X] * v[Q_X] + v[Q_Z] * v[Q_Z] > 0.00005)
52 lon = atan2(v[Q_X], v[Q_Z]);
57void Quaternion::normalize()
59 (*this) *= 1.0 / length();
62qreal Quaternion::length()
const
64 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]);
67Quaternion &Quaternion::operator*=(qreal mult)
69 (*this) = (*this) * mult;
74Quaternion Quaternion::inverse()
const
76 Quaternion inverse(v[Q_W], -v[Q_X], -v[Q_Y], -v[Q_Z]);
82Quaternion Quaternion::log()
const
84 double const qlen = length();
85 double const vlen = sqrt(v[Q_X] * v[Q_X] + v[Q_Y] * v[Q_Y] + v[Q_Z] * v[Q_Z]);
86 double const a = acos(v[Q_W] / qlen) / vlen;
87 return {std::log(qlen), v[Q_X] * a, v[Q_Y] * a, v[Q_Z] * a};
90Quaternion Quaternion::exp()
const
92 double const vlen = sqrt(v[Q_X] * v[Q_X] + v[Q_Y] * v[Q_Y] + v[Q_Z] * v[Q_Z]);
93 double const s = std::exp(v[Q_W]);
94 double const a = s * sin(vlen) / vlen;
95 return {s * cos(vlen), v[Q_X] * a, v[Q_Y] * a, v[Q_Z] * a};
98Quaternion Quaternion::fromEuler(qreal pitch, qreal yaw, qreal roll)
100 const qreal cPhi = cos(0.5 * pitch);
101 const qreal cThe = cos(0.5 * yaw);
102 const qreal cPsi = cos(0.5 * roll);
104 const qreal sPhi = sin(0.5 * pitch);
105 const qreal sThe = sin(0.5 * yaw);
106 const qreal sPsi = sin(0.5 * roll);
108 const qreal w = cPhi * cThe * cPsi + sPhi * sThe * sPsi;
109 const qreal x = sPhi * cThe * cPsi - cPhi * sThe * sPsi;
110 const qreal y = cPhi * sThe * cPsi + sPhi * cThe * sPsi;
111 const qreal z = cPhi * cThe * sPsi - sPhi * sThe * cPsi;
116qreal Quaternion::pitch() const
118 return atan2(2.0 * (v[Q_X] * v[Q_W] - v[Q_Y] * v[Q_Z]), (1.0 - 2.0 * (v[Q_X] * v[Q_X] + v[Q_Z] * v[Q_Z])));
121qreal Quaternion::yaw() const
123 return atan2(2.0 * (v[Q_Y] * v[Q_W] - v[Q_X] * v[Q_Z]), (1.0 - 2.0 * (v[Q_Y] * v[Q_Y] + v[Q_Z] * v[Q_Z])));
126qreal Quaternion::roll() const
128 return asin(2.0 * (v[Q_X] * v[Q_Y] + v[Q_Z] * v[Q_W]));
131#ifndef QT_NO_DEBUG_STREAM
135 QStringLiteral(
"Quaternion: w= %1, x= %2, y= %3, z= %4, |q|= %5").
arg(q.v[Q_W]).
arg(q.v[Q_X]).
arg(q.v[Q_Y]).
arg(q.v[Q_Z]).
arg(q.length());
137 debug << quatdisplay;
143Quaternion &Quaternion::operator*=(
const Quaternion &q)
145 (*this) = (*this) * q;
150bool Quaternion::operator==(
const Quaternion &q)
const
152 return (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]);
155Quaternion Quaternion::operator*(
const Quaternion &q)
const
157 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];
158 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];
159 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];
160 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];
165Quaternion Quaternion::operator+(
const Quaternion &q)
const
167 return {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]};
170Quaternion Quaternion::operator*(qreal factor)
const
172 return {v[Q_W] * factor, v[Q_X] * factor, v[Q_Y] * factor, v[Q_Z] * factor};
175void Quaternion::rotateAroundAxis(
const Quaternion &q)
177 const qreal w = +v[Q_X] * q.v[Q_X] + v[Q_Y] * q.v[Q_Y] + v[Q_Z] * q.v[Q_Z];
178 const qreal x = +v[Q_X] * q.v[Q_W] - v[Q_Y] * q.v[Q_Z] + v[Q_Z] * q.v[Q_Y];
179 const qreal y = +v[Q_X] * q.v[Q_Z] + v[Q_Y] * q.v[Q_W] - v[Q_Z] * q.v[Q_X];
180 const qreal z = -v[Q_X] * q.v[Q_Y] + v[Q_Y] * q.v[Q_X] + v[Q_Z] * q.v[Q_W];
182 (*this) = q * Quaternion(w, x, y, z);
185Quaternion Quaternion::slerp(
const Quaternion &q1,
const Quaternion &q2, qreal t)
191 qreal cosAlpha = (q1.v[Q_X] * q2.v[Q_X] + q1.v[Q_Y] * q2.v[Q_Y] + q1.v[Q_Z] * q2.v[Q_Z] + q1.v[Q_W] * q2.v[Q_W]);
192 qreal alpha = acos(cosAlpha);
193 qreal sinAlpha = sin(alpha);
195 if (sinAlpha > 0.0) {
196 p1 = sin((1.0 - t) * alpha) / sinAlpha;
197 p2 = sin(t * alpha) / sinAlpha;
204 const qreal w = p1 * q1.v[Q_W] + p2 * q2.v[Q_W];
205 const qreal x = p1 * q1.v[Q_X] + p2 * q2.v[Q_X];
206 const qreal y = p1 * q1.v[Q_Y] + p2 * q2.v[Q_Y];
207 const qreal z = p1 * q1.v[Q_Z] + p2 * q2.v[Q_Z];
212Quaternion Quaternion::nlerp(
const Quaternion &q1,
const Quaternion &q2, qreal t)
214 const qreal p1 = 1.0 - t;
216 const qreal w = p1 * q1.v[Q_W] + t * q2.v[Q_W];
217 const qreal x = p1 * q1.v[Q_X] + t * q2.v[Q_X];
218 const qreal y = p1 * q1.v[Q_Y] + t * q2.v[Q_Y];
219 const qreal z = p1 * q1.v[Q_Z] + t * q2.v[Q_Z];
221 Quaternion result(w, x, y, z);
227void Quaternion::toMatrix(matrix &m)
const
229 const qreal xy = v[Q_X] * v[Q_Y], xz = v[Q_X] * v[Q_Z];
230 const qreal yy = v[Q_Y] * v[Q_Y], yw = v[Q_Y] * v[Q_W];
231 const qreal zw = v[Q_Z] * v[Q_W], zz = v[Q_Z] * v[Q_Z];
233 m[0][0] = 1.0 - 2.0 * (yy + zz);
234 m[0][1] = 2.0 * (xy + zw);
235 m[0][2] = 2.0 * (xz - yw);
238 const qreal xx = v[Q_X] * v[Q_X];
239 const qreal xw = v[Q_X] * v[Q_W];
240 const qreal yz = v[Q_Y] * v[Q_Z];
242 m[1][0] = 2.0 * (xy - zw);
243 m[1][1] = 1.0 - 2.0 * (xx + zz);
244 m[1][2] = 2.0 * (yz + xw);
247 m[2][0] = 2.0 * (xz + yw);
248 m[2][1] = 2.0 * (yz - xw);
249 m[2][2] = 1.0 - 2.0 * (xx + yy);
253void Quaternion::rotateAroundAxis(
const matrix &m)
255 const qreal x = m[0][0] * v[Q_X] + m[1][0] * v[Q_Y] + m[2][0] * v[Q_Z];
256 const qreal y = m[0][1] * v[Q_X] + m[1][1] * v[Q_Y] + m[2][1] * v[Q_Z];
257 const qreal z = m[0][2] * v[Q_X] + m[1][2] * v[Q_Y] + m[2][2] * v[Q_Z];
KTEXTEDITOR_EXPORT QDebug operator<<(QDebug s, const MovingCursor &cursor)
Binds a QML item to a specific geodetic location in screen coordinates.
QString arg(Args &&... args) const const