]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGVec2.hxx
Switch to new vector conversion functions.
[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 { return (1/norm(v))*v; }
248
249 /// Return true if exactly the same
250 template<typename T>
251 inline
252 bool
253 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
254 { return v1(0) == v2(0) && v1(1) == v2(1); }
255
256 /// Return true if not exactly the same
257 template<typename T>
258 inline
259 bool
260 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
261 { return ! (v1 == v2); }
262
263 /// Return true if smaller, good for putting that into a std::map
264 template<typename T>
265 inline
266 bool
267 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
268 {
269   if (v1(0) < v2(0)) return true;
270   else if (v2(0) < v1(0)) return false;
271   else return (v1(1) < v2(1));
272 }
273
274 template<typename T>
275 inline
276 bool
277 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
278 {
279   if (v1(0) < v2(0)) return true;
280   else if (v2(0) < v1(0)) return false;
281   else return (v1(1) <= v2(1));
282 }
283
284 template<typename T>
285 inline
286 bool
287 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
288 { return operator<(v2, v1); }
289
290 template<typename T>
291 inline
292 bool
293 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
294 { return operator<=(v2, v1); }
295
296 /// Return true if equal to the relative tolerance tol
297 template<typename T>
298 inline
299 bool
300 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
301 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
302
303 /// Return true if equal to the relative tolerance tol
304 template<typename T>
305 inline
306 bool
307 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
308 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
309
310 /// Return true if about equal to roundoff of the underlying type
311 template<typename T>
312 inline
313 bool
314 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
315 {
316   T tol = 100*SGLimits<T>::epsilon();
317   return equivalent(v1, v2, tol, tol);
318 }
319
320 /// The euclidean distance of the two vectors
321 template<typename T>
322 inline
323 T
324 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
325 { return norm(v1 - v2); }
326
327 /// The squared euclidean distance of the two vectors
328 template<typename T>
329 inline
330 T
331 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
332 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
333
334 #ifndef NDEBUG
335 template<typename T>
336 inline
337 bool
338 isNaN(const SGVec2<T>& v)
339 {
340   return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
341 }
342 #endif
343
344 /// Output to an ostream
345 template<typename char_type, typename traits_type, typename T>
346 inline
347 std::basic_ostream<char_type, traits_type>&
348 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
349 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
350
351 inline
352 SGVec2f
353 toVec2f(const SGVec2d& v)
354 { return SGVec2f((float)v(0), (float)v(1)); }
355
356 inline
357 SGVec2d
358 toVec2d(const SGVec2f& v)
359 { return SGVec2d(v(0), v(1)); }
360
361 #ifndef NO_OPENSCENEGRAPH_INTERFACE
362 inline
363 SGVec2d
364 toSG(const osg::Vec2d& v)
365 { return SGVec2d(v[0], v[1]); }
366
367 inline
368 SGVec2f
369 toSG(const osg::Vec2f& v)
370 { return SGVec2f(v[0], v[1]); }
371
372 inline
373 osg::Vec2d
374 toOsg(const SGVec2d& v)
375 { return osg::Vec2d(v[0], v[1]); }
376
377 inline
378 osg::Vec2f
379 toOsg(const SGVec2f& v)
380 { return osg::Vec2f(v[0], v[1]); }
381
382 #endif
383
384 #endif