Blender V4.3
math_rotation.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "BLI_math_rotation.h"
10
11#include "BLI_math_base_safe.h"
12#include "BLI_math_geom.h"
13#include "BLI_math_matrix.h"
14#include "BLI_math_vector.h"
15
16#include "BLI_strict_flags.h" /* Keep last. */
17
18/******************************** Quaternions ********************************/
19
20/* used to test is a quat is not normalized (only used for debug prints) */
21#ifndef NDEBUG
22# define QUAT_EPSILON 0.0001
23#endif
24
31#define EULER_HYPOT_EPSILON 0.0000375
32
33void unit_axis_angle(float axis[3], float *angle)
34{
35 axis[0] = 0.0f;
36 axis[1] = 1.0f;
37 axis[2] = 0.0f;
38 *angle = 0.0f;
39}
40
41void unit_qt(float q[4])
42{
43 q[0] = 1.0f;
44 q[1] = q[2] = q[3] = 0.0f;
45}
46
47void copy_qt_qt(float q[4], const float a[4])
48{
49 q[0] = a[0];
50 q[1] = a[1];
51 q[2] = a[2];
52 q[3] = a[3];
53}
54
55bool is_zero_qt(const float q[4])
56{
57 return (q[0] == 0 && q[1] == 0 && q[2] == 0 && q[3] == 0);
58}
59
60void mul_qt_qtqt(float q[4], const float a[4], const float b[4])
61{
62 float t0, t1, t2;
63
64 t0 = a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3];
65 t1 = a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2];
66 t2 = a[0] * b[2] + a[2] * b[0] + a[3] * b[1] - a[1] * b[3];
67 q[3] = a[0] * b[3] + a[3] * b[0] + a[1] * b[2] - a[2] * b[1];
68 q[0] = t0;
69 q[1] = t1;
70 q[2] = t2;
71}
72
73void mul_qt_v3(const float q[4], float r[3])
74{
75 float t0, t1, t2;
76
77 t0 = -q[1] * r[0] - q[2] * r[1] - q[3] * r[2];
78 t1 = q[0] * r[0] + q[2] * r[2] - q[3] * r[1];
79 t2 = q[0] * r[1] + q[3] * r[0] - q[1] * r[2];
80 r[2] = q[0] * r[2] + q[1] * r[1] - q[2] * r[0];
81 r[0] = t1;
82 r[1] = t2;
83
84 t1 = t0 * -q[1] + r[0] * q[0] - r[1] * q[3] + r[2] * q[2];
85 t2 = t0 * -q[2] + r[1] * q[0] - r[2] * q[1] + r[0] * q[3];
86 r[2] = t0 * -q[3] + r[2] * q[0] - r[0] * q[2] + r[1] * q[1];
87 r[0] = t1;
88 r[1] = t2;
89}
90
91void conjugate_qt_qt(float q1[4], const float q2[4])
92{
93 q1[0] = q2[0];
94 q1[1] = -q2[1];
95 q1[2] = -q2[2];
96 q1[3] = -q2[3];
97}
98
99void conjugate_qt(float q[4])
100{
101 q[1] = -q[1];
102 q[2] = -q[2];
103 q[3] = -q[3];
104}
105
106float dot_qtqt(const float a[4], const float b[4])
107{
108 return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
109}
110
111void invert_qt(float q[4])
112{
113 const float f = dot_qtqt(q, q);
114
115 if (f == 0.0f) {
116 return;
117 }
118
119 conjugate_qt(q);
120 mul_qt_fl(q, 1.0f / f);
121}
122
123void invert_qt_qt(float q1[4], const float q2[4])
124{
125 copy_qt_qt(q1, q2);
126 invert_qt(q1);
127}
128
129void invert_qt_normalized(float q[4])
130{
132 conjugate_qt(q);
133}
134
135void invert_qt_qt_normalized(float q1[4], const float q2[4])
136{
137 copy_qt_qt(q1, q2);
139}
140
141void mul_qt_fl(float q[4], const float f)
142{
143 q[0] *= f;
144 q[1] *= f;
145 q[2] *= f;
146 q[3] *= f;
147}
148
149void sub_qt_qtqt(float q[4], const float a[4], const float b[4])
150{
151 float n_b[4];
152
153 n_b[0] = -b[0];
154 n_b[1] = b[1];
155 n_b[2] = b[2];
156 n_b[3] = b[3];
157
158 mul_qt_qtqt(q, a, n_b);
159}
160
161void pow_qt_fl_normalized(float q[4], const float fac)
162{
164 const float angle = fac * safe_acosf(q[0]); /* quat[0] = cos(0.5 * angle),
165 * but now the 0.5 and 2.0 rule out */
166 const float co = cosf(angle);
167 const float si = sinf(angle);
168 q[0] = co;
169 normalize_v3_length(q + 1, si);
170}
171
172void quat_to_compatible_quat(float q[4], const float a[4], const float old[4])
173{
174 const float eps = 1e-4f;
176 float old_unit[4];
177 /* Skips `!finite_v4(old)` case too. */
178 if (normalize_qt_qt(old_unit, old) > eps) {
179 float q_negate[4];
180 float delta[4];
181 rotation_between_quats_to_quat(delta, old_unit, a);
182 mul_qt_qtqt(q, old, delta);
183 negate_v4_v4(q_negate, q);
184 if (len_squared_v4v4(q_negate, old) < len_squared_v4v4(q, old)) {
185 copy_qt_qt(q, q_negate);
186 }
187 }
188 else {
189 copy_qt_qt(q, a);
190 }
191}
192
193/* Skip error check, currently only needed by #mat3_to_quat_legacy. */
194static void quat_to_mat3_no_error(float m[3][3], const float q[4])
195{
196 double q0, q1, q2, q3, qda, qdb, qdc, qaa, qab, qac, qbb, qbc, qcc;
197
198 q0 = M_SQRT2 * (double)q[0];
199 q1 = M_SQRT2 * (double)q[1];
200 q2 = M_SQRT2 * (double)q[2];
201 q3 = M_SQRT2 * (double)q[3];
202
203 qda = q0 * q1;
204 qdb = q0 * q2;
205 qdc = q0 * q3;
206 qaa = q1 * q1;
207 qab = q1 * q2;
208 qac = q1 * q3;
209 qbb = q2 * q2;
210 qbc = q2 * q3;
211 qcc = q3 * q3;
212
213 m[0][0] = (float)(1.0 - qbb - qcc);
214 m[0][1] = (float)(qdc + qab);
215 m[0][2] = (float)(-qdb + qac);
216
217 m[1][0] = (float)(-qdc + qab);
218 m[1][1] = (float)(1.0 - qaa - qcc);
219 m[1][2] = (float)(qda + qbc);
220
221 m[2][0] = (float)(qdb + qac);
222 m[2][1] = (float)(-qda + qbc);
223 m[2][2] = (float)(1.0 - qaa - qbb);
224}
225
226void quat_to_mat3(float m[3][3], const float q[4])
227{
228#ifndef NDEBUG
229 float f;
230 if (!((f = dot_qtqt(q, q)) == 0.0f || (fabsf(f - 1.0f) < (float)QUAT_EPSILON))) {
231 fprintf(stderr,
232 "Warning! quat_to_mat3() called with non-normalized: size %.8f *** report a bug ***\n",
233 f);
234 }
235#endif
236
238}
239
240void quat_to_mat4(float m[4][4], const float q[4])
241{
242 double q0, q1, q2, q3, qda, qdb, qdc, qaa, qab, qac, qbb, qbc, qcc;
243
244#ifndef NDEBUG
245 if (!((q0 = dot_qtqt(q, q)) == 0.0 || (fabs(q0 - 1.0) < QUAT_EPSILON))) {
246 fprintf(stderr,
247 "Warning! quat_to_mat4() called with non-normalized: size %.8f *** report a bug ***\n",
248 (float)q0);
249 }
250#endif
251
252 q0 = M_SQRT2 * (double)q[0];
253 q1 = M_SQRT2 * (double)q[1];
254 q2 = M_SQRT2 * (double)q[2];
255 q3 = M_SQRT2 * (double)q[3];
256
257 qda = q0 * q1;
258 qdb = q0 * q2;
259 qdc = q0 * q3;
260 qaa = q1 * q1;
261 qab = q1 * q2;
262 qac = q1 * q3;
263 qbb = q2 * q2;
264 qbc = q2 * q3;
265 qcc = q3 * q3;
266
267 m[0][0] = (float)(1.0 - qbb - qcc);
268 m[0][1] = (float)(qdc + qab);
269 m[0][2] = (float)(-qdb + qac);
270 m[0][3] = 0.0f;
271
272 m[1][0] = (float)(-qdc + qab);
273 m[1][1] = (float)(1.0 - qaa - qcc);
274 m[1][2] = (float)(qda + qbc);
275 m[1][3] = 0.0f;
276
277 m[2][0] = (float)(qdb + qac);
278 m[2][1] = (float)(-qda + qbc);
279 m[2][2] = (float)(1.0 - qaa - qbb);
280 m[2][3] = 0.0f;
281
282 m[3][0] = m[3][1] = m[3][2] = 0.0f;
283 m[3][3] = 1.0f;
284}
285
286void mat3_normalized_to_quat_fast(float q[4], const float mat[3][3])
287{
289 /* Caller must ensure matrices aren't negative for valid results, see: #24291, #94231. */
291
292 /* Method outlined by Mike Day, ref: https://math.stackexchange.com/a/3183435/220949
293 * with an additional `sqrtf(..)` for higher precision result.
294 * Removing the `sqrt` causes tests to fail unless the precision is set to 1e-6 or larger. */
295
296 if (mat[2][2] < 0.0f) {
297 if (mat[0][0] > mat[1][1]) {
298 const float trace = 1.0f + mat[0][0] - mat[1][1] - mat[2][2];
299 float s = 2.0f * sqrtf(trace);
300 if (mat[1][2] < mat[2][1]) {
301 /* Ensure W is non-negative for a canonical result. */
302 s = -s;
303 }
304 q[1] = 0.25f * s;
305 s = 1.0f / s;
306 q[0] = (mat[1][2] - mat[2][1]) * s;
307 q[2] = (mat[0][1] + mat[1][0]) * s;
308 q[3] = (mat[2][0] + mat[0][2]) * s;
309 if (UNLIKELY((trace == 1.0f) && (q[0] == 0.0f && q[2] == 0.0f && q[3] == 0.0f))) {
310 /* Avoids the need to normalize the degenerate case. */
311 q[1] = 1.0f;
312 }
313 }
314 else {
315 const float trace = 1.0f - mat[0][0] + mat[1][1] - mat[2][2];
316 float s = 2.0f * sqrtf(trace);
317 if (mat[2][0] < mat[0][2]) {
318 /* Ensure W is non-negative for a canonical result. */
319 s = -s;
320 }
321 q[2] = 0.25f * s;
322 s = 1.0f / s;
323 q[0] = (mat[2][0] - mat[0][2]) * s;
324 q[1] = (mat[0][1] + mat[1][0]) * s;
325 q[3] = (mat[1][2] + mat[2][1]) * s;
326 if (UNLIKELY((trace == 1.0f) && (q[0] == 0.0f && q[1] == 0.0f && q[3] == 0.0f))) {
327 /* Avoids the need to normalize the degenerate case. */
328 q[2] = 1.0f;
329 }
330 }
331 }
332 else {
333 if (mat[0][0] < -mat[1][1]) {
334 const float trace = 1.0f - mat[0][0] - mat[1][1] + mat[2][2];
335 float s = 2.0f * sqrtf(trace);
336 if (mat[0][1] < mat[1][0]) {
337 /* Ensure W is non-negative for a canonical result. */
338 s = -s;
339 }
340 q[3] = 0.25f * s;
341 s = 1.0f / s;
342 q[0] = (mat[0][1] - mat[1][0]) * s;
343 q[1] = (mat[2][0] + mat[0][2]) * s;
344 q[2] = (mat[1][2] + mat[2][1]) * s;
345 if (UNLIKELY((trace == 1.0f) && (q[0] == 0.0f && q[1] == 0.0f && q[2] == 0.0f))) {
346 /* Avoids the need to normalize the degenerate case. */
347 q[3] = 1.0f;
348 }
349 }
350 else {
351 /* NOTE(@ideasman42): A zero matrix will fall through to this block,
352 * needed so a zero scaled matrices to return a quaternion without rotation, see: #101848. */
353 const float trace = 1.0f + mat[0][0] + mat[1][1] + mat[2][2];
354 float s = 2.0f * sqrtf(trace);
355 q[0] = 0.25f * s;
356 s = 1.0f / s;
357 q[1] = (mat[1][2] - mat[2][1]) * s;
358 q[2] = (mat[2][0] - mat[0][2]) * s;
359 q[3] = (mat[0][1] - mat[1][0]) * s;
360 if (UNLIKELY((trace == 1.0f) && (q[1] == 0.0f && q[2] == 0.0f && q[3] == 0.0f))) {
361 /* Avoids the need to normalize the degenerate case. */
362 q[0] = 1.0f;
363 }
364 }
365 }
366
367 BLI_assert(!(q[0] < 0.0f));
368
369 /* Sometimes normalization is necessary due to round-off errors in the above
370 * calculations. The comparison here uses tighter tolerances than
371 * BLI_ASSERT_UNIT_QUAT(), so it's likely that even after a few more
372 * transformations the quaternion will still be considered unit-ish. */
373 const float q_len_squared = dot_qtqt(q, q);
374 const float threshold = 0.0002f /* #BLI_ASSERT_UNIT_EPSILON */ * 3;
375 if (fabs(q_len_squared - 1.0f) >= threshold) {
376 normalize_qt(q);
377 }
378}
379
380static void mat3_normalized_to_quat_with_checks(float q[4], float mat[3][3])
381{
382 const float det = determinant_m3_array(mat);
383 if (UNLIKELY(!isfinite(det))) {
384 unit_m3(mat);
385 }
386 else if (UNLIKELY(det < 0.0f)) {
387 negate_m3(mat);
388 }
390}
391
392void mat3_normalized_to_quat(float q[4], const float mat[3][3])
393{
394 float unit_mat_abs[3][3];
395 copy_m3_m3(unit_mat_abs, mat);
397}
398
399void mat3_to_quat(float q[4], const float mat[3][3])
400{
401 float unit_mat_abs[3][3];
402 normalize_m3_m3(unit_mat_abs, mat);
404}
405
406void mat4_normalized_to_quat(float q[4], const float mat[4][4])
407{
408 float unit_mat_abs[3][3];
409 copy_m3_m4(unit_mat_abs, mat);
411}
412
413void mat4_to_quat(float q[4], const float mat[4][4])
414{
415 float unit_mat_abs[3][3];
416 copy_m3_m4(unit_mat_abs, mat);
417 normalize_m3(unit_mat_abs);
419}
420
421void mat3_to_quat_legacy(float q[4], const float wmat[3][3])
422{
423 /* Legacy version of #mat3_to_quat which has slightly different behavior.
424 * Keep for particle-system & boids since replacing this will make subtle changes
425 * that impact hair in existing files. See: D15772. */
426
427 float mat[3][3], matr[3][3], matn[3][3], q1[4], q2[4], angle, si, co, nor[3];
428
429 /* work on a copy */
430 copy_m3_m3(mat, wmat);
431 normalize_m3(mat);
432
433 /* rotate z-axis of matrix to z-axis */
434
435 nor[0] = mat[2][1]; /* cross product with (0,0,1) */
436 nor[1] = -mat[2][0];
437 nor[2] = 0.0;
439
440 co = mat[2][2];
441 angle = 0.5f * safe_acosf(co);
442
443 co = cosf(angle);
444 si = sinf(angle);
445 q1[0] = co;
446 q1[1] = -nor[0] * si; /* negative here, but why? */
447 q1[2] = -nor[1] * si;
448 q1[3] = -nor[2] * si;
449
450 /* rotate back x-axis from mat, using inverse q1 */
452 invert_m3_m3(matn, matr);
453 mul_m3_v3(matn, mat[0]);
454
455 /* and align x-axes */
456 angle = 0.5f * atan2f(mat[0][1], mat[0][0]);
457
458 co = cosf(angle);
459 si = sinf(angle);
460 q2[0] = co;
461 q2[1] = 0.0f;
462 q2[2] = 0.0f;
463 q2[3] = si;
464
465 mul_qt_qtqt(q, q1, q2);
466}
467
468float normalize_qt(float q[4])
469{
470 const float len = sqrtf(dot_qtqt(q, q));
471
472 if (len != 0.0f) {
473 mul_qt_fl(q, 1.0f / len);
474 }
475 else {
476 q[1] = 1.0f;
477 q[0] = q[2] = q[3] = 0.0f;
478 }
479
480 return len;
481}
482
483float normalize_qt_qt(float r[4], const float q[4])
484{
485 copy_qt_qt(r, q);
486 return normalize_qt(r);
487}
488
489void rotation_between_vecs_to_mat3(float m[3][3], const float v1[3], const float v2[3])
490{
491 float axis[3];
492 /* avoid calculating the angle */
493 float angle_sin;
494 float angle_cos;
495
498
499 cross_v3_v3v3(axis, v1, v2);
500
501 angle_sin = normalize_v3(axis);
502 angle_cos = dot_v3v3(v1, v2);
503
504 if (angle_sin > FLT_EPSILON) {
505 axis_calc:
506 BLI_ASSERT_UNIT_V3(axis);
507 axis_angle_normalized_to_mat3_ex(m, axis, angle_sin, angle_cos);
509 }
510 else {
511 if (angle_cos > 0.0f) {
512 /* Same vectors, zero rotation... */
513 unit_m3(m);
514 }
515 else {
516 /* Colinear but opposed vectors, 180 rotation... */
517 ortho_v3_v3(axis, v1);
518 normalize_v3(axis);
519 angle_sin = 0.0f; /* sin(M_PI) */
520 angle_cos = -1.0f; /* cos(M_PI) */
521 goto axis_calc;
522 }
523 }
524}
525
526void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3])
527{
528 float axis[3];
529
530 cross_v3_v3v3(axis, v1, v2);
531
532 if (normalize_v3(axis) > FLT_EPSILON) {
533 float angle;
534
535 angle = angle_normalized_v3v3(v1, v2);
536
537 axis_angle_normalized_to_quat(q, axis, angle);
538 }
539 else {
540 /* degenerate case */
541
542 if (dot_v3v3(v1, v2) > 0.0f) {
543 /* Same vectors, zero rotation... */
544 unit_qt(q);
545 }
546 else {
547 /* Colinear but opposed vectors, 180 rotation... */
548 ortho_v3_v3(axis, v1);
549 axis_angle_to_quat(q, axis, (float)M_PI);
550 }
551 }
552}
553
554void rotation_between_quats_to_quat(float q[4], const float q1[4], const float q2[4])
555{
556 float tquat[4];
557
558 conjugate_qt_qt(tquat, q1);
559
560 mul_qt_fl(tquat, 1.0f / dot_qtqt(tquat, tquat));
561
562 mul_qt_qtqt(q, tquat, q2);
563}
564
565float quat_split_swing_and_twist(const float q_in[4],
566 const int axis,
567 float r_swing[4],
568 float r_twist[4])
569{
570 BLI_assert(axis >= 0 && axis <= 2);
571
572 /* The calculation requires a canonical quaternion. */
573 float q[4];
574
575 if (q_in[0] < 0) {
576 negate_v4_v4(q, q_in);
577 }
578 else {
579 copy_v4_v4(q, q_in);
580 }
581
582 /* Half-twist angle can be computed directly. */
583 float t = atan2f(q[axis + 1], q[0]);
584
585 if (r_swing || r_twist) {
586 float sin_t = sinf(t), cos_t = cosf(t);
587
588 /* Compute swing by multiplying the original quaternion by inverted twist. */
589 if (r_swing) {
590 float twist_inv[4];
591
592 twist_inv[0] = cos_t;
593 zero_v3(twist_inv + 1);
594 twist_inv[axis + 1] = -sin_t;
595
596 mul_qt_qtqt(r_swing, q, twist_inv);
597
598 BLI_assert(fabsf(r_swing[axis + 1]) < BLI_ASSERT_UNIT_EPSILON);
599 }
600
601 /* Output twist last just in case q overlaps r_twist. */
602 if (r_twist) {
603 r_twist[0] = cos_t;
604 zero_v3(r_twist + 1);
605 r_twist[axis + 1] = sin_t;
606 }
607 }
608
609 return 2.0f * t;
610}
611
612/* -------------------------------------------------------------------- */
620float angle_normalized_qt(const float q[4])
621{
623 return 2.0f * safe_acosf(q[0]);
624}
625
626float angle_qt(const float q[4])
627{
628 float tquat[4];
629
630 normalize_qt_qt(tquat, q);
631
632 return angle_normalized_qt(tquat);
633}
634
635float angle_normalized_qtqt(const float q1[4], const float q2[4])
636{
637 float qdelta[4];
638
641
643
644 return angle_normalized_qt(qdelta);
645}
646
647float angle_qtqt(const float q1[4], const float q2[4])
648{
649 float quat1[4], quat2[4];
650
651 normalize_qt_qt(quat1, q1);
652 normalize_qt_qt(quat2, q2);
653
654 return angle_normalized_qtqt(quat1, quat2);
655}
656
659/* -------------------------------------------------------------------- */
669float angle_signed_normalized_qt(const float q[4])
670{
672 if (q[0] >= 0.0f) {
673 return 2.0f * safe_acosf(q[0]);
674 }
675
676 return -2.0f * safe_acosf(-q[0]);
677}
678
679float angle_signed_normalized_qtqt(const float q1[4], const float q2[4])
680{
681 if (dot_qtqt(q1, q2) >= 0.0f) {
682 return angle_normalized_qtqt(q1, q2);
683 }
684
685 float q2_copy[4];
686 negate_v4_v4(q2_copy, q2);
687 return -angle_normalized_qtqt(q1, q2_copy);
688}
689
690float angle_signed_qt(const float q[4])
691{
692 float tquat[4];
693
694 normalize_qt_qt(tquat, q);
695
696 return angle_signed_normalized_qt(tquat);
697}
698
699float angle_signed_qtqt(const float q1[4], const float q2[4])
700{
701 if (dot_qtqt(q1, q2) >= 0.0f) {
702 return angle_qtqt(q1, q2);
703 }
704
705 float q2_copy[4];
706 negate_v4_v4(q2_copy, q2);
707 return -angle_qtqt(q1, q2_copy);
708}
709
712void vec_to_quat(float q[4], const float vec[3], short axis, const short upflag)
713{
714 const float eps = 1e-4f;
715 float nor[3], tvec[3];
716 float angle, si, co, len;
717
718 BLI_assert(axis >= 0 && axis <= 5);
719 BLI_assert(upflag >= 0 && upflag <= 2);
720
721 /* first set the quat to unit */
722 unit_qt(q);
723
724 len = len_v3(vec);
725
726 if (UNLIKELY(len == 0.0f)) {
727 return;
728 }
729
730 /* rotate to axis */
731 if (axis > 2) {
732 copy_v3_v3(tvec, vec);
733 axis = (short)(axis - 3);
734 }
735 else {
736 negate_v3_v3(tvec, vec);
737 }
738
739 /* nasty! I need a good routine for this...
740 * problem is a rotation of an Y axis to the negative Y-axis for example.
741 */
742
743 if (axis == 0) { /* x-axis */
744 nor[0] = 0.0;
745 nor[1] = -tvec[2];
746 nor[2] = tvec[1];
747
748 if (fabsf(tvec[1]) + fabsf(tvec[2]) < eps) {
749 nor[1] = 1.0f;
750 }
751
752 co = tvec[0];
753 }
754 else if (axis == 1) { /* y-axis */
755 nor[0] = tvec[2];
756 nor[1] = 0.0;
757 nor[2] = -tvec[0];
758
759 if (fabsf(tvec[0]) + fabsf(tvec[2]) < eps) {
760 nor[2] = 1.0f;
761 }
762
763 co = tvec[1];
764 }
765 else { /* z-axis */
766 nor[0] = -tvec[1];
767 nor[1] = tvec[0];
768 nor[2] = 0.0;
769
770 if (fabsf(tvec[0]) + fabsf(tvec[1]) < eps) {
771 nor[0] = 1.0f;
772 }
773
774 co = tvec[2];
775 }
776 co /= len;
777
779
781
782 if (axis != upflag) {
783 float mat[3][3];
784 float q2[4];
785 const float *fp = mat[2];
786 quat_to_mat3(mat, q);
787
788 if (axis == 0) {
789 if (upflag == 1) {
790 angle = 0.5f * atan2f(fp[2], fp[1]);
791 }
792 else {
793 angle = -0.5f * atan2f(fp[1], fp[2]);
794 }
795 }
796 else if (axis == 1) {
797 if (upflag == 0) {
798 angle = -0.5f * atan2f(fp[2], fp[0]);
799 }
800 else {
801 angle = 0.5f * atan2f(fp[0], fp[2]);
802 }
803 }
804 else {
805 if (upflag == 0) {
806 angle = 0.5f * atan2f(-fp[1], -fp[0]);
807 }
808 else {
809 angle = -0.5f * atan2f(-fp[0], -fp[1]);
810 }
811 }
812
813 co = cosf(angle);
814 si = sinf(angle) / len;
815 q2[0] = co;
816 q2[1] = tvec[0] * si;
817 q2[2] = tvec[1] * si;
818 q2[3] = tvec[2] * si;
819
820 mul_qt_qtqt(q, q2, q);
821 }
822}
823
824#if 0
825
826/* A & M Watt, Advanced animation and rendering techniques, 1992 ACM press */
827void QuatInterpolW(float *result, float quat1[4], float quat2[4], float t)
828{
829 float omega, cosom, sinom, sc1, sc2;
830
831 cosom = quat1[0] * quat2[0] + quat1[1] * quat2[1] + quat1[2] * quat2[2] + quat1[3] * quat2[3];
832
833 /* rotate around shortest angle */
834 if ((1.0f + cosom) > 0.0001f) {
835
836 if ((1.0f - cosom) > 0.0001f) {
837 omega = (float)acos(cosom);
838 sinom = sinf(omega);
839 sc1 = sinf((1.0 - t) * omega) / sinom;
840 sc2 = sinf(t * omega) / sinom;
841 }
842 else {
843 sc1 = 1.0f - t;
844 sc2 = t;
845 }
846 result[0] = sc1 * quat1[0] + sc2 * quat2[0];
847 result[1] = sc1 * quat1[1] + sc2 * quat2[1];
848 result[2] = sc1 * quat1[2] + sc2 * quat2[2];
849 result[3] = sc1 * quat1[3] + sc2 * quat2[3];
850 }
851 else {
852 result[0] = quat2[3];
853 result[1] = -quat2[2];
854 result[2] = quat2[1];
855 result[3] = -quat2[0];
856
857 sc1 = sinf((1.0 - t) * M_PI_2);
858 sc2 = sinf(t * M_PI_2);
859
860 result[0] = sc1 * quat1[0] + sc2 * result[0];
861 result[1] = sc1 * quat1[1] + sc2 * result[1];
862 result[2] = sc1 * quat1[2] + sc2 * result[2];
863 result[3] = sc1 * quat1[3] + sc2 * result[3];
864 }
865}
866#endif
867
868void interp_dot_slerp(const float t, const float cosom, float r_w[2])
869{
870 const float eps = 1e-4f;
871
872 BLI_assert(IN_RANGE_INCL(cosom, -1.0001f, 1.0001f));
873
874 /* within [-1..1] range, avoid aligned axis */
875 if (LIKELY(fabsf(cosom) < (1.0f - eps))) {
876 float omega, sinom;
877
878 omega = acosf(cosom);
879 sinom = sinf(omega);
880 r_w[0] = sinf((1.0f - t) * omega) / sinom;
881 r_w[1] = sinf(t * omega) / sinom;
882 }
883 else {
884 /* fallback to lerp */
885 r_w[0] = 1.0f - t;
886 r_w[1] = t;
887 }
888}
889
890void interp_qt_qtqt(float q[4], const float a[4], const float b[4], const float t)
891{
892 float quat[4], cosom, w[2];
893
896
897 cosom = dot_qtqt(a, b);
898
899 /* rotate around shortest angle */
900 if (cosom < 0.0f) {
901 cosom = -cosom;
902 negate_v4_v4(quat, a);
903 }
904 else {
905 copy_qt_qt(quat, a);
906 }
907
908 interp_dot_slerp(t, cosom, w);
909
910 q[0] = w[0] * quat[0] + w[1] * b[0];
911 q[1] = w[0] * quat[1] + w[1] * b[1];
912 q[2] = w[0] * quat[2] + w[1] * b[2];
913 q[3] = w[0] * quat[3] + w[1] * b[3];
914}
915
916void add_qt_qtqt(float q[4], const float a[4], const float b[4], const float t)
917{
918 q[0] = a[0] + t * b[0];
919 q[1] = a[1] + t * b[1];
920 q[2] = a[2] + t * b[2];
921 q[3] = a[3] + t * b[3];
922}
923
925 float quat[4], const float v1[3], const float v2[3], const float v3[3], const float no_orig[3])
926{
927 /* imaginary x-axis, y-axis triangle is being rotated */
928 float vec[3], q1[4], q2[4], n[3], si, co, angle, mat[3][3], imat[3][3];
929
930 /* move z-axis to face-normal */
931#if 0
932 normal_tri_v3(vec, v1, v2, v3);
933#else
934 copy_v3_v3(vec, no_orig);
935 (void)v3;
936#endif
937
938 n[0] = vec[1];
939 n[1] = -vec[0];
940 n[2] = 0.0f;
941 normalize_v3(n);
942
943 if (n[0] == 0.0f && n[1] == 0.0f) {
944 n[0] = 1.0f;
945 }
946
947 angle = -0.5f * safe_acosf(vec[2]);
948 co = cosf(angle);
949 si = sinf(angle);
950 q1[0] = co;
951 q1[1] = n[0] * si;
952 q1[2] = n[1] * si;
953 q1[3] = 0.0f;
954
955 /* rotate back line v1-v2 */
956 quat_to_mat3(mat, q1);
957 invert_m3_m3(imat, mat);
958 sub_v3_v3v3(vec, v2, v1);
959 mul_m3_v3(imat, vec);
960
961 /* what angle has this line with x-axis? */
962 vec[2] = 0.0f;
963 normalize_v3(vec);
964
965 angle = 0.5f * atan2f(vec[1], vec[0]);
966 co = cosf(angle);
967 si = sinf(angle);
968 q2[0] = co;
969 q2[1] = 0.0f;
970 q2[2] = 0.0f;
971 q2[3] = si;
972
973 mul_qt_qtqt(quat, q1, q2);
974}
975
976float tri_to_quat(float q[4], const float a[3], const float b[3], const float c[3])
977{
978 float vec[3];
979 const float len = normal_tri_v3(vec, a, b, c);
980
981 tri_to_quat_ex(q, a, b, c, vec);
982 return len;
983}
984
985void sin_cos_from_fraction(int numerator, int denominator, float *r_sin, float *r_cos)
986{
987 /* By default, creating a circle from an integer: calling #sinf & #cosf on the fraction doesn't
988 * create symmetrical values (because floats can't represent Pi exactly).
989 * Resolve this when the rotation is calculated from a fraction by mapping the `numerator`
990 * to lower values so X/Y values for points around a circle are exactly symmetrical, see #87779.
991 *
992 * Multiply both the `numerator` and `denominator` by eight to ensure we can divide the circle
993 * into 8 octants. For each octant, we then use symmetry and negation to bring the `numerator`
994 * closer to the origin where precision is highest.
995 *
996 * Cases 2, 4, 5 and 7, use the trigonometric identity sin(-x) == -sin(x).
997 * Cases 1, 2, 5 and 6, swap the pointers `r_sin` and `r_cos`.
998 */
999 BLI_assert(0 <= numerator);
1000 BLI_assert(numerator <= denominator);
1001 BLI_assert(denominator > 0);
1002
1003 numerator *= 8; /* Multiply numerator the same as denominator. */
1004 const int octant = numerator / denominator; /* Determine the octant. */
1005 denominator *= 8; /* Ensure denominator is a multiple of eight. */
1006 float cos_sign = 1.0f; /* Either 1.0f or -1.0f. */
1007
1008 switch (octant) {
1009 case 0:
1010 /* Primary octant, nothing to do. */
1011 break;
1012 case 1:
1013 case 2:
1014 numerator = (denominator / 4) - numerator;
1015 SWAP(float *, r_sin, r_cos);
1016 break;
1017 case 3:
1018 case 4:
1019 numerator = (denominator / 2) - numerator;
1020 cos_sign = -1.0f;
1021 break;
1022 case 5:
1023 case 6:
1024 numerator = numerator - (denominator * 3 / 4);
1025 SWAP(float *, r_sin, r_cos);
1026 cos_sign = -1.0f;
1027 break;
1028 case 7:
1029 numerator = numerator - denominator;
1030 break;
1031 default:
1033 }
1034
1035 BLI_assert(-denominator / 4 <= numerator); /* Numerator may be negative. */
1036 BLI_assert(numerator <= denominator / 4);
1037 BLI_assert(ELEM(cos_sign, -1.0f, 1.0f));
1038
1039 const float angle = (float)(2.0 * M_PI) * ((float)numerator / (float)denominator);
1040 *r_sin = sinf(angle);
1041 *r_cos = cosf(angle) * cos_sign;
1042}
1043
1044void print_qt(const char *str, const float q[4])
1045{
1046 printf("%s: %.3f %.3f %.3f %.3f\n", str, q[0], q[1], q[2], q[3]);
1047}
1048
1049/******************************** Axis Angle *********************************/
1050
1051void axis_angle_normalized_to_quat(float r[4], const float axis[3], const float angle)
1052{
1053 const float phi = 0.5f * angle;
1054 const float si = sinf(phi);
1055 const float co = cosf(phi);
1056 BLI_ASSERT_UNIT_V3(axis);
1057 r[0] = co;
1058 mul_v3_v3fl(r + 1, axis, si);
1059}
1060
1061void axis_angle_to_quat(float r[4], const float axis[3], const float angle)
1062{
1063 float nor[3];
1064
1065 if (LIKELY(normalize_v3_v3(nor, axis) != 0.0f)) {
1067 }
1068 else {
1069 unit_qt(r);
1070 }
1071}
1072
1073void quat_to_axis_angle(float axis[3], float *angle, const float q[4])
1074{
1075 float ha, si;
1076
1077#ifndef NDEBUG
1078 if (!((ha = dot_qtqt(q, q)) == 0.0f || (fabsf(ha - 1.0f) < (float)QUAT_EPSILON))) {
1079 fprintf(stderr,
1080 "Warning! quat_to_axis_angle() called with non-normalized: size %.8f *** report a bug "
1081 "***\n",
1082 ha);
1083 }
1084#endif
1085
1086 /* calculate angle/2, and sin(angle/2) */
1087 ha = acosf(q[0]);
1088 si = sinf(ha);
1089
1090 /* from half-angle to angle */
1091 *angle = ha * 2;
1092
1093 /* prevent division by zero for axis conversion */
1094 if (fabsf(si) < 0.0005f) {
1095 si = 1.0f;
1096 }
1097
1098 axis[0] = q[1] / si;
1099 axis[1] = q[2] / si;
1100 axis[2] = q[3] / si;
1101 if (is_zero_v3(axis)) {
1102 axis[1] = 1.0f;
1103 }
1104}
1105
1106void axis_angle_to_eulO(float eul[3], const short order, const float axis[3], const float angle)
1107{
1108 float q[4];
1109
1110 /* use quaternions as intermediate representation for now... */
1111 axis_angle_to_quat(q, axis, angle);
1112 quat_to_eulO(eul, order, q);
1113}
1114
1115void eulO_to_axis_angle(float axis[3], float *angle, const float eul[3], const short order)
1116{
1117 float q[4];
1118
1119 /* use quaternions as intermediate representation for now... */
1120 eulO_to_quat(q, eul, order);
1121 quat_to_axis_angle(axis, angle, q);
1122}
1123
1125 const float axis[3],
1126 const float angle_sin,
1127 const float angle_cos)
1128{
1129 float nsi[3], ico;
1130 float n_00, n_01, n_11, n_02, n_12, n_22;
1131
1132 BLI_ASSERT_UNIT_V3(axis);
1133
1134 /* now convert this to a 3x3 matrix */
1135
1136 ico = (1.0f - angle_cos);
1137 nsi[0] = axis[0] * angle_sin;
1138 nsi[1] = axis[1] * angle_sin;
1139 nsi[2] = axis[2] * angle_sin;
1140
1141 n_00 = (axis[0] * axis[0]) * ico;
1142 n_01 = (axis[0] * axis[1]) * ico;
1143 n_11 = (axis[1] * axis[1]) * ico;
1144 n_02 = (axis[0] * axis[2]) * ico;
1145 n_12 = (axis[1] * axis[2]) * ico;
1146 n_22 = (axis[2] * axis[2]) * ico;
1147
1148 mat[0][0] = n_00 + angle_cos;
1149 mat[0][1] = n_01 + nsi[2];
1150 mat[0][2] = n_02 - nsi[1];
1151 mat[1][0] = n_01 - nsi[2];
1152 mat[1][1] = n_11 + angle_cos;
1153 mat[1][2] = n_12 + nsi[0];
1154 mat[2][0] = n_02 + nsi[1];
1155 mat[2][1] = n_12 - nsi[0];
1156 mat[2][2] = n_22 + angle_cos;
1157}
1158
1159void axis_angle_normalized_to_mat3(float R[3][3], const float axis[3], const float angle)
1160{
1161 axis_angle_normalized_to_mat3_ex(R, axis, sinf(angle), cosf(angle));
1162}
1163
1164void axis_angle_to_mat3(float R[3][3], const float axis[3], const float angle)
1165{
1166 float nor[3];
1167
1168 /* normalize the axis first (to remove unwanted scaling) */
1169 if (normalize_v3_v3(nor, axis) == 0.0f) {
1170 unit_m3(R);
1171 return;
1172 }
1173
1175}
1176
1177void axis_angle_to_mat4(float R[4][4], const float axis[3], const float angle)
1178{
1179 float tmat[3][3];
1180
1181 axis_angle_to_mat3(tmat, axis, angle);
1182 unit_m4(R);
1183 copy_m4_m3(R, tmat);
1184}
1185
1186void mat3_normalized_to_axis_angle(float axis[3], float *angle, const float mat[3][3])
1187{
1188 float q[4];
1189
1190 /* use quaternions as intermediate representation */
1191 /* TODO: it would be nicer to go straight there... */
1193 quat_to_axis_angle(axis, angle, q);
1194}
1195void mat3_to_axis_angle(float axis[3], float *angle, const float mat[3][3])
1196{
1197 float q[4];
1198
1199 /* use quaternions as intermediate representation */
1200 /* TODO: it would be nicer to go straight there... */
1201 mat3_to_quat(q, mat);
1202 quat_to_axis_angle(axis, angle, q);
1203}
1204
1205void mat4_normalized_to_axis_angle(float axis[3], float *angle, const float mat[4][4])
1206{
1207 float q[4];
1208
1209 /* use quaternions as intermediate representation */
1210 /* TODO: it would be nicer to go straight there... */
1212 quat_to_axis_angle(axis, angle, q);
1213}
1214
1215void mat4_to_axis_angle(float axis[3], float *angle, const float mat[4][4])
1216{
1217 float q[4];
1218
1219 /* use quaternions as intermediate representation */
1220 /* TODO: it would be nicer to go straight there... */
1221 mat4_to_quat(q, mat);
1222 quat_to_axis_angle(axis, angle, q);
1223}
1224
1225void axis_angle_to_mat4_single(float R[4][4], const char axis, const float angle)
1226{
1227 float mat3[3][3];
1228 axis_angle_to_mat3_single(mat3, axis, angle);
1229 copy_m4_m3(R, mat3);
1230}
1231
1232void axis_angle_to_mat3_single(float R[3][3], const char axis, const float angle)
1233{
1234 const float angle_cos = cosf(angle);
1235 const float angle_sin = sinf(angle);
1236
1237 switch (axis) {
1238 case 'X': /* rotation around X */
1239 R[0][0] = 1.0f;
1240 R[0][1] = 0.0f;
1241 R[0][2] = 0.0f;
1242 R[1][0] = 0.0f;
1243 R[1][1] = angle_cos;
1244 R[1][2] = angle_sin;
1245 R[2][0] = 0.0f;
1246 R[2][1] = -angle_sin;
1247 R[2][2] = angle_cos;
1248 break;
1249 case 'Y': /* rotation around Y */
1250 R[0][0] = angle_cos;
1251 R[0][1] = 0.0f;
1252 R[0][2] = -angle_sin;
1253 R[1][0] = 0.0f;
1254 R[1][1] = 1.0f;
1255 R[1][2] = 0.0f;
1256 R[2][0] = angle_sin;
1257 R[2][1] = 0.0f;
1258 R[2][2] = angle_cos;
1259 break;
1260 case 'Z': /* rotation around Z */
1261 R[0][0] = angle_cos;
1262 R[0][1] = angle_sin;
1263 R[0][2] = 0.0f;
1264 R[1][0] = -angle_sin;
1265 R[1][1] = angle_cos;
1266 R[1][2] = 0.0f;
1267 R[2][0] = 0.0f;
1268 R[2][1] = 0.0f;
1269 R[2][2] = 1.0f;
1270 break;
1271 default:
1273 break;
1274 }
1275}
1276
1277void angle_to_mat2(float R[2][2], const float angle)
1278{
1279 const float angle_cos = cosf(angle);
1280 const float angle_sin = sinf(angle);
1281
1282 /* 2D rotation matrix */
1283 R[0][0] = angle_cos;
1284 R[0][1] = angle_sin;
1285 R[1][0] = -angle_sin;
1286 R[1][1] = angle_cos;
1287}
1288
1289void axis_angle_to_quat_single(float q[4], const char axis, const float angle)
1290{
1291 const float angle_half = angle * 0.5f;
1292 const float angle_cos = cosf(angle_half);
1293 const float angle_sin = sinf(angle_half);
1294 const int axis_index = (axis - 'X');
1295
1296 BLI_assert(axis >= 'X' && axis <= 'Z');
1297
1298 q[0] = angle_cos;
1299 zero_v3(q + 1);
1300 q[axis_index + 1] = angle_sin;
1301}
1302
1303/****************************** Exponential Map ******************************/
1304
1305void quat_normalized_to_expmap(float expmap[3], const float q[4])
1306{
1307 float angle;
1309
1310 /* Obtain axis/angle representation. */
1311 quat_to_axis_angle(expmap, &angle, q);
1312
1313 /* Convert to exponential map. */
1314 mul_v3_fl(expmap, angle);
1315}
1316
1317void quat_to_expmap(float expmap[3], const float q[4])
1318{
1319 float q_no[4];
1320 normalize_qt_qt(q_no, q);
1321 quat_normalized_to_expmap(expmap, q_no);
1322}
1323
1324void expmap_to_quat(float r[4], const float expmap[3])
1325{
1326 float axis[3];
1327 float angle;
1328
1329 /* Obtain axis/angle representation. */
1330 if (LIKELY((angle = normalize_v3_v3(axis, expmap)) != 0.0f)) {
1332 }
1333 else {
1334 unit_qt(r);
1335 }
1336}
1337
1338/******************************** XYZ Eulers *********************************/
1339
1340void eul_to_mat3(float mat[3][3], const float eul[3])
1341{
1342 double ci, cj, ch, si, sj, sh, cc, cs, sc, ss;
1343
1344 ci = cos(eul[0]);
1345 cj = cos(eul[1]);
1346 ch = cos(eul[2]);
1347 si = sin(eul[0]);
1348 sj = sin(eul[1]);
1349 sh = sin(eul[2]);
1350 cc = ci * ch;
1351 cs = ci * sh;
1352 sc = si * ch;
1353 ss = si * sh;
1354
1355 mat[0][0] = (float)(cj * ch);
1356 mat[1][0] = (float)(sj * sc - cs);
1357 mat[2][0] = (float)(sj * cc + ss);
1358 mat[0][1] = (float)(cj * sh);
1359 mat[1][1] = (float)(sj * ss + cc);
1360 mat[2][1] = (float)(sj * cs - sc);
1361 mat[0][2] = (float)-sj;
1362 mat[1][2] = (float)(cj * si);
1363 mat[2][2] = (float)(cj * ci);
1364}
1365
1366void eul_to_mat4(float mat[4][4], const float eul[3])
1367{
1368 double ci, cj, ch, si, sj, sh, cc, cs, sc, ss;
1369
1370 ci = cos(eul[0]);
1371 cj = cos(eul[1]);
1372 ch = cos(eul[2]);
1373 si = sin(eul[0]);
1374 sj = sin(eul[1]);
1375 sh = sin(eul[2]);
1376 cc = ci * ch;
1377 cs = ci * sh;
1378 sc = si * ch;
1379 ss = si * sh;
1380
1381 mat[0][0] = (float)(cj * ch);
1382 mat[1][0] = (float)(sj * sc - cs);
1383 mat[2][0] = (float)(sj * cc + ss);
1384 mat[0][1] = (float)(cj * sh);
1385 mat[1][1] = (float)(sj * ss + cc);
1386 mat[2][1] = (float)(sj * cs - sc);
1387 mat[0][2] = (float)-sj;
1388 mat[1][2] = (float)(cj * si);
1389 mat[2][2] = (float)(cj * ci);
1390
1391 mat[3][0] = mat[3][1] = mat[3][2] = mat[0][3] = mat[1][3] = mat[2][3] = 0.0f;
1392 mat[3][3] = 1.0f;
1393}
1394
1395/* returns two euler calculation methods, so we can pick the best */
1396
1397/* XYZ order */
1398static void mat3_normalized_to_eul2(const float mat[3][3], float eul1[3], float eul2[3])
1399{
1400 const float cy = hypotf(mat[0][0], mat[0][1]);
1401
1402 BLI_ASSERT_UNIT_M3(mat);
1403
1404 if (cy > (float)EULER_HYPOT_EPSILON) {
1405 eul1[0] = atan2f(mat[1][2], mat[2][2]);
1406 eul1[1] = atan2f(-mat[0][2], cy);
1407 eul1[2] = atan2f(mat[0][1], mat[0][0]);
1408
1409 eul2[0] = atan2f(-mat[1][2], -mat[2][2]);
1410 eul2[1] = atan2f(-mat[0][2], -cy);
1411 eul2[2] = atan2f(-mat[0][1], -mat[0][0]);
1412 }
1413 else {
1414 eul1[0] = atan2f(-mat[2][1], mat[1][1]);
1415 eul1[1] = atan2f(-mat[0][2], cy);
1416 eul1[2] = 0.0f;
1417
1418 copy_v3_v3(eul2, eul1);
1419 }
1420}
1421
1422void mat3_normalized_to_eul(float eul[3], const float mat[3][3])
1423{
1424 float eul1[3], eul2[3];
1425
1426 mat3_normalized_to_eul2(mat, eul1, eul2);
1427
1428 /* return best, which is just the one with lowest values it in */
1429 if (fabsf(eul1[0]) + fabsf(eul1[1]) + fabsf(eul1[2]) >
1430 fabsf(eul2[0]) + fabsf(eul2[1]) + fabsf(eul2[2]))
1431 {
1432 copy_v3_v3(eul, eul2);
1433 }
1434 else {
1435 copy_v3_v3(eul, eul1);
1436 }
1437}
1438void mat3_to_eul(float eul[3], const float mat[3][3])
1439{
1440 float unit_mat[3][3];
1441 normalize_m3_m3(unit_mat, mat);
1442 mat3_normalized_to_eul(eul, unit_mat);
1443}
1444
1445void mat4_normalized_to_eul(float eul[3], const float m[4][4])
1446{
1447 float mat3[3][3];
1448 copy_m3_m4(mat3, m);
1449 mat3_normalized_to_eul(eul, mat3);
1450}
1451void mat4_to_eul(float eul[3], const float mat[4][4])
1452{
1453 float mat3[3][3];
1454 copy_m3_m4(mat3, mat);
1455 mat3_to_eul(eul, mat3);
1456}
1457
1458void quat_to_eul(float eul[3], const float quat[4])
1459{
1460 float unit_mat[3][3];
1461 quat_to_mat3(unit_mat, quat);
1462 mat3_normalized_to_eul(eul, unit_mat);
1463}
1464
1465void eul_to_quat(float quat[4], const float eul[3])
1466{
1467 float ti, tj, th, ci, cj, ch, si, sj, sh, cc, cs, sc, ss;
1468
1469 ti = eul[0] * 0.5f;
1470 tj = eul[1] * 0.5f;
1471 th = eul[2] * 0.5f;
1472 ci = cosf(ti);
1473 cj = cosf(tj);
1474 ch = cosf(th);
1475 si = sinf(ti);
1476 sj = sinf(tj);
1477 sh = sinf(th);
1478 cc = ci * ch;
1479 cs = ci * sh;
1480 sc = si * ch;
1481 ss = si * sh;
1482
1483 quat[0] = cj * cc + sj * ss;
1484 quat[1] = cj * sc - sj * cs;
1485 quat[2] = cj * ss + sj * cc;
1486 quat[3] = cj * cs - sj * sc;
1487}
1488
1489void rotate_eul(float beul[3], const char axis, const float angle)
1490{
1491 float eul[3], mat1[3][3], mat2[3][3], totmat[3][3];
1492
1493 BLI_assert(axis >= 'X' && axis <= 'Z');
1494
1495 eul[0] = eul[1] = eul[2] = 0.0f;
1496 if (axis == 'X') {
1497 eul[0] = angle;
1498 }
1499 else if (axis == 'Y') {
1500 eul[1] = angle;
1501 }
1502 else {
1503 eul[2] = angle;
1504 }
1505
1506 eul_to_mat3(mat1, eul);
1507 eul_to_mat3(mat2, beul);
1508
1509 mul_m3_m3m3(totmat, mat2, mat1);
1510
1511 mat3_to_eul(beul, totmat);
1512}
1513
1514void compatible_eul(float eul[3], const float oldrot[3])
1515{
1516 /* When the rotation exceeds 180 degrees, it can be wrapped by 360 degrees
1517 * to produce a closer match.
1518 * NOTE: Values between `pi` & `2 * pi` work, where `pi` has the lowest number of
1519 * discontinuities and values approaching `2 * pi` center the resulting rotation around zero,
1520 * at the expense of the result being less compatible, see !104856. */
1521 const float pi_thresh = (float)M_PI;
1522 const float pi_x2 = (2.0f * (float)M_PI);
1523
1524 float deul[3];
1525 uint i;
1526
1527 /* Correct differences around 360 degrees first. */
1528 for (i = 0; i < 3; i++) {
1529 deul[i] = eul[i] - oldrot[i];
1530 if (deul[i] > pi_thresh) {
1531 eul[i] -= floorf((deul[i] / pi_x2) + 0.5f) * pi_x2;
1532 deul[i] = eul[i] - oldrot[i];
1533 }
1534 else if (deul[i] < -pi_thresh) {
1535 eul[i] += floorf((-deul[i] / pi_x2) + 0.5f) * pi_x2;
1536 deul[i] = eul[i] - oldrot[i];
1537 }
1538 }
1539
1540 uint j = 1, k = 2;
1541 for (i = 0; i < 3; j = k, k = i++) {
1542 /* Check if this axis of rotations larger than 180 degrees and
1543 * the others are smaller than 90 degrees. */
1544 if (fabsf(deul[i]) > M_PI && fabsf(deul[j]) < M_PI_2 && fabsf(deul[k]) < M_PI_2) {
1545 if (deul[i] > 0.0f) {
1546 eul[i] -= pi_x2;
1547 }
1548 else {
1549 eul[i] += pi_x2;
1550 }
1551 }
1552 }
1553}
1554
1555/* uses 2 methods to retrieve eulers, and picks the closest */
1556
1557void mat3_normalized_to_compatible_eul(float eul[3], const float oldrot[3], float mat[3][3])
1558{
1559 float eul1[3], eul2[3];
1560 float d1, d2;
1561
1562 mat3_normalized_to_eul2(mat, eul1, eul2);
1563
1564 compatible_eul(eul1, oldrot);
1565 compatible_eul(eul2, oldrot);
1566
1567 d1 = fabsf(eul1[0] - oldrot[0]) + fabsf(eul1[1] - oldrot[1]) + fabsf(eul1[2] - oldrot[2]);
1568 d2 = fabsf(eul2[0] - oldrot[0]) + fabsf(eul2[1] - oldrot[1]) + fabsf(eul2[2] - oldrot[2]);
1569
1570 /* return best, which is just the one with lowest difference */
1571 if (d1 > d2) {
1572 copy_v3_v3(eul, eul2);
1573 }
1574 else {
1575 copy_v3_v3(eul, eul1);
1576 }
1577}
1578void mat3_to_compatible_eul(float eul[3], const float oldrot[3], float mat[3][3])
1579{
1580 float unit_mat[3][3];
1581 normalize_m3_m3(unit_mat, mat);
1582 mat3_normalized_to_compatible_eul(eul, oldrot, unit_mat);
1583}
1584
1585void quat_to_compatible_eul(float eul[3], const float oldrot[3], const float quat[4])
1586{
1587 float unit_mat[3][3];
1588 quat_to_mat3(unit_mat, quat);
1589 mat3_normalized_to_compatible_eul(eul, oldrot, unit_mat);
1590}
1591
1592/************************** Arbitrary Order Eulers ***************************/
1593
1594/* Euler Rotation Order Code:
1595 * was adapted from
1596 * ANSI C code from the article
1597 * "Euler Angle Conversion"
1598 * by Ken Shoemake <shoemake@graphics.cis.upenn.edu>
1599 * in "Graphics Gems IV", Academic Press, 1994
1600 * for use in Blender
1601 */
1602
1603/* Type for rotation order info - see wiki for derivation details */
1604typedef struct RotOrderInfo {
1605 short axis[3];
1606 short parity; /* parity of axis permutation (even=0, odd=1) - 'n' in original code */
1608
1609/* Array of info for Rotation Order calculations
1610 * WARNING: must be kept in same order as eEulerRotationOrders
1611 */
1612static const RotOrderInfo rotOrders[] = {
1613 /* i, j, k, n */
1614 {{0, 1, 2}, 0}, /* XYZ */
1615 {{0, 2, 1}, 1}, /* XZY */
1616 {{1, 0, 2}, 1}, /* YXZ */
1617 {{1, 2, 0}, 0}, /* YZX */
1618 {{2, 0, 1}, 0}, /* ZXY */
1619 {{2, 1, 0}, 1} /* ZYX */
1620};
1621
1622/* Get relevant pointer to rotation order set from the array
1623 * NOTE: since we start at 1 for the values, but arrays index from 0,
1624 * there is -1 factor involved in this process...
1625 */
1626static const RotOrderInfo *get_rotation_order_info(const short order)
1627{
1628 BLI_assert(order >= 0 && order <= 6);
1629 if (order < 1) {
1630 return &rotOrders[0];
1631 }
1632 if (order < 6) {
1633 return &rotOrders[order - 1];
1634 }
1635
1636 return &rotOrders[5];
1637}
1638
1639void eulO_to_quat(float q[4], const float e[3], const short order)
1640{
1641 const RotOrderInfo *R = get_rotation_order_info(order);
1642 short i = R->axis[0], j = R->axis[1], k = R->axis[2];
1643 double ti, tj, th, ci, cj, ch, si, sj, sh, cc, cs, sc, ss;
1644 double a[3];
1645
1646 ti = (double)e[i] * 0.5;
1647 tj = (double)e[j] * (R->parity ? -0.5 : 0.5);
1648 th = (double)e[k] * 0.5;
1649
1650 ci = cos(ti);
1651 cj = cos(tj);
1652 ch = cos(th);
1653 si = sin(ti);
1654 sj = sin(tj);
1655 sh = sin(th);
1656
1657 cc = ci * ch;
1658 cs = ci * sh;
1659 sc = si * ch;
1660 ss = si * sh;
1661
1662 a[i] = cj * sc - sj * cs;
1663 a[j] = cj * ss + sj * cc;
1664 a[k] = cj * cs - sj * sc;
1665
1666 q[0] = (float)(cj * cc + sj * ss);
1667 q[1] = (float)(a[0]);
1668 q[2] = (float)(a[1]);
1669 q[3] = (float)(a[2]);
1670
1671 if (R->parity) {
1672 q[j + 1] = -q[j + 1];
1673 }
1674}
1675
1676void quat_to_eulO(float e[3], short const order, const float q[4])
1677{
1678 float unit_mat[3][3];
1679
1680 quat_to_mat3(unit_mat, q);
1681 mat3_normalized_to_eulO(e, order, unit_mat);
1682}
1683
1684void eulO_to_mat3(float M[3][3], const float e[3], const short order)
1685{
1686 const RotOrderInfo *R = get_rotation_order_info(order);
1687 short i = R->axis[0], j = R->axis[1], k = R->axis[2];
1688 double ti, tj, th, ci, cj, ch, si, sj, sh, cc, cs, sc, ss;
1689
1690 if (R->parity) {
1691 ti = -e[i];
1692 tj = -e[j];
1693 th = -e[k];
1694 }
1695 else {
1696 ti = e[i];
1697 tj = e[j];
1698 th = e[k];
1699 }
1700
1701 ci = cos(ti);
1702 cj = cos(tj);
1703 ch = cos(th);
1704 si = sin(ti);
1705 sj = sin(tj);
1706 sh = sin(th);
1707
1708 cc = ci * ch;
1709 cs = ci * sh;
1710 sc = si * ch;
1711 ss = si * sh;
1712
1713 M[i][i] = (float)(cj * ch);
1714 M[j][i] = (float)(sj * sc - cs);
1715 M[k][i] = (float)(sj * cc + ss);
1716 M[i][j] = (float)(cj * sh);
1717 M[j][j] = (float)(sj * ss + cc);
1718 M[k][j] = (float)(sj * cs - sc);
1719 M[i][k] = (float)(-sj);
1720 M[j][k] = (float)(cj * si);
1721 M[k][k] = (float)(cj * ci);
1722}
1723
1724/* returns two euler calculation methods, so we can pick the best */
1725static void mat3_normalized_to_eulo2(const float mat[3][3],
1726 float eul1[3],
1727 float eul2[3],
1728 const short order)
1729{
1730 const RotOrderInfo *R = get_rotation_order_info(order);
1731 short i = R->axis[0], j = R->axis[1], k = R->axis[2];
1732 float cy;
1733
1734 BLI_ASSERT_UNIT_M3(mat);
1735
1736 cy = hypotf(mat[i][i], mat[i][j]);
1737
1738 if (cy > (float)EULER_HYPOT_EPSILON) {
1739 eul1[i] = atan2f(mat[j][k], mat[k][k]);
1740 eul1[j] = atan2f(-mat[i][k], cy);
1741 eul1[k] = atan2f(mat[i][j], mat[i][i]);
1742
1743 eul2[i] = atan2f(-mat[j][k], -mat[k][k]);
1744 eul2[j] = atan2f(-mat[i][k], -cy);
1745 eul2[k] = atan2f(-mat[i][j], -mat[i][i]);
1746 }
1747 else {
1748 eul1[i] = atan2f(-mat[k][j], mat[j][j]);
1749 eul1[j] = atan2f(-mat[i][k], cy);
1750 eul1[k] = 0;
1751
1752 copy_v3_v3(eul2, eul1);
1753 }
1754
1755 if (R->parity) {
1756 negate_v3(eul1);
1757 negate_v3(eul2);
1758 }
1759}
1760
1761void eulO_to_mat4(float mat[4][4], const float e[3], const short order)
1762{
1763 float unit_mat[3][3];
1764
1765 /* for now, we'll just do this the slow way (i.e. copying matrices) */
1766 eulO_to_mat3(unit_mat, e, order);
1767 copy_m4_m3(mat, unit_mat);
1768}
1769
1770void mat3_normalized_to_eulO(float eul[3], const short order, const float m[3][3])
1771{
1772 float eul1[3], eul2[3];
1773 float d1, d2;
1774
1775 mat3_normalized_to_eulo2(m, eul1, eul2, order);
1776
1777 d1 = fabsf(eul1[0]) + fabsf(eul1[1]) + fabsf(eul1[2]);
1778 d2 = fabsf(eul2[0]) + fabsf(eul2[1]) + fabsf(eul2[2]);
1779
1780 /* return best, which is just the one with lowest values it in */
1781 if (d1 > d2) {
1782 copy_v3_v3(eul, eul2);
1783 }
1784 else {
1785 copy_v3_v3(eul, eul1);
1786 }
1787}
1788void mat3_to_eulO(float eul[3], const short order, const float m[3][3])
1789{
1790 float unit_mat[3][3];
1791 normalize_m3_m3(unit_mat, m);
1792 mat3_normalized_to_eulO(eul, order, unit_mat);
1793}
1794
1795void mat4_normalized_to_eulO(float eul[3], const short order, const float m[4][4])
1796{
1797 float mat3[3][3];
1798
1799 /* for now, we'll just do this the slow way (i.e. copying matrices) */
1800 copy_m3_m4(mat3, m);
1801 mat3_normalized_to_eulO(eul, order, mat3);
1802}
1803
1804void mat4_to_eulO(float eul[3], const short order, const float m[4][4])
1805{
1806 float mat3[3][3];
1807 copy_m3_m4(mat3, m);
1808 normalize_m3(mat3);
1809 mat3_normalized_to_eulO(eul, order, mat3);
1810}
1811
1813 const float oldrot[3],
1814 const short order,
1815 const float mat[3][3])
1816{
1817 float eul1[3], eul2[3];
1818 float d1, d2;
1819
1820 mat3_normalized_to_eulo2(mat, eul1, eul2, order);
1821
1822 compatible_eul(eul1, oldrot);
1823 compatible_eul(eul2, oldrot);
1824
1825 d1 = fabsf(eul1[0] - oldrot[0]) + fabsf(eul1[1] - oldrot[1]) + fabsf(eul1[2] - oldrot[2]);
1826 d2 = fabsf(eul2[0] - oldrot[0]) + fabsf(eul2[1] - oldrot[1]) + fabsf(eul2[2] - oldrot[2]);
1827
1828 /* return best, which is just the one with lowest difference */
1829 if (d1 > d2) {
1830 copy_v3_v3(eul, eul2);
1831 }
1832 else {
1833 copy_v3_v3(eul, eul1);
1834 }
1835}
1836void mat3_to_compatible_eulO(float eul[3],
1837 const float oldrot[3],
1838 const short order,
1839 const float mat[3][3])
1840{
1841 float unit_mat[3][3];
1842
1843 normalize_m3_m3(unit_mat, mat);
1844 mat3_normalized_to_compatible_eulO(eul, oldrot, order, unit_mat);
1845}
1846
1848 const float oldrot[3],
1849 const short order,
1850 const float mat[4][4])
1851{
1852 float mat3[3][3];
1853
1854 /* for now, we'll just do this the slow way (i.e. copying matrices) */
1855 copy_m3_m4(mat3, mat);
1856 mat3_normalized_to_compatible_eulO(eul, oldrot, order, mat3);
1857}
1858void mat4_to_compatible_eulO(float eul[3],
1859 const float oldrot[3],
1860 const short order,
1861 const float mat[4][4])
1862{
1863 float mat3[3][3];
1864
1865 /* for now, we'll just do this the slow way (i.e. copying matrices) */
1866 copy_m3_m4(mat3, mat);
1867 normalize_m3(mat3);
1868 mat3_normalized_to_compatible_eulO(eul, oldrot, order, mat3);
1869}
1870
1871void quat_to_compatible_eulO(float eul[3],
1872 const float oldrot[3],
1873 const short order,
1874 const float quat[4])
1875{
1876 float unit_mat[3][3];
1877
1878 quat_to_mat3(unit_mat, quat);
1879 mat3_normalized_to_compatible_eulO(eul, oldrot, order, unit_mat);
1880}
1881
1882/* rotate the given euler by the given angle on the specified axis */
1883/* NOTE: is this safe to do with different axis orders? */
1884
1885void rotate_eulO(float beul[3], const short order, const char axis, const float angle)
1886{
1887 float eul[3], mat1[3][3], mat2[3][3], totmat[3][3];
1888
1889 BLI_assert(axis >= 'X' && axis <= 'Z');
1890
1891 zero_v3(eul);
1892
1893 if (axis == 'X') {
1894 eul[0] = angle;
1895 }
1896 else if (axis == 'Y') {
1897 eul[1] = angle;
1898 }
1899 else {
1900 eul[2] = angle;
1901 }
1902
1903 eulO_to_mat3(mat1, eul, order);
1904 eulO_to_mat3(mat2, beul, order);
1905
1906 mul_m3_m3m3(totmat, mat2, mat1);
1907
1908 mat3_to_eulO(beul, order, totmat);
1909}
1910
1911void eulO_to_gimbal_axis(float gmat[3][3], const float eul[3], const short order)
1912{
1913 const RotOrderInfo *R = get_rotation_order_info(order);
1914
1915 float mat[3][3];
1916 float teul[3];
1917
1918 /* first axis is local */
1919 eulO_to_mat3(mat, eul, order);
1920 copy_v3_v3(gmat[R->axis[0]], mat[R->axis[0]]);
1921
1922 /* second axis is local minus first rotation */
1923 copy_v3_v3(teul, eul);
1924 teul[R->axis[0]] = 0;
1925 eulO_to_mat3(mat, teul, order);
1926 copy_v3_v3(gmat[R->axis[1]], mat[R->axis[1]]);
1927
1928 /* Last axis is global */
1929 zero_v3(gmat[R->axis[2]]);
1930 gmat[R->axis[2]][R->axis[2]] = 1;
1931}
1932
1933void add_eul_euleul(float r_eul[3], float a[3], float b[3], const short order)
1934{
1935 float quat[4], quat_b[4];
1936
1937 eulO_to_quat(quat, a, order);
1938 eulO_to_quat(quat_b, b, order);
1939
1940 mul_qt_qtqt(quat, quat_b, quat);
1941
1942 quat_to_eulO(r_eul, order, quat);
1943}
1944
1945void sub_eul_euleul(float r_eul[3], float a[3], float b[3], const short order)
1946{
1947 float quat[4], quat_b[4];
1948
1949 eulO_to_quat(quat, a, order);
1950 eulO_to_quat(quat_b, b, order);
1951
1952 invert_qt_normalized(quat_b);
1953 mul_qt_qtqt(quat, quat_b, quat);
1954
1955 quat_to_eulO(r_eul, order, quat);
1956}
1957
1958/******************************* Dual Quaternions ****************************/
1959
1960/* Conversion routines between (regular quaternion, translation) and dual quaternion.
1961 *
1962 * Version 1.0.0, February 7th, 2007
1963 *
1964 * SPDX-License-Identifier: Zlib
1965 * Copyright 2006-2007 University of Dublin, Trinity College, All Rights Reserved.
1966 *
1967 * Changes for Blender:
1968 * - renaming, style changes and optimization's
1969 * - added support for scaling
1970 */
1971
1972void mat4_to_dquat(DualQuat *dq, const float basemat[4][4], const float mat[4][4])
1973{
1974 float dscale[3], scale[3], basequat[4], mat3[3][3];
1975 float baseRS[4][4], baseinv[4][4], baseR[4][4], baseRinv[4][4];
1976 float R[4][4], S[4][4];
1977
1978 /* split scaling and rotation, there is probably a faster way to do
1979 * this, it's done like this now to correctly get negative scaling */
1980 mul_m4_m4m4(baseRS, mat, basemat);
1981 mat4_to_size(scale, baseRS);
1982
1983 dscale[0] = scale[0] - 1.0f;
1984 dscale[1] = scale[1] - 1.0f;
1985 dscale[2] = scale[2] - 1.0f;
1986
1987 copy_m3_m4(mat3, mat);
1988
1989 if (!is_orthonormal_m3(mat3) || (determinant_m4(mat) < 0.0f) ||
1990 len_squared_v3(dscale) > square_f(1e-4f))
1991 {
1992 /* Extract R and S. */
1993 float tmp[4][4];
1994
1995 /* extra orthogonalize, to avoid flipping with stretched bones */
1996 copy_m4_m4(tmp, baseRS);
1997 orthogonalize_m4(tmp, 1);
1998 mat4_to_quat(basequat, tmp);
1999
2000 quat_to_mat4(baseR, basequat);
2001 copy_v3_v3(baseR[3], baseRS[3]);
2002
2003 invert_m4_m4(baseinv, basemat);
2004 mul_m4_m4m4(R, baseR, baseinv);
2005
2006 invert_m4_m4(baseRinv, baseR);
2007 mul_m4_m4m4(S, baseRinv, baseRS);
2008
2009 /* set scaling part */
2010 mul_m4_series(dq->scale, basemat, S, baseinv);
2011 dq->scale_weight = 1.0f;
2012 }
2013 else {
2014 /* matrix does not contain scaling */
2015 copy_m4_m4(R, mat);
2016 dq->scale_weight = 0.0f;
2017 }
2018
2019 /* non-dual part */
2020 mat4_to_quat(dq->quat, R);
2021
2022 /* dual part */
2023 const float *t = R[3];
2024 const float *q = dq->quat;
2025 dq->trans[0] = -0.5f * (t[0] * q[1] + t[1] * q[2] + t[2] * q[3]);
2026 dq->trans[1] = 0.5f * (t[0] * q[0] + t[1] * q[3] - t[2] * q[2]);
2027 dq->trans[2] = 0.5f * (-t[0] * q[3] + t[1] * q[0] + t[2] * q[1]);
2028 dq->trans[3] = 0.5f * (t[0] * q[2] - t[1] * q[1] + t[2] * q[0]);
2029}
2030
2031void dquat_to_mat4(float R[4][4], const DualQuat *dq)
2032{
2033 float len, q0[4];
2034 const float *t;
2035
2036 /* regular quaternion */
2037 copy_qt_qt(q0, dq->quat);
2038
2039 /* normalize */
2040 len = sqrtf(dot_qtqt(q0, q0));
2041 if (len != 0.0f) {
2042 len = 1.0f / len;
2043 }
2044 mul_qt_fl(q0, len);
2045
2046 /* rotation */
2047 quat_to_mat4(R, q0);
2048
2049 /* translation */
2050 t = dq->trans;
2051 R[3][0] = 2.0f * (-t[0] * q0[1] + t[1] * q0[0] - t[2] * q0[3] + t[3] * q0[2]) * len;
2052 R[3][1] = 2.0f * (-t[0] * q0[2] + t[1] * q0[3] + t[2] * q0[0] - t[3] * q0[1]) * len;
2053 R[3][2] = 2.0f * (-t[0] * q0[3] - t[1] * q0[2] + t[2] * q0[1] + t[3] * q0[0]) * len;
2054
2055 /* scaling */
2056 if (dq->scale_weight) {
2057 mul_m4_m4m4(R, R, dq->scale);
2058 }
2059}
2060
2061void add_weighted_dq_dq(DualQuat *dq_sum, const DualQuat *dq, float weight)
2062{
2063 bool flipped = false;
2064
2065 /* make sure we interpolate quats in the right direction */
2066 if (dot_qtqt(dq->quat, dq_sum->quat) < 0) {
2067 flipped = true;
2068 weight = -weight;
2069 }
2070
2071 /* interpolate rotation and translation */
2072 dq_sum->quat[0] += weight * dq->quat[0];
2073 dq_sum->quat[1] += weight * dq->quat[1];
2074 dq_sum->quat[2] += weight * dq->quat[2];
2075 dq_sum->quat[3] += weight * dq->quat[3];
2076
2077 dq_sum->trans[0] += weight * dq->trans[0];
2078 dq_sum->trans[1] += weight * dq->trans[1];
2079 dq_sum->trans[2] += weight * dq->trans[2];
2080 dq_sum->trans[3] += weight * dq->trans[3];
2081
2082 /* Interpolate scale - but only if there is scale present. If any dual
2083 * quaternions without scale are added, they will be compensated for in
2084 * normalize_dq. */
2085 if (dq->scale_weight) {
2086 float wmat[4][4];
2087
2088 if (flipped) {
2089 /* we don't want negative weights for scaling */
2090 weight = -weight;
2091 }
2092
2093 copy_m4_m4(wmat, (float(*)[4])dq->scale);
2094 mul_m4_fl(wmat, weight);
2095 add_m4_m4m4(dq_sum->scale, dq_sum->scale, wmat);
2096 dq_sum->scale_weight += weight;
2097 }
2098}
2099
2109 const DualQuat *dq,
2110 const float pivot[3],
2111 const float weight,
2112 const bool compute_scale_matrix)
2113{
2114 /* FIX #32022, #43188, #100373 - bad deformation when combining scaling and rotation. */
2115 if (dq->scale_weight) {
2116 DualQuat mdq = *dq;
2117
2118 /* Compute the translation induced by scale at the pivot point. */
2119 float dst[3];
2120 mul_v3_m4v3(dst, mdq.scale, pivot);
2121 sub_v3_v3(dst, pivot);
2122
2123 /* Apply the scale translation to the translation part of the DualQuat. */
2124 mdq.trans[0] -= .5f * (mdq.quat[1] * dst[0] + mdq.quat[2] * dst[1] + mdq.quat[3] * dst[2]);
2125 mdq.trans[1] += .5f * (mdq.quat[0] * dst[0] + mdq.quat[2] * dst[2] - mdq.quat[3] * dst[1]);
2126 mdq.trans[2] += .5f * (mdq.quat[0] * dst[1] + mdq.quat[3] * dst[0] - mdq.quat[1] * dst[2]);
2127 mdq.trans[3] += .5f * (mdq.quat[0] * dst[2] + mdq.quat[1] * dst[1] - mdq.quat[2] * dst[0]);
2128
2129 /* Neutralize the scale matrix at the pivot point. */
2130 if (compute_scale_matrix) {
2131 /* This translates the matrix to transform the pivot point to itself. */
2132 sub_v3_v3(mdq.scale[3], dst);
2133 }
2134 else {
2135 /* This completely discards the scale matrix - if the resulting DualQuat
2136 * is converted to a matrix, it would have no scale or shear. */
2137 mdq.scale_weight = 0.0f;
2138 }
2139
2140 add_weighted_dq_dq(dq_sum, &mdq, weight);
2141 }
2142 else {
2143 add_weighted_dq_dq(dq_sum, dq, weight);
2144 }
2145}
2146
2147void normalize_dq(DualQuat *dq, float totweight)
2148{
2149 const float scale = 1.0f / totweight;
2150
2151 mul_qt_fl(dq->quat, scale);
2152 mul_qt_fl(dq->trans, scale);
2153
2154 /* Handle scale if needed. */
2155 if (dq->scale_weight) {
2156 /* Compensate for any dual quaternions added without scale. This is an
2157 * optimization so that we can skip the scale part when not needed. */
2158 float addweight = totweight - dq->scale_weight;
2159
2160 if (addweight) {
2161 dq->scale[0][0] += addweight;
2162 dq->scale[1][1] += addweight;
2163 dq->scale[2][2] += addweight;
2164 dq->scale[3][3] += addweight;
2165 }
2166
2167 mul_m4_fl(dq->scale, scale);
2168 dq->scale_weight = 1.0f;
2169 }
2170}
2171
2172void mul_v3m3_dq(float r[3], float R[3][3], DualQuat *dq)
2173{
2174 float M[3][3], t[3], scalemat[3][3], len2;
2175 float w = dq->quat[0], x = dq->quat[1], y = dq->quat[2], z = dq->quat[3];
2176 float t0 = dq->trans[0], t1 = dq->trans[1], t2 = dq->trans[2], t3 = dq->trans[3];
2177
2178 /* rotation matrix */
2179 M[0][0] = w * w + x * x - y * y - z * z;
2180 M[1][0] = 2 * (x * y - w * z);
2181 M[2][0] = 2 * (x * z + w * y);
2182
2183 M[0][1] = 2 * (x * y + w * z);
2184 M[1][1] = w * w + y * y - x * x - z * z;
2185 M[2][1] = 2 * (y * z - w * x);
2186
2187 M[0][2] = 2 * (x * z - w * y);
2188 M[1][2] = 2 * (y * z + w * x);
2189 M[2][2] = w * w + z * z - x * x - y * y;
2190
2191 len2 = dot_qtqt(dq->quat, dq->quat);
2192 if (len2 > 0.0f) {
2193 len2 = 1.0f / len2;
2194 }
2195
2196 /* translation */
2197 t[0] = 2 * (-t0 * x + w * t1 - t2 * z + y * t3);
2198 t[1] = 2 * (-t0 * y + t1 * z - x * t3 + w * t2);
2199 t[2] = 2 * (-t0 * z + x * t2 + w * t3 - t1 * y);
2200
2201 /* apply scaling */
2202 if (dq->scale_weight) {
2203 mul_m4_v3(dq->scale, r);
2204 }
2205
2206 /* apply rotation and translation */
2207 mul_m3_v3(M, r);
2208 r[0] = (r[0] + t[0]) * len2;
2209 r[1] = (r[1] + t[1]) * len2;
2210 r[2] = (r[2] + t[2]) * len2;
2211
2212 /* Compute crazy-space correction matrix. */
2213 if (R) {
2214 if (dq->scale_weight) {
2215 copy_m3_m4(scalemat, dq->scale);
2216 mul_m3_m3m3(R, M, scalemat);
2217 }
2218 else {
2219 copy_m3_m3(R, M);
2220 }
2221 mul_m3_fl(R, len2);
2222 }
2223}
2224
2225void copy_dq_dq(DualQuat *r, const DualQuat *dq)
2226{
2227 memcpy(r, dq, sizeof(DualQuat));
2228}
2229
2230void quat_apply_track(float quat[4], short axis, short upflag)
2231{
2232 /* rotations are hard coded to match vec_to_quat */
2233 const float sqrt_1_2 = (float)M_SQRT1_2;
2234 const float quat_track[][4] = {
2235 /* pos-y90 */
2236 {sqrt_1_2, 0.0, -sqrt_1_2, 0.0},
2237 /* Quaternion((1,0,0), radians(90)) * Quaternion((0,1,0), radians(90)) */
2238 {0.5, 0.5, 0.5, 0.5},
2239 /* pos-z90 */
2240 {sqrt_1_2, 0.0, 0.0, sqrt_1_2},
2241 /* neg-y90 */
2242 {sqrt_1_2, 0.0, sqrt_1_2, 0.0},
2243 /* Quaternion((1,0,0), radians(-90)) * Quaternion((0,1,0), radians(-90)) */
2244 {0.5, -0.5, -0.5, 0.5},
2245 /* no rotation */
2246 {0.0, sqrt_1_2, sqrt_1_2, 0.0},
2247 };
2248
2249 BLI_assert(axis >= 0 && axis <= 5);
2250 BLI_assert(upflag >= 0 && upflag <= 2);
2251
2252 mul_qt_qtqt(quat, quat, quat_track[axis]);
2253
2254 if (axis > 2) {
2255 axis = (short)(axis - 3);
2256 }
2257
2258 /* there are 2 possible up-axis for each axis used, the 'quat_track' applies so the first
2259 * up axis is used X->Y, Y->X, Z->X, if this first up axis isn't used then rotate 90d
2260 * the strange bit shift below just find the low axis {X:Y, Y:X, Z:X} */
2261 if (upflag != (2 - axis) >> 1) {
2262 float q[4] = {sqrt_1_2, 0.0, 0.0, 0.0}; /* assign 90d rotation axis */
2263 q[axis + 1] = (axis == 1) ? sqrt_1_2 : -sqrt_1_2; /* flip non Y axis */
2264 mul_qt_qtqt(quat, quat, q);
2265 }
2266}
2267
2268void vec_apply_track(float vec[3], short axis)
2269{
2270 float tvec[3];
2271
2272 BLI_assert(axis >= 0 && axis <= 5);
2273
2274 copy_v3_v3(tvec, vec);
2275
2276 switch (axis) {
2277 case 0: /* pos-x */
2278 // vec[0] = 0.0;
2279 vec[1] = tvec[2];
2280 vec[2] = -tvec[1];
2281 break;
2282 case 1: /* pos-y */
2283 // vec[0] = tvec[0];
2284 // vec[1] = 0.0;
2285 // vec[2] = tvec[2];
2286 break;
2287 case 2: /* pos-z */
2288 // vec[0] = tvec[0];
2289 // vec[1] = tvec[1];
2290 // vec[2] = 0.0;
2291 break;
2292 case 3: /* neg-x */
2293 // vec[0] = 0.0;
2294 vec[1] = tvec[2];
2295 vec[2] = -tvec[1];
2296 break;
2297 case 4: /* neg-y */
2298 vec[0] = -tvec[2];
2299 // vec[1] = 0.0;
2300 vec[2] = tvec[0];
2301 break;
2302 case 5: /* neg-z */
2303 vec[0] = -tvec[0];
2304 vec[1] = -tvec[1];
2305 // vec[2] = 0.0;
2306 break;
2307 }
2308}
2309
2310float focallength_to_fov(float focal_length, float sensor)
2311{
2312 return 2.0f * atanf((sensor / 2.0f) / focal_length);
2313}
2314
2315float fov_to_focallength(float hfov, float sensor)
2316{
2317 return (sensor / 2.0f) / tanf(hfov * 0.5f);
2318}
2319
2320/* 'mod_inline(-3, 4)= 1', 'fmod(-3, 4)= -3' */
2321static float mod_inline(float a, float b)
2322{
2323 return a - (b * floorf(a / b));
2324}
2325
2326float angle_wrap_rad(float angle)
2327{
2328 return mod_inline(angle + (float)M_PI, (float)M_PI * 2.0f) - (float)M_PI;
2329}
2330
2331float angle_wrap_deg(float angle)
2332{
2333 return mod_inline(angle + 180.0f, 360.0f) - 180.0f;
2334}
2335
2336float angle_compat_rad(float angle, float angle_compat)
2337{
2338 return angle_compat + angle_wrap_rad(angle - angle_compat);
2339}
2340
2341/* axis conversion */
2342static float _axis_convert_matrix[23][3][3] = {
2343 {{-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}},
2344 {{-1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, -1.0, 0.0}},
2345 {{-1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 1.0, 0.0}},
2346 {{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, 0.0, -1.0}},
2347 {{0.0, -1.0, 0.0}, {-1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}},
2348 {{0.0, 0.0, 1.0}, {-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}},
2349 {{0.0, 0.0, -1.0}, {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}},
2350 {{0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}},
2351 {{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {-1.0, 0.0, 0.0}},
2352 {{0.0, 0.0, -1.0}, {0.0, -1.0, 0.0}, {-1.0, 0.0, 0.0}},
2353 {{0.0, 0.0, 1.0}, {0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}},
2354 {{0.0, 1.0, 0.0}, {0.0, 0.0, -1.0}, {-1.0, 0.0, 0.0}},
2355 {{0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}, {1.0, 0.0, 0.0}},
2356 {{0.0, 0.0, 1.0}, {0.0, -1.0, 0.0}, {1.0, 0.0, 0.0}},
2357 {{0.0, 0.0, -1.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}},
2358 {{0.0, 1.0, 0.0}, {0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}},
2359 {{0.0, -1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}},
2360 {{0.0, 0.0, -1.0}, {1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}},
2361 {{0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}},
2362 {{0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}},
2363 {{1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0}},
2364 {{1.0, 0.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, -1.0, 0.0}},
2365 {{1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}, {0.0, 1.0, 0.0}},
2366};
2367
2368static int _axis_convert_lut[23][24] = {
2369 {0x8C8, 0x4D0, 0x2E0, 0xAE8, 0x701, 0x511, 0x119, 0xB29, 0x682, 0x88A, 0x09A, 0x2A2,
2370 0x80B, 0x413, 0x223, 0xA2B, 0x644, 0x454, 0x05C, 0xA6C, 0x745, 0x94D, 0x15D, 0x365},
2371 {0xAC8, 0x8D0, 0x4E0, 0x2E8, 0x741, 0x951, 0x159, 0x369, 0x702, 0xB0A, 0x11A, 0x522,
2372 0xA0B, 0x813, 0x423, 0x22B, 0x684, 0x894, 0x09C, 0x2AC, 0x645, 0xA4D, 0x05D, 0x465},
2373 {0x4C8, 0x2D0, 0xAE0, 0x8E8, 0x681, 0x291, 0x099, 0x8A9, 0x642, 0x44A, 0x05A, 0xA62,
2374 0x40B, 0x213, 0xA23, 0x82B, 0x744, 0x354, 0x15C, 0x96C, 0x705, 0x50D, 0x11D, 0xB25},
2375 {0x2C8, 0xAD0, 0x8E0, 0x4E8, 0x641, 0xA51, 0x059, 0x469, 0x742, 0x34A, 0x15A, 0x962,
2376 0x20B, 0xA13, 0x823, 0x42B, 0x704, 0xB14, 0x11C, 0x52C, 0x685, 0x28D, 0x09D, 0x8A5},
2377 {0x708, 0xB10, 0x120, 0x528, 0x8C1, 0xAD1, 0x2D9, 0x4E9, 0x942, 0x74A, 0x35A, 0x162,
2378 0x64B, 0xA53, 0x063, 0x46B, 0x804, 0xA14, 0x21C, 0x42C, 0x885, 0x68D, 0x29D, 0x0A5},
2379 {0xB08, 0x110, 0x520, 0x728, 0x941, 0x151, 0x359, 0x769, 0x802, 0xA0A, 0x21A, 0x422,
2380 0xA4B, 0x053, 0x463, 0x66B, 0x884, 0x094, 0x29C, 0x6AC, 0x8C5, 0xACD, 0x2DD, 0x4E5},
2381 {0x508, 0x710, 0xB20, 0x128, 0x881, 0x691, 0x299, 0x0A9, 0x8C2, 0x4CA, 0x2DA, 0xAE2,
2382 0x44B, 0x653, 0xA63, 0x06B, 0x944, 0x754, 0x35C, 0x16C, 0x805, 0x40D, 0x21D, 0xA25},
2383 {0x108, 0x510, 0x720, 0xB28, 0x801, 0x411, 0x219, 0xA29, 0x882, 0x08A, 0x29A, 0x6A2,
2384 0x04B, 0x453, 0x663, 0xA6B, 0x8C4, 0x4D4, 0x2DC, 0xAEC, 0x945, 0x14D, 0x35D, 0x765},
2385 {0x748, 0x350, 0x160, 0x968, 0xAC1, 0x2D1, 0x4D9, 0x8E9, 0xA42, 0x64A, 0x45A, 0x062,
2386 0x68B, 0x293, 0x0A3, 0x8AB, 0xA04, 0x214, 0x41C, 0x82C, 0xB05, 0x70D, 0x51D, 0x125},
2387 {0x948, 0x750, 0x360, 0x168, 0xB01, 0x711, 0x519, 0x129, 0xAC2, 0x8CA, 0x4DA, 0x2E2,
2388 0x88B, 0x693, 0x2A3, 0x0AB, 0xA44, 0x654, 0x45C, 0x06C, 0xA05, 0x80D, 0x41D, 0x225},
2389 {0x348, 0x150, 0x960, 0x768, 0xA41, 0x051, 0x459, 0x669, 0xA02, 0x20A, 0x41A, 0x822,
2390 0x28B, 0x093, 0x8A3, 0x6AB, 0xB04, 0x114, 0x51C, 0x72C, 0xAC5, 0x2CD, 0x4DD, 0x8E5},
2391 {0x148, 0x950, 0x760, 0x368, 0xA01, 0x811, 0x419, 0x229, 0xB02, 0x10A, 0x51A, 0x722,
2392 0x08B, 0x893, 0x6A3, 0x2AB, 0xAC4, 0x8D4, 0x4DC, 0x2EC, 0xA45, 0x04D, 0x45D, 0x665},
2393 {0x688, 0x890, 0x0A0, 0x2A8, 0x4C1, 0x8D1, 0xAD9, 0x2E9, 0x502, 0x70A, 0xB1A, 0x122,
2394 0x74B, 0x953, 0x163, 0x36B, 0x404, 0x814, 0xA1C, 0x22C, 0x445, 0x64D, 0xA5D, 0x065},
2395 {0x888, 0x090, 0x2A0, 0x6A8, 0x501, 0x111, 0xB19, 0x729, 0x402, 0x80A, 0xA1A, 0x222,
2396 0x94B, 0x153, 0x363, 0x76B, 0x444, 0x054, 0xA5C, 0x66C, 0x4C5, 0x8CD, 0xADD, 0x2E5},
2397 {0x288, 0x690, 0x8A0, 0x0A8, 0x441, 0x651, 0xA59, 0x069, 0x4C2, 0x2CA, 0xADA, 0x8E2,
2398 0x34B, 0x753, 0x963, 0x16B, 0x504, 0x714, 0xB1C, 0x12C, 0x405, 0x20D, 0xA1D, 0x825},
2399 {0x088, 0x290, 0x6A0, 0x8A8, 0x401, 0x211, 0xA19, 0x829, 0x442, 0x04A, 0xA5A, 0x662,
2400 0x14B, 0x353, 0x763, 0x96B, 0x4C4, 0x2D4, 0xADC, 0x8EC, 0x505, 0x10D, 0xB1D, 0x725},
2401 {0x648, 0x450, 0x060, 0xA68, 0x2C1, 0x4D1, 0x8D9, 0xAE9, 0x282, 0x68A, 0x89A, 0x0A2,
2402 0x70B, 0x513, 0x123, 0xB2B, 0x204, 0x414, 0x81C, 0xA2C, 0x345, 0x74D, 0x95D, 0x165},
2403 {0xA48, 0x650, 0x460, 0x068, 0x341, 0x751, 0x959, 0x169, 0x2C2, 0xACA, 0x8DA, 0x4E2,
2404 0xB0B, 0x713, 0x523, 0x12B, 0x284, 0x694, 0x89C, 0x0AC, 0x205, 0xA0D, 0x81D, 0x425},
2405 {0x448, 0x050, 0xA60, 0x668, 0x281, 0x091, 0x899, 0x6A9, 0x202, 0x40A, 0x81A, 0xA22,
2406 0x50B, 0x113, 0xB23, 0x72B, 0x344, 0x154, 0x95C, 0x76C, 0x2C5, 0x4CD, 0x8DD, 0xAE5},
2407 {0x048, 0xA50, 0x660, 0x468, 0x201, 0xA11, 0x819, 0x429, 0x342, 0x14A, 0x95A, 0x762,
2408 0x10B, 0xB13, 0x723, 0x52B, 0x2C4, 0xAD4, 0x8DC, 0x4EC, 0x285, 0x08D, 0x89D, 0x6A5},
2409 {0x808, 0xA10, 0x220, 0x428, 0x101, 0xB11, 0x719, 0x529, 0x142, 0x94A, 0x75A, 0x362,
2410 0x8CB, 0xAD3, 0x2E3, 0x4EB, 0x044, 0xA54, 0x65C, 0x46C, 0x085, 0x88D, 0x69D, 0x2A5},
2411 {0xA08, 0x210, 0x420, 0x828, 0x141, 0x351, 0x759, 0x969, 0x042, 0xA4A, 0x65A, 0x462,
2412 0xACB, 0x2D3, 0x4E3, 0x8EB, 0x084, 0x294, 0x69C, 0x8AC, 0x105, 0xB0D, 0x71D, 0x525},
2413 {0x408, 0x810, 0xA20, 0x228, 0x081, 0x891, 0x699, 0x2A9, 0x102, 0x50A, 0x71A, 0xB22,
2414 0x4CB, 0x8D3, 0xAE3, 0x2EB, 0x144, 0x954, 0x75C, 0x36C, 0x045, 0x44D, 0x65D, 0xA65},
2415};
2416
2417// _axis_convert_num = {'X': 0, 'Y': 1, 'Z': 2, '-X': 3, '-Y': 4, '-Z': 5}
2418
2419BLI_INLINE int _axis_signed(const int axis)
2420{
2421 return (axis < 3) ? axis : axis - 3;
2422}
2423
2425 int src_forward, int src_up, int dst_forward, int dst_up, float r_mat[3][3])
2426{
2427 int value;
2428
2429 if (src_forward == dst_forward && src_up == dst_up) {
2430 unit_m3(r_mat);
2431 return false;
2432 }
2433
2434 if ((_axis_signed(src_forward) == _axis_signed(src_up)) ||
2435 (_axis_signed(dst_forward) == _axis_signed(dst_up)))
2436 {
2437 /* we could assert here! */
2438 unit_m3(r_mat);
2439 return false;
2440 }
2441
2442 value = ((src_forward << (0 * 3)) | (src_up << (1 * 3)) | (dst_forward << (2 * 3)) |
2443 (dst_up << (3 * 3)));
2444
2445 for (uint i = 0; i < ARRAY_SIZE(_axis_convert_matrix); i++) {
2446 for (uint j = 0; j < ARRAY_SIZE(*_axis_convert_lut); j++) {
2447 if (_axis_convert_lut[i][j] == value) {
2449 return true;
2450 }
2451 }
2452 }
2453 // BLI_assert(0);
2454 return false;
2455}
2456
2457bool mat3_from_axis_conversion_single(int src_axis, int dst_axis, float r_mat[3][3])
2458{
2459 if (src_axis == dst_axis) {
2460 unit_m3(r_mat);
2461 return false;
2462 }
2463
2464 /* Pick predictable next axis. */
2465 int src_axis_next = (src_axis + 1) % 3;
2466 int dst_axis_next = (dst_axis + 1) % 3;
2467
2468 if ((src_axis < 3) != (dst_axis < 3)) {
2469 /* Flip both axis so matrix sign remains positive. */
2470 dst_axis_next += 3;
2471 }
2472
2473 return mat3_from_axis_conversion(src_axis, src_axis_next, dst_axis, dst_axis_next, r_mat);
2474}
#define BLI_assert_unreachable()
Definition BLI_assert.h:97
#define BLI_assert(a)
Definition BLI_assert.h:50
#define BLI_INLINE
#define BLI_ASSERT_UNIT_EPSILON
#define M_SQRT2
#define BLI_ASSERT_UNIT_M3(m)
#define M_PI_2
MINLINE float square_f(float a)
#define BLI_ASSERT_UNIT_QUAT(q)
#define M_SQRT1_2
#define BLI_ASSERT_UNIT_V3(v)
#define M_PI
MINLINE float safe_acosf(float a)
float normal_tri_v3(float n[3], const float v1[3], const float v2[3], const float v3[3])
Definition math_geom.cc:39
bool is_negative_m3(const float mat[3][3])
void negate_m3(float R[3][3])
void orthogonalize_m4(float R[4][4], int axis)
void mul_m3_v3(const float M[3][3], float r[3])
void mul_m4_fl(float R[4][4], float f)
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void normalize_m3_m3(float R[3][3], const float M[3][3]) ATTR_NONNULL()
void copy_m3_m3(float m1[3][3], const float m2[3][3])
void unit_m3(float m[3][3])
void add_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
void copy_m3_m4(float m1[3][3], const float m2[4][4])
void mul_m3_fl(float R[3][3], float f)
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void unit_m4(float m[4][4])
Definition rct.c:1127
void copy_m4_m3(float m1[4][4], const float m2[3][3])
bool is_orthonormal_m3(const float m[3][3])
void normalize_m3(float R[3][3]) ATTR_NONNULL()
void mul_m4_v3(const float M[4][4], float r[3])
#define mul_m4_series(...)
float determinant_m4(const float m[4][4])
void copy_m4_m4(float m1[4][4], const float m2[4][4])
void mul_v3_m4v3(float r[3], const float mat[4][4], const float vec[3])
float determinant_m3_array(const float m[3][3])
bool invert_m4_m4(float inverse[4][4], const float mat[4][4])
void mat4_to_size(float size[3], const float M[4][4])
void mul_m3_m3m3(float R[3][3], const float A[3][3], const float B[3][3])
MINLINE void copy_v4_v4(float r[4], const float a[4])
MINLINE float len_squared_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
void ortho_v3_v3(float out[3], const float v[3])
MINLINE void negate_v4_v4(float r[4], const float a[4])
MINLINE void negate_v3(float r[3])
MINLINE float normalize_v3_v3(float r[3], const float a[3])
MINLINE float len_squared_v4v4(const float a[4], const float b[4]) ATTR_WARN_UNUSED_RESULT
MINLINE bool is_zero_v3(const float v[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void zero_v3(float r[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
MINLINE float normalize_v3_length(float n[3], float unit_length)
MINLINE float normalize_v3(float n[3])
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
unsigned int uint
#define ARRAY_SIZE(arr)
#define SWAP(type, a, b)
#define UNLIKELY(x)
#define ELEM(...)
#define IN_RANGE_INCL(a, b, c)
#define LIKELY(x)
typedef double(DMatrix)[4][4]
static double angle(const Eigen::Vector3d &v1, const Eigen::Vector3d &v2)
Definition IK_Math.h:125
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
local_group_size(16, 16) .push_constant(Type b
#define printf
#define sinf(x)
#define cosf(x)
#define tanf(x)
#define atan2f(x, y)
#define atanf(x)
#define hypotf(x, y)
#define floorf(x)
#define acosf(x)
#define fabsf(x)
#define sqrtf(x)
int len
draw_view in_light_buf[] float
#define str(s)
ccl_device_inline float2 fabs(const float2 a)
ccl_device_inline float3 cos(float3 v)
float angle_compat_rad(float angle, float angle_compat)
void rotation_between_quats_to_quat(float q[4], const float q1[4], const float q2[4])
void eulO_to_quat(float q[4], const float e[3], const short order)
float angle_qtqt(const float q1[4], const float q2[4])
void angle_to_mat2(float R[2][2], const float angle)
void mat3_normalized_to_eulO(float eul[3], const short order, const float m[3][3])
float angle_signed_normalized_qtqt(const float q1[4], const float q2[4])
void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3])
void print_qt(const char *str, const float q[4])
void vec_apply_track(float vec[3], short axis)
static void mat3_normalized_to_eulo2(const float mat[3][3], float eul1[3], float eul2[3], const short order)
float angle_normalized_qt(const float q[4])
float tri_to_quat(float q[4], const float a[3], const float b[3], const float c[3])
void mat3_normalized_to_compatible_eul(float eul[3], const float oldrot[3], float mat[3][3])
void quat_to_compatible_eulO(float eul[3], const float oldrot[3], const short order, const float quat[4])
void add_eul_euleul(float r_eul[3], float a[3], float b[3], const short order)
float angle_signed_qt(const float q[4])
void invert_qt_normalized(float q[4])
void invert_qt_qt(float q1[4], const float q2[4])
void conjugate_qt_qt(float q1[4], const float q2[4])
void eul_to_mat3(float mat[3][3], const float eul[3])
static float mod_inline(float a, float b)
void add_weighted_dq_dq_pivot(DualQuat *dq_sum, const DualQuat *dq, const float pivot[3], const float weight, const bool compute_scale_matrix)
void add_weighted_dq_dq(DualQuat *dq_sum, const DualQuat *dq, float weight)
void eulO_to_axis_angle(float axis[3], float *angle, const float eul[3], const short order)
void mul_qt_fl(float q[4], const float f)
static const RotOrderInfo * get_rotation_order_info(const short order)
void quat_to_mat3(float m[3][3], const float q[4])
void mat3_to_eulO(float eul[3], const short order, const float m[3][3])
void axis_angle_normalized_to_mat3_ex(float mat[3][3], const float axis[3], const float angle_sin, const float angle_cos)
void sin_cos_from_fraction(int numerator, int denominator, float *r_sin, float *r_cos)
void axis_angle_to_mat3_single(float R[3][3], const char axis, const float angle)
void sub_qt_qtqt(float q[4], const float a[4], const float b[4])
void mat3_to_quat(float q[4], const float mat[3][3])
void mat4_to_eul(float eul[3], const float mat[4][4])
void axis_angle_to_quat(float r[4], const float axis[3], const float angle)
float normalize_qt(float q[4])
void invert_qt(float q[4])
void quat_normalized_to_expmap(float expmap[3], const float q[4])
void quat_to_mat4(float m[4][4], const float q[4])
void dquat_to_mat4(float R[4][4], const DualQuat *dq)
float quat_split_swing_and_twist(const float q_in[4], const int axis, float r_swing[4], float r_twist[4])
void mat4_normalized_to_axis_angle(float axis[3], float *angle, const float mat[4][4])
void mat4_normalized_to_eulO(float eul[3], const short order, const float m[4][4])
void mul_qt_v3(const float q[4], float r[3])
void unit_qt(float q[4])
void mat3_normalized_to_compatible_eulO(float eul[3], const float oldrot[3], const short order, const float mat[3][3])
void quat_to_eulO(float e[3], short const order, const float q[4])
void axis_angle_normalized_to_mat3(float R[3][3], const float axis[3], const float angle)
void sub_eul_euleul(float r_eul[3], float a[3], float b[3], const short order)
#define QUAT_EPSILON
void eul_to_quat(float quat[4], const float eul[3])
void mat4_to_eulO(float eul[3], const short order, const float m[4][4])
float fov_to_focallength(float hfov, float sensor)
void axis_angle_normalized_to_quat(float r[4], const float axis[3], const float angle)
void pow_qt_fl_normalized(float q[4], const float fac)
void mat4_to_compatible_eulO(float eul[3], const float oldrot[3], const short order, const float mat[4][4])
void mat3_to_quat_legacy(float q[4], const float wmat[3][3])
void mat4_to_axis_angle(float axis[3], float *angle, const float mat[4][4])
void mat4_normalized_to_eul(float eul[3], const float m[4][4])
float angle_signed_normalized_qt(const float q[4])
void quat_to_eul(float eul[3], const float quat[4])
float normalize_qt_qt(float r[4], const float q[4])
float dot_qtqt(const float a[4], const float b[4])
void invert_qt_qt_normalized(float q1[4], const float q2[4])
static void mat3_normalized_to_eul2(const float mat[3][3], float eul1[3], float eul2[3])
bool is_zero_qt(const float q[4])
void mat3_normalized_to_eul(float eul[3], const float mat[3][3])
void mul_qt_qtqt(float q[4], const float a[4], const float b[4])
void axis_angle_to_quat_single(float q[4], const char axis, const float angle)
void quat_to_axis_angle(float axis[3], float *angle, const float q[4])
void mat3_to_eul(float eul[3], const float mat[3][3])
void quat_to_expmap(float expmap[3], const float q[4])
static float _axis_convert_matrix[23][3][3]
#define EULER_HYPOT_EPSILON
static void quat_to_mat3_no_error(float m[3][3], const float q[4])
void mat4_to_dquat(DualQuat *dq, const float basemat[4][4], const float mat[4][4])
void copy_dq_dq(DualQuat *r, const DualQuat *dq)
void mat3_to_compatible_eul(float eul[3], const float oldrot[3], float mat[3][3])
void axis_angle_to_eulO(float eul[3], const short order, const float axis[3], const float angle)
void mat4_to_quat(float q[4], const float mat[4][4])
void rotate_eul(float beul[3], const char axis, const float angle)
void rotation_between_vecs_to_mat3(float m[3][3], const float v1[3], const float v2[3])
bool mat3_from_axis_conversion(int src_forward, int src_up, int dst_forward, int dst_up, float r_mat[3][3])
void mat4_normalized_to_compatible_eulO(float eul[3], const float oldrot[3], const short order, const float mat[4][4])
float focallength_to_fov(float focal_length, float sensor)
static int _axis_convert_lut[23][24]
void axis_angle_to_mat3(float R[3][3], const float axis[3], const float angle)
void mul_v3m3_dq(float r[3], float R[3][3], DualQuat *dq)
void interp_qt_qtqt(float q[4], const float a[4], const float b[4], const float t)
void conjugate_qt(float q[4])
void compatible_eul(float eul[3], const float oldrot[3])
void quat_apply_track(float quat[4], short axis, short upflag)
void mat3_to_compatible_eulO(float eul[3], const float oldrot[3], const short order, const float mat[3][3])
void tri_to_quat_ex(float quat[4], const float v1[3], const float v2[3], const float v3[3], const float no_orig[3])
void eulO_to_mat4(float mat[4][4], const float e[3], const short order)
void axis_angle_to_mat4(float R[4][4], const float axis[3], const float angle)
void mat4_normalized_to_quat(float q[4], const float mat[4][4])
void mat3_normalized_to_quat_fast(float q[4], const float mat[3][3])
void quat_to_compatible_eul(float eul[3], const float oldrot[3], const float quat[4])
static void mat3_normalized_to_quat_with_checks(float q[4], float mat[3][3])
void axis_angle_to_mat4_single(float R[4][4], const char axis, const float angle)
float angle_wrap_deg(float angle)
void unit_axis_angle(float axis[3], float *angle)
void mat3_to_axis_angle(float axis[3], float *angle, const float mat[3][3])
struct RotOrderInfo RotOrderInfo
float angle_qt(const float q[4])
void copy_qt_qt(float q[4], const float a[4])
void rotate_eulO(float beul[3], const short order, const char axis, const float angle)
float angle_wrap_rad(float angle)
void normalize_dq(DualQuat *dq, float totweight)
void eulO_to_mat3(float M[3][3], const float e[3], const short order)
void quat_to_compatible_quat(float q[4], const float a[4], const float old[4])
void expmap_to_quat(float r[4], const float expmap[3])
BLI_INLINE int _axis_signed(const int axis)
void mat3_normalized_to_quat(float q[4], const float mat[3][3])
void interp_dot_slerp(const float t, const float cosom, float r_w[2])
float angle_signed_qtqt(const float q1[4], const float q2[4])
static const RotOrderInfo rotOrders[]
void mat3_normalized_to_axis_angle(float axis[3], float *angle, const float mat[3][3])
float angle_normalized_qtqt(const float q1[4], const float q2[4])
void vec_to_quat(float q[4], const float vec[3], short axis, const short upflag)
void eul_to_mat4(float mat[4][4], const float eul[3])
void add_qt_qtqt(float q[4], const float a[4], const float b[4], const float t)
bool mat3_from_axis_conversion_single(int src_axis, int dst_axis, float r_mat[3][3])
void eulO_to_gimbal_axis(float gmat[3][3], const float eul[3], const short order)
#define M
#define R
T acos(const T &a)
Frequency::GEOMETRY nor[]
const btScalar eps
Definition poly34.cpp:11
float scale_weight
float scale[4][4]
float quat[4]
float trans[4]