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