10 /// Default constructor. Does not initialize at all.
11 /// If you need them zero initialized, use SGVec3::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 < 3; ++i)
19 _data[i] = SGLimits<T>::quiet_NaN();
22 /// Constructor. Initialize by the given values
24 { _data[0] = x; _data[1] = y; _data[2] = z; }
25 /// Constructor. Initialize by the content of a plain array,
26 /// make sure it has at least 3 elements
27 explicit SGVec3(const T* data)
28 { _data[0] = data[0]; _data[1] = data[1]; _data[2] = data[2]; }
29 /// Constructor. Initialize by a geodetic coordinate
30 /// Note that this conversion is relatively expensive to compute
31 SGVec3(const SGGeod& geod)
32 { SGGeodesy::SGGeodToCart(geod, *this); }
33 /// Constructor. Initialize by a geocentric coordinate
34 /// Note that this conversion is relatively expensive to compute
35 SGVec3(const SGGeoc& geoc)
36 { SGGeodesy::SGGeocToCart(geoc, *this); }
38 /// Access by index, the index is unchecked
39 const T& operator()(unsigned i) const
41 /// Access 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
64 /// Get the data pointer
65 const T* data(void) const
67 /// Get the data pointer
71 /// Readonly interface function to ssg's sgVec3/sgdVec3
72 const T (&sg(void) const)[3]
74 /// Interface function to ssg's sgVec3/sgdVec3
79 SGVec3& operator+=(const SGVec3& v)
80 { _data[0] += v(0); _data[1] += v(1); _data[2] += v(2); return *this; }
81 /// Inplace subtraction
82 SGVec3& operator-=(const SGVec3& v)
83 { _data[0] -= v(0); _data[1] -= v(1); _data[2] -= v(2); return *this; }
84 /// Inplace scalar multiplication
86 SGVec3& operator*=(S s)
87 { _data[0] *= s; _data[1] *= s; _data[2] *= s; return *this; }
88 /// Inplace scalar multiplication by 1/s
90 SGVec3& operator/=(S s)
91 { return operator*=(1/T(s)); }
93 /// Return an all zero vector
94 static SGVec3 zeros(void)
95 { return SGVec3(0, 0, 0); }
96 /// Return unit vectors
97 static SGVec3 e1(void)
98 { return SGVec3(1, 0, 0); }
99 static SGVec3 e2(void)
100 { return SGVec3(0, 1, 0); }
101 static SGVec3 e3(void)
102 { return SGVec3(0, 0, 1); }
109 /// Unary +, do nothing ...
113 operator+(const SGVec3<T>& v)
116 /// Unary -, do nearly nothing
120 operator-(const SGVec3<T>& v)
121 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
127 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
128 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
134 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
135 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
137 /// Scalar multiplication
138 template<typename S, typename T>
141 operator*(S s, const SGVec3<T>& v)
142 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
144 /// Scalar multiplication
145 template<typename S, typename T>
148 operator*(const SGVec3<T>& v, S s)
149 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
151 /// Scalar dot product
155 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
156 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
158 /// The euclidean norm of the vector, that is what most people call length
162 norm(const SGVec3<T>& v)
163 { return sqrt(dot(v, v)); }
165 /// The euclidean norm of the vector, that is what most people call length
169 length(const SGVec3<T>& v)
170 { return sqrt(dot(v, v)); }
172 /// The 1-norm of the vector, this one is the fastest length function we
173 /// can implement on modern cpu's
177 norm1(const SGVec3<T>& v)
178 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
180 /// Vector cross product
184 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
186 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
187 v1(2)*v2(0) - v1(0)*v2(2),
188 v1(0)*v2(1) - v1(1)*v2(0));
191 /// The euclidean norm of the vector, that is what most people call length
195 normalize(const SGVec3<T>& v)
196 { return (1/norm(v))*v; }
198 /// Return true if exactly the same
202 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
203 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
205 /// Return true if not exactly the same
209 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
210 { return ! (v1 == v2); }
212 /// Return true if equal to the relative tolerance tol
216 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
217 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
219 /// Return true if equal to the relative tolerance tol
223 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
224 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
226 /// Return true if about equal to roundoff of the underlying type
230 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
232 T tol = 100*SGLimits<T>::epsilon();
233 return equivalent(v1, v2, tol, tol);
240 isNaN(const SGVec3<T>& v)
242 return SGMisc<T>::isNaN(v(0)) ||
243 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
247 /// Output to an ostream
248 template<typename char_type, typename traits_type, typename T>
250 std::basic_ostream<char_type, traits_type>&
251 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
252 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
254 /// Two classes doing actually the same on different types
255 typedef SGVec3<float> SGVec3f;
256 typedef SGVec3<double> SGVec3d;
260 toVec3f(const SGVec3d& v)
261 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
265 toVec3d(const SGVec3f& v)
266 { return SGVec3d(v(0), v(1), v(2)); }