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.
25 struct SGVec2Storage {
26 /// Readonly raw storage interface
27 const T (&data(void) const)[2]
29 /// Readonly raw storage interface
41 struct SGVec2Storage<float> : public osg::Vec2f {
42 /// Access raw data by index, the index is unchecked
43 const float (&data(void) const)[2]
44 { return osg::Vec2f::_v; }
45 /// Access raw data by index, the index is unchecked
46 float (&data(void))[2]
47 { return osg::Vec2f::_v; }
49 const osg::Vec2f& osg() const
56 struct SGVec2Storage<double> : public osg::Vec2d {
57 /// Access raw data by index, the index is unchecked
58 const double (&data(void) const)[2]
59 { return osg::Vec2d::_v; }
60 /// Access raw data by index, the index is unchecked
61 double (&data(void))[2]
62 { return osg::Vec2d::_v; }
64 const osg::Vec2d& osg() const
72 class SGVec2 : protected SGVec2Storage<T> {
76 /// Default constructor. Does not initialize at all.
77 /// If you need them zero initialized, use SGVec2::zeros()
80 /// Initialize with nans in the debug build, that will guarantee to have
81 /// a fast uninitialized default constructor in the release but shows up
82 /// uninitialized values in the debug build very fast ...
84 for (unsigned i = 0; i < 2; ++i)
85 data()[i] = SGLimits<T>::quiet_NaN();
88 /// Constructor. Initialize by the given values
90 { data()[0] = x; data()[1] = y; }
91 /// Constructor. Initialize by the content of a plain array,
92 /// make sure it has at least 2 elements
93 explicit SGVec2(const T* d)
94 { data()[0] = d[0]; data()[1] = d[1]; }
95 explicit SGVec2(const osg::Vec2f& d)
96 { data()[0] = d[0]; data()[1] = d[1]; }
97 explicit SGVec2(const osg::Vec2d& d)
98 { data()[0] = d[0]; data()[1] = d[1]; }
100 /// Access by index, the index is unchecked
101 const T& operator()(unsigned i) const
102 { return data()[i]; }
103 /// Access by index, the index is unchecked
104 T& operator()(unsigned i)
105 { return data()[i]; }
107 /// Access raw data by index, the index is unchecked
108 const T& operator[](unsigned i) const
109 { return data()[i]; }
110 /// Access raw data by index, the index is unchecked
111 T& operator[](unsigned i)
112 { return data()[i]; }
114 /// Access the x component
115 const T& x(void) const
116 { return data()[0]; }
117 /// Access the x component
119 { return data()[0]; }
120 /// Access the y component
121 const T& y(void) const
122 { return data()[1]; }
123 /// Access the y component
125 { return data()[1]; }
127 /// Get the data pointer
128 using SGVec2Storage<T>::data;
130 /// Readonly interface function to ssg's sgVec2/sgdVec2
131 const T (&sg(void) const)[2]
133 /// Interface function to ssg's sgVec2/sgdVec2
137 /// Interface function to osg's Vec2*
138 using SGVec2Storage<T>::osg;
141 SGVec2& operator+=(const SGVec2& v)
142 { data()[0] += v(0); data()[1] += v(1); return *this; }
143 /// Inplace subtraction
144 SGVec2& operator-=(const SGVec2& v)
145 { data()[0] -= v(0); data()[1] -= v(1); return *this; }
146 /// Inplace scalar multiplication
148 SGVec2& operator*=(S s)
149 { data()[0] *= s; data()[1] *= s; return *this; }
150 /// Inplace scalar multiplication by 1/s
152 SGVec2& operator/=(S s)
153 { return operator*=(1/T(s)); }
155 /// Return an all zero vector
156 static SGVec2 zeros(void)
157 { return SGVec2(0, 0); }
158 /// Return unit vectors
159 static SGVec2 e1(void)
160 { return SGVec2(1, 0); }
161 static SGVec2 e2(void)
162 { return SGVec2(0, 1); }
165 /// Unary +, do nothing ...
169 operator+(const SGVec2<T>& v)
172 /// Unary -, do nearly nothing
176 operator-(const SGVec2<T>& v)
177 { return SGVec2<T>(-v(0), -v(1)); }
183 operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
184 { return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
190 operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
191 { return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
193 /// Scalar multiplication
194 template<typename S, typename T>
197 operator*(S s, const SGVec2<T>& v)
198 { return SGVec2<T>(s*v(0), s*v(1)); }
200 /// Scalar multiplication
201 template<typename S, typename T>
204 operator*(const SGVec2<T>& v, S s)
205 { return SGVec2<T>(s*v(0), s*v(1)); }
207 /// multiplication as a multiplicator, that is assume that the first vector
208 /// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
209 /// Then the result is the product of that matrix times the second vector.
213 mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
214 { return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
216 /// component wise min
220 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
221 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
222 template<typename S, typename T>
225 min(const SGVec2<T>& v, S s)
226 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
227 template<typename S, typename T>
230 min(S s, const SGVec2<T>& v)
231 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
233 /// component wise max
237 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
238 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
239 template<typename S, typename T>
242 max(const SGVec2<T>& v, S s)
243 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
244 template<typename S, typename T>
247 max(S s, const SGVec2<T>& v)
248 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
250 /// Scalar dot product
254 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
255 { return v1(0)*v2(0) + v1(1)*v2(1); }
257 /// The euclidean norm of the vector, that is what most people call length
261 norm(const SGVec2<T>& v)
262 { return sqrt(dot(v, v)); }
264 /// The euclidean norm of the vector, that is what most people call length
268 length(const SGVec2<T>& v)
269 { return sqrt(dot(v, v)); }
271 /// The 1-norm of the vector, this one is the fastest length function we
272 /// can implement on modern cpu's
276 norm1(const SGVec2<T>& v)
277 { return fabs(v(0)) + fabs(v(1)); }
279 /// The inf-norm of the vector
283 normI(const SGVec2<T>& v)
284 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
286 /// The euclidean norm of the vector, that is what most people call length
290 normalize(const SGVec2<T>& v)
291 { return (1/norm(v))*v; }
293 /// Return true if exactly the same
297 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
298 { return v1(0) == v2(0) && v1(1) == v2(1); }
300 /// Return true if not exactly the same
304 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
305 { return ! (v1 == v2); }
307 /// Return true if smaller, good for putting that into a std::map
311 operator<(const SGVec2<T>& v1, const SGVec2<T>& v2)
313 if (v1(0) < v2(0)) return true;
314 else if (v2(0) < v1(0)) return false;
315 else return (v1(1) < v2(1));
321 operator<=(const SGVec2<T>& v1, const SGVec2<T>& v2)
323 if (v1(0) < v2(0)) return true;
324 else if (v2(0) < v1(0)) return false;
325 else return (v1(1) <= v2(1));
331 operator>(const SGVec2<T>& v1, const SGVec2<T>& v2)
332 { return operator<(v2, v1); }
337 operator>=(const SGVec2<T>& v1, const SGVec2<T>& v2)
338 { return operator<=(v2, v1); }
340 /// Return true if equal to the relative tolerance tol
344 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
345 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
347 /// Return true if equal to the relative tolerance tol
351 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
352 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
354 /// Return true if about equal to roundoff of the underlying type
358 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
360 T tol = 100*SGLimits<T>::epsilon();
361 return equivalent(v1, v2, tol, tol);
364 /// The euclidean distance of the two vectors
368 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
369 { return norm(v1 - v2); }
371 /// The squared euclidean distance of the two vectors
375 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
376 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
382 isNaN(const SGVec2<T>& v)
384 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
388 /// Output to an ostream
389 template<typename char_type, typename traits_type, typename T>
391 std::basic_ostream<char_type, traits_type>&
392 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
393 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
397 toVec2f(const SGVec2d& v)
398 { return SGVec2f((float)v(0), (float)v(1)); }
402 toVec2d(const SGVec2f& v)
403 { return SGVec2d(v(0), v(1)); }