]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec2.hxx
1e683b49aceb7674ca32b00856b65cc4df1c88f5
[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 #ifndef NO_OPENSCENEGRAPH_INTERFACE
59   explicit SGVec2(const osg::Vec2f& d)
60   { data()[0] = d[0]; data()[1] = d[1]; }
61   explicit SGVec2(const osg::Vec2d& d)
62   { data()[0] = d[0]; data()[1] = d[1]; }
63 #endif
64
65   /// Access by index, the index is unchecked
66   const T& operator()(unsigned i) const
67   { return data()[i]; }
68   /// Access by index, the index is unchecked
69   T& operator()(unsigned i)
70   { return data()[i]; }
71
72   /// Access raw data by index, the index is unchecked
73   const T& operator[](unsigned i) const
74   { return data()[i]; }
75   /// Access raw data by index, the index is unchecked
76   T& operator[](unsigned i)
77   { return data()[i]; }
78
79   /// Access the x component
80   const T& x(void) const
81   { return data()[0]; }
82   /// Access the x component
83   T& x(void)
84   { return data()[0]; }
85   /// Access the y component
86   const T& y(void) const
87   { return data()[1]; }
88   /// Access the y component
89   T& y(void)
90   { return data()[1]; }
91
92   /// Access raw data
93   const T (&data(void) const)[2]
94   { return _data; }
95   /// Access raw data
96   T (&data(void))[2]
97   { return _data; }
98
99 #ifndef NO_OPENSCENEGRAPH_INTERFACE
100   osg::Vec2d osg() const
101   { return osg::Vec2d(data()[0], data()[1]); }
102 #endif
103
104   /// Inplace addition
105   SGVec2& operator+=(const SGVec2& v)
106   { data()[0] += v(0); data()[1] += v(1); return *this; }
107   /// Inplace subtraction
108   SGVec2& operator-=(const SGVec2& v)
109   { data()[0] -= v(0); data()[1] -= v(1); return *this; }
110   /// Inplace scalar multiplication
111   template<typename S>
112   SGVec2& operator*=(S s)
113   { data()[0] *= s; data()[1] *= s; return *this; }
114   /// Inplace scalar multiplication by 1/s
115   template<typename S>
116   SGVec2& operator/=(S s)
117   { return operator*=(1/T(s)); }
118
119   /// Return an all zero vector
120   static SGVec2 zeros(void)
121   { return SGVec2(0, 0); }
122   /// Return unit vectors
123   static SGVec2 e1(void)
124   { return SGVec2(1, 0); }
125   static SGVec2 e2(void)
126   { return SGVec2(0, 1); }
127
128 private:
129   T _data[2];
130 };
131
132 /// Unary +, do nothing ...
133 template<typename T>
134 inline
135 const SGVec2<T>&
136 operator+(const SGVec2<T>& v)
137 { return v; }
138
139 /// Unary -, do nearly nothing
140 template<typename T>
141 inline
142 SGVec2<T>
143 operator-(const SGVec2<T>& v)
144 { return SGVec2<T>(-v(0), -v(1)); }
145
146 /// Binary +
147 template<typename T>
148 inline
149 SGVec2<T>
150 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
151 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
152
153 /// Binary -
154 template<typename T>
155 inline
156 SGVec2<T>
157 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
158 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
159
160 /// Scalar multiplication
161 template<typename S, typename T>
162 inline
163 SGVec2<T>
164 operator*(S s, const SGVec2<T>& v)
165 { return SGVec2<T>(s*v(0), s*v(1)); }
166
167 /// Scalar multiplication
168 template<typename S, typename T>
169 inline
170 SGVec2<T>
171 operator*(const SGVec2<T>& v, S s)
172 { return SGVec2<T>(s*v(0), s*v(1)); }
173
174 /// multiplication as a multiplicator, that is assume that the first vector
175 /// represents a 2x2 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 SGVec2<T>
180 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
181 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
182
183 /// component wise min
184 template<typename T>
185 inline
186 SGVec2<T>
187 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
188 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
189 template<typename S, typename T>
190 inline
191 SGVec2<T>
192 min(const SGVec2<T>& v, S s)
193 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
194 template<typename S, typename T>
195 inline
196 SGVec2<T>
197 min(S s, const SGVec2<T>& v)
198 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
199
200 /// component wise max
201 template<typename T>
202 inline
203 SGVec2<T>
204 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
205 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
206 template<typename S, typename T>
207 inline
208 SGVec2<T>
209 max(const SGVec2<T>& v, S s)
210 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
211 template<typename S, typename T>
212 inline
213 SGVec2<T>
214 max(S s, const SGVec2<T>& v)
215 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
216
217 /// Scalar dot product
218 template<typename T>
219 inline
220 T
221 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
222 { return v1(0)*v2(0) + v1(1)*v2(1); }
223
224 /// The euclidean norm of the vector, that is what most people call length
225 template<typename T>
226 inline
227 T
228 norm(const SGVec2<T>& v)
229 { return sqrt(dot(v, v)); }
230
231 /// The euclidean norm of the vector, that is what most people call length
232 template<typename T>
233 inline
234 T
235 length(const SGVec2<T>& v)
236 { return sqrt(dot(v, v)); }
237
238 /// The 1-norm of the vector, this one is the fastest length function we
239 /// can implement on modern cpu's
240 template<typename T>
241 inline
242 T
243 norm1(const SGVec2<T>& v)
244 { return fabs(v(0)) + fabs(v(1)); }
245
246 /// The inf-norm of the vector
247 template<typename T>
248 inline
249 T
250 normI(const SGVec2<T>& v)
251 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
252
253 /// The euclidean norm of the vector, that is what most people call length
254 template<typename T>
255 inline
256 SGVec2<T>
257 normalize(const SGVec2<T>& v)
258 { return (1/norm(v))*v; }
259
260 /// Return true if exactly the same
261 template<typename T>
262 inline
263 bool
264 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
265 { return v1(0) == v2(0) && v1(1) == v2(1); }
266
267 /// Return true if not exactly the same
268 template<typename T>
269 inline
270 bool
271 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
272 { return ! (v1 == v2); }
273
274 /// Return true if smaller, good for putting that into a std::map
275 template<typename T>
276 inline
277 bool
278 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
279 {
280   if (v1(0) < v2(0)) return true;
281   else if (v2(0) < v1(0)) return false;
282   else return (v1(1) < v2(1));
283 }
284
285 template<typename T>
286 inline
287 bool
288 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
289 {
290   if (v1(0) < v2(0)) return true;
291   else if (v2(0) < v1(0)) return false;
292   else return (v1(1) <= v2(1));
293 }
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 template<typename T>
302 inline
303 bool
304 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
305 { return operator<=(v2, v1); }
306
307 /// Return true if equal to the relative tolerance tol
308 template<typename T>
309 inline
310 bool
311 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
312 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
313
314 /// Return true if equal to the relative tolerance tol
315 template<typename T>
316 inline
317 bool
318 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
319 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
320
321 /// Return true if about equal to roundoff of the underlying type
322 template<typename T>
323 inline
324 bool
325 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
326 {
327   T tol = 100*SGLimits<T>::epsilon();
328   return equivalent(v1, v2, tol, tol);
329 }
330
331 /// The euclidean distance of the two vectors
332 template<typename T>
333 inline
334 T
335 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
336 { return norm(v1 - v2); }
337
338 /// The squared euclidean distance of the two vectors
339 template<typename T>
340 inline
341 T
342 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
343 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
344
345 #ifndef NDEBUG
346 template<typename T>
347 inline
348 bool
349 isNaN(const SGVec2<T>& v)
350 {
351   return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
352 }
353 #endif
354
355 /// Output to an ostream
356 template<typename char_type, typename traits_type, typename T>
357 inline
358 std::basic_ostream<char_type, traits_type>&
359 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
360 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
361
362 inline
363 SGVec2f
364 toVec2f(const SGVec2d& v)
365 { return SGVec2f((float)v(0), (float)v(1)); }
366
367 inline
368 SGVec2d
369 toVec2d(const SGVec2f& v)
370 { return SGVec2d(v(0), v(1)); }
371
372 #ifndef NO_OPENSCENEGRAPH_INTERFACE
373 inline
374 SGVec2d
375 toSG(const osg::Vec2d& v)
376 { return SGVec2d(v[0], v[1]); }
377
378 inline
379 SGVec2f
380 toSG(const osg::Vec2f& v)
381 { return SGVec2f(v[0], v[1]); }
382
383 inline
384 osg::Vec2d
385 toOsg(const SGVec2d& v)
386 { return osg::Vec2d(v[0], v[1]); }
387
388 inline
389 osg::Vec2f
390 toOsg(const SGVec2f& v)
391 { return osg::Vec2f(v[0], v[1]); }
392
393 #endif
394
395 #endif