-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVector.h
320 lines (304 loc) · 7.29 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
#include "SDK.h"
#pragma once
#define CHECK_VALID( _v ) 0
#define Assert( _exp ) ((void)0)
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);
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;
__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 Length() const;
__forceinline float LengthSqr(void) const
{
CHECK_VALID(*this);
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 NormalizeInPlace();
__forceinline float DistToSqr(const Vector &vOther) const;
float Dot(const Vector& vOther) const;
float Length2D(void) const;
float Length2DSqr(void) const;
Vector& operator=(const Vector &vOther);
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;
};
//===============================================
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 Vector& Vector::operator=(const Vector &vOther)
{
CHECK_VALID(vOther);
x=vOther.x; y=vOther.y; z=vOther.z;
return *this;
}
//===============================================
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);
}
//===============================================
__forceinline void VectorCopy( const Vector& src, Vector& dst )
{
CHECK_VALID(src);
dst.x = src.x;
dst.y = src.y;
dst.z = src.z;
}
//===============================================
__forceinline Vector& Vector::operator+=(const Vector& v)
{
CHECK_VALID(*this);
CHECK_VALID(v);
x+=v.x; y+=v.y; z += v.z;
return *this;
}
//===============================================
__forceinline Vector& Vector::operator-=(const Vector& v)
{
CHECK_VALID(*this);
CHECK_VALID(v);
x-=v.x; y-=v.y; z -= v.z;
return *this;
}
//===============================================
__forceinline Vector& Vector::operator*=(float fl)
{
x *= fl;
y *= fl;
z *= fl;
CHECK_VALID(*this);
return *this;
}
//===============================================
__forceinline Vector& Vector::operator*=(const Vector& v)
{
CHECK_VALID(v);
x *= v.x;
y *= v.y;
z *= v.z;
CHECK_VALID(*this);
return *this;
}
//===============================================
__forceinline Vector& Vector::operator+=(float fl)
{
x += fl;
y += fl;
z += fl;
CHECK_VALID(*this);
return *this;
}
//===============================================
__forceinline Vector& Vector::operator-=(float fl)
{
x -= fl;
y -= fl;
z -= fl;
CHECK_VALID(*this);
return *this;
}
//===============================================
__forceinline 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;
}
//===============================================
__forceinline 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 float Vector::Length(void) const
{
CHECK_VALID(*this);
float root = 0.0f;
float sqsr = x*x + y*y + z*z;
__asm
{
sqrtss xmm0, sqsr
movss root, xmm0
}
return root;
}
//===============================================
inline float Vector::Length2D(void) const
{
CHECK_VALID(*this);
float root = 0.0f;
float sqst = x*x + y*y;
__asm
{
sqrtss xmm0, sqst
movss root, xmm0
}
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::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 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 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 );
}