summaryrefslogtreecommitdiffstats
path: root/src/Runtime/ogl-runtime/src/system/Qt3DSMatrix.cpp
blob: b5310d0e03db55b113935c98700c1ae4deab9bde (plain)
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
/****************************************************************************
**
** Copyright (C) 1993-2009 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "SystemPrefix.h"

//==============================================================================
//	Includes
//==============================================================================
#include "Qt3DSMatrix.h"
#include "Qt3DSVector3.h"
#include "Qt3DSEulerAngles.h"
#include "Qt3DSDataLogger.h"
#include <math.h>

//==============================================================================
//	Namespace
//==============================================================================
namespace Q3DStudio {

//==============================================================================
//	Constants
//==============================================================================
FLOAT g_IdentityInit[4][4] = { { 1.0f, 0.0f, 0.0f, 0.0f },
                               { 0.0f, 1.0f, 0.0f, 0.0f },
                               { 0.0f, 0.0f, 1.0f, 0.0f },
                               { 0.0f, 0.0f, 0.0f, 1.0f } };

const RuntimeMatrix RuntimeMatrix::IDENTITY = RuntimeMatrix(g_IdentityInit);
extern const FLOAT RUNTIME_EPSILON;

//==============================================================================
/**
 *	Empty constructor.
 *	Initializes this matrix to the identity matrix.
 */
RuntimeMatrix::RuntimeMatrix(const BOOL inInitializeIdentity /*= true*/)
{
    if (inInitializeIdentity)
        Q3DStudio_memcpy(&m_Data, &IDENTITY, sizeof(m_Data));
}

//==============================================================================
/**
 *	Copy constructor.
 *	@param inMatrix			the source matrix to copy
 */
RuntimeMatrix::RuntimeMatrix(const RuntimeMatrix &inMatrix)
{
    Q3DStudio_memcpy(&m_Data, &inMatrix.m_Data, sizeof(m_Data));
}

//==============================================================================
/**
 *	Assignment constructor.
 *	Initializes the matrix from an array.
 *	@param inComponents		an array of 16 values
 */
RuntimeMatrix::RuntimeMatrix(const FLOAT inComponents[4][4])
{
    if (inComponents)
        Q3DStudio_memcpy(&m_Data, inComponents, sizeof(m_Data));
}

//==============================================================================
/**
 *	Sets this matrix to a NULL matrix with all values at zero.
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::Zero()
{
    Q3DStudio_memset(&m_Data, 0, sizeof(m_Data));
    return *this;
}

//==============================================================================
/**
 *	Sets the matrix to the identity matrix.
 *	@return reference to this matrix
 */
RuntimeMatrix &RuntimeMatrix::Identity()
{
    Q3DStudio_memcpy(&m_Data, &IDENTITY, sizeof(m_Data));
    return *this;
}

//==============================================================================
/**
 *	Copies the elements from another matrix.
 *	@param inMatrix		the source matrix
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::Set(const RuntimeMatrix &inMatrix)
{
    Q3DStudio_memcpy(&m_Data, &inMatrix.m_Data, sizeof(m_Data));
    return *this;
}

//==============================================================================
/**
 *	Copies the given components to this matrix.
 *	@param inComponents		an array of 16 components
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::Set(const FLOAT inComponents[4][4])
{
    if (inComponents)
        Q3DStudio_memcpy(&m_Data, inComponents, sizeof(m_Data));
    return *this;
}

//==============================================================================
/**
 *	Heavily optimized assignment method.  This has the same result as creating
 *	four full 4x4 matrices and multiplying them together.
 *	@todo Look into optimized code.
 *	@param inTranslation		translation coordinates
 *	@param inRotation			Euler angle rotations
 *	@param inScale				scaling dimensions
 *	@param inPivot				pivot offset vector
 *	@param inRotationOrder		rotation order
 *	@param inCoordinateSystem	the coordindate system
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::Set(const RuntimeVector3 &inTranslation, const RuntimeVector3 &inRotation,
                      const RuntimeVector3 &inScale, const RuntimeVector3 &inPivot, const UINT8 inRotationOrder,
                      const UINT8 inCoordinateSystem)
{
    //	*this = m_ScaleMatrix * m_PivotMatrix * m_RotMatrix * m_TransMatrix;	// Distributed
    //original - plain speak

    RuntimeVector3 theScaledPivot;
    theScaledPivot.m_X = -inPivot.m_X * inScale.m_X;
    theScaledPivot.m_Y = -inPivot.m_Y * inScale.m_Y;
    theScaledPivot.m_Z = -inPivot.m_Z * inScale.m_Z;

    // This would take care of rotation in the right order
    RuntimeMatrix theRotation;
    theRotation.SetRotate(inRotation, inRotationOrder, inCoordinateSystem);

    Q3DStudio_memcpy(&m_Data, &IDENTITY, sizeof(m_Data));
    m_Data[0][0] = inScale.m_X;
    m_Data[1][1] = inScale.m_Y;
    m_Data[2][2] = inScale.m_Z;

    m_Data[3][0] = theScaledPivot.m_X;
    m_Data[3][1] = theScaledPivot.m_Y;

    if (inCoordinateSystem == ORIENTATION_LEFT_HANDED)
        m_Data[3][2] = theScaledPivot.m_Z;
    else
        m_Data[3][2] = -theScaledPivot.m_Z;

    MultiplyAffine(theRotation); // This could be optimized to only 9 multiplications instead of 16
                                 // if you do them by hand

    m_Data[3][0] += inTranslation.m_X;
    m_Data[3][1] += inTranslation.m_Y;

    if (inCoordinateSystem == ORIENTATION_LEFT_HANDED)
        m_Data[3][2] += inTranslation.m_Z;
    else
        m_Data[3][2] += -inTranslation.m_Z;

    if (inCoordinateSystem == ORIENTATION_LEFT_HANDED)
        return FlipCoordinateSystem();

    return *this;
}

//==============================================================================
/**
 *	Sets the translation portion of the matrix without affecting the rest.
 *
 *	If you want a pure translation matrix you must make sure to start with
 *	the identity matrix.
 *	@param inTranslate		a translation vector
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::SetTranslate(const RuntimeVector3 &inTranslate)
{
    m_Data[3][0] = inTranslate.m_X;
    m_Data[3][1] = inTranslate.m_Y;
    m_Data[3][2] = inTranslate.m_Z;
    return *this;
}

//==============================================================================
/**
 *	Sets the rotation portion of the matrix without affecting the rest.
 *
 *	FYI: The rotate and scale portions overlap.  If you want a pure scale matrix
 *	you must make sure to start with the identity matrix.
 *	@param inRotation		a quaternion describing the rotation
 *	@return reference to this modified matrix
 */
// CMatrix& CMatrix::SetRotate( const CQuaternion& inRotation )
//{
//	inRotation.ToMatrix( *this );
//	return *this;
//}

//==============================================================================
/**
 *	Sets the rotation portion of the matrix without affecting the rest.
 *
 *	FYI: The rotate and scale portions overlap.  If you want a pure rotation matrix
 *	you must make sure to start with the identity matrix.
 *	@param inRotation			Euler angle rotation
 *	@param inRotationOrder		rotation order
 *	@param inCoordinateSystem	the coordindate system
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::SetRotate(const RuntimeVector3 &inRotation, const UINT8 inRotationOrder,
                            const UINT8 inCoordinateSystem)
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);
    Q3DStudio_UNREFERENCED_PARAMETER(inCoordinateSystem);

    EulerAngles theEulerAngles;
    switch (inRotationOrder) {
    case ROTATIONORDER_XYZ:
        theEulerAngles.x = inRotation.m_X;
        theEulerAngles.y = inRotation.m_Y;
        theEulerAngles.z = inRotation.m_Z;
        theEulerAngles.w = EulOrdXYZs;
        break;
    case ROTATIONORDER_XYZR:
        theEulerAngles.x = inRotation.m_X;
        theEulerAngles.y = inRotation.m_Y;
        theEulerAngles.z = inRotation.m_Z;
        theEulerAngles.w = EulOrdXYZr;
        break;
    case ROTATIONORDER_YZX:
        theEulerAngles.x = inRotation.m_Y;
        theEulerAngles.y = inRotation.m_Z;
        theEulerAngles.z = inRotation.m_X;
        theEulerAngles.w = EulOrdYZXs;
        break;
    case ROTATIONORDER_YZXR:
        theEulerAngles.x = inRotation.m_Y;
        theEulerAngles.y = inRotation.m_Z;
        theEulerAngles.z = inRotation.m_X;
        theEulerAngles.w = EulOrdYZXr;
        break;
    case ROTATIONORDER_ZXY:
        theEulerAngles.x = inRotation.m_Z;
        theEulerAngles.y = inRotation.m_X;
        theEulerAngles.z = inRotation.m_Y;
        theEulerAngles.w = EulOrdZXYs;
        break;
    case ROTATIONORDER_ZXYR:
        theEulerAngles.x = inRotation.m_Z;
        theEulerAngles.y = inRotation.m_X;
        theEulerAngles.z = inRotation.m_Y;
        theEulerAngles.w = EulOrdZXYr;
        break;
    case ROTATIONORDER_XZY:
        theEulerAngles.x = inRotation.m_X;
        theEulerAngles.y = inRotation.m_Z;
        theEulerAngles.z = inRotation.m_Y;
        theEulerAngles.w = EulOrdXZYs;
        break;
    case ROTATIONORDER_XZYR:
        theEulerAngles.x = inRotation.m_X;
        theEulerAngles.y = inRotation.m_Z;
        theEulerAngles.z = inRotation.m_Y;
        theEulerAngles.w = EulOrdXZYr;
        break;
    case ROTATIONORDER_YXZ:
        theEulerAngles.x = inRotation.m_Y;
        theEulerAngles.y = inRotation.m_X;
        theEulerAngles.z = inRotation.m_Z;
        theEulerAngles.w = EulOrdYXZs;
        break;
    case ROTATIONORDER_YXZR:
        theEulerAngles.x = inRotation.m_Y;
        theEulerAngles.y = inRotation.m_X;
        theEulerAngles.z = inRotation.m_Z;
        theEulerAngles.w = EulOrdYXZr;
        break;
    case ROTATIONORDER_ZYX:
        theEulerAngles.x = inRotation.m_Z;
        theEulerAngles.y = inRotation.m_Y;
        theEulerAngles.z = inRotation.m_X;
        theEulerAngles.w = EulOrdZYXs;
        break;
    case ROTATIONORDER_ZYXR:
        theEulerAngles.x = inRotation.m_Z;
        theEulerAngles.y = inRotation.m_Y;
        theEulerAngles.z = inRotation.m_X;
        theEulerAngles.w = EulOrdZYXr;
        break;
    default: // defaults to Studio's rotation type
        theEulerAngles.x = inRotation.m_Y;
        theEulerAngles.y = inRotation.m_X;
        theEulerAngles.z = inRotation.m_Z;
        theEulerAngles.w = EulOrdYXZs;
        break;
    }

    theEulerAngles.x *= -1;
    theEulerAngles.y *= -1;
    theEulerAngles.z *= -1;

    CEulerAngleConverter theConverter;
    theConverter.Eul_ToHMatrix(theEulerAngles, m_Data);

    return *this;
}

//==============================================================================
/**
 *	Sets the scale portion of the matrix without affecting the rest.
 *
 *	FYI: The rotate and scale portions overlap.  If you want a pure scale matrix
 *	you must make sure to start with the identity matrix.
 *	@param inScale		scale vector
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::SetScale(const RuntimeVector3 &inScale)
{
    m_Data[0][0] = inScale.m_X;
    m_Data[1][1] = inScale.m_Y;
    m_Data[2][2] = inScale.m_Z;
    return *this;
}

//==============================================================================
/**
 *	Check this matrix against the identity matrix.
 *	@return true if this matrix is equivalent to the identity matrix
 */
BOOL RuntimeMatrix::IsIdentity() const
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    return *this == IDENTITY;
}

//==============================================================================
/**
 *	Checks to see if this matrix is affine.
 *	An affine matrix has the last row being [ 0 0 0 1 ]
 *	@return true if the matrix is affine
 */
BOOL RuntimeMatrix::IsAffine() const
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    return m_Data[0][3] == 0 && m_Data[1][3] == 0 && m_Data[2][3] == 0 && m_Data[3][3] == 1.0f;
}

//==============================================================================
/**
 *	Appends the matrix to include a translation.  Equivalent to post-multiplying
 *	this matrix with a transformation matrix derived from the given vector.
 *	@param inTranslation	transformation vector applied
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::Translate(const RuntimeVector3 &inTranslation)
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    m_Data[3][0] += inTranslation.m_X;
    m_Data[3][1] += inTranslation.m_Y;
    m_Data[3][2] += inTranslation.m_Z;
    return *this;
}

//==============================================================================
/**
 *	Appends the matrix to include a rotation.  Equivalent to post-multiplying
 *	this matrix with a rotation matrix derived from the given quaternion.
 *	@param inRotation		the rotation quaternion applied
 *	@return reference to this modified matrix
 */
// CMatrix& CMatrix::Rotate( const CQuaternion& inRotation )
//{
//	CMatrix		theRotation;
//	return MultiplyAffine( inRotation.ToMatrix( theRotation ) );
//}

//==============================================================================
/**
 *	Appends the matrix to include scaling.  Equivalent to post-multiplying
 *	this matrix with a scale matrix derived from the given vector.
 *	@param inScale		the scale vector applied
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::Scale(const RuntimeVector3 &inScale)
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    m_Data[0][0] *= inScale.m_X;
    m_Data[0][1] *= inScale.m_X;
    m_Data[0][2] *= inScale.m_X;

    m_Data[1][0] *= inScale.m_Y;
    m_Data[1][1] *= inScale.m_Y;
    m_Data[1][2] *= inScale.m_Y;

    m_Data[2][0] *= inScale.m_Z;
    m_Data[2][1] *= inScale.m_Z;
    m_Data[2][2] *= inScale.m_Z;
    return *this;
}

//==============================================================================
/**
 *	Flips the matrix elements around the identity diagonal.
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::Transpose()
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    FLOAT theSwap = m_Data[1][0];
    m_Data[1][0] = m_Data[0][1];
    m_Data[0][1] = theSwap;

    theSwap = m_Data[2][0];
    m_Data[2][0] = m_Data[0][2];
    m_Data[0][2] = theSwap;

    theSwap = m_Data[3][0];
    m_Data[3][0] = m_Data[0][3];
    m_Data[0][3] = theSwap;

    theSwap = m_Data[2][1];
    m_Data[2][1] = m_Data[1][2];
    m_Data[1][2] = theSwap;

    theSwap = m_Data[3][1];
    m_Data[3][1] = m_Data[1][3];
    m_Data[1][3] = theSwap;

    theSwap = m_Data[3][2];
    m_Data[3][2] = m_Data[2][3];
    m_Data[2][3] = theSwap;

    return *this;
}

//==============================================================================
/**
 * Compute the inverse of a 3D affine matrix; i.e. a matrix with a
 * dimensionality of 4 where the bottom row has the entries (0, 0, 0, 1).
 *
 * This procedure treats the 4 by 4 matrix as a block matrix and
 * calculates the inverse of one submatrix for a significant performance
 * improvement over a general procedure that can invert any non-singular matrix:
@code
       |           | -1    |   -1       -1 |
       | A       C |       |  A    -C A    |
  -1   |           |       |               |
 M  =  |           |   =   |               |
       | 0       1 |       |  0       1    |
       |           |       |               |
@endcode
 * where M is a 4 by 4 matrix,
 * A is the 3 by 3 upper left submatrix of M,
 * C is the 3 by 1 upper right submatrix of M.
 *
 * @return the determinant of matrix
 */
FLOAT RuntimeMatrix::Invert()
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    const FLOAT PRECISIONLIMIT = 1.0e-07f;
    FLOAT thePositiveDet = 0.0f;
    FLOAT theNegativeDet = 0.0f;
    FLOAT theTempDet;

    // Calculate the determinant of submatrix A and determine if the
    // the matrix is singular as limited by the float precision.
    theTempDet = m_Data[0][0] * m_Data[1][1] * m_Data[2][2];
    if (theTempDet >= 0.0f)
        thePositiveDet += theTempDet;
    else
        theNegativeDet += theTempDet;

    theTempDet = m_Data[0][1] * m_Data[1][2] * m_Data[2][0];
    if (theTempDet >= 0.0f)
        thePositiveDet += theTempDet;
    else
        theNegativeDet += theTempDet;

    theTempDet = m_Data[0][2] * m_Data[1][0] * m_Data[2][1];
    if (theTempDet >= 0.0f)
        thePositiveDet += theTempDet;
    else
        theNegativeDet += theTempDet;

    theTempDet = -m_Data[0][2] * m_Data[1][1] * m_Data[2][0];
    if (theTempDet >= 0.0f)
        thePositiveDet += theTempDet;
    else
        theNegativeDet += theTempDet;

    theTempDet = -m_Data[0][1] * m_Data[1][0] * m_Data[2][2];
    if (theTempDet >= 0.0f)
        thePositiveDet += theTempDet;
    else
        theNegativeDet += theTempDet;

    theTempDet = -m_Data[0][0] * m_Data[1][2] * m_Data[2][1];
    if (theTempDet >= 0.0f)
        thePositiveDet += theTempDet;
    else
        theNegativeDet += theTempDet;

    // Is the submatrix A nonsingular? (i.e. there is an inverse?)
    FLOAT theDeterminant = thePositiveDet + theNegativeDet;
    if (theDeterminant != 0
        || ::fabs(theDeterminant / (thePositiveDet - theNegativeDet)) >= PRECISIONLIMIT) {
        RuntimeMatrix theInverse;
        FLOAT theInvDeterminant = 1.0f / theDeterminant;

        // Calculate inverse(A) = adj(A) / det(A)
        theInverse.m_Data[0][0] =
            (m_Data[1][1] * m_Data[2][2] - m_Data[1][2] * m_Data[2][1]) * theInvDeterminant;
        theInverse.m_Data[1][0] =
            -(m_Data[1][0] * m_Data[2][2] - m_Data[1][2] * m_Data[2][0]) * theInvDeterminant;
        theInverse.m_Data[2][0] =
            (m_Data[1][0] * m_Data[2][1] - m_Data[1][1] * m_Data[2][0]) * theInvDeterminant;
        theInverse.m_Data[0][1] =
            -(m_Data[0][1] * m_Data[2][2] - m_Data[0][2] * m_Data[2][1]) * theInvDeterminant;
        theInverse.m_Data[1][1] =
            (m_Data[0][0] * m_Data[2][2] - m_Data[0][2] * m_Data[2][0]) * theInvDeterminant;
        theInverse.m_Data[2][1] =
            -(m_Data[0][0] * m_Data[2][1] - m_Data[0][1] * m_Data[2][0]) * theInvDeterminant;
        theInverse.m_Data[0][2] =
            (m_Data[0][1] * m_Data[1][2] - m_Data[0][2] * m_Data[1][1]) * theInvDeterminant;
        theInverse.m_Data[1][2] =
            -(m_Data[0][0] * m_Data[1][2] - m_Data[0][2] * m_Data[1][0]) * theInvDeterminant;
        theInverse.m_Data[2][2] =
            (m_Data[0][0] * m_Data[1][1] - m_Data[0][1] * m_Data[1][0]) * theInvDeterminant;

        // Calculate -C * inverse(A)
        theInverse.m_Data[3][0] =
            -(m_Data[3][0] * theInverse.m_Data[0][0] + m_Data[3][1] * theInverse.m_Data[1][0]
              + m_Data[3][2] * theInverse.m_Data[2][0]);
        theInverse.m_Data[3][1] =
            -(m_Data[3][0] * theInverse.m_Data[0][1] + m_Data[3][1] * theInverse.m_Data[1][1]
              + m_Data[3][2] * theInverse.m_Data[2][1]);
        theInverse.m_Data[3][2] =
            -(m_Data[3][0] * theInverse.m_Data[0][2] + m_Data[3][1] * theInverse.m_Data[1][2]
              + m_Data[3][2] * theInverse.m_Data[2][2]);

        // assign ourselves to the inverse
        *this = theInverse;
    }

    return theDeterminant;
}

//==============================================================================
/**
 *	Fast multiplication of two affine 4x4 matrices.  No affine pre-check.
 *  @todo MF - Convert to SSE Assembly Code
 *	@param inMatrix		the source matrix
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::MultiplyAffine(const RuntimeMatrix &inMatrix)
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    FLOAT theMult[4][4];

    theMult[0][0] = m_Data[0][0] * inMatrix.m_Data[0][0] + m_Data[0][1] * inMatrix.m_Data[1][0]
        + m_Data[0][2] * inMatrix.m_Data[2][0];
    theMult[0][1] = m_Data[0][0] * inMatrix.m_Data[0][1] + m_Data[0][1] * inMatrix.m_Data[1][1]
        + m_Data[0][2] * inMatrix.m_Data[2][1];
    theMult[0][2] = m_Data[0][0] * inMatrix.m_Data[0][2] + m_Data[0][1] * inMatrix.m_Data[1][2]
        + m_Data[0][2] * inMatrix.m_Data[2][2];
    theMult[0][3] = 0;

    theMult[1][0] = m_Data[1][0] * inMatrix.m_Data[0][0] + m_Data[1][1] * inMatrix.m_Data[1][0]
        + m_Data[1][2] * inMatrix.m_Data[2][0];
    theMult[1][1] = m_Data[1][0] * inMatrix.m_Data[0][1] + m_Data[1][1] * inMatrix.m_Data[1][1]
        + m_Data[1][2] * inMatrix.m_Data[2][1];
    theMult[1][2] = m_Data[1][0] * inMatrix.m_Data[0][2] + m_Data[1][1] * inMatrix.m_Data[1][2]
        + m_Data[1][2] * inMatrix.m_Data[2][2];
    theMult[1][3] = 0;

    theMult[2][0] = m_Data[2][0] * inMatrix.m_Data[0][0] + m_Data[2][1] * inMatrix.m_Data[1][0]
        + m_Data[2][2] * inMatrix.m_Data[2][0];
    theMult[2][1] = m_Data[2][0] * inMatrix.m_Data[0][1] + m_Data[2][1] * inMatrix.m_Data[1][1]
        + m_Data[2][2] * inMatrix.m_Data[2][1];
    theMult[2][2] = m_Data[2][0] * inMatrix.m_Data[0][2] + m_Data[2][1] * inMatrix.m_Data[1][2]
        + m_Data[2][2] * inMatrix.m_Data[2][2];
    theMult[2][3] = 0;

    theMult[3][0] = m_Data[3][0] * inMatrix.m_Data[0][0] + m_Data[3][1] * inMatrix.m_Data[1][0]
        + m_Data[3][2] * inMatrix.m_Data[2][0] + inMatrix.m_Data[3][0];
    theMult[3][1] = m_Data[3][0] * inMatrix.m_Data[0][1] + m_Data[3][1] * inMatrix.m_Data[1][1]
        + m_Data[3][2] * inMatrix.m_Data[2][1] + inMatrix.m_Data[3][1];
    theMult[3][2] = m_Data[3][0] * inMatrix.m_Data[0][2] + m_Data[3][1] * inMatrix.m_Data[1][2]
        + m_Data[3][2] * inMatrix.m_Data[2][2] + inMatrix.m_Data[3][2];
    theMult[3][3] = 1.0f;

    return Set(theMult);
}

//==============================================================================
/**
 *	Standard matrix multiplication
 *  @todo MF - Convert to SSE Assembly Code
 *	@param inMatrix		matrix to multiply with
 */
RuntimeMatrix &RuntimeMatrix::Multiply(const RuntimeMatrix &inMatrix)
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    FLOAT theMult[4][4];

    theMult[0][0] = m_Data[0][0] * inMatrix.m_Data[0][0] + m_Data[1][0] * inMatrix.m_Data[0][1]
        + m_Data[2][0] * inMatrix.m_Data[0][2] + m_Data[3][0] * inMatrix.m_Data[0][3];
    theMult[0][1] = m_Data[0][1] * inMatrix.m_Data[0][0] + m_Data[1][1] * inMatrix.m_Data[0][1]
        + m_Data[2][1] * inMatrix.m_Data[0][2] + m_Data[3][1] * inMatrix.m_Data[0][3];
    theMult[0][2] = m_Data[0][2] * inMatrix.m_Data[0][0] + m_Data[1][2] * inMatrix.m_Data[0][1]
        + m_Data[2][2] * inMatrix.m_Data[0][2] + m_Data[3][2] * inMatrix.m_Data[0][3];
    theMult[0][3] = m_Data[0][3] * inMatrix.m_Data[0][0] + m_Data[1][3] * inMatrix.m_Data[0][1]
        + m_Data[2][3] * inMatrix.m_Data[0][2] + m_Data[3][3] * inMatrix.m_Data[0][3];
    theMult[1][0] = m_Data[0][0] * inMatrix.m_Data[1][0] + m_Data[1][0] * inMatrix.m_Data[1][1]
        + m_Data[2][0] * inMatrix.m_Data[1][2] + m_Data[3][0] * inMatrix.m_Data[1][3];
    theMult[1][1] = m_Data[0][1] * inMatrix.m_Data[1][0] + m_Data[1][1] * inMatrix.m_Data[1][1]
        + m_Data[2][1] * inMatrix.m_Data[1][2] + m_Data[3][1] * inMatrix.m_Data[1][3];
    theMult[1][2] = m_Data[0][2] * inMatrix.m_Data[1][0] + m_Data[1][2] * inMatrix.m_Data[1][1]
        + m_Data[2][2] * inMatrix.m_Data[1][2] + m_Data[3][2] * inMatrix.m_Data[1][3];
    theMult[1][3] = m_Data[0][3] * inMatrix.m_Data[1][0] + m_Data[1][3] * inMatrix.m_Data[1][1]
        + m_Data[2][3] * inMatrix.m_Data[1][2] + m_Data[3][3] * inMatrix.m_Data[1][3];
    theMult[2][0] = m_Data[0][0] * inMatrix.m_Data[2][0] + m_Data[1][0] * inMatrix.m_Data[2][1]
        + m_Data[2][0] * inMatrix.m_Data[2][2] + m_Data[3][0] * inMatrix.m_Data[2][3];
    theMult[2][1] = m_Data[0][1] * inMatrix.m_Data[2][0] + m_Data[1][1] * inMatrix.m_Data[2][1]
        + m_Data[2][1] * inMatrix.m_Data[2][2] + m_Data[3][1] * inMatrix.m_Data[2][3];
    theMult[2][2] = m_Data[0][2] * inMatrix.m_Data[2][0] + m_Data[1][2] * inMatrix.m_Data[2][1]
        + m_Data[2][2] * inMatrix.m_Data[2][2] + m_Data[3][2] * inMatrix.m_Data[2][3];
    theMult[2][3] = m_Data[0][3] * inMatrix.m_Data[2][0] + m_Data[1][3] * inMatrix.m_Data[2][1]
        + m_Data[2][3] * inMatrix.m_Data[2][2] + m_Data[3][3] * inMatrix.m_Data[2][3];

    theMult[3][0] = m_Data[0][0] * inMatrix.m_Data[3][0] + m_Data[1][0] * inMatrix.m_Data[3][1]
        + m_Data[2][0] * inMatrix.m_Data[3][2] + m_Data[3][0] * inMatrix.m_Data[3][3];
    theMult[3][1] = m_Data[0][1] * inMatrix.m_Data[3][0] + m_Data[1][1] * inMatrix.m_Data[3][1]
        + m_Data[2][1] * inMatrix.m_Data[3][2] + m_Data[3][1] * inMatrix.m_Data[3][3];
    theMult[3][2] = m_Data[0][2] * inMatrix.m_Data[3][0] + m_Data[1][2] * inMatrix.m_Data[3][1]
        + m_Data[2][2] * inMatrix.m_Data[3][2] + m_Data[3][2] * inMatrix.m_Data[3][3];
    theMult[3][3] = m_Data[0][3] * inMatrix.m_Data[3][0] + m_Data[1][3] * inMatrix.m_Data[3][1]
        + m_Data[2][3] * inMatrix.m_Data[3][2] + m_Data[3][3] * inMatrix.m_Data[3][3];

    return Set(theMult);
}

//==============================================================================
/**
 *	Toggle between left-hand and right-hand coordinate system
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::FlipCoordinateSystem()
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    // rotation conversion
    m_Data[0][2] *= -1;
    m_Data[1][2] *= -1;
    m_Data[2][0] *= -1;
    m_Data[2][1] *= -1;

    // translation conversion
    m_Data[3][2] *= -1;

    return *this;
}

//==============================================================================
/**
 *	Rotate this matrix to align with the rotation of the specified matrix.
 *
 *	@param inMatrix		the maxtrix we are cloning.
 *	@param inMirrorFlag	flag indicating that we should flip the z and face the opposite
 *direction.
 *	@return	This matrix after the rotation.
 */
RuntimeMatrix &RuntimeMatrix::CloneRotation(const RuntimeMatrix &inMatrix, BOOL inMirrorFlag /*= false*/)
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    // Create the axes
    RuntimeVector3 theZ(inMatrix.Get(2, 0), inMatrix.Get(2, 1), inMatrix.Get(2, 2));
    if (inMirrorFlag)
        theZ *= -1;
    RuntimeVector3 theY(inMatrix.Get(1, 0), inMatrix.Get(1, 1), inMatrix.Get(1, 2));
    RuntimeVector3 theX(theY);
    theX.CrossProduct(theZ);

    // Normalize
    theX.Normalize();
    theY.Normalize();
    theZ.Normalize();

    // Copy it into the matrix
    m_Data[0][0] = theX.m_X;
    m_Data[0][1] = theX.m_Y;
    m_Data[0][2] = theX.m_Z;

    m_Data[1][0] = theY.m_X;
    m_Data[1][1] = theY.m_Y;
    m_Data[1][2] = theY.m_Z;

    m_Data[2][0] = theZ.m_X;
    m_Data[2][1] = theZ.m_Y;
    m_Data[2][2] = theZ.m_Z;

    return *this;
}

//==============================================================================
/**
 *	Compute the square distance between the position vectors of two transforms.
 *	@param inMatrix the other transform, signifying a global object position for example
 *	@return the square of the "distance" between the two transforms
 */
FLOAT RuntimeMatrix::SquareDistance(const RuntimeMatrix &inMatrix) const
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    FLOAT theResult = 0;
    FLOAT theFactor;

    theFactor = m_Data[3][0] - inMatrix.m_Data[3][0];
    theFactor *= theFactor;
    theResult += theFactor;

    theFactor = m_Data[3][1] - inMatrix.m_Data[3][1];
    theFactor *= theFactor;
    theResult += theFactor;

    theFactor = m_Data[3][2] - inMatrix.m_Data[3][2];
    theFactor *= theFactor;
    theResult += theFactor;

    return theResult;
}

//==============================================================================
/**
 *	Simple assignment operator.
 *	@param inMatrix	new matrix being assigned
 *	@return reference to this modified matrix
 */
RuntimeMatrix &RuntimeMatrix::operator=(const RuntimeMatrix &inMatrix)
{
    Q3DStudio_memcpy(&m_Data, &inMatrix.m_Data, sizeof(m_Data));
    return *this;
}

//==============================================================================
/**
 *	Compares this matrix's elements with another matrix's and returns true
 *	if the matrices are equivalent.
 *	@param inMatrix		the matrix we are comparing against
 *	@return true if all elements are within EPSILON of each other.
 */
BOOL RuntimeMatrix::operator==(const RuntimeMatrix &inMatrix) const
{
    PerfLogMathEvent1(DATALOGGER_MATRIX);

    for (INT32 iRow = 0; iRow < 4; ++iRow) {
        for (INT32 iCol = 0; iCol < 4; ++iCol) {
            if (::fabs(m_Data[iRow][iCol] - inMatrix.m_Data[iRow][iCol]) > RUNTIME_EPSILON)
                return false;
        }
    }
    return true;
}

//==============================================================================
/**
 *	Compares this matrix's elements with another matrix's and returns true
 *	if the matrices are not equivalent.
 *	@param inMatrix		the matrix we are comparing against
 *	@return true if one or more elements are more than EPSILON from each other
 */
BOOL RuntimeMatrix::operator!=(const RuntimeMatrix &inMatrix) const
{
    // Reuse same code path..
    return !(*this == inMatrix);
}

//==============================================================================
/**
 *	Standardized conversion to string.
 *	@param outString	string becoming a representation for the matrix
 */
// void CMatrix::ToString( AK_STRING& outString ) const
//{
//	INT8 theBuffer[ 256 ];
//
//	Q3DStudio_sprintf
//	(
//		theBuffer, 255,
//		"%.2f %.2f %.2f %.2f "
//		"%.2f %.2f %.2f %.2f "
//		"%.2f %.2f %.2f %.2f "
//		"%.2f %.2f %.2f %.2f",
//
//		m_Data.m[ 0 ][ 0 ],
//		m_Data.m[ 0 ][ 1 ],
//		m_Data.m[ 0 ][ 2 ],
//		m_Data.m[ 0 ][ 3 ],
//
//		m_Data.m[ 1 ][ 0 ],
//		m_Data.m[ 1 ][ 1 ],
//		m_Data.m[ 1 ][ 2 ],
//		m_Data.m[ 1 ][ 3 ],
//
//		m_Data.m[ 2 ][ 0 ],
//		m_Data.m[ 2 ][ 1 ],
//		m_Data.m[ 2 ][ 2 ],
//		m_Data.m[ 2 ][ 3 ],
//
//		m_Data.m[ 3 ][ 0 ],
//		m_Data.m[ 3 ][ 1 ],
//		m_Data.m[ 3 ][ 2 ],
//		m_Data.m[ 3 ][ 3 ]
//	);
//
//	outString = theBuffer;
//}
//
////==============================================================================
///**
// *	Standardized conversion from string.
// *	@param inString		string being a representation for the matrix
// */
// void CMatrix::FromString( const AK_STRING& inString )
//{
//	std::sscanf
//	(
//		inString.c_str( ),
//
//		"%f %f %f %f "
//		"%f %f %f %f "
//		"%f %f %f %f "
//		"%f %f %f %f",
//
//		&m_Data.m[ 0 ][ 0 ],
//		&m_Data.m[ 0 ][ 1 ],
//		&m_Data.m[ 0 ][ 2 ],
//		&m_Data.m[ 0 ][ 3 ],
//
//		&m_Data.m[ 1 ][ 0 ],
//		&m_Data.m[ 1 ][ 1 ],
//		&m_Data.m[ 1 ][ 2 ],
//		&m_Data.m[ 1 ][ 3 ],
//
//		&m_Data.m[ 2 ][ 0 ],
//		&m_Data.m[ 2 ][ 1 ],
//		&m_Data.m[ 2 ][ 2 ],
//		&m_Data.m[ 2 ][ 3 ],
//
//		&m_Data.m[ 3 ][ 0 ],
//		&m_Data.m[ 3 ][ 1 ],
//		&m_Data.m[ 3 ][ 2 ],
//		&m_Data.m[ 3 ][ 3 ]
//	);
//}

} // namespace Q3DStudio