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 /// Scalar dot product
211 dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
212 { return v1(0)*v2(0) + v1(1)*v2(1); }
214 /// The euclidean norm of the vector, that is what most people call length
218 norm(const SGVec2<T>& v)
219 { return sqrt(dot(v, v)); }
221 /// The euclidean norm of the vector, that is what most people call length
225 length(const SGVec2<T>& v)
226 { return sqrt(dot(v, v)); }
228 /// The 1-norm of the vector, this one is the fastest length function we
229 /// can implement on modern cpu's
233 norm1(const SGVec2<T>& v)
234 { return fabs(v(0)) + fabs(v(1)); }
236 /// The euclidean norm of the vector, that is what most people call length
240 normalize(const SGVec2<T>& v)
241 { return (1/norm(v))*v; }
243 /// Return true if exactly the same
247 operator==(const SGVec2<T>& v1, const SGVec2<T>& v2)
248 { return v1(0) == v2(0) && v1(1) == v2(1); }
250 /// Return true if not exactly the same
254 operator!=(const SGVec2<T>& v1, const SGVec2<T>& v2)
255 { return ! (v1 == v2); }
257 /// Return true if equal to the relative tolerance tol
261 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol, T atol)
262 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
264 /// Return true if equal to the relative tolerance tol
268 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2, T rtol)
269 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
271 /// Return true if about equal to roundoff of the underlying type
275 equivalent(const SGVec2<T>& v1, const SGVec2<T>& v2)
277 T tol = 100*SGLimits<T>::epsilon();
278 return equivalent(v1, v2, tol, tol);
281 /// The euclidean distance of the two vectors
285 dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
286 { return norm(v1 - v2); }
288 /// The squared euclidean distance of the two vectors
292 distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
293 { SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
299 isNaN(const SGVec2<T>& v)
301 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1));
305 /// Output to an ostream
306 template<typename char_type, typename traits_type, typename T>
308 std::basic_ostream<char_type, traits_type>&
309 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
310 { return s << "[ " << v(0) << ", " << v(1) << " ]"; }
314 toVec2f(const SGVec2d& v)
315 { return SGVec2f((float)v(0), (float)v(1)); }
319 toVec2d(const SGVec2f& v)
320 { return SGVec2d(v(0), v(1)); }