]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec4.hxx
Merge branch 'maint' into next
[simgear.git] / simgear / math / SGVec4.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 SGVec4_H
19 #define SGVec4_H
20
21 #include <osg/Vec4f>
22 #include <osg/Vec4d>
23
24 template<typename T>
25 struct SGVec4Storage {
26   /// Readonly raw storage interface
27   const T (&data(void) const)[4]
28   { return _data; }
29   /// Readonly raw storage interface
30   T (&data(void))[4]
31   { return _data; }
32
33   void osg() const
34   { }
35
36 private:
37   T _data[4];
38 };
39
40 template<>
41 struct SGVec4Storage<float> : public osg::Vec4f {
42   /// Access raw data by index, the index is unchecked
43   const float (&data(void) const)[4]
44   { return osg::Vec4f::_v; }
45   /// Access raw data by index, the index is unchecked
46   float (&data(void))[4]
47   { return osg::Vec4f::_v; }
48
49   const osg::Vec4f& osg() const
50   { return *this; }
51   osg::Vec4f& osg()
52   { return *this; }
53 };
54
55 template<>
56 struct SGVec4Storage<double> : public osg::Vec4d {
57   /// Access raw data by index, the index is unchecked
58   const double (&data(void) const)[4]
59   { return osg::Vec4d::_v; }
60   /// Access raw data by index, the index is unchecked
61   double (&data(void))[4]
62   { return osg::Vec4d::_v; }
63
64   const osg::Vec4d& osg() const
65   { return *this; }
66   osg::Vec4d& osg()
67   { return *this; }
68 };
69
70 /// 4D Vector Class
71 template<typename T>
72 class SGVec4 : protected SGVec4Storage<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 SGVec4::zeros()
78   SGVec4(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 < 4; ++i)
85       data()[i] = SGLimits<T>::quiet_NaN();
86 #endif
87   }
88   /// Constructor. Initialize by the given values
89   SGVec4(T x, T y, T z, T w)
90   { data()[0] = x; data()[1] = y; data()[2] = z; data()[3] = w; }
91   /// Constructor. Initialize by the content of a plain array,
92   /// make sure it has at least 3 elements
93   explicit SGVec4(const T* d)
94   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
95   template<typename S>
96   explicit SGVec4(const SGVec4<S>& d)
97   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
98   explicit SGVec4(const osg::Vec4f& d)
99   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
100   explicit SGVec4(const osg::Vec4d& d)
101   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
102   explicit SGVec4(const SGVec3<T>& v3, const T& v4 = 0)
103   { data()[0] = v3[0]; data()[1] = v3[1]; data()[2] = v3[2]; data()[3] = v4; }
104
105
106   /// Access by index, the index is unchecked
107   const T& operator()(unsigned i) const
108   { return data()[i]; }
109   /// Access by index, the index is unchecked
110   T& operator()(unsigned i)
111   { return data()[i]; }
112
113   /// Access raw data by index, the index is unchecked
114   const T& operator[](unsigned i) const
115   { return data()[i]; }
116   /// Access raw data by index, the index is unchecked
117   T& operator[](unsigned i)
118   { return data()[i]; }
119
120   /// Access the x component
121   const T& x(void) const
122   { return data()[0]; }
123   /// Access the x component
124   T& x(void)
125   { return data()[0]; }
126   /// Access the y component
127   const T& y(void) const
128   { return data()[1]; }
129   /// Access the y component
130   T& y(void)
131   { return data()[1]; }
132   /// Access the z component
133   const T& z(void) const
134   { return data()[2]; }
135   /// Access the z component
136   T& z(void)
137   { return data()[2]; }
138   /// Access the x component
139   const T& w(void) const
140   { return data()[3]; }
141   /// Access the x component
142   T& w(void)
143   { return data()[3]; }
144
145   /// Get the data pointer
146   using SGVec4Storage<T>::data;
147
148   /// Readonly interface function to ssg's sgVec4/sgdVec4
149   const T (&sg(void) const)[4]
150   { return data(); }
151   /// Interface function to ssg's sgVec4/sgdVec4
152   T (&sg(void))[4]
153   { return data(); }
154
155   /// Interface function to osg's Vec4*
156   using SGVec4Storage<T>::osg;
157
158   /// Inplace addition
159   SGVec4& operator+=(const SGVec4& v)
160   { data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; }
161   /// Inplace subtraction
162   SGVec4& operator-=(const SGVec4& v)
163   { data()[0]-=v(0);data()[1]-=v(1);data()[2]-=v(2);data()[3]-=v(3);return *this; }
164   /// Inplace scalar multiplication
165   template<typename S>
166   SGVec4& operator*=(S s)
167   { data()[0] *= s; data()[1] *= s; data()[2] *= s; data()[3] *= s; return *this; }
168   /// Inplace scalar multiplication by 1/s
169   template<typename S>
170   SGVec4& operator/=(S s)
171   { return operator*=(1/T(s)); }
172
173   /// Return an all zero vector
174   static SGVec4 zeros(void)
175   { return SGVec4(0, 0, 0, 0); }
176   /// Return unit vectors
177   static SGVec4 e1(void)
178   { return SGVec4(1, 0, 0, 0); }
179   static SGVec4 e2(void)
180   { return SGVec4(0, 1, 0, 0); }
181   static SGVec4 e3(void)
182   { return SGVec4(0, 0, 1, 0); }
183   static SGVec4 e4(void)
184   { return SGVec4(0, 0, 0, 1); }
185 };
186
187 /// Unary +, do nothing ...
188 template<typename T>
189 inline
190 const SGVec4<T>&
191 operator+(const SGVec4<T>& v)
192 { return v; }
193
194 /// Unary -, do nearly nothing
195 template<typename T>
196 inline
197 SGVec4<T>
198 operator-(const SGVec4<T>& v)
199 { return SGVec4<T>(-v(0), -v(1), -v(2), -v(3)); }
200
201 /// Binary +
202 template<typename T>
203 inline
204 SGVec4<T>
205 operator+(const SGVec4<T>& v1, const SGVec4<T>& v2)
206 { return SGVec4<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
207
208 /// Binary -
209 template<typename T>
210 inline
211 SGVec4<T>
212 operator-(const SGVec4<T>& v1, const SGVec4<T>& v2)
213 { return SGVec4<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
214
215 /// Scalar multiplication
216 template<typename S, typename T>
217 inline
218 SGVec4<T>
219 operator*(S s, const SGVec4<T>& v)
220 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
221
222 /// Scalar multiplication
223 template<typename S, typename T>
224 inline
225 SGVec4<T>
226 operator*(const SGVec4<T>& v, S s)
227 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
228
229 /// multiplication as a multiplicator, that is assume that the first vector
230 /// represents a 4x4 diagonal matrix with the diagonal elements in the vector.
231 /// Then the result is the product of that matrix times the second vector.
232 template<typename T>
233 inline
234 SGVec4<T>
235 mult(const SGVec4<T>& v1, const SGVec4<T>& v2)
236 { return SGVec4<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2), v1(3)*v2(3)); }
237
238 /// component wise min
239 template<typename T>
240 inline
241 SGVec4<T>
242 min(const SGVec4<T>& v1, const SGVec4<T>& v2)
243 {
244   return SGVec4<T>(SGMisc<T>::min(v1(0), v2(0)),
245                    SGMisc<T>::min(v1(1), v2(1)),
246                    SGMisc<T>::min(v1(2), v2(2)),
247                    SGMisc<T>::min(v1(3), v2(3)));
248 }
249 template<typename S, typename T>
250 inline
251 SGVec4<T>
252 min(const SGVec4<T>& v, S s)
253 {
254   return SGVec4<T>(SGMisc<T>::min(s, v(0)),
255                    SGMisc<T>::min(s, v(1)),
256                    SGMisc<T>::min(s, v(2)),
257                    SGMisc<T>::min(s, v(3)));
258 }
259 template<typename S, typename T>
260 inline
261 SGVec4<T>
262 min(S s, const SGVec4<T>& v)
263 {
264   return SGVec4<T>(SGMisc<T>::min(s, v(0)),
265                    SGMisc<T>::min(s, v(1)),
266                    SGMisc<T>::min(s, v(2)),
267                    SGMisc<T>::min(s, v(3)));
268 }
269
270 /// component wise max
271 template<typename T>
272 inline
273 SGVec4<T>
274 max(const SGVec4<T>& v1, const SGVec4<T>& v2)
275 {
276   return SGVec4<T>(SGMisc<T>::max(v1(0), v2(0)),
277                    SGMisc<T>::max(v1(1), v2(1)),
278                    SGMisc<T>::max(v1(2), v2(2)),
279                    SGMisc<T>::max(v1(3), v2(3)));
280 }
281 template<typename S, typename T>
282 inline
283 SGVec4<T>
284 max(const SGVec4<T>& v, S s)
285 {
286   return SGVec4<T>(SGMisc<T>::max(s, v(0)),
287                    SGMisc<T>::max(s, v(1)),
288                    SGMisc<T>::max(s, v(2)),
289                    SGMisc<T>::max(s, v(3)));
290 }
291 template<typename S, typename T>
292 inline
293 SGVec4<T>
294 max(S s, const SGVec4<T>& v)
295 {
296   return SGVec4<T>(SGMisc<T>::max(s, v(0)),
297                    SGMisc<T>::max(s, v(1)),
298                    SGMisc<T>::max(s, v(2)),
299                    SGMisc<T>::max(s, v(3)));
300 }
301
302 /// Scalar dot product
303 template<typename T>
304 inline
305 T
306 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
307 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
308
309 /// The euclidean norm of the vector, that is what most people call length
310 template<typename T>
311 inline
312 T
313 norm(const SGVec4<T>& v)
314 { return sqrt(dot(v, v)); }
315
316 /// The euclidean norm of the vector, that is what most people call length
317 template<typename T>
318 inline
319 T
320 length(const SGVec4<T>& v)
321 { return sqrt(dot(v, v)); }
322
323 /// The 1-norm of the vector, this one is the fastest length function we
324 /// can implement on modern cpu's
325 template<typename T>
326 inline
327 T
328 norm1(const SGVec4<T>& v)
329 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
330
331 /// The inf-norm of the vector
332 template<typename T>
333 inline
334 T
335 normI(const SGVec4<T>& v)
336 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2)), fabs(v(2))); }
337
338 /// The euclidean norm of the vector, that is what most people call length
339 template<typename T>
340 inline
341 SGVec4<T>
342 normalize(const SGVec4<T>& v)
343 { return (1/norm(v))*v; }
344
345 /// Return true if exactly the same
346 template<typename T>
347 inline
348 bool
349 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
350 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
351
352 /// Return true if not exactly the same
353 template<typename T>
354 inline
355 bool
356 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
357 { return ! (v1 == v2); }
358
359 /// Return true if smaller, good for putting that into a std::map
360 template<typename T>
361 inline
362 bool
363 operator<(const SGVec4<T>& v1, const SGVec4<T>& v2)
364 {
365   if (v1(0) < v2(0)) return true;
366   else if (v2(0) < v1(0)) return false;
367   else if (v1(1) < v2(1)) return true;
368   else if (v2(1) < v1(1)) return false;
369   else if (v1(2) < v2(2)) return true;
370   else if (v2(2) < v1(2)) return false;
371   else return (v1(3) < v2(3));
372 }
373
374 template<typename T>
375 inline
376 bool
377 operator<=(const SGVec4<T>& v1, const SGVec4<T>& v2)
378 {
379   if (v1(0) < v2(0)) return true;
380   else if (v2(0) < v1(0)) return false;
381   else if (v1(1) < v2(1)) return true;
382   else if (v2(1) < v1(1)) return false;
383   else if (v1(2) < v2(2)) return true;
384   else if (v2(2) < v1(2)) return false;
385   else return (v1(3) <= v2(3));
386 }
387
388 template<typename T>
389 inline
390 bool
391 operator>(const SGVec4<T>& v1, const SGVec4<T>& v2)
392 { return operator<(v2, v1); }
393
394 template<typename T>
395 inline
396 bool
397 operator>=(const SGVec4<T>& v1, const SGVec4<T>& v2)
398 { return operator<=(v2, v1); }
399
400 /// Return true if equal to the relative tolerance tol
401 template<typename T>
402 inline
403 bool
404 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
405 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
406
407 /// Return true if equal to the relative tolerance tol
408 template<typename T>
409 inline
410 bool
411 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
412 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
413
414 /// Return true if about equal to roundoff of the underlying type
415 template<typename T>
416 inline
417 bool
418 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
419 {
420   T tol = 100*SGLimits<T>::epsilon();
421   return equivalent(v1, v2, tol, tol);
422 }
423
424 /// The euclidean distance of the two vectors
425 template<typename T>
426 inline
427 T
428 dist(const SGVec4<T>& v1, const SGVec4<T>& v2)
429 { return norm(v1 - v2); }
430
431 /// The squared euclidean distance of the two vectors
432 template<typename T>
433 inline
434 T
435 distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
436 { SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
437
438 #ifndef NDEBUG
439 template<typename T>
440 inline
441 bool
442 isNaN(const SGVec4<T>& v)
443 {
444   return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
445     || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
446 }
447 #endif
448
449 /// Output to an ostream
450 template<typename char_type, typename traits_type, typename T>
451 inline
452 std::basic_ostream<char_type, traits_type>&
453 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
454 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
455
456 inline
457 SGVec4f
458 toVec4f(const SGVec4d& v)
459 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
460
461 inline
462 SGVec4d
463 toVec4d(const SGVec4f& v)
464 { return SGVec4d(v(0), v(1), v(2), v(3)); }
465
466 #endif