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 the x component
39 const T& x(void) const
41 /// Access the x component
44 /// Access the y component
45 const T& y(void) const
47 /// Access the y component
50 /// Access the z component
51 const T& z(void) const
53 /// Access the z component
56 /// Access the x component
57 const T& w(void) const
59 /// Access the x component
64 /// Get the data pointer, usefull for interfacing with plib's sg*Vec
65 const T* data(void) const
67 /// Get the data pointer, usefull for interfacing with plib's sg*Vec
71 /// Readonly interface function to ssg's sgVec3/sgdVec3
72 const T (&sg(void) const)[4]
74 /// Interface function to ssg's sgVec3/sgdVec3
79 SGVec4& operator+=(const SGVec4& v)
80 { _data[0]+=v(0);_data[1]+=v(1);_data[2]+=v(2);_data[3]+=v(3);return *this; }
81 /// Inplace subtraction
82 SGVec4& operator-=(const SGVec4& v)
83 { _data[0]-=v(0);_data[1]-=v(1);_data[2]-=v(2);_data[3]-=v(3);return *this; }
84 /// Inplace scalar multiplication
86 SGVec4& operator*=(S s)
87 { _data[0] *= s; _data[1] *= s; _data[2] *= s; _data[3] *= s; return *this; }
88 /// Inplace scalar multiplication by 1/s
90 SGVec4& operator/=(S s)
91 { return operator*=(1/T(s)); }
93 /// Return an all zero vector
94 static SGVec4 zeros(void)
95 { return SGVec4(0, 0, 0, 0); }
96 /// Return unit vectors
97 static SGVec4 e1(void)
98 { return SGVec4(1, 0, 0, 0); }
99 static SGVec4 e2(void)
100 { return SGVec4(0, 1, 0, 0); }
101 static SGVec4 e3(void)
102 { return SGVec4(0, 0, 1, 0); }
103 static SGVec4 e4(void)
104 { return SGVec4(0, 0, 0, 1); }
111 /// Unary +, do nothing ...
115 operator+(const SGVec4<T>& v)
118 /// Unary -, do nearly nothing
122 operator-(const SGVec4<T>& v)
123 { return SGVec4<T>(-v(0), -v(1), -v(2), -v(3)); }
129 operator+(const SGVec4<T>& v1, const SGVec4<T>& v2)
130 { return SGVec4<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(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)); }
139 /// Scalar multiplication
140 template<typename S, typename T>
143 operator*(S s, const SGVec4<T>& v)
144 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
146 /// Scalar multiplication
147 template<typename S, typename T>
150 operator*(const SGVec4<T>& v, S s)
151 { return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
153 /// Scalar dot product
157 dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
158 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
160 /// The euclidean norm of the vector, that is what most people call length
164 norm(const SGVec4<T>& v)
165 { return sqrt(dot(v, v)); }
167 /// The euclidean norm of the vector, that is what most people call length
171 length(const SGVec4<T>& v)
172 { return sqrt(dot(v, v)); }
174 /// The 1-norm of the vector, this one is the fastest length function we
175 /// can implement on modern cpu's
179 norm1(const SGVec4<T>& v)
180 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
182 /// The euclidean norm of the vector, that is what most people call length
186 normalize(const SGVec4<T>& v)
187 { return (1/norm(v))*v; }
189 /// Return true if exactly the same
193 operator==(const SGVec4<T>& v1, const SGVec4<T>& v2)
194 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
196 /// Return true if not exactly the same
200 operator!=(const SGVec4<T>& v1, const SGVec4<T>& v2)
201 { return ! (v1 == v2); }
203 /// Return true if equal to the relative tolerance tol
207 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol, T atol)
208 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
210 /// Return true if equal to the relative tolerance tol
214 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2, T rtol)
215 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
217 /// Return true if about equal to roundoff of the underlying type
221 equivalent(const SGVec4<T>& v1, const SGVec4<T>& v2)
223 T tol = 100*SGLimits<T>::epsilon();
224 return equivalent(v1, v2, tol, tol);
231 isNaN(const SGVec4<T>& v)
233 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
234 || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
238 /// Output to an ostream
239 template<typename char_type, typename traits_type, typename T>
241 std::basic_ostream<char_type, traits_type>&
242 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
243 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
245 /// Two classes doing actually the same on different types
246 typedef SGVec4<float> SGVec4f;
247 typedef SGVec4<double> SGVec4d;
251 toVec4f(const SGVec4d& v)
252 { return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
256 toVec4d(const SGVec4f& v)
257 { return SGVec4d(v(0), v(1), v(2), v(3)); }