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 Library General Public
14 // License along with this library; if not, write to the
15 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 // Boston, MA 02111-1307, USA.
28 /// Default constructor. Does not initialize at all.
29 /// If you need them zero initialized, use SGVec3::zeros()
32 /// Initialize with nans in the debug build, that will guarantee to have
33 /// a fast uninitialized default constructor in the release but shows up
34 /// uninitialized values in the debug build very fast ...
36 for (unsigned i = 0; i < 3; ++i)
37 _data[i] = SGLimits<T>::quiet_NaN();
40 /// Constructor. Initialize by the given values
42 { _data[0] = x; _data[1] = y; _data[2] = z; }
43 /// Constructor. Initialize by the content of a plain array,
44 /// make sure it has at least 3 elements
45 explicit SGVec3(const T* data)
46 { _data[0] = data[0]; _data[1] = data[1]; _data[2] = data[2]; }
47 /// Constructor. Initialize by a geodetic coordinate
48 /// 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
53 SGVec3(const SGGeoc& geoc)
54 { SGGeodesy::SGGeocToCart(geoc, *this); }
56 /// Access by index, the index is unchecked
57 const T& operator()(unsigned i) const
59 /// Access by index, the index is unchecked
60 T& operator()(unsigned i)
63 /// Access raw data by index, the index is unchecked
64 const T& operator[](unsigned i) const
66 /// Access raw data by index, the index is unchecked
67 T& operator[](unsigned i)
70 /// Access the x component
71 const T& x(void) const
73 /// Access the x component
76 /// Access the y component
77 const T& y(void) const
79 /// Access the y component
82 /// Access the z component
83 const T& z(void) const
85 /// Access the z component
89 /// Get the data pointer
90 const T* data(void) const
92 /// Get the data pointer
96 /// Readonly interface function to ssg's sgVec3/sgdVec3
97 const T (&sg(void) const)[3]
99 /// Interface function to ssg's sgVec3/sgdVec3
104 SGVec3& operator+=(const SGVec3& v)
105 { _data[0] += v(0); _data[1] += v(1); _data[2] += v(2); return *this; }
106 /// Inplace subtraction
107 SGVec3& operator-=(const SGVec3& v)
108 { _data[0] -= v(0); _data[1] -= v(1); _data[2] -= v(2); return *this; }
109 /// Inplace scalar multiplication
111 SGVec3& operator*=(S s)
112 { _data[0] *= s; _data[1] *= s; _data[2] *= s; return *this; }
113 /// Inplace scalar multiplication by 1/s
115 SGVec3& operator/=(S s)
116 { return operator*=(1/T(s)); }
118 /// Return an all zero vector
119 static SGVec3 zeros(void)
120 { return SGVec3(0, 0, 0); }
121 /// Return unit vectors
122 static SGVec3 e1(void)
123 { return SGVec3(1, 0, 0); }
124 static SGVec3 e2(void)
125 { return SGVec3(0, 1, 0); }
126 static SGVec3 e3(void)
127 { return SGVec3(0, 0, 1); }
134 /// Unary +, do nothing ...
138 operator+(const SGVec3<T>& v)
141 /// Unary -, do nearly nothing
145 operator-(const SGVec3<T>& v)
146 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
152 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
153 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
159 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
160 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
162 /// Scalar multiplication
163 template<typename S, typename T>
166 operator*(S s, const SGVec3<T>& v)
167 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
169 /// Scalar multiplication
170 template<typename S, typename T>
173 operator*(const SGVec3<T>& v, S s)
174 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
176 /// Scalar dot product
180 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
181 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
183 /// The euclidean norm of the vector, that is what most people call length
187 norm(const SGVec3<T>& v)
188 { return sqrt(dot(v, v)); }
190 /// The euclidean norm of the vector, that is what most people call length
194 length(const SGVec3<T>& v)
195 { return sqrt(dot(v, v)); }
197 /// The 1-norm of the vector, this one is the fastest length function we
198 /// can implement on modern cpu's
202 norm1(const SGVec3<T>& v)
203 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
205 /// Vector cross product
209 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
211 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
212 v1(2)*v2(0) - v1(0)*v2(2),
213 v1(0)*v2(1) - v1(1)*v2(0));
216 /// The euclidean norm of the vector, that is what most people call length
220 normalize(const SGVec3<T>& v)
221 { return (1/norm(v))*v; }
223 /// Return true if exactly the same
227 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
228 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
230 /// Return true if not exactly the same
234 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
235 { return ! (v1 == v2); }
237 /// Return true if equal to the relative tolerance tol
241 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
242 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
244 /// Return true if equal to the relative tolerance tol
248 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
249 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
251 /// Return true if about equal to roundoff of the underlying type
255 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
257 T tol = 100*SGLimits<T>::epsilon();
258 return equivalent(v1, v2, tol, tol);
265 isNaN(const SGVec3<T>& v)
267 return SGMisc<T>::isNaN(v(0)) ||
268 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
272 /// Output to an ostream
273 template<typename char_type, typename traits_type, typename T>
275 std::basic_ostream<char_type, traits_type>&
276 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
277 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
279 /// Two classes doing actually the same on different types
280 typedef SGVec3<float> SGVec3f;
281 typedef SGVec3<double> SGVec3d;
285 toVec3f(const SGVec3d& v)
286 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
290 toVec3d(const SGVec3f& v)
291 { return SGVec3d(v(0), v(1), v(2)); }