]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec2.hxx
math: Add integer valued vector types.
[simgear.git] / simgear / math / SGVec2.hxx
1 // Copyright (C) 2006-2009  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifndef SGVec2_H
19 #define SGVec2_H
20
21 #include <iosfwd>
22
23 /// 2D Vector Class
24 template<typename T>
25 class SGVec2 {
26 public:
27   typedef T value_type;
28
29   /// Default constructor. Does not initialize at all.
30   /// If you need them zero initialized, use SGVec2::zeros()
31   SGVec2(void)
32   {
33     /// Initialize with nans in the debug build, that will guarantee to have
34     /// a fast uninitialized default constructor in the release but shows up
35     /// uninitialized values in the debug build very fast ...
36 #ifndef NDEBUG
37     for (unsigned i = 0; i < 2; ++i)
38       data()[i] = SGLimits<T>::quiet_NaN();
39 #endif
40   }
41   /// Constructor. Initialize by the given values
42   SGVec2(T x, T y)
43   { data()[0] = x; data()[1] = y; }
44   /// Constructor. Initialize by the content of a plain array,
45   /// make sure it has at least 2 elements
46   explicit SGVec2(const T* d)
47   { data()[0] = d[0]; data()[1] = d[1]; }
48   template<typename S>
49   explicit SGVec2(const SGVec2<S>& d)
50   { data()[0] = d[0]; data()[1] = d[1]; }
51
52   /// Access by index, the index is unchecked
53   const T& operator()(unsigned i) const
54   { return data()[i]; }
55   /// Access by index, the index is unchecked
56   T& operator()(unsigned i)
57   { return data()[i]; }
58
59   /// Access raw data by index, the index is unchecked
60   const T& operator[](unsigned i) const
61   { return data()[i]; }
62   /// Access raw data by index, the index is unchecked
63   T& operator[](unsigned i)
64   { return data()[i]; }
65
66   /// Access the x component
67   const T& x(void) const
68   { return data()[0]; }
69   /// Access the x component
70   T& x(void)
71   { return data()[0]; }
72   /// Access the y component
73   const T& y(void) const
74   { return data()[1]; }
75   /// Access the y component
76   T& y(void)
77   { return data()[1]; }
78
79   /// Access raw data
80   const T (&data(void) const)[2]
81   { return _data; }
82   /// Access raw data
83   T (&data(void))[2]
84   { return _data; }
85
86   /// Inplace addition
87   SGVec2& operator+=(const SGVec2& v)
88   { data()[0] += v(0); data()[1] += v(1); return *this; }
89   /// Inplace subtraction
90   SGVec2& operator-=(const SGVec2& v)
91   { data()[0] -= v(0); data()[1] -= v(1); return *this; }
92   /// Inplace scalar multiplication
93   template<typename S>
94   SGVec2& operator*=(S s)
95   { data()[0] *= s; data()[1] *= s; return *this; }
96   /// Inplace scalar multiplication by 1/s
97   template<typename S>
98   SGVec2& operator/=(S s)
99   { return operator*=(1/T(s)); }
100
101   /// Return an all zero vector
102   static SGVec2 zeros(void)
103   { return SGVec2(0, 0); }
104   /// Return unit vectors
105   static SGVec2 e1(void)
106   { return SGVec2(1, 0); }
107   static SGVec2 e2(void)
108   { return SGVec2(0, 1); }
109
110 private:
111   T _data[2];
112 };
113
114 /// Unary +, do nothing ...
115 template<typename T>
116 inline
117 const SGVec2<T>&
118 operator+(const SGVec2<T>& v)
119 { return v; }
120
121 /// Unary -, do nearly nothing
122 template<typename T>
123 inline
124 SGVec2<T>
125 operator-(const SGVec2<T>& v)
126 { return SGVec2<T>(-v(0), -v(1)); }
127
128 /// Binary +
129 template<typename T>
130 inline
131 SGVec2<T>
132 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
133 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
134
135 /// Binary -
136 template<typename T>
137 inline
138 SGVec2<T>
139 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
140 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
141
142 /// Scalar multiplication
143 template<typename S, typename T>
144 inline
145 SGVec2<T>
146 operator*(S s, const SGVec2<T>& v)
147 { return SGVec2<T>(s*v(0), s*v(1)); }
148
149 /// Scalar multiplication
150 template<typename S, typename T>
151 inline
152 SGVec2<T>
153 operator*(const SGVec2<T>& v, S s)
154 { return SGVec2<T>(s*v(0), s*v(1)); }
155
156 /// multiplication as a multiplicator, that is assume that the first vector
157 /// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
158 /// Then the result is the product of that matrix times the second vector.
159 template<typename T>
160 inline
161 SGVec2<T>
162 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
163 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
164
165 /// component wise min
166 template<typename T>
167 inline
168 SGVec2<T>
169 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
170 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
171 template<typename S, typename T>
172 inline
173 SGVec2<T>
174 min(const SGVec2<T>& v, S s)
175 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
176 template<typename S, typename T>
177 inline
178 SGVec2<T>
179 min(S s, const SGVec2<T>& v)
180 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
181
182 /// component wise max
183 template<typename T>
184 inline
185 SGVec2<T>
186 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
187 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
188 template<typename S, typename T>
189 inline
190 SGVec2<T>
191 max(const SGVec2<T>& v, S s)
192 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
193 template<typename S, typename T>
194 inline
195 SGVec2<T>
196 max(S s, const SGVec2<T>& v)
197 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
198
199 /// Scalar dot product
200 template<typename T>
201 inline
202 T
203 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
204 { return v1(0)*v2(0) + v1(1)*v2(1); }
205
206 /// The euclidean norm of the vector, that is what most people call length
207 template<typename T>
208 inline
209 T
210 norm(const SGVec2<T>& v)
211 { return sqrt(dot(v, v)); }
212
213 /// The euclidean norm of the vector, that is what most people call length
214 template<typename T>
215 inline
216 T
217 length(const SGVec2<T>& v)
218 { return sqrt(dot(v, v)); }
219
220 /// The 1-norm of the vector, this one is the fastest length function we
221 /// can implement on modern cpu's
222 template<typename T>
223 inline
224 T
225 norm1(const SGVec2<T>& v)
226 { return fabs(v(0)) + fabs(v(1)); }
227
228 /// The inf-norm of the vector
229 template<typename T>
230 inline
231 T
232 normI(const SGVec2<T>& v)
233 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
234
235 /// The euclidean norm of the vector, that is what most people call length
236 template<typename T>
237 inline
238 SGVec2<T>
239 normalize(const SGVec2<T>& v)
240 {
241   T normv = norm(v);
242   if (normv <= SGLimits<T>::min())
243     return SGVec2<T>::zeros();
244   return (1/normv)*v;
245 }
246
247 /// Return true if exactly the same
248 template<typename T>
249 inline
250 bool
251 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
252 { return v1(0) == v2(0) && v1(1) == v2(1); }
253
254 /// Return true if not exactly the same
255 template<typename T>
256 inline
257 bool
258 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
259 { return ! (v1 == v2); }
260
261 /// Return true if smaller, good for putting that into a std::map
262 template<typename T>
263 inline
264 bool
265 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
266 {
267   if (v1(0) < v2(0)) return true;
268   else if (v2(0) < v1(0)) return false;
269   else return (v1(1) < v2(1));
270 }
271
272 template<typename T>
273 inline
274 bool
275 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
276 {
277   if (v1(0) < v2(0)) return true;
278   else if (v2(0) < v1(0)) return false;
279   else return (v1(1) <= v2(1));
280 }
281
282 template<typename T>
283 inline
284 bool
285 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
286 { return operator<(v2, v1); }
287
288 template<typename T>
289 inline
290 bool
291 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
292 { return operator<=(v2, v1); }
293
294 /// Return true if equal to the relative tolerance tol
295 template<typename T>
296 inline
297 bool
298 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
299 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
300
301 /// Return true if equal to the relative tolerance tol
302 template<typename T>
303 inline
304 bool
305 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
306 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
307
308 /// Return true if about equal to roundoff of the underlying type
309 template<typename T>
310 inline
311 bool
312 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
313 {
314   T tol = 100*SGLimits<T>::epsilon();
315   return equivalent(v1, v2, tol, tol);
316 }
317
318 /// The euclidean distance of the two vectors
319 template<typename T>
320 inline
321 T
322 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
323 { return norm(v1 - v2); }
324
325 /// The squared euclidean distance of the two vectors
326 template<typename T>
327 inline
328 T
329 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
330 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
331
332 // calculate the projection of u along the direction of d.
333 template<typename T>
334 inline
335 SGVec2<T>
336 projection(const SGVec2<T>& u, const SGVec2<T>& d)
337 {
338   T denom = dot(d, d);
339   T ud = dot(u, d);
340   if (SGLimits<T>::min() < denom) return u;
341   else return d * (dot(u, d) / denom);
342 }
343
344 #ifndef NDEBUG
345 template<typename T>
346 inline
347 bool
348 isNaN(const SGVec2<T>& v)
349 {
350   return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
351 }
352 #endif
353
354 /// Output to an ostream
355 template<typename char_type, typename traits_type, typename T>
356 inline
357 std::basic_ostream<char_type, traits_type>&
358 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
359 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
360
361 inline
362 SGVec2f
363 toVec2f(const SGVec2d& v)
364 { return SGVec2f((float)v(0), (float)v(1)); }
365
366 inline
367 SGVec2d
368 toVec2d(const SGVec2f& v)
369 { return SGVec2d(v(0), v(1)); }
370
371 #endif