-
Notifications
You must be signed in to change notification settings - Fork 102
/
fixquat.h
73 lines (54 loc) · 1.81 KB
/
fixquat.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
/* Basic quaternion implementation based on libfixmath fix16_t datatype. */
#ifndef _FIXQUAT_H_
#define _FIXQUAT_H_
#include <fix16.h>
#include "fixmatrix.h"
#include "fixvector3d.h"
typedef struct {
fix16_t a; // Real part
fix16_t b; // i
fix16_t c; // j
fix16_t d; // k
} qf16;
// Conjugate of quaternion
void qf16_conj(qf16 *dest, const qf16 *q);
// Multiply two quaternions, dest = q * r.
void qf16_mul(qf16 *dest, const qf16 *q, const qf16 *r);
// Add two quaternions, dest = q + r
void qf16_add(qf16 *dest, const qf16 *q, const qf16 *r);
// Multiply quaternion by scalar
void qf16_mul_s(qf16 *dest, const qf16 *q, fix16_t s);
// Divide quaternion by scalar
void qf16_div_s(qf16 *dest, const qf16 *q, fix16_t s);
// Dot product of two quaternions
fix16_t qf16_dot(const qf16 *q, const qf16 *r);
// Quaternion norm
fix16_t qf16_norm(const qf16 *q);
// Normalize quaternion
void qf16_normalize(qf16 *dest, const qf16 *q);
// Quaternion power (exponentation)
void qf16_pow(qf16 *dest, const qf16 *q, fix16_t power);
// Weighted average of two quaternions
// Think of it as q = w * q1 + (1 - w) * q2, but the internal algorithm considers attitudes.
void qf16_avg(qf16 *dest, const qf16 *q1, const qf16 *q2, fix16_t weight);
// Unit quaternion from axis and angle.
// Axis should have unit length and angle in radians.
void qf16_from_axis_angle(qf16 *dest, const v3d *axis, fix16_t angle);
// Unit quaternion to rotation matrix
void qf16_to_matrix(mf16 *dest, const qf16 *q);
// Rotate vector using quaternion
void qf16_rotate(v3d *dest, const qf16 *q, const v3d *v);
static inline void qf16_from_v3d(qf16 *q, const v3d *v, fix16_t a)
{
q->a = a;
q->b = v->x;
q->c = v->y;
q->d = v->z;
}
static inline void qf16_to_v3d(v3d *v, const qf16 *q)
{
v->x = q->b;
v->y = q->c;
v->z = q->d;
}
#endif