1 /* --------------------------------------------------------------------
2 EXTREME TUXRACER
3 
4 Copyright (C) 1999-2001 Jasmin F. Patry (Tuxracer)
5 Copyright (C) 2010 Extreme Tux Racer Team
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 ---------------------------------------------------------------------*/
17 
18 #ifndef VECTORS_H
19 #define VECTORS_H
20 
21 #include <cmath>
22 
23 
24 template<typename T>
25 struct TVector2 {
26 	T x, y;
27 	constexpr explicit TVector2(T _x = (T)0, T _y = (T)0)
xTVector228 		: x(_x), y(_y)
29 	{}
LengthTVector230 	constexpr double Length() const {
31 		return std::hypot(x, y);
32 	}
33 	double Norm();
34 	TVector2<T>& operator*=(T f) {
35 		x *= f;
36 		y *= f;
37 		return *this;
38 	}
39 	TVector2<T>& operator+=(const TVector2<T>& v) {
40 		x += v.x;
41 		y += v.y;
42 		return *this;
43 	}
44 	TVector2<T>& operator-=(const TVector2<T>& v) {
45 		x -= v.x;
46 		y -= v.y;
47 		return *this;
48 	}
49 };
50 
51 template<typename T>
52 struct TVector3 {
53 	T x, y, z;
54 	constexpr explicit TVector3(T _x = (T)0, T _y = (T)0, T _z = (T)0)
xTVector355 		: x(_x), y(_y), z(_z)
56 	{}
LengthTVector357 	constexpr double Length() const {
58 		return std::sqrt(static_cast<double>(x*x + y*y + z*z));
59 	}
60 	double Norm();
61 	TVector3<T>& operator*=(T f) {
62 		x *= f;
63 		y *= f;
64 		z *= f;
65 		return *this;
66 	}
67 	TVector3<T>& operator+=(const TVector3<T>& v) {
68 		x += v.x;
69 		y += v.y;
70 		z += v.z;
71 		return *this;
72 	}
73 	TVector3<T>& operator-=(const TVector3<T>& v) {
74 		x -= v.x;
75 		y -= v.y;
76 		z -= v.z;
77 		return *this;
78 	}
79 };
80 
81 template<typename T>
82 struct TVector4 {
83 	T x, y, z, w;
84 	constexpr explicit TVector4(T _x = (T)0, T _y = (T)0, T _z = (T)0, T _w = (T)0)
xTVector485 		: x(_x), y(_y), z(_z), w(_w)
86 	{}
LengthTVector487 	constexpr double Length() const {
88 		return std::sqrt(static_cast<double>(x*x + y*y + z*z + w*w));
89 	}
90 	double Norm();
91 	TVector4<T>& operator*=(T f) {
92 		x *= f;
93 		y *= f;
94 		z *= f;
95 		w *= f;
96 		return *this;
97 	}
98 	TVector4<T>& operator+=(const TVector4<T>& v) {
99 		x += v.x;
100 		y += v.y;
101 		z += v.z;
102 		w += v.w;
103 		return *this;
104 	}
105 	TVector4<T>& operator-=(const TVector4<T>& v) {
106 		x -= v.x;
107 		y -= v.y;
108 		z -= v.z;
109 		w -= v.w;
110 		return *this;
111 	}
112 };
113 
114 typedef TVector4<double> TVector4d;
115 typedef TVector3<double> TVector3d;
116 typedef TVector2<double> TVector2d;
117 typedef TVector4<int> TVector4i;
118 typedef TVector3<int> TVector3i;
119 typedef TVector2<int> TVector2i;
120 typedef TVector4d TQuaternion;
121 
122 template<typename T>
123 constexpr TVector2<T> operator*(T f, const TVector2<T>& v) {
124 	return TVector2<T>(v.x*f, v.y*f);
125 }
126 template<typename T>
127 constexpr TVector3<T> operator*(T f, const TVector3<T>& v) {
128 	return TVector3<T>(v.x*f, v.y*f, v.z*f);
129 }
130 template<typename T>
131 constexpr TVector4<T> operator*(T f, const TVector4<T>& v) {
132 	return TVector4<T>(v.x*f, v.y*f, v.z*f, v.w*f);
133 }
134 
135 template<typename T>
136 constexpr TVector2<T> operator+(const TVector2<T>& l, const TVector2<T>& r) {
137 	return TVector2<T>(l.x + r.x, l.y + r.y);
138 }
139 template<typename T>
140 constexpr TVector3<T> operator+(const TVector3<T>& l, const TVector3<T>& r) {
141 	return TVector3<T>(l.x + r.x, l.y + r.y, l.z + r.z);
142 }
143 template<typename T>
144 constexpr TVector4<T> operator+(const TVector4<T>& l, const TVector4<T>& r) {
145 	return TVector4<T>(l.x + r.x, l.y + r.y, l.z + r.z, l.w + r.w);
146 }
147 
148 template<typename T>
149 constexpr TVector2<T> operator-(const TVector2<T>& l, const TVector2<T>& r) {
150 	return TVector2<T>(l.x - r.x, l.y - r.y);
151 }
152 template<typename T>
153 constexpr TVector3<T> operator-(const TVector3<T>& l, const TVector3<T>& r) {
154 	return TVector3<T>(l.x - r.x, l.y - r.y, l.z - r.z);
155 }
156 template<typename T>
157 constexpr TVector4<T> operator-(const TVector4<T>& l, const TVector4<T>& r) {
158 	return TVector4<T>(l.x - r.x, l.y - r.y, l.z - r.z, l.w - r.w);
159 }
160 
161 template<typename T>
162 constexpr TVector2<T> operator-(const TVector2<T>&r) {
163 	return TVector2<T>(-r.x, -r.y);
164 }
165 template<typename T>
166 constexpr TVector3<T> operator-(const TVector3<T>& r) {
167 	return TVector3<T>(-r.x, -r.y, -r.z);
168 }
169 template<typename T>
170 constexpr TVector4<T> operator-(const TVector4<T>& r) {
171 	return TVector4<T>(-r.x, -r.y, -r.z, -r.w);
172 }
173 
174 template<typename T>
DotProduct(const TVector3<T> & v1,const TVector3<T> & v2)175 constexpr double DotProduct(const TVector3<T>& v1, const TVector3<T>& v2) {
176 	return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
177 }
178 template<typename T>
DotProduct(const TVector4<T> & v1,const TVector4<T> & v2)179 constexpr double DotProduct(const TVector4<T>& v1, const TVector4<T>& v2) {
180 	return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
181 }
182 
183 TVector3d CrossProduct(const TVector3d& u, const TVector3d& v);
184 
185 
186 extern const TVector2d NullVec2;
187 extern const TVector3d NullVec3;
188 extern const TVector4d NullVec4;
189 extern const TVector2i NullVec2i;
190 extern const TVector3i NullVec3i;
191 extern const TVector4i NullVec4i;
192 
193 
194 #endif
195