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