1 // Copyright (C) 2006 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__ )
29 struct SGVec2Storage {
30 /// Readonly raw storage interface
31 const T (&data(void) const)[2]
33 /// Readonly raw storage interface
45 struct SGVec2Storage<float> : public osg::Vec2f {
46 /// Access raw data by index, the index is unchecked
47 const float (&data(void) const)[2]
48 { return osg::Vec2f::_v; }
49 /// Access raw data by index, the index is unchecked
50 float (&data(void))[2]
51 { return osg::Vec2f::_v; }
53 const osg::Vec2f& osg() const
60 struct SGVec2Storage<double> : public osg::Vec2d {
61 /// Access raw data by index, the index is unchecked
62 const double (&data(void) const)[2]
63 { return osg::Vec2d::_v; }
64 /// Access raw data by index, the index is unchecked
65 double (&data(void))[2]
66 { return osg::Vec2d::_v; }
68 const osg::Vec2d& osg() const
76 class SGVec2 : protected SGVec2Storage<T> {
80 /// Default constructor. Does not initialize at all.
81 /// If you need them zero initialized, use SGVec2::zeros()
84 /// Initialize with nans in the debug build, that will guarantee to have
85 /// a fast uninitialized default constructor in the release but shows up
86 /// uninitialized values in the debug build very fast ...
88 for (unsigned i = 0; i < 2; ++i)
89 data()[i] = SGLimits<T>::quiet_NaN();
92 /// Constructor. Initialize by the given values
94 { data()[0] = x; data()[1] = y; }
95 /// Constructor. Initialize by the content of a plain array,
96 /// make sure it has at least 2 elements
97 explicit SGVec2(const T* d)
98 { data()[0] = d[0]; data()[1] = d[1]; }
99 explicit SGVec2(const osg::Vec2f& d)
100 { data()[0] = d[0]; data()[1] = d[1]; }
101 explicit SGVec2(const osg::Vec2d& d)
102 { data()[0] = d[0]; data()[1] = d[1]; }
104 /// Access by index, the index is unchecked
105 const T& operator()(unsigned i) const
106 { return data()[i]; }
107 /// Access by index, the index is unchecked
108 T& operator()(unsigned i)
109 { return data()[i]; }
111 /// Access raw data by index, the index is unchecked
112 const T& operator[](unsigned i) const
113 { return data()[i]; }
114 /// Access raw data by index, the index is unchecked
115 T& operator[](unsigned i)
116 { return data()[i]; }
118 /// Access the x component
119 const T& x(void) const
120 { return data()[0]; }
121 /// Access the x component
123 { return data()[0]; }
124 /// Access the y component
125 const T& y(void) const
126 { return data()[1]; }
127 /// Access the y component
129 { return data()[1]; }
131 /// Get the data pointer
132 using SGVec2Storage<T>::data;
134 /// Readonly interface function to ssg's sgVec2/sgdVec2
135 const T (&sg(void) const)[2]
137 /// Interface function to ssg's sgVec2/sgdVec2
141 /// Interface function to osg's Vec2*
142 using SGVec2Storage<T>::osg;
145 SGVec2& operator+=(const SGVec2& v)
146 { data()[0] += v(0); data()[1] += v(1); return *this; }
147 /// Inplace subtraction
148 SGVec2& operator-=(const SGVec2& v)
149 { data()[0] -= v(0); data()[1] -= v(1); return *this; }
150 /// Inplace scalar multiplication
152 SGVec2& operator*=(S s)
153 { data()[0] *= s; data()[1] *= s; return *this; }
154 /// Inplace scalar multiplication by 1/s
156 SGVec2& operator/=(S s)
157 { return operator*=(1/T(s)); }
159 /// Return an all zero vector
160 static SGVec2 zeros(void)
161 { return SGVec2(0, 0); }
162 /// Return unit vectors
163 static SGVec2 e1(void)
164 { return SGVec2(1, 0); }
165 static SGVec2 e2(void)
166 { return SGVec2(0, 1); }
169 /// Unary +, do nothing ...
173 operator+(const SGVec2<T>& v)
176 /// Unary -, do nearly nothing
180 operator-(const SGVec2<T>& v)
181 { return SGVec2<T>(-v(0), -v(1)); }
187 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
188 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
194 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
195 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
197 /// Scalar multiplication
198 template<typename S, typename T>
201 operator*(S s, const SGVec2<T>& v)
202 { return SGVec2<T>(s*v(0), s*v(1)); }
204 /// Scalar multiplication
205 template<typename S, typename T>
208 operator*(const SGVec2<T>& v, S s)
209 { return SGVec2<T>(s*v(0), s*v(1)); }
211 /// multiplication as a multiplicator, that is assume that the first vector
212 /// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
213 /// Then the result is the product of that matrix times the second vector.
217 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
218 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
220 /// component wise min
224 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
225 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
226 template<typename S, typename T>
229 min(const SGVec2<T>& v, S s)
230 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
231 template<typename S, typename T>
234 min(S s, const SGVec2<T>& v)
235 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
237 /// component wise max
241 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
242 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
243 template<typename S, typename T>
246 max(const SGVec2<T>& v, S s)
247 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
248 template<typename S, typename T>
251 max(S s, const SGVec2<T>& v)
252 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
254 /// Scalar dot product
258 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
259 { return v1(0)*v2(0) + v1(1)*v2(1); }
261 /// The euclidean norm of the vector, that is what most people call length
265 norm(const SGVec2<T>& v)
266 { return sqrt(dot(v, v)); }
268 /// The euclidean norm of the vector, that is what most people call length
272 length(const SGVec2<T>& v)
273 { return sqrt(dot(v, v)); }
275 /// The 1-norm of the vector, this one is the fastest length function we
276 /// can implement on modern cpu's
280 norm1(const SGVec2<T>& v)
281 { return fabs(v(0)) + fabs(v(1)); }
283 /// The inf-norm of the vector
287 normI(const SGVec2<T>& v)
288 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
290 /// The euclidean norm of the vector, that is what most people call length
294 normalize(const SGVec2<T>& v)
295 { return (1/norm(v))*v; }
297 /// Return true if exactly the same
301 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
302 { return v1(0) == v2(0) && v1(1) == v2(1); }
304 /// Return true if not exactly the same
308 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
309 { return ! (v1 == v2); }
311 /// Return true if smaller, good for putting that into a std::map
315 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
317 if (v1(0) < v2(0)) return true;
318 else if (v2(0) < v1(0)) return false;
319 else return (v1(1) < v2(1));
325 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
327 if (v1(0) < v2(0)) return true;
328 else if (v2(0) < v1(0)) return false;
329 else return (v1(1) <= v2(1));
335 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
336 { return operator<(v2, v1); }
341 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
342 { return operator<=(v2, v1); }
344 /// Return true if equal to the relative tolerance tol
348 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
349 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
351 /// Return true if equal to the relative tolerance tol
355 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
356 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
358 /// Return true if about equal to roundoff of the underlying type
362 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
364 T tol = 100*SGLimits<T>::epsilon();
365 return equivalent(v1, v2, tol, tol);
368 /// The euclidean distance of the two vectors
372 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
373 { return norm(v1 - v2); }
375 /// The squared euclidean distance of the two vectors
379 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
380 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
386 isNaN(const SGVec2<T>& v)
388 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
392 /// Output to an ostream
393 template<typename char_type, typename traits_type, typename T>
395 std::basic_ostream<char_type, traits_type>&
396 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
397 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
401 toVec2f(const SGVec2d& v)
402 { return SGVec2f((float)v(0), (float)v(1)); }
406 toVec2d(const SGVec2f& v)
407 { return SGVec2d(v(0), v(1)); }