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.
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]; }
48 /// Access by index, the index is unchecked
49 const T& operator()(unsigned i) const
51 /// Access by index, the index is unchecked
52 T& operator()(unsigned i)
55 /// Access raw data by index, the index is unchecked
56 const T& operator[](unsigned i) const
58 /// Access raw data by index, the index is unchecked
59 T& operator[](unsigned i)
62 /// Access the x component
63 const T& x(void) const
65 /// Access the x component
68 /// Access the y component
69 const T& y(void) const
71 /// Access the y component
74 /// Access the z component
75 const T& z(void) const
77 /// Access the z component
80 /// Access the x component
81 const T& w(void) const
83 /// Access the x component
88 /// Get the data pointer, usefull for interfacing with plib's sg*Vec
89 const T* data(void) const
91 /// Get the data pointer, usefull for interfacing with plib's sg*Vec
95 /// Readonly interface function to ssg's sgVec3/sgdVec3
96 const T (&sg(void) const)[4]
98 /// Interface function to ssg's sgVec3/sgdVec3
103 SGVec4& operator+=(const SGVec4& v)
104 { _data[0]+=v(0);_data[1]+=v(1);_data[2]+=v(2);_data[3]+=v(3);return *this; }
105 /// Inplace subtraction
106 SGVec4& operator-=(const SGVec4& v)
107 { _data[0]-=v(0);_data[1]-=v(1);_data[2]-=v(2);_data[3]-=v(3);return *this; }
108 /// Inplace scalar multiplication
110 SGVec4& operator*=(S s)
111 { _data[0] *= s; _data[1] *= s; _data[2] *= s; _data[3] *= s; return *this; }
112 /// Inplace scalar multiplication by 1/s
114 SGVec4& operator/=(S s)
115 { return operator*=(1/T(s)); }
117 /// Return an all zero vector
118 static SGVec4 zeros(void)
119 { return SGVec4(0, 0, 0, 0); }
120 /// Return unit vectors
121 static SGVec4 e1(void)
122 { return SGVec4(1, 0, 0, 0); }
123 static SGVec4 e2(void)
124 { return SGVec4(0, 1, 0, 0); }
125 static SGVec4 e3(void)
126 { return SGVec4(0, 0, 1, 0); }
127 static SGVec4 e4(void)
128 { 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 /// Scalar dot product
181 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
182 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
184 /// The euclidean norm of the vector, that is what most people call length
188 norm(const SGVec4<T>& v)
189 { return sqrt(dot(v, v)); }
191 /// The euclidean norm of the vector, that is what most people call length
195 length(const SGVec4<T>& v)
196 { return sqrt(dot(v, v)); }
198 /// The 1-norm of the vector, this one is the fastest length function we
199 /// can implement on modern cpu's
203 norm1(const SGVec4<T>& v)
204 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
206 /// The euclidean norm of the vector, that is what most people call length
210 normalize(const SGVec4<T>& v)
211 { return (1/norm(v))*v; }
213 /// Return true if exactly the same
217 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
218 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
220 /// Return true if not exactly the same
224 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
225 { return ! (v1 == v2); }
227 /// Return true if equal to the relative tolerance tol
231 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
232 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
234 /// Return true if equal to the relative tolerance tol
238 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
239 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
241 /// Return true if about equal to roundoff of the underlying type
245 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
247 T tol = 100*SGLimits<T>::epsilon();
248 return equivalent(v1, v2, tol, tol);
251 /// The euclidean distance of the two vectors
255 dist(const SGVec4<T>& v1, const SGVec4<T>& v2)
256 { return norm(v1 - v2); }
258 /// The squared euclidean distance of the two vectors
262 distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
263 { SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
269 isNaN(const SGVec4<T>& v)
271 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
272 || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
276 /// Output to an ostream
277 template<typename char_type, typename traits_type, typename T>
279 std::basic_ostream<char_type, traits_type>&
280 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
281 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
283 /// Two classes doing actually the same on different types
284 typedef SGVec4<float> SGVec4f;
285 typedef SGVec4<double> SGVec4d;
289 toVec4f(const SGVec4d& v)
290 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
294 toVec4d(const SGVec4f& v)
295 { return SGVec4d(v(0), v(1), v(2), v(3)); }