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