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
48 SGVec3(const SGGeod& geod)
49 { SGGeodesy::SGGeodToCart(geod, *this); }
50 /// Constructor. Initialize by a geocentric coordinate
51 /// Note that this conversion is relatively expensive to compute
52 SGVec3(const SGGeoc& geoc)
53 { SGGeodesy::SGGeocToCart(geoc, *this); }
55 /// Access by index, the index is unchecked
56 const T& operator()(unsigned i) const
58 /// Access by index, the index is unchecked
59 T& operator()(unsigned i)
62 /// Access raw data by index, the index is unchecked
63 const T& operator[](unsigned i) const
65 /// Access raw data by index, the index is unchecked
66 T& operator[](unsigned i)
69 /// Access the x component
70 const T& x(void) const
72 /// Access the x component
75 /// Access the y component
76 const T& y(void) const
78 /// Access the y component
81 /// Access the z component
82 const T& z(void) const
84 /// Access the z component
88 /// Get the data pointer
89 const T* data(void) const
91 /// Get the data pointer
95 /// Readonly interface function to ssg's sgVec3/sgdVec3
96 const T (&sg(void) const)[3]
98 /// Interface function to ssg's sgVec3/sgdVec3
103 SGVec3& operator+=(const SGVec3& v)
104 { _data[0] += v(0); _data[1] += v(1); _data[2] += v(2); return *this; }
105 /// Inplace subtraction
106 SGVec3& operator-=(const SGVec3& v)
107 { _data[0] -= v(0); _data[1] -= v(1); _data[2] -= v(2); return *this; }
108 /// Inplace scalar multiplication
110 SGVec3& operator*=(S s)
111 { _data[0] *= s; _data[1] *= s; _data[2] *= s; return *this; }
112 /// Inplace scalar multiplication by 1/s
114 SGVec3& operator/=(S s)
115 { return operator*=(1/T(s)); }
117 /// Return an all zero vector
118 static SGVec3 zeros(void)
119 { return SGVec3(0, 0, 0); }
120 /// Return unit vectors
121 static SGVec3 e1(void)
122 { return SGVec3(1, 0, 0); }
123 static SGVec3 e2(void)
124 { return SGVec3(0, 1, 0); }
125 static SGVec3 e3(void)
126 { return SGVec3(0, 0, 1); }
133 /// Unary +, do nothing ...
137 operator+(const SGVec3<T>& v)
140 /// Unary -, do nearly nothing
144 operator-(const SGVec3<T>& v)
145 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
151 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
152 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
158 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
159 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
161 /// Scalar multiplication
162 template<typename S, typename T>
165 operator*(S s, const SGVec3<T>& v)
166 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
168 /// Scalar multiplication
169 template<typename S, typename T>
172 operator*(const SGVec3<T>& v, S s)
173 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
175 /// Scalar dot product
179 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
180 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
182 /// The euclidean norm of the vector, that is what most people call length
186 norm(const SGVec3<T>& v)
187 { return sqrt(dot(v, v)); }
189 /// The euclidean norm of the vector, that is what most people call length
193 length(const SGVec3<T>& v)
194 { return sqrt(dot(v, v)); }
196 /// The 1-norm of the vector, this one is the fastest length function we
197 /// can implement on modern cpu's
201 norm1(const SGVec3<T>& v)
202 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
204 /// Vector cross product
208 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
210 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
211 v1(2)*v2(0) - v1(0)*v2(2),
212 v1(0)*v2(1) - v1(1)*v2(0));
215 /// The euclidean norm of the vector, that is what most people call length
219 normalize(const SGVec3<T>& v)
220 { return (1/norm(v))*v; }
222 /// Return true if exactly the same
226 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
227 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
229 /// Return true if not exactly the same
233 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
234 { return ! (v1 == v2); }
236 /// Return true if equal to the relative tolerance tol
240 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
241 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
243 /// Return true if equal to the relative tolerance tol
247 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
248 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
250 /// Return true if about equal to roundoff of the underlying type
254 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
256 T tol = 100*SGLimits<T>::epsilon();
257 return equivalent(v1, v2, tol, tol);
264 isNaN(const SGVec3<T>& v)
266 return SGMisc<T>::isNaN(v(0)) ||
267 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
271 /// Output to an ostream
272 template<typename char_type, typename traits_type, typename T>
274 std::basic_ostream<char_type, traits_type>&
275 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
276 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
278 /// Two classes doing actually the same on different types
279 typedef SGVec3<float> SGVec3f;
280 typedef SGVec3<double> SGVec3d;
284 toVec3f(const SGVec3d& v)
285 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
289 toVec3d(const SGVec3f& v)
290 { return SGVec3d(v(0), v(1), v(2)); }