Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "Quaternion.h"
21 :
22 : #include "maths/MathUtil.h"
23 : #include "maths/Matrix3D.h"
24 : #include "maths/Vector3D.h"
25 :
26 : #include <cmath>
27 : #include <numbers>
28 :
29 : const float EPSILON=0.0001f;
30 :
31 :
32 21 : CQuaternion::CQuaternion() :
33 21 : m_W(1)
34 : {
35 9 : }
36 :
37 4 : CQuaternion::CQuaternion(float x, float y, float z, float w) :
38 4 : m_V(x, y, z), m_W(w)
39 : {
40 4 : }
41 :
42 0 : CQuaternion CQuaternion::operator + (const CQuaternion &quat) const
43 : {
44 0 : CQuaternion Temp;
45 0 : Temp.m_W = m_W + quat.m_W;
46 0 : Temp.m_V = m_V + quat.m_V;
47 0 : return Temp;
48 : }
49 :
50 0 : CQuaternion &CQuaternion::operator += (const CQuaternion &quat)
51 : {
52 0 : *this = *this + quat;
53 0 : return *this;
54 : }
55 :
56 0 : CQuaternion CQuaternion::operator - (const CQuaternion &quat) const
57 : {
58 0 : CQuaternion Temp;
59 0 : Temp.m_W = m_W - quat.m_W;
60 0 : Temp.m_V = m_V - quat.m_V;
61 0 : return Temp;
62 : }
63 :
64 0 : CQuaternion &CQuaternion::operator -= (const CQuaternion &quat)
65 : {
66 0 : *this = *this - quat;
67 0 : return *this;
68 : }
69 :
70 8 : CQuaternion CQuaternion::operator * (const CQuaternion &quat) const
71 : {
72 8 : CQuaternion Temp;
73 8 : Temp.m_W = (m_W * quat.m_W) - (m_V.Dot(quat.m_V));
74 8 : Temp.m_V = (m_V.Cross(quat.m_V)) + (quat.m_V * m_W) + (m_V * quat.m_W);
75 8 : return Temp;
76 : }
77 :
78 0 : CQuaternion &CQuaternion::operator *= (const CQuaternion &quat)
79 : {
80 0 : *this = *this * quat;
81 0 : return *this;
82 : }
83 :
84 0 : CQuaternion CQuaternion::operator * (float factor) const
85 : {
86 0 : CQuaternion Temp;
87 0 : Temp.m_W = m_W * factor;
88 0 : Temp.m_V = m_V * factor;
89 0 : return Temp;
90 : }
91 :
92 :
93 0 : float CQuaternion::Dot(const CQuaternion& quat) const
94 : {
95 0 : return
96 0 : m_V.X * quat.m_V.X +
97 0 : m_V.Y * quat.m_V.Y +
98 0 : m_V.Z * quat.m_V.Z +
99 0 : m_W * quat.m_W;
100 : }
101 :
102 4 : void CQuaternion::FromEulerAngles (float x, float y, float z)
103 : {
104 4 : float cr, cp, cy;
105 4 : float sr, sp, sy;
106 :
107 4 : CQuaternion QRoll, QPitch, QYaw;
108 :
109 4 : cr = cosf(x * 0.5f);
110 4 : cp = cosf(y * 0.5f);
111 4 : cy = cosf(z * 0.5f);
112 :
113 4 : sr = sinf(x * 0.5f);
114 4 : sp = sinf(y * 0.5f);
115 4 : sy = sinf(z * 0.5f);
116 :
117 4 : QRoll.m_V = CVector3D(sr, 0, 0);
118 4 : QRoll.m_W = cr;
119 :
120 4 : QPitch.m_V = CVector3D(0, sp, 0);
121 4 : QPitch.m_W = cp;
122 :
123 4 : QYaw.m_V = CVector3D(0, 0, sy);
124 4 : QYaw.m_W = cy;
125 :
126 4 : (*this) = QYaw * QPitch * QRoll;
127 4 : }
128 0 : CVector3D CQuaternion::ToEulerAngles()
129 : {
130 0 : float heading, attitude, bank;
131 0 : float sqw = m_W * m_W;
132 0 : float sqx = m_V.X*m_V.X;
133 0 : float sqy = m_V.Y*m_V.Y;
134 0 : float sqz = m_V.Z*m_V.Z;
135 0 : float unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
136 0 : float test = m_V.X*m_V.Y + m_V.Z*m_W;
137 0 : if (test > (.5f-EPSILON)*unit)
138 : { // singularity at north pole
139 0 : heading = 2 * atan2( m_V.X, m_W);
140 0 : attitude = std::numbers::pi_v<float> / 2.f;
141 0 : bank = 0;
142 : }
143 0 : else if (test < (-.5f+EPSILON)*unit)
144 : { // singularity at south pole
145 0 : heading = -2 * atan2(m_V.X, m_W);
146 0 : attitude = -std::numbers::pi_v<float> / 2.f;
147 0 : bank = 0;
148 : }
149 : else
150 : {
151 0 : heading = atan2(2.f * (m_V.X*m_V.Y + m_V.Z*m_W),(sqx - sqy - sqz + sqw));
152 0 : bank = atan2(2.f * (m_V.Y*m_V.Z + m_V.X*m_W),(-sqx - sqy + sqz + sqw));
153 0 : attitude = asin(-2.f * (m_V.X*m_V.Z - m_V.Y*m_W));
154 : }
155 0 : return CVector3D(bank, attitude, heading);
156 : }
157 :
158 4 : CMatrix3D CQuaternion::ToMatrix () const
159 : {
160 4 : CMatrix3D result;
161 4 : ToMatrix(result);
162 4 : return result;
163 : }
164 :
165 8 : void CQuaternion::ToMatrix(CMatrix3D& result) const
166 : {
167 8 : float wx, wy, wz, xx, xy, xz, yy, yz, zz;
168 :
169 : // calculate coefficients
170 8 : xx = m_V.X * m_V.X * 2.f;
171 8 : xy = m_V.X * m_V.Y * 2.f;
172 8 : xz = m_V.X * m_V.Z * 2.f;
173 :
174 8 : yy = m_V.Y * m_V.Y * 2.f;
175 8 : yz = m_V.Y * m_V.Z * 2.f;
176 :
177 8 : zz = m_V.Z * m_V.Z * 2.f;
178 :
179 8 : wx = m_W * m_V.X * 2.f;
180 8 : wy = m_W * m_V.Y * 2.f;
181 8 : wz = m_W * m_V.Z * 2.f;
182 :
183 8 : result._11 = 1.0f - (yy + zz);
184 8 : result._12 = xy - wz;
185 8 : result._13 = xz + wy;
186 8 : result._14 = 0;
187 :
188 8 : result._21 = xy + wz;
189 8 : result._22 = 1.0f - (xx + zz);
190 8 : result._23 = yz - wx;
191 8 : result._24 = 0;
192 :
193 8 : result._31 = xz - wy;
194 8 : result._32 = yz + wx;
195 8 : result._33 = 1.0f - (xx + yy);
196 8 : result._34 = 0;
197 :
198 8 : result._41 = 0;
199 8 : result._42 = 0;
200 8 : result._43 = 0;
201 8 : result._44 = 1;
202 8 : }
203 :
204 0 : void CQuaternion::Slerp(const CQuaternion& from, const CQuaternion& to, float ratio)
205 : {
206 0 : float to1[4];
207 0 : float omega, cosom, sinom, scale0, scale1;
208 :
209 : // calc cosine
210 0 : cosom = from.Dot(to);
211 :
212 :
213 : // adjust signs (if necessary)
214 0 : if (cosom < 0.0)
215 : {
216 0 : cosom = -cosom;
217 0 : to1[0] = -to.m_V.X;
218 0 : to1[1] = -to.m_V.Y;
219 0 : to1[2] = -to.m_V.Z;
220 0 : to1[3] = -to.m_W;
221 : }
222 : else
223 : {
224 : to1[0] = to.m_V.X;
225 : to1[1] = to.m_V.Y;
226 : to1[2] = to.m_V.Z;
227 : to1[3] = to.m_W;
228 : }
229 :
230 : // calculate coefficients
231 0 : if ((1.0f - cosom) > EPSILON)
232 : {
233 : // standard case (slerp)
234 0 : omega = acosf(cosom);
235 0 : sinom = sinf(omega);
236 0 : scale0 = sinf((1.0f - ratio) * omega) / sinom;
237 0 : scale1 = sinf(ratio * omega) / sinom;
238 : }
239 : else
240 : {
241 : // "from" and "to" quaternions are very close
242 : // ... so we can do a linear interpolation
243 0 : scale0 = 1.0f - ratio;
244 0 : scale1 = ratio;
245 : }
246 :
247 : // calculate final values
248 0 : m_V.X = scale0 * from.m_V.X + scale1 * to1[0];
249 0 : m_V.Y = scale0 * from.m_V.Y + scale1 * to1[1];
250 0 : m_V.Z = scale0 * from.m_V.Z + scale1 * to1[2];
251 0 : m_W = scale0 * from.m_W + scale1 * to1[3];
252 0 : }
253 :
254 0 : void CQuaternion::Nlerp(const CQuaternion& from, const CQuaternion& to, float ratio)
255 : {
256 0 : float c = from.Dot(to);
257 0 : if (c < 0.f)
258 0 : *this = from - (to + from) * ratio;
259 : else
260 0 : *this = from + (to - from) * ratio;
261 0 : Normalize();
262 0 : }
263 :
264 : ///////////////////////////////////////////////////////////////////////////////////////////////
265 : // FromAxisAngle: create a quaternion from axis/angle representation of a rotation
266 0 : void CQuaternion::FromAxisAngle(const CVector3D& axis, float angle)
267 : {
268 0 : float sinHalfTheta=(float) sin(angle/2);
269 0 : float cosHalfTheta=(float) cos(angle/2);
270 :
271 0 : m_V.X=axis.X*sinHalfTheta;
272 0 : m_V.Y=axis.Y*sinHalfTheta;
273 0 : m_V.Z=axis.Z*sinHalfTheta;
274 0 : m_W=cosHalfTheta;
275 0 : }
276 :
277 : ///////////////////////////////////////////////////////////////////////////////////////////////
278 : // ToAxisAngle: convert the quaternion to axis/angle representation of a rotation
279 0 : void CQuaternion::ToAxisAngle(CVector3D& axis, float& angle)
280 : {
281 0 : CQuaternion q = *this;
282 0 : q.Normalize();
283 0 : angle = acosf(q.m_W) * 2.f;
284 0 : float sin_a = sqrtf(1.f - q.m_W * q.m_W);
285 0 : if (fabsf(sin_a) < 0.0005f) sin_a = 1.f;
286 0 : axis.X = q.m_V.X / sin_a;
287 0 : axis.Y = q.m_V.Y / sin_a;
288 0 : axis.Z = q.m_V.Z / sin_a;
289 0 : }
290 :
291 :
292 : ///////////////////////////////////////////////////////////////////////////////////////////////
293 : // Normalize: normalize this quaternion
294 0 : void CQuaternion::Normalize()
295 : {
296 0 : float lensqrd=SQR(m_V.X)+SQR(m_V.Y)+SQR(m_V.Z)+SQR(m_W);
297 0 : if (lensqrd>0) {
298 0 : float invlen=1.0f/sqrtf(lensqrd);
299 0 : m_V*=invlen;
300 0 : m_W*=invlen;
301 : }
302 0 : }
303 :
304 : ///////////////////////////////////////////////////////////////////////////////////////////////
305 :
306 0 : CVector3D CQuaternion::Rotate(const CVector3D& vec) const
307 : {
308 : // v' = q * v * q^-1
309 : // (where v is the quat. with w=0, xyz=vec)
310 :
311 0 : return (*this * CQuaternion(vec.X, vec.Y, vec.Z, 0.f) * GetInverse()).m_V;
312 : }
313 :
314 0 : CQuaternion CQuaternion::GetInverse() const
315 : {
316 : // (x,y,z,w)^-1 = (-x/l^2, -y/l^2, -z/l^2, w/l^2) where l^2=x^2+y^2+z^2+w^2
317 : // Since we're only using quaternions for rotation, they should always have unit
318 : // length, so assume l=1
319 0 : return CQuaternion(-m_V.X, -m_V.Y, -m_V.Z, m_W);
320 : }
|