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