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]; }
46 /// Constructor. Initialize by a geodetic coordinate
47 /// Note that this conversion is relatively expensive to compute
49 SGVec3(const SGGeod& geod)
50 { SGGeodesy::SGGeodToCart(geod, *this); }
51 /// Constructor. Initialize by a geocentric coordinate
52 /// Note that this conversion is relatively expensive to compute
54 SGVec3(const SGGeoc& geoc)
55 { SGGeodesy::SGGeocToCart(geoc, *this); }
57 /// Access by index, the index is unchecked
58 const T& operator()(unsigned i) const
60 /// Access by index, the index is unchecked
61 T& operator()(unsigned i)
64 /// Access raw data by index, the index is unchecked
65 const T& operator[](unsigned i) const
67 /// Access raw data by index, the index is unchecked
68 T& operator[](unsigned i)
71 /// Access the x component
72 const T& x(void) const
74 /// Access the x component
77 /// Access the y component
78 const T& y(void) const
80 /// Access the y component
83 /// Access the z component
84 const T& z(void) const
86 /// Access the z component
90 /// Get the data pointer
91 const T* data(void) const
93 /// Get the data pointer
97 /// Readonly interface function to ssg's sgVec3/sgdVec3
98 const T (&sg(void) const)[3]
100 /// Interface function to ssg's sgVec3/sgdVec3
105 SGVec3& operator+=(const SGVec3& v)
106 { _data[0] += v(0); _data[1] += v(1); _data[2] += v(2); return *this; }
107 /// Inplace subtraction
108 SGVec3& operator-=(const SGVec3& v)
109 { _data[0] -= v(0); _data[1] -= v(1); _data[2] -= v(2); return *this; }
110 /// Inplace scalar multiplication
112 SGVec3& operator*=(S s)
113 { _data[0] *= s; _data[1] *= s; _data[2] *= s; return *this; }
114 /// Inplace scalar multiplication by 1/s
116 SGVec3& operator/=(S s)
117 { return operator*=(1/T(s)); }
119 /// Return an all zero vector
120 static SGVec3 zeros(void)
121 { return SGVec3(0, 0, 0); }
122 /// Return unit vectors
123 static SGVec3 e1(void)
124 { return SGVec3(1, 0, 0); }
125 static SGVec3 e2(void)
126 { return SGVec3(0, 1, 0); }
127 static SGVec3 e3(void)
128 { return SGVec3(0, 0, 1); }
130 /// Constructor. Initialize by a geodetic coordinate
131 /// Note that this conversion is relatively expensive to compute
132 static SGVec3 fromGeod(const SGGeod& geod);
133 /// Constructor. Initialize by a geocentric coordinate
134 /// Note that this conversion is relatively expensive to compute
135 static SGVec3 fromGeoc(const SGGeoc& geoc);
145 SGVec3<double>::fromGeod(const SGGeod& geod)
148 SGGeodesy::SGGeodToCart(geod, cart);
155 SGVec3<float>::fromGeod(const SGGeod& geod)
158 SGGeodesy::SGGeodToCart(geod, cart);
159 return SGVec3<float>(cart(0), cart(1), cart(2));
165 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
168 SGGeodesy::SGGeocToCart(geoc, cart);
175 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
178 SGGeodesy::SGGeocToCart(geoc, cart);
179 return SGVec3<float>(cart(0), cart(1), cart(2));
182 /// Unary +, do nothing ...
186 operator+(const SGVec3<T>& v)
189 /// Unary -, do nearly nothing
193 operator-(const SGVec3<T>& v)
194 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
200 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
201 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
207 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
208 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
210 /// Scalar multiplication
211 template<typename S, typename T>
214 operator*(S s, const SGVec3<T>& v)
215 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
217 /// Scalar multiplication
218 template<typename S, typename T>
221 operator*(const SGVec3<T>& v, S s)
222 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
224 /// Scalar dot product
228 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
229 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
231 /// The euclidean norm of the vector, that is what most people call length
235 norm(const SGVec3<T>& v)
236 { return sqrt(dot(v, v)); }
238 /// The euclidean norm of the vector, that is what most people call length
242 length(const SGVec3<T>& v)
243 { return sqrt(dot(v, v)); }
245 /// The 1-norm of the vector, this one is the fastest length function we
246 /// can implement on modern cpu's
250 norm1(const SGVec3<T>& v)
251 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
253 /// Vector cross product
257 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
259 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
260 v1(2)*v2(0) - v1(0)*v2(2),
261 v1(0)*v2(1) - v1(1)*v2(0));
264 /// The euclidean norm of the vector, that is what most people call length
268 normalize(const SGVec3<T>& v)
269 { return (1/norm(v))*v; }
271 /// Return true if exactly the same
275 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
276 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
278 /// Return true if not exactly the same
282 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
283 { return ! (v1 == v2); }
285 /// Return true if equal to the relative tolerance tol
289 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
290 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
292 /// Return true if equal to the relative tolerance tol
296 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
297 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
299 /// Return true if about equal to roundoff of the underlying type
303 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
305 T tol = 100*SGLimits<T>::epsilon();
306 return equivalent(v1, v2, tol, tol);
313 isNaN(const SGVec3<T>& v)
315 return SGMisc<T>::isNaN(v(0)) ||
316 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
320 /// Output to an ostream
321 template<typename char_type, typename traits_type, typename T>
323 std::basic_ostream<char_type, traits_type>&
324 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
325 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
327 /// Two classes doing actually the same on different types
328 typedef SGVec3<float> SGVec3f;
329 typedef SGVec3<double> SGVec3d;
333 toVec3f(const SGVec3d& v)
334 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
338 toVec3d(const SGVec3f& v)
339 { return SGVec3d(v(0), v(1), v(2)); }