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