1 // Copyright (C) 2006-2009 Mathias Froehlich - Mathias.Froehlich@web.de
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.
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.
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.
29 /// Default constructor. Does not initialize at all.
30 /// If you need them zero initialized, use SGVec2::zeros()
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 ...
37 for (unsigned i = 0; i < 2; ++i)
38 data()[i] = SGLimits<T>::quiet_NaN();
41 /// Constructor. Initialize by the given values
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]; }
49 explicit SGVec2(const SGVec2<S>& d)
50 { data()[0] = d[0]; data()[1] = d[1]; }
52 /// Access by index, the index is unchecked
53 const T& operator()(unsigned i) const
55 /// Access by index, the index is unchecked
56 T& operator()(unsigned i)
59 /// Access raw data by index, the index is unchecked
60 const T& operator[](unsigned i) const
62 /// Access raw data by index, the index is unchecked
63 T& operator[](unsigned i)
66 /// Access the x component
67 const T& x(void) const
69 /// Access the x component
72 /// Access the y component
73 const T& y(void) const
75 /// Access the y component
80 const T (&data(void) const)[2]
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
94 SGVec2& operator*=(S s)
95 { data()[0] *= s; data()[1] *= s; return *this; }
96 /// Inplace scalar multiplication by 1/s
98 SGVec2& operator/=(S s)
99 { return operator*=(1/T(s)); }
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); }
114 /// Unary +, do nothing ...
118 operator+(const SGVec2<T>& v)
121 /// Unary -, do nearly nothing
125 operator-(const SGVec2<T>& v)
126 { return SGVec2<T>(-v(0), -v(1)); }
132 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
133 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
139 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
140 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
142 /// Scalar multiplication
143 template<typename S, typename T>
146 operator*(S s, const SGVec2<T>& v)
147 { return SGVec2<T>(s*v(0), s*v(1)); }
149 /// Scalar multiplication
150 template<typename S, typename T>
153 operator*(const SGVec2<T>& v, S s)
154 { return SGVec2<T>(s*v(0), s*v(1)); }
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.
162 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
163 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
165 /// component wise min
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>
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>
179 min(S s, const SGVec2<T>& v)
180 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
182 /// component wise max
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>
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>
196 max(S s, const SGVec2<T>& v)
197 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
199 /// Scalar dot product
203 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
204 { return v1(0)*v2(0) + v1(1)*v2(1); }
206 /// The euclidean norm of the vector, that is what most people call length
210 norm(const SGVec2<T>& v)
211 { return sqrt(dot(v, v)); }
213 /// The euclidean norm of the vector, that is what most people call length
217 length(const SGVec2<T>& v)
218 { return sqrt(dot(v, v)); }
220 /// The 1-norm of the vector, this one is the fastest length function we
221 /// can implement on modern cpu's
225 norm1(const SGVec2<T>& v)
226 { return fabs(v(0)) + fabs(v(1)); }
228 /// The inf-norm of the vector
232 normI(const SGVec2<T>& v)
233 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
235 /// The euclidean norm of the vector, that is what most people call length
239 normalize(const SGVec2<T>& v)
242 if (normv <= SGLimits<T>::min())
243 return SGVec2<T>::zeros();
247 /// Return true if exactly the same
251 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
252 { return v1(0) == v2(0) && v1(1) == v2(1); }
254 /// Return true if not exactly the same
258 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
259 { return ! (v1 == v2); }
261 /// Return true if smaller, good for putting that into a std::map
265 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
267 if (v1(0) < v2(0)) return true;
268 else if (v2(0) < v1(0)) return false;
269 else return (v1(1) < v2(1));
275 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
277 if (v1(0) < v2(0)) return true;
278 else if (v2(0) < v1(0)) return false;
279 else return (v1(1) <= v2(1));
285 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
286 { return operator<(v2, v1); }
291 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
292 { return operator<=(v2, v1); }
294 /// Return true if equal to the relative tolerance tol
298 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
299 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
301 /// Return true if equal to the relative tolerance tol
305 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
306 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
308 /// Return true if about equal to roundoff of the underlying type
312 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
314 T tol = 100*SGLimits<T>::epsilon();
315 return equivalent(v1, v2, tol, tol);
318 /// The euclidean distance of the two vectors
322 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
323 { return norm(v1 - v2); }
325 /// The squared euclidean distance of the two vectors
329 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
330 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
332 // calculate the projection of u along the direction of d.
336 projection(const SGVec2<T>& u, const SGVec2<T>& d)
340 if (SGLimits<T>::min() < denom) return u;
341 else return d * (dot(u, d) / denom);
348 isNaN(const SGVec2<T>& v)
350 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
354 /// Output to an ostream
355 template<typename char_type, typename traits_type, typename T>
357 std::basic_ostream<char_type, traits_type>&
358 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
359 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
363 toVec2f(const SGVec2d& v)
364 { return SGVec2f((float)v(0), (float)v(1)); }
368 toVec2d(const SGVec2f& v)
369 { return SGVec2d(v(0), v(1)); }