forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathlib_base.cpp
5834 lines (4913 loc) · 157 KB
/
mathlib_base.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 � 1996-2005, Valve Corporation, All rights reserved. ======//
//
// Purpose: Math primitives.
//
//===========================================================================//
/// FIXME: As soon as all references to mathlib.c are gone, include it in here
#include <math.h>
#include <float.h> // needed for flt_epsilon
#include "tier0/basetypes.h"
//#include <memory.h>
#include "tier0/dbg.h"
#include "tier0/vprof.h"
//#define _VPROF_MATHLIB
#if !defined(__SPU__)
#pragma warning(disable:4244) // "conversion from 'const int' to 'float', possible loss of data"
#pragma warning(disable:4730) // "mixing _m64 and floating point expressions may result in incorrect code"
#endif
#include "mathlib/mathlib.h"
#include "mathlib/vector.h"
#include "mathlib/vplane.h"
#if !defined(__SPU__)
#include "mathlib/vmatrix.h"
#endif
#if !defined( _X360 )
#include "sse.h"
#endif
#include "mathlib/ssemath.h"
#include "mathlib/ssequaternion.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
bool s_bMathlibInitialized = false;
#ifdef PARANOID
// User must provide an implementation of Sys_Error()
void Sys_Error (char *error, ...);
#endif
const Vector vec3_origin(0,0,0);
const QAngle vec3_angle(0,0,0);
const Quaternion quat_identity(0,0,0,1);
const Vector vec3_invalid( FLT_MAX, FLT_MAX, FLT_MAX );
const int nanmask = 255<<23;
const matrix3x4a_t g_MatrixIdentity(
1,0,0,0,
0,1,0,0,
0,0,1,0
);
#if !defined(__SPU__)
//-----------------------------------------------------------------------------
// Standard C implementations of optimized routines:
//-----------------------------------------------------------------------------
float _sqrtf(float _X)
{
Assert( s_bMathlibInitialized );
return sqrtf(_X);
}
float _rsqrtf(float x)
{
Assert( s_bMathlibInitialized );
return 1.f / _sqrtf( x );
}
#ifndef PLATFORM_PPC
float VectorNormalize (Vector& vec)
{
#ifdef _VPROF_MATHLIB
VPROF_BUDGET( "_VectorNormalize", "Mathlib" );
#endif
Assert( s_bMathlibInitialized );
float radius = sqrtf(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
// FLT_EPSILON is added to the radius to eliminate the possibility of divide by zero.
float iradius = 1.f / ( radius + FLT_EPSILON );
vec.x *= iradius;
vec.y *= iradius;
vec.z *= iradius;
return radius;
}
#endif
// TODO: Add fast C VectorNormalizeFast.
// Perhaps use approximate rsqrt trick, if the accuracy isn't too bad.
void FASTCALL _VectorNormalizeFast (Vector& vec)
{
Assert( s_bMathlibInitialized );
// FLT_EPSILON is added to the radius to eliminate the possibility of divide by zero.
float iradius = 1.f / ( sqrtf(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z) + FLT_EPSILON );
vec.x *= iradius;
vec.y *= iradius;
vec.z *= iradius;
}
float _InvRSquared(const float* v)
{
Assert( s_bMathlibInitialized );
float r2 = DotProduct(v, v);
return r2 < 1.f ? 1.f : 1/r2;
}
#if !defined(__SPU__)
//-----------------------------------------------------------------------------
// Function pointers selecting the appropriate implementation
//-----------------------------------------------------------------------------
void (FASTCALL *pfVectorNormalizeFast)(Vector& v) = _VectorNormalizeFast;
float SinCosTable[SIN_TABLE_SIZE];
void InitSinCosTable()
{
for( int i = 0; i < SIN_TABLE_SIZE; i++ )
{
SinCosTable[i] = sin(i * 2.0 * M_PI / SIN_TABLE_SIZE);
}
}
#endif // !defined(__SPU__)
qboolean VectorsEqual( const float *v1, const float *v2 )
{
Assert( s_bMathlibInitialized );
return ( ( v1[0] == v2[0] ) &&
( v1[1] == v2[1] ) &&
( v1[2] == v2[2] ) );
}
#endif // #if !defined(__SPU__)
//-----------------------------------------------------------------------------
// Purpose: Generates Euler angles given a left-handed orientation matrix. The
// columns of the matrix contain the forward, left, and up vectors.
// Input : matrix - Left-handed orientation matrix.
// angles[PITCH, YAW, ROLL]. Receives right-handed counterclockwise
// rotations in degrees around Y, Z, and X respectively.
//-----------------------------------------------------------------------------
void MatrixAngles( const matrix3x4_t& matrix, RadianEuler &angles, Vector &position )
{
MatrixGetColumn( matrix, 3, position );
MatrixAngles( matrix, angles );
}
void MatrixAngles( const matrix3x4_t &matrix, Quaternion &q, Vector &pos )
{
#ifdef _VPROF_MATHLIB
VPROF_BUDGET( "MatrixQuaternion", "Mathlib" );
#endif
float trace;
trace = matrix[0][0] + matrix[1][1] + matrix[2][2] + 1.0f;
if( trace > 1.0f + FLT_EPSILON )
{
// VPROF_INCREMENT_COUNTER("MatrixQuaternion A",1);
q.x = ( matrix[2][1] - matrix[1][2] );
q.y = ( matrix[0][2] - matrix[2][0] );
q.z = ( matrix[1][0] - matrix[0][1] );
q.w = trace;
}
else if ( matrix[0][0] > matrix[1][1] && matrix[0][0] > matrix[2][2] )
{
// VPROF_INCREMENT_COUNTER("MatrixQuaternion B",1);
trace = 1.0f + matrix[0][0] - matrix[1][1] - matrix[2][2];
q.x = trace;
q.y = (matrix[1][0] + matrix[0][1] );
q.z = (matrix[0][2] + matrix[2][0] );
q.w = (matrix[2][1] - matrix[1][2] );
}
else if (matrix[1][1] > matrix[2][2])
{
// VPROF_INCREMENT_COUNTER("MatrixQuaternion C",1);
trace = 1.0f + matrix[1][1] - matrix[0][0] - matrix[2][2];
q.x = (matrix[0][1] + matrix[1][0] );
q.y = trace;
q.z = (matrix[2][1] + matrix[1][2] );
q.w = (matrix[0][2] - matrix[2][0] );
}
else
{
// VPROF_INCREMENT_COUNTER("MatrixQuaternion D",1);
trace = 1.0f + matrix[2][2] - matrix[0][0] - matrix[1][1];
q.x = (matrix[0][2] + matrix[2][0] );
q.y = (matrix[2][1] + matrix[1][2] );
q.z = trace;
q.w = (matrix[1][0] - matrix[0][1] );
}
QuaternionNormalize( q );
#if 0
// check against the angle version
RadianEuler ang;
MatrixAngles( matrix, ang );
Quaternion test;
AngleQuaternion( ang, test );
float d = QuaternionDotProduct( q, test );
Assert( fabs(d) > 0.99 && fabs(d) < 1.01 );
#endif
MatrixGetColumn( matrix, 3, pos );
}
void MatrixAngles( const matrix3x4_t& matrix, float *angles )
{
#ifdef _VPROF_MATHLIB
VPROF_BUDGET( "MatrixAngles", "Mathlib" );
#endif
Assert( s_bMathlibInitialized );
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] = matrix[0][0];
forward[1] = matrix[1][0];
forward[2] = matrix[2][0];
left[0] = matrix[0][1];
left[1] = matrix[1][1];
left[2] = matrix[2][1];
up[2] = matrix[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
angles[1] = RAD2DEG( atan2f( forward[1], forward[0] ) );
// (pitch) x = ATAN( -forward.z, sqrt(forward.x*forward.x+forward.y*forward.y) );
angles[0] = RAD2DEG( atan2f( -forward[2], xyDist ) );
// (roll) z = ATAN( left.z, up.z );
angles[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
angles[1] = RAD2DEG( atan2f( -left[0], left[1] ) );
// (pitch) x = ATAN( -forward.z, sqrt(forward.x*forward.x+forward.y*forward.y) );
angles[0] = RAD2DEG( atan2f( -forward[2], xyDist ) );
// Assume no roll in this case as one degree of freedom has been lost (i.e. yaw == roll)
angles[2] = 0;
}
}
Vector MatrixNormalize( const matrix3x4_t &in, matrix3x4_t &out )
{
Vector vScale;
vScale.x = sqrt( in[ 0 ][ 0 ] * in[ 0 ][ 0 ] + in[ 1 ][ 0 ] * in[ 1 ][ 0 ] + in[ 2 ][ 0 ] * in[ 2 ][ 0 ] );
vScale.y = sqrt( in[ 0 ][ 1 ] * in[ 0 ][ 1 ] + in[ 1 ][ 1 ] * in[ 1 ][ 1 ] + in[ 2 ][ 1 ] * in[ 2 ][ 1 ] );
vScale.z = sqrt( in[ 0 ][ 2 ] * in[ 0 ][ 2 ] + in[ 1 ][ 2 ] * in[ 1 ][ 2 ] + in[ 2 ][ 2 ] * in[ 2 ][ 2 ] );
matrix3x4_t norm;
float flInvScaleX = 1.0f / vScale.x;
float flInvScaleY = 1.0f / vScale.y;
float flInvScaleZ = 1.0f / vScale.z;
out[ 0 ][ 0 ] = in[ 0 ][ 0 ] * flInvScaleX; out[ 1 ][ 0 ] = in[ 1 ][ 0 ] * flInvScaleX; out[ 2 ][ 0 ] = in[ 2 ][ 0 ] * flInvScaleX;
out[ 0 ][ 1 ] = in[ 0 ][ 1 ] * flInvScaleY; out[ 1 ][ 1 ] = in[ 1 ][ 1 ] * flInvScaleY; out[ 2 ][ 1 ] = in[ 2 ][ 1 ] * flInvScaleY;
out[ 0 ][ 2 ] = in[ 0 ][ 2 ] * flInvScaleZ; out[ 1 ][ 2 ] = in[ 1 ][ 2 ] * flInvScaleZ; out[ 2 ][ 2 ] = in[ 2 ][ 2 ] * flInvScaleZ;
out[ 0 ][ 3 ] = in[ 0 ][ 3 ]; out[ 1 ][ 3 ] = in[ 1 ][ 3 ]; out[ 2 ][ 3 ] = in[ 2 ][ 3 ];
return vScale;
}
#if !defined(__SPU__)
// transform in1 by the matrix in2
void VectorTransform (const float * RESTRICT in1, const matrix3x4_t& in2, float * RESTRICT out)
{
Assert( s_bMathlibInitialized );
float x = DotProduct(in1, in2[0]) + in2[0][3];
float y = DotProduct(in1, in2[1]) + in2[1][3];
float z = DotProduct(in1, in2[2]) + in2[2][3];
out[ 0 ] = x;
out[ 1 ] = y;
out[ 2 ] = z;
}
// assuming the matrix is orthonormal, transform in1 by the transpose (also the inverse in this case) of in2.
void VectorITransform (const float *in1, const matrix3x4_t& in2, float *out)
{
Assert( s_bMathlibInitialized );
float in1t[3];
in1t[0] = in1[0] - in2[0][3];
in1t[1] = in1[1] - in2[1][3];
in1t[2] = in1[2] - in2[2][3];
float x = in1t[0] * in2[0][0] + in1t[1] * in2[1][0] + in1t[2] * in2[2][0];
float y = in1t[0] * in2[0][1] + in1t[1] * in2[1][1] + in1t[2] * in2[2][1];
float z = in1t[0] * in2[0][2] + in1t[1] * in2[1][2] + in1t[2] * in2[2][2];
out[ 0 ] = x;
out[ 1 ] = y;
out[ 2 ] = z;
}
#endif // #if !defined(__SPU__)
// assume in2 is a rotation and rotate the input vector
void VectorRotate( const float * RESTRICT in1, const matrix3x4_t& in2, float * RESTRICT out )
{
Assert( s_bMathlibInitialized );
float x = DotProduct( in1, in2[ 0 ] );
float y = DotProduct( in1, in2[ 1 ] );
float z = DotProduct( in1, in2[ 2 ] );
out[ 0 ] = x;
out[ 1 ] = y;
out[ 2 ] = z;
}
#if !defined(__SPU__)
// assume in2 is a rotation and rotate the input vector
void VectorRotate( const Vector &in1, const QAngle &in2, Vector &out )
{
matrix3x4_t matRotate;
AngleMatrix( in2, matRotate );
VectorRotate( in1, matRotate, out );
}
// assume in2 is a rotation and rotate the input vector
void VectorRotate( const Vector &in1, const Quaternion &in2, Vector &out )
{
#if WE_WANT_OUR_CODE_TO_BE_POINTLESSLY_SLOW
matrix3x4_t matRotate;
QuaternionMatrix( in2, matRotate );
VectorRotate( in1, matRotate, out );
#else
// rotation is q * v * q^-1
Quaternion conjugate = in2.Conjugate();
// do the rotation as unrolled flop code ( QuaternionMult is a function call, which murders instruction scheduling )
// first q*v
Quaternion temp;
temp.x = in2.y * in1.z - in2.z * in1.y + in2.w * in1.x;
temp.y = -in2.x * in1.z + in2.z * in1.x + in2.w * in1.y;
temp.z = in2.x * in1.y - in2.y * in1.x + in2.w * in1.z;
temp.w = -in2.x * in1.x - in2.y * in1.y - in2.z * in1.z;
// now (qv)(q*)
out.x = temp.x * conjugate.w + temp.y * conjugate.z - temp.z * conjugate.y + temp.w * conjugate.x;
out.y = -temp.x * conjugate.z + temp.y * conjugate.w + temp.z * conjugate.x + temp.w * conjugate.y;
out.z = temp.x * conjugate.y - temp.y * conjugate.x + temp.z * conjugate.w + temp.w * conjugate.z;
Assert( fabs(-temp.x * conjugate.x - temp.y * conjugate.y - temp.z * conjugate.z + temp.w * conjugate.w) < 0.0001 );
#endif
}
// rotate by the inverse of the matrix
void VectorIRotate( const float * RESTRICT in1, const matrix3x4_t& in2, float * RESTRICT out )
{
Assert( s_bMathlibInitialized );
Assert( in1 != out );
out[0] = in1[0]*in2[0][0] + in1[1]*in2[1][0] + in1[2]*in2[2][0];
out[1] = in1[0]*in2[0][1] + in1[1]*in2[1][1] + in1[2]*in2[2][1];
out[2] = in1[0]*in2[0][2] + in1[1]*in2[1][2] + in1[2]*in2[2][2];
}
#ifndef VECTOR_NO_SLOW_OPERATIONS
// transform a set of angles in the output space of parentMatrix to the input space
QAngle TransformAnglesToLocalSpace( const QAngle &angles, const matrix3x4_t &parentMatrix )
{
matrix3x4_t angToWorld, worldToParent, localMatrix;
MatrixInvert( parentMatrix, worldToParent );
AngleMatrix( angles, angToWorld );
ConcatTransforms( worldToParent, angToWorld, localMatrix );
QAngle out;
MatrixAngles( localMatrix, out );
return out;
}
// transform a set of angles in the input space of parentMatrix to the output space
QAngle TransformAnglesToWorldSpace( const QAngle &angles, const matrix3x4_t &parentMatrix )
{
matrix3x4_t angToParent, angToWorld;
AngleMatrix( angles, angToParent );
ConcatTransforms( parentMatrix, angToParent, angToWorld );
QAngle out;
MatrixAngles( angToWorld, out );
return out;
}
#endif // VECTOR_NO_SLOW_OPERATIONS
void MatrixInitialize( matrix3x4_t &mat, const Vector &vecOrigin, const Vector &vecXAxis, const Vector &vecYAxis, const Vector &vecZAxis )
{
MatrixSetColumn( vecXAxis, 0, mat );
MatrixSetColumn( vecYAxis, 1, mat );
MatrixSetColumn( vecZAxis, 2, mat );
MatrixSetColumn( vecOrigin, 3, mat );
}
void MatrixCopy( const matrix3x4_t& in, matrix3x4_t& out )
{
Assert( s_bMathlibInitialized );
memcpy( out.Base(), in.Base(), sizeof( float ) * 3 * 4 );
}
//-----------------------------------------------------------------------------
// Matrix equality test
//-----------------------------------------------------------------------------
bool MatricesAreEqual( const matrix3x4_t &src1, const matrix3x4_t &src2, float flTolerance )
{
for ( int i = 0; i < 3; ++i )
{
for ( int j = 0; j < 4; ++j )
{
if ( fabs( src1[i][j] - src2[i][j] ) > flTolerance )
return false;
}
}
return true;
}
#endif // #if !defined(__SPU__)
// NOTE: This is just the transpose not a general inverse
void MatrixInvert( const matrix3x4_t& in, matrix3x4_t& out )
{
Assert( s_bMathlibInitialized );
if ( &in == &out )
{
V_swap(out[0][1],out[1][0]);
V_swap(out[0][2],out[2][0]);
V_swap(out[1][2],out[2][1]);
}
else
{
// transpose the matrix
out[0][0] = in[0][0];
out[0][1] = in[1][0];
out[0][2] = in[2][0];
out[1][0] = in[0][1];
out[1][1] = in[1][1];
out[1][2] = in[2][1];
out[2][0] = in[0][2];
out[2][1] = in[1][2];
out[2][2] = in[2][2];
}
// now fix up the translation to be in the other space
float tmp[3];
tmp[0] = in[0][3];
tmp[1] = in[1][3];
tmp[2] = in[2][3];
out[0][3] = -DotProduct( tmp, out[0] );
out[1][3] = -DotProduct( tmp, out[1] );
out[2][3] = -DotProduct( tmp, out[2] );
}
void MatrixGetColumn( const matrix3x4_t& in, int column, Vector &out )
{
out.x = in[0][column];
out.y = in[1][column];
out.z = in[2][column];
}
void MatrixSetColumn( const Vector &in, int column, matrix3x4_t& out )
{
out[0][column] = in.x;
out[1][column] = in.y;
out[2][column] = in.z;
}
#if !defined(__SPU__)
int VectorCompare (const float *v1, const float *v2)
{
Assert( s_bMathlibInitialized );
int i;
for (i=0 ; i<3 ; i++)
if (v1[i] != v2[i])
return 0;
return 1;
}
void CrossProduct (const float* v1, const float* v2, float* cross)
{
Assert( s_bMathlibInitialized );
Assert( v1 != cross );
Assert( v2 != cross );
cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
}
size_t Q_log2( unsigned int val )
{
#ifdef _X360 // use hardware
// both zero and one return zero (per old implementation)
return ( val == 0 ) ? 0 : 31 - _CountLeadingZeros( val );
#else // use N. Compoop's algorithm ( inherited from days of yore )
int answer=0;
while (val>>=1)
answer++;
return answer;
#endif
}
// Matrix is right-handed x=forward, y=left, z=up. We a left-handed convention for vectors in the game code (forward, right, up)
void MatrixVectorsFLU( const matrix3x4_t &matrix, Vector* pForward, Vector *pLeft, Vector *pUp )
{
MatrixGetColumn( matrix, FORWARD_AXIS, *pForward );
MatrixGetColumn( matrix, LEFT_AXIS, *pLeft );
MatrixGetColumn( matrix, UP_AXIS, *pUp );
}
// Matrix is right-handed x=forward, y=left, z=up. We a left-handed convention for vectors in the game code (forward, right, up)
void MatrixVectors( const matrix3x4_t &matrix, Vector* pForward, Vector *pRight, Vector *pUp )
{
MatrixGetColumn( matrix, 0, *pForward );
MatrixGetColumn( matrix, 1, *pRight );
MatrixGetColumn( matrix, 2, *pUp );
*pRight *= -1.0f;
}
void VectorVectors( const Vector &forward, Vector &right, Vector &up )
{
Assert( s_bMathlibInitialized );
Vector tmp;
if ( fabs( forward[0] ) < 1e-6 && fabs( forward[1] ) < 1e-6 )
{
// pitch 90 degrees up/down from identity
right[0] = 0;
right[1] = -1;
right[2] = 0;
up[0] = -forward[2];
up[1] = 0;
up[2] = 0;
}
else
{
tmp[0] = 0; tmp[1] = 0; tmp[2] = 1.0;
CrossProduct( forward, tmp, right );
VectorNormalize( right );
CrossProduct( right, forward, up );
VectorNormalize( up );
}
}
void VectorMatrix( const Vector &forward, matrix3x4_t& matrix)
{
Assert( s_bMathlibInitialized );
Vector right, up;
VectorVectors(forward, right, up);
MatrixSetColumn( forward, 0, matrix );
MatrixSetColumn( -right, 1, matrix );
MatrixSetColumn( up, 2, matrix );
}
void VectorPerpendicularToVector( Vector const &in, Vector *pvecOut )
{
float flY = in.y * in.y;
pvecOut->x = RemapVal( flY, 0, 1, in.z, 1 );
pvecOut->y = 0;
pvecOut->z = -in.x;
pvecOut->NormalizeInPlace();
float flDot = DotProduct( *pvecOut, in );
*pvecOut -= flDot * in;
pvecOut->NormalizeInPlace();
}
//-----------------------------------------------------------------------------
// Euler QAngle -> Basis Vectors. Each vector is optional
//-----------------------------------------------------------------------------
void AngleVectorsFLU( const QAngle &angles, Vector *pForward, Vector *pLeft, Vector *pUp )
{
Assert( s_bMathlibInitialized );
float sr, sp, sy, cr, cp, cy;
#ifdef _X360
fltx4 radians, scale, sine, cosine;
radians = LoadUnaligned3SIMD( angles.Base() );
scale = ReplicateX4( M_PI_F / 180.f );
radians = MulSIMD( radians, scale );
SinCos3SIMD( sine, cosine, radians );
sp = SubFloat( sine, 0 ); sy = SubFloat( sine, 1 ); sr = SubFloat( sine, 2 );
cp = SubFloat( cosine, 0 ); cy = SubFloat( cosine, 1 ); cr = SubFloat( cosine, 2 );
#else
SinCos( DEG2RAD( angles[YAW] ), &sy, &cy );
SinCos( DEG2RAD( angles[PITCH] ), &sp, &cp );
SinCos( DEG2RAD( angles[ROLL] ), &sr, &cr );
#endif
if ( pForward )
{
(*pForward)[FORWARD_AXIS] = cp*cy;
(*pForward)[LEFT_AXIS] = cp*sy;
(*pForward)[UP_AXIS] = -sp;
}
if ( pLeft )
{
(*pLeft)[FORWARD_AXIS] = (sr*sp*cy+cr*-sy);
(*pLeft)[LEFT_AXIS] = (sr*sp*sy+cr*cy);
(*pLeft)[UP_AXIS] = sr*cp;
}
if ( pUp )
{
(*pUp)[FORWARD_AXIS] = (cr*sp*cy+-sr*-sy);
(*pUp)[LEFT_AXIS] = (cr*sp*sy+-sr*cy);
(*pUp)[UP_AXIS] = cr*cp;
}
}
void VectorAngles( const float *forward, float *angles )
{
Assert( s_bMathlibInitialized );
float tmp, yaw, pitch;
if (forward[1] == 0 && forward[0] == 0)
{
yaw = 0;
if (forward[2] > 0)
pitch = 270;
else
pitch = 90;
}
else
{
yaw = (atan2(forward[1], forward[0]) * 180 / M_PI);
if (yaw < 0)
yaw += 360;
tmp = sqrt (forward[0]*forward[0] + forward[1]*forward[1]);
pitch = (atan2(-forward[2], tmp) * 180 / M_PI);
if (pitch < 0)
pitch += 360;
}
angles[0] = pitch;
angles[1] = yaw;
angles[2] = 0;
}
/*
================
R_ConcatRotations
================
*/
void ConcatRotations (const float in1[3][3], const float in2[3][3], float out[3][3])
{
Assert( s_bMathlibInitialized );
Assert( in1 != out );
Assert( in2 != out );
out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
in1[0][2] * in2[2][0];
out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
in1[0][2] * in2[2][1];
out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
in1[0][2] * in2[2][2];
out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
in1[1][2] * in2[2][0];
out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
in1[1][2] * in2[2][1];
out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
in1[1][2] * in2[2][2];
out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
in1[2][2] * in2[2][0];
out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
in1[2][2] * in2[2][1];
out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
in1[2][2] * in2[2][2];
}
#endif // #if !defined(__SPU__)
void ConcatTransforms_Aligned( const matrix3x4a_t &m0, const matrix3x4a_t &m1, matrix3x4a_t &out )
{
AssertAligned( &m0 );
AssertAligned( &m1 );
AssertAligned( &out );
fltx4 lastMask = *(fltx4 *)(&g_SIMD_ComponentMask[3]);
fltx4 rowA0 = LoadAlignedSIMD( m0.m_flMatVal[0] );
fltx4 rowA1 = LoadAlignedSIMD( m0.m_flMatVal[1] );
fltx4 rowA2 = LoadAlignedSIMD( m0.m_flMatVal[2] );
fltx4 rowB0 = LoadAlignedSIMD( m1.m_flMatVal[0] );
fltx4 rowB1 = LoadAlignedSIMD( m1.m_flMatVal[1] );
fltx4 rowB2 = LoadAlignedSIMD( m1.m_flMatVal[2] );
// now we have the rows of m0 and the columns of m1
// first output row
fltx4 A0 = SplatXSIMD(rowA0);
fltx4 A1 = SplatYSIMD(rowA0);
fltx4 A2 = SplatZSIMD(rowA0);
fltx4 mul00 = MulSIMD( A0, rowB0 );
fltx4 mul01 = MulSIMD( A1, rowB1 );
fltx4 mul02 = MulSIMD( A2, rowB2 );
fltx4 out0 = AddSIMD( mul00, AddSIMD(mul01,mul02) );
// second output row
A0 = SplatXSIMD(rowA1);
A1 = SplatYSIMD(rowA1);
A2 = SplatZSIMD(rowA1);
fltx4 mul10 = MulSIMD( A0, rowB0 );
fltx4 mul11 = MulSIMD( A1, rowB1 );
fltx4 mul12 = MulSIMD( A2, rowB2 );
fltx4 out1 = AddSIMD( mul10, AddSIMD(mul11,mul12) );
// third output row
A0 = SplatXSIMD(rowA2);
A1 = SplatYSIMD(rowA2);
A2 = SplatZSIMD(rowA2);
fltx4 mul20 = MulSIMD( A0, rowB0 );
fltx4 mul21 = MulSIMD( A1, rowB1 );
fltx4 mul22 = MulSIMD( A2, rowB2 );
fltx4 out2 = AddSIMD( mul20, AddSIMD(mul21,mul22) );
// add in translation vector
A0 = AndSIMD(rowA0,lastMask);
A1 = AndSIMD(rowA1,lastMask);
A2 = AndSIMD(rowA2,lastMask);
out0 = AddSIMD(out0, A0);
out1 = AddSIMD(out1, A1);
out2 = AddSIMD(out2, A2);
StoreAlignedSIMD( out.m_flMatVal[0], out0 );
StoreAlignedSIMD( out.m_flMatVal[1], out1 );
StoreAlignedSIMD( out.m_flMatVal[2], out2 );
}
/*
================
R_ConcatTransforms
================
*/
void ConcatTransforms (const matrix3x4_t& in1, const matrix3x4_t& in2, matrix3x4_t& out)
{
#if 0
// test for ones that'll be 2x faster
if ( (((size_t)&in1) % 16) == 0 && (((size_t)&in2) % 16) == 0 && (((size_t)&out) % 16) == 0 )
{
ConcatTransforms_Aligned( in1, in2, out );
return;
}
#endif
fltx4 lastMask = *(fltx4 *)(&g_SIMD_ComponentMask[3]);
fltx4 rowA0 = LoadUnalignedSIMD( in1.m_flMatVal[0] );
fltx4 rowA1 = LoadUnalignedSIMD( in1.m_flMatVal[1] );
fltx4 rowA2 = LoadUnalignedSIMD( in1.m_flMatVal[2] );
fltx4 rowB0 = LoadUnalignedSIMD( in2.m_flMatVal[0] );
fltx4 rowB1 = LoadUnalignedSIMD( in2.m_flMatVal[1] );
fltx4 rowB2 = LoadUnalignedSIMD( in2.m_flMatVal[2] );
// now we have the rows of m0 and the columns of m1
// first output row
fltx4 A0 = SplatXSIMD(rowA0);
fltx4 A1 = SplatYSIMD(rowA0);
fltx4 A2 = SplatZSIMD(rowA0);
fltx4 mul00 = MulSIMD( A0, rowB0 );
fltx4 mul01 = MulSIMD( A1, rowB1 );
fltx4 mul02 = MulSIMD( A2, rowB2 );
fltx4 out0 = AddSIMD( mul00, AddSIMD(mul01,mul02) );
// second output row
A0 = SplatXSIMD(rowA1);
A1 = SplatYSIMD(rowA1);
A2 = SplatZSIMD(rowA1);
fltx4 mul10 = MulSIMD( A0, rowB0 );
fltx4 mul11 = MulSIMD( A1, rowB1 );
fltx4 mul12 = MulSIMD( A2, rowB2 );
fltx4 out1 = AddSIMD( mul10, AddSIMD(mul11,mul12) );
// third output row
A0 = SplatXSIMD(rowA2);
A1 = SplatYSIMD(rowA2);
A2 = SplatZSIMD(rowA2);
fltx4 mul20 = MulSIMD( A0, rowB0 );
fltx4 mul21 = MulSIMD( A1, rowB1 );
fltx4 mul22 = MulSIMD( A2, rowB2 );
fltx4 out2 = AddSIMD( mul20, AddSIMD(mul21,mul22) );
// add in translation vector
A0 = AndSIMD(rowA0,lastMask);
A1 = AndSIMD(rowA1,lastMask);
A2 = AndSIMD(rowA2,lastMask);
out0 = AddSIMD(out0, A0);
out1 = AddSIMD(out1, A1);
out2 = AddSIMD(out2, A2);
// write to output
StoreUnalignedSIMD( out.m_flMatVal[0], out0 );
StoreUnalignedSIMD( out.m_flMatVal[1], out1 );
StoreUnalignedSIMD( out.m_flMatVal[2], out2 );
}
/*
===================
FloorDivMod
Returns mathematically correct (floor-based) quotient and remainder for
numer and denom, both of which should contain no fractional part. The
quotient must fit in 32 bits.
====================
*/
#if !defined(__SPU__)
void FloorDivMod (double numer, double denom, int *quotient,
int *rem)
{
Assert( s_bMathlibInitialized );
int q, r;
double x;
#ifdef PARANOID
if (denom <= 0.0)
Sys_Error ("FloorDivMod: bad denominator %d\n", denom);
// if ((floor(numer) != numer) || (floor(denom) != denom))
// Sys_Error ("FloorDivMod: non-integer numer or denom %f %f\n",
// numer, denom);
#endif
if (numer >= 0.0)
{
x = floor(numer / denom);
q = (int)x;
r = Floor2Int(numer - (x * denom));
}
else
{
//
// perform operations with positive values, and fix mod to make floor-based
//
x = floor(-numer / denom);
q = -(int)x;
r = Floor2Int(-numer - (x * denom));
if (r != 0)
{
q--;
r = (int)denom - r;
}
}
*quotient = q;
*rem = r;
}
/*
===================
GreatestCommonDivisor
====================
*/
int GreatestCommonDivisor (int i1, int i2)
{
Assert( s_bMathlibInitialized );
if (i1 > i2)
{
if (i2 == 0)
return (i1);
return GreatestCommonDivisor (i2, i1 % i2);
}
else
{
if (i1 == 0)
return (i2);
return GreatestCommonDivisor (i1, i2 % i1);
}
}
bool IsDenormal( const float &val )
{
const int x = *reinterpret_cast <const int *> (&val); // needs 32-bit int
const int abs_mantissa = x & 0x007FFFFF;
const int biased_exponent = x & 0x7F800000;
return ( biased_exponent == 0 && abs_mantissa != 0 );
}
int SignbitsForPlane (cplane_t *out)
{
Assert( s_bMathlibInitialized );
int bits, j;
// for fast box on planeside test
bits = 0;
for (j=0 ; j<3 ; j++)
{
if (out->normal[j] < 0)
bits |= 1<<j;
}
return bits;
}
/*
==================
BoxOnPlaneSide
Returns 1, 2, or 1 + 2
==================
*/
int __cdecl BoxOnPlaneSide (const float *emins, const float *emaxs, const cplane_t *p)
{
Assert( s_bMathlibInitialized );
float dist1, dist2;
int sides;
// fast axial cases
if (p->type < 3)
{
if (p->dist <= emins[p->type])
return 1;
if (p->dist >= emaxs[p->type])
return 2;
return 3;
}
// general case
switch (p->signbits)
{
case 0:
dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
break;
case 1:
dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
break;
case 2:
dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
break;
case 3:
dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
break;
case 4:
dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
break;
case 5:
dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
break;
case 6:
dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
break;
case 7:
dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
break;
default:
dist1 = dist2 = 0; // shut up compiler
Assert( 0 );
break;
}
sides = 0;
if (dist1 >= p->dist)
sides = 1;
if (dist2 < p->dist)
sides |= 2;
Assert( sides != 0 );