forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmatrix.cpp
1353 lines (1113 loc) · 43.5 KB
/
vmatrix.cpp
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#if !defined(_STATIC_LINKED) || defined(_SHARED_LIB)
#include "basetypes.h"
#include "mathlib/vmatrix.h"
#include "mathlib/mathlib.h"
#include <string.h>
#include "mathlib/vector4d.h"
#include "ssemath.h"
#include "tier0/dbg.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#pragma warning (disable : 4700) // local variable 'x' used without having been initialized
// ------------------------------------------------------------------------------------------- //
// Helper functions.
// ------------------------------------------------------------------------------------------- //
#ifndef VECTOR_NO_SLOW_OPERATIONS
VMatrix SetupMatrixIdentity()
{
return VMatrix(
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);
}
VMatrix SetupMatrixTranslation(const Vector &vTranslation)
{
return VMatrix(
1.0f, 0.0f, 0.0f, vTranslation.x,
0.0f, 1.0f, 0.0f, vTranslation.y,
0.0f, 0.0f, 1.0f, vTranslation.z,
0.0f, 0.0f, 0.0f, 1.0f
);
}
VMatrix SetupMatrixScale(const Vector &vScale)
{
return VMatrix(
vScale.x, 0.0f, 0.0f, 0.0f,
0.0f, vScale.y, 0.0f, 0.0f,
0.0f, 0.0f, vScale.z, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
}
VMatrix SetupMatrixReflection(const VPlane &thePlane)
{
VMatrix mReflect, mBack, mForward;
Vector vOrigin, N;
N = thePlane.m_Normal;
mReflect.Init(
-2.0f*N.x*N.x + 1.0f, -2.0f*N.x*N.y, -2.0f*N.x*N.z, 0.0f,
-2.0f*N.y*N.x, -2.0f*N.y*N.y + 1.0f, -2.0f*N.y*N.z, 0.0f,
-2.0f*N.z*N.x, -2.0f*N.z*N.y, -2.0f*N.z*N.z + 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
vOrigin = thePlane.GetPointOnPlane();
mBack.Identity();
mBack.SetTranslation(-vOrigin);
mForward.Identity();
mForward.SetTranslation(vOrigin);
// (multiplied in reverse order, so it translates to the origin point,
// reflects, and translates back).
return mForward * mReflect * mBack;
}
VMatrix SetupMatrixProjection(const Vector &vOrigin, const VPlane &thePlane)
{
vec_t dot;
VMatrix mRet;
#define PN thePlane.m_Normal
#define PD thePlane.m_Dist;
dot = PN[0]*vOrigin.x + PN[1]*vOrigin.y + PN[2]*vOrigin.z - PD;
mRet.m[0][0] = dot - vOrigin.x * PN[0];
mRet.m[0][1] = -vOrigin.x * PN[1];
mRet.m[0][2] = -vOrigin.x * PN[2];
mRet.m[0][3] = -vOrigin.x * -PD;
mRet.m[1][0] = -vOrigin.y * PN[0];
mRet.m[1][1] = dot - vOrigin.y * PN[1];
mRet.m[1][2] = -vOrigin.y * PN[2];
mRet.m[1][3] = -vOrigin.y * -PD;
mRet.m[2][0] = -vOrigin.z * PN[0];
mRet.m[2][1] = -vOrigin.z * PN[1];
mRet.m[2][2] = dot - vOrigin.z * PN[2];
mRet.m[2][3] = -vOrigin.z * -PD;
mRet.m[3][0] = -PN[0];
mRet.m[3][1] = -PN[1];
mRet.m[3][2] = -PN[2];
mRet.m[3][3] = dot + PD;
#undef PN
#undef PD
return mRet;
}
VMatrix SetupMatrixAxisRot(const Vector &vAxis, vec_t fDegrees)
{
vec_t s, c, t; // sin, cos, 1-cos
vec_t tx, ty, tz;
vec_t sx, sy, sz;
vec_t fRadians;
fRadians = fDegrees * (M_PI / 180.0f);
s = (vec_t)sin(fRadians);
c = (vec_t)cos(fRadians);
t = 1.0f - c;
tx = t * vAxis.x; ty = t * vAxis.y; tz = t * vAxis.z;
sx = s * vAxis.x; sy = s * vAxis.y; sz = s * vAxis.z;
return VMatrix(
tx*vAxis.x + c, tx*vAxis.y - sz, tx*vAxis.z + sy, 0.0f,
tx*vAxis.y + sz, ty*vAxis.y + c, ty*vAxis.z - sx, 0.0f,
tx*vAxis.z - sy, ty*vAxis.z + sx, tz*vAxis.z + c, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
// Basically takes a cross product and then does the same thing as SetupMatrixAxisRot
// above, but takes advantage of the fact that the sin angle is precomputed.
VMatrix SetupMatrixAxisToAxisRot(const Vector &vFromAxis, const Vector &vToAxis)
{
Assert( vFromAxis.LengthSqr() == 1 ); // these axes
Assert( vToAxis.LengthSqr() == 1 ); // must be normal.
vec_t s, c, t; // sin(theta), cos(theta), 1-cos
vec_t tx, ty, tz;
vec_t sx, sy, sz;
Vector vAxis = vFromAxis.Cross(vToAxis);
s = vAxis.Length();
c = vFromAxis.Dot(vToAxis);
t = 1.0f - c;
if ( s > 0 )
{
vAxis *= 1.0/s;
tx = t * vAxis.x; ty = t * vAxis.y; tz = t * vAxis.z;
sx = s * vAxis.x; sy = s * vAxis.y; sz = s * vAxis.z;
return VMatrix(
tx*vAxis.x + c, tx*vAxis.y - sz, tx*vAxis.z + sy, 0.0f,
tx*vAxis.y + sz, ty*vAxis.y + c, ty*vAxis.z - sx, 0.0f,
tx*vAxis.z - sy, ty*vAxis.z + sx, tz*vAxis.z + c, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f);
}
else
{
return SetupMatrixIdentity();
}
}
VMatrix SetupMatrixAngles(const QAngle &vAngles)
{
VMatrix mRet;
MatrixFromAngles( vAngles, mRet );
return mRet;
}
VMatrix SetupMatrixOrgAngles(const Vector &origin, const QAngle &vAngles)
{
VMatrix mRet;
mRet.SetupMatrixOrgAngles( origin, vAngles );
return mRet;
}
#endif // VECTOR_NO_SLOW_OPERATIONS
#if 1
bool PlaneIntersection( const VPlane &vp1, const VPlane &vp2, const VPlane &vp3, Vector &vOut )
{
Vector v2Cross3 = CrossProduct( vp2.m_Normal, vp3.m_Normal );
float flDenom = DotProduct( vp1.m_Normal, v2Cross3 );
if ( fabs( flDenom ) < FLT_EPSILON )
return false;
Vector vRet = vp1.m_Dist * v2Cross3 + vp2.m_Dist * CrossProduct( vp3.m_Normal, vp1.m_Normal ) + vp3.m_Dist * CrossProduct( vp1.m_Normal, vp2.m_Normal );
vOut = vRet * ( 1.0 / flDenom );
return true;
}
#else // old slow innaccurate code
bool PlaneIntersection( const VPlane &vp1, const VPlane &vp2, const VPlane &vp3, Vector &vOut )
{
VMatrix mMat, mInverse;
mMat.Init(
vp1.m_Normal.x, vp1.m_Normal.y, vp1.m_Normal.z, -vp1.m_Dist,
vp2.m_Normal.x, vp2.m_Normal.y, vp2.m_Normal.z, -vp2.m_Dist,
vp3.m_Normal.x, vp3.m_Normal.y, vp3.m_Normal.z, -vp3.m_Dist,
0.0f, 0.0f, 0.0f, 1.0f
);
if(mMat.InverseGeneral(mInverse))
{
//vOut = mInverse * Vector(0.0f, 0.0f, 0.0f);
mInverse.GetTranslation( vOut );
return true;
}
else
{
return false;
}
}
#endif
// ------------------------------------------------------------------------------------------- //
// VMatrix functions.
// ------------------------------------------------------------------------------------------- //
VMatrix& VMatrix::operator=(const VMatrix &mOther)
{
m[0][0] = mOther.m[0][0];
m[0][1] = mOther.m[0][1];
m[0][2] = mOther.m[0][2];
m[0][3] = mOther.m[0][3];
m[1][0] = mOther.m[1][0];
m[1][1] = mOther.m[1][1];
m[1][2] = mOther.m[1][2];
m[1][3] = mOther.m[1][3];
m[2][0] = mOther.m[2][0];
m[2][1] = mOther.m[2][1];
m[2][2] = mOther.m[2][2];
m[2][3] = mOther.m[2][3];
m[3][0] = mOther.m[3][0];
m[3][1] = mOther.m[3][1];
m[3][2] = mOther.m[3][2];
m[3][3] = mOther.m[3][3];
return *this;
}
bool VMatrix::operator==( const VMatrix& src ) const
{
return !memcmp( src.m, m, sizeof(m) );
}
void VMatrix::MatrixMul( const VMatrix &vm, VMatrix &out ) const
{
out.Init(
m[0][0]*vm.m[0][0] + m[0][1]*vm.m[1][0] + m[0][2]*vm.m[2][0] + m[0][3]*vm.m[3][0],
m[0][0]*vm.m[0][1] + m[0][1]*vm.m[1][1] + m[0][2]*vm.m[2][1] + m[0][3]*vm.m[3][1],
m[0][0]*vm.m[0][2] + m[0][1]*vm.m[1][2] + m[0][2]*vm.m[2][2] + m[0][3]*vm.m[3][2],
m[0][0]*vm.m[0][3] + m[0][1]*vm.m[1][3] + m[0][2]*vm.m[2][3] + m[0][3]*vm.m[3][3],
m[1][0]*vm.m[0][0] + m[1][1]*vm.m[1][0] + m[1][2]*vm.m[2][0] + m[1][3]*vm.m[3][0],
m[1][0]*vm.m[0][1] + m[1][1]*vm.m[1][1] + m[1][2]*vm.m[2][1] + m[1][3]*vm.m[3][1],
m[1][0]*vm.m[0][2] + m[1][1]*vm.m[1][2] + m[1][2]*vm.m[2][2] + m[1][3]*vm.m[3][2],
m[1][0]*vm.m[0][3] + m[1][1]*vm.m[1][3] + m[1][2]*vm.m[2][3] + m[1][3]*vm.m[3][3],
m[2][0]*vm.m[0][0] + m[2][1]*vm.m[1][0] + m[2][2]*vm.m[2][0] + m[2][3]*vm.m[3][0],
m[2][0]*vm.m[0][1] + m[2][1]*vm.m[1][1] + m[2][2]*vm.m[2][1] + m[2][3]*vm.m[3][1],
m[2][0]*vm.m[0][2] + m[2][1]*vm.m[1][2] + m[2][2]*vm.m[2][2] + m[2][3]*vm.m[3][2],
m[2][0]*vm.m[0][3] + m[2][1]*vm.m[1][3] + m[2][2]*vm.m[2][3] + m[2][3]*vm.m[3][3],
m[3][0]*vm.m[0][0] + m[3][1]*vm.m[1][0] + m[3][2]*vm.m[2][0] + m[3][3]*vm.m[3][0],
m[3][0]*vm.m[0][1] + m[3][1]*vm.m[1][1] + m[3][2]*vm.m[2][1] + m[3][3]*vm.m[3][1],
m[3][0]*vm.m[0][2] + m[3][1]*vm.m[1][2] + m[3][2]*vm.m[2][2] + m[3][3]*vm.m[3][2],
m[3][0]*vm.m[0][3] + m[3][1]*vm.m[1][3] + m[3][2]*vm.m[2][3] + m[3][3]*vm.m[3][3]
);
}
#ifndef VECTOR_NO_SLOW_OPERATIONS
VMatrix VMatrix::operator*(const VMatrix &vm) const
{
VMatrix ret;
MatrixMul( vm, ret );
return ret;
}
#endif
bool VMatrix::InverseGeneral(VMatrix &vInverse) const
{
return MatrixInverseGeneral( *this, vInverse );
}
bool MatrixInverseGeneral(const VMatrix& src, VMatrix& dst)
{
int iRow, i, j, iTemp, iTest;
vec_t mul, fTest, fLargest;
vec_t mat[4][8];
int rowMap[4], iLargest;
vec_t *pOut, *pRow, *pScaleRow;
// How it's done.
// AX = I
// A = this
// X = the matrix we're looking for
// I = identity
// Setup AI
for(i=0; i < 4; i++)
{
const vec_t *pIn = src[i];
pOut = mat[i];
for(j=0; j < 4; j++)
{
pOut[j] = pIn[j];
}
pOut[4] = 0.0f;
pOut[5] = 0.0f;
pOut[6] = 0.0f;
pOut[7] = 0.0f;
pOut[i+4] = 1.0f;
rowMap[i] = i;
}
// Use row operations to get to reduced row-echelon form using these rules:
// 1. Multiply or divide a row by a nonzero number.
// 2. Add a multiple of one row to another.
// 3. Interchange two rows.
for(iRow=0; iRow < 4; iRow++)
{
// Find the row with the largest element in this column.
fLargest = 1e-6f;
iLargest = -1;
for(iTest=iRow; iTest < 4; iTest++)
{
fTest = (vec_t)FloatMakePositive(mat[rowMap[iTest]][iRow]);
if(fTest > fLargest)
{
iLargest = iTest;
fLargest = fTest;
}
}
// They're all too small.. sorry.
if(iLargest == -1)
{
return false;
}
// Swap the rows.
iTemp = rowMap[iLargest];
rowMap[iLargest] = rowMap[iRow];
rowMap[iRow] = iTemp;
pRow = mat[rowMap[iRow]];
// Divide this row by the element.
mul = 1.0f / pRow[iRow];
for(j=0; j < 8; j++)
pRow[j] *= mul;
pRow[iRow] = 1.0f; // Preserve accuracy...
// Eliminate this element from the other rows using operation 2.
for(i=0; i < 4; i++)
{
if(i == iRow)
continue;
pScaleRow = mat[rowMap[i]];
// Multiply this row by -(iRow*the element).
mul = -pScaleRow[iRow];
for(j=0; j < 8; j++)
{
pScaleRow[j] += pRow[j] * mul;
}
pScaleRow[iRow] = 0.0f; // Preserve accuracy...
}
}
// The inverse is on the right side of AX now (the identity is on the left).
for(i=0; i < 4; i++)
{
const vec_t *pIn = mat[rowMap[i]] + 4;
pOut = dst.m[i];
for(j=0; j < 4; j++)
{
pOut[j] = pIn[j];
}
}
return true;
}
//-----------------------------------------------------------------------------
// Does a fast inverse, assuming the matrix only contains translation and rotation.
//-----------------------------------------------------------------------------
void MatrixInverseTR( const VMatrix& src, VMatrix &dst )
{
Vector vTrans, vNewTrans;
// Transpose the upper 3x3.
dst.m[0][0] = src.m[0][0]; dst.m[0][1] = src.m[1][0]; dst.m[0][2] = src.m[2][0];
dst.m[1][0] = src.m[0][1]; dst.m[1][1] = src.m[1][1]; dst.m[1][2] = src.m[2][1];
dst.m[2][0] = src.m[0][2]; dst.m[2][1] = src.m[1][2]; dst.m[2][2] = src.m[2][2];
// Transform the translation.
vTrans.Init( -src.m[0][3], -src.m[1][3], -src.m[2][3] );
Vector3DMultiply( dst, vTrans, vNewTrans );
MatrixSetColumn( dst, 3, vNewTrans );
// Fill in the bottom row.
dst.m[3][0] = dst.m[3][1] = dst.m[3][2] = 0.0f;
dst.m[3][3] = 1.0f;
}
void VMatrix::InverseTR( VMatrix &ret ) const
{
MatrixInverseTR( *this, ret );
}
void MatrixInverseTranspose( const VMatrix& src, VMatrix& dst )
{
src.InverseGeneral( dst );
MatrixTranspose( dst, dst );
}
//-----------------------------------------------------------------------------
// Computes the inverse transpose
//-----------------------------------------------------------------------------
void MatrixInverseTranspose( const matrix3x4_t& src, matrix3x4_t& dst )
{
VMatrix tmp, out;
tmp.CopyFrom3x4( src );
::MatrixInverseTranspose( tmp, out );
out.Set3x4( dst );
}
#ifndef VECTOR_NO_SLOW_OPERATIONS
VMatrix VMatrix::InverseTR() const
{
VMatrix ret;
MatrixInverseTR( *this, ret );
return ret;
}
Vector VMatrix::GetScale() const
{
Vector vecs[3];
GetBasisVectors(vecs[0], vecs[1], vecs[2]);
return Vector(
vecs[0].Length(),
vecs[1].Length(),
vecs[2].Length()
);
}
VMatrix VMatrix::Scale(const Vector &vScale)
{
return VMatrix(
m[0][0]*vScale.x, m[0][1]*vScale.y, m[0][2]*vScale.z, m[0][3],
m[1][0]*vScale.x, m[1][1]*vScale.y, m[1][2]*vScale.z, m[1][3],
m[2][0]*vScale.x, m[2][1]*vScale.y, m[2][2]*vScale.z, m[2][3],
m[3][0]*vScale.x, m[3][1]*vScale.y, m[3][2]*vScale.z, 1.0f
);
}
VMatrix VMatrix::NormalizeBasisVectors() const
{
Vector vecs[3];
VMatrix mRet;
GetBasisVectors(vecs[0], vecs[1], vecs[2]);
VectorNormalize( vecs[0] );
VectorNormalize( vecs[1] );
VectorNormalize( vecs[2] );
mRet.SetBasisVectors(vecs[0], vecs[1], vecs[2]);
// Set everything but basis vectors to identity.
mRet.m[3][0] = mRet.m[3][1] = mRet.m[3][2] = 0.0f;
mRet.m[3][3] = 1.0f;
return mRet;
}
VMatrix VMatrix::Transpose() const
{
return VMatrix(
m[0][0], m[1][0], m[2][0], m[3][0],
m[0][1], m[1][1], m[2][1], m[3][1],
m[0][2], m[1][2], m[2][2], m[3][2],
m[0][3], m[1][3], m[2][3], m[3][3]);
}
// Transpose upper-left 3x3.
VMatrix VMatrix::Transpose3x3() const
{
return VMatrix(
m[0][0], m[1][0], m[2][0], m[0][3],
m[0][1], m[1][1], m[2][1], m[1][3],
m[0][2], m[1][2], m[2][2], m[2][3],
m[3][0], m[3][1], m[3][2], m[3][3]);
}
#endif // VECTOR_NO_SLOW_OPERATIONS
bool VMatrix::IsRotationMatrix() const
{
Vector &v1 = (Vector&)m[0][0];
Vector &v2 = (Vector&)m[1][0];
Vector &v3 = (Vector&)m[2][0];
return
FloatMakePositive( 1 - v1.Length() ) < 0.01f &&
FloatMakePositive( 1 - v2.Length() ) < 0.01f &&
FloatMakePositive( 1 - v3.Length() ) < 0.01f &&
FloatMakePositive( v1.Dot(v2) ) < 0.01f &&
FloatMakePositive( v1.Dot(v3) ) < 0.01f &&
FloatMakePositive( v2.Dot(v3) ) < 0.01f;
}
void VMatrix::SetupMatrixOrgAngles( const Vector &origin, const QAngle &vAngles )
{
float sr, sp, sy, cr, cp, cy;
SinCos( DEG2RAD( vAngles[YAW] ), &sy, &cy );
SinCos( DEG2RAD( vAngles[PITCH] ), &sp, &cp );
SinCos( DEG2RAD( vAngles[ROLL] ), &sr, &cr );
// matrix = (YAW * PITCH) * ROLL
m[0][0] = cp*cy;
m[1][0] = cp*sy;
m[2][0] = -sp;
m[0][1] = sr*sp*cy+cr*-sy;
m[1][1] = sr*sp*sy+cr*cy;
m[2][1] = sr*cp;
m[0][2] = (cr*sp*cy+-sr*-sy);
m[1][2] = (cr*sp*sy+-sr*cy);
m[2][2] = cr*cp;
m[0][3] = 0.f;
m[1][3] = 0.f;
m[2][3] = 0.f;
// Add translation
m[0][3] = origin.x;
m[1][3] = origin.y;
m[2][3] = origin.z;
m[3][0] = 0.0f;
m[3][1] = 0.0f;
m[3][2] = 0.0f;
m[3][3] = 1.0f;
}
//-----------------------------------------------------------------------------
// Sets matrix to identity
//-----------------------------------------------------------------------------
void MatrixSetIdentity( VMatrix &dst )
{
dst[0][0] = 1.0f; dst[0][1] = 0.0f; dst[0][2] = 0.0f; dst[0][3] = 0.0f;
dst[1][0] = 0.0f; dst[1][1] = 1.0f; dst[1][2] = 0.0f; dst[1][3] = 0.0f;
dst[2][0] = 0.0f; dst[2][1] = 0.0f; dst[2][2] = 1.0f; dst[2][3] = 0.0f;
dst[3][0] = 0.0f; dst[3][1] = 0.0f; dst[3][2] = 0.0f; dst[3][3] = 1.0f;
}
//-----------------------------------------------------------------------------
// Setup a matrix from euler angles.
//-----------------------------------------------------------------------------
void MatrixFromAngles( const QAngle& vAngles, VMatrix& dst )
{
dst.SetupMatrixOrgAngles( vec3_origin, vAngles );
}
//-----------------------------------------------------------------------------
// Creates euler angles from a matrix
//-----------------------------------------------------------------------------
void MatrixToAngles( const VMatrix& src, QAngle& vAngles )
{
float forward[3];
float left[3];
float up[3];
// Extract the basis vectors from the matrix. Since we only need the Z
// component of the up vector, we don't get X and Y.
forward[0] = src[0][0];
forward[1] = src[1][0];
forward[2] = src[2][0];
left[0] = src[0][1];
left[1] = src[1][1];
left[2] = src[2][1];
up[2] = src[2][2];
float xyDist = sqrtf( forward[0] * forward[0] + forward[1] * forward[1] );
// enough here to get angles?
if ( xyDist > 0.001f )
{
// (yaw) y = ATAN( forward.y, forward.x ); -- in our space, forward is the X axis
vAngles[1] = RAD2DEG( atan2f( forward[1], forward[0] ) );
// The engine does pitch inverted from this, but we always end up negating it in the DLL
// UNDONE: Fix the engine to make it consistent
// (pitch) x = ATAN( -forward.z, sqrt(forward.x*forward.x+forward.y*forward.y) );
vAngles[0] = RAD2DEG( atan2f( -forward[2], xyDist ) );
// (roll) z = ATAN( left.z, up.z );
vAngles[2] = RAD2DEG( atan2f( left[2], up[2] ) );
}
else // forward is mostly Z, gimbal lock-
{
// (yaw) y = ATAN( -left.x, left.y ); -- forward is mostly z, so use right for yaw
vAngles[1] = RAD2DEG( atan2f( -left[0], left[1] ) );
// The engine does pitch inverted from this, but we always end up negating it in the DLL
// UNDONE: Fix the engine to make it consistent
// (pitch) x = ATAN( -forward.z, sqrt(forward.x*forward.x+forward.y*forward.y) );
vAngles[0] = RAD2DEG( atan2f( -forward[2], xyDist ) );
// Assume no roll in this case as one degree of freedom has been lost (i.e. yaw == roll)
vAngles[2] = 0;
}
}
//-----------------------------------------------------------------------------
// Transpose
//-----------------------------------------------------------------------------
inline void Swap( float& a, float& b )
{
float tmp = a;
a = b;
b = tmp;
}
void MatrixTranspose( const VMatrix& src, VMatrix& dst )
{
if (&src == &dst)
{
Swap( dst[0][1], dst[1][0] );
Swap( dst[0][2], dst[2][0] );
Swap( dst[0][3], dst[3][0] );
Swap( dst[1][2], dst[2][1] );
Swap( dst[1][3], dst[3][1] );
Swap( dst[2][3], dst[3][2] );
}
else
{
dst[0][0] = src[0][0]; dst[0][1] = src[1][0]; dst[0][2] = src[2][0]; dst[0][3] = src[3][0];
dst[1][0] = src[0][1]; dst[1][1] = src[1][1]; dst[1][2] = src[2][1]; dst[1][3] = src[3][1];
dst[2][0] = src[0][2]; dst[2][1] = src[1][2]; dst[2][2] = src[2][2]; dst[2][3] = src[3][2];
dst[3][0] = src[0][3]; dst[3][1] = src[1][3]; dst[3][2] = src[2][3]; dst[3][3] = src[3][3];
}
}
//-----------------------------------------------------------------------------
// Matrix copy
//-----------------------------------------------------------------------------
void MatrixCopy( const VMatrix& src, VMatrix& dst )
{
if (&src != &dst)
{
memcpy( dst.m, src.m, 16 * sizeof(float) );
}
}
//-----------------------------------------------------------------------------
// Matrix multiply
//-----------------------------------------------------------------------------
typedef float VMatrixRaw_t[4];
void MatrixMultiply( const VMatrix& src1, const VMatrix& src2, VMatrix& dst )
{
// Make sure it works if src1 == dst or src2 == dst
VMatrix tmp1, tmp2;
const VMatrixRaw_t* s1 = (&src1 == &dst) ? tmp1.m : src1.m;
const VMatrixRaw_t* s2 = (&src2 == &dst) ? tmp2.m : src2.m;
if (&src1 == &dst)
{
MatrixCopy( src1, tmp1 );
}
if (&src2 == &dst)
{
MatrixCopy( src2, tmp2 );
}
dst[0][0] = s1[0][0] * s2[0][0] + s1[0][1] * s2[1][0] + s1[0][2] * s2[2][0] + s1[0][3] * s2[3][0];
dst[0][1] = s1[0][0] * s2[0][1] + s1[0][1] * s2[1][1] + s1[0][2] * s2[2][1] + s1[0][3] * s2[3][1];
dst[0][2] = s1[0][0] * s2[0][2] + s1[0][1] * s2[1][2] + s1[0][2] * s2[2][2] + s1[0][3] * s2[3][2];
dst[0][3] = s1[0][0] * s2[0][3] + s1[0][1] * s2[1][3] + s1[0][2] * s2[2][3] + s1[0][3] * s2[3][3];
dst[1][0] = s1[1][0] * s2[0][0] + s1[1][1] * s2[1][0] + s1[1][2] * s2[2][0] + s1[1][3] * s2[3][0];
dst[1][1] = s1[1][0] * s2[0][1] + s1[1][1] * s2[1][1] + s1[1][2] * s2[2][1] + s1[1][3] * s2[3][1];
dst[1][2] = s1[1][0] * s2[0][2] + s1[1][1] * s2[1][2] + s1[1][2] * s2[2][2] + s1[1][3] * s2[3][2];
dst[1][3] = s1[1][0] * s2[0][3] + s1[1][1] * s2[1][3] + s1[1][2] * s2[2][3] + s1[1][3] * s2[3][3];
dst[2][0] = s1[2][0] * s2[0][0] + s1[2][1] * s2[1][0] + s1[2][2] * s2[2][0] + s1[2][3] * s2[3][0];
dst[2][1] = s1[2][0] * s2[0][1] + s1[2][1] * s2[1][1] + s1[2][2] * s2[2][1] + s1[2][3] * s2[3][1];
dst[2][2] = s1[2][0] * s2[0][2] + s1[2][1] * s2[1][2] + s1[2][2] * s2[2][2] + s1[2][3] * s2[3][2];
dst[2][3] = s1[2][0] * s2[0][3] + s1[2][1] * s2[1][3] + s1[2][2] * s2[2][3] + s1[2][3] * s2[3][3];
dst[3][0] = s1[3][0] * s2[0][0] + s1[3][1] * s2[1][0] + s1[3][2] * s2[2][0] + s1[3][3] * s2[3][0];
dst[3][1] = s1[3][0] * s2[0][1] + s1[3][1] * s2[1][1] + s1[3][2] * s2[2][1] + s1[3][3] * s2[3][1];
dst[3][2] = s1[3][0] * s2[0][2] + s1[3][1] * s2[1][2] + s1[3][2] * s2[2][2] + s1[3][3] * s2[3][2];
dst[3][3] = s1[3][0] * s2[0][3] + s1[3][1] * s2[1][3] + s1[3][2] * s2[2][3] + s1[3][3] * s2[3][3];
}
//-----------------------------------------------------------------------------
// Matrix/vector multiply
//-----------------------------------------------------------------------------
void Vector4DMultiply( const VMatrix& src1, Vector4D const& src2, Vector4D& dst )
{
// Make sure it works if src2 == dst
Vector4D tmp;
Vector4D const&v = (&src2 == &dst) ? tmp : src2;
if (&src2 == &dst)
{
Vector4DCopy( src2, tmp );
}
dst[0] = src1[0][0] * v[0] + src1[0][1] * v[1] + src1[0][2] * v[2] + src1[0][3] * v[3];
dst[1] = src1[1][0] * v[0] + src1[1][1] * v[1] + src1[1][2] * v[2] + src1[1][3] * v[3];
dst[2] = src1[2][0] * v[0] + src1[2][1] * v[1] + src1[2][2] * v[2] + src1[2][3] * v[3];
dst[3] = src1[3][0] * v[0] + src1[3][1] * v[1] + src1[3][2] * v[2] + src1[3][3] * v[3];
}
//-----------------------------------------------------------------------------
// Matrix/vector multiply
//-----------------------------------------------------------------------------
void Vector4DMultiplyPosition( const VMatrix& src1, Vector const& src2, Vector4D& dst )
{
// Make sure it works if src2 == dst
Vector tmp;
Vector const&v = ( &src2 == &dst.AsVector3D() ) ? static_cast<const Vector>(tmp) : src2;
if (&src2 == &dst.AsVector3D())
{
VectorCopy( src2, tmp );
}
dst[0] = src1[0][0] * v[0] + src1[0][1] * v[1] + src1[0][2] * v[2] + src1[0][3];
dst[1] = src1[1][0] * v[0] + src1[1][1] * v[1] + src1[1][2] * v[2] + src1[1][3];
dst[2] = src1[2][0] * v[0] + src1[2][1] * v[1] + src1[2][2] * v[2] + src1[2][3];
dst[3] = src1[3][0] * v[0] + src1[3][1] * v[1] + src1[3][2] * v[2] + src1[3][3];
}
//-----------------------------------------------------------------------------
// Matrix/vector multiply
//-----------------------------------------------------------------------------
void Vector3DMultiply( const VMatrix &src1, const Vector &src2, Vector &dst )
{
// Make sure it works if src2 == dst
Vector tmp;
const Vector &v = (&src2 == &dst) ? static_cast<const Vector>(tmp) : src2;
if( &src2 == &dst )
{
VectorCopy( src2, tmp );
}
dst[0] = src1[0][0] * v[0] + src1[0][1] * v[1] + src1[0][2] * v[2];
dst[1] = src1[1][0] * v[0] + src1[1][1] * v[1] + src1[1][2] * v[2];
dst[2] = src1[2][0] * v[0] + src1[2][1] * v[1] + src1[2][2] * v[2];
}
//-----------------------------------------------------------------------------
// Vector3DMultiplyPositionProjective treats src2 as if it's a point
// and does the perspective divide at the end
//-----------------------------------------------------------------------------
void Vector3DMultiplyPositionProjective( const VMatrix& src1, const Vector &src2, Vector& dst )
{
// Make sure it works if src2 == dst
Vector tmp;
const Vector &v = (&src2 == &dst) ? static_cast<const Vector>(tmp): src2;
if( &src2 == &dst )
{
VectorCopy( src2, tmp );
}
float w = src1[3][0] * v[0] + src1[3][1] * v[1] + src1[3][2] * v[2] + src1[3][3];
if ( w != 0.0f )
{
w = 1.0f / w;
}
dst[0] = src1[0][0] * v[0] + src1[0][1] * v[1] + src1[0][2] * v[2] + src1[0][3];
dst[1] = src1[1][0] * v[0] + src1[1][1] * v[1] + src1[1][2] * v[2] + src1[1][3];
dst[2] = src1[2][0] * v[0] + src1[2][1] * v[1] + src1[2][2] * v[2] + src1[2][3];
dst *= w;
}
//-----------------------------------------------------------------------------
// Vector3DMultiplyProjective treats src2 as if it's a direction
// and does the perspective divide at the end
//-----------------------------------------------------------------------------
void Vector3DMultiplyProjective( const VMatrix& src1, const Vector &src2, Vector& dst )
{
// Make sure it works if src2 == dst
Vector tmp;
const Vector &v = (&src2 == &dst) ? static_cast<const Vector>(tmp) : src2;
if( &src2 == &dst )
{
VectorCopy( src2, tmp );
}
float w;
dst[0] = src1[0][0] * v[0] + src1[0][1] * v[1] + src1[0][2] * v[2];
dst[1] = src1[1][0] * v[0] + src1[1][1] * v[1] + src1[1][2] * v[2];
dst[2] = src1[2][0] * v[0] + src1[2][1] * v[1] + src1[2][2] * v[2];
w = src1[3][0] * v[0] + src1[3][1] * v[1] + src1[3][2] * v[2];
if (w != 0.0f)
{
dst /= w;
}
else
{
dst = vec3_origin;
}
}
//-----------------------------------------------------------------------------
// Multiplies the vector by the transpose of the matrix
//-----------------------------------------------------------------------------
void Vector4DMultiplyTranspose( const VMatrix& src1, Vector4D const& src2, Vector4D& dst )
{
// Make sure it works if src2 == dst
bool srcEqualsDst = (&src2 == &dst);
Vector4D tmp;
Vector4D const&v = srcEqualsDst ? tmp : src2;
if (srcEqualsDst)
{
Vector4DCopy( src2, tmp );
}
dst[0] = src1[0][0] * v[0] + src1[1][0] * v[1] + src1[2][0] * v[2] + src1[3][0] * v[3];
dst[1] = src1[0][1] * v[0] + src1[1][1] * v[1] + src1[2][1] * v[2] + src1[3][1] * v[3];
dst[2] = src1[0][2] * v[0] + src1[1][2] * v[1] + src1[2][2] * v[2] + src1[3][2] * v[3];
dst[3] = src1[0][3] * v[0] + src1[1][3] * v[1] + src1[2][3] * v[2] + src1[3][3] * v[3];
}
//-----------------------------------------------------------------------------
// Multiplies the vector by the transpose of the matrix
//-----------------------------------------------------------------------------
void Vector3DMultiplyTranspose( const VMatrix& src1, const Vector& src2, Vector& dst )
{
// Make sure it works if src2 == dst
bool srcEqualsDst = (&src2 == &dst);
Vector tmp;
const Vector&v = srcEqualsDst ? static_cast<const Vector>(tmp) : src2;
if (srcEqualsDst)
{
VectorCopy( src2, tmp );
}
dst[0] = src1[0][0] * v[0] + src1[1][0] * v[1] + src1[2][0] * v[2];
dst[1] = src1[0][1] * v[0] + src1[1][1] * v[1] + src1[2][1] * v[2];
dst[2] = src1[0][2] * v[0] + src1[1][2] * v[1] + src1[2][2] * v[2];
}
//-----------------------------------------------------------------------------
// Transform a plane
//-----------------------------------------------------------------------------
void MatrixTransformPlane( const VMatrix &src, const cplane_t &inPlane, cplane_t &outPlane )
{
// What we want to do is the following:
// 1) transform the normal into the new space.
// 2) Determine a point on the old plane given by plane dist * plane normal
// 3) Transform that point into the new space
// 4) Plane dist = DotProduct( new normal, new point )
// An optimized version, which works if the plane is orthogonal.
// 1) Transform the normal into the new space
// 2) Realize that transforming the old plane point into the new space
// is given by [ d * n'x + Tx, d * n'y + Ty, d * n'z + Tz ]
// where d = old plane dist, n' = transformed normal, Tn = translational component of transform
// 3) Compute the new plane dist using the dot product of the normal result of #2
// For a correct result, this should be an inverse-transpose matrix
// but that only matters if there are nonuniform scale or skew factors in this matrix.
Vector vTrans;
Vector3DMultiply( src, inPlane.normal, outPlane.normal );
outPlane.dist = inPlane.dist * DotProduct( outPlane.normal, outPlane.normal );
outPlane.dist += DotProduct( outPlane.normal, src.GetTranslation(vTrans) );
}
#ifndef VECTOR_NO_SLOW_OPERATIONS
VPlane VMatrix::operator*(const VPlane &thePlane) const
{
VPlane ret;
TransformPlane( thePlane, ret );
return ret;
}
#endif
//-----------------------------------------------------------------------------
// Builds a rotation matrix that rotates one direction vector into another
//-----------------------------------------------------------------------------
void MatrixBuildTranslation( VMatrix& dst, float x, float y, float z )
{
MatrixSetIdentity( dst );
dst[0][3] = x;
dst[1][3] = y;
dst[2][3] = z;
}
void MatrixBuildTranslation( VMatrix& dst, const Vector &translation )
{
MatrixSetIdentity( dst );
dst[0][3] = translation[0];
dst[1][3] = translation[1];
dst[2][3] = translation[2];
}
//-----------------------------------------------------------------------------
// Purpose: Builds the matrix for a counterclockwise rotation about an arbitrary axis.
//
// | ax2 + (1 - ax2)cosQ axay(1 - cosQ) - azsinQ azax(1 - cosQ) + aysinQ |
// Ra(Q) = | axay(1 - cosQ) + azsinQ ay2 + (1 - ay2)cosQ ayaz(1 - cosQ) - axsinQ |
// | azax(1 - cosQ) - aysinQ ayaz(1 - cosQ) + axsinQ az2 + (1 - az2)cosQ |
//
// Input : mat -
// vAxisOrRot -
// angle -
//-----------------------------------------------------------------------------
void MatrixBuildRotationAboutAxis( VMatrix &dst, const Vector &vAxisOfRot, float angleDegrees )
{
MatrixBuildRotationAboutAxis( vAxisOfRot, angleDegrees, dst.As3x4() );
dst[3][0] = 0;
dst[3][1] = 0;
dst[3][2] = 0;
dst[3][3] = 1;
}
//-----------------------------------------------------------------------------
// Builds a rotation matrix that rotates one direction vector into another
//-----------------------------------------------------------------------------
void MatrixBuildRotation( VMatrix &dst, const Vector& initialDirection, const Vector& finalDirection )
{
float angle = DotProduct( initialDirection, finalDirection );
Assert( IsFinite(angle) );