-
Notifications
You must be signed in to change notification settings - Fork 0
/
quaternionf.h
56 lines (44 loc) · 1.67 KB
/
quaternionf.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
/*
* quaternion.h
*
* Created on: 19/mar/2015
* Author: mauro
*/
#ifndef MY_MATH_INCLUDE_QUATERNIONF_H_
#define MY_MATH_INCLUDE_QUATERNIONF_H_
#include "vector3f.h"
struct Quaternion4f {
float w, x, y, z;
};
static void conjugate(const struct Quaternion4f *origin, struct Quaternion4f *result) {
result->x = -origin->x;
result->y = -origin->y;
result->z = -origin->z;
result->w = origin->w;
}
static void multiply(const struct Quaternion4f *q1, const struct Quaternion4f *q2, struct Quaternion4f *result) {
result->w = q1->w * q2->w - q1->x * q2->x - q1->y * q2->y - q1->z * q2->z;
result->x = q1->w * q2->x + q2->w * q1->x + q1->y * q2->z - q1->z * q2->y;
result->y = q1->w * q2->y + q2->w * q1->y - q1->x * q2->z + q1->z * q2->x;
result->z = q1->w * q2->z + q2->w * q1->z + q1->x * q2->y - q1->y * q2->x;
}
static void transform(const struct Vector3f *v, const struct Quaternion4f *q, struct Vector3f *result) {
struct Quaternion4f tmp1;
struct Quaternion4f tmp2;
conjugate(q, &tmp2); /* conijugate Q and put result in tmp2 */
tmp1.x = v->x;
tmp1.y = v->y;
tmp1.z = v->z;
tmp1.w = 0;
multiply(&tmp1, &tmp2, &tmp2); /* mul tmp1 and tmp2, store in tmp2 */
multiply(q, &tmp2, &tmp2); /* mul q and tmp2, store in tmp2 */
result->x = tmp2.x;
result->y = tmp2.y;
result->z = tmp2.z;
}
static const struct {
void (*conjugate)(const struct Quaternion4f*, struct Quaternion4f*);
void (*multiply)(const struct Quaternion4f*, const struct Quaternion4f*, struct Quaternion4f*);
void (*transform)(const struct Vector3f*, const struct Quaternion4f*, struct Vector3f*);
} quaterionf_helper = { conjugate, multiply, transform };
#endif /* MY_MATH_INCLUDE_QUATERNIONF_H_ */