]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec3.hxx
Boolean uniforms are now updatable by properties
[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 /// 3D Vector Class
22 template<typename T>
23 class SGVec3 {
24 public:
25   typedef T value_type;
26
27 #ifdef __GNUC__
28 // Avoid "_data not initialized" warnings (see comment below).
29 #   pragma GCC diagnostic ignored "-Wuninitialized"
30 #endif
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
45 #ifdef __GNUC__
46   // Restore warning settings.
47 #   pragma GCC diagnostic warning "-Wuninitialized"
48 #endif
49
50   /// Constructor. Initialize by the given values
51   SGVec3(T x, T y, T z)
52   { data()[0] = x; data()[1] = y; data()[2] = z; }
53   /// Constructor. Initialize by the content of a plain array,
54   /// make sure it has at least 3 elements
55   explicit SGVec3(const T* d)
56   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
57   template<typename S>
58   explicit SGVec3(const SGVec3<S>& d)
59   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
60   explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
61   { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
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   /// Inplace addition
104   SGVec3& operator+=(const SGVec3& v)
105   { data()[0] += v(0); data()[1] += v(1); data()[2] += v(2); return *this; }
106   /// Inplace subtraction
107   SGVec3& operator-=(const SGVec3& v)
108   { data()[0] -= v(0); data()[1] -= v(1); data()[2] -= v(2); return *this; }
109   /// Inplace scalar multiplication
110   template<typename S>
111   SGVec3& operator*=(S s)
112   { data()[0] *= s; data()[1] *= s; data()[2] *= s; return *this; }
113   /// Inplace scalar multiplication by 1/s
114   template<typename S>
115   SGVec3& operator/=(S s)
116   { return operator*=(1/T(s)); }
117
118   /// Return an all zero vector
119   static SGVec3 zeros(void)
120   { return SGVec3(0, 0, 0); }
121   /// Return unit vectors
122   static SGVec3 e1(void)
123   { return SGVec3(1, 0, 0); }
124   static SGVec3 e2(void)
125   { return SGVec3(0, 1, 0); }
126   static SGVec3 e3(void)
127   { return SGVec3(0, 0, 1); }
128
129   /// Constructor. Initialize by a geodetic coordinate
130   /// Note that this conversion is relatively expensive to compute
131   static SGVec3 fromGeod(const SGGeod& geod);
132   /// Constructor. Initialize by a geocentric coordinate
133   /// Note that this conversion is relatively expensive to compute
134   static SGVec3 fromGeoc(const SGGeoc& geoc);
135
136 private:
137   T _data[3];
138 };
139
140 template<>
141 inline
142 SGVec3<double>
143 SGVec3<double>::fromGeod(const SGGeod& geod)
144 {
145   SGVec3<double> cart;
146   SGGeodesy::SGGeodToCart(geod, cart);
147   return cart;
148 }
149
150 template<>
151 inline
152 SGVec3<float>
153 SGVec3<float>::fromGeod(const SGGeod& geod)
154 {
155   SGVec3<double> cart;
156   SGGeodesy::SGGeodToCart(geod, cart);
157   return SGVec3<float>(cart(0), cart(1), cart(2));
158 }
159
160 template<>
161 inline
162 SGVec3<double>
163 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
164 {
165   SGVec3<double> cart;
166   SGGeodesy::SGGeocToCart(geoc, cart);
167   return cart;
168 }
169
170 template<>
171 inline
172 SGVec3<float>
173 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
174 {
175   SGVec3<double> cart;
176   SGGeodesy::SGGeocToCart(geoc, cart);
177   return SGVec3<float>(cart(0), cart(1), cart(2));
178 }
179
180 /// Unary +, do nothing ...
181 template<typename T>
182 inline
183 const SGVec3<T>&
184 operator+(const SGVec3<T>& v)
185 { return v; }
186
187 /// Unary -, do nearly nothing
188 template<typename T>
189 inline
190 SGVec3<T>
191 operator-(const SGVec3<T>& v)
192 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
193
194 /// Binary +
195 template<typename T>
196 inline
197 SGVec3<T>
198 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
199 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
200
201 /// Binary -
202 template<typename T>
203 inline
204 SGVec3<T>
205 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
206 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
207
208 /// Scalar multiplication
209 template<typename S, typename T>
210 inline
211 SGVec3<T>
212 operator*(S s, const SGVec3<T>& v)
213 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
214
215 /// Scalar multiplication
216 template<typename S, typename T>
217 inline
218 SGVec3<T>
219 operator*(const SGVec3<T>& v, S s)
220 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
221
222 /// multiplication as a multiplicator, that is assume that the first vector
223 /// represents a 3x3 diagonal matrix with the diagonal elements in the vector.
224 /// Then the result is the product of that matrix times the second vector.
225 template<typename T>
226 inline
227 SGVec3<T>
228 mult(const SGVec3<T>& v1, const SGVec3<T>& v2)
229 { return SGVec3<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2)); }
230
231 /// component wise min
232 template<typename T>
233 inline
234 SGVec3<T>
235 min(const SGVec3<T>& v1, const SGVec3<T>& v2)
236 {
237   return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
238                    SGMisc<T>::min(v1(1), v2(1)),
239                    SGMisc<T>::min(v1(2), v2(2)));
240 }
241 template<typename S, typename T>
242 inline
243 SGVec3<T>
244 min(const SGVec3<T>& v, S s)
245 {
246   return SGVec3<T>(SGMisc<T>::min(s, v(0)),
247                    SGMisc<T>::min(s, v(1)),
248                    SGMisc<T>::min(s, v(2)));
249 }
250 template<typename S, typename T>
251 inline
252 SGVec3<T>
253 min(S s, const SGVec3<T>& v)
254 {
255   return SGVec3<T>(SGMisc<T>::min(s, v(0)),
256                    SGMisc<T>::min(s, v(1)),
257                    SGMisc<T>::min(s, v(2)));
258 }
259
260 /// component wise max
261 template<typename T>
262 inline
263 SGVec3<T>
264 max(const SGVec3<T>& v1, const SGVec3<T>& v2)
265 {
266   return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
267                    SGMisc<T>::max(v1(1), v2(1)),
268                    SGMisc<T>::max(v1(2), v2(2)));
269 }
270 template<typename S, typename T>
271 inline
272 SGVec3<T>
273 max(const SGVec3<T>& v, S s)
274 {
275   return SGVec3<T>(SGMisc<T>::max(s, v(0)),
276                    SGMisc<T>::max(s, v(1)),
277                    SGMisc<T>::max(s, v(2)));
278 }
279 template<typename S, typename T>
280 inline
281 SGVec3<T>
282 max(S s, const SGVec3<T>& v)
283 {
284   return SGVec3<T>(SGMisc<T>::max(s, v(0)),
285                    SGMisc<T>::max(s, v(1)),
286                    SGMisc<T>::max(s, v(2)));
287 }
288
289 /// Scalar dot product
290 template<typename T>
291 inline
292 T
293 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
294 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
295
296 /// The euclidean norm of the vector, that is what most people call length
297 template<typename T>
298 inline
299 T
300 norm(const SGVec3<T>& v)
301 { return sqrt(dot(v, v)); }
302
303 /// The euclidean norm of the vector, that is what most people call length
304 template<typename T>
305 inline
306 T
307 length(const SGVec3<T>& v)
308 { return sqrt(dot(v, v)); }
309
310 /// The 1-norm of the vector, this one is the fastest length function we
311 /// can implement on modern cpu's
312 template<typename T>
313 inline
314 T
315 norm1(const SGVec3<T>& v)
316 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
317
318 /// The inf-norm of the vector
319 template<typename T>
320 inline
321 T
322 normI(const SGVec3<T>& v)
323 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
324
325 /// Vector cross product
326 template<typename T>
327 inline
328 SGVec3<T>
329 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
330 {
331   return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
332                    v1(2)*v2(0) - v1(0)*v2(2),
333                    v1(0)*v2(1) - v1(1)*v2(0));
334 }
335
336 /// return any normalized vector perpendicular to v
337 template<typename T>
338 inline
339 SGVec3<T>
340 perpendicular(const SGVec3<T>& v)
341 {
342   T absv1 = fabs(v(0));
343   T absv2 = fabs(v(1));
344   T absv3 = fabs(v(2));
345
346   if (absv2 < absv1 && absv3 < absv1) {
347     T quot = v(1)/v(0);
348     return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
349   } else if (absv3 < absv2) {
350     T quot = v(2)/v(1);
351     return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
352   } else if (SGLimits<T>::min() < absv3) {
353     T quot = v(0)/v(2);
354     return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
355   } else {
356     // the all zero case ...
357     return SGVec3<T>(0, 0, 0);
358   }
359 }
360
361 /// Construct a unit vector in the given direction.
362 /// or the zero vector if the input vector is zero.
363 template<typename T>
364 inline
365 SGVec3<T>
366 normalize(const SGVec3<T>& v)
367 {
368   T normv = norm(v);
369   if (normv <= SGLimits<T>::min())
370     return SGVec3<T>::zeros();
371   return (1/normv)*v;
372 }
373
374 /// Return true if exactly the same
375 template<typename T>
376 inline
377 bool
378 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
379 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
380
381 /// Return true if not exactly the same
382 template<typename T>
383 inline
384 bool
385 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
386 { return ! (v1 == v2); }
387
388 /// Return true if smaller, good for putting that into a std::map
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 {
406   if (v1(0) < v2(0)) return true;
407   else if (v2(0) < v1(0)) return false;
408   else if (v1(1) < v2(1)) return true;
409   else if (v2(1) < v1(1)) return false;
410   else return (v1(2) <= v2(2));
411 }
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 template<typename T>
420 inline
421 bool
422 operator>=(const SGVec3<T>& v1, const SGVec3<T>& v2)
423 { return operator<=(v2, v1); }
424
425 /// Return true if equal to the relative tolerance tol
426 template<typename T>
427 inline
428 bool
429 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
430 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
431
432 /// Return true if equal to the relative tolerance tol
433 template<typename T>
434 inline
435 bool
436 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
437 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
438
439 /// Return true if about equal to roundoff of the underlying type
440 template<typename T>
441 inline
442 bool
443 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
444 {
445   T tol = 100*SGLimits<T>::epsilon();
446   return equivalent(v1, v2, tol, tol);
447 }
448
449 /// The euclidean distance of the two vectors
450 template<typename T>
451 inline
452 T
453 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
454 { return norm(v1 - v2); }
455
456 /// The squared euclidean distance of the two vectors
457 template<typename T>
458 inline
459 T
460 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
461 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
462
463 // calculate the projection of u along the direction of d.
464 template<typename T>
465 inline
466 SGVec3<T>
467 projection(const SGVec3<T>& u, const SGVec3<T>& d)
468 {
469   T denom = dot(d, d);
470   T ud = dot(u, d);
471   if (SGLimits<T>::min() < denom) return u;
472   else return d * (dot(u, d) / denom);
473 }
474
475 #ifndef NDEBUG
476 template<typename T>
477 inline
478 bool
479 isNaN(const SGVec3<T>& v)
480 {
481   return SGMisc<T>::isNaN(v(0)) ||
482     SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
483 }
484 #endif
485
486 /// Output to an ostream
487 template<typename char_type, typename traits_type, typename T>
488 inline
489 std::basic_ostream<char_type, traits_type>&
490 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
491 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
492
493 inline
494 SGVec3f
495 toVec3f(const SGVec3d& v)
496 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
497
498 inline
499 SGVec3d
500 toVec3d(const SGVec3f& v)
501 { return SGVec3d(v(0), v(1), v(2)); }
502
503 #endif