]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec2.hxx
Remove unneeded inclusions of windows.h, GL.h and GLU.H
[simgear.git] / simgear / math / SGVec2.hxx
1 // Copyright (C) 2006  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 #if defined ( __CYGWIN__ )
22 #include <ieeefp.h>
23 #endif
24
25 #include <osg/Vec2f>
26 #include <osg/Vec2d>
27
28 template<typename T>
29 struct SGVec2Storage {
30   /// Readonly raw storage interface
31   const T (&data(void) const)[2]
32   { return _data; }
33   /// Readonly raw storage interface
34   T (&data(void))[2]
35   { return _data; }
36
37   void osg() const
38   { }
39
40 private:
41   T _data[2];
42 };
43
44 template<>
45 struct SGVec2Storage<float> : public osg::Vec2f {
46   /// Access raw data by index, the index is unchecked
47   const float (&data(void) const)[2]
48   { return osg::Vec2f::_v; }
49   /// Access raw data by index, the index is unchecked
50   float (&data(void))[2]
51   { return osg::Vec2f::_v; }
52
53   const osg::Vec2f& osg() const
54   { return *this; }
55   osg::Vec2f& osg()
56   { return *this; }
57 };
58
59 template<>
60 struct SGVec2Storage<double> : public osg::Vec2d {
61   /// Access raw data by index, the index is unchecked
62   const double (&data(void) const)[2]
63   { return osg::Vec2d::_v; }
64   /// Access raw data by index, the index is unchecked
65   double (&data(void))[2]
66   { return osg::Vec2d::_v; }
67
68   const osg::Vec2d& osg() const
69   { return *this; }
70   osg::Vec2d& osg()
71   { return *this; }
72 };
73
74 /// 2D Vector Class
75 template<typename T>
76 class SGVec2 : protected SGVec2Storage<T> {
77 public:
78   typedef T value_type;
79
80   /// Default constructor. Does not initialize at all.
81   /// If you need them zero initialized, use SGVec2::zeros()
82   SGVec2(void)
83   {
84     /// Initialize with nans in the debug build, that will guarantee to have
85     /// a fast uninitialized default constructor in the release but shows up
86     /// uninitialized values in the debug build very fast ...
87 #ifndef NDEBUG
88     for (unsigned i = 0; i < 2; ++i)
89       data()[i] = SGLimits<T>::quiet_NaN();
90 #endif
91   }
92   /// Constructor. Initialize by the given values
93   SGVec2(T x, T y)
94   { data()[0] = x; data()[1] = y; }
95   /// Constructor. Initialize by the content of a plain array,
96   /// make sure it has at least 2 elements
97   explicit SGVec2(const T* d)
98   { data()[0] = d[0]; data()[1] = d[1]; }
99   explicit SGVec2(const osg::Vec2f& d)
100   { data()[0] = d[0]; data()[1] = d[1]; }
101   explicit SGVec2(const osg::Vec2d& d)
102   { data()[0] = d[0]; data()[1] = d[1]; }
103
104   /// Access by index, the index is unchecked
105   const T& operator()(unsigned i) const
106   { return data()[i]; }
107   /// Access by index, the index is unchecked
108   T& operator()(unsigned i)
109   { return data()[i]; }
110
111   /// Access raw data by index, the index is unchecked
112   const T& operator[](unsigned i) const
113   { return data()[i]; }
114   /// Access raw data by index, the index is unchecked
115   T& operator[](unsigned i)
116   { return data()[i]; }
117
118   /// Access the x component
119   const T& x(void) const
120   { return data()[0]; }
121   /// Access the x component
122   T& x(void)
123   { return data()[0]; }
124   /// Access the y component
125   const T& y(void) const
126   { return data()[1]; }
127   /// Access the y component
128   T& y(void)
129   { return data()[1]; }
130
131   /// Get the data pointer
132   using SGVec2Storage<T>::data;
133
134   /// Readonly interface function to ssg's sgVec2/sgdVec2
135   const T (&sg(void) const)[2]
136   { return data(); }
137   /// Interface function to ssg's sgVec2/sgdVec2
138   T (&sg(void))[2]
139   { return data(); }
140
141   /// Interface function to osg's Vec2*
142   using SGVec2Storage<T>::osg;
143
144   /// Inplace addition
145   SGVec2& operator+=(const SGVec2& v)
146   { data()[0] += v(0); data()[1] += v(1); return *this; }
147   /// Inplace subtraction
148   SGVec2& operator-=(const SGVec2& v)
149   { data()[0] -= v(0); data()[1] -= v(1); return *this; }
150   /// Inplace scalar multiplication
151   template<typename S>
152   SGVec2& operator*=(S s)
153   { data()[0] *= s; data()[1] *= s; return *this; }
154   /// Inplace scalar multiplication by 1/s
155   template<typename S>
156   SGVec2& operator/=(S s)
157   { return operator*=(1/T(s)); }
158
159   /// Return an all zero vector
160   static SGVec2 zeros(void)
161   { return SGVec2(0, 0); }
162   /// Return unit vectors
163   static SGVec2 e1(void)
164   { return SGVec2(1, 0); }
165   static SGVec2 e2(void)
166   { return SGVec2(0, 1); }
167 };
168
169 /// Unary +, do nothing ...
170 template<typename T>
171 inline
172 const SGVec2<T>&
173 operator+(const SGVec2<T>& v)
174 { return v; }
175
176 /// Unary -, do nearly nothing
177 template<typename T>
178 inline
179 SGVec2<T>
180 operator-(const SGVec2<T>& v)
181 { return SGVec2<T>(-v(0), -v(1)); }
182
183 /// Binary +
184 template<typename T>
185 inline
186 SGVec2<T>
187 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
188 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
189
190 /// Binary -
191 template<typename T>
192 inline
193 SGVec2<T>
194 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
195 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
196
197 /// Scalar multiplication
198 template<typename S, typename T>
199 inline
200 SGVec2<T>
201 operator*(S s, const SGVec2<T>& v)
202 { return SGVec2<T>(s*v(0), s*v(1)); }
203
204 /// Scalar multiplication
205 template<typename S, typename T>
206 inline
207 SGVec2<T>
208 operator*(const SGVec2<T>& v, S s)
209 { return SGVec2<T>(s*v(0), s*v(1)); }
210
211 /// multiplication as a multiplicator, that is assume that the first vector
212 /// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
213 /// Then the result is the product of that matrix times the second vector.
214 template<typename T>
215 inline
216 SGVec2<T>
217 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
218 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
219
220 /// component wise min
221 template<typename T>
222 inline
223 SGVec2<T>
224 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
225 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
226 template<typename S, typename T>
227 inline
228 SGVec2<T>
229 min(const SGVec2<T>& v, S s)
230 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
231 template<typename S, typename T>
232 inline
233 SGVec2<T>
234 min(S s, const SGVec2<T>& v)
235 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
236
237 /// component wise max
238 template<typename T>
239 inline
240 SGVec2<T>
241 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
242 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
243 template<typename S, typename T>
244 inline
245 SGVec2<T>
246 max(const SGVec2<T>& v, S s)
247 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
248 template<typename S, typename T>
249 inline
250 SGVec2<T>
251 max(S s, const SGVec2<T>& v)
252 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
253
254 /// Scalar dot product
255 template<typename T>
256 inline
257 T
258 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
259 { return v1(0)*v2(0) + v1(1)*v2(1); }
260
261 /// The euclidean norm of the vector, that is what most people call length
262 template<typename T>
263 inline
264 T
265 norm(const SGVec2<T>& v)
266 { return sqrt(dot(v, v)); }
267
268 /// The euclidean norm of the vector, that is what most people call length
269 template<typename T>
270 inline
271 T
272 length(const SGVec2<T>& v)
273 { return sqrt(dot(v, v)); }
274
275 /// The 1-norm of the vector, this one is the fastest length function we
276 /// can implement on modern cpu's
277 template<typename T>
278 inline
279 T
280 norm1(const SGVec2<T>& v)
281 { return fabs(v(0)) + fabs(v(1)); }
282
283 /// The inf-norm of the vector
284 template<typename T>
285 inline
286 T
287 normI(const SGVec2<T>& v)
288 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
289
290 /// The euclidean norm of the vector, that is what most people call length
291 template<typename T>
292 inline
293 SGVec2<T>
294 normalize(const SGVec2<T>& v)
295 { return (1/norm(v))*v; }
296
297 /// Return true if exactly the same
298 template<typename T>
299 inline
300 bool
301 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
302 { return v1(0) == v2(0) && v1(1) == v2(1); }
303
304 /// Return true if not exactly the same
305 template<typename T>
306 inline
307 bool
308 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
309 { return ! (v1 == v2); }
310
311 /// Return true if smaller, good for putting that into a std::map
312 template<typename T>
313 inline
314 bool
315 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
316 {
317   if (v1(0) < v2(0)) return true;
318   else if (v2(0) < v1(0)) return false;
319   else return (v1(1) < v2(1));
320 }
321
322 template<typename T>
323 inline
324 bool
325 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
326 {
327   if (v1(0) < v2(0)) return true;
328   else if (v2(0) < v1(0)) return false;
329   else return (v1(1) <= v2(1));
330 }
331
332 template<typename T>
333 inline
334 bool
335 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
336 { return operator<(v2, v1); }
337
338 template<typename T>
339 inline
340 bool
341 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
342 { return operator<=(v2, v1); }
343
344 /// Return true if equal to the relative tolerance tol
345 template<typename T>
346 inline
347 bool
348 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
349 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
350
351 /// Return true if equal to the relative tolerance tol
352 template<typename T>
353 inline
354 bool
355 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
356 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
357
358 /// Return true if about equal to roundoff of the underlying type
359 template<typename T>
360 inline
361 bool
362 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
363 {
364   T tol = 100*SGLimits<T>::epsilon();
365   return equivalent(v1, v2, tol, tol);
366 }
367
368 /// The euclidean distance of the two vectors
369 template<typename T>
370 inline
371 T
372 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
373 { return norm(v1 - v2); }
374
375 /// The squared euclidean distance of the two vectors
376 template<typename T>
377 inline
378 T
379 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
380 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
381
382 #ifndef NDEBUG
383 template<typename T>
384 inline
385 bool
386 isNaN(const SGVec2<T>& v)
387 {
388   return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
389 }
390 #endif
391
392 /// Output to an ostream
393 template<typename char_type, typename traits_type, typename T>
394 inline
395 std::basic_ostream<char_type, traits_type>&
396 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
397 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
398
399 inline
400 SGVec2f
401 toVec2f(const SGVec2d& v)
402 { return SGVec2f((float)v(0), (float)v(1)); }
403
404 inline
405 SGVec2d
406 toVec2d(const SGVec2f& v)
407 { return SGVec2d(v(0), v(1)); }
408
409 #endif