aboutsummaryrefslogtreecommitdiff
path: root/primedev/core/math/vector.h
blob: e62f2c93f0590d19bd55bf88d76f535f118828bd (plain)
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
#pragma once

#include "bits.h"
#include "math_pfns.h"

#include <cmath>

#define DEG2RAD(a) (a) * (3.14159265358979323846f / 180.0f)
#define RAD2DEG(a) (a) * (180.0f / 3.14159265358979323846f)

class Vector3
{
public:
	float x, y, z;

	Vector3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
	Vector3(float _f) : x(_f), y(_f), z(_f) {}
	Vector3() : x(0.0f), y(0.0f), z(0.0f) {}

	inline bool IsValid()
	{
		return IsFinite(x) && IsFinite(y) && IsFinite(z);
	}

	inline void Init(float fX = 0.0f, float fY = 0.0f, float fZ = 0.0f)
	{
		x = fX;
		y = fY;
		z = fZ;
	}

	inline float Length()
	{
		return FastSqrt(x * x + y * y + z * z);
	}

	inline float DistTo(const Vector3& vOther)
	{
		Vector3 vDelta;
		vDelta.x = vOther.x - x;
		vDelta.y = vOther.y - y;
		vDelta.z = vOther.z - z;

		return vDelta.Length();
	}

	float Dot(const Vector3& vOther) const
	{
		return x * vOther.x + y * vOther.y + z * vOther.z;
	}

	// Cross product between two vectors.
	Vector3 Cross(const Vector3& vOther) const;

	void Normalize();

	bool operator==(const Vector3& v) const;
	bool operator!=(const Vector3& v) const;

	FORCEINLINE Vector3& operator+=(const Vector3& v);
	FORCEINLINE Vector3& operator-=(const Vector3& v);
	FORCEINLINE Vector3& operator*=(const Vector3& v);
	FORCEINLINE Vector3& operator*=(float s);
	FORCEINLINE Vector3& operator/=(const Vector3& v);
	FORCEINLINE Vector3& operator/=(float s);
	FORCEINLINE Vector3& operator+=(float fl); ///< broadcast add
	FORCEINLINE Vector3& operator-=(float fl);

	// arithmetic operations
	Vector3 operator-(void) const;

	Vector3 operator+(const Vector3& v) const;
	Vector3 operator-(const Vector3& v) const;
	Vector3 operator*(const Vector3& v) const;
	Vector3 operator/(const Vector3& v) const;
	Vector3 operator*(float fl) const;
	Vector3 operator/(float fl) const;
};

FORCEINLINE void VectorAdd(const Vector3& a, const Vector3& b, Vector3& result);
FORCEINLINE void VectorSubtract(const Vector3& a, const Vector3& b, Vector3& result);
FORCEINLINE void VectorMultiply(const Vector3& a, float b, Vector3& result);
FORCEINLINE void VectorMultiply(const Vector3& a, const Vector3& b, Vector3& result);
FORCEINLINE void VectorDivide(const Vector3& a, float b, Vector3& result);
FORCEINLINE void VectorDivide(const Vector3& a, const Vector3& b, Vector3& result);

inline bool Vector3::operator==(const Vector3& src) const
{
	return (src.x == x) && (src.y == y) && (src.z == z);
}

inline bool Vector3::operator!=(const Vector3& src) const
{
	return (src.x != x) || (src.y != y) || (src.z != z);
}

FORCEINLINE Vector3& Vector3::operator+=(const Vector3& v)
{
	x += v.x;
	y += v.y;
	z += v.z;
	return *this;
}

FORCEINLINE Vector3& Vector3::operator-=(const Vector3& v)
{
	x -= v.x;
	y -= v.y;
	z -= v.z;
	return *this;
}

FORCEINLINE Vector3& Vector3::operator*=(float fl)
{
	x *= fl;
	y *= fl;
	z *= fl;
	return *this;
}

FORCEINLINE Vector3& Vector3::operator*=(const Vector3& v)
{
	x *= v.x;
	y *= v.y;
	z *= v.z;
	return *this;
}

// this ought to be an opcode.
FORCEINLINE Vector3& Vector3::operator+=(float fl)
{
	x += fl;
	y += fl;
	z += fl;
	return *this;
}

FORCEINLINE Vector3& Vector3::operator-=(float fl)
{
	x -= fl;
	y -= fl;
	z -= fl;
	return *this;
}

FORCEINLINE Vector3& Vector3::operator/=(float fl)
{
	float oofl = 1.0f / fl;
	x *= oofl;
	y *= oofl;
	z *= oofl;
	return *this;
}

FORCEINLINE Vector3& Vector3::operator/=(const Vector3& v)
{
	x /= v.x;
	y /= v.y;
	z /= v.z;
	return *this;
}

inline Vector3 Vector3::operator-(void) const
{
	return Vector3(-x, -y, -z);
}

inline Vector3 Vector3::operator+(const Vector3& v) const
{
	Vector3 res;
	VectorAdd(*this, v, res);
	return res;
}

inline Vector3 Vector3::operator-(const Vector3& v) const
{
	Vector3 res;
	VectorSubtract(*this, v, res);
	return res;
}

inline Vector3 Vector3::operator*(float fl) const
{
	Vector3 res;
	VectorMultiply(*this, fl, res);
	return res;
}

inline Vector3 Vector3::operator*(const Vector3& v) const
{
	Vector3 res;
	VectorMultiply(*this, v, res);
	return res;
}

inline Vector3 Vector3::operator/(float fl) const
{
	Vector3 res;
	VectorDivide(*this, fl, res);
	return res;
}

inline Vector3 Vector3::operator/(const Vector3& v) const
{
	Vector3 res;
	VectorDivide(*this, v, res);
	return res;
}

inline Vector3 operator*(float fl, const Vector3& v)
{
	return v * fl;
}

inline Vector3 Vector3::Cross(const Vector3& vOther) const
{
	return Vector3(y * vOther.z - z * vOther.y, z * vOther.x - x * vOther.z, x * vOther.y - y * vOther.x);
}

inline void Vector3::Normalize()
{
	float fLen = Length();
	x /= fLen;
	y /= fLen;
	z /= fLen;
}

inline Vector3 StringToVector(char* pString)
{
	Vector3 vRet;

	int length = 0;
	while (pString[length])
	{
		if ((pString[length] == '<') || (pString[length] == '>'))
			pString[length] = '\0';
		length++;
	}

	int startOfFloat = 1;
	int currentIndex = 1;

	while (pString[currentIndex] && (pString[currentIndex] != ','))
		currentIndex++;
	pString[currentIndex] = '\0';
	vRet.x = std::stof(&pString[startOfFloat]);
	startOfFloat = ++currentIndex;

	while (pString[currentIndex] && (pString[currentIndex] != ','))
		currentIndex++;
	pString[currentIndex] = '\0';
	vRet.y = std::stof(&pString[startOfFloat]);
	startOfFloat = ++currentIndex;

	while (pString[currentIndex] && (pString[currentIndex] != ','))
		currentIndex++;
	pString[currentIndex] = '\0';
	vRet.z = std::stof(&pString[startOfFloat]);
	startOfFloat = ++currentIndex;

	return vRet;
}

FORCEINLINE void VectorAdd(const Vector3& a, const Vector3& b, Vector3& c)
{
	c.x = a.x + b.x;
	c.y = a.y + b.y;
	c.z = a.z + b.z;
}

FORCEINLINE void VectorSubtract(const Vector3& a, const Vector3& b, Vector3& c)
{
	c.x = a.x - b.x;
	c.y = a.y - b.y;
	c.z = a.z - b.z;
}

FORCEINLINE void VectorMultiply(const Vector3& a, float b, Vector3& c)
{
	c.x = a.x * b;
	c.y = a.y * b;
	c.z = a.z * b;
}

FORCEINLINE void VectorMultiply(const Vector3& a, const Vector3& b, Vector3& c)
{
	c.x = a.x * b.x;
	c.y = a.y * b.y;
	c.z = a.z * b.z;
}

FORCEINLINE void VectorDivide(const Vector3& a, float b, Vector3& c)
{
	float oob = 1.0f / b;
	c.x = a.x * oob;
	c.y = a.y * oob;
	c.z = a.z * oob;
}

FORCEINLINE void VectorDivide(const Vector3& a, const Vector3& b, Vector3& c)
{
	c.x = a.x / b.x;
	c.y = a.y / b.y;
	c.z = a.z / b.z;
}

class QAngle
{
public:
	float x;
	float y;
	float z;

	QAngle(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {}
	QAngle(float _f) : x(_f), y(_f), z(_f) {}
	QAngle() : x(0.0f), y(0.0f), z(0.0f) {}

	Vector3 GetNormal() const;

	// todo: more operators maybe
	bool operator==(const QAngle& other)
	{
		return x == other.x && y == other.y && z == other.z;
	}
};

inline Vector3 QAngle::GetNormal() const
{
	Vector3 ret(cos(DEG2RAD(y)), sin(DEG2RAD(y)), -sin(DEG2RAD(x)));
	return ret;
}