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 #if defined ( __CYGWIN__ )
25 #ifndef NO_OPENSCENEGRAPH_INTERFACE
36 /// Default constructor. Does not initialize at all.
37 /// If you need them zero initialized, use SGVec2::zeros()
40 /// Initialize with nans in the debug build, that will guarantee to have
41 /// a fast uninitialized default constructor in the release but shows up
42 /// uninitialized values in the debug build very fast ...
44 for (unsigned i = 0; i < 2; ++i)
45 data()[i] = SGLimits<T>::quiet_NaN();
48 /// Constructor. Initialize by the given values
50 { data()[0] = x; data()[1] = y; }
51 /// Constructor. Initialize by the content of a plain array,
52 /// make sure it has at least 2 elements
53 explicit SGVec2(const T* d)
54 { data()[0] = d[0]; data()[1] = d[1]; }
56 explicit SGVec2(const SGVec2<S>& d)
57 { data()[0] = d[0]; data()[1] = d[1]; }
59 /// Access by index, the index is unchecked
60 const T& operator()(unsigned i) const
62 /// Access by index, the index is unchecked
63 T& operator()(unsigned i)
66 /// Access raw data by index, the index is unchecked
67 const T& operator[](unsigned i) const
69 /// Access raw data by index, the index is unchecked
70 T& operator[](unsigned i)
73 /// Access the x component
74 const T& x(void) const
76 /// Access the x component
79 /// Access the y component
80 const T& y(void) const
82 /// Access the y component
87 const T (&data(void) const)[2]
94 SGVec2& operator+=(const SGVec2& v)
95 { data()[0] += v(0); data()[1] += v(1); return *this; }
96 /// Inplace subtraction
97 SGVec2& operator-=(const SGVec2& v)
98 { data()[0] -= v(0); data()[1] -= v(1); return *this; }
99 /// Inplace scalar multiplication
101 SGVec2& operator*=(S s)
102 { data()[0] *= s; data()[1] *= s; return *this; }
103 /// Inplace scalar multiplication by 1/s
105 SGVec2& operator/=(S s)
106 { return operator*=(1/T(s)); }
108 /// Return an all zero vector
109 static SGVec2 zeros(void)
110 { return SGVec2(0, 0); }
111 /// Return unit vectors
112 static SGVec2 e1(void)
113 { return SGVec2(1, 0); }
114 static SGVec2 e2(void)
115 { return SGVec2(0, 1); }
121 /// Unary +, do nothing ...
125 operator+(const SGVec2<T>& v)
128 /// Unary -, do nearly nothing
132 operator-(const SGVec2<T>& v)
133 { return SGVec2<T>(-v(0), -v(1)); }
139 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
140 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
146 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
147 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
149 /// Scalar multiplication
150 template<typename S, typename T>
153 operator*(S s, const SGVec2<T>& v)
154 { return SGVec2<T>(s*v(0), s*v(1)); }
156 /// Scalar multiplication
157 template<typename S, typename T>
160 operator*(const SGVec2<T>& v, S s)
161 { return SGVec2<T>(s*v(0), s*v(1)); }
163 /// multiplication as a multiplicator, that is assume that the first vector
164 /// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
165 /// Then the result is the product of that matrix times the second vector.
169 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
170 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
172 /// component wise min
176 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
177 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
178 template<typename S, typename T>
181 min(const SGVec2<T>& v, S s)
182 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
183 template<typename S, typename T>
186 min(S s, const SGVec2<T>& v)
187 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
189 /// component wise max
193 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
194 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
195 template<typename S, typename T>
198 max(const SGVec2<T>& v, S s)
199 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
200 template<typename S, typename T>
203 max(S s, const SGVec2<T>& v)
204 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
206 /// Scalar dot product
210 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
211 { return v1(0)*v2(0) + v1(1)*v2(1); }
213 /// The euclidean norm of the vector, that is what most people call length
217 norm(const SGVec2<T>& v)
218 { return sqrt(dot(v, v)); }
220 /// The euclidean norm of the vector, that is what most people call length
224 length(const SGVec2<T>& v)
225 { return sqrt(dot(v, v)); }
227 /// The 1-norm of the vector, this one is the fastest length function we
228 /// can implement on modern cpu's
232 norm1(const SGVec2<T>& v)
233 { return fabs(v(0)) + fabs(v(1)); }
235 /// The inf-norm of the vector
239 normI(const SGVec2<T>& v)
240 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
242 /// The euclidean norm of the vector, that is what most people call length
246 normalize(const SGVec2<T>& v)
249 if (normv <= SGLimits<T>::min())
250 return SGVec2<T>::zeros();
254 /// Return true if exactly the same
258 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
259 { return v1(0) == v2(0) && v1(1) == v2(1); }
261 /// Return true if not exactly the same
265 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
266 { return ! (v1 == v2); }
268 /// Return true if smaller, good for putting that into a std::map
272 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
274 if (v1(0) < v2(0)) return true;
275 else if (v2(0) < v1(0)) return false;
276 else return (v1(1) < v2(1));
282 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
284 if (v1(0) < v2(0)) return true;
285 else if (v2(0) < v1(0)) return false;
286 else return (v1(1) <= v2(1));
292 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
293 { return operator<(v2, v1); }
298 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
299 { return operator<=(v2, v1); }
301 /// Return true if equal to the relative tolerance tol
305 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
306 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
308 /// Return true if equal to the relative tolerance tol
312 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
313 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
315 /// Return true if about equal to roundoff of the underlying type
319 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
321 T tol = 100*SGLimits<T>::epsilon();
322 return equivalent(v1, v2, tol, tol);
325 /// The euclidean distance of the two vectors
329 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
330 { return norm(v1 - v2); }
332 /// The squared euclidean distance of the two vectors
336 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
337 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
339 // calculate the projection of u along the direction of d.
343 projection(const SGVec2<T>& u, const SGVec2<T>& d)
347 if (SGLimits<T>::min() < denom) return u;
348 else return d * (dot(u, d) / denom);
355 isNaN(const SGVec2<T>& v)
357 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
361 /// Output to an ostream
362 template<typename char_type, typename traits_type, typename T>
364 std::basic_ostream<char_type, traits_type>&
365 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
366 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
370 toVec2f(const SGVec2d& v)
371 { return SGVec2f((float)v(0), (float)v(1)); }
375 toVec2d(const SGVec2f& v)
376 { return SGVec2d(v(0), v(1)); }
378 #ifndef NO_OPENSCENEGRAPH_INTERFACE
381 toSG(const osg::Vec2d& v)
382 { return SGVec2d(v[0], v[1]); }
386 toSG(const osg::Vec2f& v)
387 { return SGVec2f(v[0], v[1]); }
391 toOsg(const SGVec2d& v)
392 { return osg::Vec2d(v[0], v[1]); }
396 toOsg(const SGVec2f& v)
397 { return osg::Vec2f(v[0], v[1]); }