]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec3.hxx
f2b7406a647083cdc5d4422d81b08cde6ba51312
[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   /// Default constructor. Does not initialize at all.
33   /// If you need them zero initialized, use SGVec3::zeros()
34   SGVec3(void)
35   {
36     /// Initialize with nans in the debug build, that will guarantee to have
37     /// a fast uninitialized default constructor in the release but shows up
38     /// uninitialized values in the debug build very fast ...
39 #ifndef NDEBUG
40     for (unsigned i = 0; i < 3; ++i)
41       data()[i] = SGLimits<T>::quiet_NaN();
42 #endif
43   }
44   /// Constructor. Initialize by the given values
45   SGVec3(T x, T y, T z)
46   { data()[0] = x; data()[1] = y; data()[2] = z; }
47   /// Constructor. Initialize by the content of a plain array,
48   /// make sure it has at least 3 elements
49   explicit SGVec3(const T* d)
50   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
51   template<typename S>
52   explicit SGVec3(const SGVec3<S>& d)
53   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
54   explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
55   { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
56 #ifndef NO_OPENSCENEGRAPH_INTERFACE
57   explicit SGVec3(const osg::Vec3f& d)
58   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
59   explicit SGVec3(const osg::Vec3d& d)
60   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
61 #endif
62
63   /// Access by index, the index is unchecked
64   const T& operator()(unsigned i) const
65   { return data()[i]; }
66   /// Access by index, the index is unchecked
67   T& operator()(unsigned i)
68   { return data()[i]; }
69
70   /// Access raw data by index, the index is unchecked
71   const T& operator[](unsigned i) const
72   { return data()[i]; }
73   /// Access raw data by index, the index is unchecked
74   T& operator[](unsigned i)
75   { return data()[i]; }
76
77   /// Access the x component
78   const T& x(void) const
79   { return data()[0]; }
80   /// Access the x component
81   T& x(void)
82   { return data()[0]; }
83   /// Access the y component
84   const T& y(void) const
85   { return data()[1]; }
86   /// Access the y component
87   T& y(void)
88   { return data()[1]; }
89   /// Access the z component
90   const T& z(void) const
91   { return data()[2]; }
92   /// Access the z component
93   T& z(void)
94   { return data()[2]; }
95
96   /// Readonly raw storage interface
97   const T (&data(void) const)[3]
98   { return _data; }
99   /// Readonly raw storage interface
100   T (&data(void))[3]
101   { return _data; }
102
103 #ifndef NO_OPENSCENEGRAPH_INTERFACE
104   osg::Vec3d osg() const
105   { return osg::Vec3d(data()[0], data()[1], data()[2]); }
106 #endif
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 /// The euclidean norm of the vector, that is what most people call length
367 template<typename T>
368 inline
369 SGVec3<T>
370 normalize(const SGVec3<T>& v)
371 { return (1/norm(v))*v; }
372
373 /// Return true if exactly the same
374 template<typename T>
375 inline
376 bool
377 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
378 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
379
380 /// Return true if not exactly the same
381 template<typename T>
382 inline
383 bool
384 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
385 { return ! (v1 == v2); }
386
387 /// Return true if smaller, good for putting that into a std::map
388 template<typename T>
389 inline
390 bool
391 operator<(const SGVec3<T>& v1, const SGVec3<T>& v2)
392 {
393   if (v1(0) < v2(0)) return true;
394   else if (v2(0) < v1(0)) return false;
395   else if (v1(1) < v2(1)) return true;
396   else if (v2(1) < v1(1)) return false;
397   else return (v1(2) < v2(2));
398 }
399
400 template<typename T>
401 inline
402 bool
403 operator<=(const SGVec3<T>& v1, const SGVec3<T>& v2)
404 {
405   if (v1(0) < v2(0)) return true;
406   else if (v2(0) < v1(0)) return false;
407   else if (v1(1) < v2(1)) return true;
408   else if (v2(1) < v1(1)) return false;
409   else return (v1(2) <= v2(2));
410 }
411
412 template<typename T>
413 inline
414 bool
415 operator>(const SGVec3<T>& v1, const SGVec3<T>& v2)
416 { return operator<(v2, v1); }
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 /// Return true if equal to the relative tolerance tol
425 template<typename T>
426 inline
427 bool
428 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
429 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
430
431 /// Return true if equal to the relative tolerance tol
432 template<typename T>
433 inline
434 bool
435 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
436 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
437
438 /// Return true if about equal to roundoff of the underlying type
439 template<typename T>
440 inline
441 bool
442 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
443 {
444   T tol = 100*SGLimits<T>::epsilon();
445   return equivalent(v1, v2, tol, tol);
446 }
447
448 /// The euclidean distance of the two vectors
449 template<typename T>
450 inline
451 T
452 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
453 { return norm(v1 - v2); }
454
455 /// The squared euclidean distance of the two vectors
456 template<typename T>
457 inline
458 T
459 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
460 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
461
462 #ifndef NDEBUG
463 template<typename T>
464 inline
465 bool
466 isNaN(const SGVec3<T>& v)
467 {
468   return SGMisc<T>::isNaN(v(0)) ||
469     SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
470 }
471 #endif
472
473 /// Output to an ostream
474 template<typename char_type, typename traits_type, typename T>
475 inline
476 std::basic_ostream<char_type, traits_type>&
477 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
478 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
479
480 inline
481 SGVec3f
482 toVec3f(const SGVec3d& v)
483 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
484
485 inline
486 SGVec3d
487 toVec3d(const SGVec3f& v)
488 { return SGVec3d(v(0), v(1), v(2)); }
489
490 #ifndef NO_OPENSCENEGRAPH_INTERFACE
491 inline
492 SGVec3d
493 toSG(const osg::Vec3d& v)
494 { return SGVec3d(v[0], v[1], v[2]); }
495
496 inline
497 SGVec3f
498 toSG(const osg::Vec3f& v)
499 { return SGVec3f(v[0], v[1], v[2]); }
500
501 inline
502 osg::Vec3d
503 toOsg(const SGVec3d& v)
504 { return osg::Vec3d(v[0], v[1], v[2]); }
505
506 inline
507 osg::Vec3f
508 toOsg(const SGVec3f& v)
509 { return osg::Vec3f(v[0], v[1], v[2]); }
510 #endif
511
512 #endif