]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec2.hxx
Merge branch 'maint' into next
[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   template<typename S>
100   explicit SGVec2(const SGVec2<S>& d)
101   { data()[0] = d[0]; data()[1] = d[1]; }
102   explicit SGVec2(const osg::Vec2f& d)
103   { data()[0] = d[0]; data()[1] = d[1]; }
104   explicit SGVec2(const osg::Vec2d& d)
105   { data()[0] = d[0]; data()[1] = d[1]; }
106
107   /// Access by index, the index is unchecked
108   const T& operator()(unsigned i) const
109   { return data()[i]; }
110   /// Access by index, the index is unchecked
111   T& operator()(unsigned i)
112   { return data()[i]; }
113
114   /// Access raw data by index, the index is unchecked
115   const T& operator[](unsigned i) const
116   { return data()[i]; }
117   /// Access raw data by index, the index is unchecked
118   T& operator[](unsigned i)
119   { return data()[i]; }
120
121   /// Access the x component
122   const T& x(void) const
123   { return data()[0]; }
124   /// Access the x component
125   T& x(void)
126   { return data()[0]; }
127   /// Access the y component
128   const T& y(void) const
129   { return data()[1]; }
130   /// Access the y component
131   T& y(void)
132   { return data()[1]; }
133
134   /// Get the data pointer
135   using SGVec2Storage<T>::data;
136
137   /// Readonly interface function to ssg's sgVec2/sgdVec2
138   const T (&sg(void) const)[2]
139   { return data(); }
140   /// Interface function to ssg's sgVec2/sgdVec2
141   T (&sg(void))[2]
142   { return data(); }
143
144   /// Interface function to osg's Vec2*
145   using SGVec2Storage<T>::osg;
146
147   /// Inplace addition
148   SGVec2& operator+=(const SGVec2& v)
149   { data()[0] += v(0); data()[1] += v(1); return *this; }
150   /// Inplace subtraction
151   SGVec2& operator-=(const SGVec2& v)
152   { data()[0] -= v(0); data()[1] -= v(1); return *this; }
153   /// Inplace scalar multiplication
154   template<typename S>
155   SGVec2& operator*=(S s)
156   { data()[0] *= s; data()[1] *= s; return *this; }
157   /// Inplace scalar multiplication by 1/s
158   template<typename S>
159   SGVec2& operator/=(S s)
160   { return operator*=(1/T(s)); }
161
162   /// Return an all zero vector
163   static SGVec2 zeros(void)
164   { return SGVec2(0, 0); }
165   /// Return unit vectors
166   static SGVec2 e1(void)
167   { return SGVec2(1, 0); }
168   static SGVec2 e2(void)
169   { return SGVec2(0, 1); }
170 };
171
172 /// Unary +, do nothing ...
173 template<typename T>
174 inline
175 const SGVec2<T>&
176 operator+(const SGVec2<T>& v)
177 { return v; }
178
179 /// Unary -, do nearly nothing
180 template<typename T>
181 inline
182 SGVec2<T>
183 operator-(const SGVec2<T>& v)
184 { return SGVec2<T>(-v(0), -v(1)); }
185
186 /// Binary +
187 template<typename T>
188 inline
189 SGVec2<T>
190 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
191 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
192
193 /// Binary -
194 template<typename T>
195 inline
196 SGVec2<T>
197 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
198 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
199
200 /// Scalar multiplication
201 template<typename S, typename T>
202 inline
203 SGVec2<T>
204 operator*(S s, const SGVec2<T>& v)
205 { return SGVec2<T>(s*v(0), s*v(1)); }
206
207 /// Scalar multiplication
208 template<typename S, typename T>
209 inline
210 SGVec2<T>
211 operator*(const SGVec2<T>& v, S s)
212 { return SGVec2<T>(s*v(0), s*v(1)); }
213
214 /// multiplication as a multiplicator, that is assume that the first vector
215 /// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
216 /// Then the result is the product of that matrix times the second vector.
217 template<typename T>
218 inline
219 SGVec2<T>
220 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
221 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
222
223 /// component wise min
224 template<typename T>
225 inline
226 SGVec2<T>
227 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
228 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
229 template<typename S, typename T>
230 inline
231 SGVec2<T>
232 min(const SGVec2<T>& v, S s)
233 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
234 template<typename S, typename T>
235 inline
236 SGVec2<T>
237 min(S s, const SGVec2<T>& v)
238 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
239
240 /// component wise max
241 template<typename T>
242 inline
243 SGVec2<T>
244 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
245 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
246 template<typename S, typename T>
247 inline
248 SGVec2<T>
249 max(const SGVec2<T>& v, S s)
250 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
251 template<typename S, typename T>
252 inline
253 SGVec2<T>
254 max(S s, const SGVec2<T>& v)
255 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
256
257 /// Scalar dot product
258 template<typename T>
259 inline
260 T
261 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
262 { return v1(0)*v2(0) + v1(1)*v2(1); }
263
264 /// The euclidean norm of the vector, that is what most people call length
265 template<typename T>
266 inline
267 T
268 norm(const SGVec2<T>& v)
269 { return sqrt(dot(v, v)); }
270
271 /// The euclidean norm of the vector, that is what most people call length
272 template<typename T>
273 inline
274 T
275 length(const SGVec2<T>& v)
276 { return sqrt(dot(v, v)); }
277
278 /// The 1-norm of the vector, this one is the fastest length function we
279 /// can implement on modern cpu's
280 template<typename T>
281 inline
282 T
283 norm1(const SGVec2<T>& v)
284 { return fabs(v(0)) + fabs(v(1)); }
285
286 /// The inf-norm of the vector
287 template<typename T>
288 inline
289 T
290 normI(const SGVec2<T>& v)
291 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
292
293 /// The euclidean norm of the vector, that is what most people call length
294 template<typename T>
295 inline
296 SGVec2<T>
297 normalize(const SGVec2<T>& v)
298 { return (1/norm(v))*v; }
299
300 /// Return true if exactly the same
301 template<typename T>
302 inline
303 bool
304 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
305 { return v1(0) == v2(0) && v1(1) == v2(1); }
306
307 /// Return true if not exactly the same
308 template<typename T>
309 inline
310 bool
311 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
312 { return ! (v1 == v2); }
313
314 /// Return true if smaller, good for putting that into a std::map
315 template<typename T>
316 inline
317 bool
318 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
319 {
320   if (v1(0) < v2(0)) return true;
321   else if (v2(0) < v1(0)) return false;
322   else return (v1(1) < v2(1));
323 }
324
325 template<typename T>
326 inline
327 bool
328 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
329 {
330   if (v1(0) < v2(0)) return true;
331   else if (v2(0) < v1(0)) return false;
332   else return (v1(1) <= v2(1));
333 }
334
335 template<typename T>
336 inline
337 bool
338 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
339 { return operator<(v2, v1); }
340
341 template<typename T>
342 inline
343 bool
344 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
345 { return operator<=(v2, v1); }
346
347 /// Return true if equal to the relative tolerance tol
348 template<typename T>
349 inline
350 bool
351 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
352 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
353
354 /// Return true if equal to the relative tolerance tol
355 template<typename T>
356 inline
357 bool
358 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
359 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
360
361 /// Return true if about equal to roundoff of the underlying type
362 template<typename T>
363 inline
364 bool
365 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
366 {
367   T tol = 100*SGLimits<T>::epsilon();
368   return equivalent(v1, v2, tol, tol);
369 }
370
371 /// The euclidean distance of the two vectors
372 template<typename T>
373 inline
374 T
375 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
376 { return norm(v1 - v2); }
377
378 /// The squared euclidean distance of the two vectors
379 template<typename T>
380 inline
381 T
382 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
383 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
384
385 #ifndef NDEBUG
386 template<typename T>
387 inline
388 bool
389 isNaN(const SGVec2<T>& v)
390 {
391   return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
392 }
393 #endif
394
395 /// Output to an ostream
396 template<typename char_type, typename traits_type, typename T>
397 inline
398 std::basic_ostream<char_type, traits_type>&
399 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
400 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
401
402 inline
403 SGVec2f
404 toVec2f(const SGVec2d& v)
405 { return SGVec2f((float)v(0), (float)v(1)); }
406
407 inline
408 SGVec2d
409 toVec2d(const SGVec2f& v)
410 { return SGVec2d(v(0), v(1)); }
411
412 #endif