1 // Copyright (C) 2006-2009 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.
28 // Avoid "_data not initialized" warnings (see comment below).
29 # pragma GCC diagnostic ignored "-Wuninitialized"
32 /// Default constructor. Does not initialize at all.
33 /// If you need them zero initialized, use SGVec3::zeros()
36 /// Initialize with nans in the debug build, that will guarantee to have
37 /// a fast uninitialized default constructor in the release but shows up
38 /// uninitialized values in the debug build very fast ...
40 for (unsigned i = 0; i < 3; ++i)
41 data()[i] = SGLimits<T>::quiet_NaN();
46 // Restore warning settings.
47 # pragma GCC diagnostic warning "-Wuninitialized"
50 /// Constructor. Initialize by the given values
52 { data()[0] = x; data()[1] = y; data()[2] = z; }
53 /// Constructor. Initialize by the content of a plain array,
54 /// make sure it has at least 3 elements
55 explicit SGVec3(const T* d)
56 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
58 explicit SGVec3(const SGVec3<S>& d)
59 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
60 explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
61 { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
63 /// Access by index, the index is unchecked
64 const T& operator()(unsigned i) const
66 /// Access by index, the index is unchecked
67 T& operator()(unsigned i)
70 /// Access raw data by index, the index is unchecked
71 const T& operator[](unsigned i) const
73 /// Access raw data by index, the index is unchecked
74 T& operator[](unsigned i)
77 /// Access the x component
78 const T& x(void) const
80 /// Access the x component
83 /// Access the y component
84 const T& y(void) const
86 /// Access the y component
89 /// Access the z component
90 const T& z(void) const
92 /// Access the z component
96 /// Readonly raw storage interface
97 const T (&data(void) const)[3]
99 /// Readonly raw storage interface
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); }
129 /// Constructor. Initialize by a geodetic coordinate
130 /// Note that this conversion is relatively expensive to compute
131 static SGVec3 fromGeod(const SGGeod& geod);
132 /// Constructor. Initialize by a geocentric coordinate
133 /// Note that this conversion is relatively expensive to compute
134 static SGVec3 fromGeoc(const SGGeoc& geoc);
143 SGVec3<double>::fromGeod(const SGGeod& geod)
146 SGGeodesy::SGGeodToCart(geod, cart);
153 SGVec3<float>::fromGeod(const SGGeod& geod)
156 SGGeodesy::SGGeodToCart(geod, cart);
157 return SGVec3<float>(cart(0), cart(1), cart(2));
163 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
166 SGGeodesy::SGGeocToCart(geoc, cart);
173 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
176 SGGeodesy::SGGeocToCart(geoc, cart);
177 return SGVec3<float>(cart(0), cart(1), cart(2));
180 /// Unary +, do nothing ...
184 operator+(const SGVec3<T>& v)
187 /// Unary -, do nearly nothing
191 operator-(const SGVec3<T>& v)
192 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
198 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
199 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
205 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
206 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
208 /// Scalar multiplication
209 template<typename S, typename T>
212 operator*(S s, const SGVec3<T>& v)
213 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
215 /// Scalar multiplication
216 template<typename S, typename T>
219 operator*(const SGVec3<T>& v, S s)
220 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
222 /// multiplication as a multiplicator, that is assume that the first vector
223 /// represents a 3x3 diagonal matrix with the diagonal elements in the vector.
224 /// Then the result is the product of that matrix times the second vector.
228 mult(const SGVec3<T>& v1, const SGVec3<T>& v2)
229 { return SGVec3<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2)); }
231 /// component wise min
235 min(const SGVec3<T>& v1, const SGVec3<T>& v2)
237 return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
238 SGMisc<T>::min(v1(1), v2(1)),
239 SGMisc<T>::min(v1(2), v2(2)));
241 template<typename S, typename T>
244 min(const SGVec3<T>& v, S s)
246 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
247 SGMisc<T>::min(s, v(1)),
248 SGMisc<T>::min(s, v(2)));
250 template<typename S, typename T>
253 min(S s, const SGVec3<T>& v)
255 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
256 SGMisc<T>::min(s, v(1)),
257 SGMisc<T>::min(s, v(2)));
260 /// component wise max
264 max(const SGVec3<T>& v1, const SGVec3<T>& v2)
266 return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
267 SGMisc<T>::max(v1(1), v2(1)),
268 SGMisc<T>::max(v1(2), v2(2)));
270 template<typename S, typename T>
273 max(const SGVec3<T>& v, S s)
275 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
276 SGMisc<T>::max(s, v(1)),
277 SGMisc<T>::max(s, v(2)));
279 template<typename S, typename T>
282 max(S s, const SGVec3<T>& v)
284 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
285 SGMisc<T>::max(s, v(1)),
286 SGMisc<T>::max(s, v(2)));
289 /// Scalar dot product
293 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
294 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
296 /// The euclidean norm of the vector, that is what most people call length
300 norm(const SGVec3<T>& v)
301 { return sqrt(dot(v, v)); }
303 /// The euclidean norm of the vector, that is what most people call length
307 length(const SGVec3<T>& v)
308 { return sqrt(dot(v, v)); }
310 /// The 1-norm of the vector, this one is the fastest length function we
311 /// can implement on modern cpu's
315 norm1(const SGVec3<T>& v)
316 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
318 /// The inf-norm of the vector
322 normI(const SGVec3<T>& v)
323 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
325 /// Vector cross product
329 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
331 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
332 v1(2)*v2(0) - v1(0)*v2(2),
333 v1(0)*v2(1) - v1(1)*v2(0));
336 /// return any normalized vector perpendicular to v
340 perpendicular(const SGVec3<T>& v)
342 T absv1 = fabs(v(0));
343 T absv2 = fabs(v(1));
344 T absv3 = fabs(v(2));
346 if (absv2 < absv1 && absv3 < absv1) {
348 return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
349 } else if (absv3 < absv2) {
351 return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
352 } else if (SGLimits<T>::min() < absv3) {
354 return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
356 // the all zero case ...
357 return SGVec3<T>(0, 0, 0);
361 /// Construct a unit vector in the given direction.
362 /// or the zero vector if the input vector is zero.
366 normalize(const SGVec3<T>& v)
369 if (normv <= SGLimits<T>::min())
370 return SGVec3<T>::zeros();
374 /// Return true if exactly the same
378 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
379 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
381 /// Return true if not exactly the same
385 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
386 { return ! (v1 == v2); }
388 /// Return true if smaller, good for putting that into a std::map
392 operator<(const SGVec3<T>& v1, const SGVec3<T>& v2)
394 if (v1(0) < v2(0)) return true;
395 else if (v2(0) < v1(0)) return false;
396 else if (v1(1) < v2(1)) return true;
397 else if (v2(1) < v1(1)) return false;
398 else return (v1(2) < v2(2));
404 operator<=(const SGVec3<T>& v1, const SGVec3<T>& v2)
406 if (v1(0) < v2(0)) return true;
407 else if (v2(0) < v1(0)) return false;
408 else if (v1(1) < v2(1)) return true;
409 else if (v2(1) < v1(1)) return false;
410 else return (v1(2) <= v2(2));
416 operator>(const SGVec3<T>& v1, const SGVec3<T>& v2)
417 { return operator<(v2, v1); }
422 operator>=(const SGVec3<T>& v1, const SGVec3<T>& v2)
423 { return operator<=(v2, v1); }
425 /// Return true if equal to the relative tolerance tol
429 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
430 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
432 /// Return true if equal to the relative tolerance tol
436 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
437 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
439 /// Return true if about equal to roundoff of the underlying type
443 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
445 T tol = 100*SGLimits<T>::epsilon();
446 return equivalent(v1, v2, tol, tol);
449 /// The euclidean distance of the two vectors
453 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
454 { return norm(v1 - v2); }
456 /// The squared euclidean distance of the two vectors
460 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
461 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
463 // calculate the projection of u along the direction of d.
467 projection(const SGVec3<T>& u, const SGVec3<T>& d)
471 if (SGLimits<T>::min() < denom) return u;
472 else return d * (dot(u, d) / denom);
479 isNaN(const SGVec3<T>& v)
481 return SGMisc<T>::isNaN(v(0)) ||
482 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
486 /// Output to an ostream
487 template<typename char_type, typename traits_type, typename T>
489 std::basic_ostream<char_type, traits_type>&
490 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
491 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
495 toVec3f(const SGVec3d& v)
496 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
500 toVec3d(const SGVec3f& v)
501 { return SGVec3d(v(0), v(1), v(2)); }