]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec3.hxx
Fix for HTTP/curl waiting on update
[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 #include <iosfwd>
22
23 #include <simgear/math/SGVec2.hxx>
24 #include <simgear/math/SGGeodesy.hxx>
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 /// Add two vectors taking care of (integer) overflows. The values are limited
295 /// to the respective minimum and maximum values.
296 template<class T>
297 SGVec3<T> addClipOverflow(SGVec3<T> const& lhs, SGVec3<T> const& rhs)
298 {
299   return SGVec3<T>(
300     SGMisc<T>::addClipOverflow(lhs.x(), rhs.x()),
301     SGMisc<T>::addClipOverflow(lhs.y(), rhs.y()),
302     SGMisc<T>::addClipOverflow(lhs.z(), rhs.z())
303   );
304 }
305
306 /// Scalar dot product
307 template<typename T>
308 inline
309 T
310 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
311 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
312
313 /// The euclidean norm of the vector, that is what most people call length
314 template<typename T>
315 inline
316 T
317 norm(const SGVec3<T>& v)
318 { return sqrt(dot(v, v)); }
319
320 /// The euclidean norm of the vector, that is what most people call length
321 template<typename T>
322 inline
323 T
324 length(const SGVec3<T>& v)
325 { return sqrt(dot(v, v)); }
326
327 /// The 1-norm of the vector, this one is the fastest length function we
328 /// can implement on modern cpu's
329 template<typename T>
330 inline
331 T
332 norm1(const SGVec3<T>& v)
333 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
334
335 /// The inf-norm of the vector
336 template<typename T>
337 inline
338 T
339 normI(const SGVec3<T>& v)
340 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
341
342 /// Vector cross product
343 template<typename T>
344 inline
345 SGVec3<T>
346 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
347 {
348   return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
349                    v1(2)*v2(0) - v1(0)*v2(2),
350                    v1(0)*v2(1) - v1(1)*v2(0));
351 }
352
353 /// return any normalized vector perpendicular to v
354 template<typename T>
355 inline
356 SGVec3<T>
357 perpendicular(const SGVec3<T>& v)
358 {
359   T absv1 = fabs(v(0));
360   T absv2 = fabs(v(1));
361   T absv3 = fabs(v(2));
362
363   if (absv2 < absv1 && absv3 < absv1) {
364     T quot = v(1)/v(0);
365     return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
366   } else if (absv3 < absv2) {
367     T quot = v(2)/v(1);
368     return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
369   } else if (SGLimits<T>::min() < absv3) {
370     T quot = v(0)/v(2);
371     return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
372   } else {
373     // the all zero case ...
374     return SGVec3<T>(0, 0, 0);
375   }
376 }
377
378 /// Construct a unit vector in the given direction.
379 /// or the zero vector if the input vector is zero.
380 template<typename T>
381 inline
382 SGVec3<T>
383 normalize(const SGVec3<T>& v)
384 {
385   T normv = norm(v);
386   if (normv <= SGLimits<T>::min())
387     return SGVec3<T>::zeros();
388   return (1/normv)*v;
389 }
390
391 /// Return true if exactly the same
392 template<typename T>
393 inline
394 bool
395 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
396 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
397
398 /// Return true if not exactly the same
399 template<typename T>
400 inline
401 bool
402 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
403 { return ! (v1 == v2); }
404
405 /// Return true if smaller, good for putting that into a std::map
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 {
423   if (v1(0) < v2(0)) return true;
424   else if (v2(0) < v1(0)) return false;
425   else if (v1(1) < v2(1)) return true;
426   else if (v2(1) < v1(1)) return false;
427   else return (v1(2) <= v2(2));
428 }
429
430 template<typename T>
431 inline
432 bool
433 operator>(const SGVec3<T>& v1, const SGVec3<T>& v2)
434 { return operator<(v2, v1); }
435
436 template<typename T>
437 inline
438 bool
439 operator>=(const SGVec3<T>& v1, const SGVec3<T>& v2)
440 { return operator<=(v2, v1); }
441
442 /// Return true if equal to the relative tolerance tol
443 template<typename T>
444 inline
445 bool
446 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
447 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
448
449 /// Return true if equal to the relative tolerance tol
450 template<typename T>
451 inline
452 bool
453 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
454 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
455
456 /// Return true if about equal to roundoff of the underlying type
457 template<typename T>
458 inline
459 bool
460 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
461 {
462   T tol = 100*SGLimits<T>::epsilon();
463   return equivalent(v1, v2, tol, tol);
464 }
465
466 /// The euclidean distance of the two vectors
467 template<typename T>
468 inline
469 T
470 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
471 { return norm(v1 - v2); }
472
473 /// The squared euclidean distance of the two vectors
474 template<typename T>
475 inline
476 T
477 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
478 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
479
480 // calculate the projection of u along the direction of d.
481 template<typename T>
482 inline
483 SGVec3<T>
484 projection(const SGVec3<T>& u, const SGVec3<T>& d)
485 {
486   T denom = dot(d, d);
487   T ud = dot(u, d);
488   if (SGLimits<T>::min() < denom) return u;
489   else return d * (dot(u, d) / denom);
490 }
491
492 #ifndef NDEBUG
493 template<typename T>
494 inline
495 bool
496 isNaN(const SGVec3<T>& v)
497 {
498   return SGMisc<T>::isNaN(v(0)) ||
499     SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
500 }
501 #endif
502
503 /// Output to an ostream
504 template<typename char_type, typename traits_type, typename T>
505 inline
506 std::basic_ostream<char_type, traits_type>&
507 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
508 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
509
510 inline
511 SGVec3f
512 toVec3f(const SGVec3d& v)
513 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
514
515 inline
516 SGVec3d
517 toVec3d(const SGVec3f& v)
518 { return SGVec3d(v(0), v(1), v(2)); }
519
520 #endif