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 /// component wise min
211 min(const SGVec2<T>& v1, const SGVec2<T>& v2)
212 {return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
213 template<typename S, typename T>
216 min(const SGVec2<T>& v, S s)
217 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
218 template<typename S, typename T>
221 min(S s, const SGVec2<T>& v)
222 { return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
224 /// component wise max
228 max(const SGVec2<T>& v1, const SGVec2<T>& v2)
229 {return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
230 template<typename S, typename T>
233 max(const SGVec2<T>& v, S s)
234 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
235 template<typename S, typename T>
238 max(S s, const SGVec2<T>& v)
239 { return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
241 /// Scalar dot product
245 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
246 { return v1(0)*v2(0) + v1(1)*v2(1); }
248 /// The euclidean norm of the vector, that is what most people call length
252 norm(const SGVec2<T>& v)
253 { return sqrt(dot(v, v)); }
255 /// The euclidean norm of the vector, that is what most people call length
259 length(const SGVec2<T>& v)
260 { return sqrt(dot(v, v)); }
262 /// The 1-norm of the vector, this one is the fastest length function we
263 /// can implement on modern cpu's
267 norm1(const SGVec2<T>& v)
268 { return fabs(v(0)) + fabs(v(1)); }
270 /// The euclidean norm of the vector, that is what most people call length
274 normalize(const SGVec2<T>& v)
275 { return (1/norm(v))*v; }
277 /// Return true if exactly the same
281 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
282 { return v1(0) == v2(0) && v1(1) == v2(1); }
284 /// Return true if not exactly the same
288 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
289 { return ! (v1 == v2); }
291 /// Return true if equal to the relative tolerance tol
295 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
296 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
298 /// Return true if equal to the relative tolerance tol
302 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
303 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
305 /// Return true if about equal to roundoff of the underlying type
309 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
311 T tol = 100*SGLimits<T>::epsilon();
312 return equivalent(v1, v2, tol, tol);
315 /// The euclidean distance of the two vectors
319 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
320 { return norm(v1 - v2); }
322 /// The squared euclidean distance of the two vectors
326 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
327 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
333 isNaN(const SGVec2<T>& v)
335 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
339 /// Output to an ostream
340 template<typename char_type, typename traits_type, typename T>
342 std::basic_ostream<char_type, traits_type>&
343 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
344 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
348 toVec2f(const SGVec2d& v)
349 { return SGVec2f((float)v(0), (float)v(1)); }
353 toVec2d(const SGVec2f& v)
354 { return SGVec2d(v(0), v(1)); }