]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec3.hxx
don't build tabbed_value_test, swap_test, openal_test1 and openal_test2 by default.
[simgear.git] / simgear / math / SGVec3.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 SGVec3_H
19 #define SGVec3_H
20
21 #include <osg/Vec3f>
22 #include <osg/Vec3d>
23
24 template<typename T>
25 struct SGVec3Storage {
26   /// Readonly raw storage interface
27   const T (&data(void) const)[3]
28   { return _data; }
29   /// Readonly raw storage interface
30   T (&data(void))[3]
31   { return _data; }
32
33   void osg() const
34   { }
35
36 private:
37   T _data[3];
38 };
39
40 template<>
41 struct SGVec3Storage<float> : public osg::Vec3f {
42   /// Access raw data by index, the index is unchecked
43   const float (&data(void) const)[3]
44   { return osg::Vec3f::_v; }
45   /// Access raw data by index, the index is unchecked
46   float (&data(void))[3]
47   { return osg::Vec3f::_v; }
48
49   const osg::Vec3f& osg() const
50   { return *this; }
51   osg::Vec3f& osg()
52   { return *this; }
53 };
54
55 template<>
56 struct SGVec3Storage<double> : public osg::Vec3d {
57   /// Access raw data by index, the index is unchecked
58   const double (&data(void) const)[3]
59   { return osg::Vec3d::_v; }
60   /// Access raw data by index, the index is unchecked
61   double (&data(void))[3]
62   { return osg::Vec3d::_v; }
63
64   const osg::Vec3d& osg() const
65   { return *this; }
66   osg::Vec3d& osg()
67   { return *this; }
68 };
69
70 /// 3D Vector Class
71 template<typename T>
72 class SGVec3 : protected SGVec3Storage<T> {
73 public:
74   typedef T value_type;
75
76   /// Default constructor. Does not initialize at all.
77   /// If you need them zero initialized, use SGVec3::zeros()
78   SGVec3(void)
79   {
80     /// Initialize with nans in the debug build, that will guarantee to have
81     /// a fast uninitialized default constructor in the release but shows up
82     /// uninitialized values in the debug build very fast ...
83 #ifndef NDEBUG
84     for (unsigned i = 0; i < 3; ++i)
85       data()[i] = SGLimits<T>::quiet_NaN();
86 #endif
87   }
88   /// Constructor. Initialize by the given values
89   SGVec3(T x, T y, T z)
90   { data()[0] = x; data()[1] = y; data()[2] = z; }
91   /// Constructor. Initialize by the content of a plain array,
92   /// make sure it has at least 3 elements
93   explicit SGVec3(const T* d)
94   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
95   template<typename S>
96   explicit SGVec3(const SGVec3<S>& d)
97   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
98   explicit SGVec3(const osg::Vec3f& d)
99   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
100   explicit SGVec3(const osg::Vec3d& d)
101   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
102   explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
103   { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
104
105   /// Access by index, the index is unchecked
106   const T& operator()(unsigned i) const
107   { return data()[i]; }
108   /// Access by index, the index is unchecked
109   T& operator()(unsigned i)
110   { return data()[i]; }
111
112   /// Access raw data by index, the index is unchecked
113   const T& operator[](unsigned i) const
114   { return data()[i]; }
115   /// Access raw data by index, the index is unchecked
116   T& operator[](unsigned i)
117   { return data()[i]; }
118
119   /// Access the x component
120   const T& x(void) const
121   { return data()[0]; }
122   /// Access the x component
123   T& x(void)
124   { return data()[0]; }
125   /// Access the y component
126   const T& y(void) const
127   { return data()[1]; }
128   /// Access the y component
129   T& y(void)
130   { return data()[1]; }
131   /// Access the z component
132   const T& z(void) const
133   { return data()[2]; }
134   /// Access the z component
135   T& z(void)
136   { return data()[2]; }
137
138   /// Get the data pointer
139   using SGVec3Storage<T>::data;
140
141   /// Readonly interface function to ssg's sgVec3/sgdVec3
142   const T (&sg(void) const)[3]
143   { return data(); }
144   /// Interface function to ssg's sgVec3/sgdVec3
145   T (&sg(void))[3]
146   { return data(); }
147
148   /// Interface function to osg's Vec3*
149   using SGVec3Storage<T>::osg;
150
151   /// Inplace addition
152   SGVec3& operator+=(const SGVec3& v)
153   { data()[0] += v(0); data()[1] += v(1); data()[2] += v(2); return *this; }
154   /// Inplace subtraction
155   SGVec3& operator-=(const SGVec3& v)
156   { data()[0] -= v(0); data()[1] -= v(1); data()[2] -= v(2); return *this; }
157   /// Inplace scalar multiplication
158   template<typename S>
159   SGVec3& operator*=(S s)
160   { data()[0] *= s; data()[1] *= s; data()[2] *= s; return *this; }
161   /// Inplace scalar multiplication by 1/s
162   template<typename S>
163   SGVec3& operator/=(S s)
164   { return operator*=(1/T(s)); }
165
166   /// Return an all zero vector
167   static SGVec3 zeros(void)
168   { return SGVec3(0, 0, 0); }
169   /// Return unit vectors
170   static SGVec3 e1(void)
171   { return SGVec3(1, 0, 0); }
172   static SGVec3 e2(void)
173   { return SGVec3(0, 1, 0); }
174   static SGVec3 e3(void)
175   { return SGVec3(0, 0, 1); }
176
177   /// Constructor. Initialize by a geodetic coordinate
178   /// Note that this conversion is relatively expensive to compute
179   static SGVec3 fromGeod(const SGGeod& geod);
180   /// Constructor. Initialize by a geocentric coordinate
181   /// Note that this conversion is relatively expensive to compute
182   static SGVec3 fromGeoc(const SGGeoc& geoc);
183 };
184
185 template<>
186 inline
187 SGVec3<double>
188 SGVec3<double>::fromGeod(const SGGeod& geod)
189 {
190   SGVec3<double> cart;
191   SGGeodesy::SGGeodToCart(geod, cart);
192   return cart;
193 }
194
195 template<>
196 inline
197 SGVec3<float>
198 SGVec3<float>::fromGeod(const SGGeod& geod)
199 {
200   SGVec3<double> cart;
201   SGGeodesy::SGGeodToCart(geod, cart);
202   return SGVec3<float>(cart(0), cart(1), cart(2));
203 }
204
205 template<>
206 inline
207 SGVec3<double>
208 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
209 {
210   SGVec3<double> cart;
211   SGGeodesy::SGGeocToCart(geoc, cart);
212   return cart;
213 }
214
215 template<>
216 inline
217 SGVec3<float>
218 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
219 {
220   SGVec3<double> cart;
221   SGGeodesy::SGGeocToCart(geoc, cart);
222   return SGVec3<float>(cart(0), cart(1), cart(2));
223 }
224
225 /// Unary +, do nothing ...
226 template<typename T>
227 inline
228 const SGVec3<T>&
229 operator+(const SGVec3<T>& v)
230 { return v; }
231
232 /// Unary -, do nearly nothing
233 template<typename T>
234 inline
235 SGVec3<T>
236 operator-(const SGVec3<T>& v)
237 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
238
239 /// Binary +
240 template<typename T>
241 inline
242 SGVec3<T>
243 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
244 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
245
246 /// Binary -
247 template<typename T>
248 inline
249 SGVec3<T>
250 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
251 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
252
253 /// Scalar multiplication
254 template<typename S, typename T>
255 inline
256 SGVec3<T>
257 operator*(S s, const SGVec3<T>& v)
258 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
259
260 /// Scalar multiplication
261 template<typename S, typename T>
262 inline
263 SGVec3<T>
264 operator*(const SGVec3<T>& v, S s)
265 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
266
267 /// multiplication as a multiplicator, that is assume that the first vector
268 /// represents a 3x3 diagonal matrix with the diagonal elements in the vector.
269 /// Then the result is the product of that matrix times the second vector.
270 template<typename T>
271 inline
272 SGVec3<T>
273 mult(const SGVec3<T>& v1, const SGVec3<T>& v2)
274 { return SGVec3<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2)); }
275
276 /// component wise min
277 template<typename T>
278 inline
279 SGVec3<T>
280 min(const SGVec3<T>& v1, const SGVec3<T>& v2)
281 {
282   return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
283                    SGMisc<T>::min(v1(1), v2(1)),
284                    SGMisc<T>::min(v1(2), v2(2)));
285 }
286 template<typename S, typename T>
287 inline
288 SGVec3<T>
289 min(const SGVec3<T>& v, S s)
290 {
291   return SGVec3<T>(SGMisc<T>::min(s, v(0)),
292                    SGMisc<T>::min(s, v(1)),
293                    SGMisc<T>::min(s, v(2)));
294 }
295 template<typename S, typename T>
296 inline
297 SGVec3<T>
298 min(S s, const SGVec3<T>& v)
299 {
300   return SGVec3<T>(SGMisc<T>::min(s, v(0)),
301                    SGMisc<T>::min(s, v(1)),
302                    SGMisc<T>::min(s, v(2)));
303 }
304
305 /// component wise max
306 template<typename T>
307 inline
308 SGVec3<T>
309 max(const SGVec3<T>& v1, const SGVec3<T>& v2)
310 {
311   return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
312                    SGMisc<T>::max(v1(1), v2(1)),
313                    SGMisc<T>::max(v1(2), v2(2)));
314 }
315 template<typename S, typename T>
316 inline
317 SGVec3<T>
318 max(const SGVec3<T>& v, S s)
319 {
320   return SGVec3<T>(SGMisc<T>::max(s, v(0)),
321                    SGMisc<T>::max(s, v(1)),
322                    SGMisc<T>::max(s, v(2)));
323 }
324 template<typename S, typename T>
325 inline
326 SGVec3<T>
327 max(S s, const SGVec3<T>& v)
328 {
329   return SGVec3<T>(SGMisc<T>::max(s, v(0)),
330                    SGMisc<T>::max(s, v(1)),
331                    SGMisc<T>::max(s, v(2)));
332 }
333
334 /// Scalar dot product
335 template<typename T>
336 inline
337 T
338 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
339 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
340
341 /// The euclidean norm of the vector, that is what most people call length
342 template<typename T>
343 inline
344 T
345 norm(const SGVec3<T>& v)
346 { return sqrt(dot(v, v)); }
347
348 /// The euclidean norm of the vector, that is what most people call length
349 template<typename T>
350 inline
351 T
352 length(const SGVec3<T>& v)
353 { return sqrt(dot(v, v)); }
354
355 /// The 1-norm of the vector, this one is the fastest length function we
356 /// can implement on modern cpu's
357 template<typename T>
358 inline
359 T
360 norm1(const SGVec3<T>& v)
361 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
362
363 /// The inf-norm of the vector
364 template<typename T>
365 inline
366 T
367 normI(const SGVec3<T>& v)
368 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
369
370 /// Vector cross product
371 template<typename T>
372 inline
373 SGVec3<T>
374 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
375 {
376   return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
377                    v1(2)*v2(0) - v1(0)*v2(2),
378                    v1(0)*v2(1) - v1(1)*v2(0));
379 }
380
381 /// return any normalized vector perpendicular to v
382 template<typename T>
383 inline
384 SGVec3<T>
385 perpendicular(const SGVec3<T>& v)
386 {
387   T absv1 = fabs(v(0));
388   T absv2 = fabs(v(1));
389   T absv3 = fabs(v(2));
390
391   if (absv2 < absv1 && absv3 < absv1) {
392     T quot = v(1)/v(0);
393     return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
394   } else if (absv3 < absv2) {
395     T quot = v(2)/v(1);
396     return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
397   } else if (SGLimits<T>::min() < absv3) {
398     T quot = v(0)/v(2);
399     return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
400   } else {
401     // the all zero case ...
402     return SGVec3<T>(0, 0, 0);
403   }
404 }
405
406 /// The euclidean norm of the vector, that is what most people call length
407 template<typename T>
408 inline
409 SGVec3<T>
410 normalize(const SGVec3<T>& v)
411 { return (1/norm(v))*v; }
412
413 /// Return true if exactly the same
414 template<typename T>
415 inline
416 bool
417 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
418 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
419
420 /// Return true if not exactly the same
421 template<typename T>
422 inline
423 bool
424 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
425 { return ! (v1 == v2); }
426
427 /// Return true if smaller, good for putting that into a std::map
428 template<typename T>
429 inline
430 bool
431 operator<(const SGVec3<T>& v1, const SGVec3<T>& v2)
432 {
433   if (v1(0) < v2(0)) return true;
434   else if (v2(0) < v1(0)) return false;
435   else if (v1(1) < v2(1)) return true;
436   else if (v2(1) < v1(1)) return false;
437   else return (v1(2) < v2(2));
438 }
439
440 template<typename T>
441 inline
442 bool
443 operator<=(const SGVec3<T>& v1, const SGVec3<T>& v2)
444 {
445   if (v1(0) < v2(0)) return true;
446   else if (v2(0) < v1(0)) return false;
447   else if (v1(1) < v2(1)) return true;
448   else if (v2(1) < v1(1)) return false;
449   else return (v1(2) <= v2(2));
450 }
451
452 template<typename T>
453 inline
454 bool
455 operator>(const SGVec3<T>& v1, const SGVec3<T>& v2)
456 { return operator<(v2, v1); }
457
458 template<typename T>
459 inline
460 bool
461 operator>=(const SGVec3<T>& v1, const SGVec3<T>& v2)
462 { return operator<=(v2, v1); }
463
464 /// Return true if equal to the relative tolerance tol
465 template<typename T>
466 inline
467 bool
468 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
469 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
470
471 /// Return true if equal to the relative tolerance tol
472 template<typename T>
473 inline
474 bool
475 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
476 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
477
478 /// Return true if about equal to roundoff of the underlying type
479 template<typename T>
480 inline
481 bool
482 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
483 {
484   T tol = 100*SGLimits<T>::epsilon();
485   return equivalent(v1, v2, tol, tol);
486 }
487
488 /// The euclidean distance of the two vectors
489 template<typename T>
490 inline
491 T
492 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
493 { return norm(v1 - v2); }
494
495 /// The squared euclidean distance of the two vectors
496 template<typename T>
497 inline
498 T
499 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
500 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
501
502 #ifndef NDEBUG
503 template<typename T>
504 inline
505 bool
506 isNaN(const SGVec3<T>& v)
507 {
508   return SGMisc<T>::isNaN(v(0)) ||
509     SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
510 }
511 #endif
512
513 /// Output to an ostream
514 template<typename char_type, typename traits_type, typename T>
515 inline
516 std::basic_ostream<char_type, traits_type>&
517 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
518 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
519
520 inline
521 SGVec3f
522 toVec3f(const SGVec3d& v)
523 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
524
525 inline
526 SGVec3d
527 toVec3d(const SGVec3f& v)
528 { return SGVec3d(v(0), v(1), v(2)); }
529
530 #endif