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 raw data by index, the index is unchecked
46 const T& operator[](unsigned i) const
48 /// Access raw data by index, the index is unchecked
49 T& operator[](unsigned i)
52 /// Access the x component
53 const T& x(void) const
55 /// Access the x component
58 /// Access the y component
59 const T& y(void) const
61 /// Access the y component
64 /// Access the z component
65 const T& z(void) const
67 /// Access the z component
71 /// Get the data pointer
72 const T* data(void) const
74 /// Get the data pointer
78 /// Readonly interface function to ssg's sgVec3/sgdVec3
79 const T (&sg(void) const)[3]
81 /// Interface function to ssg's sgVec3/sgdVec3
86 SGVec3& operator+=(const SGVec3& v)
87 { _data[0] += v(0); _data[1] += v(1); _data[2] += v(2); return *this; }
88 /// Inplace subtraction
89 SGVec3& operator-=(const SGVec3& v)
90 { _data[0] -= v(0); _data[1] -= v(1); _data[2] -= v(2); return *this; }
91 /// Inplace scalar multiplication
93 SGVec3& operator*=(S s)
94 { _data[0] *= s; _data[1] *= s; _data[2] *= s; return *this; }
95 /// Inplace scalar multiplication by 1/s
97 SGVec3& operator/=(S s)
98 { return operator*=(1/T(s)); }
100 /// Return an all zero vector
101 static SGVec3 zeros(void)
102 { return SGVec3(0, 0, 0); }
103 /// Return unit vectors
104 static SGVec3 e1(void)
105 { return SGVec3(1, 0, 0); }
106 static SGVec3 e2(void)
107 { return SGVec3(0, 1, 0); }
108 static SGVec3 e3(void)
109 { return SGVec3(0, 0, 1); }
116 /// Unary +, do nothing ...
120 operator+(const SGVec3<T>& v)
123 /// Unary -, do nearly nothing
127 operator-(const SGVec3<T>& v)
128 { return SGVec3<T>(-v(0), -v(1), -v(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)); }
141 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
142 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
144 /// Scalar multiplication
145 template<typename S, typename T>
148 operator*(S s, const SGVec3<T>& v)
149 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
151 /// Scalar multiplication
152 template<typename S, typename T>
155 operator*(const SGVec3<T>& v, S s)
156 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
158 /// Scalar dot product
162 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
163 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
165 /// The euclidean norm of the vector, that is what most people call length
169 norm(const SGVec3<T>& v)
170 { return sqrt(dot(v, v)); }
172 /// The euclidean norm of the vector, that is what most people call length
176 length(const SGVec3<T>& v)
177 { return sqrt(dot(v, v)); }
179 /// The 1-norm of the vector, this one is the fastest length function we
180 /// can implement on modern cpu's
184 norm1(const SGVec3<T>& v)
185 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
187 /// Vector cross product
191 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
193 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
194 v1(2)*v2(0) - v1(0)*v2(2),
195 v1(0)*v2(1) - v1(1)*v2(0));
198 /// The euclidean norm of the vector, that is what most people call length
202 normalize(const SGVec3<T>& v)
203 { return (1/norm(v))*v; }
205 /// Return true if exactly the same
209 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
210 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
212 /// Return true if not exactly the same
216 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
217 { return ! (v1 == v2); }
219 /// Return true if equal to the relative tolerance tol
223 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
224 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
226 /// Return true if equal to the relative tolerance tol
230 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
231 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
233 /// Return true if about equal to roundoff of the underlying type
237 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
239 T tol = 100*SGLimits<T>::epsilon();
240 return equivalent(v1, v2, tol, tol);
247 isNaN(const SGVec3<T>& v)
249 return SGMisc<T>::isNaN(v(0)) ||
250 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
254 /// Output to an ostream
255 template<typename char_type, typename traits_type, typename T>
257 std::basic_ostream<char_type, traits_type>&
258 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
259 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
261 /// Two classes doing actually the same on different types
262 typedef SGVec3<float> SGVec3f;
263 typedef SGVec3<double> SGVec3d;
267 toVec3f(const SGVec3d& v)
268 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
272 toVec3d(const SGVec3f& v)
273 { return SGVec3d(v(0), v(1), v(2)); }