Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
/*
   Copyright (c) 2016 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _QUATERNION_INL_
#define _QUATERNION_INL_

namespace Divide {
template <typename T>
Quaternion<T>::Quaternion()
{
    identity();
}

template <typename T>
Quaternion<T>::Quaternion(T x, T y, T z, T w)
{
    _elements.set(x, y, z, w);
}

template <typename T>
Quaternion<T>::Quaternion(const vec4<T>& values)
{
    _elements.set(values);
}

template <typename T>
Quaternion<T>::Quaternion(const mat3<T>& rotationMatrix)
{
    fromMatrix(rotationMatrix);
}

template <typename T>
Quaternion<T>::Quaternion(const vec3<T>& axis, T angle, bool inDegrees)
{
    fromAxisAngle(axis, angle, inDegrees);
}

template <typename T>
Quaternion<T>::Quaternion(T pitch, T yaw, T roll, bool inDegrees)
{
    fromEuler(pitch, yaw, roll, inDegrees);
}

template <typename T>
Quaternion<T>::Quaternion(const Quaternion& q)
{
    set(q);
}

template <typename T>
Quaternion<T>& Quaternion<T>::operator=(const Quaternion& q) {
    set(q);
    return *this;
}

template <typename T>
inline T Quaternion<T>::dot(const Quaternion<T>& rq) const {
    return _elements.dot(rq._elements);
}

template <typename T>
inline T Quaternion<T>::magnitude() const {
    return _elements.length();
}

template <typename T>
inline T Quaternion<T>::magnituteSq() const {
    return _elements.lengthSquared();
}

template <typename T>
inline bool Quaternion<T>::compare(const Quaternion<T>& rq,
                                   F32 tolerance) const {
    T angleRad = Angle::DegreesToRadians((T)std::acos(to_double(dot(rq))));
    F32 toleranceRad = Angle::DegreesToRadians(tolerance);

    return  IS_TOLERANCE(angleRad, toleranceRad) ||
            COMPARE_TOLERANCE(angleRad, to_float(M_PI), toleranceRad);
}

template <typename T>
inline void Quaternion<T>::set(const vec4<T>& values) {
    _elements.set(values);
}

template <typename T>
inline void Quaternion<T>::set(T x, T y, T z, T w) {
    _elements.set(x, y, z, w);
}

template <typename T>
inline void Quaternion<T>::set(const Quaternion<T>& q) {
    set(q._elements);
}

template <typename T>
inline void Quaternion<T>::normalize() {
    _elements.normalize();
}

template <typename T>
inline Quaternion<T> Quaternion<T>::inverse() const {
    return getConjugate() * (1.0f / magnitude());
}

template <typename T>
inline Quaternion<T> Quaternion<T>::getConjugate() const {
    return Quaternion<T>(-X(), -Y(), -Z(), W());
}

template <typename T>
inline Quaternion<T> Quaternion<T>::operator*(const Quaternion<T>& rq) const {
    return Quaternion<T>(
        W() * rq.X() + X() * rq.W() + Y() * rq.Z() - Z() * rq.Y(),
        W() * rq.Y() + Y() * rq.W() + Z() * rq.X() - X() * rq.Z(),
        W() * rq.Z() + Z() * rq.W() + X() * rq.Y() - Y() * rq.X(),
        W() * rq.W() - X() * rq.X() - Y() * rq.Y() - Z() * rq.Z());
}

template <typename T>
inline Quaternion<T>& Quaternion<T>::operator*=(const Quaternion<T>& rq) {
    (*this) = rq * (*this);
    return (*this);
}

template <typename T>
vec3<T> Quaternion<T>::operator*(const vec3<T>& vec) const {
    // nVidia SDK implementation
    vec3<T> uv(Cross(_elements.xyz(), vec));
    return vec + (uv * (2.0f * W())) + (Cross(_elements.xyz(), uv) * 2.0f);
}

template <typename T>
bool Quaternion<T>::operator==(const Quaternion<T>& rq) const {
    return compare(rq);
}

template <typename T>
bool Quaternion<T>::operator!=(const Quaternion<T>& rq) const {
    return !(*this == rq);
}

template <typename T>
inline Quaternion<T>& Quaternion<T>::operator+=(const Quaternion<T>& rq) {
    _elements += rq._elements;
    return *this;
}

template <typename T>
inline Quaternion<T>& Quaternion<T>::operator-=(const Quaternion<T>& rq) {
    _elements -= rq._elements;
    return *this;
}

template <typename T>
inline Quaternion<T>& Quaternion<T>::operator*=(T scalar) {
    _elements *= scalar;
    return *this;
}

template <typename T>
inline Quaternion<T>& Quaternion<T>::operator/=(T scalar) {
    _elements /= scalar;
    return *this;
}

template <typename T>
inline Quaternion<T> Quaternion<T>::operator+(const Quaternion<T>& rq) const {
    Quaternion<T> tmp(*this);
    tmp += rq;
    return tmp;
}

template <typename T>
inline Quaternion<T> Quaternion<T>::operator-(const Quaternion<T>& rq) const {
    Quaternion<T> tmp(*this);
    tmp -= rq;
    return tmp;
}

template <typename T>
inline Quaternion<T> Quaternion<T>::operator*(T scalar) const {
    Quaternion<T> tmp(*this);
    tmp *= scalar;
    return tmp;
}

template <typename T>
inline Quaternion<T> Quaternion<T>::operator/(T scalar) const {
    Quaternion<T> tmp(*this);
    tmp /= scalar;
    return tmp;
}

template <typename T>
inline void Quaternion<T>::slerp(const Quaternion<T>& q, F32 t) {
    slerp(*this, q, t);
}

template <typename T>
void Quaternion<T>::slerp(const Quaternion<T>& q0, const Quaternion<T>& q1, F32 t) {
    F32 k0, k1;
    T cosomega = q0.dot(q1);
    Quaternion<T> q;
    if (cosomega < 0.0) {
        cosomega = -cosomega;
        q._elements.set(-q1._elements);
    } else {
        q._elements.set(q1._elements);
    }

    if (1.0 - cosomega > 1e-6) {
        F32 omega = to_float(std::acos(cosomega));
        F32 sinomega = to_float(std::sin(omega));
        k0 = to_float(std::sin((1.0f - t) * omega) / sinomega);
        k1 = to_float(std::sin(t * omega) / sinomega);
    } else {
        k0 = 1.0f - t;
        k1 = t;
    }
    _elements.set(q0._elements * k0 + q._elements * k1);
}

template <typename T>
void Quaternion<T>::fromAxisAngle(const vec3<T>& v, T angle,
                                  bool inDegrees) {
    if (inDegrees) {
        angle = Angle::DegreesToRadians(angle);
    }
    angle *= 0.5f;
    vec3<T> vn(v);
    vn.normalize();

    _elements.set(vn * std::sin(angle), std::cos(angle));
}

template <typename T>
inline void Quaternion<T>::fromEuler(const vec3<T>& v, bool inDegrees) {
    fromEuler(v.pitch, v.yaw, v.roll, inDegrees);
}

template <typename T>
void Quaternion<T>::fromEuler(T pitch, T yaw, T roll, bool inDegrees) {
    T attitude = pitch;
    T heading = yaw;
    T bank = roll;

    if (inDegrees) {
        attitude = Angle::DegreesToRadians(attitude);
        heading = Angle::DegreesToRadians(heading);
        bank = Angle::DegreesToRadians(bank);
    }

    D64 c1 = std::cos(heading * 0.5);
    D64 s1 = std::sin(heading * 0.5);
    D64 c2 = std::cos(attitude * 0.5);
    D64 s2 = std::sin(attitude * 0.5);
    D64 c3 = std::cos(bank * 0.5);
    D64 s3 = std::sin(bank * 0.5);

    D64 c1c2 = c1 * c2;
    D64 s1s2 = s1 * s2;

    W(static_cast<T>(c1c2 * c3 - s1s2 * s3));
    X(static_cast<T>(c1c2 * s3 + s1s2 * c3));
    Y(static_cast<T>(s1 * c2 * c3 + c1 * s2 * s3));
    Z(static_cast<T>(c1 * s2 * c3 - s1 * c2 * s3));

    // normalize(); this method does produce a normalized quaternion
}

template <typename T>
void Quaternion<T>::fromMatrix(const mat3<T>& rotationMatrix) {
    // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
    // article "Quaternion Calculus and Fast Animation".

    T fTrace = rotationMatrix.m[0][0] + rotationMatrix.m[1][1] +
               rotationMatrix.m[2][2];
    T fRoot;

    if (fTrace > 0.0) {
        // |w| > 1/2, may as well choose w > 1/2
        fRoot = (T)std::sqrt(to_float(fTrace) + 1.0f);  // 2w
        W(0.5f * fRoot);
        fRoot = 0.5f / fRoot;  // 1/(4w)
        X((rotationMatrix.m[2][1] - rotationMatrix.m[1][2]) * fRoot);
        Y((rotationMatrix.m[0][2] - rotationMatrix.m[2][0]) * fRoot);
        Z((rotationMatrix.m[1][0] - rotationMatrix.m[0][1]) * fRoot);
    } else {
        // |w| <= 1/2
        static size_t s_iNext[3] = {1, 2, 0};
        size_t i = 0;
        if (rotationMatrix.m[1][1] > rotationMatrix.m[0][0]) {
            i = 1;
        }
        if (rotationMatrix.m[2][2] > rotationMatrix.m[i][i]) {
            i = 2;
        }
        size_t j = s_iNext[i];
        size_t k = s_iNext[j];

        fRoot = static_cast<T>(std::sqrt(
                    to_float(rotationMatrix.m[i][i] - rotationMatrix.m[j][j] -
                             rotationMatrix.m[k][k] + 1.0f)));
        T* apkQuat[3] = {&_elements.x, &_elements.y, &_elements.z};
        *apkQuat[i] = 0.5f * fRoot;
        fRoot = 0.5f / fRoot;
        W((rotationMatrix.m[k][j] - rotationMatrix.m[j][k]) * fRoot);
        *apkQuat[j] = (rotationMatrix.m[j][i] + rotationMatrix.m[i][j]) * fRoot;
        *apkQuat[k] = (rotationMatrix.m[k][i] + rotationMatrix.m[i][k]) * fRoot;
    }
}

template <typename T>
void Quaternion<T>::getMatrix(mat4<F32>& outMatrix) const {
    const T& x = X();
    const T& y = Y();
    const T& z = Z();
    const T& w = W();

    T xx = x * x;
    T xy = x * y;
    T xz = x * z;
    T xw = x * w;
    T yy = y * y;
    T yz = y * z;
    T yw = y * w;
    T zz = z * z;
    T zw = z * w;
    outMatrix.mat[0] = 1 - 2 * (yy + zz);
    outMatrix.mat[1] = 2 * (xy - zw);
    outMatrix.mat[2] = 2 * (xz + yw);
    outMatrix.mat[4] = 2 * (xy + zw);
    outMatrix.mat[5] = 1 - 2 * (xx + zz);
    outMatrix.mat[6] = 2 * (yz - xw);
    outMatrix.mat[8] = 2 * (xz - yw);
    outMatrix.mat[9] = 2 * (yz + xw);
    outMatrix.mat[10] = 1 - 2 * (xx + yy);
}

template <typename T>
void Quaternion<T>::getAxisAngle(vec3<T>* axis, T* angle, bool inDegrees) const {
    axis->set(_elements / _elements.xyz().length());
    *angle = inDegrees ? Angle::RadiansToDegrees(std::acos(W()) * 2.0f)
                       : std::acos(W()) * 2.0f;
}

template <typename T>
void Quaternion<T>::getEuler(vec3<T>& euler, bool toDegrees) const {
    T heading = 0, attitude = 0, bank = 0;
    const T& x = X();
    const T& y = Y();
    const T& z = Z();
    const T& w = W();
    T sqx = x * x;
    T sqy = y * y;
    T sqz = z * z;
    T sqw = w * w;
    T test = x * y + z * w;
    // if normalized is one, otherwise is correction factor
    T unit = sqx + sqy + sqz + sqw;  

    if (test > (0.5f - EPSILON_F32) * unit) {  // singularity at north pole
        heading = 2 * std::atan2(x, w);
        attitude = static_cast<T>(M_PI2);
        bank = 0;
    } else if (test < -(0.5f - EPSILON_F32) * unit) {  // singularity at south pole
        heading = -2 * std::atan2(x, w);
        attitude = -static_cast<T>(M_PI2);
        bank = 0;
    } else {
        T x2 = 2 * x;
        T y2 = 2 * y;

        heading = std::atan2(y2 * w - x2 * z, sqx - sqy - sqz + sqw);
        attitude = std::asin(2 * test / unit);
        bank = std::atan2(x2 * w - y2 * z, -sqx + sqy - sqz + sqw);
    }
    // Convert back from Z = pitch to Z = roll
    if (toDegrees) {
        euler.yaw = Angle::RadiansToDegrees(heading);
        euler.pitch = Angle::RadiansToDegrees(bank);
        euler.roll = Angle::RadiansToDegrees(attitude);
    } else {
        euler.yaw = heading;
        euler.pitch = bank;
        euler.roll = attitude;
    }
}

template <typename T>
inline F32 Quaternion<T>::X() const {
    return _elements.x;
}

template <typename T>
inline F32 Quaternion<T>::Y() const {
    return _elements.y;
}

template <typename T>
inline F32 Quaternion<T>::Z() const {
    return _elements.z;
}

template <typename T>
inline F32 Quaternion<T>::W() const {
    return _elements.w;
}

template <typename T>
inline void Quaternion<T>::X(F32 x) {
    _elements.x = x;
}

template <typename T>
inline void Quaternion<T>::Y(F32 y) {
    _elements.y = y;
}

template <typename T>
inline void Quaternion<T>::Z(F32 z) {
    _elements.z = z;
}

template <typename T>
inline void Quaternion<T>::W(F32 w) {
    _elements.w = w;
}

template <typename T>
inline void Quaternion<T>::identity() {
    _elements.set(0, 0, 0, 1);
}

template <typename T>
inline const vec4<T>& Quaternion<T>::asVec4() const {
    return _elements;
}


/// get the shortest arc quaternion to rotate vector 'v' to the target vector
/// 'u'(from Ogre3D!)
template <typename T>
inline Quaternion<T> RotationFromVToU(
    const vec3<T>& v, const vec3<T>& u,
    const vec3<T>& fallbackAxis) {
    // Based on Stan Melax's article in Game Programming Gems
    Quaternion<T> q;
    // Copy, since cannot modify local
    vec3<T> v0 = v;
    vec3<T> v1 = u;
    v0.normalize();
    v1.normalize();

    T d = v0.dot(v1);
    // If dot == 1, vectors are the same
    if (d >= 1.0f) {
        return q;
    } else if (d < (1e-6f - 1.0f)) {
        if (!fallbackAxis.compare(VECTOR3_ZERO)) {
            // rotate 180 degrees about the fallback axis
            q.fromAxisAngle(fallbackAxis, Angle::DegreesToRadians(to_float(M_PI)));
        } else {
            // Generate an axis
            vec3<T> axis;
            axis.cross(WORLD_X_AXIS, v);

            if (axis.isZeroLength())  // pick another if collinear
                axis.cross(WORLD_Y_AXIS, v);

            axis.normalize();
            q.fromAxisAngle(axis, Angle::DegreesToRadians(to_float(M_PI)));
        }
    } else {
        F32 s = std::sqrt((1 + d) * 2);
        F32 invs = 1 / s;

        vec3<T> c(Cross(v0, v1) * invs);
        q.set(c.x, c.y, c.z, s * 0.5f);
        q.normalize();
    }

    return q;
}

template <typename T>
inline Quaternion<T> Slerp(const Quaternion<T>& q0, const Quaternion<T>& q1,
                           F32 t) {
    Quaternion<T> temp;
    temp.slerp(q0, q1, t);
    return temp;
}

template <typename T>
inline mat4<T> GetMatrix(const Quaternion<T>& q) {
    mat4<T> temp;
    q.getMatrix(temp);
    return temp;
}

template <typename T>
inline vec3<T> GetEuler(const Quaternion<T>& q, const bool toDegrees) {
    vec3<T> euler;
    q.getEuler(euler, toDegrees);
    return euler;
}
};  // namespace Divide

#endif  //_QUATERNION_INL_

Commits for Divide-Framework/trunk/Source Code/Core/Math/Headers/Quaternion.inl

Diff revisions: vs.
Revision Author Commited Message
750 Diff Diff IonutCava picture IonutCava Thu 07 Jul, 2016 16:02:03 +0000

[IonutCava]
- Static analysis based fixes and improvements

720 Diff Diff IonutCava picture IonutCava Mon 06 Jun, 2016 15:51:06 +0000

[IonutCava]
- PVS-Studio guided static analysis fixes and optimizations
- Added a flag to SceneState to bypass the Save/Load system not needed for certain types of scenes (menus, cinematics, etc)

706 Diff Diff IonutCava picture IonutCava Fri 13 May, 2016 16:29:40 +0000

[IonutCava]
- Renamed LightManager to LightPool and changed it from a Singleton to a scene specific pointer
- More scene load / unload updates (changing scenes now works properly!)
- Allowed GUI interface to hold per-scene elements and only render relevant ones
- Reduced dependencies between nodes, scenemanager, scene, loaders and kernel
- Removed Reflector base class as it was useless and integrate Water class with the Reflection system already in place
- Extended RenderingComponent’s reflection system to allow refraction as well

680 Diff Diff IonutCava picture IonutCava Thu 14 Apr, 2016 16:15:38 +0000

[IonutCava]
- Added a mat2 class (untested)
- Added static asserts to matrices, vectors and quaternions to allow only arithmetic (sans bool) data types
- Added more vector unit tests
- Added unit tests for matrices
- D32 finally renamed to D64 as the VC++ compiler is considered as the reference compiler

648 Diff Diff IonutCava picture IonutCava Sun 21 Feb, 2016 16:32:52 +0000

[IonutCava]
- Update copyright notice
- Move BoundingBox and BoundingSphere to a new BoundsComponent
— Add a temp hack in SceneGraphNode to update these

615 Diff Diff IonutCava picture IonutCava Tue 22 Dec, 2015 16:30:42 +0000

[IonutCava]
not working properly
- Move HiZ buffer to separate render target
- Implement basic, per-scene, auto Save/Load system (works with camera position only for now)
- Fix a few wrong calls in glFramebuffer and glVertexArray
- Re-work skybox rendering

502 Diff Diff IonutCava picture IonutCava Sun 23 Aug, 2015 18:03:16 +0000

[IonutCava]
- Micro-optimise the rendering path (faster compares, less stack allocation, etc)

458 Diff Diff IonutCava picture IonutCava Tue 02 Jun, 2015 16:26:18 +0000

[Ionut]
- More Linux port work

453 Diff Diff IonutCava picture IonutCava Sun 31 May, 2015 15:24:10 +0000

[Ionut]
- More linux port work

444 IonutCava picture IonutCava Thu 21 May, 2015 16:06:53 +0000

[Ionut]
- More platform independent code cleanup:
— Moved platform specific defines to their respective header files (e.g. THREAD_LOCAL)
— Moved most preprocessor defines from the VisualC++ projects into source code
— Removed compiler specific code (e.g. warning disables) and fixed most warning resulting from this