-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquaternion.hpp
108 lines (83 loc) · 2 KB
/
quaternion.hpp
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
#ifndef QUATERNION_H
#define QUATERNION_H
#include <glm/glm.hpp>
#include <cmath>
#ifndef PI
#define PI 3.14159265359
#endif
// Quaternion class, based on the program quaternionAnimation.cpp, from the Computer Graphics Through OpengGL book code.
class Quaternion{
public:
Quaternion() {
Quaternion(0,0,0,0);
}
Quaternion(double w,double x, double y, double z) {
u.x=x;
u.y=y;
u.z=z;
this->w=w;
}
double w;
glm::vec3 u;
Quaternion operator* (const Quaternion &q)
{
Quaternion tmp= *this;
tmp.u.x = ((w * q.u.x) + (u.x * q.w) + (u.y * q.u.z) - (u.z * q.u.y));
tmp.u.y = ((w * q.u.y) - (u.x * q.u.z) + (u.y * q.w) + (u.z * q.u.x));
tmp.u.z = ((w * q.u.z) + (u.x * q.u.y) - (u.y * q.u.x) + (u.z * q.w));
tmp.w = ((w * q.w) - (u.x * q.u.x) - (u.y * q.u.y) - (u.z * q.u.z));
return tmp;
}
void to_matrix(double *matrix)
{
float wx, wy, wz, xx, yy, yz, xy, xz, zz;
xx = u.x * u.x;
xy = u.x * u.y;
xz = u.x * u.z;
yy = u.y * u.y;
zz = u.z * u.z;
yz = u.y * u.z;
wx = w * u.x;
wy = w * u.y;
wz = w * u.z;
matrix[0] = 1.0f - 2.0f*(yy + zz);
matrix[4] = 2.0f*(xy - wz);
matrix[8] = 2.0f*(xz + wy);
matrix[12] = 0.0;
matrix[1] = 2.0f*(xy + wz);
matrix[5] = 1.0f - 2.0f*(xx + zz);
matrix[9] = 2.0f*(yz - wx);
matrix[13] = 0.0;
matrix[2] = 2.0f*(xz - wy);
matrix[6] = 2.0f*(yz + wx);
matrix[10] = 1.0f - 2.0f*(xx + yy);
matrix[14] = 0.0;
matrix[3] = 0;
matrix[7] = 0;
matrix[11] = 0;
matrix[15] = 1;
}
double scale() {
return ((u.x*u.x) + (u.y*u.y) + (u.z*u.z));
}
bool is_identity() {
return ( w == 1.0 && u.x == 0.0 && u.y == 0.0 && u.z == 0.0 );
}
// RotAxis should be:
// double[4], where: {theta(radians), ax,ay,az}
void to_rotation_axis(double *rotAxis) {
double sc = scale();
if ( sc == 0.0 ) {
rotAxis[0] = 0.0;
rotAxis[1] = 0.0;
rotAxis[2] = 0.0;
rotAxis[3] = 0.0;
} else {
rotAxis[0] = 2.0 * acos(w);
rotAxis[1] = u.x / sc;
rotAxis[2] = u.y / sc;
rotAxis[3] = u.z / sc;
}
}
};
#endif