-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuat4_.h
More file actions
302 lines (262 loc) · 7.91 KB
/
Quat4_.h
File metadata and controls
302 lines (262 loc) · 7.91 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#ifndef QUAT4__H
#define QUAT4__H
#include "VmUtil.h"
#include "Tuple4.h"
#include "Matrix4_.h"
VM_BEGIN_NS
/**
* A 4 element quaternion represented by x,y,z,w coordinates.
* @version specification 1.1, implementation $Revision: 1.3 $, $Date: 1999/10/06 02:52:46 $
* @author Kenji hiranabe
*/
template<class T>
class Quat4 : public Tuple4<T> {
/*
* $Log: Quat4_.h,v $
* Revision 1.3 1999/10/06 02:52:46 hiranabe
* Java3D 1.2 and namespace
*
* Revision 1.2 1999/05/26 00:59:37 hiranabe
* support Visual C++
*
* Revision 1.1 1999/03/04 11:07:09 hiranabe
* Initial revision
*
* Revision 1.1 1999/03/04 11:07:09 hiranabe
* Initial revision
*
*/
public:
/**
* Constructs and initializes a Quat4 from the specified xyzw coordinates.
* @param x the x coordinate
* @param y the y coordinate
* @param z the z coordinate
* @param w the w scalar component
*/
Quat4(T x, T y, T z, T w): Tuple4<T>(x, y, z, w) { }
/**
* Constructs and initializes a Quat4 from the array of length 4.
* @param v the array of length 4 containing xyzw in order
*/
Quat4(const T v[]): Tuple4<T>(v) { }
/**
* Constructs and initializes a Quat4 from the specified Tuple4d.
* @param t1 the Tuple4d containing the initialization x y z w data
*/
Quat4(const Tuple4<T>& t1): Tuple4<T>(t1) { }
/**
* Constructs and initializes a Quat4 to (0,0,0,0).
*/
Quat4(): Tuple4<T>() { }
/**
* Sets the value of this tuple to the value of tuple t1.
* note: other set methods hides this set.
* @param t1 the tuple to be copied
*/
void set(const Tuple4<T>& t1) {
Tuple4<T>::set(t1);
}
/**
* Sets the value of this tuple to the specified xyzw coordinates.
* note: other set methods hides this set.
* @param x the x coordinate
* @param y the y coordinate
* @param z the z coordinate
* @param w the w coordinate
*/
void set(T x, T y, T z, T w) {
Tuple4<T>::set(x, y, z, w);
}
/**
* Sets the value of this quaternion to the conjugate of quaternion q1.
* @param q1 the source vector
*/
void conjugate(const Quat4& q1) {
this->x = -q1.x;
this->y = -q1.y;
this->z = -q1.z;
this->w = q1.w;
}
/**
* Negate the value of of each of this quaternion's x,y,z coordinates
* in place.
*/
void conjugate() {
this->x = -this->x;
this->y = -this->y;
this->z = -this->z;
}
/**
* Sets the value of this quaternion to the quaternion product of
* quaternions q1 and q2 (this = q1 * q2).
* Note that this is safe for aliasing (e.g. this can be q1 or q2).
* @param q1 the first quaternion
* @param q2 the second quaternion
*/
void mul(const Quat4& q1, const Quat4& q2);
/**
* Sets the value of this quaternion to the quaternion product of
* itself and q1 (this = this * q1).
* @param q1 the other quaternion
*/
void mul(const Quat4& q1);
/**
*
* Multiplies quaternion q1 by the inverse of quaternion q2 and places
* the value into this quaternion. The value of both argument quaternions
* is preservered (this = q1 * q2^-1).
* @param q1 the left quaternion
* @param q2 the right quaternion
*/
void mulInverse(const Quat4& q1, const Quat4& q2);
/**
* Multiplies this quaternion by the inverse of quaternion q1 and places
* the value into this quaternion. The value of the argument quaternion
* is preserved (this = this * q^-1).
* @param q1 the other quaternion
*/
void mulInverse(const Quat4& q1);
protected:
T norm() const {
return this->x*this->x +this-> y*this->y + this->z*this->z + this->w*this->w;
}
void setFromMat(T m00, T m01, T m02,
T m10, T m11, T m12,
T m20, T m21, T m22);
public:
/**
* Sets the value of this quaternion to quaternion inverse of quaternion q1.
* @param q1 the quaternion to be inverted
*/
void inverse(const Quat4& q1) {
T n = q1.norm();
// zero-div may occur.
this->x = -q1.x/n;
this->y = -q1.y/n;
this->z = -q1.z/n;
this->w = q1.w/n;
}
/**
* Sets the value of this quaternion to the quaternion inverse of itself.
*/
void inverse() {
T n = norm();
// zero-div may occur.
this->x = -this->x/n;
this->y = -this->y/n;
this->z = -this->z/n;
this->w /= n;
}
/**
* Sets the value of this quaternion to the normalized value
* of quaternion q1.
* @param q1 the quaternion to be normalized.
*/
void normalize(const Quat4& q1) {
T n = VmUtil<T>::sqrt(q1.norm());
// zero-div may occur.
this->x = q1.x/n;
this->y = q1.y/n;
this->z = q1.z/n;
this->w = q1.w/n;
}
/**
* Normalizes the value of this quaternion in place.
*/
void normalize() {
T n = VmUtil<T>::sqrt(norm());
// zero-div may occur.
this->x /= n;
this->y /= n;
this->z /= n;
this->w /= n;
}
#if 0
/**
* Sets the value of this quaternion to the rotational component of
* the passed matrix.
* @param m1 the matrix4f
*/
void set(Matrix4f m1) {
setFromMat(
m1.m00, m1.m01, m1.m02,
m1.m10, m1.m11, m1.m12,
m1.m20, m1.m21, m1.m22
);
}
/**
* Sets the value of this quaternion to the rotational component of
* the passed matrix.
* @param m1 the matrix3f
*/
void set(Matrix3f m1) {
setFromMat(
m1.m00, m1.m01, m1.m02,
m1.m10, m1.m11, m1.m12,
m1.m20, m1.m21, m1.m22
);
}
/**
* Sets the value of this quaternion to the equivalent rotation of teh
* AxisAngle argument.
* @param a1 the axis-angle
*/
void set(AxisAngle4f a1) {
x = a1.x;
y = a1.y;
z = a1.z;
T n = Math.sqrt(x*x + y*y + z*z);
// zero-div may occur.
T s = Math.sin(0.5*a1.angle)/n;
x *= s;
y *= s;
z *= s;
w = Math.cos(0.5*a1.angle);
}
#endif
/**
* Sets the value of this quaternion to the rotational component of
* the passed matrix.
* @param m1 the matrix4d
*/
void set(const Matrix4<T>& m1);
/**
* Sets the value of this quaternion to the rotational component of
* the passed matrix.
* @param m1 the matrix3d
*/
void set(const Matrix3<T>& m1);
/**
* Sets the value of this quaternion to the equivalent rotation of teh
* AxisAngle argument.
* @param a1 the axis-angle
*/
void set(const AxisAngle4<T>& a1);
/**
* Performs a great circle interpolation between this quaternion and the
* quaternion parameter and places the result into this quaternion.
* @param q1 the other quaternion
* @param alpha the alpha interpolation parameter
*/
void interpolate(const Quat4& q1, T alpha);
/**
* Performs a great circle interpolation between quaternion q1 and
* quaternion q2 and places the result into this quaternion.
* @param q1 the first quaternion
* @param q2 the second quaternion
* @param alpha the alpha interpolation parameter
*/
void interpolate(const Quat4& q1, const Quat4& q2, T alpha);
// copy constructor and operator = is made by complier
Quat4& operator*=(const Quat4& m1);
Quat4 operator*(const Quat4& m1) const;
};
#ifdef VM_INCLUDE_IO
template <class T>
std::ostream& operator<<(std::ostream& o, const VM_VECMATH_NS::Quat4<T>& q1);
#endif
typedef Quat4<double> Quat4d;
typedef Quat4<float> Quat4f;
VM_END_NS
#endif /* QUAT4__H */