10 /// Default constructor. Does not initialize at all.
11 /// If you need them zero initialized, use SGVec4::zeros()
14 /// Initialize with nans in the debug build, that will guarantee to have
15 /// a fast uninitialized default constructor in the release but shows up
16 /// uninitialized values in the debug build very fast ...
18 for (unsigned i = 0; i < 4; ++i)
19 _data[i] = SGLimits<T>::quiet_NaN();
22 /// Constructor. Initialize by the given values
23 SGVec4(T x, T y, T z, T w)
24 { _data[0] = x; _data[1] = y; _data[2] = z; _data[3] = w; }
25 /// Constructor. Initialize by the content of a plain array,
26 /// make sure it has at least 3 elements
27 explicit SGVec4(const T* d)
28 { _data[0] = d[0]; _data[1] = d[1]; _data[2] = d[2]; _data[3] = d[3]; }
31 /// Access by index, the index is unchecked
32 const T& operator()(unsigned i) const
34 /// Access by index, the index is unchecked
35 T& operator()(unsigned i)
38 /// Access raw data by index, the index is unchecked
39 const T& operator[](unsigned i) const
41 /// Access raw data by index, the index is unchecked
42 T& operator[](unsigned i)
45 /// Access the x component
46 const T& x(void) const
48 /// Access the x component
51 /// Access the y component
52 const T& y(void) const
54 /// Access the y component
57 /// Access the z component
58 const T& z(void) const
60 /// Access the z component
63 /// Access the x component
64 const T& w(void) const
66 /// Access the x component
71 /// Get the data pointer, usefull for interfacing with plib's sg*Vec
72 const T* data(void) const
74 /// Get the data pointer, usefull for interfacing with plib's sg*Vec
78 /// Readonly interface function to ssg's sgVec3/sgdVec3
79 const T (&sg(void) const)[4]
81 /// Interface function to ssg's sgVec3/sgdVec3
86 SGVec4& operator+=(const SGVec4& v)
87 { _data[0]+=v(0);_data[1]+=v(1);_data[2]+=v(2);_data[3]+=v(3);return *this; }
88 /// Inplace subtraction
89 SGVec4& operator-=(const SGVec4& v)
90 { _data[0]-=v(0);_data[1]-=v(1);_data[2]-=v(2);_data[3]-=v(3);return *this; }
91 /// Inplace scalar multiplication
93 SGVec4& operator*=(S s)
94 { _data[0] *= s; _data[1] *= s; _data[2] *= s; _data[3] *= s; return *this; }
95 /// Inplace scalar multiplication by 1/s
97 SGVec4& operator/=(S s)
98 { return operator*=(1/T(s)); }
100 /// Return an all zero vector
101 static SGVec4 zeros(void)
102 { return SGVec4(0, 0, 0, 0); }
103 /// Return unit vectors
104 static SGVec4 e1(void)
105 { return SGVec4(1, 0, 0, 0); }
106 static SGVec4 e2(void)
107 { return SGVec4(0, 1, 0, 0); }
108 static SGVec4 e3(void)
109 { return SGVec4(0, 0, 1, 0); }
110 static SGVec4 e4(void)
111 { return SGVec4(0, 0, 0, 1); }
118 /// Unary +, do nothing ...
122 operator+(const SGVec4<T>& v)
125 /// Unary -, do nearly nothing
129 operator-(const SGVec4<T>& v)
130 { return SGVec4<T>(-v(0), -v(1), -v(2), -v(3)); }
136 operator+(const SGVec4<T>& v1, const SGVec4<T>& v2)
137 { return SGVec4<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
143 operator-(const SGVec4<T>& v1, const SGVec4<T>& v2)
144 { return SGVec4<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
146 /// Scalar multiplication
147 template<typename S, typename T>
150 operator*(S s, const SGVec4<T>& v)
151 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
153 /// Scalar multiplication
154 template<typename S, typename T>
157 operator*(const SGVec4<T>& v, S s)
158 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
160 /// Scalar dot product
164 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
165 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
167 /// The euclidean norm of the vector, that is what most people call length
171 norm(const SGVec4<T>& v)
172 { return sqrt(dot(v, v)); }
174 /// The euclidean norm of the vector, that is what most people call length
178 length(const SGVec4<T>& v)
179 { return sqrt(dot(v, v)); }
181 /// The 1-norm of the vector, this one is the fastest length function we
182 /// can implement on modern cpu's
186 norm1(const SGVec4<T>& v)
187 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
189 /// The euclidean norm of the vector, that is what most people call length
193 normalize(const SGVec4<T>& v)
194 { return (1/norm(v))*v; }
196 /// Return true if exactly the same
200 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
201 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
203 /// Return true if not exactly the same
207 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
208 { return ! (v1 == v2); }
210 /// Return true if equal to the relative tolerance tol
214 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
215 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
217 /// Return true if equal to the relative tolerance tol
221 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
222 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
224 /// Return true if about equal to roundoff of the underlying type
228 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
230 T tol = 100*SGLimits<T>::epsilon();
231 return equivalent(v1, v2, tol, tol);
238 isNaN(const SGVec4<T>& v)
240 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
241 || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
245 /// Output to an ostream
246 template<typename char_type, typename traits_type, typename T>
248 std::basic_ostream<char_type, traits_type>&
249 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
250 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
252 /// Two classes doing actually the same on different types
253 typedef SGVec4<float> SGVec4f;
254 typedef SGVec4<double> SGVec4d;
258 toVec4f(const SGVec4d& v)
259 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
263 toVec4d(const SGVec4f& v)
264 { return SGVec4d(v(0), v(1), v(2), v(3)); }