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