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