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