rev |
line source |
nuclear@29
|
1 #include "vector.h"
|
nuclear@29
|
2 #include "matrix.h"
|
nuclear@29
|
3
|
nuclear@29
|
4 #ifdef __cplusplus
|
nuclear@29
|
5 extern "C" {
|
nuclear@29
|
6 #endif /* __cplusplus */
|
nuclear@29
|
7
|
nuclear@29
|
8 static inline quat_t quat_mul(quat_t q1, quat_t q2)
|
nuclear@29
|
9 {
|
nuclear@29
|
10 quat_t res;
|
nuclear@29
|
11 vec3_t v1 = quat_vec(q1);
|
nuclear@29
|
12 vec3_t v2 = quat_vec(q2);
|
nuclear@29
|
13
|
nuclear@29
|
14 res.w = q1.w * q2.w - v3_dot(v1, v2);
|
nuclear@29
|
15 /* resvec = v2 * q1 + v1 * q2 + cross(v1, v2) */
|
nuclear@29
|
16 res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);
|
nuclear@29
|
17 res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);
|
nuclear@29
|
18 res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);
|
nuclear@29
|
19 return res;
|
nuclear@29
|
20 }
|
nuclear@29
|
21
|
nuclear@29
|
22 static inline quat_t quat_conjugate(quat_t q)
|
nuclear@29
|
23 {
|
nuclear@29
|
24 q.x = -q.x;
|
nuclear@29
|
25 q.y = -q.y;
|
nuclear@29
|
26 q.z = -q.z;
|
nuclear@29
|
27 return q;
|
nuclear@29
|
28 }
|
nuclear@29
|
29
|
nuclear@29
|
30 static inline quat_t quat_inverse(quat_t q)
|
nuclear@29
|
31 {
|
nuclear@29
|
32 scalar_t lensq = quat_length_sq(q);
|
nuclear@29
|
33 q = quat_conjugate(q);
|
nuclear@29
|
34 q.x /= lensq;
|
nuclear@29
|
35 q.y /= lensq;
|
nuclear@29
|
36 q.z /= lensq;
|
nuclear@29
|
37 q.w /= lensq;
|
nuclear@29
|
38 return q;
|
nuclear@29
|
39 }
|
nuclear@29
|
40
|
nuclear@29
|
41 static inline void quat_to_mat3(mat3_t res, quat_t q)
|
nuclear@29
|
42 {
|
nuclear@29
|
43 m3_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z, 2.0 * q.z * q.x - 2.0 * q.w * q.y,
|
nuclear@29
|
44 2.0 * q.x * q.y - 2.0 * q.w * q.z, 1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x,
|
nuclear@29
|
45 2.0 * q.z * q.x + 2.0 * q.w * q.y, 2.0 * q.y * q.z - 2.0 * q.w * q.x, 1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y);
|
nuclear@29
|
46 }
|
nuclear@29
|
47
|
nuclear@29
|
48 static inline void quat_to_mat4(mat4_t res, quat_t q)
|
nuclear@29
|
49 {
|
nuclear@29
|
50 m4_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z, 2.0 * q.z * q.x - 2.0 * q.w * q.y, 0,
|
nuclear@29
|
51 2.0 * q.x * q.y - 2.0 * q.w * q.z, 1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x, 0,
|
nuclear@29
|
52 2.0 * q.z * q.x + 2.0 * q.w * q.y, 2.0 * q.y * q.z - 2.0 * q.w * q.x, 1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y, 0,
|
nuclear@29
|
53 0, 0, 0, 1);
|
nuclear@29
|
54 }
|
nuclear@29
|
55
|
nuclear@29
|
56 #ifdef __cplusplus
|
nuclear@29
|
57 } /* extern "C" */
|
nuclear@29
|
58
|
nuclear@29
|
59 inline Quaternion lerp(const Quaternion &a, const Quaternion &b, scalar_t t)
|
nuclear@29
|
60 {
|
nuclear@29
|
61 return slerp(a, b, t);
|
nuclear@29
|
62 }
|
nuclear@29
|
63 #endif /* __cplusplus */
|