-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quaternion.hpp
164 lines (133 loc) · 3.7 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef __QUATERNION_HPP__
#define __QUATERNION_HPP__
#include <cassert>
class Quaternion
{
public:
Quaternion(float x,float y, float z, float w)
:_x(x),_y(y),_z(z),_w(w)
{
float x_squared = _x*_x;
float y_squared = _y*_y;
float z_squared = _z*_z;
float w_squared = _w*_w;
if (sqrt(x_squared + y_squared + z_squared + w_squared) > 1.01)
assert(false);
if (sqrt(x_squared + y_squared + z_squared + w_squared) < .99)
assert(false);
// first row
mult_data [0] = 1 - 2*y_squared - 2*z_squared;
mult_data [4] = 2*_x*_y - 2*_w*_z;
mult_data [8] = 2*_x*_z + 2*_w*_y;
mult_data [12] = 0;
// second row
mult_data[1] = 2*_x*_y + 2*_w*_z;
mult_data[5] = 1 - 2*x_squared - 2*z_squared;
mult_data[9] = 2*_y*_z - 2*_w*_x;
mult_data[13] = 0;
// third row
mult_data[2] = 2*_x*_z - 2*_w*_y;
mult_data[6] = 2*_w*_x + 2*_y*_z;
mult_data[10] = 1 - 2*x_squared - 2*y_squared;
mult_data[14] = 0;
// fourth row
mult_data[3] = 0;
mult_data[7] = 0;
mult_data[11] = 0;
mult_data[15] = 1;
// // first row
// mult_data [0] = 1 - 2*y_squared - 2*z_squared;
// mult_data [1] = 2*_x*_y - 2*_w*_z;
// mult_data [2] = 2*_x*_z + 2*_w*_y;
// mult_data [3] = 0;
// // second row
// mult_data[4] = 2*_x*_y + 2*_w*_z;
// mult_data[5] = 1 - 2*x_squared - 2*z_squared;
// mult_data[6] = 2*_y*_z - 2*_w*_x;
// mult_data[7] = 0;
// // third row
// mult_data[8] = 2*_x*_z - 2*_w*_y;
// mult_data[9] = 2*_w*_x + 2*_y*_z;
// mult_data[10] = 1 - 2*x_squared - 2*y_squared;
// mult_data[11] = 0;
// // fourth row
// mult_data[12] = 0;
// mult_data[13] = 0;
// mult_data[14] = 0;
// mult_data[15] = 1;
}
Quaternion(){}
~Quaternion(){}
const float* gl_mult_matrix() const
{
return mult_data;
}
// dot product
float operator* (const Quaternion& q) const
{
return q.x()*_x + q.y()*_y + q.z()*_z + q.w()*_w;
}
// cross product
Quaternion operator% (const Quaternion& q) const
{
return Quaternion(
_w*q.x() + _x*q.w() + _y*q.z() - _z*q.y(),
_w*q.y() - _x*q.z() + _y*q.w() + _z*q.x(),
_w*q.z() + _x*q.y() - _y*q.x() + _z*q.w(),
_w*q.w() - _x*q.x() - _y*q.y() - _z*q.z());
}
Quaternion operator% (const Point3& p) const
{
return Quaternion(
_w*p.x + _y*p.z - _z*p.y,
_w*p.y - _x*p.z + _z*p.x,
_w*p.z + _x*p.y - _y*p.x,
- _x*p.x - _y*p.y - _z*p.z);
}
bool operator==(const Quaternion& q) const
{
return ((q.x() == _x) && (q.y() == _y) &&
(q.z() == _z) && (q.w() == _w));
}
Quaternion operator- () const
{
return Quaternion(-_x,-_y,-_z,-_w);
}
static const Quaternion& identity()
{
static Quaternion identity (0,0,0,1);
return identity;
}
void normalize ()
{
float len = sqrt(_x*_x + _y*_y + _z*_z + _w*_w);
_x /= len;
_y /= len;
_z /= len;
_w /= len;
}
Quaternion conjugate() const
{
return Quaternion(-_x,-_y,-_z,_w);
}
float x() const
{
return _x;
}
float y() const
{
return _y;
}
float z() const
{
return _z;
}
float w() const
{
return _w;
}
private:
float _x,_y,_z,_w;
float mult_data[16];
};
#endif