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