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