]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec3.hxx
Modified Files:
[simgear.git] / simgear / math / SGVec3.hxx
1 // Copyright (C) 2006  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 <osg/Vec3f>
22 #include <osg/Vec3d>
23
24 template<typename T>
25 struct SGVec3Storage {
26   /// Readonly raw storage interface
27   const T (&data(void) const)[3]
28   { return _data; }
29   /// Readonly raw storage interface
30   T (&data(void))[3]
31   { return _data; }
32
33   void osg() const
34   { }
35
36 private:
37   T _data[3];
38 };
39
40 template<>
41 struct SGVec3Storage<float> : public osg::Vec3f {
42   /// Access raw data by index, the index is unchecked
43   const float (&data(void) const)[3]
44   { return osg::Vec3f::_v; }
45   /// Access raw data by index, the index is unchecked
46   float (&data(void))[3]
47   { return osg::Vec3f::_v; }
48
49   const osg::Vec3f& osg() const
50   { return *this; }
51   osg::Vec3f& osg()
52   { return *this; }
53 };
54
55 template<>
56 struct SGVec3Storage<double> : public osg::Vec3d {
57   /// Access raw data by index, the index is unchecked
58   const double (&data(void) const)[3]
59   { return osg::Vec3d::_v; }
60   /// Access raw data by index, the index is unchecked
61   double (&data(void))[3]
62   { return osg::Vec3d::_v; }
63
64   const osg::Vec3d& osg() const
65   { return *this; }
66   osg::Vec3d& osg()
67   { return *this; }
68 };
69
70 /// 3D Vector Class
71 template<typename T>
72 class SGVec3 : protected SGVec3Storage<T> {
73 public:
74   typedef T value_type;
75
76   /// Default constructor. Does not initialize at all.
77   /// If you need them zero initialized, use SGVec3::zeros()
78   SGVec3(void)
79   {
80     /// Initialize with nans in the debug build, that will guarantee to have
81     /// a fast uninitialized default constructor in the release but shows up
82     /// uninitialized values in the debug build very fast ...
83 #ifndef NDEBUG
84     for (unsigned i = 0; i < 3; ++i)
85       data()[i] = SGLimits<T>::quiet_NaN();
86 #endif
87   }
88   /// Constructor. Initialize by the given values
89   SGVec3(T x, T y, T z)
90   { data()[0] = x; data()[1] = y; data()[2] = z; }
91   /// Constructor. Initialize by the content of a plain array,
92   /// make sure it has at least 3 elements
93   explicit SGVec3(const T* d)
94   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
95   explicit SGVec3(const osg::Vec3f& d)
96   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
97   explicit SGVec3(const osg::Vec3d& d)
98   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
99
100   /// Access by index, the index is unchecked
101   const T& operator()(unsigned i) const
102   { return data()[i]; }
103   /// Access by index, the index is unchecked
104   T& operator()(unsigned i)
105   { return data()[i]; }
106
107   /// Access raw data by index, the index is unchecked
108   const T& operator[](unsigned i) const
109   { return data()[i]; }
110   /// Access raw data by index, the index is unchecked
111   T& operator[](unsigned i)
112   { return data()[i]; }
113
114   /// Access the x component
115   const T& x(void) const
116   { return data()[0]; }
117   /// Access the x component
118   T& x(void)
119   { return data()[0]; }
120   /// Access the y component
121   const T& y(void) const
122   { return data()[1]; }
123   /// Access the y component
124   T& y(void)
125   { return data()[1]; }
126   /// Access the z component
127   const T& z(void) const
128   { return data()[2]; }
129   /// Access the z component
130   T& z(void)
131   { return data()[2]; }
132
133   /// Get the data pointer
134   using SGVec3Storage<T>::data;
135
136   /// Readonly interface function to ssg's sgVec3/sgdVec3
137   const T (&sg(void) const)[3]
138   { return data(); }
139   /// Interface function to ssg's sgVec3/sgdVec3
140   T (&sg(void))[3]
141   { return data(); }
142
143   /// Interface function to osg's Vec3*
144   using SGVec3Storage<T>::osg;
145
146   /// Inplace addition
147   SGVec3& operator+=(const SGVec3& v)
148   { data()[0] += v(0); data()[1] += v(1); data()[2] += v(2); return *this; }
149   /// Inplace subtraction
150   SGVec3& operator-=(const SGVec3& v)
151   { data()[0] -= v(0); data()[1] -= v(1); data()[2] -= v(2); return *this; }
152   /// Inplace scalar multiplication
153   template<typename S>
154   SGVec3& operator*=(S s)
155   { data()[0] *= s; data()[1] *= s; data()[2] *= s; return *this; }
156   /// Inplace scalar multiplication by 1/s
157   template<typename S>
158   SGVec3& operator/=(S s)
159   { return operator*=(1/T(s)); }
160
161   /// Return an all zero vector
162   static SGVec3 zeros(void)
163   { return SGVec3(0, 0, 0); }
164   /// Return unit vectors
165   static SGVec3 e1(void)
166   { return SGVec3(1, 0, 0); }
167   static SGVec3 e2(void)
168   { return SGVec3(0, 1, 0); }
169   static SGVec3 e3(void)
170   { return SGVec3(0, 0, 1); }
171
172   /// Constructor. Initialize by a geodetic coordinate
173   /// Note that this conversion is relatively expensive to compute
174   static SGVec3 fromGeod(const SGGeod& geod);
175   /// Constructor. Initialize by a geocentric coordinate
176   /// Note that this conversion is relatively expensive to compute
177   static SGVec3 fromGeoc(const SGGeoc& geoc);
178 };
179
180 template<>
181 inline
182 SGVec3<double>
183 SGVec3<double>::fromGeod(const SGGeod& geod)
184 {
185   SGVec3<double> cart;
186   SGGeodesy::SGGeodToCart(geod, cart);
187   return cart;
188 }
189
190 template<>
191 inline
192 SGVec3<float>
193 SGVec3<float>::fromGeod(const SGGeod& geod)
194 {
195   SGVec3<double> cart;
196   SGGeodesy::SGGeodToCart(geod, cart);
197   return SGVec3<float>(cart(0), cart(1), cart(2));
198 }
199
200 template<>
201 inline
202 SGVec3<double>
203 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
204 {
205   SGVec3<double> cart;
206   SGGeodesy::SGGeocToCart(geoc, cart);
207   return cart;
208 }
209
210 template<>
211 inline
212 SGVec3<float>
213 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
214 {
215   SGVec3<double> cart;
216   SGGeodesy::SGGeocToCart(geoc, cart);
217   return SGVec3<float>(cart(0), cart(1), cart(2));
218 }
219
220 /// Unary +, do nothing ...
221 template<typename T>
222 inline
223 const SGVec3<T>&
224 operator+(const SGVec3<T>& v)
225 { return v; }
226
227 /// Unary -, do nearly nothing
228 template<typename T>
229 inline
230 SGVec3<T>
231 operator-(const SGVec3<T>& v)
232 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
233
234 /// Binary +
235 template<typename T>
236 inline
237 SGVec3<T>
238 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
239 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
240
241 /// Binary -
242 template<typename T>
243 inline
244 SGVec3<T>
245 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
246 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
247
248 /// Scalar multiplication
249 template<typename S, typename T>
250 inline
251 SGVec3<T>
252 operator*(S s, const SGVec3<T>& v)
253 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
254
255 /// Scalar multiplication
256 template<typename S, typename T>
257 inline
258 SGVec3<T>
259 operator*(const SGVec3<T>& v, S s)
260 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
261
262 /// component wise min
263 template<typename T>
264 inline
265 SGVec3<T>
266 min(const SGVec3<T>& v1, const SGVec3<T>& v2)
267 {
268   return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
269                    SGMisc<T>::min(v1(1), v2(1)),
270                    SGMisc<T>::min(v1(2), v2(2)));
271 }
272 template<typename S, typename T>
273 inline
274 SGVec3<T>
275 min(const SGVec3<T>& v, S s)
276 {
277   return SGVec3<T>(SGMisc<T>::min(s, v(0)),
278                    SGMisc<T>::min(s, v(1)),
279                    SGMisc<T>::min(s, v(2)));
280 }
281 template<typename S, typename T>
282 inline
283 SGVec3<T>
284 min(S s, const SGVec3<T>& v)
285 {
286   return SGVec3<T>(SGMisc<T>::min(s, v(0)),
287                    SGMisc<T>::min(s, v(1)),
288                    SGMisc<T>::min(s, v(2)));
289 }
290
291 /// component wise max
292 template<typename T>
293 inline
294 SGVec3<T>
295 max(const SGVec3<T>& v1, const SGVec3<T>& v2)
296 {
297   return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
298                    SGMisc<T>::max(v1(1), v2(1)),
299                    SGMisc<T>::max(v1(2), v2(2)));
300 }
301 template<typename S, typename T>
302 inline
303 SGVec3<T>
304 max(const SGVec3<T>& v, S s)
305 {
306   return SGVec3<T>(SGMisc<T>::max(s, v(0)),
307                    SGMisc<T>::max(s, v(1)),
308                    SGMisc<T>::max(s, v(2)));
309 }
310 template<typename S, typename T>
311 inline
312 SGVec3<T>
313 max(S s, const SGVec3<T>& v)
314 {
315   return SGVec3<T>(SGMisc<T>::max(s, v(0)),
316                    SGMisc<T>::max(s, v(1)),
317                    SGMisc<T>::max(s, v(2)));
318 }
319
320 /// Scalar dot product
321 template<typename T>
322 inline
323 T
324 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
325 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
326
327 /// The euclidean norm of the vector, that is what most people call length
328 template<typename T>
329 inline
330 T
331 norm(const SGVec3<T>& v)
332 { return sqrt(dot(v, v)); }
333
334 /// The euclidean norm of the vector, that is what most people call length
335 template<typename T>
336 inline
337 T
338 length(const SGVec3<T>& v)
339 { return sqrt(dot(v, v)); }
340
341 /// The 1-norm of the vector, this one is the fastest length function we
342 /// can implement on modern cpu's
343 template<typename T>
344 inline
345 T
346 norm1(const SGVec3<T>& v)
347 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
348
349 /// The inf-norm of the vector
350 template<typename T>
351 inline
352 T
353 normI(const SGVec3<T>& v)
354 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
355
356 /// Vector cross product
357 template<typename T>
358 inline
359 SGVec3<T>
360 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
361 {
362   return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
363                    v1(2)*v2(0) - v1(0)*v2(2),
364                    v1(0)*v2(1) - v1(1)*v2(0));
365 }
366
367 /// return any normalized vector perpendicular to v
368 template<typename T>
369 inline
370 SGVec3<T>
371 perpendicular(const SGVec3<T>& v)
372 {
373   T absv1 = fabs(v(0));
374   T absv2 = fabs(v(1));
375   T absv3 = fabs(v(2));
376
377   if (absv2 < absv1 && absv3 < absv1) {
378     T quot = v(1)/v(0);
379     return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
380   } else if (absv1 < absv2 && absv3 < absv2) {
381     T quot = v(2)/v(1);
382     return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
383   } else if (absv1 < absv3 && absv2 < absv3) {
384     T quot = v(0)/v(2);
385     return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
386   } else {
387     // the all zero case ...
388     return SGVec3<T>(0, 0, 0);
389   }
390 }
391
392 /// The euclidean norm of the vector, that is what most people call length
393 template<typename T>
394 inline
395 SGVec3<T>
396 normalize(const SGVec3<T>& v)
397 { return (1/norm(v))*v; }
398
399 /// Return true if exactly the same
400 template<typename T>
401 inline
402 bool
403 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
404 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
405
406 /// Return true if not exactly the same
407 template<typename T>
408 inline
409 bool
410 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
411 { return ! (v1 == v2); }
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 #endif