Oolite
Loading...
Searching...
No Matches
OOQuaternion.m
Go to the documentation of this file.
1/*
2
3OOQuaternion.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#include "OOMaths.h"
26
27const Quaternion kIdentityQuaternion = { 1.0f, 0.0f, 0.0f, 0.0f };
28const Quaternion kZeroQuaternion = { 0.0f, 0.0f, 0.0f, 0.0f };
29
31{
32 Quaternion result;
33 result.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
34 result.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
35 result.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;
36 result.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;
37 return result;
38}
39
40// NOTE: this is broken - its distribution is weighted towards corners of the hypercube. Probably doesn't matter, though.
42{
43 quat->w = (OOScalar)(Ranrot() % 1024) - 511.5f; // -511.5 to +511.5
44 quat->x = (OOScalar)(Ranrot() % 1024) - 511.5f; // -511.5 to +511.5
45 quat->y = (OOScalar)(Ranrot() % 1024) - 511.5f; // -511.5 to +511.5
46 quat->z = (OOScalar)(Ranrot() % 1024) - 511.5f; // -511.5 to +511.5
48}
49
51{
52 OOScalar w, wy, wx;
53 OOScalar x, xz, xx;
54 OOScalar y, yz, yy;
55 OOScalar z, zz;
56 Vector res;
57
58 w = quat.w;
59 z = quat.z;
60 y = quat.y;
61 x = quat.x;
62
63 xx = 2.0f * x;
64 yy = 2.0f * y;
65 zz = 2.0f * z;
66 wx = w * xx;
67 wy = w * yy;
68 xx = x * xx;
69 xz = x * zz;
70 yy = y * yy;
71 yz = y * zz;
72
73 res.x = xz - wy;
74 res.y = yz + wx;
75 res.z = 1.0f - xx - yy;
76
77 if (res.x || res.y || res.z)
78 return vector_normal(res);
79 else
80 return make_vector(0.0f, 0.0f, 1.0f);
81}
82
84{
85 // HPVect: profile this later
87}
88
90{
91 OOScalar w, wz, wx;
92 OOScalar x, xy, xx;
93 OOScalar y, yz, yy;
94 OOScalar z, zz;
95 Vector res;
96
97 w = quat.w;
98 z = quat.z;
99 y = quat.y;
100 x = quat.x;
101
102 xx = 2.0f * x;
103 yy = 2.0f * y;
104 zz = 2.0f * z;
105 wx = w * xx;
106 wz = w * zz;
107 xx = x * xx;
108 xy = x * yy;
109 yz = y * zz;
110 zz = z * zz;
111
112 res.x = xy + wz;
113 res.y = 1.0f - xx - zz;
114 res.z = yz - wx;
115
116 if (res.x || res.y || res.z)
117 return vector_normal(res);
118 else
119 return make_vector(0.0f, 1.0f, 0.0f);
120}
121
123{
124 OOScalar w, wz, wy;
125 OOScalar x, xz, xy;
126 OOScalar y, yy;
127 OOScalar z, zz;
128 Vector res;
129
130 w = quat.w;
131 z = quat.z;
132 y = quat.y;
133 x = quat.x;
134
135 yy = 2.0f * y;
136 zz = 2.0f * z;
137 wy = w * yy;
138 wz = w * zz;
139 xy = x * yy;
140 xz = x * zz;
141 yy = y * yy;
142 zz = z * zz;
143
144 res.x = 1.0f - yy - zz;
145 res.y = xy - wz;
146 res.z = xz + wy;
147
148 if (res.x || res.y || res.z)
149 return vector_normal(res);
150 else
151 return make_vector(1.0f, 0.0f, 0.0f);
152}
153
154void basis_vectors_from_quaternion(Quaternion quat, Vector* outRight, Vector* outUp, Vector* outForward)
155{
156 OOScalar w, wz, wy, wx;
157 OOScalar x, xz, xy, xx;
158 OOScalar y, yz, yy;
159 OOScalar z, zz;
160
161 w = quat.w;
162 z = quat.z;
163 y = quat.y;
164 x = quat.x;
165
166 xx = 2.0f * x;
167 yy = 2.0f * y;
168 zz = 2.0f * z;
169 wx = w * xx;
170 wy = w * yy;
171 wz = w * zz;
172 xx = x * xx;
173 xy = x * yy;
174 xz = x * zz;
175 yy = y * yy;
176 yz = y * zz;
177 zz = z * zz;
178
179 if (outRight != NULL) {
180 outRight->x = 1.0f - yy - zz;
181 outRight->y = xy - wz;
182 outRight->z = xz + wy;
183
184 if (outRight->x || outRight->y || outRight->z)
185 *outRight = vector_normal(*outRight);
186 else
187 *outRight = make_vector(1.0f, 0.0f, 0.0f);
188 }
189
190 if (outUp != NULL) {
191 outUp->x = xy + wz;
192 outUp->y = 1.0f - xx - zz;
193 outUp->z = yz - wx;
194
195 if (outUp->x || outUp->y || outUp->z)
196 *outUp = vector_normal(*outUp);
197 else
198 *outUp = make_vector(0.0f, 1.0f, 0.0f);
199 }
200
201 if (outForward != NULL) {
202 outForward->x = xz - wy;
203 outForward->y = yz + wx;
204 outForward->z = 1.0f - xx - yy;
205
206 if (outForward->x || outForward->y || outForward->z)
207 *outForward = vector_normal(*outForward);
208 else
209 *outForward = make_vector(0.0f, 0.0f, 1.0f);
210 }
211}
212
214{
215 Quaternion q;
216 OOScalar s = sqrt((1.0f + v0.x * v1.x + v0.y * v1.y + v0.z * v1.z) * 2.0f);
217 if (EXPECT(s > 0.0f)) {
218 OOScalar is = 1.0f / s;
219 q.x = (v0.y * v1.z - v0.z * v1.y) * is;
220 q.y = (v0.z * v1.x - v0.x * v1.z) * is;
221 q.z = (v0.x * v1.y - v0.y * v1.x) * is;
222 q.w = s * 0.5f;
223 } else {
224 // Is this actually a problem?
226 q = make_quaternion(0, 1, 0, 0);
227 } else {
229 }
230 // We arrive here for antiparallel vectors. Rotation axis is then undefined, but not rotating is
231 // wrong. Probably the calling function should exclude this situation. For current
232 // in-game use of this function we return (0,1,0,0), but generally that is also wrong.
233 }
234 return q;
235}
236
238{
239 Quaternion q;
240 OOHPScalar s = sqrt((1.0 + v0.x * v1.x + v0.y * v1.y + v0.z * v1.z) * 2.0);
241 if (EXPECT(s > 0.0)) {
242 OOHPScalar is = 1.0 / s;
243 q.x = (v0.y * v1.z - v0.z * v1.y) * is;
244 q.y = (v0.z * v1.x - v0.x * v1.z) * is;
245 q.z = (v0.x * v1.y - v0.y * v1.x) * is;
246 q.w = s * 0.5;
247 } else {
248 // Is this actually a problem?
250 q = make_quaternion(0, 1, 0, 0);
251 } else {
253 }
254 // We arrive here for antiparallel vectors. Rotation axis is then undefined, but not rotating is
255 // wrong. Probably the calling function should exclude this situation. For current
256 // in-game use of this function we return (0,1,0,0), but generally that is also wrong.
257 }
258 return q;
259}
260
261Quaternion quaternion_limited_rotation_between(Vector v0, Vector v1, float maxArc) // vectors both normalised
262{
263 Quaternion q;
264 OOScalar min_s = 2.0f * cosf(0.5f * maxArc);
265 OOScalar s = sqrtf((1.0f + v0.x * v1.x + v0.y * v1.y + v0.z * v1.z) * 2.0f);
266 // for some antiparallel vectors, s returns a NaN instead of 0. Testing s > 0 catches both.
267 if (EXPECT(s > 0.0f)) {
268 if (s < min_s) // larger angle => smaller cos
269 {
270 OOScalar a = maxArc * 0.5f;
271 OOScalar w = cosf(a);
272 OOScalar scale = sinf(a);
273 q.x = (v0.y * v1.z - v0.z * v1.y) * scale;
274 q.y = (v0.z * v1.x - v0.x * v1.z) * scale;
275 q.z = (v0.x * v1.y - v0.y * v1.x) * scale;
276 q.w = w;
277 } else {
278 OOScalar is = 1.0f / s;
279 q.x = (v0.y * v1.z - v0.z * v1.y) * is;
280 q.y = (v0.z * v1.x - v0.x * v1.z) * is;
281 q.z = (v0.x * v1.y - v0.y * v1.x) * is;
282 q.w = s * 0.5f;
283 }
284 } else {
285 // Is this actually a problem?
287 }
288 return q;
289}
290
292{
293 Quaternion result;
294 OOScalar a = angle * 0.5f;
295 OOScalar w = cosf(a);
296 OOScalar scale = sinf(a);
297
298 result.w = quat->w * w - quat->x * scale;
299 result.x = quat->w * scale + quat->x * w;
300 result.y = quat->y * w + quat->z * scale;
301 result.z = quat->z * w - quat->y * scale;
302
303 quat->w = result.w;
304 quat->x = result.x;
305 quat->y = result.y;
306 quat->z = result.z;
307}
308
310{
311 Quaternion result;
312 OOScalar a = angle * 0.5f;
313 OOScalar w = cosf(a);
314 OOScalar scale = sinf(a);
315
316 result.w = quat->w * w - quat->y * scale;
317 result.x = quat->x * w - quat->z * scale;
318 result.y = quat->w * scale + quat->y * w;
319 result.z = quat->z * w + quat->x * scale;
320
321 quat->w = result.w;
322 quat->x = result.x;
323 quat->y = result.y;
324 quat->z = result.z;
325}
326
328{
329 Quaternion result;
330 OOScalar a = angle * 0.5f;
331 OOScalar w = cosf(a);
332 OOScalar scale = sinf(a);
333
334 result.w = quat->w * w - quat->z * scale;
335 result.x = quat->x * w + quat->y * scale;
336 result.y = quat->y * w - quat->x * scale;
337 result.z = quat->w * scale + quat->z * w;
338
339 quat->w = result.w;
340 quat->x = result.x;
341 quat->y = result.y;
342 quat->z = result.z;
343}
344
346{
347 Quaternion q2 /*, result */;
348 OOScalar a = angle * 0.5f;
349 OOScalar w = cosf(a);
350 OOScalar scale = sinf(a);
351
352 q2.w = w;
353 q2.x = axis.x * scale;
354 q2.y = axis.y * scale;
355 q2.z = axis.z * scale;
356
357 *quat = quaternion_multiply(*quat, q2);
358}
359
360#if __OBJC__
361NSString* QuaternionDescription(Quaternion quaternion)
362{
363 float x, y, z;
364 char xs, ys, zs;
365
366 x = fabsf(quaternion.x);
367 y = fabsf(quaternion.y);
368 z = fabsf(quaternion.z);
369
370 xs = (quaternion.x >= 0.0f) ? '+' : '-';
371 ys = (quaternion.y >= 0.0f) ? '+' : '-';
372 zs = (quaternion.z >= 0.0f) ? '+' : '-';
373
374 return [NSString stringWithFormat:@"(%g %c %gi %c %gj %c %gk)", quaternion.w, xs, x, ys, y, zs, z];
375}
376#endif
377
379{
380 Quaternion qv;
381
382 qv.w = 0.0f - q.x * v.x - q.y * v.y - q.z * v.z;
383 qv.x = -q.w * v.x + q.y * v.z - q.z * v.y;
384 qv.y = -q.w * v.y + q.z * v.x - q.x * v.z;
385 qv.z = -q.w * v.z + q.x * v.y - q.y * v.x;
386
387 v.x = qv.w * -q.x + qv.x * -q.w + qv.y * -q.z - qv.z * -q.y;
388 v.y = qv.w * -q.y + qv.y * -q.w + qv.z * -q.x - qv.x * -q.z;
389 v.z = qv.w * -q.z + qv.z * -q.w + qv.x * -q.y - qv.y * -q.x;
390
391 return v;
392}
393
395{
396 OOHPScalar qvw, qvx, qvy, qvz;
397
398 qvw = 0.0 - q.x * v.x - q.y * v.y - q.z * v.z;
399 qvx = -q.w * v.x + q.y * v.z - q.z * v.y;
400 qvy = -q.w * v.y + q.z * v.x - q.x * v.z;
401 qvz = -q.w * v.z + q.x * v.y - q.y * v.x;
402
403 v.x = qvw * -q.x + qvx * -q.w + qvy * -q.z - qvz * -q.y;
404 v.y = qvw * -q.y + qvy * -q.w + qvz * -q.x - qvx * -q.z;
405 v.z = qvw * -q.z + qvz * -q.w + qvx * -q.y - qvy * -q.x;
406
407 return v;
408}
#define EXPECT(x)
OOINLINE HPVector vectorToHPVector(Vector v) INLINE_CONST_FUNC
Definition OOHPVector.h:166
OOINLINE bool HPvector_equal(HPVector a, HPVector b) INLINE_CONST_FUNC
Definition OOHPVector.h:250
const HPVector kBasisZHPVector
Definition OOHPVector.h:51
double OOHPScalar
Definition OOMaths.h:51
GLfloat OOScalar
Definition OOMaths.h:49
OOINLINE Quaternion make_quaternion(OOScalar qw, OOScalar qx, OOScalar qy, OOScalar qz) INLINE_CONST_FUNC
const Quaternion kIdentityQuaternion
OOINLINE void quaternion_normalize(Quaternion *quat) NONNULL_FUNC ALWAYS_INLINE_FUNC
const Quaternion kZeroQuaternion
Vector vector_up_from_quaternion(Quaternion quat)
void quaternion_rotate_about_x(Quaternion *quat, OOScalar angle)
HPVector HPvector_forward_from_quaternion(Quaternion quat)
Vector vector_right_from_quaternion(Quaternion quat)
Vector vector_forward_from_quaternion(Quaternion quat)
void basis_vectors_from_quaternion(Quaternion quat, Vector *outRight, Vector *outUp, Vector *outForward)
void quaternion_rotate_about_z(Quaternion *quat, OOScalar angle)
HPVector quaternion_rotate_HPvector(Quaternion q, HPVector v)
void quaternion_set_random(Quaternion *quat)
Vector quaternion_rotate_vector(Quaternion q, Vector v)
Quaternion quaternion_rotation_between(Vector v0, Vector v1)
void quaternion_rotate_about_y(Quaternion *quat, OOScalar angle)
Quaternion quaternion_limited_rotation_between(Vector v0, Vector v1, float maxArc)
Quaternion quaternion_rotation_betweenHP(HPVector v0, HPVector v1)
void quaternion_rotate_about_axis(Quaternion *quat, Vector axis, OOScalar angle)
Quaternion quaternion_multiply(Quaternion q1, Quaternion q2)
float y
float x
OOINLINE Vector make_vector(OOScalar vx, OOScalar vy, OOScalar vz) INLINE_CONST_FUNC
Definition OOVector.h:149
OOINLINE bool vector_equal(Vector a, Vector b) INLINE_CONST_FUNC
Definition OOVector.h:232
const Vector kBasisZVector
Definition OOVector.h:47
OOINLINE Vector vector_normal(Vector vec) INLINE_CONST_FUNC
Definition OOVector.h:270
unsigned Ranrot(void)
OOHPScalar x
Definition OOHPVector.h:36
OOHPScalar y
Definition OOHPVector.h:37
OOHPScalar z
Definition OOHPVector.h:38
OOScalar w
OOScalar x
OOScalar y
OOScalar z
OOScalar x
Definition OOVector.h:32
OOScalar y
Definition OOVector.h:33
OOScalar z
Definition OOVector.h:34