forked from alimb2/Ayyware-Full-Fixed-Updated-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.h
687 lines (538 loc) · 13.2 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
#ifndef _VECTORZ_H
#define _VECTORZ_H
#define CHECK_VALID( _v ) 0
#define Assert( _exp ) ((void)0)
#define rad(a) a * 0.01745329251
#define deg(a) a * 57.295779513082
#include <Windows.h>
#include <math.h>
#include <emmintrin.h>
#include <xmmintrin.h>
inline void SinCosX(const float rad, float &sin, float &cos)
{
const __m128 _ps_fopi = _mm_set_ss(1.27323954473516f);
const __m128 _ps_0p5 = _mm_set_ss(0.5f);
const __m128 _ps_1 = _mm_set_ss(1.0f);
const __m128 _ps_dp1 = _mm_set_ss(-0.7851562f);
const __m128 _ps_dp2 = _mm_set_ss(-2.4187564849853515625e-4f);
const __m128 _ps_dp3 = _mm_set_ss(-3.77489497744594108e-8f);
const __m128 _ps_sincof_p0 = _mm_set_ss(2.443315711809948e-5f);
const __m128 _ps_sincof_p1 = _mm_set_ss(8.3321608736e-3f);
const __m128 _ps_sincof_p2 = _mm_set_ss(-1.6666654611e-1f);
const __m128 _ps_coscof_p0 = _mm_set_ss(2.443315711809948e-5f);
const __m128 _ps_coscof_p1 = _mm_set_ss(-1.388731625493765e-3f);
const __m128 _ps_coscof_p2 = _mm_set_ss(4.166664568298827e-2f);
const __m128i _pi32_1 = _mm_set1_epi32(1);
const __m128i _pi32_i1 = _mm_set1_epi32(~1);
const __m128i _pi32_2 = _mm_set1_epi32(2);
const __m128i _pi32_4 = _mm_set1_epi32(4);
const __m128 _mask_sign_raw = *(__m128*)&_mm_set1_epi32(0x80000000);
const __m128 _mask_sign_inv = *(__m128*)&_mm_set1_epi32(~0x80000000);
__m128 xmm3 = _mm_setzero_ps();
__m128i emm0, emm2, emm4;
__m128 sign_bit_cos, sign_bit_sin;
__m128 x, y, z;
__m128 y1, y2;
__m128 a = _mm_set_ss(rad);
x = _mm_and_ps(a, _mask_sign_inv);
y = _mm_mul_ps(x, _ps_fopi);
emm2 = _mm_cvtps_epi32(y);
emm2 = _mm_add_epi32(emm2, _pi32_1);
emm2 = _mm_and_si128(emm2, _pi32_i1);
y = _mm_cvtepi32_ps(emm2);
emm4 = emm2;
emm0 = _mm_and_si128(emm2, _pi32_4);
emm0 = _mm_slli_epi32(emm0, 29);
__m128 swap_sign_bit_sin = _mm_castsi128_ps(emm0);
emm2 = _mm_and_si128(emm2, _pi32_2);
emm2 = _mm_cmpeq_epi32(emm2, _mm_setzero_si128());
__m128 poly_mask = _mm_castsi128_ps(emm2);
x = _mm_add_ps(x, _mm_mul_ps(y, _ps_dp1));
x = _mm_add_ps(x, _mm_mul_ps(y, _ps_dp2));
x = _mm_add_ps(x, _mm_mul_ps(y, _ps_dp3));
emm4 = _mm_sub_epi32(emm4, _pi32_2);
emm4 = _mm_andnot_si128(emm4, _pi32_4);
emm4 = _mm_slli_epi32(emm4, 29);
sign_bit_cos = _mm_castsi128_ps(emm4);
sign_bit_sin = _mm_xor_ps(_mm_and_ps(a, _mask_sign_raw), swap_sign_bit_sin);
z = _mm_mul_ps(x, x);
y1 = _mm_mul_ps(_ps_coscof_p0, z);
y1 = _mm_add_ps(y1, _ps_coscof_p1);
y1 = _mm_mul_ps(y1, z);
y1 = _mm_add_ps(y1, _ps_coscof_p2);
y1 = _mm_mul_ps(y1, z);
y1 = _mm_mul_ps(y1, z);
y1 = _mm_sub_ps(y1, _mm_mul_ps(z, _ps_0p5));
y1 = _mm_add_ps(y1, _ps_1);
y2 = _mm_mul_ps(_ps_sincof_p0, z);
y2 = _mm_add_ps(y2, _ps_sincof_p1);
y2 = _mm_mul_ps(y2, z);
y2 = _mm_add_ps(y2, _ps_sincof_p2);
y2 = _mm_mul_ps(y2, z);
y2 = _mm_mul_ps(y2, x);
y2 = _mm_add_ps(y2, x);
xmm3 = poly_mask;
__m128 ysin2 = _mm_and_ps(xmm3, y2);
__m128 ysin1 = _mm_andnot_ps(xmm3, y1);
sin = _mm_cvtss_f32(_mm_xor_ps(_mm_add_ps(ysin1, ysin2), sign_bit_sin));
cos = _mm_cvtss_f32(_mm_xor_ps(_mm_add_ps(_mm_sub_ps(y1, ysin1), _mm_sub_ps(y2, ysin2)), sign_bit_cos));
}
// from the leaked sdk
#define FORCEINLINE __forceinline
typedef float vec_t;
inline vec_t BitsToFloat(unsigned long i)
{
return *reinterpret_cast<vec_t*>(&i);
}
inline float sqrt2(float sqr)
{
float root = 0;
__asm
{
sqrtss xmm0, sqr
movss root, xmm0
}
return root;
}
#define FLOAT32_NAN_BITS (unsigned long)0x7FC00000
#define FLOAT32_NAN BitsToFloat( FLOAT32_NAN_BITS )
#define VEC_T_NAN FLOAT32_NAN
class Vector
{
public:
vec_t x, y, z;
Vector(void);
Vector(vec_t X, vec_t Y, vec_t Z);
explicit Vector(vec_t XYZ);
void Init(vec_t ix = 0.0f, vec_t iy = 0.0f, vec_t iz = 0.0f);
bool IsValid() const;
void Invalidate();
vec_t operator[](int i) const;
vec_t& operator[](int i);
vec_t* Base();
vec_t const* Base() const;
inline void Zero();
bool operator==(const Vector& v) const;
bool operator!=(const Vector& v) const;
bool operator!() { return !x && !y && !z; }
FORCEINLINE Vector& operator+=(const Vector& v);
FORCEINLINE Vector& operator-=(const Vector& v);
FORCEINLINE Vector& operator*=(const Vector& v);
FORCEINLINE Vector& operator*=(float s);
FORCEINLINE Vector& operator/=(const Vector& v);
FORCEINLINE Vector& operator/=(float s);
FORCEINLINE Vector& operator+=(float fl);
FORCEINLINE Vector& operator-=(float fl);
inline float Long() { return sqrt2(x*x + y*y + z*z); }
void Negate();
inline vec_t Length() const;
inline Vector Angle(Vector* up = 0)
{
if (!x && !y)
return Vector(0, 0, 0);
float roll = 0;
if (up)
{
Vector left = (*up).Cross(*this);
roll = deg(atan2f(left.z, (left.y * x) - (left.x * y)));
}
return Vector(deg(atan2f(-z, sqrtf(x*x + y*y))), deg(atan2f(y, x)), roll);
}
inline Vector Forward()
{
float cp, cy, sp, sy;
SinCosX(rad(x), sp, cp);
SinCosX(rad(y), sy, cy);
return Vector(cp*cy, cp*sy, -sp);
}
FORCEINLINE vec_t LengthSqr(void) const
{
return (x * x + y * y + z * z);
}
bool IsZero(float tolerance = 0.01f) const
{
return (x > -tolerance && x < tolerance &&
y > -tolerance && y < tolerance &&
z > -tolerance && z < tolerance);
}
float Vector::Size()
{
return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
}
vec_t NormalizeInPlace();
Vector Normalized() const;
bool IsLengthGreaterThan(float val) const;
bool IsLengthLessThan(float val) const;
FORCEINLINE bool WithinAABox(Vector const &boxmin, Vector const &boxmax);
vec_t DistTo(const Vector& vOther) const;
FORCEINLINE vec_t 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();
}
void CopyToArray(float* rgfl) const;
void MulAdd(const Vector& a, const Vector& b, float scalar);
vec_t Dot(const Vector& vOther) const;
Vector& operator=(const Vector& vOther);
vec_t Length2D(void) const;
vec_t 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;
Vector Cross(const Vector& vOther) const;
Vector Min(const Vector& vOther) const;
Vector Max(const Vector& vOther) const;
};
using QAngle = Vector;
FORCEINLINE Vector ReplicateToVector(float x)
{
return Vector(x, x, x);
}
inline Vector::Vector(void)
{
}
inline Vector::Vector(vec_t X, vec_t Y, vec_t Z)
{
x = X; y = Y; z = Z;
}
inline Vector::Vector(vec_t XYZ)
{
x = y = z = XYZ;
}
inline void Vector::Init(vec_t ix, vec_t iy, vec_t iz)
{
x = ix; y = iy; z = iz;
}
inline void Vector::Zero()
{
x = y = z = 0.0f;
}
inline void VectorClear(Vector& a)
{
a.x = a.y = a.z = 0.0f;
}
inline Vector& Vector::operator=(const Vector& vOther)
{
x = vOther.x; y = vOther.y; z = vOther.z;
return *this;
}
inline vec_t& Vector::operator[](int i)
{
return ((vec_t*)this)[i];
}
inline vec_t Vector::operator[](int i) const
{
return ((vec_t*)this)[i];
}
inline vec_t* Vector::Base()
{
return (vec_t*)this;
}
inline vec_t const* Vector::Base() const
{
return (vec_t const*)this;
}
inline bool Vector::IsValid() const
{
return (x == x && y == y && z == z);
}
inline void Vector::Invalidate()
{
x = y = z = VEC_T_NAN;
}
inline bool Vector::operator==(const Vector& src) const
{
return (src.x == x) && (src.y == y) && (src.z == z);
}
inline bool Vector::operator!=(const Vector& src) const
{
return (src.x != x) || (src.y != y) || (src.z != z);
}
FORCEINLINE void VectorCopy(const Vector& src, Vector& dst)
{
dst.x = src.x;
dst.y = src.y;
dst.z = src.z;
}
inline void Vector::CopyToArray(float* rgfl) const
{
rgfl[0] = x; rgfl[1] = y; rgfl[2] = z;
}
inline void Vector::Negate()
{
x = -x; y = -y; z = -z;
}
FORCEINLINE Vector& Vector::operator+=(const Vector& v)
{
x += v.x; y += v.y; z += v.z;
return *this;
}
FORCEINLINE Vector& Vector::operator-=(const Vector& v)
{
x -= v.x; y -= v.y; z -= v.z;
return *this;
}
FORCEINLINE Vector& Vector::operator*=(float fl)
{
x *= fl;
y *= fl;
z *= fl;
return *this;
}
FORCEINLINE Vector& Vector::operator*=(const Vector& v)
{
x *= v.x;
y *= v.y;
z *= v.z;
return *this;
}
FORCEINLINE Vector& Vector::operator+=(float fl)
{
x += fl;
y += fl;
z += fl;
return *this;
}
FORCEINLINE Vector& Vector::operator-=(float fl)
{
x -= fl;
y -= fl;
z -= fl;
return *this;
}
FORCEINLINE Vector& Vector::operator/=(float fl)
{
float oofl = 1.0f / fl;
x *= oofl;
y *= oofl;
z *= oofl;
return *this;
}
FORCEINLINE Vector& Vector::operator/=(const Vector& v)
{
x /= v.x;
y /= v.y;
z /= v.z;
return *this;
}
FORCEINLINE void VectorAdd(const Vector& a, const Vector& b, Vector& c)
{
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;
}
FORCEINLINE void VectorSubtract(const Vector& a, const Vector& b, Vector& c)
{
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
}
FORCEINLINE void VectorMultiply(const Vector& a, vec_t b, Vector& c)
{
c.x = a.x * b;
c.y = a.y * b;
c.z = a.z * b;
}
FORCEINLINE void VectorMultiply(const Vector& a, const Vector& b, Vector& c)
{
c.x = a.x * b.x;
c.y = a.y * b.y;
c.z = a.z * b.z;
}
inline void VectorScale(const Vector& in, vec_t scale, Vector& result)
{
VectorMultiply(in, scale, result);
}
FORCEINLINE void VectorDivide(const Vector& a, vec_t b, Vector& c)
{
vec_t oob = 1.0f / b;
c.x = a.x * oob;
c.y = a.y * oob;
c.z = a.z * oob;
}
FORCEINLINE void VectorDivide(const Vector& a, const Vector& b, Vector& c)
{
c.x = a.x / b.x;
c.y = a.y / b.y;
c.z = a.z / b.z;
}
inline void Vector::MulAdd(const Vector& a, const Vector& b, float scalar)
{
x = a.x + b.x * scalar;
y = a.y + b.y * scalar;
z = a.z + b.z * scalar;
}
inline void VectorLerp(const Vector& src1, const Vector& src2, vec_t t, Vector& dest)
{
dest.x = src1.x + (src2.x - src1.x) * t;
dest.y = src1.y + (src2.y - src1.y) * t;
dest.z = src1.z + (src2.z - src1.z) * t;
}
FORCEINLINE vec_t DotProduct(const Vector& a, const Vector& b)
{
return (a.x * b.x + a.y * b.y + a.z * b.z);
}
inline vec_t Vector::Dot(const Vector& vOther) const
{
return DotProduct(*this, vOther);
}
inline void CrossProduct(const Vector& a, const Vector& b, Vector& result)
{
result.x = a.y * b.z - a.z * b.y;
result.y = a.z * b.x - a.x * b.z;
result.z = a.x * b.y - a.y * b.x;
}
//inline vec_t DotProductAbs(const Vector& v0, const Vector& v1)
//{
// return abs(v0.x * v1.x) + abs(v0.y * v1.y) + abs(v0.z * v1.z);
//}
inline vec_t VectorLength(const Vector& v)
{
return (vec_t)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
inline vec_t Vector::Length(void) const
{
return VectorLength(*this);
}
inline vec_t VectorNormalize(Vector& v)
{
vec_t l = v.Length();
if (l != 0.0f)
{
v /= l;
}
else
{
v.x = v.y = 0.0f; v.z = 1.0f;
}
return l;
}
FORCEINLINE float VectorNormalizer(float * v)
{
return VectorNormalize(*(reinterpret_cast<Vector *>(v)));
}
inline vec_t Vector::NormalizeInPlace()
{
return VectorNormalize(*this);
}
bool Vector::WithinAABox(Vector const &boxmin, Vector const &boxmax)
{
return (
(x >= boxmin.x) && (x <= boxmax.x) &&
(y >= boxmin.y) && (y <= boxmax.y) &&
(z >= boxmin.z) && (z <= boxmax.z)
);
}
inline vec_t Vector::DistTo(const Vector& vOther) const
{
Vector delta;
VectorSubtract(*this, vOther, delta);
return delta.Length();
}
inline Vector Vector::Min(const Vector& vOther) const
{
return Vector(x < vOther.x ? x : vOther.x,
y < vOther.y ? y : vOther.y,
z < vOther.z ? z : vOther.z);
}
inline Vector Vector::Max(const Vector& vOther) const
{
return Vector(x > vOther.x ? x : vOther.x,
y > vOther.y ? y : vOther.y,
z > vOther.z ? z : vOther.z);
}
inline Vector Vector::operator-(void) const
{
return Vector(-x, -y, -z);
}
inline Vector Vector::operator+(const Vector& v) const
{
Vector res;
VectorAdd(*this, v, res);
return res;
}
inline Vector Vector::operator-(const Vector& v) const
{
Vector res;
VectorSubtract(*this, v, res);
return res;
}
inline Vector Vector::operator*(float fl) const
{
Vector res;
VectorMultiply(*this, fl, res);
return res;
}
inline Vector Vector::operator*(const Vector& v) const
{
Vector res;
VectorMultiply(*this, v, res);
return res;
}
inline Vector Vector::operator/(float fl) const
{
Vector res;
VectorDivide(*this, fl, res);
return res;
}
inline Vector Vector::operator/(const Vector& v) const
{
Vector res;
VectorDivide(*this, v, res);
return res;
}
inline Vector operator*(float fl, const Vector& v)
{
return v * fl;
}
inline Vector Vector::Cross(const Vector& vOther) const
{
Vector res;
CrossProduct(*this, vOther, res);
return res;
}
inline vec_t Vector::Length2D(void) const
{
return (vec_t)::sqrtf(x * x + y * y);
}
inline vec_t 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);
}
inline void VectorMin(const Vector& a, const Vector& b, Vector& result)
{
result.x = min(a.x, b.x);
result.y = min(a.y, b.y);
result.z = min(a.z, b.z);
}
inline void VectorMax(const Vector& a, const Vector& b, Vector& result)
{
result.x = max(a.x, b.x);
result.y = max(a.y, b.y);
result.z = max(a.z, b.z);
}
class VectorAligned : public Vector
{
public:
VectorAligned()
{
x = y = z = 0.0f;
}
VectorAligned(const Vector& v)
{
x = v.x; y = v.y; z = v.z;
}
float w;
};
#endif // VECTOR_H