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