Merge pull request #832 from GreYFoX/oypull1
[twcon.git] / src / base / vmath.h
blob3461adf89eda16cd25b3715a4f17d8d01255a056
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
6 #include <math.h>
8 // ------------------------------------
10 template<typename T>
11 class vector2_base
13 public:
14 union { T x,u; };
15 union { T y,v; };
17 vector2_base() {}
18 vector2_base(float nx, float ny)
20 x = nx;
21 y = ny;
24 vector2_base operator -() const { return vector2_base(-x, -y); }
25 vector2_base operator -(const vector2_base &v) const { return vector2_base(x-v.x, y-v.y); }
26 vector2_base operator +(const vector2_base &v) const { return vector2_base(x+v.x, y+v.y); }
27 vector2_base operator *(const T v) const { return vector2_base(x*v, y*v); }
29 const vector2_base &operator =(const vector2_base &v) { x = v.x; y = v.y; return *this; }
31 const vector2_base &operator +=(const vector2_base &v) { x += v.x; y += v.y; return *this; }
32 const vector2_base &operator -=(const vector2_base &v) { x -= v.x; y -= v.y; return *this; }
33 const vector2_base &operator *=(const T v) { x *= v; y *= v; return *this; }
35 bool operator ==(const vector2_base &v) const { return x == v.x && y == v.y; } //TODO: do this with an eps instead
37 operator const T* () { return &x; }
41 template<typename T>
42 inline T length(const vector2_base<T> &a)
44 return sqrtf(a.x*a.x + a.y*a.y);
47 template<typename T>
48 inline T distance(const vector2_base<T> a, const vector2_base<T> &b)
50 return length(a-b);
53 template<typename T>
54 inline T dot(const vector2_base<T> a, const vector2_base<T> &b)
56 return a.x*b.x + a.y*b.y;
59 template<typename T>
60 inline vector2_base<T> normalize(const vector2_base<T> &v)
62 T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y));
63 return vector2_base<T>(v.x*l, v.y*l);
66 typedef vector2_base<float> vec2;
67 typedef vector2_base<bool> bvec2;
68 typedef vector2_base<int> ivec2;
70 template<typename T>
71 inline vector2_base<T> closest_point_on_line(vector2_base<T> line_point0, vector2_base<T> line_point1, vector2_base<T> target_point)
73 vector2_base<T> c = target_point - line_point0;
74 vector2_base<T> v = (line_point1 - line_point0);
75 v = normalize(v);
76 T d = length(line_point0-line_point1);
77 T t = dot(v, c)/d;
78 return mix(line_point0, line_point1, clamp(t, (T)0, (T)1));
80 if (t < 0) t = 0;
81 if (t > 1.0f) return 1.0f;
82 return t;*/
85 // ------------------------------------
86 template<typename T>
87 class vector3_base
89 public:
90 union { T x,r,h; };
91 union { T y,g,s; };
92 union { T z,b,v,l; };
94 vector3_base() {}
95 vector3_base(float nx, float ny, float nz)
97 x = nx;
98 y = ny;
99 z = nz;
102 const vector3_base &operator =(const vector3_base &v) { x = v.x; y = v.y; z = v.z; return *this; }
104 vector3_base operator -(const vector3_base &v) const { return vector3_base(x-v.x, y-v.y, z-v.z); }
105 vector3_base operator -() const { return vector3_base(-x, -y, -z); }
106 vector3_base operator +(const vector3_base &v) const { return vector3_base(x+v.x, y+v.y, z+v.z); }
107 vector3_base operator *(const T v) const { return vector3_base(x*v, y*v, z*v); }
108 vector3_base operator *(const vector3_base &v) const { return vector3_base(x*v.x, y*v.y, z*v.z); }
109 vector3_base operator /(const T v) const { return vector3_base(x/v, y/v, z/v); }
111 const vector3_base &operator +=(const vector3_base &v) { x += v.x; y += v.y; z += v.z; return *this; }
112 const vector3_base &operator -=(const vector3_base &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
113 const vector3_base &operator *=(const T v) { x *= v; y *= v; z *= v; return *this; }
115 bool operator ==(const vector3_base &v) const { return x == v.x && y == v.y && z == v.z; } //TODO: do this with an eps instead
117 operator const T* () { return &x; }
120 template<typename T>
121 inline T length(const vector3_base<T> &a)
123 return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z);
126 template<typename T>
127 inline T distance(const vector3_base<T> &a, const vector3_base<T> &b)
129 return length(a-b);
132 template<typename T>
133 inline T dot(const vector3_base<T> &a, const vector3_base<T> &b)
135 return a.x*b.x + a.y*b.y + a.z*b.z;
138 template<typename T>
139 inline vector3_base<T> normalize(const vector3_base<T> &v)
141 T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y + v.z*v.z));
142 return vector3_base<T>(v.x*l, v.y*l, v.z*l);
145 template<typename T>
146 inline vector3_base<T> cross(const vector3_base<T> &a, const vector3_base<T> &b)
148 return vector3_base<T>(
149 a.y*b.z - a.z*b.y,
150 a.z*b.x - a.x*b.z,
151 a.x*b.y - a.y*b.x);
154 typedef vector3_base<float> vec3;
155 typedef vector3_base<bool> bvec3;
156 typedef vector3_base<int> ivec3;
158 // ------------------------------------
160 template<typename T>
161 class vector4_base
163 public:
164 union { T x,r; };
165 union { T y,g; };
166 union { T z,b; };
167 union { T w,a; };
169 vector4_base() {}
170 vector4_base(float nx, float ny, float nz, float nw)
172 x = nx;
173 y = ny;
174 z = nz;
175 w = nw;
178 vector4_base operator +(const vector4_base &v) const { return vector4_base(x+v.x, y+v.y, z+v.z, w+v.w); }
179 vector4_base operator -(const vector4_base &v) const { return vector4_base(x-v.x, y-v.y, z-v.z, w-v.w); }
180 vector4_base operator -() const { return vector4_base(-x, -y, -z, -w); }
181 vector4_base operator *(const vector4_base &v) const { return vector4_base(x*v.x, y*v.y, z*v.z, w*v.w); }
182 vector4_base operator *(const T v) const { return vector4_base(x*v, y*v, z*v, w*v); }
184 const vector4_base &operator =(const vector4_base &v) { x = v.x; y = v.y; z = v.z; w = v.w; return *this; }
186 const vector4_base &operator +=(const vector4_base &v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; }
187 const vector4_base &operator -=(const vector4_base &v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; }
188 const vector4_base &operator *=(const T v) { x *= v; y *= v; z *= v; w *= v; return *this; }
190 bool operator ==(const vector4_base &v) const { return x == v.x && y == v.y && z == v.z && w == v.w; } //TODO: do this with an eps instead
192 operator const T* () { return &x; }
195 typedef vector4_base<float> vec4;
196 typedef vector4_base<bool> bvec4;
197 typedef vector4_base<int> ivec4;
199 #endif