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]; }
100 explicit SGVec2(const SGVec2<S>& d)
101 { data()[0] = d[0]; data()[1] = d[1]; }
102 explicit SGVec2(const osg::Vec2f& d)
103 { data()[0] = d[0]; data()[1] = d[1]; }
104 explicit SGVec2(const osg::Vec2d& d)
105 { data()[0] = d[0]; data()[1] = d[1]; }
107 /// Access by index, the index is unchecked
108 const T& operator()(unsigned i) const
109 { return data()[i]; }
110 /// Access by index, the index is unchecked
111 T& operator()(unsigned i)
112 { return data()[i]; }
114 /// Access raw data by index, the index is unchecked
115 const T& operator[](unsigned i) const
116 { return data()[i]; }
117 /// Access raw data by index, the index is unchecked
118 T& operator[](unsigned i)
119 { return data()[i]; }
121 /// Access the x component
122 const T& x(void) const
123 { return data()[0]; }
124 /// Access the x component
126 { return data()[0]; }
127 /// Access the y component
128 const T& y(void) const
129 { return data()[1]; }
130 /// Access the y component
132 { return data()[1]; }
134 /// Get the data pointer
135 using SGVec2Storage<T>::data;
137 /// Readonly interface function to ssg's sgVec2/sgdVec2
138 const T (&sg(void) const)[2]
140 /// Interface function to ssg's sgVec2/sgdVec2
144 /// Interface function to osg's Vec2*
145 using SGVec2Storage<T>::osg;
148 SGVec2& operator+=(const SGVec2& v)
149 { data()[0] += v(0); data()[1] += v(1); return *this; }
150 /// Inplace subtraction
151 SGVec2& operator-=(const SGVec2& v)
152 { data()[0] -= v(0); data()[1] -= v(1); return *this; }
153 /// Inplace scalar multiplication
155 SGVec2& operator*=(S s)
156 { data()[0] *= s; data()[1] *= s; return *this; }
157 /// Inplace scalar multiplication by 1/s
159 SGVec2& operator/=(S s)
160 { return operator*=(1/T(s)); }
162 /// Return an all zero vector
163 static SGVec2 zeros(void)
164 { return SGVec2(0, 0); }
165 /// Return unit vectors
166 static SGVec2 e1(void)
167 { return SGVec2(1, 0); }
168 static SGVec2 e2(void)
169 { return SGVec2(0, 1); }
172 /// Unary +, do nothing ...
176 operator+(const SGVec2<T>& v)
179 /// Unary -, do nearly nothing
183 operator-(const SGVec2<T>& v)
184 { return SGVec2<T>(-v(0), -v(1)); }
190 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
191 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
197 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
198 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
200 /// Scalar multiplication
201 template<typename S, typename T>
204 operator*(S s, const SGVec2<T>& v)
205 { return SGVec2<T>(s*v(0), s*v(1)); }
207 /// Scalar multiplication
208 template<typename S, typename T>
211 operator*(const SGVec2<T>& v, S s)
212 { return SGVec2<T>(s*v(0), s*v(1)); }
214 /// multiplication as a multiplicator, that is assume that the first vector
215 /// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
216 /// Then the result is the product of that matrix times the second vector.
220 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
221 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
223 /// component wise min
227 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
228 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
229 template<typename S, typename T>
232 min(const SGVec2<T>& v, S s)
233 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
234 template<typename S, typename T>
237 min(S s, const SGVec2<T>& v)
238 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
240 /// component wise max
244 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
245 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
246 template<typename S, typename T>
249 max(const SGVec2<T>& v, S s)
250 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
251 template<typename S, typename T>
254 max(S s, const SGVec2<T>& v)
255 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
257 /// Scalar dot product
261 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
262 { return v1(0)*v2(0) + v1(1)*v2(1); }
264 /// The euclidean norm of the vector, that is what most people call length
268 norm(const SGVec2<T>& v)
269 { return sqrt(dot(v, v)); }
271 /// The euclidean norm of the vector, that is what most people call length
275 length(const SGVec2<T>& v)
276 { return sqrt(dot(v, v)); }
278 /// The 1-norm of the vector, this one is the fastest length function we
279 /// can implement on modern cpu's
283 norm1(const SGVec2<T>& v)
284 { return fabs(v(0)) + fabs(v(1)); }
286 /// The inf-norm of the vector
290 normI(const SGVec2<T>& v)
291 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
293 /// The euclidean norm of the vector, that is what most people call length
297 normalize(const SGVec2<T>& v)
298 { return (1/norm(v))*v; }
300 /// Return true if exactly the same
304 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
305 { return v1(0) == v2(0) && v1(1) == v2(1); }
307 /// Return true if not exactly the same
311 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
312 { return ! (v1 == v2); }
314 /// Return true if smaller, good for putting that into a std::map
318 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
320 if (v1(0) < v2(0)) return true;
321 else if (v2(0) < v1(0)) return false;
322 else return (v1(1) < v2(1));
328 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
330 if (v1(0) < v2(0)) return true;
331 else if (v2(0) < v1(0)) return false;
332 else return (v1(1) <= v2(1));
338 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
339 { return operator<(v2, v1); }
344 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
345 { return operator<=(v2, v1); }
347 /// Return true if equal to the relative tolerance tol
351 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
352 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
354 /// Return true if equal to the relative tolerance tol
358 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
359 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
361 /// Return true if about equal to roundoff of the underlying type
365 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
367 T tol = 100*SGLimits<T>::epsilon();
368 return equivalent(v1, v2, tol, tol);
371 /// The euclidean distance of the two vectors
375 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
376 { return norm(v1 - v2); }
378 /// The squared euclidean distance of the two vectors
382 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
383 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
389 isNaN(const SGVec2<T>& v)
391 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
395 /// Output to an ostream
396 template<typename char_type, typename traits_type, typename T>
398 std::basic_ostream<char_type, traits_type>&
399 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
400 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
404 toVec2f(const SGVec2d& v)
405 { return SGVec2f((float)v(0), (float)v(1)); }
409 toVec2d(const SGVec2f& v)
410 { return SGVec2d(v(0), v(1)); }