]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec4.hxx
Correct finite precision issues.
[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 {
292   T normv = norm(v);
293   if (normv <= SGLimits<T>::min())
294     return SGVec4<T>::zeros();
295   return (1/normv)*v;
296 }
297
298 /// Return true if exactly the same
299 template<typename T>
300 inline
301 bool
302 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
303 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
304
305 /// Return true if not exactly the same
306 template<typename T>
307 inline
308 bool
309 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
310 { return ! (v1 == v2); }
311
312 /// Return true if smaller, good for putting that into a std::map
313 template<typename T>
314 inline
315 bool
316 operator<(const SGVec4<T>& v1, const SGVec4<T>& v2)
317 {
318   if (v1(0) < v2(0)) return true;
319   else if (v2(0) < v1(0)) return false;
320   else if (v1(1) < v2(1)) return true;
321   else if (v2(1) < v1(1)) return false;
322   else if (v1(2) < v2(2)) return true;
323   else if (v2(2) < v1(2)) return false;
324   else return (v1(3) < v2(3));
325 }
326
327 template<typename T>
328 inline
329 bool
330 operator<=(const SGVec4<T>& v1, const SGVec4<T>& v2)
331 {
332   if (v1(0) < v2(0)) return true;
333   else if (v2(0) < v1(0)) return false;
334   else if (v1(1) < v2(1)) return true;
335   else if (v2(1) < v1(1)) return false;
336   else if (v1(2) < v2(2)) return true;
337   else if (v2(2) < v1(2)) return false;
338   else return (v1(3) <= v2(3));
339 }
340
341 template<typename T>
342 inline
343 bool
344 operator>(const SGVec4<T>& v1, const SGVec4<T>& v2)
345 { return operator<(v2, v1); }
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 /// Return true if equal to the relative tolerance tol
354 template<typename T>
355 inline
356 bool
357 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
358 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
359
360 /// Return true if equal to the relative tolerance tol
361 template<typename T>
362 inline
363 bool
364 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
365 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
366
367 /// Return true if about equal to roundoff of the underlying type
368 template<typename T>
369 inline
370 bool
371 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
372 {
373   T tol = 100*SGLimits<T>::epsilon();
374   return equivalent(v1, v2, tol, tol);
375 }
376
377 /// The euclidean distance of the two vectors
378 template<typename T>
379 inline
380 T
381 dist(const SGVec4<T>& v1, const SGVec4<T>& v2)
382 { return norm(v1 - v2); }
383
384 /// The squared euclidean distance of the two vectors
385 template<typename T>
386 inline
387 T
388 distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
389 { SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
390
391 // calculate the projection of u along the direction of d.
392 template<typename T>
393 inline
394 SGVec4<T>
395 projection(const SGVec4<T>& u, const SGVec4<T>& d)
396 {
397   T denom = dot(d, d);
398   T ud = dot(u, d);
399   if (SGLimits<T>::min() < denom) return u;
400   else return d * (dot(u, d) / denom);
401 }
402
403 #ifndef NDEBUG
404 template<typename T>
405 inline
406 bool
407 isNaN(const SGVec4<T>& v)
408 {
409   return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
410     || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
411 }
412 #endif
413
414 /// Output to an ostream
415 template<typename char_type, typename traits_type, typename T>
416 inline
417 std::basic_ostream<char_type, traits_type>&
418 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
419 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
420
421 inline
422 SGVec4f
423 toVec4f(const SGVec4d& v)
424 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
425
426 inline
427 SGVec4d
428 toVec4d(const SGVec4f& v)
429 { return SGVec4d(v(0), v(1), v(2), v(3)); }
430
431 #ifndef NO_OPENSCENEGRAPH_INTERFACE
432 inline
433 SGVec4d
434 toSG(const osg::Vec4d& v)
435 { return SGVec4d(v[0], v[1], v[2], v[3]); }
436
437 inline
438 SGVec4f
439 toSG(const osg::Vec4f& v)
440 { return SGVec4f(v[0], v[1], v[2], v[3]); }
441
442 inline
443 osg::Vec4d
444 toOsg(const SGVec4d& v)
445 { return osg::Vec4d(v[0], v[1], v[2], v[3]); }
446
447 inline
448 osg::Vec4f
449 toOsg(const SGVec4f& v)
450 { return osg::Vec4f(v[0], v[1], v[2], v[3]); }
451 #endif
452
453 #endif