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