]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec4.hxx
Boolean uniforms are now updatable by properties
[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 /// 4D Vector Class
22 template<typename T>
23 class SGVec4 {
24 public:
25   typedef T value_type;
26
27   /// Default constructor. Does not initialize at all.
28   /// If you need them zero initialized, use SGVec4::zeros()
29   SGVec4(void)
30   {
31     /// Initialize with nans in the debug build, that will guarantee to have
32     /// a fast uninitialized default constructor in the release but shows up
33     /// uninitialized values in the debug build very fast ...
34 #ifndef NDEBUG
35     for (unsigned i = 0; i < 4; ++i)
36       data()[i] = SGLimits<T>::quiet_NaN();
37 #endif
38   }
39   /// Constructor. Initialize by the given values
40   SGVec4(T x, T y, T z, T w)
41   { data()[0] = x; data()[1] = y; data()[2] = z; data()[3] = w; }
42   /// Constructor. Initialize by the content of a plain array,
43   /// make sure it has at least 3 elements
44   explicit SGVec4(const T* d)
45   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
46   template<typename S>
47   explicit SGVec4(const SGVec4<S>& d)
48   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
49   explicit SGVec4(const SGVec3<T>& v3, const T& v4 = 0)
50   { data()[0] = v3[0]; data()[1] = v3[1]; data()[2] = v3[2]; data()[3] = v4; }
51
52   /// Access by index, the index is unchecked
53   const T& operator()(unsigned i) const
54   { return data()[i]; }
55   /// Access by index, the index is unchecked
56   T& operator()(unsigned i)
57   { return data()[i]; }
58
59   /// Access raw data by index, the index is unchecked
60   const T& operator[](unsigned i) const
61   { return data()[i]; }
62   /// Access raw data by index, the index is unchecked
63   T& operator[](unsigned i)
64   { return data()[i]; }
65
66   /// Access the x component
67   const T& x(void) const
68   { return data()[0]; }
69   /// Access the x component
70   T& x(void)
71   { return data()[0]; }
72   /// Access the y component
73   const T& y(void) const
74   { return data()[1]; }
75   /// Access the y component
76   T& y(void)
77   { return data()[1]; }
78   /// Access the z component
79   const T& z(void) const
80   { return data()[2]; }
81   /// Access the z component
82   T& z(void)
83   { return data()[2]; }
84   /// Access the x component
85   const T& w(void) const
86   { return data()[3]; }
87   /// Access the x component
88   T& w(void)
89   { return data()[3]; }
90
91   /// Readonly raw storage interface
92   const T (&data(void) const)[4]
93   { return _data; }
94   /// Readonly raw storage interface
95   T (&data(void))[4]
96   { return _data; }
97
98   /// Inplace addition
99   SGVec4& operator+=(const SGVec4& v)
100   { data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; }
101   /// Inplace subtraction
102   SGVec4& operator-=(const SGVec4& v)
103   { data()[0]-=v(0);data()[1]-=v(1);data()[2]-=v(2);data()[3]-=v(3);return *this; }
104   /// Inplace scalar multiplication
105   template<typename S>
106   SGVec4& operator*=(S s)
107   { data()[0] *= s; data()[1] *= s; data()[2] *= s; data()[3] *= s; return *this; }
108   /// Inplace scalar multiplication by 1/s
109   template<typename S>
110   SGVec4& operator/=(S s)
111   { return operator*=(1/T(s)); }
112
113   /// Return an all zero vector
114   static SGVec4 zeros(void)
115   { return SGVec4(0, 0, 0, 0); }
116   /// Return unit vectors
117   static SGVec4 e1(void)
118   { return SGVec4(1, 0, 0, 0); }
119   static SGVec4 e2(void)
120   { return SGVec4(0, 1, 0, 0); }
121   static SGVec4 e3(void)
122   { return SGVec4(0, 0, 1, 0); }
123   static SGVec4 e4(void)
124   { return SGVec4(0, 0, 0, 1); }
125
126 private:
127   T _data[4];
128 };
129
130 /// Unary +, do nothing ...
131 template<typename T>
132 inline
133 const SGVec4<T>&
134 operator+(const SGVec4<T>& v)
135 { return v; }
136
137 /// Unary -, do nearly nothing
138 template<typename T>
139 inline
140 SGVec4<T>
141 operator-(const SGVec4<T>& v)
142 { return SGVec4<T>(-v(0), -v(1), -v(2), -v(3)); }
143
144 /// Binary +
145 template<typename T>
146 inline
147 SGVec4<T>
148 operator+(const SGVec4<T>& v1, const SGVec4<T>& v2)
149 { return SGVec4<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
150
151 /// Binary -
152 template<typename T>
153 inline
154 SGVec4<T>
155 operator-(const SGVec4<T>& v1, const SGVec4<T>& v2)
156 { return SGVec4<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
157
158 /// Scalar multiplication
159 template<typename S, typename T>
160 inline
161 SGVec4<T>
162 operator*(S s, const SGVec4<T>& v)
163 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
164
165 /// Scalar multiplication
166 template<typename S, typename T>
167 inline
168 SGVec4<T>
169 operator*(const SGVec4<T>& v, S s)
170 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
171
172 /// multiplication as a multiplicator, that is assume that the first vector
173 /// represents a 4x4 diagonal matrix with the diagonal elements in the vector.
174 /// Then the result is the product of that matrix times the second vector.
175 template<typename T>
176 inline
177 SGVec4<T>
178 mult(const SGVec4<T>& v1, const SGVec4<T>& v2)
179 { return SGVec4<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2), v1(3)*v2(3)); }
180
181 /// component wise min
182 template<typename T>
183 inline
184 SGVec4<T>
185 min(const SGVec4<T>& v1, const SGVec4<T>& v2)
186 {
187   return SGVec4<T>(SGMisc<T>::min(v1(0), v2(0)),
188                    SGMisc<T>::min(v1(1), v2(1)),
189                    SGMisc<T>::min(v1(2), v2(2)),
190                    SGMisc<T>::min(v1(3), v2(3)));
191 }
192 template<typename S, typename T>
193 inline
194 SGVec4<T>
195 min(const SGVec4<T>& v, S s)
196 {
197   return SGVec4<T>(SGMisc<T>::min(s, v(0)),
198                    SGMisc<T>::min(s, v(1)),
199                    SGMisc<T>::min(s, v(2)),
200                    SGMisc<T>::min(s, v(3)));
201 }
202 template<typename S, typename T>
203 inline
204 SGVec4<T>
205 min(S s, const SGVec4<T>& v)
206 {
207   return SGVec4<T>(SGMisc<T>::min(s, v(0)),
208                    SGMisc<T>::min(s, v(1)),
209                    SGMisc<T>::min(s, v(2)),
210                    SGMisc<T>::min(s, v(3)));
211 }
212
213 /// component wise max
214 template<typename T>
215 inline
216 SGVec4<T>
217 max(const SGVec4<T>& v1, const SGVec4<T>& v2)
218 {
219   return SGVec4<T>(SGMisc<T>::max(v1(0), v2(0)),
220                    SGMisc<T>::max(v1(1), v2(1)),
221                    SGMisc<T>::max(v1(2), v2(2)),
222                    SGMisc<T>::max(v1(3), v2(3)));
223 }
224 template<typename S, typename T>
225 inline
226 SGVec4<T>
227 max(const SGVec4<T>& v, S s)
228 {
229   return SGVec4<T>(SGMisc<T>::max(s, v(0)),
230                    SGMisc<T>::max(s, v(1)),
231                    SGMisc<T>::max(s, v(2)),
232                    SGMisc<T>::max(s, v(3)));
233 }
234 template<typename S, typename T>
235 inline
236 SGVec4<T>
237 max(S s, const SGVec4<T>& v)
238 {
239   return SGVec4<T>(SGMisc<T>::max(s, v(0)),
240                    SGMisc<T>::max(s, v(1)),
241                    SGMisc<T>::max(s, v(2)),
242                    SGMisc<T>::max(s, v(3)));
243 }
244
245 /// Scalar dot product
246 template<typename T>
247 inline
248 T
249 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
250 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
251
252 /// The euclidean norm of the vector, that is what most people call length
253 template<typename T>
254 inline
255 T
256 norm(const SGVec4<T>& v)
257 { return sqrt(dot(v, v)); }
258
259 /// The euclidean norm of the vector, that is what most people call length
260 template<typename T>
261 inline
262 T
263 length(const SGVec4<T>& v)
264 { return sqrt(dot(v, v)); }
265
266 /// The 1-norm of the vector, this one is the fastest length function we
267 /// can implement on modern cpu's
268 template<typename T>
269 inline
270 T
271 norm1(const SGVec4<T>& v)
272 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
273
274 /// The inf-norm of the vector
275 template<typename T>
276 inline
277 T
278 normI(const SGVec4<T>& v)
279 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2)), fabs(v(2))); }
280
281 /// The euclidean norm of the vector, that is what most people call length
282 template<typename T>
283 inline
284 SGVec4<T>
285 normalize(const SGVec4<T>& v)
286 {
287   T normv = norm(v);
288   if (normv <= SGLimits<T>::min())
289     return SGVec4<T>::zeros();
290   return (1/normv)*v;
291 }
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 // calculate the projection of u along the direction of d.
387 template<typename T>
388 inline
389 SGVec4<T>
390 projection(const SGVec4<T>& u, const SGVec4<T>& d)
391 {
392   T denom = dot(d, d);
393   T ud = dot(u, d);
394   if (SGLimits<T>::min() < denom) return u;
395   else return d * (dot(u, d) / denom);
396 }
397
398 #ifndef NDEBUG
399 template<typename T>
400 inline
401 bool
402 isNaN(const SGVec4<T>& v)
403 {
404   return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
405     || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
406 }
407 #endif
408
409 /// Output to an ostream
410 template<typename char_type, typename traits_type, typename T>
411 inline
412 std::basic_ostream<char_type, traits_type>&
413 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
414 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
415
416 inline
417 SGVec4f
418 toVec4f(const SGVec4d& v)
419 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
420
421 inline
422 SGVec4d
423 toVec4d(const SGVec4f& v)
424 { return SGVec4d(v(0), v(1), v(2), v(3)); }
425
426 #endif