1 // Copyright (C) 2006-2009 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 #ifndef NO_OPENSCENEGRAPH_INTERFACE
32 /// Default constructor. Does not initialize at all.
33 /// If you need them zero initialized, use SGVec4::zeros()
36 /// Initialize with nans in the debug build, that will guarantee to have
37 /// a fast uninitialized default constructor in the release but shows up
38 /// uninitialized values in the debug build very fast ...
40 for (unsigned i = 0; i < 4; ++i)
41 data()[i] = SGLimits<T>::quiet_NaN();
44 /// Constructor. Initialize by the given values
45 SGVec4(T x, T y, T z, T w)
46 { data()[0] = x; data()[1] = y; data()[2] = z; data()[3] = w; }
47 /// Constructor. Initialize by the content of a plain array,
48 /// make sure it has at least 3 elements
49 explicit SGVec4(const T* d)
50 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
52 explicit SGVec4(const SGVec4<S>& d)
53 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
54 explicit SGVec4(const SGVec3<T>& v3, const T& v4 = 0)
55 { data()[0] = v3[0]; data()[1] = v3[1]; data()[2] = v3[2]; data()[3] = v4; }
56 #ifndef NO_OPENSCENEGRAPH_INTERFACE
57 explicit SGVec4(const osg::Vec4f& d)
58 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
59 explicit SGVec4(const osg::Vec4d& d)
60 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
63 /// Access by index, the index is unchecked
64 const T& operator()(unsigned i) const
66 /// Access by index, the index is unchecked
67 T& operator()(unsigned i)
70 /// Access raw data by index, the index is unchecked
71 const T& operator[](unsigned i) const
73 /// Access raw data by index, the index is unchecked
74 T& operator[](unsigned i)
77 /// Access the x component
78 const T& x(void) const
80 /// Access the x component
83 /// Access the y component
84 const T& y(void) const
86 /// Access the y component
89 /// Access the z component
90 const T& z(void) const
92 /// Access the z component
95 /// Access the x component
96 const T& w(void) const
98 /// Access the x component
100 { return data()[3]; }
102 /// Readonly raw storage interface
103 const T (&data(void) const)[4]
105 /// Readonly raw storage interface
109 #ifndef NO_OPENSCENEGRAPH_INTERFACE
110 osg::Vec4d osg() const
111 { return osg::Vec4d(data()[0], data()[1], data()[2], data()[3]); }
115 SGVec4& operator+=(const SGVec4& v)
116 { data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; }
117 /// Inplace subtraction
118 SGVec4& operator-=(const SGVec4& v)
119 { data()[0]-=v(0);data()[1]-=v(1);data()[2]-=v(2);data()[3]-=v(3);return *this; }
120 /// Inplace scalar multiplication
122 SGVec4& operator*=(S s)
123 { data()[0] *= s; data()[1] *= s; data()[2] *= s; data()[3] *= s; return *this; }
124 /// Inplace scalar multiplication by 1/s
126 SGVec4& operator/=(S s)
127 { return operator*=(1/T(s)); }
129 /// Return an all zero vector
130 static SGVec4 zeros(void)
131 { return SGVec4(0, 0, 0, 0); }
132 /// Return unit vectors
133 static SGVec4 e1(void)
134 { return SGVec4(1, 0, 0, 0); }
135 static SGVec4 e2(void)
136 { return SGVec4(0, 1, 0, 0); }
137 static SGVec4 e3(void)
138 { return SGVec4(0, 0, 1, 0); }
139 static SGVec4 e4(void)
140 { return SGVec4(0, 0, 0, 1); }
146 /// Unary +, do nothing ...
150 operator+(const SGVec4<T>& v)
153 /// Unary -, do nearly nothing
157 operator-(const SGVec4<T>& v)
158 { return SGVec4<T>(-v(0), -v(1), -v(2), -v(3)); }
164 operator+(const SGVec4<T>& v1, const SGVec4<T>& v2)
165 { return SGVec4<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
171 operator-(const SGVec4<T>& v1, const SGVec4<T>& v2)
172 { return SGVec4<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
174 /// Scalar multiplication
175 template<typename S, typename T>
178 operator*(S s, const SGVec4<T>& v)
179 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
181 /// Scalar multiplication
182 template<typename S, typename T>
185 operator*(const SGVec4<T>& v, S s)
186 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
188 /// multiplication as a multiplicator, that is assume that the first vector
189 /// represents a 4x4 diagonal matrix with the diagonal elements in the vector.
190 /// Then the result is the product of that matrix times the second vector.
194 mult(const SGVec4<T>& v1, const SGVec4<T>& v2)
195 { return SGVec4<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2), v1(3)*v2(3)); }
197 /// component wise min
201 min(const SGVec4<T>& v1, const SGVec4<T>& v2)
203 return SGVec4<T>(SGMisc<T>::min(v1(0), v2(0)),
204 SGMisc<T>::min(v1(1), v2(1)),
205 SGMisc<T>::min(v1(2), v2(2)),
206 SGMisc<T>::min(v1(3), v2(3)));
208 template<typename S, typename T>
211 min(const SGVec4<T>& v, S s)
213 return SGVec4<T>(SGMisc<T>::min(s, v(0)),
214 SGMisc<T>::min(s, v(1)),
215 SGMisc<T>::min(s, v(2)),
216 SGMisc<T>::min(s, v(3)));
218 template<typename S, typename T>
221 min(S s, const SGVec4<T>& v)
223 return SGVec4<T>(SGMisc<T>::min(s, v(0)),
224 SGMisc<T>::min(s, v(1)),
225 SGMisc<T>::min(s, v(2)),
226 SGMisc<T>::min(s, v(3)));
229 /// component wise max
233 max(const SGVec4<T>& v1, const SGVec4<T>& v2)
235 return SGVec4<T>(SGMisc<T>::max(v1(0), v2(0)),
236 SGMisc<T>::max(v1(1), v2(1)),
237 SGMisc<T>::max(v1(2), v2(2)),
238 SGMisc<T>::max(v1(3), v2(3)));
240 template<typename S, typename T>
243 max(const SGVec4<T>& v, S s)
245 return SGVec4<T>(SGMisc<T>::max(s, v(0)),
246 SGMisc<T>::max(s, v(1)),
247 SGMisc<T>::max(s, v(2)),
248 SGMisc<T>::max(s, v(3)));
250 template<typename S, typename T>
253 max(S s, const SGVec4<T>& v)
255 return SGVec4<T>(SGMisc<T>::max(s, v(0)),
256 SGMisc<T>::max(s, v(1)),
257 SGMisc<T>::max(s, v(2)),
258 SGMisc<T>::max(s, v(3)));
261 /// Scalar dot product
265 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
266 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
268 /// The euclidean norm of the vector, that is what most people call length
272 norm(const SGVec4<T>& v)
273 { return sqrt(dot(v, v)); }
275 /// The euclidean norm of the vector, that is what most people call length
279 length(const SGVec4<T>& v)
280 { return sqrt(dot(v, v)); }
282 /// The 1-norm of the vector, this one is the fastest length function we
283 /// can implement on modern cpu's
287 norm1(const SGVec4<T>& v)
288 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
290 /// The inf-norm of the vector
294 normI(const SGVec4<T>& v)
295 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2)), fabs(v(2))); }
297 /// The euclidean norm of the vector, that is what most people call length
301 normalize(const SGVec4<T>& v)
302 { return (1/norm(v))*v; }
304 /// Return true if exactly the same
308 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
309 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
311 /// Return true if not exactly the same
315 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
316 { return ! (v1 == v2); }
318 /// Return true if smaller, good for putting that into a std::map
322 operator<(const SGVec4<T>& v1, const SGVec4<T>& v2)
324 if (v1(0) < v2(0)) return true;
325 else if (v2(0) < v1(0)) return false;
326 else if (v1(1) < v2(1)) return true;
327 else if (v2(1) < v1(1)) return false;
328 else if (v1(2) < v2(2)) return true;
329 else if (v2(2) < v1(2)) return false;
330 else return (v1(3) < v2(3));
336 operator<=(const SGVec4<T>& v1, const SGVec4<T>& v2)
338 if (v1(0) < v2(0)) return true;
339 else if (v2(0) < v1(0)) return false;
340 else if (v1(1) < v2(1)) return true;
341 else if (v2(1) < v1(1)) return false;
342 else if (v1(2) < v2(2)) return true;
343 else if (v2(2) < v1(2)) return false;
344 else return (v1(3) <= v2(3));
350 operator>(const SGVec4<T>& v1, const SGVec4<T>& v2)
351 { return operator<(v2, v1); }
356 operator>=(const SGVec4<T>& v1, const SGVec4<T>& v2)
357 { return operator<=(v2, v1); }
359 /// Return true if equal to the relative tolerance tol
363 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
364 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
366 /// Return true if equal to the relative tolerance tol
370 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
371 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
373 /// Return true if about equal to roundoff of the underlying type
377 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
379 T tol = 100*SGLimits<T>::epsilon();
380 return equivalent(v1, v2, tol, tol);
383 /// The euclidean distance of the two vectors
387 dist(const SGVec4<T>& v1, const SGVec4<T>& v2)
388 { return norm(v1 - v2); }
390 /// The squared euclidean distance of the two vectors
394 distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
395 { SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
401 isNaN(const SGVec4<T>& v)
403 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
404 || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
408 /// Output to an ostream
409 template<typename char_type, typename traits_type, typename T>
411 std::basic_ostream<char_type, traits_type>&
412 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
413 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
417 toVec4f(const SGVec4d& v)
418 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
422 toVec4d(const SGVec4f& v)
423 { return SGVec4d(v(0), v(1), v(2), v(3)); }
425 #ifndef NO_OPENSCENEGRAPH_INTERFACE
428 toSG(const osg::Vec4d& v)
429 { return SGVec4d(v[0], v[1], v[2], v[3]); }
433 toSG(const osg::Vec4f& v)
434 { return SGVec4f(v[0], v[1], v[2], v[3]); }
438 toOsg(const SGVec4d& v)
439 { return osg::Vec4d(v[0], v[1], v[2], v[3]); }
443 toOsg(const SGVec4f& v)
444 { return osg::Vec4f(v[0], v[1], v[2], v[3]); }