File: mini_glm.hpp

package info (click to toggle)
supertuxkart 1.4%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 768,424 kB
  • sloc: cpp: 412,077; xml: 106,334; ansic: 83,792; asm: 1,558; python: 1,403; sh: 1,366; objc: 452; makefile: 333; javascript: 23; awk: 20
file content (636 lines) | stat: -rw-r--r-- 21,162 bytes parent folder | download | duplicates (4)
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/* ==========================================================================
 * Copyright (c) 2022 SuperTuxKart-Team
 *
 * 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 HEADER_MINI_GLM_HPP
#define HEADER_MINI_GLM_HPP

#include "LinearMath/btQuaternion.h"
#include "LinearMath/btTransform.h"
#include "LinearMath/btVector3.h"

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <quaternion.h>
#include <vector3d.h>

#include "irrMath.h"

using namespace irr;

// GLM without template
namespace MiniGLM
{
    // ------------------------------------------------------------------------
    inline float overflow()
    {
        volatile float f = 1e10;
        for (int i = 0; i < 10; i++)
            f *= f; // this will overflow before the for loop terminates
        return f;
    }   // overflow
    // ------------------------------------------------------------------------
    inline float toFloat32(short value)
    {
        int s = (value >> 15) & 0x00000001;
        int e = (value >> 10) & 0x0000001f;
        int m =  value        & 0x000003ff;
        if (e == 0)
        {
            if (m == 0)
            {
                //
                // Plus or minus zero
                //
                uint32_t tmp_data = (unsigned int)(s << 31);
                float ret;
                memcpy(&ret, &tmp_data, 4);
                return ret;
            }
            else
            {
                //
                // Denormalized number -- renormalize it
                //
                while(!(m & 0x00000400))
                {
                    m <<= 1;
                    e -=  1;
                }

                e += 1;
                m &= ~0x00000400;
            }
        }
        else if (e == 31)
        {
            if (m == 0)
            {
                //
                // Positive or negative infinity
                //
                uint32_t tmp_data = (unsigned int)((s << 31) | 0x7f800000);
                float ret;
                memcpy(&ret, &tmp_data, 4);
                return ret;
            }
            else
            {
                //
                // Nan -- preserve sign and significand bits
                //
                uint32_t tmp_data = (unsigned int)((s << 31) | 0x7f800000 |
                    (m << 13));
                float ret;
                memcpy(&ret, &tmp_data, 4);
                return ret;
            }
        }

        //
        // Normalized number
        //
        e = e + (127 - 15);
        m = m << 13;
        //
        // Assemble s, e and m.
        //
        uint32_t tmp_data = (unsigned int)((s << 31) | (e << 23) | m);
        float ret;
        memcpy(&ret, &tmp_data, 4);
        return ret;
    }   // toFloat32
    // ------------------------------------------------------------------------
    inline short toFloat16(float const & f)
    {
        int i;
        memcpy(&i, &f, 4);
        //
        // Our floating point number, f, is represented by the bit
        // pattern in integer i.  Disassemble that bit pattern into
        // the sign, s, the exponent, e, and the significand, m.
        // Shift s into the position where it will go in in the
        // resulting half number.
        // Adjust e, accounting for the different exponent bias
        // of float and half (127 versus 15).
        //
        int s =  (i >> 16) & 0x00008000;
        int e = ((i >> 23) & 0x000000ff) - (127 - 15);
        int m =   i        & 0x007fffff;

        //
        // Now reassemble s, e and m into a half:
        //
        if (e <= 0)
        {
            if (e < -10)
            {
                //
                // E is less than -10.  The absolute value of f is
                // less than half_MIN (f may be a small normalized
                // float, a denormalized float or a zero).
                //
                // We convert f to a half zero.
                //
                return short(s);
            }

            //
            // E is between -10 and 0.  F is a normalized float,
            // whose magnitude is less than __half_NRM_MIN.
            //
            // We convert f to a denormalized half.
            //
            m = (m | 0x00800000) >> (1 - e);

            //
            // Round to nearest, round "0.5" up.
            //
            // Rounding may cause the significand to overflow and make
            // our number normalized.  Because of the way a half's bits
            // are laid out, we don't have to treat this case separately;
            // the code below will handle it correctly.
            //
            if (m & 0x00001000)
                m += 0x00002000;

            //
            // Assemble the half from s, e (zero) and m.
            //
            return short(s | (m >> 13));
        }
        else if (e == 0xff - (127 - 15))
        {
            if (m == 0)
            {
                //
                // F is an infinity; convert f to a half
                // infinity with the same sign as f.
                //
                return short(s | 0x7c00);
            }
            else
            {
                //
                // F is a NAN; we produce a half NAN that preserves
                // the sign bit and the 10 leftmost bits of the
                // significand of f, with one exception: If the 10
                // leftmost bits are all zero, the NAN would turn
                // into an infinity, so we have to set at least one
                // bit in the significand.
                //
                m >>= 13;
                return short(s | 0x7c00 | m | (m == 0));
            }
        }
        else
        {
            //
            // E is greater than zero.  F is a normalized float.
            // We try to convert f to a normalized half.
            //
            //
            // Round to nearest, round "0.5" up
            //
            if (m &  0x00001000)
            {
                m += 0x00002000;
                if (m & 0x00800000)
                {
                    m =  0;     // overflow in significand,
                    e += 1;     // adjust exponent
                }
            }

            //
            // Handle exponent overflow
            //
            if (e > 30)
            {
                overflow();        // Cause a hardware floating point overflow;

                return short(s | 0x7c00);
                // if this returns, the half becomes an
            }   // infinity with the same sign as f.

            //
            // Assemble the half from s, e and m.
            //
            return short(s | (e << 10) | (m >> 13));
        }
    }   // toFloat16
    // ------------------------------------------------------------------------
    inline uint32_t normalizedSignedFloatsTo1010102
        (const std::array<float, 3>& src, int extra_2_bit = -1)
    {
        int part = 0;
        uint32_t packed = 0;
        float v = fminf(1.0f, fmaxf(-1.0f, src[0]));
        if (v > 0.0f)
        {
            part = (int)((v * 511.0f) + 0.5f);
        }
        else
        {
            part = (int)((v * 512.0f) - 0.5f);
        }
        packed |= ((uint32_t)part & 1023) << 0;
        v = fminf(1.0f, fmaxf(-1.0f, src[1]));
        if (v > 0.0f)
        {
            part = (int)((v * 511.0f) + 0.5f);
        }
        else
        {
            part = (int)((v * 512.0f) - 0.5f);
        }
        packed |= ((uint32_t)part & 1023) << 10;
        v = fminf(1.0f, fmaxf(-1.0f, src[2]));
        if (v > 0.0f)
        {
            part = (int)((v * 511.0f) + 0.5f);
        }
        else
        {
            part = (int)((v * 512.0f) - 0.5f);
        }
        packed |= ((uint32_t)part & 1023) << 20;
        if (extra_2_bit >= 0)
        {
            part = extra_2_bit;
        }
        else
        {
            part = (int)(-0.5f);
        }
        packed |= ((uint32_t)part & 3) << 30;
        return packed;
    }   // normalizedSignedFloatsTo1010102
    // ------------------------------------------------------------------------
    inline std::array<short, 4> vertexType2101010RevTo4HF(uint32_t packed)
    {
        std::array<float, 4> ret;
        int part = packed & 1023;
        if (part & 512)
        {
            ret[0] = (float)(1024 - part) * (-1.0f / 512.0f);
        }
        else
        {
            ret[0] = (float)part * (1.0f / 511.0f);
        }
        part = (packed >> 10) & 1023;
        if (part & 512)
        {
            ret[1] = (float)(1024 - part) * (-1.0f / 512.0f);
        }
        else
        {
            ret[1] = (float)part * (1.0f / 511.0f);
        }
        part = (packed >> 20) & 1023;
        if (part & 512)
        {
            ret[2] = (float)(1024 - part) * (-1.0f / 512.0f);
        }
        else
        {
            ret[2] = (float)part * (1.0f / 511.0f);
        }
        part = (packed >> 30) & 3;
        if (part & 2)
        {
            ret[3] = (float)(4 - part) * (-1.0f / 2.0f);
        }
        else
        {
            ret[3] = (float)part;
        }
        std::array<short, 4> result;
        for (int i = 0; i < 4; i++)
        {
            result[i] = toFloat16(ret[i]);
        }
        return result;
    }   // vertexType2101010RevTo4HF
    // ------------------------------------------------------------------------
    inline std::array<float, 4> extractNormalizedSignedFloats(uint32_t packed,
        bool calculate_w = false)
    {
        std::array<float, 4> ret = {};
        int part = packed & 1023;
        if (part & 512)
        {
            ret[0] = (float)(1024 - part) * (-1.0f / 512.0f);
        }
        else
        {
            ret[0] = (float)part * (1.0f / 511.0f);
        }
        part = (packed >> 10) & 1023;
        if (part & 512)
        {
            ret[1] = (float)(1024 - part) * (-1.0f / 512.0f);
        }
        else
        {
            ret[1] = (float)part * (1.0f / 511.0f);
        }
        part = (packed >> 20) & 1023;
        if (part & 512)
        {
            ret[2] = (float)(1024 - part) * (-1.0f / 512.0f);
        }
        else
        {
            ret[2] = (float)part * (1.0f / 511.0f);
        }
        if (calculate_w)
        {
            float inv_sqrt_2 = 1.0f / sqrtf(2.0f);
            ret[0] *= inv_sqrt_2;
            ret[1] *= inv_sqrt_2;
            ret[2] *= inv_sqrt_2;
            float largest_val = sqrtf(fmaxf(0.0f, 1.0f -
                (ret[0] * ret[0]) - (ret[1] * ret[1]) - (ret[2] * ret[2])));
            part = (packed >> 30) & 3;
            switch(part)
            {
            case 0:
            {
                auto tmp = ret;
                ret[0] = largest_val;
                ret[1] = tmp[0];
                ret[2] = tmp[1];
                ret[3] = tmp[2];
                break;
            }
            case 1:
            {
                auto tmp = ret;
                ret[0] = tmp[0];
                ret[1] = largest_val;
                ret[2] = tmp[1];
                ret[3] = tmp[2];
                break;
            }
            case 2:
            {
                auto tmp = ret;
                ret[0] = tmp[0];
                ret[1] = tmp[1];
                ret[2] = largest_val;
                ret[3] = tmp[2];
                break;
            }
            case 3:
                ret[3] = largest_val;
                break;
            default:
                assert(false);
                break;
            }
        }
        return ret;
    }   // extractNormalizedSignedFloats
    // ------------------------------------------------------------------------
    // Please normalize vector before compressing
    // ------------------------------------------------------------------------
    inline uint32_t compressVector3(const irr::core::vector3df& vec)
    {
        return normalizedSignedFloatsTo1010102({{vec.X, vec.Y, vec.Z}});
    }   // compressVector3
    // ------------------------------------------------------------------------
    inline core::vector3df decompressVector3(uint32_t packed)
    {
        const std::array<float, 4> out = extractNormalizedSignedFloats(packed);
        core::vector3df ret(out[0], out[1], out[2]);
        return ret.normalize();
    }   // decompressVector3
    // ------------------------------------------------------------------------
    inline uint32_t compressQuaternion(const btQuaternion& q)
    {
        const float length = q.length();
        assert(length != 0.0f);
        std::array<float, 4> tmp_2 =
            {{
                q.x() / length,
                q.y() / length,
                q.z() / length,
                q.w() / length
            }};
        std::array<float, 3> tmp_3 = {};
        auto ret = std::max_element(tmp_2.begin(), tmp_2.end(),
            [](float a, float b) { return std::abs(a) < std::abs(b); });
        int extra_2_bit = int(std::distance(tmp_2.begin(), ret));
        float sqrt_2 = sqrtf(2.0f);
        switch (extra_2_bit)
        {
        case 0:
        {
            float neg = tmp_2[0] < 0.0f ? -1.0f : 1.0f;
            tmp_3[0] = tmp_2[1] * neg * sqrt_2;
            tmp_3[1] = tmp_2[2] * neg * sqrt_2;
            tmp_3[2] = tmp_2[3] * neg * sqrt_2;
            break;
        }
        case 1:
        {
            float neg = tmp_2[1] < 0.0f ? -1.0f : 1.0f;
            tmp_3[0] = tmp_2[0] * neg * sqrt_2;
            tmp_3[1] = tmp_2[2] * neg * sqrt_2;
            tmp_3[2] = tmp_2[3] * neg * sqrt_2;
            break;
        }
        case 2:
        {
            float neg = tmp_2[2] < 0.0f ? -1.0f : 1.0f;
            tmp_3[0] = tmp_2[0] * neg * sqrt_2;
            tmp_3[1] = tmp_2[1] * neg * sqrt_2;
            tmp_3[2] = tmp_2[3] * neg * sqrt_2;
            break;
        }
        case 3:
        {
            float neg = tmp_2[3] < 0.0f ? -1.0f : 1.0f;
            tmp_3[0] = tmp_2[0] * neg * sqrt_2;
            tmp_3[1] = tmp_2[1] * neg * sqrt_2;
            tmp_3[2] = tmp_2[2] * neg * sqrt_2;
            break;
        }
        default:
            assert(false);
            break;
        }
        return normalizedSignedFloatsTo1010102(tmp_3, extra_2_bit);
    }   // compressQuaternion
    // ------------------------------------------------------------------------
    inline uint32_t compressIrrQuaternion(const core::quaternion& q)
    {
        return compressQuaternion(btQuaternion(q.X, q.Y, q.Z, q.W));
    }
    // ------------------------------------------------------------------------
    inline core::quaternion decompressQuaternion(uint32_t packed)
    {
        const std::array<float, 4> out = extractNormalizedSignedFloats(packed,
            true/*calculate_w*/);
        core::quaternion ret(out[0], out[1], out[2], out[3]);
        return ret.normalize();
    }   // decompressQuaternion
    // ------------------------------------------------------------------------
    inline btQuaternion decompressbtQuaternion(uint32_t packed)
    {
        const std::array<float, 4> out = extractNormalizedSignedFloats(packed,
            true/*calculate_w*/);
        btQuaternion ret(out[0], out[1], out[2], out[3]);
        return ret.normalize();
    }   // decompressbtQuaternion
    // ------------------------------------------------------------------------
    inline std::array<float, 4> getQuaternionInternal(const core::matrix4& m)
    {
        btVector3 row[3];
        memcpy(&row[0][0], &m[0], 12);
        memcpy(&row[1][0], &m[4], 12);
        memcpy(&row[2][0], &m[8], 12);
        std::array<float, 4> q;
        float root = row[0].x() + row[1].y() + row[2].z();
        const float trace = root;
        if (trace > 0.0f)
        {
            root = sqrtf(trace + 1.0f);
            q[3] = 0.5f * root;
            root = 0.5f / root;
            q[0] = root * (row[1].z() - row[2].y());
            q[1] = root * (row[2].x() - row[0].z());
            q[2] = root * (row[0].y() - row[1].x());
        }
        else
        {
            static int next[3] = {1, 2, 0};
            int i = 0;
            int j = 0;
            int k = 0;
            if (row[1].y() > row[0].x())
            {
                i = 1;
            }
            if (row[2].z() > row[i][i])
            {
                i = 2;
            }
            j = next[i];
            k = next[j];

            root = sqrtf(row[i][i] - row[j][j] - row[k][k] + 1.0f);
            q[i] = 0.5f * root;
            root = 0.5f / root;
            q[j] = root * (row[i][j] + row[j][i]);
            q[k] = root * (row[i][k] + row[k][i]);
            q[3] = root * (row[j][k] - row[k][j]);
        }
        return q;
    }
    // ------------------------------------------------------------------------
    inline core::quaternion getQuaternion(const core::matrix4& m)
    {
        std::array<float, 4> q = getQuaternionInternal(m);
        return core::quaternion(q[0], q[1], q[2], q[3]).normalize();
    }
    // ------------------------------------------------------------------------
    inline btQuaternion getBulletQuaternion(const core::matrix4& m)
    {
        std::array<float, 4> q = getQuaternionInternal(m);
        return btQuaternion(q[0], q[1], q[2], q[3]).normalize();
    }
    // ------------------------------------------------------------------------

    inline uint32_t quickTangent(uint32_t packed_normal)
    {
        core::vector3df normal = decompressVector3(packed_normal);
        core::vector3df tangent;
        core::vector3df c1 =
            normal.crossProduct(core::vector3df(0.0f, 0.0f, 1.0f));
        core::vector3df c2 =
            normal.crossProduct(core::vector3df(0.0f, 1.0f, 0.0f));
        if (c1.getLengthSQ() > c2.getLengthSQ())
        {
            tangent = c1;
        }
        else
        {
            tangent = c2;
        }
        tangent.normalize();
        // Assume bitangent sign is positive 1.0f
        return compressVector3(tangent) | 1 << 30;
    }   // quickTangent
    // ------------------------------------------------------------------------
    /** Round and save compressed values (optionally) btTransform.
     *  It will round with 2 digits with min / max +/- 2^23 / 100 for origin in
     *  btTransform and call compressQuaternion above to compress the rotation
     *  part, if compressed_data is provided, 3 24 bits and 1 32 bits of
     *  compressed data will be written in an int[4] array.
     */
    inline void compressbtTransform(btTransform& cur_t,
                                    int* compressed_data = NULL)
    {
        int x = (int)(cur_t.getOrigin().x() * 100.0f);
        int y = (int)(cur_t.getOrigin().y() * 100.0f);
        int z = (int)(cur_t.getOrigin().z() * 100.0f);
        x = core::clamp(x, -0x800000, 0x7fffff);
        y = core::clamp(y, -0x800000, 0x7fffff);
        z = core::clamp(z, -0x800000, 0x7fffff);
        uint32_t compressed_q = compressQuaternion(cur_t.getRotation());
        cur_t.setOrigin(btVector3(
            (float)x / 100.0f,
            (float)y / 100.0f,
            (float)z / 100.0f));
        cur_t.setRotation(decompressbtQuaternion(compressed_q));
        if (compressed_data)
        {
            compressed_data[0] = x;
            compressed_data[1] = y;
            compressed_data[2] = z;
            compressed_data[3] = (int)compressed_q;
        }
    }   // compressbtTransform
    // ------------------------------------------------------------------------
    inline btTransform decompressbtTransform(int* compressed_data)
    {
        btTransform trans;
        trans.setOrigin(btVector3(
            (float)compressed_data[0] / 100.0f,
            (float)compressed_data[1] / 100.0f,
            (float)compressed_data[2] / 100.0f));
        trans.setRotation(decompressbtQuaternion(
            (uint32_t)compressed_data[3]));
        return trans;
    }   // decompressbtTransform
    // ------------------------------------------------------------------------
    void unitTesting();
}

#endif