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