Skip to content

Commit 1f3d298

Browse files
committed
xrCore/_quaternion.h: cleanup code
1 parent b24059e commit 1f3d298

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

src/xrCore/_quaternion.h

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#include "_vector3d.h"
55

66
/***************************************************************************
7-
The quatern module contains basic support for a quaternion object.
7+
The quaternion module contains basic support for a quaternion object.
88
99
quaternions are an extension of complex numbers that allows an
1010
expression for rotation that can be easily interpolated. Quaternions are also
11-
more numericaly stable for repeated rotations than matrices.
11+
more numerically stable for repeated rotations than matrices.
1212
1313
1414
A quaternion is a 4 element 'vector3' [w,x,y,z] where:
@@ -88,7 +88,7 @@
8888
(which is a unit vector3, of course). A unit quaternion can also be
8989
thought of as a point on the surface of a four-dimensional hypersphere,
9090
so if you try to interpolate between two unit quaternions, you can get
91-
an intermediate rotation. Gamasutra describes Shoemake's spherical
91+
an intermediate rotation. Gamasutra describes Shoemake's spherical
9292
linear interpolation method, but I think getting the logarithm of the
9393
quaternions and performing linear interpolation is easier. The
9494
logarithm of a quaternion is given by
@@ -147,14 +147,13 @@ struct _matrix;
147147
template <class T>
148148
struct _quaternion
149149
{
150-
public:
151-
typedef T TYPE;
152-
typedef _quaternion<T> Self;
153-
typedef Self& SelfRef;
154-
typedef const Self& SelfCRef;
150+
using TYPE = T;
151+
using Self = _quaternion<T>;
152+
using SelfRef = Self&;
153+
using SelfCRef = const Self&;
155154

156155
private:
157-
IC T _asin_(T val)
156+
static T _asin_(T val)
158157
{
159158
const T c1 = 0.892399f;
160159
const T c3 = 1.693204f;
@@ -166,19 +165,22 @@ struct _quaternion
166165

167166
return d;
168167
}
169-
IC T _acos_(T val) { return PI_DIV_2 - _asin_(val); }
168+
169+
static T _acos_(T val) { return PI_DIV_2 - _asin_(val); }
170+
170171
public:
171172
T x, y, z, w;
172173

173-
IC SelfRef set(T W, T X, T Y, T Z) // don't normalize
174+
SelfRef set(T W, T X, T Y, T Z) // don't normalize
174175
{
175176
x = X;
176177
y = Y;
177178
z = Z;
178179
w = W;
179180
return *this;
180181
}
181-
IC SelfRef set(SelfCRef Q) // don't normalize
182+
183+
SelfRef set(SelfCRef Q) // don't normalize
182184
{
183185
set(Q.w, Q.x, Q.y, Q.z);
184186
return *this;
@@ -196,7 +198,7 @@ struct _quaternion
196198
(w1*y2 - x1*z2 + y1*w2 + z1*x2)j {y3}
197199
(w1*z2 + x1*y2 - y1*x2 + z1*w2)k {z3}
198200
*/
199-
IC SelfRef mul(SelfCRef q1l, SelfCRef q2l)
201+
SelfRef mul(SelfCRef q1l, SelfCRef q2l)
200202
{
201203
VERIFY(q1l.isValid());
202204
VERIFY(q2l.isValid());
@@ -211,15 +213,16 @@ struct _quaternion
211213
return *this;
212214
}
213215

214-
IC SelfRef add(SelfCRef q1, SelfCRef q2)
216+
SelfRef add(SelfCRef q1, SelfCRef q2)
215217
{
216218
x = q1.x + q2.x;
217219
y = q1.y + q2.y;
218220
z = q1.z + q2.z;
219221
w = q1.w + q2.w;
220222
return *this;
221223
}
222-
IC SelfRef sub(SelfCRef q1, SelfCRef q2)
224+
225+
SelfRef sub(SelfCRef q1, SelfCRef q2)
223226
{
224227
x = q1.x - q2.x;
225228
y = q1.y - q2.y;
@@ -228,15 +231,16 @@ struct _quaternion
228231
return *this;
229232
}
230233

231-
IC SelfRef add(SelfCRef q)
234+
SelfRef add(SelfCRef q)
232235
{
233236
x += q.x;
234237
y += q.y;
235238
z += q.z;
236239
w += q.w;
237240
return *this;
238241
}
239-
IC SelfRef sub(SelfCRef q)
242+
243+
SelfRef sub(SelfCRef q)
240244
{
241245
x -= q.x;
242246
y -= q.y;
@@ -246,7 +250,7 @@ struct _quaternion
246250
}
247251

248252
// validates numerical stability
249-
IC const bool isValid(void) const
253+
const bool isValid() const
250254
{
251255
if ((w * w) < 0.0f)
252256
return false;
@@ -260,7 +264,7 @@ struct _quaternion
260264
}
261265

262266
// checks for Unit-length quanternion
263-
IC const bool isUnit(void)
267+
const bool isUnit()
264268
{
265269
T m = magnitude();
266270

@@ -270,7 +274,7 @@ struct _quaternion
270274
}
271275

272276
// normalizes Q to be a unit geQuaternion
273-
IC SelfRef normalize(void)
277+
SelfRef normalize()
274278
{
275279
T m, one_over_magnitude;
276280

@@ -289,16 +293,19 @@ struct _quaternion
289293
}
290294

291295
// inversion
292-
IC SelfRef inverse(SelfCRef Q) { return set(Q.w, -Q.x, -Q.y, -Q.z); }
293-
IC SelfRef inverse() { return set(w, -x, -y, -z); }
294-
IC SelfRef inverse_with_w(SelfCRef Q) { return set(-Q.w, -Q.x, -Q.y, -Q.z); }
295-
IC SelfRef inverse_with_w() { return set(-w, -x, -y, -z); }
296+
SelfRef inverse(SelfCRef Q) { return set(Q.w, -Q.x, -Q.y, -Q.z); }
297+
SelfRef inverse() { return set(w, -x, -y, -z); }
298+
SelfRef inverse_with_w(SelfCRef Q) { return set(-Q.w, -Q.x, -Q.y, -Q.z); }
299+
SelfRef inverse_with_w() { return set(-w, -x, -y, -z); }
300+
296301
// identity - no rotation
297-
IC SelfRef identity(void) { return set(1.f, 0.f, 0.f, 0.f); }
302+
SelfRef identity() { return set(1.f, 0.f, 0.f, 0.f); }
303+
298304
// square length
299-
IC T magnitude(void) { return w * w + x * x + y * y + z * z; }
305+
T magnitude() { return w * w + x * x + y * y + z * z; }
306+
300307
// makes unit rotation
301-
IC SelfRef rotationYawPitchRoll(T _x, T _y, T _z)
308+
SelfRef rotationYawPitchRoll(T _x, T _y, T _z)
302309
{
303310
T fSinYaw = _sin(_x * .5f);
304311
T fCosYaw = _cos(_x * .5f);
@@ -315,9 +322,10 @@ struct _quaternion
315322
}
316323

317324
// makes unit rotation
318-
IC SelfRef rotationYawPitchRoll(const Fvector& ypr) { return rotationYawPitchRoll(ypr.x, ypr.y, ypr.z); }
325+
SelfRef rotationYawPitchRoll(const Fvector& ypr) { return rotationYawPitchRoll(ypr.x, ypr.y, ypr.z); }
326+
319327
// set a quaternion from an axis and a rotation around the axis
320-
IC SelfRef rotation(Fvector& axis, T angle)
328+
SelfRef rotation(Fvector& axis, T angle)
321329
{
322330
T sinTheta;
323331

@@ -333,7 +341,7 @@ struct _quaternion
333341
// returns TRUE if there is an axis.
334342
// returns FALSE if there is no axis (and Axis is set to 0,0,0, and Theta is 0)
335343

336-
IC BOOL get_axis_angle(Fvector& axis, T& angle)
344+
bool get_axis_angle(Fvector& axis, T& angle)
337345
{
338346
T s = _sqrt(x * x + y * y + z * z);
339347
if (s > EPS_S)
@@ -345,12 +353,9 @@ struct _quaternion
345353
angle = 2.0f * atan2(s, w);
346354
return true;
347355
}
348-
else
349-
{
350-
axis.x = axis.y = axis.z = 0.0f;
351-
angle = 0.0f;
352-
return false;
353-
}
356+
axis.x = axis.y = axis.z = 0.0f;
357+
angle = 0.0f;
358+
return false;
354359
}
355360

356361
// spherical interpolation between q0 and q1. 0<=t<=1
@@ -403,19 +408,19 @@ struct _quaternion
403408
return *this;
404409
}
405410

406-
// return TRUE if quaternions differ elementwise by less than Tolerance.
407-
IC BOOL cmp(SelfCRef Q, T Tolerance = 0.0001f)
411+
// return true if quaternions differ elementwise by less than Tolerance.
412+
bool cmp(SelfCRef Q, T Tolerance = 0.0001f)
408413
{
409-
if ( // they are the same but with opposite signs
414+
if (// they are the same but with opposite signs
410415
((_abs(x + Q.x) <= Tolerance) && (_abs(y + Q.y) <= Tolerance) && (_abs(z + Q.z) <= Tolerance) &&
411416
(_abs(w + Q.w) <= Tolerance)) || // they are the same with same signs
412417
((_abs(x - Q.x) <= Tolerance) && (_abs(y - Q.y) <= Tolerance) && (_abs(z - Q.z) <= Tolerance) &&
413418
(_abs(w - Q.w) <= Tolerance)))
414419
return true;
415-
else
416-
return false;
420+
return false;
417421
}
418-
IC SelfRef ln(SelfCRef Q)
422+
423+
SelfRef ln(SelfCRef Q)
419424
{
420425
T n = Q.x * Q.x + Q.y * Q.y + Q.z * Q.z;
421426
T r = _sqrt(n);
@@ -426,7 +431,8 @@ struct _quaternion
426431
w = .5f * _log(n + Q.w * Q.w);
427432
return *this;
428433
}
429-
IC SelfRef exp(SelfCRef Q)
434+
435+
SelfRef exp(SelfCRef Q)
430436
{
431437
T r = _sqrt(Q.x * Q.x + Q.y * Q.y + Q.z * Q.z);
432438
T et = expf(Q.w);
@@ -443,7 +449,7 @@ typedef _quaternion<float> Fquaternion;
443449
typedef _quaternion<double> Dquaternion;
444450

445451
template <class T>
446-
BOOL _valid(const _quaternion<T>& s)
452+
bool _valid(const _quaternion<T>& s)
447453
{
448454
return _valid(s.x) && _valid(s.y) && _valid(s.z) && _valid(s.w);
449455
}

0 commit comments

Comments
 (0)