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