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 #ifndef NO_OPENSCENEGRAPH_INTERFACE
32 /// Default constructor. Does not initialize at all.
33 /// If you need them zero initialized, use SGVec2::zeros()
36 /// Initialize with nans in the debug build, that will guarantee to have
37 /// a fast uninitialized default constructor in the release but shows up
38 /// uninitialized values in the debug build very fast ...
40 for (unsigned i = 0; i < 2; ++i)
41 data()[i] = SGLimits<T>::quiet_NaN();
44 /// Constructor. Initialize by the given values
46 { data()[0] = x; data()[1] = y; }
47 /// Constructor. Initialize by the content of a plain array,
48 /// make sure it has at least 2 elements
49 explicit SGVec2(const T* d)
50 { data()[0] = d[0]; data()[1] = d[1]; }
52 explicit SGVec2(const SGVec2<S>& d)
53 { data()[0] = d[0]; data()[1] = d[1]; }
55 /// Access by index, the index is unchecked
56 const T& operator()(unsigned i) const
58 /// Access by index, the index is unchecked
59 T& operator()(unsigned i)
62 /// Access raw data by index, the index is unchecked
63 const T& operator[](unsigned i) const
65 /// Access raw data by index, the index is unchecked
66 T& operator[](unsigned i)
69 /// Access the x component
70 const T& x(void) const
72 /// Access the x component
75 /// Access the y component
76 const T& y(void) const
78 /// Access the y component
83 const T (&data(void) const)[2]
90 SGVec2& operator+=(const SGVec2& v)
91 { data()[0] += v(0); data()[1] += v(1); return *this; }
92 /// Inplace subtraction
93 SGVec2& operator-=(const SGVec2& v)
94 { data()[0] -= v(0); data()[1] -= v(1); return *this; }
95 /// Inplace scalar multiplication
97 SGVec2& operator*=(S s)
98 { data()[0] *= s; data()[1] *= s; return *this; }
99 /// Inplace scalar multiplication by 1/s
101 SGVec2& operator/=(S s)
102 { return operator*=(1/T(s)); }
104 /// Return an all zero vector
105 static SGVec2 zeros(void)
106 { return SGVec2(0, 0); }
107 /// Return unit vectors
108 static SGVec2 e1(void)
109 { return SGVec2(1, 0); }
110 static SGVec2 e2(void)
111 { return SGVec2(0, 1); }
117 /// Unary +, do nothing ...
121 operator+(const SGVec2<T>& v)
124 /// Unary -, do nearly nothing
128 operator-(const SGVec2<T>& v)
129 { return SGVec2<T>(-v(0), -v(1)); }
135 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
136 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
142 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
143 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
145 /// Scalar multiplication
146 template<typename S, typename T>
149 operator*(S s, const SGVec2<T>& v)
150 { return SGVec2<T>(s*v(0), s*v(1)); }
152 /// Scalar multiplication
153 template<typename S, typename T>
156 operator*(const SGVec2<T>& v, S s)
157 { return SGVec2<T>(s*v(0), s*v(1)); }
159 /// multiplication as a multiplicator, that is assume that the first vector
160 /// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
161 /// Then the result is the product of that matrix times the second vector.
165 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
166 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
168 /// component wise min
172 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
173 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
174 template<typename S, typename T>
177 min(const SGVec2<T>& v, S s)
178 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
179 template<typename S, typename T>
182 min(S s, const SGVec2<T>& v)
183 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
185 /// component wise max
189 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
190 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
191 template<typename S, typename T>
194 max(const SGVec2<T>& v, S s)
195 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
196 template<typename S, typename T>
199 max(S s, const SGVec2<T>& v)
200 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
202 /// Scalar dot product
206 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
207 { return v1(0)*v2(0) + v1(1)*v2(1); }
209 /// The euclidean norm of the vector, that is what most people call length
213 norm(const SGVec2<T>& v)
214 { return sqrt(dot(v, v)); }
216 /// The euclidean norm of the vector, that is what most people call length
220 length(const SGVec2<T>& v)
221 { return sqrt(dot(v, v)); }
223 /// The 1-norm of the vector, this one is the fastest length function we
224 /// can implement on modern cpu's
228 norm1(const SGVec2<T>& v)
229 { return fabs(v(0)) + fabs(v(1)); }
231 /// The inf-norm of the vector
235 normI(const SGVec2<T>& v)
236 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
238 /// The euclidean norm of the vector, that is what most people call length
242 normalize(const SGVec2<T>& v)
245 if (normv <= SGLimits<T>::min())
246 return SGVec2<T>::zeros();
250 /// Return true if exactly the same
254 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
255 { return v1(0) == v2(0) && v1(1) == v2(1); }
257 /// Return true if not exactly the same
261 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
262 { return ! (v1 == v2); }
264 /// Return true if smaller, good for putting that into a std::map
268 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
270 if (v1(0) < v2(0)) return true;
271 else if (v2(0) < v1(0)) return false;
272 else return (v1(1) < v2(1));
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)
289 { return operator<(v2, v1); }
294 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
295 { return operator<=(v2, v1); }
297 /// Return true if equal to the relative tolerance tol
301 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
302 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
304 /// Return true if equal to the relative tolerance tol
308 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
309 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
311 /// Return true if about equal to roundoff of the underlying type
315 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
317 T tol = 100*SGLimits<T>::epsilon();
318 return equivalent(v1, v2, tol, tol);
321 /// The euclidean distance of the two vectors
325 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
326 { return norm(v1 - v2); }
328 /// The squared euclidean distance of the two vectors
332 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
333 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
335 // calculate the projection of u along the direction of d.
339 projection(const SGVec2<T>& u, const SGVec2<T>& d)
343 if (SGLimits<T>::min() < denom) return u;
344 else return d * (dot(u, d) / denom);
351 isNaN(const SGVec2<T>& v)
353 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
357 /// Output to an ostream
358 template<typename char_type, typename traits_type, typename T>
360 std::basic_ostream<char_type, traits_type>&
361 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
362 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
366 toVec2f(const SGVec2d& v)
367 { return SGVec2f((float)v(0), (float)v(1)); }
371 toVec2d(const SGVec2f& v)
372 { return SGVec2d(v(0), v(1)); }
374 #ifndef NO_OPENSCENEGRAPH_INTERFACE
377 toSG(const osg::Vec2d& v)
378 { return SGVec2d(v[0], v[1]); }
382 toSG(const osg::Vec2f& v)
383 { return SGVec2f(v[0], v[1]); }
387 toOsg(const SGVec2d& v)
388 { return osg::Vec2d(v[0], v[1]); }
392 toOsg(const SGVec2f& v)
393 { return osg::Vec2f(v[0], v[1]); }