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