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.
29 /// Default constructor. Does not initialize at all.
30 /// If you need them zero initialized, use SGVec4::zeros()
33 /// Initialize with nans in the debug build, that will guarantee to have
34 /// a fast uninitialized default constructor in the release but shows up
35 /// uninitialized values in the debug build very fast ...
37 for (unsigned i = 0; i < 4; ++i)
38 data()[i] = SGLimits<T>::quiet_NaN();
41 /// Constructor. Initialize by the given values
42 SGVec4(T x, T y, T z, T w)
43 { data()[0] = x; data()[1] = y; data()[2] = z; data()[3] = w; }
44 /// Constructor. Initialize by the content of a plain array,
45 /// make sure it has at least 3 elements
46 explicit SGVec4(const T* d)
47 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
49 explicit SGVec4(const SGVec4<S>& d)
50 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
51 explicit SGVec4(const SGVec3<T>& v3, const T& v4 = 0)
52 { data()[0] = v3[0]; data()[1] = v3[1]; data()[2] = v3[2]; data()[3] = v4; }
54 /// Access by index, the index is unchecked
55 const T& operator()(unsigned i) const
57 /// Access by index, the index is unchecked
58 T& operator()(unsigned i)
61 /// Access raw data by index, the index is unchecked
62 const T& operator[](unsigned i) const
64 /// Access raw data by index, the index is unchecked
65 T& operator[](unsigned i)
68 /// Access the x component
69 const T& x(void) const
71 /// Access the x component
74 /// Access the y component
75 const T& y(void) const
77 /// Access the y component
80 /// Access the z component
81 const T& z(void) const
83 /// Access the z component
86 /// Access the x component
87 const T& w(void) const
89 /// Access the x component
93 /// Readonly raw storage interface
94 const T (&data(void) const)[4]
96 /// Readonly raw storage interface
101 SGVec4& operator+=(const SGVec4& v)
102 { data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; }
103 /// Inplace subtraction
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 scalar multiplication
108 SGVec4& operator*=(S s)
109 { data()[0] *= s; data()[1] *= s; data()[2] *= s; data()[3] *= s; return *this; }
110 /// Inplace scalar multiplication by 1/s
112 SGVec4& operator/=(S s)
113 { return operator*=(1/T(s)); }
115 /// Return an all zero vector
116 static SGVec4 zeros(void)
117 { return SGVec4(0, 0, 0, 0); }
118 /// Return unit vectors
119 static SGVec4 e1(void)
120 { return SGVec4(1, 0, 0, 0); }
121 static SGVec4 e2(void)
122 { return SGVec4(0, 1, 0, 0); }
123 static SGVec4 e3(void)
124 { return SGVec4(0, 0, 1, 0); }
125 static SGVec4 e4(void)
126 { return SGVec4(0, 0, 0, 1); }
132 /// Unary +, do nothing ...
136 operator+(const SGVec4<T>& v)
139 /// Unary -, do nearly nothing
143 operator-(const SGVec4<T>& v)
144 { return SGVec4<T>(-v(0), -v(1), -v(2), -v(3)); }
150 operator+(const SGVec4<T>& v1, const SGVec4<T>& v2)
151 { return SGVec4<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
157 operator-(const SGVec4<T>& v1, const SGVec4<T>& v2)
158 { return SGVec4<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
160 /// Scalar multiplication
161 template<typename S, typename T>
164 operator*(S s, const SGVec4<T>& v)
165 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
167 /// Scalar multiplication
168 template<typename S, typename T>
171 operator*(const SGVec4<T>& v, S s)
172 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
174 /// multiplication as a multiplicator, that is assume that the first vector
175 /// represents a 4x4 diagonal matrix with the diagonal elements in the vector.
176 /// Then the result is the product of that matrix times the second vector.
180 mult(const SGVec4<T>& v1, const SGVec4<T>& v2)
181 { return SGVec4<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2), v1(3)*v2(3)); }
183 /// component wise min
187 min(const SGVec4<T>& v1, const SGVec4<T>& v2)
189 return SGVec4<T>(SGMisc<T>::min(v1(0), v2(0)),
190 SGMisc<T>::min(v1(1), v2(1)),
191 SGMisc<T>::min(v1(2), v2(2)),
192 SGMisc<T>::min(v1(3), v2(3)));
194 template<typename S, typename T>
197 min(const SGVec4<T>& v, S s)
199 return SGVec4<T>(SGMisc<T>::min(s, v(0)),
200 SGMisc<T>::min(s, v(1)),
201 SGMisc<T>::min(s, v(2)),
202 SGMisc<T>::min(s, v(3)));
204 template<typename S, typename T>
207 min(S s, const SGVec4<T>& v)
209 return SGVec4<T>(SGMisc<T>::min(s, v(0)),
210 SGMisc<T>::min(s, v(1)),
211 SGMisc<T>::min(s, v(2)),
212 SGMisc<T>::min(s, v(3)));
215 /// component wise max
219 max(const SGVec4<T>& v1, const SGVec4<T>& v2)
221 return SGVec4<T>(SGMisc<T>::max(v1(0), v2(0)),
222 SGMisc<T>::max(v1(1), v2(1)),
223 SGMisc<T>::max(v1(2), v2(2)),
224 SGMisc<T>::max(v1(3), v2(3)));
226 template<typename S, typename T>
229 max(const SGVec4<T>& v, S s)
231 return SGVec4<T>(SGMisc<T>::max(s, v(0)),
232 SGMisc<T>::max(s, v(1)),
233 SGMisc<T>::max(s, v(2)),
234 SGMisc<T>::max(s, v(3)));
236 template<typename S, typename T>
239 max(S s, const SGVec4<T>& v)
241 return SGVec4<T>(SGMisc<T>::max(s, v(0)),
242 SGMisc<T>::max(s, v(1)),
243 SGMisc<T>::max(s, v(2)),
244 SGMisc<T>::max(s, v(3)));
247 /// Scalar dot product
251 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
252 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
254 /// The euclidean norm of the vector, that is what most people call length
258 norm(const SGVec4<T>& v)
259 { return sqrt(dot(v, v)); }
261 /// The euclidean norm of the vector, that is what most people call length
265 length(const SGVec4<T>& v)
266 { return sqrt(dot(v, v)); }
268 /// The 1-norm of the vector, this one is the fastest length function we
269 /// can implement on modern cpu's
273 norm1(const SGVec4<T>& v)
274 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
276 /// The inf-norm of the vector
280 normI(const SGVec4<T>& v)
281 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2)), fabs(v(2))); }
283 /// The euclidean norm of the vector, that is what most people call length
287 normalize(const SGVec4<T>& v)
290 if (normv <= SGLimits<T>::min())
291 return SGVec4<T>::zeros();
295 /// Return true if exactly the same
299 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
300 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
302 /// Return true if not exactly the same
306 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
307 { return ! (v1 == v2); }
309 /// Return true if smaller, good for putting that into a std::map
313 operator<(const SGVec4<T>& v1, const SGVec4<T>& v2)
315 if (v1(0) < v2(0)) return true;
316 else if (v2(0) < v1(0)) return false;
317 else if (v1(1) < v2(1)) return true;
318 else if (v2(1) < v1(1)) return false;
319 else if (v1(2) < v2(2)) return true;
320 else if (v2(2) < v1(2)) return false;
321 else return (v1(3) < v2(3));
327 operator<=(const SGVec4<T>& v1, const SGVec4<T>& v2)
329 if (v1(0) < v2(0)) return true;
330 else if (v2(0) < v1(0)) return false;
331 else if (v1(1) < v2(1)) return true;
332 else if (v2(1) < v1(1)) return false;
333 else if (v1(2) < v2(2)) return true;
334 else if (v2(2) < v1(2)) return false;
335 else return (v1(3) <= v2(3));
341 operator>(const SGVec4<T>& v1, const SGVec4<T>& v2)
342 { return operator<(v2, v1); }
347 operator>=(const SGVec4<T>& v1, const SGVec4<T>& v2)
348 { return operator<=(v2, v1); }
350 /// Return true if equal to the relative tolerance tol
354 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
355 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
357 /// Return true if equal to the relative tolerance tol
361 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
362 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
364 /// Return true if about equal to roundoff of the underlying type
368 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
370 T tol = 100*SGLimits<T>::epsilon();
371 return equivalent(v1, v2, tol, tol);
374 /// The euclidean distance of the two vectors
378 dist(const SGVec4<T>& v1, const SGVec4<T>& v2)
379 { return norm(v1 - v2); }
381 /// The squared euclidean distance of the two vectors
385 distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
386 { SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
388 // calculate the projection of u along the direction of d.
392 projection(const SGVec4<T>& u, const SGVec4<T>& d)
396 if (SGLimits<T>::min() < denom) return u;
397 else return d * (dot(u, d) / denom);
404 isNaN(const SGVec4<T>& v)
406 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
407 || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
411 /// Output to an ostream
412 template<typename char_type, typename traits_type, typename T>
414 std::basic_ostream<char_type, traits_type>&
415 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
416 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
420 toVec4f(const SGVec4d& v)
421 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
425 toVec4d(const SGVec4f& v)
426 { return SGVec4d(v(0), v(1), v(2), v(3)); }