-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.h
1473 lines (1243 loc) · 35.6 KB
/
vector.h
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
#include <cmath>
#include <cstdlib>
#pragma once
#define CHECK_VALID( _v ) 0
#define Assert( _exp ) ((void)0)
#define FastSqrt(x) (sqrt)(x)
#define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
#define M_PI_F ((float)(M_PI)) // Shouldn't collide with anything.
#define M_PHI 1.61803398874989484820 // golden ratio
// NJS: Inlined to prevent floats from being autopromoted to doubles, as with the old system.
#ifndef RAD2DEG
#define RAD2DEG( x ) ( (float)(x) * (float)(180.f / M_PI_F) )
#endif
#ifndef DEG2RAD
#define DEG2RAD( x ) ( (float)(x) * (float)(M_PI_F / 180.f) )
#endif
// MOVEMENT INFO
enum
{
PITCH = 0, // up / down
YAW, // left / right
ROLL // fall over
};
// decls for aligning data
#define DECL_ALIGN(x) __attribute__((aligned(x)))
#define ALIGN16 DECL_ALIGN(16)
#define VALVE_RAND_MAX 0x7fff
#define VectorExpand(v) (v).x, (v).y, (v).z
inline unsigned long& FloatBits(float& f)
{
return *reinterpret_cast<unsigned long*>(&f);
}
inline bool IsFinite(float f)
{
return ((FloatBits(f) & 0x7F800000) != 0x7F800000);
}
struct matrix3x4_t
{
matrix3x4_t() {}
matrix3x4_t(
float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23)
{
m_flMatVal[0][0] = m00; m_flMatVal[0][1] = m01; m_flMatVal[0][2] = m02; m_flMatVal[0][3] = m03;
m_flMatVal[1][0] = m10; m_flMatVal[1][1] = m11; m_flMatVal[1][2] = m12; m_flMatVal[1][3] = m13;
m_flMatVal[2][0] = m20; m_flMatVal[2][1] = m21; m_flMatVal[2][2] = m22; m_flMatVal[2][3] = m23;
}
float* operator[](int i) { Assert((i >= 0) && (i < 3)); return m_flMatVal[i]; }
const float* operator[](int i) const { Assert((i >= 0) && (i < 3)); return m_flMatVal[i]; }
float* Base() { return &m_flMatVal[0][0]; }
[[nodiscard]] const float* Base() const { return &m_flMatVal[0][0]; }
float m_flMatVal[3][4];
};
class Vector
{
public:
float x, y, z;
Vector(void);
Vector(float X, float Y, float Z);
void Init(float ix = 0.0f, float iy = 0.0f, float iz = 0.0f);
[[nodiscard]] bool IsValid() const;
float operator[](int i) const;
float& operator[](int i);
inline void Zero();
bool operator==(const Vector& v) const;
bool operator!=(const Vector& v) const;
inline Vector& operator+=(const Vector& v);
inline Vector& operator-=(const Vector& v);
inline Vector& operator*=(const Vector& v);
inline Vector& operator*=(float s);
inline Vector& operator/=(const Vector& v);
inline Vector& operator/=(float s);
inline Vector& operator+=(float fl);
inline Vector& operator-=(float fl);
inline Vector operator%(const Vector& v);
[[nodiscard]] inline float Length() const;
[[nodiscard]] inline float LengthSqr(void) const
{
CHECK_VALID(*this);
return (x * x + y * y + z * z);
}
[[nodiscard]] bool IsZero(float tolerance = 0.01f) const
{
return (x > -tolerance && x < tolerance&&
y > -tolerance && y < tolerance&&
z > -tolerance && z < tolerance);
}
Vector Normalize();
float NormalizeInPlace();
[[nodiscard]] inline float DistTo(const Vector& vOther) const;
[[nodiscard]] inline float DistToSqr(const Vector& vOther) const;
[[nodiscard]] float Dot(const Vector& vOther) const;
[[nodiscard]] float Length2D(void) const;
[[nodiscard]] float Length2DSqr(void) const;
Vector operator-(void) const;
Vector operator+(const Vector& v) const;
Vector operator-(const Vector& v) const;
Vector operator*(const Vector& v) const;
Vector operator/(const Vector& v) const;
Vector operator*(float fl) const;
Vector operator/(float fl) const;
// Base address...
float* Base();
[[nodiscard]] float const* Base() const;
};
//===============================================
inline void Vector::Init(float ix, float iy, float iz)
{
x = ix; y = iy; z = iz;
CHECK_VALID(*this);
}
//===============================================
inline Vector::Vector(float X, float Y, float Z)
{
x = X; y = Y; z = Z;
CHECK_VALID(*this);
}
//===============================================
inline Vector::Vector(void) { }
//===============================================
inline void Vector::Zero()
{
x = y = z = 0.0f;
}
//===============================================
inline void VectorClear(Vector& a)
{
a.x = a.y = a.z = 0.0f;
}
//===============================================
inline float& Vector::operator[](int i)
{
Assert((i >= 0) && (i < 3));
return ((float*)this)[i];
}
//===============================================
inline float Vector::operator[](int i) const
{
Assert((i >= 0) && (i < 3));
return ((float*)this)[i];
}
//===============================================
inline bool Vector::operator==(const Vector& src) const
{
CHECK_VALID(src);
CHECK_VALID(*this);
return (src.x == x) && (src.y == y) && (src.z == z);
}
//===============================================
inline bool Vector::operator!=(const Vector& src) const
{
CHECK_VALID(src);
CHECK_VALID(*this);
return (src.x != x) || (src.y != y) || (src.z != z);
}
//===============================================
inline void VectorCopy(const Vector& src, Vector& dst)
{
CHECK_VALID(src);
dst.x = src.x;
dst.y = src.y;
dst.z = src.z;
}
//===============================================
inline Vector& Vector::operator+=(const Vector& v)
{
CHECK_VALID(*this);
CHECK_VALID(v);
x += v.x; y += v.y; z += v.z;
return *this;
}
//===============================================
inline Vector& Vector::operator-=(const Vector& v)
{
CHECK_VALID(*this);
CHECK_VALID(v);
x -= v.x; y -= v.y; z -= v.z;
return *this;
}
//===============================================
// Alias for Cross Product.
inline Vector Vector::operator%(const Vector& v)
{
CHECK_VALID(*this);
CHECK_VALID(v);
return Vector(y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x);
}
//===============================================
inline Vector& Vector::operator*=(float fl)
{
x *= fl;
y *= fl;
z *= fl;
CHECK_VALID(*this);
return *this;
}
//===============================================
inline Vector& Vector::operator*=(const Vector& v)
{
CHECK_VALID(v);
x *= v.x;
y *= v.y;
z *= v.z;
CHECK_VALID(*this);
return *this;
}
//===============================================
inline Vector& Vector::operator+=(float fl)
{
x += fl;
y += fl;
z += fl;
CHECK_VALID(*this);
return *this;
}
//===============================================
inline Vector& Vector::operator-=(float fl)
{
x -= fl;
y -= fl;
z -= fl;
CHECK_VALID(*this);
return *this;
}
//===============================================
inline Vector& Vector::operator/=(float fl)
{
Assert(fl != 0.0f);
float oofl = 1.0f / fl;
x *= oofl;
y *= oofl;
z *= oofl;
CHECK_VALID(*this);
return *this;
}
//===============================================
inline Vector& Vector::operator/=(const Vector& v)
{
CHECK_VALID(v);
Assert(v.x != 0.0f && v.y != 0.0f && v.z != 0.0f);
x /= v.x;
y /= v.y;
z /= v.z;
CHECK_VALID(*this);
return *this;
}
//===============================================
inline bool Vector::IsValid() const
{
return IsFinite(x) && IsFinite(y) && IsFinite(z);
}
//===============================================
inline float Vector::Length(void) const
{
CHECK_VALID(*this);
float sqsr = x * x + y * y + z * z;
float root = sqrt(sqsr);
return root;
}
//===============================================
inline float Vector::Length2D(void) const
{
CHECK_VALID(*this);
float sqst = x * x + y * y;
float root = sqrt(sqst);
return root;
}
//===============================================
inline float Vector::Length2DSqr(void) const
{
return (x * x + y * y);
}
//===============================================
inline Vector CrossProduct(const Vector& a, const Vector& b)
{
return Vector(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
//===============================================
float Vector::DistTo(const Vector& vOther) const
{
Vector delta;
delta.x = x - vOther.x;
delta.y = y - vOther.y;
delta.z = z - vOther.z;
return delta.Length();
}
float Vector::DistToSqr(const Vector& vOther) const
{
Vector delta;
delta.x = x - vOther.x;
delta.y = y - vOther.y;
delta.z = z - vOther.z;
return delta.LengthSqr();
}
//===============================================
inline Vector Vector::Normalize()
{
Vector vector;
float length = this->Length();
if (length != 0)
{
vector.x = x / length;
vector.y = y / length;
vector.z = z / length;
}
else
{
vector.x = vector.y = 0.0f; vector.z = 1.0f;
}
return vector;
}
//===============================================
inline float Vector::NormalizeInPlace()
{
Vector& v = *this;
float iradius = 1.f / (this->Length() + 1.192092896e-07F); //FLT_EPSILON
v.x *= iradius;
v.y *= iradius;
v.z *= iradius;
}
//===============================================
inline float VectorNormalize(Vector& v)
{
Assert(v.IsValid());
float l = v.Length();
if (l != 0.0f)
{
v /= l;
}
else
{
// FIXME:
// Just copying the existing implemenation; shouldn't res.z == 0?
v.x = v.y = 0.0f; v.z = 1.0f;
}
return l;
}
//===============================================
inline float VectorNormalize(float* v)
{
return VectorNormalize(*(reinterpret_cast<Vector*>(v)));
}
//===============================================
inline Vector Vector::operator+(const Vector& v) const
{
Vector res;
res.x = x + v.x;
res.y = y + v.y;
res.z = z + v.z;
return res;
}
//===============================================
inline Vector Vector::operator-(const Vector& v) const
{
Vector res;
res.x = x - v.x;
res.y = y - v.y;
res.z = z - v.z;
return res;
}
//===============================================
inline Vector Vector::operator*(float fl) const
{
Vector res;
res.x = x * fl;
res.y = y * fl;
res.z = z * fl;
return res;
}
//===============================================
inline Vector Vector::operator*(const Vector& v) const
{
Vector res;
res.x = x * v.x;
res.y = y * v.y;
res.z = z * v.z;
return res;
}
//===============================================
inline Vector Vector::operator/(float fl) const
{
Vector res;
res.x = x / fl;
res.y = y / fl;
res.z = z / fl;
return res;
}
//===============================================
inline Vector Vector::operator/(const Vector& v) const
{
Vector res;
res.x = x / v.x;
res.y = y / v.y;
res.z = z / v.z;
return res;
}
inline float Vector::Dot(const Vector& vOther) const
{
const Vector& a = *this;
return(a.x * vOther.x + a.y * vOther.y + a.z * vOther.z);
}
//-----------------------------------------------------------------------------
// length
//-----------------------------------------------------------------------------
inline float VectorLength(const Vector& v)
{
CHECK_VALID(v);
return FastSqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
//VECTOR SUBTRAC
inline void VectorSubtract(const Vector& a, const Vector& b, Vector& c)
{
CHECK_VALID(a);
CHECK_VALID(b);
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
}
//VECTORADD
inline void VectorAdd(const Vector& a, const Vector& b, Vector& c)
{
CHECK_VALID(a);
CHECK_VALID(b);
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;
}
//-----------------------------------------------------------------------------
// Base address...
//-----------------------------------------------------------------------------
inline float* Vector::Base()
{
return (float*)this;
}
inline float const* Vector::Base() const
{
return (float const*)this;
}
inline void VectorMAInline(const float* start, float scale, const float* direction, float* dest)
{
dest[0] = start[0] + direction[0] * scale;
dest[1] = start[1] + direction[1] * scale;
dest[2] = start[2] + direction[2] * scale;
}
inline void VectorMAInline(const Vector& start, float scale, const Vector& direction, Vector& dest)
{
dest.x = start.x + direction.x * scale;
dest.y = start.y + direction.y * scale;
dest.z = start.z + direction.z * scale;
}
inline void VectorMA(const Vector& start, float scale, const Vector& direction, Vector& dest)
{
VectorMAInline(start, scale, direction, dest);
}
inline void VectorMA(const float* start, float scale, const float* direction, float* dest)
{
VectorMAInline(start, scale, direction, dest);
}
class VectorAligned : public Vector
{
public:
inline VectorAligned(void) {};
inline VectorAligned(float X, float Y, float Z)
{
Init(X, Y, Z);
}
#ifdef VECTOR_NO_SLOW_OPERATIONS
private:
// No copy constructors allowed if we're in optimal mode
VectorAligned(const VectorAligned& vOther);
VectorAligned(const Vector& vOther);
#else
public:
explicit VectorAligned(const Vector& vOther)
{
Init(vOther.x, vOther.y, vOther.z);
}
VectorAligned& operator=(const Vector& vOther)
{
Init(vOther.x, vOther.y, vOther.z);
return *this;
}
#endif
float w; // this space is used anyway
};
//=========================================================
// 2D Vector2D
//=========================================================
class Vector2D
{
public:
// Members
float x, y;
// Construction/destruction
Vector2D(void);
Vector2D(float X, float Y);
explicit Vector2D(const float* pFloat);
// Initialization
void Init(float ix = 0.0f, float iy = 0.0f);
// Got any nasty NAN's?
[[nodiscard]] bool IsValid() const;
// array access...
float operator[](int i) const;
float& operator[](int i);
// Base address...
float* Base();
[[nodiscard]] float const* Base() const;
// Initialization methods
void Random(float minVal, float maxVal);
// equality
bool operator==(const Vector2D& v) const;
bool operator!=(const Vector2D& v) const;
// arithmetic operations
Vector2D& operator+=(const Vector2D& v);
Vector2D& operator-=(const Vector2D& v);
Vector2D& operator*=(const Vector2D& v);
Vector2D& operator*=(float s);
Vector2D& operator/=(const Vector2D& v);
Vector2D& operator/=(float s);
// negate the Vector2D components
void Negate();
// Get the Vector2D's magnitude.
[[nodiscard]] float Length() const;
// Get the Vector2D's magnitude squared.
[[nodiscard]] float LengthSqr(void) const;
// return true if this vector is (0,0) within tolerance
[[nodiscard]] bool IsZero(float tolerance = 0.01f) const
{
return (x > -tolerance && x < tolerance&&
y > -tolerance && y < tolerance);
}
float Normalize();
// Normalize in place and return the old length.
float NormalizeInPlace();
// Compare length.
[[nodiscard]] bool IsLengthGreaterThan(float val) const;
[[nodiscard]] bool IsLengthLessThan(float val) const;
// Get the distance from this Vector2D to the other one.
[[nodiscard]] float DistTo(const Vector2D& vOther) const;
// Get the distance from this Vector2D to the other one squared.
[[nodiscard]] float DistToSqr(const Vector2D& vOther) const;
// Copy
void CopyToArray(float* rgfl) const;
// Multiply, add, and assign to this (ie: *this = a + b * scalar). This
// is about 12% faster than the actual Vector2D equation (because it's done per-component
// rather than per-Vector2D).
void MulAdd(const Vector2D& a, const Vector2D& b, float scalar);
// Dot product.
[[nodiscard]] float Dot(const Vector2D& vOther) const;
// assignment
Vector2D& operator=(const Vector2D& vOther);
#ifndef VECTOR_NO_SLOW_OPERATIONS
// copy constructors
Vector2D(const Vector2D& vOther);
// arithmetic operations
Vector2D operator-(void) const;
Vector2D operator+(const Vector2D& v) const;
Vector2D operator-(const Vector2D& v) const;
Vector2D operator*(const Vector2D& v) const;
Vector2D operator/(const Vector2D& v) const;
Vector2D operator*(float fl) const;
Vector2D operator/(float fl) const;
// Cross product between two vectors.
[[nodiscard]] Vector2D Cross(const Vector2D& vOther) const;
// Returns a Vector2D with the min or max in X, Y, and Z.
[[nodiscard]] Vector2D Min(const Vector2D& vOther) const;
[[nodiscard]] Vector2D Max(const Vector2D& vOther) const;
#else
private:
// No copy constructors allowed if we're in optimal mode
Vector2D(const Vector2D& vOther);
#endif
};
//-----------------------------------------------------------------------------
const Vector2D vec2_origin(0, 0);
const Vector2D vec2_invalid(3.40282347E+38F, 3.40282347E+38F);
//-----------------------------------------------------------------------------
// Vector2D related operations
//-----------------------------------------------------------------------------
// Vector2D clear
void Vector2DClear(Vector2D& a);
// Copy
void Vector2DCopy(const Vector2D& src, Vector2D& dst);
// Vector2D arithmetic
void Vector2DAdd(const Vector2D& a, const Vector2D& b, Vector2D& result);
void Vector2DSubtract(const Vector2D& a, const Vector2D& b, Vector2D& result);
void Vector2DMultiply(const Vector2D& a, float b, Vector2D& result);
void Vector2DMultiply(const Vector2D& a, const Vector2D& b, Vector2D& result);
void Vector2DDivide(const Vector2D& a, float b, Vector2D& result);
void Vector2DDivide(const Vector2D& a, const Vector2D& b, Vector2D& result);
void Vector2DMA(const Vector2D& start, float s, const Vector2D& dir, Vector2D& result);
// Store the min or max of each of x, y, and z into the result.
void Vector2DMin(const Vector2D& a, const Vector2D& b, Vector2D& result);
void Vector2DMax(const Vector2D& a, const Vector2D& b, Vector2D& result);
#define Vector2DExpand( v ) (v).x, (v).y
// Normalization
float Vector2DNormalize(Vector2D& v);
// Length
float Vector2DLength(const Vector2D& v);
// Dot Product
float DotProduct2D(const Vector2D& a, const Vector2D& b);
// Linearly interpolate between two vectors
void Vector2DLerp(const Vector2D& src1, const Vector2D& src2, float t, Vector2D& dest);
//-----------------------------------------------------------------------------
//
// Inlined Vector2D methods
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// constructors
//-----------------------------------------------------------------------------
inline Vector2D::Vector2D(void)
{
#ifdef _DEBUG
// Initialize to NAN to catch errors
//x = y = float_NAN;
#endif
}
inline Vector2D::Vector2D(float X, float Y)
{
x = X; y = Y;
Assert(IsValid());
}
inline Vector2D::Vector2D(const float* pFloat)
{
Assert(pFloat);
x = pFloat[0]; y = pFloat[1];
Assert(IsValid());
}
//-----------------------------------------------------------------------------
// copy constructor
//-----------------------------------------------------------------------------
inline Vector2D::Vector2D(const Vector2D& vOther)
{
Assert(vOther.IsValid());
x = vOther.x; y = vOther.y;
}
//-----------------------------------------------------------------------------
// initialization
//-----------------------------------------------------------------------------
inline void Vector2D::Init(float ix, float iy)
{
x = ix; y = iy;
Assert(IsValid());
}
inline void Vector2D::Random(float minVal, float maxVal)
{
x = minVal + ((float)rand() / VALVE_RAND_MAX) * (maxVal - minVal);
y = minVal + ((float)rand() / VALVE_RAND_MAX) * (maxVal - minVal);
}
inline void Vector2DClear(Vector2D& a)
{
a.x = a.y = 0.0f;
}
//-----------------------------------------------------------------------------
// assignment
//-----------------------------------------------------------------------------
inline Vector2D& Vector2D::operator=(const Vector2D& vOther)
{
Assert(vOther.IsValid());
x = vOther.x; y = vOther.y;
return *this;
}
//-----------------------------------------------------------------------------
// Array access
//-----------------------------------------------------------------------------
inline float& Vector2D::operator[](int i)
{
Assert((i >= 0) && (i < 2));
return ((float*)this)[i];
}
inline float Vector2D::operator[](int i) const
{
Assert((i >= 0) && (i < 2));
return ((float*)this)[i];
}
//-----------------------------------------------------------------------------
// Base address...
//-----------------------------------------------------------------------------
inline float* Vector2D::Base()
{
return (float*)this;
}
inline float const* Vector2D::Base() const
{
return (float const*)this;
}
//-----------------------------------------------------------------------------
// IsValid?
//-----------------------------------------------------------------------------
inline bool Vector2D::IsValid() const
{
return IsFinite(x) && IsFinite(y);
}
//-----------------------------------------------------------------------------
// comparison
//-----------------------------------------------------------------------------
inline bool Vector2D::operator==(const Vector2D& src) const
{
Assert(src.IsValid() && IsValid());
return (src.x == x) && (src.y == y);
}
inline bool Vector2D::operator!=(const Vector2D& src) const
{
Assert(src.IsValid() && IsValid());
return (src.x != x) || (src.y != y);
}
//-----------------------------------------------------------------------------
// Copy
//-----------------------------------------------------------------------------
inline void Vector2DCopy(const Vector2D& src, Vector2D& dst)
{
Assert(src.IsValid());
dst.x = src.x;
dst.y = src.y;
}
inline void Vector2D::CopyToArray(float* rgfl) const
{
Assert(IsValid());
Assert(rgfl);
rgfl[0] = x; rgfl[1] = y;
}
//-----------------------------------------------------------------------------
// standard math operations
//-----------------------------------------------------------------------------
inline void Vector2D::Negate()
{
Assert(IsValid());
x = -x; y = -y;
}
inline Vector2D& Vector2D::operator+=(const Vector2D& v)
{
Assert(IsValid() && v.IsValid());
x += v.x; y += v.y;
return *this;
}
inline Vector2D& Vector2D::operator-=(const Vector2D& v)
{
Assert(IsValid() && v.IsValid());
x -= v.x; y -= v.y;
return *this;
}
inline Vector2D& Vector2D::operator*=(float fl)
{
x *= fl;
y *= fl;
Assert(IsValid());
return *this;
}
inline Vector2D& Vector2D::operator*=(const Vector2D& v)
{
x *= v.x;
y *= v.y;
Assert(IsValid());
return *this;
}
inline Vector2D& Vector2D::operator/=(float fl)
{
Assert(fl != 0.0f);
float oofl = 1.0f / fl;
x *= oofl;
y *= oofl;
Assert(IsValid());
return *this;
}
inline Vector2D& Vector2D::operator/=(const Vector2D& v)
{
Assert(v.x != 0.0f && v.y != 0.0f);
x /= v.x;
y /= v.y;
Assert(IsValid());
return *this;
}
inline void Vector2DAdd(const Vector2D& a, const Vector2D& b, Vector2D& c)
{
Assert(a.IsValid() && b.IsValid());
c.x = a.x + b.x;
c.y = a.y + b.y;
}
inline void Vector2DSubtract(const Vector2D& a, const Vector2D& b, Vector2D& c)
{
Assert(a.IsValid() && b.IsValid());
c.x = a.x - b.x;
c.y = a.y - b.y;
}
inline void Vector2DMultiply(const Vector2D& a, float b, Vector2D& c)
{
Assert(a.IsValid() && IsFinite(b));
c.x = a.x * b;
c.y = a.y * b;
}
inline void Vector2DMultiply(const Vector2D& a, const Vector2D& b, Vector2D& c)
{
Assert(a.IsValid() && b.IsValid());
c.x = a.x * b.x;
c.y = a.y * b.y;
}
inline void Vector2DDivide(const Vector2D& a, float b, Vector2D& c)
{
Assert(a.IsValid());
Assert(b != 0.0f);
float oob = 1.0f / b;
c.x = a.x * oob;
c.y = a.y * oob;
}
inline void Vector2DDivide(const Vector2D& a, const Vector2D& b, Vector2D& c)
{
Assert(a.IsValid());
Assert((b.x != 0.0f) && (b.y != 0.0f));
c.x = a.x / b.x;
c.y = a.y / b.y;
}
inline void Vector2DMA(const Vector2D& start, float s, const Vector2D& dir, Vector2D& result)
{
Assert(start.IsValid() && IsFinite(s) && dir.IsValid());
result.x = start.x + s * dir.x;
result.y = start.y + s * dir.y;
}
// FIXME: Remove
// For backwards compatability
inline void Vector2D::MulAdd(const Vector2D& a, const Vector2D& b, float scalar)
{
x = a.x + b.x * scalar;
y = a.y + b.y * scalar;
}
inline void Vector2DLerp(const Vector2D& src1, const Vector2D& src2, float t, Vector2D& dest)
{
dest[0] = src1[0] + (src2[0] - src1[0]) * t;
dest[1] = src1[1] + (src2[1] - src1[1]) * t;
}
//-----------------------------------------------------------------------------
// dot, cross
//-----------------------------------------------------------------------------
inline float DotProduct2D(const Vector2D& a, const Vector2D& b)
{
Assert(a.IsValid() && b.IsValid());
return(a.x * b.x + a.y * b.y);
}
// for backwards compatability
inline float Vector2D::Dot(const Vector2D& vOther) const
{
return DotProduct2D(*this, vOther);
}
//-----------------------------------------------------------------------------
// length
//-----------------------------------------------------------------------------
inline float Vector2DLength(const Vector2D& v)
{