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.
30 // Avoid "_data not initialized" warnings (see comment below).
31 # pragma GCC diagnostic ignored "-Wuninitialized"
34 /// Default constructor. Does not initialize at all.
35 /// If you need them zero initialized, use SGVec3::zeros()
38 /// Initialize with nans in the debug build, that will guarantee to have
39 /// a fast uninitialized default constructor in the release but shows up
40 /// uninitialized values in the debug build very fast ...
42 for (unsigned i = 0; i < 3; ++i)
43 data()[i] = SGLimits<T>::quiet_NaN();
48 // Restore warning settings.
49 # pragma GCC diagnostic warning "-Wuninitialized"
52 /// Constructor. Initialize by the given values
54 { data()[0] = x; data()[1] = y; data()[2] = z; }
55 /// Constructor. Initialize by the content of a plain array,
56 /// make sure it has at least 3 elements
57 explicit SGVec3(const T* d)
58 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
60 explicit SGVec3(const SGVec3<S>& d)
61 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
62 explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
63 { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
65 /// Access by index, the index is unchecked
66 const T& operator()(unsigned i) const
68 /// Access by index, the index is unchecked
69 T& operator()(unsigned i)
72 /// Access raw data by index, the index is unchecked
73 const T& operator[](unsigned i) const
75 /// Access raw data by index, the index is unchecked
76 T& operator[](unsigned i)
79 /// Access the x component
80 const T& x(void) const
82 /// Access the x component
85 /// Access the y component
86 const T& y(void) const
88 /// Access the y component
91 /// Access the z component
92 const T& z(void) const
94 /// Access the z component
98 /// Readonly raw storage interface
99 const T (&data(void) const)[3]
101 /// Readonly raw storage interface
106 SGVec3& operator+=(const SGVec3& v)
107 { data()[0] += v(0); data()[1] += v(1); data()[2] += v(2); return *this; }
108 /// Inplace subtraction
109 SGVec3& operator-=(const SGVec3& v)
110 { data()[0] -= v(0); data()[1] -= v(1); data()[2] -= v(2); return *this; }
111 /// Inplace scalar multiplication
113 SGVec3& operator*=(S s)
114 { data()[0] *= s; data()[1] *= s; data()[2] *= s; return *this; }
115 /// Inplace scalar multiplication by 1/s
117 SGVec3& operator/=(S s)
118 { return operator*=(1/T(s)); }
120 /// Return an all zero vector
121 static SGVec3 zeros(void)
122 { return SGVec3(0, 0, 0); }
123 /// Return unit vectors
124 static SGVec3 e1(void)
125 { return SGVec3(1, 0, 0); }
126 static SGVec3 e2(void)
127 { return SGVec3(0, 1, 0); }
128 static SGVec3 e3(void)
129 { return SGVec3(0, 0, 1); }
131 /// Constructor. Initialize by a geodetic coordinate
132 /// Note that this conversion is relatively expensive to compute
133 static SGVec3 fromGeod(const SGGeod& geod);
134 /// Constructor. Initialize by a geocentric coordinate
135 /// Note that this conversion is relatively expensive to compute
136 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 /// multiplication as a multiplicator, that is assume that the first vector
225 /// represents a 3x3 diagonal matrix with the diagonal elements in the vector.
226 /// Then the result is the product of that matrix times the second vector.
230 mult(const SGVec3<T>& v1, const SGVec3<T>& v2)
231 { return SGVec3<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2)); }
233 /// component wise min
237 min(const SGVec3<T>& v1, const SGVec3<T>& v2)
239 return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
240 SGMisc<T>::min(v1(1), v2(1)),
241 SGMisc<T>::min(v1(2), v2(2)));
243 template<typename S, typename T>
246 min(const SGVec3<T>& v, S s)
248 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
249 SGMisc<T>::min(s, v(1)),
250 SGMisc<T>::min(s, v(2)));
252 template<typename S, typename T>
255 min(S s, const SGVec3<T>& v)
257 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
258 SGMisc<T>::min(s, v(1)),
259 SGMisc<T>::min(s, v(2)));
262 /// component wise max
266 max(const SGVec3<T>& v1, const SGVec3<T>& v2)
268 return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
269 SGMisc<T>::max(v1(1), v2(1)),
270 SGMisc<T>::max(v1(2), v2(2)));
272 template<typename S, typename T>
275 max(const SGVec3<T>& v, S s)
277 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
278 SGMisc<T>::max(s, v(1)),
279 SGMisc<T>::max(s, v(2)));
281 template<typename S, typename T>
284 max(S s, const SGVec3<T>& v)
286 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
287 SGMisc<T>::max(s, v(1)),
288 SGMisc<T>::max(s, v(2)));
291 /// Scalar dot product
295 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
296 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
298 /// The euclidean norm of the vector, that is what most people call length
302 norm(const SGVec3<T>& v)
303 { return sqrt(dot(v, v)); }
305 /// The euclidean norm of the vector, that is what most people call length
309 length(const SGVec3<T>& v)
310 { return sqrt(dot(v, v)); }
312 /// The 1-norm of the vector, this one is the fastest length function we
313 /// can implement on modern cpu's
317 norm1(const SGVec3<T>& v)
318 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
320 /// The inf-norm of the vector
324 normI(const SGVec3<T>& v)
325 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
327 /// Vector cross product
331 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
333 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
334 v1(2)*v2(0) - v1(0)*v2(2),
335 v1(0)*v2(1) - v1(1)*v2(0));
338 /// return any normalized vector perpendicular to v
342 perpendicular(const SGVec3<T>& v)
344 T absv1 = fabs(v(0));
345 T absv2 = fabs(v(1));
346 T absv3 = fabs(v(2));
348 if (absv2 < absv1 && absv3 < absv1) {
350 return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
351 } else if (absv3 < absv2) {
353 return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
354 } else if (SGLimits<T>::min() < absv3) {
356 return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
358 // the all zero case ...
359 return SGVec3<T>(0, 0, 0);
363 /// Construct a unit vector in the given direction.
364 /// or the zero vector if the input vector is zero.
368 normalize(const SGVec3<T>& v)
371 if (normv <= SGLimits<T>::min())
372 return SGVec3<T>::zeros();
376 /// Return true if exactly the same
380 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
381 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
383 /// Return true if not exactly the same
387 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
388 { return ! (v1 == v2); }
390 /// Return true if smaller, good for putting that into a std::map
394 operator<(const SGVec3<T>& v1, const SGVec3<T>& v2)
396 if (v1(0) < v2(0)) return true;
397 else if (v2(0) < v1(0)) return false;
398 else if (v1(1) < v2(1)) return true;
399 else if (v2(1) < v1(1)) return false;
400 else return (v1(2) < v2(2));
406 operator<=(const SGVec3<T>& v1, const SGVec3<T>& v2)
408 if (v1(0) < v2(0)) return true;
409 else if (v2(0) < v1(0)) return false;
410 else if (v1(1) < v2(1)) return true;
411 else if (v2(1) < v1(1)) return false;
412 else return (v1(2) <= v2(2));
418 operator>(const SGVec3<T>& v1, const SGVec3<T>& v2)
419 { return operator<(v2, v1); }
424 operator>=(const SGVec3<T>& v1, const SGVec3<T>& v2)
425 { return operator<=(v2, v1); }
427 /// Return true if equal to the relative tolerance tol
431 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
432 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
434 /// Return true if equal to the relative tolerance tol
438 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
439 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
441 /// Return true if about equal to roundoff of the underlying type
445 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
447 T tol = 100*SGLimits<T>::epsilon();
448 return equivalent(v1, v2, tol, tol);
451 /// The euclidean distance of the two vectors
455 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
456 { return norm(v1 - v2); }
458 /// The squared euclidean distance of the two vectors
462 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
463 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
465 // calculate the projection of u along the direction of d.
469 projection(const SGVec3<T>& u, const SGVec3<T>& d)
473 if (SGLimits<T>::min() < denom) return u;
474 else return d * (dot(u, d) / denom);
481 isNaN(const SGVec3<T>& v)
483 return SGMisc<T>::isNaN(v(0)) ||
484 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
488 /// Output to an ostream
489 template<typename char_type, typename traits_type, typename T>
491 std::basic_ostream<char_type, traits_type>&
492 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
493 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
497 toVec3f(const SGVec3d& v)
498 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
502 toVec3d(const SGVec3f& v)
503 { return SGVec3d(v(0), v(1), v(2)); }