forked from moble/quaternion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquaternion.h
483 lines (465 loc) · 16.8 KB
/
quaternion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Copyright (c) 2017, Michael Boyle
// See LICENSE file for details: <https://github.com/moble/quaternion/blob/master/LICENSE>
#ifndef __QUATERNION_H__
#define __QUATERNION_H__
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_MSC_VER)
#include "math_msvc_compatibility.h"
#else
#include <math.h>
#endif
#define _QUATERNION_EPS 1e-14
#if defined(_MSC_VER)
#define NPY_INLINE __inline
#elif defined(__GNUC__)
#if defined(__STRICT_ANSI__)
#define NPY_INLINE __inline__
#else
#define NPY_INLINE inline
#endif
#else
#define NPY_INLINE
#endif
typedef struct {
double w;
double x;
double y;
double z;
} quaternion;
// Constructor-ish
quaternion quaternion_create_from_spherical_coords(double vartheta, double varphi);
quaternion quaternion_create_from_euler_angles(double alpha, double beta, double gamma);
// Unary bool returners
static NPY_INLINE int quaternion_isnan(quaternion q) {
return isnan(q.w) || isnan(q.x) || isnan(q.y) || isnan(q.z);
}
static NPY_INLINE int quaternion_nonzero(quaternion q) {
if(quaternion_isnan(q)) { return 1; }
return ! (q.w == 0 && q.x == 0 && q.y == 0 && q.z == 0);
}
static NPY_INLINE int quaternion_isinf(quaternion q) {
return isinf(q.w) || isinf(q.x) || isinf(q.y) || isinf(q.z);
}
static NPY_INLINE int quaternion_isfinite(quaternion q) {
return isfinite(q.w) && isfinite(q.x) && isfinite(q.y) && isfinite(q.z);
}
// Binary bool returners
static NPY_INLINE int quaternion_equal(quaternion q1, quaternion q2) {
return
!quaternion_isnan(q1) &&
!quaternion_isnan(q2) &&
q1.w == q2.w &&
q1.x == q2.x &&
q1.y == q2.y &&
q1.z == q2.z;
}
static NPY_INLINE int quaternion_not_equal(quaternion q1, quaternion q2) {
return !quaternion_equal(q1, q2);
}
static NPY_INLINE int quaternion_less(quaternion q1, quaternion q2) {
return
(!quaternion_isnan(q1) && !quaternion_isnan(q2))
&&
(q1.w != q2.w ? q1.w < q2.w :
q1.x != q2.x ? q1.x < q2.x :
q1.y != q2.y ? q1.y < q2.y :
q1.z != q2.z ? q1.z < q2.z : 0);
}
static NPY_INLINE int quaternion_greater(quaternion q1, quaternion q2) {
return
(!quaternion_isnan(q1) && !quaternion_isnan(q2))
&&
(q1.w != q2.w ? q1.w > q2.w :
q1.x != q2.x ? q1.x > q2.x :
q1.y != q2.y ? q1.y > q2.y :
q1.z != q2.z ? q1.z > q2.z : 0);
}
static NPY_INLINE int quaternion_less_equal(quaternion q1, quaternion q2) {
return
(!quaternion_isnan(q1) && !quaternion_isnan(q2))
&&
(q1.w != q2.w ? q1.w < q2.w :
q1.x != q2.x ? q1.x < q2.x :
q1.y != q2.y ? q1.y < q2.y :
q1.z != q2.z ? q1.z < q2.z : 1);
// Note that the final possibility is 1, whereas in
// `quaternion_less` it was 0. This distinction correctly
// accounts for equality.
}
static NPY_INLINE int quaternion_greater_equal(quaternion q1, quaternion q2) {
return
(!quaternion_isnan(q1) && !quaternion_isnan(q2))
&&
(q1.w != q2.w ? q1.w > q2.w :
q1.x != q2.x ? q1.x > q2.x :
q1.y != q2.y ? q1.y > q2.y :
q1.z != q2.z ? q1.z > q2.z : 1);
// Note that the final possibility is 1, whereas in
// `quaternion_greater` it was 0. This distinction correctly
// accounts for equality.
}
// Unary float returners
quaternion quaternion_log(quaternion q); // Pre-declare; declared again below, in its rightful place
static NPY_INLINE double quaternion_norm(quaternion q) {
return q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z;
}
static NPY_INLINE double quaternion_absolute(quaternion q) {
return sqrt(q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z);
}
static NPY_INLINE double quaternion_angle(quaternion q) {
return 2 * quaternion_absolute( quaternion_log( q ) );
}
// Unary quaternion returners
quaternion quaternion_sqrt(quaternion q);
quaternion quaternion_log(quaternion q);
quaternion quaternion_exp(quaternion q);
static NPY_INLINE quaternion quaternion_normalized(quaternion q) {
double q_abs = quaternion_absolute(q);
quaternion r = {q.w/q_abs, q.x/q_abs, q.y/q_abs, q.z/q_abs};
return r;
}
static NPY_INLINE quaternion quaternion_x_parity_conjugate(quaternion q) {
quaternion r = {q.w, q.x, -q.y, -q.z};
return r;
}
static NPY_INLINE quaternion quaternion_x_parity_symmetric_part(quaternion q) {
quaternion r = {q.w, q.x, 0.0, 0.0};
return r;
}
static NPY_INLINE quaternion quaternion_x_parity_antisymmetric_part(quaternion q) {
quaternion r = {0.0, 0.0, q.y, q.z};
return r;
}
static NPY_INLINE quaternion quaternion_y_parity_conjugate(quaternion q) {
quaternion r = {q.w, -q.x, q.y, -q.z};
return r;
}
static NPY_INLINE quaternion quaternion_y_parity_symmetric_part(quaternion q) {
quaternion r = {q.w, 0.0, q.y, 0.0};
return r;
}
static NPY_INLINE quaternion quaternion_y_parity_antisymmetric_part(quaternion q) {
quaternion r = {0.0, q.x, 0.0, q.z};
return r;
}
static NPY_INLINE quaternion quaternion_z_parity_conjugate(quaternion q) {
quaternion r = {q.w, -q.x, -q.y, q.z};
return r;
}
static NPY_INLINE quaternion quaternion_z_parity_symmetric_part(quaternion q) {
quaternion r = {q.w, 0.0, 0.0, q.z};
return r;
}
static NPY_INLINE quaternion quaternion_z_parity_antisymmetric_part(quaternion q) {
quaternion r = {0.0, q.x, q.y, 0.0};
return r;
}
static NPY_INLINE quaternion quaternion_parity_conjugate(quaternion q) {
quaternion r = {q.w, q.x, q.y, q.z};
return r;
}
static NPY_INLINE quaternion quaternion_parity_symmetric_part(quaternion q) {
quaternion r = {q.w, q.x, q.y, q.z};
return r;
}
static NPY_INLINE quaternion quaternion_parity_antisymmetric_part(quaternion q) {
quaternion r = {0.0, 0.0, 0.0, 0.0};
return r;
}
static NPY_INLINE quaternion quaternion_negative(quaternion q) {
quaternion r = {-q.w, -q.x, -q.y, -q.z};
return r;
}
static NPY_INLINE quaternion quaternion_conjugate(quaternion q) {
quaternion r = {q.w, -q.x, -q.y, -q.z};
return r;
}
static NPY_INLINE quaternion quaternion_inverse(quaternion q) {
double norm = quaternion_norm(q);
quaternion r = {q.w/norm, -q.x/norm, -q.y/norm, -q.z/norm};
return r;
}
// Quaternion-quaternion binary quaternion returners
static NPY_INLINE quaternion quaternion_copysign(quaternion q1, quaternion q2) {
quaternion r = {
copysign(q1.w, q2.w),
copysign(q1.x, q2.x),
copysign(q1.y, q2.y),
copysign(q1.z, q2.z)
};
return r;
}
// Quaternion-vector binary void returner
// static inline void _cross(double a[], double b[], double c[]) {
// c[0] = a[1]*b[2] - a[2]*b[1];
// c[1] = a[2]*b[0] - a[0]*b[2];
// c[2] = a[0]*b[1] - a[1]*b[0];
// return;
// }
// static inline void _cross_times_scalar(double s, double a[], double b[], double c[]) {
// c[0] = s*(a[1]*b[2] - a[2]*b[1]);
// c[1] = s*(a[2]*b[0] - a[0]*b[2]);
// c[2] = s*(a[0]*b[1] - a[1]*b[0]);
// return;
// }
static NPY_INLINE void _sv_plus_rxv(quaternion q, double v[], double w[]) {
w[0] = q.w * v[0] + q.y*v[2] - q.z*v[1];
w[1] = q.w * v[1] + q.z*v[0] - q.x*v[2];
w[2] = q.w * v[2] + q.x*v[1] - q.y*v[0];
return;
}
static NPY_INLINE void _v_plus_2rxvprime_over_m(quaternion q, double v[], double w[], double two_over_m, double vprime[]) {
vprime[0] = v[0] + two_over_m * (q.y*w[2] - q.z*w[1]);
vprime[1] = v[1] + two_over_m * (q.z*w[0] - q.x*w[2]);
vprime[2] = v[2] + two_over_m * (q.x*w[1] - q.y*w[0]);
return;
}
static NPY_INLINE void quaternion_rotate_vector(quaternion q, double v[], double vprime[]) {
// The most efficient formula I know of for rotating a vector by a quaternion is
// v' = v + 2 * r x (s * v + r x v) / m
// where x represents the cross product, s and r are the scalar and vector parts of the quaternion, respectively,
// and m is the sum of the squares of the components of the quaternion. This requires 22 multiplications and
// 14 additions, as opposed to 32 and 24 for naive application of `q*v*q.conj()`. In this function, I will further
// reduce the operation count to 18 and 12 by skipping the normalization by `m`. The full version will be
// implemented in another function.
double w[3];
_sv_plus_rxv(q, v, w);
_v_plus_2rxvprime_over_m(q, v, w, 2, vprime);
return;
}
static NPY_INLINE void quaternion_rotate_vector_and_normalize(quaternion q, double v[], double vprime[]) {
// This applies the algorithm described above, but also includes normalization of the quaternion.
double w[3];
double m = q.x*q.x+q.y*q.y+q.z*q.z;
_sv_plus_rxv(q, v, w);
_v_plus_2rxvprime_over_m(q, v, w, 2/m, vprime);
return;
}
// Quaternion-quaternion/quaternion-scalar binary quaternion returners
static NPY_INLINE quaternion quaternion_add(quaternion q1, quaternion q2) {
quaternion r = {
q1.w+q2.w,
q1.x+q2.x,
q1.y+q2.y,
q1.z+q2.z,
};
return r;
}
static NPY_INLINE void quaternion_inplace_add(quaternion* q1, quaternion q2) {
q1->w += q2.w;
q1->x += q2.x;
q1->y += q2.y;
q1->z += q2.z;
return;
}
static NPY_INLINE quaternion quaternion_scalar_add(double s, quaternion q) {
quaternion r = {s+q.w, q.x, q.y, q.z};
return r;
}
static NPY_INLINE void quaternion_inplace_scalar_add(double s, quaternion* q) {
q->w += s;
return;
}
static NPY_INLINE quaternion quaternion_add_scalar(quaternion q, double s) {
quaternion r = {s+q.w, q.x, q.y, q.z};
return r;
}
static NPY_INLINE void quaternion_inplace_add_scalar(quaternion* q, double s) {
q->w += s;
return;
}
static NPY_INLINE quaternion quaternion_subtract(quaternion q1, quaternion q2) {
quaternion r = {
q1.w-q2.w,
q1.x-q2.x,
q1.y-q2.y,
q1.z-q2.z,
};
return r;
}
static NPY_INLINE void quaternion_inplace_subtract(quaternion* q1, quaternion q2) {
q1->w -= q2.w;
q1->x -= q2.x;
q1->y -= q2.y;
q1->z -= q2.z;
return;
}
static NPY_INLINE quaternion quaternion_scalar_subtract(double s, quaternion q) {
quaternion r = {s-q.w, -q.x, -q.y, -q.z};
return r;
}
static NPY_INLINE quaternion quaternion_subtract_scalar(quaternion q, double s) {
quaternion r = {q.w-s, q.x, q.y, q.z};
return r;
}
static NPY_INLINE void quaternion_inplace_subtract_scalar(quaternion* q, double s) {
q->w -= s;
return;
}
static NPY_INLINE quaternion quaternion_multiply(quaternion q1, quaternion q2) {
quaternion r = {
q1.w*q2.w - q1.x*q2.x - q1.y*q2.y - q1.z*q2.z,
q1.w*q2.x + q1.x*q2.w + q1.y*q2.z - q1.z*q2.y,
q1.w*q2.y - q1.x*q2.z + q1.y*q2.w + q1.z*q2.x,
q1.w*q2.z + q1.x*q2.y - q1.y*q2.x + q1.z*q2.w,
};
return r;
}
static NPY_INLINE void quaternion_inplace_multiply(quaternion* q1a, quaternion q2) {
quaternion q1 = {q1a->w, q1a->x, q1a->y, q1a->z};
q1a->w = q1.w*q2.w - q1.x*q2.x - q1.y*q2.y - q1.z*q2.z;
q1a->x = q1.w*q2.x + q1.x*q2.w + q1.y*q2.z - q1.z*q2.y;
q1a->y = q1.w*q2.y - q1.x*q2.z + q1.y*q2.w + q1.z*q2.x;
q1a->z = q1.w*q2.z + q1.x*q2.y - q1.y*q2.x + q1.z*q2.w;
return;
}
static NPY_INLINE quaternion quaternion_scalar_multiply(double s, quaternion q) {
quaternion r = {s*q.w, s*q.x, s*q.y, s*q.z};
return r;
}
static NPY_INLINE void quaternion_inplace_scalar_multiply(double s, quaternion* q) {
q->w *= s;
q->x *= s;
q->y *= s;
q->z *= s;
return;
}
static NPY_INLINE quaternion quaternion_multiply_scalar(quaternion q, double s) {
quaternion r = {s*q.w, s*q.x, s*q.y, s*q.z};
return r;
}
static NPY_INLINE void quaternion_inplace_multiply_scalar(quaternion* q, double s) {
q->w *= s;
q->x *= s;
q->y *= s;
q->z *= s;
return;
}
static NPY_INLINE quaternion quaternion_divide(quaternion q1, quaternion q2) {
double q2norm = q2.w*q2.w + q2.x*q2.x + q2.y*q2.y + q2.z*q2.z;
quaternion r = {
( q1.w*q2.w + q1.x*q2.x + q1.y*q2.y + q1.z*q2.z) / q2norm,
(- q1.w*q2.x + q1.x*q2.w - q1.y*q2.z + q1.z*q2.y) / q2norm,
(- q1.w*q2.y + q1.x*q2.z + q1.y*q2.w - q1.z*q2.x) / q2norm,
(- q1.w*q2.z - q1.x*q2.y + q1.y*q2.x + q1.z*q2.w) / q2norm
};
return r;
}
static NPY_INLINE void quaternion_inplace_divide(quaternion* q1a, quaternion q2) {
double q2norm;
quaternion q1 = *q1a;
q2norm = q2.w*q2.w + q2.x*q2.x + q2.y*q2.y + q2.z*q2.z;
q1a->w = ( q1.w*q2.w + q1.x*q2.x + q1.y*q2.y + q1.z*q2.z)/q2norm;
q1a->x = (-q1.w*q2.x + q1.x*q2.w - q1.y*q2.z + q1.z*q2.y)/q2norm;
q1a->y = (-q1.w*q2.y + q1.x*q2.z + q1.y*q2.w - q1.z*q2.x)/q2norm;
q1a->z = (-q1.w*q2.z - q1.x*q2.y + q1.y*q2.x + q1.z*q2.w)/q2norm;
return;
}
static NPY_INLINE quaternion quaternion_scalar_divide(double s, quaternion q) {
double qnorm = q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z;
quaternion r = {
( s*q.w) / qnorm,
(-s*q.x) / qnorm,
(-s*q.y) / qnorm,
(-s*q.z) / qnorm
};
return r;
}
/* The following function is impossible, but listed for completeness: */
/* static NPY_INLINE void quaternion_inplace_scalar_divide(double* sa, quaternion q2) { } */
static NPY_INLINE quaternion quaternion_divide_scalar(quaternion q, double s) {
quaternion r = {q.w/s, q.x/s, q.y/s, q.z/s};
return r;
}
static NPY_INLINE void quaternion_inplace_divide_scalar(quaternion* q, double s) {
q->w /= s;
q->x /= s;
q->y /= s;
q->z /= s;
return;
}
static NPY_INLINE quaternion quaternion_power(quaternion q, quaternion p) {
/* Note that the following is just my chosen definition of the power. */
/* Other definitions may disagree due to non-commutativity. */
if(! quaternion_nonzero(q)) { /* log(q)=-inf */
if(! quaternion_nonzero(p)) {
quaternion r = {1.0, 0.0, 0.0, 0.0}; /* consistent with python */
return r;
} else {
quaternion r = {0.0, 0.0, 0.0, 0.0}; /* consistent with python */
return r;
}
}
return quaternion_exp(quaternion_multiply(quaternion_log(q), p));
}
static NPY_INLINE void quaternion_inplace_power(quaternion* q1, quaternion q2) {
/* Not overly useful as an in-place operator, but here for completeness. */
quaternion q3 = quaternion_power(*q1,q2);
*q1 = q3;
return;
}
quaternion quaternion_scalar_power(double s, quaternion q);
static NPY_INLINE void quaternion_inplace_scalar_power(double s, quaternion* q) {
/* Not overly useful as an in-place operator, but here for completeness. */
quaternion q2 = quaternion_scalar_power(s, *q);
*q = q2;
return;
}
static NPY_INLINE quaternion quaternion_power_scalar(quaternion q, double s) {
/* Unlike the quaternion^quaternion power, this is unambiguous. */
if(! quaternion_nonzero(q)) { /* log(q)=-inf */
if(s==0) {
quaternion r = {1.0, 0.0, 0.0, 0.0}; /* consistent with python */
return r;
} else {
quaternion r = {0.0, 0.0, 0.0, 0.0}; /* consistent with python */
return r;
}
}
return quaternion_exp(quaternion_multiply_scalar(quaternion_log(q), s));
}
static NPY_INLINE void quaternion_inplace_power_scalar(quaternion* q, double s) {
/* Not overly useful as an in-place operator, but here for completeness. */
quaternion q2 = quaternion_power_scalar(*q, s);
*q = q2;
return;
}
// Associated functions
static NPY_INLINE double rotor_intrinsic_distance(quaternion q1, quaternion q2) {
return 2*quaternion_absolute(quaternion_log(quaternion_divide(q1,q2)));
}
static NPY_INLINE double rotor_chordal_distance(quaternion q1, quaternion q2) {
return quaternion_absolute(quaternion_subtract(q1,q2));
}
static NPY_INLINE double rotation_intrinsic_distance(quaternion q1, quaternion q2) {
if(rotor_chordal_distance(q1,q2)<=1.414213562373096) {
return 2*quaternion_absolute(quaternion_log(quaternion_divide(q1,q2)));
} else {
return 2*quaternion_absolute(quaternion_log(quaternion_divide(q1,quaternion_negative(q2))));
}
}
static NPY_INLINE double rotation_chordal_distance(quaternion q1, quaternion q2) {
if(rotor_chordal_distance(q1,q2)<=1.414213562373096) {
return quaternion_absolute(quaternion_subtract(q1,q2));
} else {
return quaternion_absolute(quaternion_add(q1,q2));
}
}
static NPY_INLINE quaternion slerp(quaternion q1, quaternion q2, double tau) {
if(rotor_chordal_distance(q1,q2)<=1.414213562373096) {
return quaternion_multiply( quaternion_power_scalar(quaternion_divide(q2,q1), tau), q1);
} else {
return quaternion_multiply( quaternion_power_scalar(quaternion_divide(quaternion_negative(q2),q1), tau), q1);
}
}
static NPY_INLINE quaternion squad_evaluate(double tau_i, quaternion q_i, quaternion a_i, quaternion b_ip1, quaternion q_ip1) {
return slerp(slerp(q_i, q_ip1, tau_i),
slerp(a_i, b_ip1, tau_i),
2*tau_i*(1-tau_i));
}
#ifdef __cplusplus
}
#endif
#endif // __QUATERNION_H__