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.
27 /// Default constructor. Does not initialize at all.
28 /// If you need them zero initialized, use SGVec4::zeros()
31 /// Initialize with nans in the debug build, that will guarantee to have
32 /// a fast uninitialized default constructor in the release but shows up
33 /// uninitialized values in the debug build very fast ...
35 for (unsigned i = 0; i < 4; ++i)
36 data()[i] = SGLimits<T>::quiet_NaN();
39 /// Constructor. Initialize by the given values
40 SGVec4(T x, T y, T z, T w)
41 { data()[0] = x; data()[1] = y; data()[2] = z; data()[3] = w; }
42 /// Constructor. Initialize by the content of a plain array,
43 /// make sure it has at least 3 elements
44 explicit SGVec4(const T* d)
45 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
47 explicit SGVec4(const SGVec4<S>& d)
48 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
49 explicit SGVec4(const SGVec3<T>& v3, const T& v4 = 0)
50 { data()[0] = v3[0]; data()[1] = v3[1]; data()[2] = v3[2]; data()[3] = v4; }
52 /// Access by index, the index is unchecked
53 const T& operator()(unsigned i) const
55 /// Access by index, the index is unchecked
56 T& operator()(unsigned i)
59 /// Access raw data by index, the index is unchecked
60 const T& operator[](unsigned i) const
62 /// Access raw data by index, the index is unchecked
63 T& operator[](unsigned i)
66 /// Access the x component
67 const T& x(void) const
69 /// Access the x component
72 /// Access the y component
73 const T& y(void) const
75 /// Access the y component
78 /// Access the z component
79 const T& z(void) const
81 /// Access the z component
84 /// Access the x component
85 const T& w(void) const
87 /// Access the x component
91 /// Readonly raw storage interface
92 const T (&data(void) const)[4]
94 /// Readonly raw storage interface
99 SGVec4& operator+=(const SGVec4& v)
100 { data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; }
101 /// Inplace subtraction
102 SGVec4& operator-=(const SGVec4& v)
103 { data()[0]-=v(0);data()[1]-=v(1);data()[2]-=v(2);data()[3]-=v(3);return *this; }
104 /// Inplace scalar multiplication
106 SGVec4& operator*=(S s)
107 { data()[0] *= s; data()[1] *= s; data()[2] *= s; data()[3] *= s; return *this; }
108 /// Inplace scalar multiplication by 1/s
110 SGVec4& operator/=(S s)
111 { return operator*=(1/T(s)); }
113 /// Return an all zero vector
114 static SGVec4 zeros(void)
115 { return SGVec4(0, 0, 0, 0); }
116 /// Return unit vectors
117 static SGVec4 e1(void)
118 { return SGVec4(1, 0, 0, 0); }
119 static SGVec4 e2(void)
120 { return SGVec4(0, 1, 0, 0); }
121 static SGVec4 e3(void)
122 { return SGVec4(0, 0, 1, 0); }
123 static SGVec4 e4(void)
124 { return SGVec4(0, 0, 0, 1); }
130 /// Unary +, do nothing ...
134 operator+(const SGVec4<T>& v)
137 /// Unary -, do nearly nothing
141 operator-(const SGVec4<T>& v)
142 { return SGVec4<T>(-v(0), -v(1), -v(2), -v(3)); }
148 operator+(const SGVec4<T>& v1, const SGVec4<T>& v2)
149 { return SGVec4<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
155 operator-(const SGVec4<T>& v1, const SGVec4<T>& v2)
156 { return SGVec4<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
158 /// Scalar multiplication
159 template<typename S, typename T>
162 operator*(S s, const SGVec4<T>& v)
163 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
165 /// Scalar multiplication
166 template<typename S, typename T>
169 operator*(const SGVec4<T>& v, S s)
170 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
172 /// multiplication as a multiplicator, that is assume that the first vector
173 /// represents a 4x4 diagonal matrix with the diagonal elements in the vector.
174 /// Then the result is the product of that matrix times the second vector.
178 mult(const SGVec4<T>& v1, const SGVec4<T>& v2)
179 { return SGVec4<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2), v1(3)*v2(3)); }
181 /// component wise min
185 min(const SGVec4<T>& v1, const SGVec4<T>& v2)
187 return SGVec4<T>(SGMisc<T>::min(v1(0), v2(0)),
188 SGMisc<T>::min(v1(1), v2(1)),
189 SGMisc<T>::min(v1(2), v2(2)),
190 SGMisc<T>::min(v1(3), v2(3)));
192 template<typename S, typename T>
195 min(const SGVec4<T>& v, S s)
197 return SGVec4<T>(SGMisc<T>::min(s, v(0)),
198 SGMisc<T>::min(s, v(1)),
199 SGMisc<T>::min(s, v(2)),
200 SGMisc<T>::min(s, v(3)));
202 template<typename S, typename T>
205 min(S s, const SGVec4<T>& v)
207 return SGVec4<T>(SGMisc<T>::min(s, v(0)),
208 SGMisc<T>::min(s, v(1)),
209 SGMisc<T>::min(s, v(2)),
210 SGMisc<T>::min(s, v(3)));
213 /// component wise max
217 max(const SGVec4<T>& v1, const SGVec4<T>& v2)
219 return SGVec4<T>(SGMisc<T>::max(v1(0), v2(0)),
220 SGMisc<T>::max(v1(1), v2(1)),
221 SGMisc<T>::max(v1(2), v2(2)),
222 SGMisc<T>::max(v1(3), v2(3)));
224 template<typename S, typename T>
227 max(const SGVec4<T>& v, S s)
229 return SGVec4<T>(SGMisc<T>::max(s, v(0)),
230 SGMisc<T>::max(s, v(1)),
231 SGMisc<T>::max(s, v(2)),
232 SGMisc<T>::max(s, v(3)));
234 template<typename S, typename T>
237 max(S s, const SGVec4<T>& v)
239 return SGVec4<T>(SGMisc<T>::max(s, v(0)),
240 SGMisc<T>::max(s, v(1)),
241 SGMisc<T>::max(s, v(2)),
242 SGMisc<T>::max(s, v(3)));
245 /// Scalar dot product
249 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
250 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
252 /// The euclidean norm of the vector, that is what most people call length
256 norm(const SGVec4<T>& v)
257 { return sqrt(dot(v, v)); }
259 /// The euclidean norm of the vector, that is what most people call length
263 length(const SGVec4<T>& v)
264 { return sqrt(dot(v, v)); }
266 /// The 1-norm of the vector, this one is the fastest length function we
267 /// can implement on modern cpu's
271 norm1(const SGVec4<T>& v)
272 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
274 /// The inf-norm of the vector
278 normI(const SGVec4<T>& v)
279 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2)), fabs(v(2))); }
281 /// The euclidean norm of the vector, that is what most people call length
285 normalize(const SGVec4<T>& v)
288 if (normv <= SGLimits<T>::min())
289 return SGVec4<T>::zeros();
293 /// Return true if exactly the same
297 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
298 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
300 /// Return true if not exactly the same
304 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
305 { return ! (v1 == v2); }
307 /// Return true if smaller, good for putting that into a std::map
311 operator<(const SGVec4<T>& v1, const SGVec4<T>& v2)
313 if (v1(0) < v2(0)) return true;
314 else if (v2(0) < v1(0)) return false;
315 else if (v1(1) < v2(1)) return true;
316 else if (v2(1) < v1(1)) return false;
317 else if (v1(2) < v2(2)) return true;
318 else if (v2(2) < v1(2)) return false;
319 else return (v1(3) < v2(3));
325 operator<=(const SGVec4<T>& v1, const SGVec4<T>& v2)
327 if (v1(0) < v2(0)) return true;
328 else if (v2(0) < v1(0)) return false;
329 else if (v1(1) < v2(1)) return true;
330 else if (v2(1) < v1(1)) return false;
331 else if (v1(2) < v2(2)) return true;
332 else if (v2(2) < v1(2)) return false;
333 else return (v1(3) <= v2(3));
339 operator>(const SGVec4<T>& v1, const SGVec4<T>& v2)
340 { return operator<(v2, v1); }
345 operator>=(const SGVec4<T>& v1, const SGVec4<T>& v2)
346 { return operator<=(v2, v1); }
348 /// Return true if equal to the relative tolerance tol
352 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
353 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
355 /// Return true if equal to the relative tolerance tol
359 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
360 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
362 /// Return true if about equal to roundoff of the underlying type
366 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
368 T tol = 100*SGLimits<T>::epsilon();
369 return equivalent(v1, v2, tol, tol);
372 /// The euclidean distance of the two vectors
376 dist(const SGVec4<T>& v1, const SGVec4<T>& v2)
377 { return norm(v1 - v2); }
379 /// The squared euclidean distance of the two vectors
383 distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
384 { SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
386 // calculate the projection of u along the direction of d.
390 projection(const SGVec4<T>& u, const SGVec4<T>& d)
394 if (SGLimits<T>::min() < denom) return u;
395 else return d * (dot(u, d) / denom);
402 isNaN(const SGVec4<T>& v)
404 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
405 || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
409 /// Output to an ostream
410 template<typename char_type, typename traits_type, typename T>
412 std::basic_ostream<char_type, traits_type>&
413 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
414 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
418 toVec4f(const SGVec4d& v)
419 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
423 toVec4d(const SGVec4f& v)
424 { return SGVec4d(v(0), v(1), v(2), v(3)); }