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