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; }
57 /// Access by index, the index is unchecked
58 const T& operator()(unsigned i) const
60 /// Access by index, the index is unchecked
61 T& operator()(unsigned i)
64 /// Access raw data by index, the index is unchecked
65 const T& operator[](unsigned i) const
67 /// Access raw data by index, the index is unchecked
68 T& operator[](unsigned i)
71 /// Access the x component
72 const T& x(void) const
74 /// Access the x component
77 /// Access the y component
78 const T& y(void) const
80 /// Access the y component
83 /// Access the z component
84 const T& z(void) const
86 /// Access the z component
89 /// Access the x component
90 const T& w(void) const
92 /// Access the x component
96 /// Readonly raw storage interface
97 const T (&data(void) const)[4]
99 /// Readonly raw storage interface
104 SGVec4& operator+=(const SGVec4& v)
105 { data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; }
106 /// Inplace subtraction
107 SGVec4& operator-=(const SGVec4& v)
108 { data()[0]-=v(0);data()[1]-=v(1);data()[2]-=v(2);data()[3]-=v(3);return *this; }
109 /// Inplace scalar multiplication
111 SGVec4& operator*=(S s)
112 { data()[0] *= s; data()[1] *= s; data()[2] *= s; data()[3] *= s; return *this; }
113 /// Inplace scalar multiplication by 1/s
115 SGVec4& operator/=(S s)
116 { return operator*=(1/T(s)); }
118 /// Return an all zero vector
119 static SGVec4 zeros(void)
120 { return SGVec4(0, 0, 0, 0); }
121 /// Return unit vectors
122 static SGVec4 e1(void)
123 { return SGVec4(1, 0, 0, 0); }
124 static SGVec4 e2(void)
125 { return SGVec4(0, 1, 0, 0); }
126 static SGVec4 e3(void)
127 { return SGVec4(0, 0, 1, 0); }
128 static SGVec4 e4(void)
129 { return SGVec4(0, 0, 0, 1); }
135 /// Unary +, do nothing ...
139 operator+(const SGVec4<T>& v)
142 /// Unary -, do nearly nothing
146 operator-(const SGVec4<T>& v)
147 { return SGVec4<T>(-v(0), -v(1), -v(2), -v(3)); }
153 operator+(const SGVec4<T>& v1, const SGVec4<T>& v2)
154 { return SGVec4<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
160 operator-(const SGVec4<T>& v1, const SGVec4<T>& v2)
161 { return SGVec4<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
163 /// Scalar multiplication
164 template<typename S, typename T>
167 operator*(S s, const SGVec4<T>& v)
168 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
170 /// Scalar multiplication
171 template<typename S, typename T>
174 operator*(const SGVec4<T>& v, S s)
175 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
177 /// multiplication as a multiplicator, that is assume that the first vector
178 /// represents a 4x4 diagonal matrix with the diagonal elements in the vector.
179 /// Then the result is the product of that matrix times the second vector.
183 mult(const SGVec4<T>& v1, const SGVec4<T>& v2)
184 { return SGVec4<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2), v1(3)*v2(3)); }
186 /// component wise min
190 min(const SGVec4<T>& v1, const SGVec4<T>& v2)
192 return SGVec4<T>(SGMisc<T>::min(v1(0), v2(0)),
193 SGMisc<T>::min(v1(1), v2(1)),
194 SGMisc<T>::min(v1(2), v2(2)),
195 SGMisc<T>::min(v1(3), v2(3)));
197 template<typename S, typename T>
200 min(const SGVec4<T>& v, S s)
202 return SGVec4<T>(SGMisc<T>::min(s, v(0)),
203 SGMisc<T>::min(s, v(1)),
204 SGMisc<T>::min(s, v(2)),
205 SGMisc<T>::min(s, v(3)));
207 template<typename S, typename T>
210 min(S s, const SGVec4<T>& v)
212 return SGVec4<T>(SGMisc<T>::min(s, v(0)),
213 SGMisc<T>::min(s, v(1)),
214 SGMisc<T>::min(s, v(2)),
215 SGMisc<T>::min(s, v(3)));
218 /// component wise max
222 max(const SGVec4<T>& v1, const SGVec4<T>& v2)
224 return SGVec4<T>(SGMisc<T>::max(v1(0), v2(0)),
225 SGMisc<T>::max(v1(1), v2(1)),
226 SGMisc<T>::max(v1(2), v2(2)),
227 SGMisc<T>::max(v1(3), v2(3)));
229 template<typename S, typename T>
232 max(const SGVec4<T>& v, S s)
234 return SGVec4<T>(SGMisc<T>::max(s, v(0)),
235 SGMisc<T>::max(s, v(1)),
236 SGMisc<T>::max(s, v(2)),
237 SGMisc<T>::max(s, v(3)));
239 template<typename S, typename T>
242 max(S s, const SGVec4<T>& v)
244 return SGVec4<T>(SGMisc<T>::max(s, v(0)),
245 SGMisc<T>::max(s, v(1)),
246 SGMisc<T>::max(s, v(2)),
247 SGMisc<T>::max(s, v(3)));
250 /// Scalar dot product
254 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
255 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
257 /// The euclidean norm of the vector, that is what most people call length
261 norm(const SGVec4<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 SGVec4<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 SGVec4<T>& v)
277 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
279 /// The inf-norm of the vector
283 normI(const SGVec4<T>& v)
284 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2)), fabs(v(2))); }
286 /// The euclidean norm of the vector, that is what most people call length
290 normalize(const SGVec4<T>& v)
293 if (normv <= SGLimits<T>::min())
294 return SGVec4<T>::zeros();
298 /// Return true if exactly the same
302 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
303 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
305 /// Return true if not exactly the same
309 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
310 { return ! (v1 == v2); }
312 /// Return true if smaller, good for putting that into a std::map
316 operator<(const SGVec4<T>& v1, const SGVec4<T>& v2)
318 if (v1(0) < v2(0)) return true;
319 else if (v2(0) < v1(0)) return false;
320 else if (v1(1) < v2(1)) return true;
321 else if (v2(1) < v1(1)) return false;
322 else if (v1(2) < v2(2)) return true;
323 else if (v2(2) < v1(2)) return false;
324 else return (v1(3) < v2(3));
330 operator<=(const SGVec4<T>& v1, const SGVec4<T>& v2)
332 if (v1(0) < v2(0)) return true;
333 else if (v2(0) < v1(0)) return false;
334 else if (v1(1) < v2(1)) return true;
335 else if (v2(1) < v1(1)) return false;
336 else if (v1(2) < v2(2)) return true;
337 else if (v2(2) < v1(2)) return false;
338 else return (v1(3) <= v2(3));
344 operator>(const SGVec4<T>& v1, const SGVec4<T>& v2)
345 { return operator<(v2, v1); }
350 operator>=(const SGVec4<T>& v1, const SGVec4<T>& v2)
351 { return operator<=(v2, v1); }
353 /// Return true if equal to the relative tolerance tol
357 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
358 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
360 /// Return true if equal to the relative tolerance tol
364 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
365 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
367 /// Return true if about equal to roundoff of the underlying type
371 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
373 T tol = 100*SGLimits<T>::epsilon();
374 return equivalent(v1, v2, tol, tol);
377 /// The euclidean distance of the two vectors
381 dist(const SGVec4<T>& v1, const SGVec4<T>& v2)
382 { return norm(v1 - v2); }
384 /// The squared euclidean distance of the two vectors
388 distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
389 { SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
391 // calculate the projection of u along the direction of d.
395 projection(const SGVec4<T>& u, const SGVec4<T>& d)
399 if (SGLimits<T>::min() < denom) return u;
400 else return d * (dot(u, d) / denom);
407 isNaN(const SGVec4<T>& v)
409 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
410 || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
414 /// Output to an ostream
415 template<typename char_type, typename traits_type, typename T>
417 std::basic_ostream<char_type, traits_type>&
418 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
419 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
423 toVec4f(const SGVec4d& v)
424 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
428 toVec4d(const SGVec4f& v)
429 { return SGVec4d(v(0), v(1), v(2), v(3)); }
431 #ifndef NO_OPENSCENEGRAPH_INTERFACE
434 toSG(const osg::Vec4d& v)
435 { return SGVec4d(v[0], v[1], v[2], v[3]); }
439 toSG(const osg::Vec4f& v)
440 { return SGVec4f(v[0], v[1], v[2], v[3]); }
444 toOsg(const SGVec4d& v)
445 { return osg::Vec4d(v[0], v[1], v[2], v[3]); }
449 toOsg(const SGVec4f& v)
450 { return osg::Vec4f(v[0], v[1], v[2], v[3]); }