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 SGVec3::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 < 3; ++i)
36 _data[i] = SGLimits<T>::quiet_NaN();
39 /// Constructor. Initialize by the given values
41 { _data[0] = x; _data[1] = y; _data[2] = z; }
42 /// Constructor. Initialize by the content of a plain array,
43 /// make sure it has at least 3 elements
44 explicit SGVec3(const T* data)
45 { _data[0] = data[0]; _data[1] = data[1]; _data[2] = data[2]; }
47 /// Access by index, the index is unchecked
48 const T& operator()(unsigned i) const
50 /// Access by index, the index is unchecked
51 T& operator()(unsigned i)
54 /// Access raw data by index, the index is unchecked
55 const T& operator[](unsigned i) const
57 /// Access raw data by index, the index is unchecked
58 T& operator[](unsigned i)
61 /// Access the x component
62 const T& x(void) const
64 /// Access the x component
67 /// Access the y component
68 const T& y(void) const
70 /// Access the y component
73 /// Access the z component
74 const T& z(void) const
76 /// Access the z component
80 /// Get the data pointer
81 const T* data(void) const
83 /// Get the data pointer
87 /// Readonly interface function to ssg's sgVec3/sgdVec3
88 const T (&sg(void) const)[3]
90 /// Interface function to ssg's sgVec3/sgdVec3
95 SGVec3& operator+=(const SGVec3& v)
96 { _data[0] += v(0); _data[1] += v(1); _data[2] += v(2); return *this; }
97 /// Inplace subtraction
98 SGVec3& operator-=(const SGVec3& v)
99 { _data[0] -= v(0); _data[1] -= v(1); _data[2] -= v(2); return *this; }
100 /// Inplace scalar multiplication
102 SGVec3& operator*=(S s)
103 { _data[0] *= s; _data[1] *= s; _data[2] *= s; return *this; }
104 /// Inplace scalar multiplication by 1/s
106 SGVec3& operator/=(S s)
107 { return operator*=(1/T(s)); }
109 /// Return an all zero vector
110 static SGVec3 zeros(void)
111 { return SGVec3(0, 0, 0); }
112 /// Return unit vectors
113 static SGVec3 e1(void)
114 { return SGVec3(1, 0, 0); }
115 static SGVec3 e2(void)
116 { return SGVec3(0, 1, 0); }
117 static SGVec3 e3(void)
118 { return SGVec3(0, 0, 1); }
120 /// Constructor. Initialize by a geodetic coordinate
121 /// Note that this conversion is relatively expensive to compute
122 static SGVec3 fromGeod(const SGGeod& geod);
123 /// Constructor. Initialize by a geocentric coordinate
124 /// Note that this conversion is relatively expensive to compute
125 static SGVec3 fromGeoc(const SGGeoc& geoc);
135 SGVec3<double>::fromGeod(const SGGeod& geod)
138 SGGeodesy::SGGeodToCart(geod, cart);
145 SGVec3<float>::fromGeod(const SGGeod& geod)
148 SGGeodesy::SGGeodToCart(geod, cart);
149 return SGVec3<float>(cart(0), cart(1), cart(2));
155 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
158 SGGeodesy::SGGeocToCart(geoc, cart);
165 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
168 SGGeodesy::SGGeocToCart(geoc, cart);
169 return SGVec3<float>(cart(0), cart(1), cart(2));
172 /// Unary +, do nothing ...
176 operator+(const SGVec3<T>& v)
179 /// Unary -, do nearly nothing
183 operator-(const SGVec3<T>& v)
184 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
190 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
191 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
197 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
198 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
200 /// Scalar multiplication
201 template<typename S, typename T>
204 operator*(S s, const SGVec3<T>& v)
205 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
207 /// Scalar multiplication
208 template<typename S, typename T>
211 operator*(const SGVec3<T>& v, S s)
212 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
214 /// Scalar dot product
218 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
219 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
221 /// The euclidean norm of the vector, that is what most people call length
225 norm(const SGVec3<T>& v)
226 { return sqrt(dot(v, v)); }
228 /// The euclidean norm of the vector, that is what most people call length
232 length(const SGVec3<T>& v)
233 { return sqrt(dot(v, v)); }
235 /// The 1-norm of the vector, this one is the fastest length function we
236 /// can implement on modern cpu's
240 norm1(const SGVec3<T>& v)
241 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
243 /// Vector cross product
247 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
249 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
250 v1(2)*v2(0) - v1(0)*v2(2),
251 v1(0)*v2(1) - v1(1)*v2(0));
254 /// The euclidean norm of the vector, that is what most people call length
258 normalize(const SGVec3<T>& v)
259 { return (1/norm(v))*v; }
261 /// Return true if exactly the same
265 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
266 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
268 /// Return true if not exactly the same
272 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
273 { return ! (v1 == v2); }
275 /// Return true if equal to the relative tolerance tol
279 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
280 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
282 /// Return true if equal to the relative tolerance tol
286 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
287 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
289 /// Return true if about equal to roundoff of the underlying type
293 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
295 T tol = 100*SGLimits<T>::epsilon();
296 return equivalent(v1, v2, tol, tol);
299 /// The euclidean distance of the two vectors
303 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
304 { return norm(v1 - v2); }
306 /// The squared euclidean distance of the two vectors
310 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
311 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
317 isNaN(const SGVec3<T>& v)
319 return SGMisc<T>::isNaN(v(0)) ||
320 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
324 /// Output to an ostream
325 template<typename char_type, typename traits_type, typename T>
327 std::basic_ostream<char_type, traits_type>&
328 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
329 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
331 /// Two classes doing actually the same on different types
332 typedef SGVec3<float> SGVec3f;
333 typedef SGVec3<double> SGVec3d;
337 toVec3f(const SGVec3d& v)
338 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
342 toVec3d(const SGVec3f& v)
343 { return SGVec3d(v(0), v(1), v(2)); }