DDraceNetwork Documentation
Loading...
Searching...
No Matches
vmath.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef BASE_VMATH_H
4#define BASE_VMATH_H
5
6#include <cmath>
7#include <cstdint>
8
9#include "math.h"
10
11// ------------------------------------
12
13template<Numeric T>
15{
16public:
17 union
18 {
19 T x, u;
20 };
21 union
22 {
23 T y, v;
24 };
25
26 constexpr vector2_base() = default;
27 constexpr vector2_base(T nx, T ny) :
28 x(nx), y(ny)
29 {
30 }
31
32 constexpr vector2_base operator-() const { return vector2_base(-x, -y); }
33 constexpr vector2_base operator-(const vector2_base &vec) const { return vector2_base(x - vec.x, y - vec.y); }
34 constexpr vector2_base operator+(const vector2_base &vec) const { return vector2_base(x + vec.x, y + vec.y); }
35 constexpr vector2_base operator*(const T rhs) const { return vector2_base(x * rhs, y * rhs); }
36 constexpr vector2_base operator*(const vector2_base &vec) const { return vector2_base(x * vec.x, y * vec.y); }
37 constexpr vector2_base operator/(const T rhs) const { return vector2_base(x / rhs, y / rhs); }
38 constexpr vector2_base operator/(const vector2_base &vec) const { return vector2_base(x / vec.x, y / vec.y); }
39
40 constexpr vector2_base &operator+=(const vector2_base &vec)
41 {
42 x += vec.x;
43 y += vec.y;
44 return *this;
45 }
46 constexpr vector2_base &operator-=(const vector2_base &vec)
47 {
48 x -= vec.x;
49 y -= vec.y;
50 return *this;
51 }
52 constexpr vector2_base &operator*=(const T rhs)
53 {
54 x *= rhs;
55 y *= rhs;
56 return *this;
57 }
58 constexpr vector2_base &operator*=(const vector2_base &vec)
59 {
60 x *= vec.x;
61 y *= vec.y;
62 return *this;
63 }
64 constexpr vector2_base &operator/=(const T rhs)
65 {
66 x /= rhs;
67 y /= rhs;
68 return *this;
69 }
70 constexpr vector2_base &operator/=(const vector2_base &vec)
71 {
72 x /= vec.x;
73 y /= vec.y;
74 return *this;
75 }
76
77 constexpr bool operator==(const vector2_base &vec) const { return x == vec.x && y == vec.y; } // TODO: do this with an eps instead
78 constexpr bool operator!=(const vector2_base &vec) const { return x != vec.x || y != vec.y; }
79
80 constexpr T &operator[](const int index) { return index ? y : x; }
81 constexpr const T &operator[](const int index) const { return index ? y : x; }
82};
83
84template<Numeric T>
85constexpr inline vector2_base<T> rotate(const vector2_base<T> &a, float angle)
86{
87 angle = angle * pi / 180.0f;
88 float s = std::sin(angle);
89 float c = std::cos(angle);
90 return vector2_base<T>(static_cast<T>(c * a.x - s * a.y), static_cast<T>(s * a.x + c * a.y));
91}
92
93template<Numeric T>
94inline T distance(const vector2_base<T> a, const vector2_base<T> &b)
95{
96 return length(a - b);
97}
98
99template<Numeric T>
100constexpr inline T dot(const vector2_base<T> a, const vector2_base<T> &b)
101{
102 return a.x * b.x + a.y * b.y;
103}
104
105template<std::floating_point T>
106inline float length(const vector2_base<T> &a)
107{
108 return std::sqrt(dot(a, a));
109}
110
111template<std::integral T>
112inline float length(const vector2_base<T> &a)
113{
114 return std::sqrt(static_cast<float>(dot(a, a)));
115}
116
117constexpr inline float length_squared(const vector2_base<float> &a)
118{
119 return dot(a, a);
120}
121
122constexpr inline float angle(const vector2_base<float> &a)
123{
124 if(a.x == 0 && a.y == 0)
125 return 0.0f;
126 else if(a.x == 0)
127 return a.y < 0 ? -pi / 2 : pi / 2;
128 float result = std::atan(a.y / a.x);
129 if(a.x < 0)
130 result = result + pi;
131 return result;
132}
133
134template<Numeric T>
136{
137 if(len == 0)
138 return vector2_base<T>();
139 return vector2_base<T>(v.x / len, v.y / len);
140}
141
143{
144 float divisor = length(v);
145 if(divisor == 0.0f)
146 return vector2_base<float>(0.0f, 0.0f);
147 float l = (float)(1.0f / divisor);
148 return vector2_base<float>(v.x * l, v.y * l);
149}
150
152{
153 return vector2_base<float>(std::cos(angle), std::sin(angle));
154}
155
160
164
165template<Numeric T>
167{
170 if(SquaredMagnitudeAB > 0)
171 {
173 T APdotAB = dot(AP, AB);
175 out_pos = line_pointA + AB * std::clamp(t, (T)0, (T)1);
176 return true;
177 }
178 else
179 return false;
180}
181
182// ------------------------------------
183template<Numeric T>
185{
186public:
187 union
188 {
189 T x, r, h, u;
190 };
191 union
192 {
193 T y, g, s, v;
194 };
195 union
196 {
197 T z, b, l, w;
198 };
199
200 constexpr vector3_base() = default;
201 constexpr vector3_base(T nx, T ny, T nz) :
202 x(nx), y(ny), z(nz)
203 {
204 }
205
206 constexpr vector3_base operator-(const vector3_base &vec) const { return vector3_base(x - vec.x, y - vec.y, z - vec.z); }
207 constexpr vector3_base operator-() const { return vector3_base(-x, -y, -z); }
208 constexpr vector3_base operator+(const vector3_base &vec) const { return vector3_base(x + vec.x, y + vec.y, z + vec.z); }
209 constexpr vector3_base operator*(const T rhs) const { return vector3_base(x * rhs, y * rhs, z * rhs); }
210 constexpr vector3_base operator*(const vector3_base &vec) const { return vector3_base(x * vec.x, y * vec.y, z * vec.z); }
211 constexpr vector3_base operator/(const T rhs) const { return vector3_base(x / rhs, y / rhs, z / rhs); }
212 constexpr vector3_base operator/(const vector3_base &vec) const { return vector3_base(x / vec.x, y / vec.y, z / vec.z); }
213
215 {
216 x += vec.x;
217 y += vec.y;
218 z += vec.z;
219 return *this;
220 }
222 {
223 x -= vec.x;
224 y -= vec.y;
225 z -= vec.z;
226 return *this;
227 }
228 constexpr vector3_base &operator*=(const T rhs)
229 {
230 x *= rhs;
231 y *= rhs;
232 z *= rhs;
233 return *this;
234 }
236 {
237 x *= vec.x;
238 y *= vec.y;
239 z *= vec.z;
240 return *this;
241 }
242 constexpr vector3_base &operator/=(const T rhs)
243 {
244 x /= rhs;
245 y /= rhs;
246 z /= rhs;
247 return *this;
248 }
250 {
251 x /= vec.x;
252 y /= vec.y;
253 z /= vec.z;
254 return *this;
255 }
256
257 constexpr bool operator==(const vector3_base &vec) const { return x == vec.x && y == vec.y && z == vec.z; } // TODO: do this with an eps instead
258 constexpr bool operator!=(const vector3_base &vec) const { return x != vec.x || y != vec.y || z != vec.z; }
259};
260
261template<Numeric T>
262inline T distance(const vector3_base<T> &a, const vector3_base<T> &b)
263{
264 return length(a - b);
265}
266
267template<Numeric T>
268constexpr inline T dot(const vector3_base<T> &a, const vector3_base<T> &b)
269{
270 return a.x * b.x + a.y * b.y + a.z * b.z;
271}
272
273template<Numeric T>
274constexpr inline vector3_base<T> cross(const vector3_base<T> &a, const vector3_base<T> &b)
275{
276 return vector3_base<T>(
277 a.y * b.z - a.z * b.y,
278 a.z * b.x - a.x * b.z,
279 a.x * b.y - a.y * b.x);
280}
281
282//
283inline float length(const vector3_base<float> &a)
284{
285 return std::sqrt(dot(a, a));
286}
287
289{
290 float divisor = length(v);
291 if(divisor == 0.0f)
292 return vector3_base<float>(0.0f, 0.0f, 0.0f);
293 float l = (float)(1.0f / divisor);
294 return vector3_base<float>(v.x * l, v.y * l, v.z * l);
295}
296
300
301// ------------------------------------
302
303template<Numeric T>
305{
306public:
307 union
308 {
309 T x, r, h;
310 };
311 union
312 {
313 T y, g, s;
314 };
315 union
316 {
317 T z, b, l;
318 };
319 union
320 {
321 T w, a;
322 };
323
324 constexpr vector4_base() = default;
325 constexpr vector4_base(T nx, T ny, T nz, T nw) :
326 x(nx), y(ny), z(nz), w(nw)
327 {
328 }
329
330 constexpr vector4_base operator+(const vector4_base &vec) const { return vector4_base(x + vec.x, y + vec.y, z + vec.z, w + vec.w); }
331 constexpr vector4_base operator-(const vector4_base &vec) const { return vector4_base(x - vec.x, y - vec.y, z - vec.z, w - vec.w); }
332 constexpr vector4_base operator-() const { return vector4_base(-x, -y, -z, -w); }
333 constexpr vector4_base operator*(const vector4_base &vec) const { return vector4_base(x * vec.x, y * vec.y, z * vec.z, w * vec.w); }
334 constexpr vector4_base operator*(const T rhs) const { return vector4_base(x * rhs, y * rhs, z * rhs, w * rhs); }
335 constexpr vector4_base operator/(const vector4_base &vec) const { return vector4_base(x / vec.x, y / vec.y, z / vec.z, w / vec.w); }
336 constexpr vector4_base operator/(const T vec) const { return vector4_base(x / vec, y / vec, z / vec, w / vec); }
337
339 {
340 x += vec.x;
341 y += vec.y;
342 z += vec.z;
343 w += vec.w;
344 return *this;
345 }
347 {
348 x -= vec.x;
349 y -= vec.y;
350 z -= vec.z;
351 w -= vec.w;
352 return *this;
353 }
354 constexpr vector4_base &operator*=(const T rhs)
355 {
356 x *= rhs;
357 y *= rhs;
358 z *= rhs;
359 w *= rhs;
360 return *this;
361 }
363 {
364 x *= vec.x;
365 y *= vec.y;
366 z *= vec.z;
367 w *= vec.w;
368 return *this;
369 }
370 constexpr vector4_base &operator/=(const T rhs)
371 {
372 x /= rhs;
373 y /= rhs;
374 z /= rhs;
375 w /= rhs;
376 return *this;
377 }
379 {
380 x /= vec.x;
381 y /= vec.y;
382 z /= vec.z;
383 w /= vec.w;
384 return *this;
385 }
386
387 constexpr bool operator==(const vector4_base &vec) const { return x == vec.x && y == vec.y && z == vec.z && w == vec.w; } // TODO: do this with an eps instead
388 constexpr bool operator!=(const vector4_base &vec) const { return x != vec.x || y != vec.y || z != vec.z || w != vec.w; }
389};
390
395
396#endif
Definition vmath.h:15
T x
Definition vmath.h:19
constexpr bool operator!=(const vector2_base &vec) const
Definition vmath.h:78
constexpr vector2_base operator*(const T rhs) const
Definition vmath.h:35
constexpr vector2_base operator*(const vector2_base &vec) const
Definition vmath.h:36
constexpr vector2_base operator+(const vector2_base &vec) const
Definition vmath.h:34
constexpr vector2_base(T nx, T ny)
Definition vmath.h:27
constexpr vector2_base operator/(const vector2_base &vec) const
Definition vmath.h:38
T y
Definition vmath.h:23
constexpr vector2_base operator-() const
Definition vmath.h:32
constexpr const T & operator[](const int index) const
Definition vmath.h:81
T u
Definition vmath.h:19
constexpr vector2_base & operator/=(const T rhs)
Definition vmath.h:64
constexpr vector2_base & operator*=(const vector2_base &vec)
Definition vmath.h:58
constexpr vector2_base & operator-=(const vector2_base &vec)
Definition vmath.h:46
constexpr bool operator==(const vector2_base &vec) const
Definition vmath.h:77
constexpr vector2_base()=default
constexpr vector2_base & operator*=(const T rhs)
Definition vmath.h:52
constexpr vector2_base operator-(const vector2_base &vec) const
Definition vmath.h:33
constexpr vector2_base operator/(const T rhs) const
Definition vmath.h:37
constexpr vector2_base & operator/=(const vector2_base &vec)
Definition vmath.h:70
T v
Definition vmath.h:23
constexpr T & operator[](const int index)
Definition vmath.h:80
constexpr vector2_base & operator+=(const vector2_base &vec)
Definition vmath.h:40
Definition vmath.h:185
T z
Definition vmath.h:197
constexpr vector3_base()=default
constexpr vector3_base & operator+=(const vector3_base &vec)
Definition vmath.h:214
constexpr vector3_base operator*(const T rhs) const
Definition vmath.h:209
constexpr vector3_base operator-(const vector3_base &vec) const
Definition vmath.h:206
T x
Definition vmath.h:189
constexpr vector3_base & operator/=(const T rhs)
Definition vmath.h:242
constexpr bool operator==(const vector3_base &vec) const
Definition vmath.h:257
constexpr vector3_base & operator-=(const vector3_base &vec)
Definition vmath.h:221
T y
Definition vmath.h:193
constexpr vector3_base & operator/=(const vector3_base &vec)
Definition vmath.h:249
T s
Definition vmath.h:193
T v
Definition vmath.h:193
T r
Definition vmath.h:189
constexpr vector3_base operator/(const T rhs) const
Definition vmath.h:211
T g
Definition vmath.h:193
constexpr vector3_base operator/(const vector3_base &vec) const
Definition vmath.h:212
constexpr vector3_base & operator*=(const T rhs)
Definition vmath.h:228
T u
Definition vmath.h:189
constexpr bool operator!=(const vector3_base &vec) const
Definition vmath.h:258
constexpr vector3_base operator+(const vector3_base &vec) const
Definition vmath.h:208
constexpr vector3_base operator*(const vector3_base &vec) const
Definition vmath.h:210
T w
Definition vmath.h:197
T b
Definition vmath.h:197
constexpr vector3_base & operator*=(const vector3_base &vec)
Definition vmath.h:235
T h
Definition vmath.h:189
constexpr vector3_base operator-() const
Definition vmath.h:207
T l
Definition vmath.h:197
constexpr vector3_base(T nx, T ny, T nz)
Definition vmath.h:201
Definition vmath.h:305
T g
Definition vmath.h:313
constexpr vector4_base & operator/=(const vector4_base &vec)
Definition vmath.h:378
T w
Definition vmath.h:321
constexpr bool operator!=(const vector4_base &vec) const
Definition vmath.h:388
T b
Definition vmath.h:317
T s
Definition vmath.h:313
T r
Definition vmath.h:309
T h
Definition vmath.h:309
constexpr vector4_base operator+(const vector4_base &vec) const
Definition vmath.h:330
T z
Definition vmath.h:317
constexpr vector4_base operator/(const vector4_base &vec) const
Definition vmath.h:335
constexpr vector4_base & operator/=(const T rhs)
Definition vmath.h:370
constexpr vector4_base operator-(const vector4_base &vec) const
Definition vmath.h:331
constexpr vector4_base(T nx, T ny, T nz, T nw)
Definition vmath.h:325
constexpr vector4_base & operator*=(const vector4_base &vec)
Definition vmath.h:362
T y
Definition vmath.h:313
T l
Definition vmath.h:317
constexpr vector4_base & operator*=(const T rhs)
Definition vmath.h:354
constexpr vector4_base()=default
constexpr vector4_base operator*(const T rhs) const
Definition vmath.h:334
T a
Definition vmath.h:321
constexpr vector4_base operator/(const T vec) const
Definition vmath.h:336
constexpr vector4_base & operator+=(const vector4_base &vec)
Definition vmath.h:338
constexpr vector4_base & operator-=(const vector4_base &vec)
Definition vmath.h:346
constexpr vector4_base operator*(const vector4_base &vec) const
Definition vmath.h:333
T x
Definition vmath.h:309
constexpr vector4_base operator-() const
Definition vmath.h:332
constexpr bool operator==(const vector4_base &vec) const
Definition vmath.h:387
static SHA256_DIGEST s(const char *pSha256)
Definition mapbugs.cpp:37
constexpr float pi
Definition math.h:14
float random_angle()
Definition math.h:76
vector4_base< int > ivec4
Definition vmath.h:393
constexpr float angle(const vector2_base< float > &a)
Definition vmath.h:122
vector2_base< float > normalize(const vector2_base< float > &v)
Definition vmath.h:142
vector4_base< uint8_t > ubvec4
Definition vmath.h:394
constexpr float length_squared(const vector2_base< float > &a)
Definition vmath.h:117
constexpr vector3_base< T > cross(const vector3_base< T > &a, const vector3_base< T > &b)
Definition vmath.h:274
vector2_base< float > direction(float angle)
Definition vmath.h:151
float length(const vector2_base< T > &a)
Definition vmath.h:106
vector4_base< bool > bvec4
Definition vmath.h:392
vector3_base< float > vec3
Definition vmath.h:297
vector4_base< float > vec4
Definition vmath.h:391
vector2_base< int > ivec2
Definition vmath.h:163
vector3_base< int > ivec3
Definition vmath.h:299
vector2_base< bool > bvec2
Definition vmath.h:162
vector2_base< float > random_direction()
Definition vmath.h:156
vector3_base< bool > bvec3
Definition vmath.h:298
constexpr vector2_base< T > rotate(const vector2_base< T > &a, float angle)
Definition vmath.h:85
vector2_base< float > vec2
Definition vmath.h:161
constexpr vector2_base< T > normalize_pre_length(const vector2_base< T > &v, T len)
Definition vmath.h:135
constexpr bool closest_point_on_line(vector2_base< T > line_pointA, vector2_base< T > line_pointB, vector2_base< T > target_point, vector2_base< T > &out_pos)
Definition vmath.h:166
constexpr T dot(const vector2_base< T > a, const vector2_base< T > &b)
Definition vmath.h:100
T distance(const vector2_base< T > a, const vector2_base< T > &b)
Definition vmath.h:94