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]; }
58 #ifndef NO_OPENSCENEGRAPH_INTERFACE
59 explicit SGVec2(const osg::Vec2f& d)
60 { data()[0] = d[0]; data()[1] = d[1]; }
61 explicit SGVec2(const osg::Vec2d& d)
62 { data()[0] = d[0]; data()[1] = d[1]; }
65 /// Access by index, the index is unchecked
66 const T& operator()(unsigned i) const
68 /// Access by index, the index is unchecked
69 T& operator()(unsigned i)
72 /// Access raw data by index, the index is unchecked
73 const T& operator[](unsigned i) const
75 /// Access raw data by index, the index is unchecked
76 T& operator[](unsigned i)
79 /// Access the x component
80 const T& x(void) const
82 /// Access the x component
85 /// Access the y component
86 const T& y(void) const
88 /// Access the y component
93 const T (&data(void) const)[2]
99 #ifndef NO_OPENSCENEGRAPH_INTERFACE
100 osg::Vec2d osg() const
101 { return osg::Vec2d(data()[0], data()[1]); }
105 SGVec2& operator+=(const SGVec2& v)
106 { data()[0] += v(0); data()[1] += v(1); return *this; }
107 /// Inplace subtraction
108 SGVec2& operator-=(const SGVec2& v)
109 { data()[0] -= v(0); data()[1] -= v(1); return *this; }
110 /// Inplace scalar multiplication
112 SGVec2& operator*=(S s)
113 { data()[0] *= s; data()[1] *= s; return *this; }
114 /// Inplace scalar multiplication by 1/s
116 SGVec2& operator/=(S s)
117 { return operator*=(1/T(s)); }
119 /// Return an all zero vector
120 static SGVec2 zeros(void)
121 { return SGVec2(0, 0); }
122 /// Return unit vectors
123 static SGVec2 e1(void)
124 { return SGVec2(1, 0); }
125 static SGVec2 e2(void)
126 { return SGVec2(0, 1); }
132 /// Unary +, do nothing ...
136 operator+(const SGVec2<T>& v)
139 /// Unary -, do nearly nothing
143 operator-(const SGVec2<T>& v)
144 { return SGVec2<T>(-v(0), -v(1)); }
150 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
151 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
157 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
158 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
160 /// Scalar multiplication
161 template<typename S, typename T>
164 operator*(S s, const SGVec2<T>& v)
165 { return SGVec2<T>(s*v(0), s*v(1)); }
167 /// Scalar multiplication
168 template<typename S, typename T>
171 operator*(const SGVec2<T>& v, S s)
172 { return SGVec2<T>(s*v(0), s*v(1)); }
174 /// multiplication as a multiplicator, that is assume that the first vector
175 /// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
176 /// Then the result is the product of that matrix times the second vector.
180 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
181 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
183 /// component wise min
187 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
188 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
189 template<typename S, typename T>
192 min(const SGVec2<T>& v, S s)
193 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
194 template<typename S, typename T>
197 min(S s, const SGVec2<T>& v)
198 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
200 /// component wise max
204 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
205 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
206 template<typename S, typename T>
209 max(const SGVec2<T>& v, S s)
210 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
211 template<typename S, typename T>
214 max(S s, const SGVec2<T>& v)
215 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
217 /// Scalar dot product
221 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
222 { return v1(0)*v2(0) + v1(1)*v2(1); }
224 /// The euclidean norm of the vector, that is what most people call length
228 norm(const SGVec2<T>& v)
229 { return sqrt(dot(v, v)); }
231 /// The euclidean norm of the vector, that is what most people call length
235 length(const SGVec2<T>& v)
236 { return sqrt(dot(v, v)); }
238 /// The 1-norm of the vector, this one is the fastest length function we
239 /// can implement on modern cpu's
243 norm1(const SGVec2<T>& v)
244 { return fabs(v(0)) + fabs(v(1)); }
246 /// The inf-norm of the vector
250 normI(const SGVec2<T>& v)
251 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
253 /// The euclidean norm of the vector, that is what most people call length
257 normalize(const SGVec2<T>& v)
258 { return (1/norm(v))*v; }
260 /// Return true if exactly the same
264 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
265 { return v1(0) == v2(0) && v1(1) == v2(1); }
267 /// Return true if not exactly the same
271 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
272 { return ! (v1 == v2); }
274 /// Return true if smaller, good for putting that into a std::map
278 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
280 if (v1(0) < v2(0)) return true;
281 else if (v2(0) < v1(0)) return false;
282 else return (v1(1) < v2(1));
288 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
290 if (v1(0) < v2(0)) return true;
291 else if (v2(0) < v1(0)) return false;
292 else return (v1(1) <= v2(1));
298 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
299 { return operator<(v2, v1); }
304 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
305 { return operator<=(v2, v1); }
307 /// Return true if equal to the relative tolerance tol
311 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
312 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
314 /// Return true if equal to the relative tolerance tol
318 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
319 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
321 /// Return true if about equal to roundoff of the underlying type
325 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
327 T tol = 100*SGLimits<T>::epsilon();
328 return equivalent(v1, v2, tol, tol);
331 /// The euclidean distance of the two vectors
335 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
336 { return norm(v1 - v2); }
338 /// The squared euclidean distance of the two vectors
342 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
343 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
349 isNaN(const SGVec2<T>& v)
351 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
355 /// Output to an ostream
356 template<typename char_type, typename traits_type, typename T>
358 std::basic_ostream<char_type, traits_type>&
359 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
360 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
364 toVec2f(const SGVec2d& v)
365 { return SGVec2f((float)v(0), (float)v(1)); }
369 toVec2d(const SGVec2f& v)
370 { return SGVec2d(v(0), v(1)); }
372 #ifndef NO_OPENSCENEGRAPH_INTERFACE
375 toSG(const osg::Vec2d& v)
376 { return SGVec2d(v[0], v[1]); }
380 toSG(const osg::Vec2f& v)
381 { return SGVec2f(v[0], v[1]); }
385 toOsg(const SGVec2d& v)
386 { return osg::Vec2d(v[0], v[1]); }
390 toOsg(const SGVec2f& v)
391 { return osg::Vec2f(v[0], v[1]); }