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.
23 #include <simgear/math/SGVec2.hxx>
24 #include <simgear/math/SGGeodesy.hxx>
33 // Avoid "_data not initialized" warnings (see comment below).
34 # pragma GCC diagnostic ignored "-Wuninitialized"
37 /// Default constructor. Does not initialize at all.
38 /// If you need them zero initialized, use SGVec3::zeros()
41 /// Initialize with nans in the debug build, that will guarantee to have
42 /// a fast uninitialized default constructor in the release but shows up
43 /// uninitialized values in the debug build very fast ...
45 for (unsigned i = 0; i < 3; ++i)
46 data()[i] = SGLimits<T>::quiet_NaN();
51 // Restore warning settings.
52 # pragma GCC diagnostic warning "-Wuninitialized"
55 /// Constructor. Initialize by the given values
57 { data()[0] = x; data()[1] = y; data()[2] = z; }
58 /// Constructor. Initialize by the content of a plain array,
59 /// make sure it has at least 3 elements
60 explicit SGVec3(const T* d)
61 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
63 explicit SGVec3(const SGVec3<S>& d)
64 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
65 explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
66 { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
68 /// Access by index, the index is unchecked
69 const T& operator()(unsigned i) const
71 /// Access by index, the index is unchecked
72 T& operator()(unsigned i)
75 /// Access raw data by index, the index is unchecked
76 const T& operator[](unsigned i) const
78 /// Access raw data by index, the index is unchecked
79 T& operator[](unsigned i)
82 /// Access the x component
83 const T& x(void) const
85 /// Access the x component
88 /// Access the y component
89 const T& y(void) const
91 /// Access the y component
94 /// Access the z component
95 const T& z(void) const
97 /// Access the z component
101 /// Readonly raw storage interface
102 const T (&data(void) const)[3]
104 /// Readonly raw storage interface
109 SGVec3& operator+=(const SGVec3& v)
110 { data()[0] += v(0); data()[1] += v(1); data()[2] += v(2); return *this; }
111 /// Inplace subtraction
112 SGVec3& operator-=(const SGVec3& v)
113 { data()[0] -= v(0); data()[1] -= v(1); data()[2] -= v(2); return *this; }
114 /// Inplace scalar multiplication
116 SGVec3& operator*=(S s)
117 { data()[0] *= s; data()[1] *= s; data()[2] *= s; return *this; }
118 /// Inplace scalar multiplication by 1/s
120 SGVec3& operator/=(S s)
121 { return operator*=(1/T(s)); }
123 /// Return an all zero vector
124 static SGVec3 zeros(void)
125 { return SGVec3(0, 0, 0); }
126 /// Return unit vectors
127 static SGVec3 e1(void)
128 { return SGVec3(1, 0, 0); }
129 static SGVec3 e2(void)
130 { return SGVec3(0, 1, 0); }
131 static SGVec3 e3(void)
132 { return SGVec3(0, 0, 1); }
134 /// Constructor. Initialize by a geodetic coordinate
135 /// Note that this conversion is relatively expensive to compute
136 static SGVec3 fromGeod(const SGGeod& geod);
137 /// Constructor. Initialize by a geocentric coordinate
138 /// Note that this conversion is relatively expensive to compute
139 static SGVec3 fromGeoc(const SGGeoc& geoc);
148 SGVec3<double>::fromGeod(const SGGeod& geod)
151 SGGeodesy::SGGeodToCart(geod, cart);
158 SGVec3<float>::fromGeod(const SGGeod& geod)
161 SGGeodesy::SGGeodToCart(geod, cart);
162 return SGVec3<float>(cart(0), cart(1), cart(2));
168 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
171 SGGeodesy::SGGeocToCart(geoc, cart);
178 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
181 SGGeodesy::SGGeocToCart(geoc, cart);
182 return SGVec3<float>(cart(0), cart(1), cart(2));
185 /// Unary +, do nothing ...
189 operator+(const SGVec3<T>& v)
192 /// Unary -, do nearly nothing
196 operator-(const SGVec3<T>& v)
197 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
203 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
204 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
210 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
211 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
213 /// Scalar multiplication
214 template<typename S, typename T>
217 operator*(S s, const SGVec3<T>& v)
218 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
220 /// Scalar multiplication
221 template<typename S, typename T>
224 operator*(const SGVec3<T>& v, S s)
225 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
227 /// multiplication as a multiplicator, that is assume that the first vector
228 /// represents a 3x3 diagonal matrix with the diagonal elements in the vector.
229 /// Then the result is the product of that matrix times the second vector.
233 mult(const SGVec3<T>& v1, const SGVec3<T>& v2)
234 { return SGVec3<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2)); }
236 /// component wise min
240 min(const SGVec3<T>& v1, const SGVec3<T>& v2)
242 return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
243 SGMisc<T>::min(v1(1), v2(1)),
244 SGMisc<T>::min(v1(2), v2(2)));
246 template<typename S, typename T>
249 min(const SGVec3<T>& v, S s)
251 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
252 SGMisc<T>::min(s, v(1)),
253 SGMisc<T>::min(s, v(2)));
255 template<typename S, typename T>
258 min(S s, const SGVec3<T>& v)
260 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
261 SGMisc<T>::min(s, v(1)),
262 SGMisc<T>::min(s, v(2)));
265 /// component wise max
269 max(const SGVec3<T>& v1, const SGVec3<T>& v2)
271 return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
272 SGMisc<T>::max(v1(1), v2(1)),
273 SGMisc<T>::max(v1(2), v2(2)));
275 template<typename S, typename T>
278 max(const SGVec3<T>& v, S s)
280 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
281 SGMisc<T>::max(s, v(1)),
282 SGMisc<T>::max(s, v(2)));
284 template<typename S, typename T>
287 max(S s, const SGVec3<T>& v)
289 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
290 SGMisc<T>::max(s, v(1)),
291 SGMisc<T>::max(s, v(2)));
294 /// Add two vectors taking care of (integer) overflows. The values are limited
295 /// to the respective minimum and maximum values.
297 SGVec3<T> addClipOverflow(SGVec3<T> const& lhs, SGVec3<T> const& rhs)
300 SGMisc<T>::addClipOverflow(lhs.x(), rhs.x()),
301 SGMisc<T>::addClipOverflow(lhs.y(), rhs.y()),
302 SGMisc<T>::addClipOverflow(lhs.z(), rhs.z())
306 /// Scalar dot product
310 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
311 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
313 /// The euclidean norm of the vector, that is what most people call length
317 norm(const SGVec3<T>& v)
318 { return sqrt(dot(v, v)); }
320 /// The euclidean norm of the vector, that is what most people call length
324 length(const SGVec3<T>& v)
325 { return sqrt(dot(v, v)); }
327 /// The 1-norm of the vector, this one is the fastest length function we
328 /// can implement on modern cpu's
332 norm1(const SGVec3<T>& v)
333 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
335 /// The inf-norm of the vector
339 normI(const SGVec3<T>& v)
340 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
342 /// Vector cross product
346 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
348 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
349 v1(2)*v2(0) - v1(0)*v2(2),
350 v1(0)*v2(1) - v1(1)*v2(0));
353 /// return any normalized vector perpendicular to v
357 perpendicular(const SGVec3<T>& v)
359 T absv1 = fabs(v(0));
360 T absv2 = fabs(v(1));
361 T absv3 = fabs(v(2));
363 if (absv2 < absv1 && absv3 < absv1) {
365 return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
366 } else if (absv3 < absv2) {
368 return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
369 } else if (SGLimits<T>::min() < absv3) {
371 return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
373 // the all zero case ...
374 return SGVec3<T>(0, 0, 0);
378 /// Construct a unit vector in the given direction.
379 /// or the zero vector if the input vector is zero.
383 normalize(const SGVec3<T>& v)
386 if (normv <= SGLimits<T>::min())
387 return SGVec3<T>::zeros();
391 /// Return true if exactly the same
395 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
396 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
398 /// Return true if not exactly the same
402 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
403 { return ! (v1 == v2); }
405 /// Return true if smaller, good for putting that into a std::map
409 operator<(const SGVec3<T>& v1, const SGVec3<T>& v2)
411 if (v1(0) < v2(0)) return true;
412 else if (v2(0) < v1(0)) return false;
413 else if (v1(1) < v2(1)) return true;
414 else if (v2(1) < v1(1)) return false;
415 else return (v1(2) < v2(2));
421 operator<=(const SGVec3<T>& v1, const SGVec3<T>& v2)
423 if (v1(0) < v2(0)) return true;
424 else if (v2(0) < v1(0)) return false;
425 else if (v1(1) < v2(1)) return true;
426 else if (v2(1) < v1(1)) return false;
427 else return (v1(2) <= v2(2));
433 operator>(const SGVec3<T>& v1, const SGVec3<T>& v2)
434 { return operator<(v2, v1); }
439 operator>=(const SGVec3<T>& v1, const SGVec3<T>& v2)
440 { return operator<=(v2, v1); }
442 /// Return true if equal to the relative tolerance tol
446 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
447 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
449 /// Return true if equal to the relative tolerance tol
453 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
454 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
456 /// Return true if about equal to roundoff of the underlying type
460 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
462 T tol = 100*SGLimits<T>::epsilon();
463 return equivalent(v1, v2, tol, tol);
466 /// The euclidean distance of the two vectors
470 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
471 { return norm(v1 - v2); }
473 /// The squared euclidean distance of the two vectors
477 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
478 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
480 // calculate the projection of u along the direction of d.
484 projection(const SGVec3<T>& u, const SGVec3<T>& d)
488 if (SGLimits<T>::min() < denom) return u;
489 else return d * (dot(u, d) / denom);
496 isNaN(const SGVec3<T>& v)
498 return SGMisc<T>::isNaN(v(0)) ||
499 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
503 /// Output to an ostream
504 template<typename char_type, typename traits_type, typename T>
506 std::basic_ostream<char_type, traits_type>&
507 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
508 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
512 toVec3f(const SGVec3d& v)
513 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
517 toVec3d(const SGVec3f& v)
518 { return SGVec3d(v(0), v(1), v(2)); }