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
|
// This code is in the public domain -- castano@gmail.com
#pragma once
#ifndef NV_MATH_QUATERNION_H
#define NV_MATH_QUATERNION_H
#include "nvmath/nvmath.h"
#include "nvmath/Vector.inl" // @@ Do not include inl files from header files.
#include "nvmath/Matrix.h"
namespace nv
{
class NVMATH_CLASS Quaternion
{
public:
typedef Quaternion const & Arg;
Quaternion();
explicit Quaternion(float f);
Quaternion(float x, float y, float z, float w);
Quaternion(Vector4::Arg v);
const Quaternion & operator=(Quaternion::Arg v);
Vector4 asVector() const;
union {
struct {
float x, y, z, w;
};
float component[4];
};
};
inline Quaternion::Quaternion() {}
inline Quaternion::Quaternion(float f) : x(f), y(f), z(f), w(f) {}
inline Quaternion::Quaternion(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
inline Quaternion::Quaternion(Vector4::Arg v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
// @@ Move all these to Quaternion.inl!
inline const Quaternion & Quaternion::operator=(Quaternion::Arg v) {
x = v.x;
y = v.y;
z = v.z;
w = v.w;
return *this;
}
inline Vector4 Quaternion::asVector() const { return Vector4(x, y, z, w); }
inline Quaternion mul(Quaternion::Arg a, Quaternion::Arg b)
{
return Quaternion(
+ a.x*b.w + a.y*b.z - a.z*b.y + a.w*b.x,
- a.x*b.z + a.y*b.w + a.z*b.x + a.w*b.y,
+ a.x*b.y - a.y*b.x + a.z*b.w + a.w*b.z,
- a.x*b.x - a.y*b.y - a.z*b.z + a.w*b.w);
}
inline Quaternion mul(Quaternion::Arg a, Vector3::Arg b)
{
return Quaternion(
+ a.y*b.z - a.z*b.y + a.w*b.x,
- a.x*b.z + a.z*b.x + a.w*b.y,
+ a.x*b.y - a.y*b.x + a.w*b.z,
- a.x*b.x - a.y*b.y - a.z*b.z );
}
inline Quaternion mul(Vector3::Arg a, Quaternion::Arg b)
{
return Quaternion(
+ a.x*b.w + a.y*b.z - a.z*b.y,
- a.x*b.z + a.y*b.w + a.z*b.x,
+ a.x*b.y - a.y*b.x + a.z*b.w,
- a.x*b.x - a.y*b.y - a.z*b.z);
}
inline Quaternion operator *(Quaternion::Arg a, Quaternion::Arg b)
{
return mul(a, b);
}
inline Quaternion operator *(Quaternion::Arg a, Vector3::Arg b)
{
return mul(a, b);
}
inline Quaternion operator *(Vector3::Arg a, Quaternion::Arg b)
{
return mul(a, b);
}
inline Quaternion scale(Quaternion::Arg q, float s)
{
return scale(q.asVector(), s);
}
inline Quaternion operator *(Quaternion::Arg q, float s)
{
return scale(q, s);
}
inline Quaternion operator *(float s, Quaternion::Arg q)
{
return scale(q, s);
}
inline Quaternion scale(Quaternion::Arg q, Vector4::Arg s)
{
return scale(q.asVector(), s);
}
/*inline Quaternion operator *(Quaternion::Arg q, Vector4::Arg s)
{
return scale(q, s);
}
inline Quaternion operator *(Vector4::Arg s, Quaternion::Arg q)
{
return scale(q, s);
}*/
inline Quaternion conjugate(Quaternion::Arg q)
{
return scale(q, Vector4(-1, -1, -1, 1));
}
inline float length(Quaternion::Arg q)
{
return length(q.asVector());
}
inline bool isNormalized(Quaternion::Arg q, float epsilon = NV_NORMAL_EPSILON)
{
return equal(length(q), 1, epsilon);
}
inline Quaternion normalize(Quaternion::Arg q, float epsilon = NV_EPSILON)
{
float l = length(q);
nvDebugCheck(!isZero(l, epsilon));
Quaternion n = scale(q, 1.0f / l);
nvDebugCheck(isNormalized(n));
return n;
}
inline Quaternion inverse(Quaternion::Arg q)
{
return conjugate(normalize(q));
}
/// Create a rotation quaternion for @a angle alpha around normal vector @a v.
inline Quaternion axisAngle(Vector3::Arg v, float alpha)
{
float s = sinf(alpha * 0.5f);
float c = cosf(alpha * 0.5f);
return Quaternion(Vector4(v * s, c));
}
inline Vector3 imag(Quaternion::Arg q)
{
return q.asVector().xyz();
}
inline float real(Quaternion::Arg q)
{
return q.w;
}
/// Transform vector.
inline Vector3 transform(Quaternion::Arg q, Vector3::Arg v)
{
//Quaternion t = q * v * conjugate(q);
//return imag(t);
// Faster method by Fabian Giesen and others:
// http://molecularmusings.wordpress.com/2013/05/24/a-faster-quaternion-vector-multiplication/
// http://mollyrocket.com/forums/viewtopic.php?t=833&sid=3a84e00a70ccb046cfc87ac39881a3d0
Vector3 t = 2 * cross(imag(q), v);
return v + q.w * t + cross(imag(q), t);
}
// @@ Not tested.
// From Insomniac's Mike Day:
// http://www.insomniacgames.com/converting-a-rotation-matrix-to-a-quaternion/
inline Quaternion fromMatrix(const Matrix & m) {
if (m(2, 2) < 0) {
if (m(0, 0) < m(1,1)) {
float t = 1 - m(0, 0) - m(1, 1) - m(2, 2);
return Quaternion(t, m(0,1)+m(1,0), m(2,0)+m(0,2), m(1,2)-m(2,1));
}
else {
float t = 1 - m(0, 0) + m(1, 1) - m(2, 2);
return Quaternion(t, m(0,1) + m(1,0), m(1,2) + m(2,1), m(2,0) - m(0,2));
}
}
else {
if (m(0, 0) < -m(1, 1)) {
float t = 1 - m(0, 0) - m(1, 1) + m(2, 2);
return Quaternion(t, m(2,0) + m(0,2), m(1,2) + m(2,1), m(0,1) - m(1,0));
}
else {
float t = 1 + m(0, 0) + m(1, 1) + m(2, 2);
return Quaternion(t, m(1,2) - m(2,1), m(2,0) - m(0,2), m(0,1) - m(1,0));
}
}
}
} // nv namespace
#endif // NV_MATH_QUATERNION_H
|