-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuaternion.h
198 lines (170 loc) · 3.92 KB
/
Quaternion.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
----------------------------------------------------------------------
This file is revised from the Quaternion.h of Ray's code.
----------------------------------------------------------------------
*/
#ifndef _QUATERNION_H_
#define _QUATERNION_H_
#include"Angle.h"
#include<iostream>
#include"Matrix.h"
#include"Vector.h"
#include <cmath>
using namespace std;
class Quaternion
{
public:
/**
* Initialize to rotate 0 about Y-axis.
* @note
* n is cos(theta/2), not angle!
*/
Quaternion() : n(1), v(0,0,0) {}
Quaternion(Real n, Real x, Real y, Real z) : n(n), v(x, y, z)
{
// normalize the quaternion
v.normalize();
v *= 1 - n*n;
}
Quaternion(Real n, Vector3 v) : n(n), v(v)
{
// normalize the quaternion
v.normalize();
v *= 1 - n*n;
}
Quaternion(Angle angle, Vector3 axis)
{
n = cos(angle.getRadianValue()/2);
axis.normalize();
v = axis * sin(angle.getRadianValue()/2);
}
void set(Real n, Real x, Real y, Real z)
{
this->n=n;
v.set(x, y, z);
// normalize the quaternion
v.normalize();
v *= 1 - n*n;
}
void set(Angle angle, Vector3 axis)
{
n = cos(angle.getRadianValue()/2);
axis.normalize();
v = axis * sin(angle.getRadianValue()/2);
}
void normalize()
{
//!
Real len = sqrt(n*n + v.lengthSquare());
n /= len;
v /= len;
}
Real getRadianValue()
{
return (Real)(2*acos(n));
}
Angle getAngle()
{
return Radian(2*acos(n));
}
Vector3 getAxis()
{
return v.normalizedVector();
}
Quaternion operator~() ///< conjugate
{
return Quaternion(n, v*-1);
}
Quaternion operator*(Quaternion& q)
{
return Quaternion(n*q.n-v.dot(q.v),
//q.v*n+v*q.n+q.v.cross(v)); // note the order of cross product!
q.v*n+v*q.n+v.cross(q.v)); //! for debug
}
void operator*=(Quaternion& q)
{
Real scale;
Vector3 Vector;
scale = n*q.n-v.dot(q.v);
//Vector = q.v*n+v*q.n+q.v.cross(v); // note the order of cross product!
Vector = q.v*n+v*q.n+v.cross(q.v); //! for debug
n = scale;
v = Vector;
n = n>1 ? 1 : n; // n sometimes becomes 1.00002, etc.
n = n<-1 ? -1 : n;
}
Vector3 rotate(const Vector3& v)
{
Quaternion q;
q = *this * Quaternion(0, v) * ~(*this);
return q.v;
}
Vector3 rotatedVector(const Vector3& v)
{
Quaternion q;
q = *this * Quaternion(0, v) * ~(*this);
return q.v;
}
/**
* Convert to 3x3 rotation matrix.
*/
Matrix3x3 toMatrix3x3()
{
Matrix3x3 m;
m.v11 = n*n + v.x*v.x - v.y*v.y - v.z*v.z;
m.v21 = 2*v.x*v.y + 2*v.z*n;
m.v31 = 2*v.z*v.x - 2*v.y*n;
m.v12 = 2*v.x*v.y - 2*v.z*n;
m.v22 = n*n - v.x*v.x + v.y*v.y - v.z*v.z;
m.v32 = 2*v.z*v.y + 2*v.x*n;
m.v13 = 2*v.x*v.z + 2*v.y*n;
m.v23 = 2*v.y*v.z - 2*v.x*n;
m.v33 = n*n - v.x*v.x - v.y*v.y + v.z*v.z;
return m;
}
Matrix4x4 toMatrix4x4()
{
Matrix3x3 m;
m = this->toMatrix3x3();
return Matrix4x4(m.a, m.b, m.c, 0,
m.d, m.e, m.f, 0,
m.g, m.h, m.i, 0,
0, 0, 0, 1);
}
Vector3 toEulerAngle()
{
Real heading, attitude, bank;
attitude = 0;
if(v.x*v.y + v.z*w == 0.5) //north pole
{
heading = 2*atan2(v.x,w);
bank = 0;
}
else if(v.x*v.y + v.z*w == -0.5) //south pole
{
heading = -2*atan2(v.x,w);
bank = 0;
}
else
{
heading = atan2(2*v.y*w-2*v.x*v.z, 1 - 2*v.y*v.y - 2*v.z*v.z);
attitude = asin(2*v.x*v.y + 2*v.z*w);
bank = atan2(2*v.x*w-2*v.y*v.z , 1 - 2*v.x*v.x - 2*v.z*v.z);
}
return Vector3(heading, attitude, bank);
}
public:
union
{
Real n;
Real w;
};
Vector3 v;
static const Quaternion ZeroRotation;
};
inline std::ostream& operator<<(std::ostream& os, const Quaternion& q)
{
os<<q.n<<" "<<q.v<<endl;
return os;
}
#endif