]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec3.hxx
cbdf0fa2c24d355dfdcded0da93b9d17a758e527
[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 /// The euclidean norm of the vector, that is what most people call length
356 template<typename T>
357 inline
358 SGVec3<T>
359 normalize(const SGVec3<T>& v)
360 { return (1/norm(v))*v; }
361
362 /// Return true if exactly the same
363 template<typename T>
364 inline
365 bool
366 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
367 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
368
369 /// Return true if not exactly the same
370 template<typename T>
371 inline
372 bool
373 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
374 { return ! (v1 == v2); }
375
376 /// Return true if smaller, good for putting that into a std::map
377 template<typename T>
378 inline
379 bool
380 operator<(const SGVec3<T>& v1, const SGVec3<T>& v2)
381 {
382   if (v1(0) < v2(0)) return true;
383   else if (v2(0) < v1(0)) return false;
384   else if (v1(1) < v2(1)) return true;
385   else if (v2(1) < v1(1)) return false;
386   else return (v1(2) < v2(2));
387 }
388
389 template<typename T>
390 inline
391 bool
392 operator<=(const SGVec3<T>& v1, const SGVec3<T>& v2)
393 {
394   if (v1(0) < v2(0)) return true;
395   else if (v2(0) < v1(0)) return false;
396   else if (v1(1) < v2(1)) return true;
397   else if (v2(1) < v1(1)) return false;
398   else return (v1(2) <= v2(2));
399 }
400
401 template<typename T>
402 inline
403 bool
404 operator>(const SGVec3<T>& v1, const SGVec3<T>& v2)
405 { return operator<(v2, v1); }
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 /// Return true if equal to the relative tolerance tol
414 template<typename T>
415 inline
416 bool
417 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
418 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
419
420 /// Return true if equal to the relative tolerance tol
421 template<typename T>
422 inline
423 bool
424 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
425 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
426
427 /// Return true if about equal to roundoff of the underlying type
428 template<typename T>
429 inline
430 bool
431 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
432 {
433   T tol = 100*SGLimits<T>::epsilon();
434   return equivalent(v1, v2, tol, tol);
435 }
436
437 /// The euclidean distance of the two vectors
438 template<typename T>
439 inline
440 T
441 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
442 { return norm(v1 - v2); }
443
444 /// The squared euclidean distance of the two vectors
445 template<typename T>
446 inline
447 T
448 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
449 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
450
451 #ifndef NDEBUG
452 template<typename T>
453 inline
454 bool
455 isNaN(const SGVec3<T>& v)
456 {
457   return SGMisc<T>::isNaN(v(0)) ||
458     SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
459 }
460 #endif
461
462 /// Output to an ostream
463 template<typename char_type, typename traits_type, typename T>
464 inline
465 std::basic_ostream<char_type, traits_type>&
466 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
467 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
468
469 inline
470 SGVec3f
471 toVec3f(const SGVec3d& v)
472 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
473
474 inline
475 SGVec3d
476 toVec3d(const SGVec3f& v)
477 { return SGVec3d(v(0), v(1), v(2)); }
478
479 #ifndef NO_OPENSCENEGRAPH_INTERFACE
480 inline
481 SGVec3d
482 toSG(const osg::Vec3d& v)
483 { return SGVec3d(v[0], v[1], v[2]); }
484
485 inline
486 SGVec3f
487 toSG(const osg::Vec3f& v)
488 { return SGVec3f(v[0], v[1], v[2]); }
489
490 inline
491 osg::Vec3d
492 toOsg(const SGVec3d& v)
493 { return osg::Vec3d(v[0], v[1], v[2]); }
494
495 inline
496 osg::Vec3f
497 toOsg(const SGVec3f& v)
498 { return osg::Vec3f(v[0], v[1], v[2]); }
499 #endif
500
501 #endif