]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec4.hxx
canvas::Text: get maximum width (if displayed on a single line).
[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 /// Scalar dot product
248 template<typename T>
249 inline
250 T
251 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
252 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
253
254 /// The euclidean norm of the vector, that is what most people call length
255 template<typename T>
256 inline
257 T
258 norm(const SGVec4<T>& v)
259 { return sqrt(dot(v, v)); }
260
261 /// The euclidean norm of the vector, that is what most people call length
262 template<typename T>
263 inline
264 T
265 length(const SGVec4<T>& v)
266 { return sqrt(dot(v, v)); }
267
268 /// The 1-norm of the vector, this one is the fastest length function we
269 /// can implement on modern cpu's
270 template<typename T>
271 inline
272 T
273 norm1(const SGVec4<T>& v)
274 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
275
276 /// The inf-norm of the vector
277 template<typename T>
278 inline
279 T
280 normI(const SGVec4<T>& v)
281 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2)), fabs(v(2))); }
282
283 /// The euclidean norm of the vector, that is what most people call length
284 template<typename T>
285 inline
286 SGVec4<T>
287 normalize(const SGVec4<T>& v)
288 {
289   T normv = norm(v);
290   if (normv <= SGLimits<T>::min())
291     return SGVec4<T>::zeros();
292   return (1/normv)*v;
293 }
294
295 /// Return true if exactly the same
296 template<typename T>
297 inline
298 bool
299 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
300 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
301
302 /// Return true if not exactly the same
303 template<typename T>
304 inline
305 bool
306 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
307 { return ! (v1 == v2); }
308
309 /// Return true if smaller, good for putting that into a std::map
310 template<typename T>
311 inline
312 bool
313 operator<(const SGVec4<T>& v1, const SGVec4<T>& v2)
314 {
315   if (v1(0) < v2(0)) return true;
316   else if (v2(0) < v1(0)) return false;
317   else if (v1(1) < v2(1)) return true;
318   else if (v2(1) < v1(1)) return false;
319   else if (v1(2) < v2(2)) return true;
320   else if (v2(2) < v1(2)) return false;
321   else return (v1(3) < v2(3));
322 }
323
324 template<typename T>
325 inline
326 bool
327 operator<=(const SGVec4<T>& v1, const SGVec4<T>& v2)
328 {
329   if (v1(0) < v2(0)) return true;
330   else if (v2(0) < v1(0)) return false;
331   else if (v1(1) < v2(1)) return true;
332   else if (v2(1) < v1(1)) return false;
333   else if (v1(2) < v2(2)) return true;
334   else if (v2(2) < v1(2)) return false;
335   else return (v1(3) <= v2(3));
336 }
337
338 template<typename T>
339 inline
340 bool
341 operator>(const SGVec4<T>& v1, const SGVec4<T>& v2)
342 { return operator<(v2, v1); }
343
344 template<typename T>
345 inline
346 bool
347 operator>=(const SGVec4<T>& v1, const SGVec4<T>& v2)
348 { return operator<=(v2, v1); }
349
350 /// Return true if equal to the relative tolerance tol
351 template<typename T>
352 inline
353 bool
354 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
355 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
356
357 /// Return true if equal to the relative tolerance tol
358 template<typename T>
359 inline
360 bool
361 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
362 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
363
364 /// Return true if about equal to roundoff of the underlying type
365 template<typename T>
366 inline
367 bool
368 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
369 {
370   T tol = 100*SGLimits<T>::epsilon();
371   return equivalent(v1, v2, tol, tol);
372 }
373
374 /// The euclidean distance of the two vectors
375 template<typename T>
376 inline
377 T
378 dist(const SGVec4<T>& v1, const SGVec4<T>& v2)
379 { return norm(v1 - v2); }
380
381 /// The squared euclidean distance of the two vectors
382 template<typename T>
383 inline
384 T
385 distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
386 { SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
387
388 // calculate the projection of u along the direction of d.
389 template<typename T>
390 inline
391 SGVec4<T>
392 projection(const SGVec4<T>& u, const SGVec4<T>& d)
393 {
394   T denom = dot(d, d);
395   T ud = dot(u, d);
396   if (SGLimits<T>::min() < denom) return u;
397   else return d * (dot(u, d) / denom);
398 }
399
400 #ifndef NDEBUG
401 template<typename T>
402 inline
403 bool
404 isNaN(const SGVec4<T>& v)
405 {
406   return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
407     || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
408 }
409 #endif
410
411 /// Output to an ostream
412 template<typename char_type, typename traits_type, typename T>
413 inline
414 std::basic_ostream<char_type, traits_type>&
415 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
416 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
417
418 inline
419 SGVec4f
420 toVec4f(const SGVec4d& v)
421 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
422
423 inline
424 SGVec4d
425 toVec4d(const SGVec4f& v)
426 { return SGVec4d(v(0), v(1), v(2), v(3)); }
427
428 #endif