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.
21 #include "SGLimits.hxx"
22 #include "SGMathFwd.hxx"
33 /// Default constructor. Does not initialize at all.
34 /// If you need them zero initialized, use SGVec2::zeros()
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 ...
41 for (unsigned i = 0; i < 2; ++i)
42 data()[i] = SGLimits<T>::quiet_NaN();
45 /// Constructor. Initialize by the given values
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]; }
53 explicit SGVec2(const SGVec2<S>& d)
54 { data()[0] = d[0]; data()[1] = d[1]; }
56 /// Access by index, the index is unchecked
57 const T& operator()(unsigned i) const
59 /// Access by index, the index is unchecked
60 T& operator()(unsigned i)
63 /// Access raw data by index, the index is unchecked
64 const T& operator[](unsigned i) const
66 /// Access raw data by index, the index is unchecked
67 T& operator[](unsigned i)
70 /// Access the x component
71 const T& x(void) const
73 /// Access the x component
76 /// Access the y component
77 const T& y(void) const
79 /// Access the y component
84 const T (&data(void) const)[2]
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
98 SGVec2& operator*=(S s)
99 { data()[0] *= s; data()[1] *= s; return *this; }
100 /// Inplace scalar multiplication by 1/s
102 SGVec2& operator/=(S s)
103 { return operator*=(1/T(s)); }
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); }
118 /// Unary +, do nothing ...
122 operator+(const SGVec2<T>& v)
125 /// Unary -, do nearly nothing
129 operator-(const SGVec2<T>& v)
130 { return SGVec2<T>(-v(0), -v(1)); }
136 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
137 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
143 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
144 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
146 /// Scalar multiplication
147 template<typename S, typename T>
150 operator*(S s, const SGVec2<T>& v)
151 { return SGVec2<T>(s*v(0), s*v(1)); }
153 /// Scalar multiplication
154 template<typename S, typename T>
157 operator*(const SGVec2<T>& v, S s)
158 { return SGVec2<T>(s*v(0), s*v(1)); }
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.
166 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
167 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
169 /// component wise min
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>
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>
183 min(S s, const SGVec2<T>& v)
184 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
186 /// component wise max
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>
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>
200 max(S s, const SGVec2<T>& v)
201 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
203 /// Scalar dot product
207 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
208 { return v1(0)*v2(0) + v1(1)*v2(1); }
210 /// The euclidean norm of the vector, that is what most people call length
214 norm(const SGVec2<T>& v)
215 { return sqrt(dot(v, v)); }
217 /// The euclidean norm of the vector, that is what most people call length
221 length(const SGVec2<T>& v)
222 { return sqrt(dot(v, v)); }
224 /// The 1-norm of the vector, this one is the fastest length function we
225 /// can implement on modern cpu's
229 norm1(const SGVec2<T>& v)
230 { return fabs(v(0)) + fabs(v(1)); }
232 /// The inf-norm of the vector
236 normI(const SGVec2<T>& v)
237 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
239 /// The euclidean norm of the vector, that is what most people call length
243 normalize(const SGVec2<T>& v)
246 if (normv <= SGLimits<T>::min())
247 return SGVec2<T>::zeros();
251 /// Return true if exactly the same
255 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
256 { return v1(0) == v2(0) && v1(1) == v2(1); }
258 /// Return true if not exactly the same
262 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
263 { return ! (v1 == v2); }
265 /// Return true if smaller, good for putting that into a std::map
269 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
271 if (v1(0) < v2(0)) return true;
272 else if (v2(0) < v1(0)) return false;
273 else return (v1(1) < v2(1));
279 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
281 if (v1(0) < v2(0)) return true;
282 else if (v2(0) < v1(0)) return false;
283 else return (v1(1) <= v2(1));
289 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
290 { return operator<(v2, v1); }
295 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
296 { return operator<=(v2, v1); }
298 /// Return true if equal to the relative tolerance tol
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; }
305 /// Return true if equal to the relative tolerance tol
309 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
310 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
312 /// Return true if about equal to roundoff of the underlying type
316 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
318 T tol = 100*SGLimits<T>::epsilon();
319 return equivalent(v1, v2, tol, tol);
322 /// The euclidean distance of the two vectors
326 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
327 { return norm(v1 - v2); }
329 /// The squared euclidean distance of the two vectors
333 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
334 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
336 // calculate the projection of u along the direction of d.
340 projection(const SGVec2<T>& u, const SGVec2<T>& d)
344 if (SGLimits<T>::min() < denom) return u;
345 else return d * (dot(u, d) / denom);
352 isNaN(const SGVec2<T>& v)
354 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
358 /// Output to an ostream
359 template<typename char_type, typename traits_type, typename T>
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) << " ]"; }
367 toVec2f(const SGVec2d& v)
368 { return SGVec2f((float)v(0), (float)v(1)); }
372 toVec2d(const SGVec2f& v)
373 { return SGVec2d(v(0), v(1)); }