summaryrefslogtreecommitdiff
path: root/src/common/vector_math.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/vector_math.h')
-rw-r--r--src/common/vector_math.h640
1 files changed, 640 insertions, 0 deletions
diff --git a/src/common/vector_math.h b/src/common/vector_math.h
new file mode 100644
index 000000000..4928c9bf2
--- /dev/null
+++ b/src/common/vector_math.h
@@ -0,0 +1,640 @@
1// Licensed under GPLv2 or any later version
2// Refer to the license.txt file included.
3
4
5// Copyright 2014 Tony Wasserka
6// All rights reserved.
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are met:
10//
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// * Neither the name of the owner nor the names of its contributors may
17// be used to endorse or promote products derived from this software
18// without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#pragma once
33
34#include <cmath>
35
36namespace Math {
37
38template<typename T> class Vec2;
39template<typename T> class Vec3;
40template<typename T> class Vec4;
41
42template<typename T>
43static inline Vec2<T> MakeVec(const T& x, const T& y);
44template<typename T>
45static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z);
46template<typename T>
47static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w);
48
49
50template<typename T>
51class Vec2 {
52public:
53 T x;
54 T y;
55
56 T* AsArray() { return &x; }
57
58 Vec2() = default;
59 Vec2(const T a[2]) : x(a[0]), y(a[1]) {}
60 Vec2(const T& _x, const T& _y) : x(_x), y(_y) {}
61
62 template<typename T2>
63 Vec2<T2> Cast() const {
64 return Vec2<T2>((T2)x, (T2)y);
65 }
66
67 static Vec2 AssignToAll(const T& f)
68 {
69 return Vec2<T>(f, f);
70 }
71
72 void Write(T a[2])
73 {
74 a[0] = x; a[1] = y;
75 }
76
77 Vec2<decltype(T{}+T{})> operator +(const Vec2& other) const
78 {
79 return MakeVec(x+other.x, y+other.y);
80 }
81 void operator += (const Vec2 &other)
82 {
83 x+=other.x; y+=other.y;
84 }
85 Vec2<decltype(T{}-T{})> operator -(const Vec2& other) const
86 {
87 return MakeVec(x-other.x, y-other.y);
88 }
89 void operator -= (const Vec2& other)
90 {
91 x-=other.x; y-=other.y;
92 }
93 Vec2<decltype(-T{})> operator -() const
94 {
95 return MakeVec(-x,-y);
96 }
97 Vec2<decltype(T{}*T{})> operator * (const Vec2& other) const
98 {
99 return MakeVec(x*other.x, y*other.y);
100 }
101 template<typename V>
102 Vec2<decltype(T{}*V{})> operator * (const V& f) const
103 {
104 return MakeVec(x*f,y*f);
105 }
106 template<typename V>
107 void operator *= (const V& f)
108 {
109 x*=f; y*=f;
110 }
111 template<typename V>
112 Vec2<decltype(T{}/V{})> operator / (const V& f) const
113 {
114 return MakeVec(x/f,y/f);
115 }
116 template<typename V>
117 void operator /= (const V& f)
118 {
119 *this = *this / f;
120 }
121
122 T Length2() const
123 {
124 return x*x + y*y;
125 }
126
127 // Only implemented for T=float
128 float Length() const;
129 void SetLength(const float l);
130 Vec2 WithLength(const float l) const;
131 float Distance2To(Vec2 &other);
132 Vec2 Normalized() const;
133 float Normalize(); // returns the previous length, which is often useful
134
135 T& operator [] (int i) //allow vector[1] = 3 (vector.y=3)
136 {
137 return *((&x) + i);
138 }
139 T operator [] (const int i) const
140 {
141 return *((&x) + i);
142 }
143
144 void SetZero()
145 {
146 x=0; y=0;
147 }
148
149 // Common aliases: UV (texel coordinates), ST (texture coordinates)
150 T& u() { return x; }
151 T& v() { return y; }
152 T& s() { return x; }
153 T& t() { return y; }
154
155 const T& u() const { return x; }
156 const T& v() const { return y; }
157 const T& s() const { return x; }
158 const T& t() const { return y; }
159
160 // swizzlers - create a subvector of specific components
161 const Vec2 yx() const { return Vec2(y, x); }
162 const Vec2 vu() const { return Vec2(y, x); }
163 const Vec2 ts() const { return Vec2(y, x); }
164};
165
166template<typename T, typename V>
167Vec2<T> operator * (const V& f, const Vec2<T>& vec)
168{
169 return Vec2<T>(f*vec.x,f*vec.y);
170}
171
172typedef Vec2<float> Vec2f;
173
174template<typename T>
175class Vec3
176{
177public:
178 T x;
179 T y;
180 T z;
181
182 T* AsArray() { return &x; }
183
184 Vec3() = default;
185 Vec3(const T a[3]) : x(a[0]), y(a[1]), z(a[2]) {}
186 Vec3(const T& _x, const T& _y, const T& _z) : x(_x), y(_y), z(_z) {}
187
188 template<typename T2>
189 Vec3<T2> Cast() const {
190 return MakeVec<T2>((T2)x, (T2)y, (T2)z);
191 }
192
193 // Only implemented for T=int and T=float
194 static Vec3 FromRGB(unsigned int rgb);
195 unsigned int ToRGB() const; // alpha bits set to zero
196
197 static Vec3 AssignToAll(const T& f)
198 {
199 return MakeVec(f, f, f);
200 }
201
202 void Write(T a[3])
203 {
204 a[0] = x; a[1] = y; a[2] = z;
205 }
206
207 Vec3<decltype(T{}+T{})> operator +(const Vec3 &other) const
208 {
209 return MakeVec(x+other.x, y+other.y, z+other.z);
210 }
211 void operator += (const Vec3 &other)
212 {
213 x+=other.x; y+=other.y; z+=other.z;
214 }
215 Vec3<decltype(T{}-T{})> operator -(const Vec3 &other) const
216 {
217 return MakeVec(x-other.x, y-other.y, z-other.z);
218 }
219 void operator -= (const Vec3 &other)
220 {
221 x-=other.x; y-=other.y; z-=other.z;
222 }
223 Vec3<decltype(-T{})> operator -() const
224 {
225 return MakeVec(-x,-y,-z);
226 }
227 Vec3<decltype(T{}*T{})> operator * (const Vec3 &other) const
228 {
229 return MakeVec(x*other.x, y*other.y, z*other.z);
230 }
231 template<typename V>
232 Vec3<decltype(T{}*V{})> operator * (const V& f) const
233 {
234 return MakeVec(x*f,y*f,z*f);
235 }
236 template<typename V>
237 void operator *= (const V& f)
238 {
239 x*=f; y*=f; z*=f;
240 }
241 template<typename V>
242 Vec3<decltype(T{}/V{})> operator / (const V& f) const
243 {
244 return MakeVec(x/f,y/f,z/f);
245 }
246 template<typename V>
247 void operator /= (const V& f)
248 {
249 *this = *this / f;
250 }
251
252 T Length2() const
253 {
254 return x*x + y*y + z*z;
255 }
256
257 // Only implemented for T=float
258 float Length() const;
259 void SetLength(const float l);
260 Vec3 WithLength(const float l) const;
261 float Distance2To(Vec3 &other);
262 Vec3 Normalized() const;
263 float Normalize(); // returns the previous length, which is often useful
264
265 T& operator [] (int i) //allow vector[2] = 3 (vector.z=3)
266 {
267 return *((&x) + i);
268 }
269 T operator [] (const int i) const
270 {
271 return *((&x) + i);
272 }
273
274 void SetZero()
275 {
276 x=0; y=0; z=0;
277 }
278
279 // Common aliases: UVW (texel coordinates), RGB (colors), STQ (texture coordinates)
280 T& u() { return x; }
281 T& v() { return y; }
282 T& w() { return z; }
283
284 T& r() { return x; }
285 T& g() { return y; }
286 T& b() { return z; }
287
288 T& s() { return x; }
289 T& t() { return y; }
290 T& q() { return z; }
291
292 const T& u() const { return x; }
293 const T& v() const { return y; }
294 const T& w() const { return z; }
295
296 const T& r() const { return x; }
297 const T& g() const { return y; }
298 const T& b() const { return z; }
299
300 const T& s() const { return x; }
301 const T& t() const { return y; }
302 const T& q() const { return z; }
303
304 // swizzlers - create a subvector of specific components
305 // e.g. Vec2 uv() { return Vec2(x,y); }
306 // _DEFINE_SWIZZLER2 defines a single such function, DEFINE_SWIZZLER2 defines all of them for all component names (x<->r) and permutations (xy<->yx)
307#define _DEFINE_SWIZZLER2(a, b, name) const Vec2<T> name() const { return Vec2<T>(a, b); }
308#define DEFINE_SWIZZLER2(a, b, a2, b2, a3, b3, a4, b4) \
309 _DEFINE_SWIZZLER2(a, b, a##b); \
310 _DEFINE_SWIZZLER2(a, b, a2##b2); \
311 _DEFINE_SWIZZLER2(a, b, a3##b3); \
312 _DEFINE_SWIZZLER2(a, b, a4##b4); \
313 _DEFINE_SWIZZLER2(b, a, b##a); \
314 _DEFINE_SWIZZLER2(b, a, b2##a2); \
315 _DEFINE_SWIZZLER2(b, a, b3##a3); \
316 _DEFINE_SWIZZLER2(b, a, b4##a4)
317
318 DEFINE_SWIZZLER2(x, y, r, g, u, v, s, t);
319 DEFINE_SWIZZLER2(x, z, r, b, u, w, s, q);
320 DEFINE_SWIZZLER2(y, z, g, b, v, w, t, q);
321#undef DEFINE_SWIZZLER2
322#undef _DEFINE_SWIZZLER2
323};
324
325template<typename T, typename V>
326Vec3<T> operator * (const V& f, const Vec3<T>& vec)
327{
328 return Vec3<T>(f*vec.x,f*vec.y,f*vec.z);
329}
330
331template<>
332inline float Vec3<float>::Length() const {
333 return std::sqrt(x * x + y * y + z * z);
334}
335
336template<>
337inline Vec3<float> Vec3<float>::Normalized() const {
338 return *this / Length();
339}
340
341
342typedef Vec3<float> Vec3f;
343
344template<typename T>
345class Vec4
346{
347public:
348 T x;
349 T y;
350 T z;
351 T w;
352
353 T* AsArray() { return &x; }
354
355 Vec4() = default;
356 Vec4(const T a[4]) : x(a[0]), y(a[1]), z(a[2]), w(a[3]) {}
357 Vec4(const T& _x, const T& _y, const T& _z, const T& _w) : x(_x), y(_y), z(_z), w(_w) {}
358
359 template<typename T2>
360 Vec4<T2> Cast() const {
361 return Vec4<T2>((T2)x, (T2)y, (T2)z, (T2)w);
362 }
363
364 // Only implemented for T=int and T=float
365 static Vec4 FromRGBA(unsigned int rgba);
366 unsigned int ToRGBA() const;
367
368 static Vec4 AssignToAll(const T& f) {
369 return Vec4<T>(f, f, f, f);
370 }
371
372 void Write(T a[4])
373 {
374 a[0] = x; a[1] = y; a[2] = z; a[3] = w;
375 }
376
377 Vec4<decltype(T{}+T{})> operator +(const Vec4& other) const
378 {
379 return MakeVec(x+other.x, y+other.y, z+other.z, w+other.w);
380 }
381 void operator += (const Vec4& other)
382 {
383 x+=other.x; y+=other.y; z+=other.z; w+=other.w;
384 }
385 Vec4<decltype(T{}-T{})> operator -(const Vec4 &other) const
386 {
387 return MakeVec(x-other.x, y-other.y, z-other.z, w-other.w);
388 }
389 void operator -= (const Vec4 &other)
390 {
391 x-=other.x; y-=other.y; z-=other.z; w-=other.w;
392 }
393 Vec4<decltype(-T{})> operator -() const
394 {
395 return MakeVec(-x,-y,-z,-w);
396 }
397 Vec4<decltype(T{}*T{})> operator * (const Vec4 &other) const
398 {
399 return MakeVec(x*other.x, y*other.y, z*other.z, w*other.w);
400 }
401 template<typename V>
402 Vec4<decltype(T{}*V{})> operator * (const V& f) const
403 {
404 return MakeVec(x*f,y*f,z*f,w*f);
405 }
406 template<typename V>
407 void operator *= (const V& f)
408 {
409 x*=f; y*=f; z*=f; w*=f;
410 }
411 template<typename V>
412 Vec4<decltype(T{}/V{})> operator / (const V& f) const
413 {
414 return MakeVec(x/f,y/f,z/f,w/f);
415 }
416 template<typename V>
417 void operator /= (const V& f)
418 {
419 *this = *this / f;
420 }
421
422 T Length2() const
423 {
424 return x*x + y*y + z*z + w*w;
425 }
426
427 // Only implemented for T=float
428 float Length() const;
429 void SetLength(const float l);
430 Vec4 WithLength(const float l) const;
431 float Distance2To(Vec4 &other);
432 Vec4 Normalized() const;
433 float Normalize(); // returns the previous length, which is often useful
434
435 T& operator [] (int i) //allow vector[2] = 3 (vector.z=3)
436 {
437 return *((&x) + i);
438 }
439 T operator [] (const int i) const
440 {
441 return *((&x) + i);
442 }
443
444 void SetZero()
445 {
446 x=0; y=0; z=0;
447 }
448
449 // Common alias: RGBA (colors)
450 T& r() { return x; }
451 T& g() { return y; }
452 T& b() { return z; }
453 T& a() { return w; }
454
455 const T& r() const { return x; }
456 const T& g() const { return y; }
457 const T& b() const { return z; }
458 const T& a() const { return w; }
459
460 // Swizzlers - Create a subvector of specific components
461 // e.g. Vec2 uv() { return Vec2(x,y); }
462
463 // _DEFINE_SWIZZLER2 defines a single such function
464 // DEFINE_SWIZZLER2_COMP1 defines one-component functions for all component names (x<->r)
465 // DEFINE_SWIZZLER2_COMP2 defines two component functions for all component names (x<->r) and permutations (xy<->yx)
466#define _DEFINE_SWIZZLER2(a, b, name) const Vec2<T> name() const { return Vec2<T>(a, b); }
467#define DEFINE_SWIZZLER2_COMP1(a, a2) \
468 _DEFINE_SWIZZLER2(a, a, a##a); \
469 _DEFINE_SWIZZLER2(a, a, a2##a2)
470#define DEFINE_SWIZZLER2_COMP2(a, b, a2, b2) \
471 _DEFINE_SWIZZLER2(a, b, a##b); \
472 _DEFINE_SWIZZLER2(a, b, a2##b2); \
473 _DEFINE_SWIZZLER2(b, a, b##a); \
474 _DEFINE_SWIZZLER2(b, a, b2##a2)
475
476 DEFINE_SWIZZLER2_COMP2(x, y, r, g);
477 DEFINE_SWIZZLER2_COMP2(x, z, r, b);
478 DEFINE_SWIZZLER2_COMP2(x, w, r, a);
479 DEFINE_SWIZZLER2_COMP2(y, z, g, b);
480 DEFINE_SWIZZLER2_COMP2(y, w, g, a);
481 DEFINE_SWIZZLER2_COMP2(z, w, b, a);
482 DEFINE_SWIZZLER2_COMP1(x, r);
483 DEFINE_SWIZZLER2_COMP1(y, g);
484 DEFINE_SWIZZLER2_COMP1(z, b);
485 DEFINE_SWIZZLER2_COMP1(w, a);
486#undef DEFINE_SWIZZLER2_COMP1
487#undef DEFINE_SWIZZLER2_COMP2
488#undef _DEFINE_SWIZZLER2
489
490#define _DEFINE_SWIZZLER3(a, b, c, name) const Vec3<T> name() const { return Vec3<T>(a, b, c); }
491#define DEFINE_SWIZZLER3_COMP1(a, a2) \
492 _DEFINE_SWIZZLER3(a, a, a, a##a##a); \
493 _DEFINE_SWIZZLER3(a, a, a, a2##a2##a2)
494#define DEFINE_SWIZZLER3_COMP3(a, b, c, a2, b2, c2) \
495 _DEFINE_SWIZZLER3(a, b, c, a##b##c); \
496 _DEFINE_SWIZZLER3(a, c, b, a##c##b); \
497 _DEFINE_SWIZZLER3(b, a, c, b##a##c); \
498 _DEFINE_SWIZZLER3(b, c, a, b##c##a); \
499 _DEFINE_SWIZZLER3(c, a, b, c##a##b); \
500 _DEFINE_SWIZZLER3(c, b, a, c##b##a); \
501 _DEFINE_SWIZZLER3(a, b, c, a2##b2##c2); \
502 _DEFINE_SWIZZLER3(a, c, b, a2##c2##b2); \
503 _DEFINE_SWIZZLER3(b, a, c, b2##a2##c2); \
504 _DEFINE_SWIZZLER3(b, c, a, b2##c2##a2); \
505 _DEFINE_SWIZZLER3(c, a, b, c2##a2##b2); \
506 _DEFINE_SWIZZLER3(c, b, a, c2##b2##a2)
507
508 DEFINE_SWIZZLER3_COMP3(x, y, z, r, g, b);
509 DEFINE_SWIZZLER3_COMP3(x, y, w, r, g, a);
510 DEFINE_SWIZZLER3_COMP3(x, z, w, r, b, a);
511 DEFINE_SWIZZLER3_COMP3(y, z, w, g, b, a);
512 DEFINE_SWIZZLER3_COMP1(x, r);
513 DEFINE_SWIZZLER3_COMP1(y, g);
514 DEFINE_SWIZZLER3_COMP1(z, b);
515 DEFINE_SWIZZLER3_COMP1(w, a);
516#undef DEFINE_SWIZZLER3_COMP1
517#undef DEFINE_SWIZZLER3_COMP3
518#undef _DEFINE_SWIZZLER3
519};
520
521
522template<typename T, typename V>
523Vec4<decltype(V{}*T{})> operator * (const V& f, const Vec4<T>& vec)
524{
525 return MakeVec(f*vec.x,f*vec.y,f*vec.z,f*vec.w);
526}
527
528typedef Vec4<float> Vec4f;
529
530
531template<typename T>
532static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec2<T>& a, const Vec2<T>& b)
533{
534 return a.x*b.x + a.y*b.y;
535}
536
537template<typename T>
538static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec3<T>& a, const Vec3<T>& b)
539{
540 return a.x*b.x + a.y*b.y + a.z*b.z;
541}
542
543template<typename T>
544static inline decltype(T{}*T{}+T{}*T{}) Dot(const Vec4<T>& a, const Vec4<T>& b)
545{
546 return a.x*b.x + a.y*b.y + a.z*b.z + a.w*b.w;
547}
548
549template<typename T>
550static inline Vec3<decltype(T{}*T{}-T{}*T{})> Cross(const Vec3<T>& a, const Vec3<T>& b)
551{
552 return MakeVec(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
553}
554
555// linear interpolation via float: 0.0=begin, 1.0=end
556template<typename X>
557static inline decltype(X{}*float{}+X{}*float{}) Lerp(const X& begin, const X& end, const float t)
558{
559 return begin*(1.f-t) + end*t;
560}
561
562// linear interpolation via int: 0=begin, base=end
563template<typename X, int base>
564static inline decltype((X{}*int{}+X{}*int{}) / base) LerpInt(const X& begin, const X& end, const int t)
565{
566 return (begin*(base-t) + end*t) / base;
567}
568
569// Utility vector factories
570template<typename T>
571static inline Vec2<T> MakeVec(const T& x, const T& y)
572{
573 return Vec2<T>{x, y};
574}
575
576template<typename T>
577static inline Vec3<T> MakeVec(const T& x, const T& y, const T& z)
578{
579 return Vec3<T>{x, y, z};
580}
581
582template<typename T>
583static inline Vec4<T> MakeVec(const T& x, const T& y, const Vec2<T>& zw)
584{
585 return MakeVec(x, y, zw[0], zw[1]);
586}
587
588template<typename T>
589static inline Vec3<T> MakeVec(const Vec2<T>& xy, const T& z)
590{
591 return MakeVec(xy[0], xy[1], z);
592}
593
594template<typename T>
595static inline Vec3<T> MakeVec(const T& x, const Vec2<T>& yz)
596{
597 return MakeVec(x, yz[0], yz[1]);
598}
599
600template<typename T>
601static inline Vec4<T> MakeVec(const T& x, const T& y, const T& z, const T& w)
602{
603 return Vec4<T>{x, y, z, w};
604}
605
606template<typename T>
607static inline Vec4<T> MakeVec(const Vec2<T>& xy, const T& z, const T& w)
608{
609 return MakeVec(xy[0], xy[1], z, w);
610}
611
612template<typename T>
613static inline Vec4<T> MakeVec(const T& x, const Vec2<T>& yz, const T& w)
614{
615 return MakeVec(x, yz[0], yz[1], w);
616}
617
618// NOTE: This has priority over "Vec2<Vec2<T>> MakeVec(const Vec2<T>& x, const Vec2<T>& y)".
619// Even if someone wanted to use an odd object like Vec2<Vec2<T>>, the compiler would error
620// out soon enough due to misuse of the returned structure.
621template<typename T>
622static inline Vec4<T> MakeVec(const Vec2<T>& xy, const Vec2<T>& zw)
623{
624 return MakeVec(xy[0], xy[1], zw[0], zw[1]);
625}
626
627template<typename T>
628static inline Vec4<T> MakeVec(const Vec3<T>& xyz, const T& w)
629{
630 return MakeVec(xyz[0], xyz[1], xyz[2], w);
631}
632
633template<typename T>
634static inline Vec4<T> MakeVec(const T& x, const Vec3<T>& yzw)
635{
636 return MakeVec(x, yzw[0], yzw[1], yzw[2]);
637}
638
639
640} // namespace