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.
21 #ifndef NO_OPENSCENEGRAPH_INTERFACE
33 // Avoid "_data not initialized" warnings (see comment below).
34 # pragma GCC diagnostic push
35 # pragma GCC diagnostic ignored "-Wuninitialized"
38 /// Default constructor. Does not initialize at all.
39 /// If you need them zero initialized, use SGVec3::zeros()
42 /// Initialize with nans in the debug build, that will guarantee to have
43 /// a fast uninitialized default constructor in the release but shows up
44 /// uninitialized values in the debug build very fast ...
46 for (unsigned i = 0; i < 3; ++i)
47 data()[i] = SGLimits<T>::quiet_NaN();
52 // Restore warning settings.
53 # pragma GCC diagnostic pop
56 /// Constructor. Initialize by the given values
58 { data()[0] = x; data()[1] = y; data()[2] = z; }
59 /// Constructor. Initialize by the content of a plain array,
60 /// make sure it has at least 3 elements
61 explicit SGVec3(const T* d)
62 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
64 explicit SGVec3(const SGVec3<S>& d)
65 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
66 explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
67 { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
69 /// Access by index, the index is unchecked
70 const T& operator()(unsigned i) const
72 /// Access by index, the index is unchecked
73 T& operator()(unsigned i)
76 /// Access raw data by index, the index is unchecked
77 const T& operator[](unsigned i) const
79 /// Access raw data by index, the index is unchecked
80 T& operator[](unsigned i)
83 /// Access the x component
84 const T& x(void) const
86 /// Access the x component
89 /// Access the y component
90 const T& y(void) const
92 /// Access the y component
95 /// Access the z component
96 const T& z(void) const
98 /// Access the z component
100 { return data()[2]; }
102 /// Readonly raw storage interface
103 const T (&data(void) const)[3]
105 /// Readonly raw storage interface
110 SGVec3& operator+=(const SGVec3& v)
111 { data()[0] += v(0); data()[1] += v(1); data()[2] += v(2); return *this; }
112 /// Inplace subtraction
113 SGVec3& operator-=(const SGVec3& v)
114 { data()[0] -= v(0); data()[1] -= v(1); data()[2] -= v(2); return *this; }
115 /// Inplace scalar multiplication
117 SGVec3& operator*=(S s)
118 { data()[0] *= s; data()[1] *= s; data()[2] *= s; return *this; }
119 /// Inplace scalar multiplication by 1/s
121 SGVec3& operator/=(S s)
122 { return operator*=(1/T(s)); }
124 /// Return an all zero vector
125 static SGVec3 zeros(void)
126 { return SGVec3(0, 0, 0); }
127 /// Return unit vectors
128 static SGVec3 e1(void)
129 { return SGVec3(1, 0, 0); }
130 static SGVec3 e2(void)
131 { return SGVec3(0, 1, 0); }
132 static SGVec3 e3(void)
133 { return SGVec3(0, 0, 1); }
135 /// Constructor. Initialize by a geodetic coordinate
136 /// Note that this conversion is relatively expensive to compute
137 static SGVec3 fromGeod(const SGGeod& geod);
138 /// Constructor. Initialize by a geocentric coordinate
139 /// Note that this conversion is relatively expensive to compute
140 static SGVec3 fromGeoc(const SGGeoc& geoc);
149 SGVec3<double>::fromGeod(const SGGeod& geod)
152 SGGeodesy::SGGeodToCart(geod, cart);
159 SGVec3<float>::fromGeod(const SGGeod& geod)
162 SGGeodesy::SGGeodToCart(geod, cart);
163 return SGVec3<float>(cart(0), cart(1), cart(2));
169 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
172 SGGeodesy::SGGeocToCart(geoc, cart);
179 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
182 SGGeodesy::SGGeocToCart(geoc, cart);
183 return SGVec3<float>(cart(0), cart(1), cart(2));
186 /// Unary +, do nothing ...
190 operator+(const SGVec3<T>& v)
193 /// Unary -, do nearly nothing
197 operator-(const SGVec3<T>& v)
198 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
204 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
205 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
211 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
212 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
214 /// Scalar multiplication
215 template<typename S, typename T>
218 operator*(S s, const SGVec3<T>& v)
219 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
221 /// Scalar multiplication
222 template<typename S, typename T>
225 operator*(const SGVec3<T>& v, S s)
226 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
228 /// multiplication as a multiplicator, that is assume that the first vector
229 /// represents a 3x3 diagonal matrix with the diagonal elements in the vector.
230 /// Then the result is the product of that matrix times the second vector.
234 mult(const SGVec3<T>& v1, const SGVec3<T>& v2)
235 { return SGVec3<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2)); }
237 /// component wise min
241 min(const SGVec3<T>& v1, const SGVec3<T>& v2)
243 return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
244 SGMisc<T>::min(v1(1), v2(1)),
245 SGMisc<T>::min(v1(2), v2(2)));
247 template<typename S, typename T>
250 min(const SGVec3<T>& v, S s)
252 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
253 SGMisc<T>::min(s, v(1)),
254 SGMisc<T>::min(s, v(2)));
256 template<typename S, typename T>
259 min(S s, const SGVec3<T>& v)
261 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
262 SGMisc<T>::min(s, v(1)),
263 SGMisc<T>::min(s, v(2)));
266 /// component wise max
270 max(const SGVec3<T>& v1, const SGVec3<T>& v2)
272 return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
273 SGMisc<T>::max(v1(1), v2(1)),
274 SGMisc<T>::max(v1(2), v2(2)));
276 template<typename S, typename T>
279 max(const SGVec3<T>& v, S s)
281 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
282 SGMisc<T>::max(s, v(1)),
283 SGMisc<T>::max(s, v(2)));
285 template<typename S, typename T>
288 max(S s, const SGVec3<T>& v)
290 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
291 SGMisc<T>::max(s, v(1)),
292 SGMisc<T>::max(s, v(2)));
295 /// Scalar dot product
299 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
300 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
302 /// The euclidean norm of the vector, that is what most people call length
306 norm(const SGVec3<T>& v)
307 { return sqrt(dot(v, v)); }
309 /// The euclidean norm of the vector, that is what most people call length
313 length(const SGVec3<T>& v)
314 { return sqrt(dot(v, v)); }
316 /// The 1-norm of the vector, this one is the fastest length function we
317 /// can implement on modern cpu's
321 norm1(const SGVec3<T>& v)
322 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
324 /// The inf-norm of the vector
328 normI(const SGVec3<T>& v)
329 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
331 /// Vector cross product
335 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
337 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
338 v1(2)*v2(0) - v1(0)*v2(2),
339 v1(0)*v2(1) - v1(1)*v2(0));
342 /// return any normalized vector perpendicular to v
346 perpendicular(const SGVec3<T>& v)
348 T absv1 = fabs(v(0));
349 T absv2 = fabs(v(1));
350 T absv3 = fabs(v(2));
352 if (absv2 < absv1 && absv3 < absv1) {
354 return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
355 } else if (absv3 < absv2) {
357 return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
358 } else if (SGLimits<T>::min() < absv3) {
360 return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
362 // the all zero case ...
363 return SGVec3<T>(0, 0, 0);
367 /// Construct a unit vector in the given direction.
368 /// or the zero vector if the input vector is zero.
372 normalize(const SGVec3<T>& v)
375 if (normv <= SGLimits<T>::min())
376 return SGVec3<T>::zeros();
380 /// Return true if exactly the same
384 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
385 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
387 /// Return true if not exactly the same
391 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
392 { return ! (v1 == v2); }
394 /// Return true if smaller, good for putting that into a std::map
398 operator<(const SGVec3<T>& v1, const SGVec3<T>& v2)
400 if (v1(0) < v2(0)) return true;
401 else if (v2(0) < v1(0)) return false;
402 else if (v1(1) < v2(1)) return true;
403 else if (v2(1) < v1(1)) return false;
404 else return (v1(2) < v2(2));
410 operator<=(const SGVec3<T>& v1, const SGVec3<T>& v2)
412 if (v1(0) < v2(0)) return true;
413 else if (v2(0) < v1(0)) return false;
414 else if (v1(1) < v2(1)) return true;
415 else if (v2(1) < v1(1)) return false;
416 else return (v1(2) <= v2(2));
422 operator>(const SGVec3<T>& v1, const SGVec3<T>& v2)
423 { return operator<(v2, v1); }
428 operator>=(const SGVec3<T>& v1, const SGVec3<T>& v2)
429 { return operator<=(v2, v1); }
431 /// Return true if equal to the relative tolerance tol
435 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
436 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
438 /// Return true if equal to the relative tolerance tol
442 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
443 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
445 /// Return true if about equal to roundoff of the underlying type
449 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
451 T tol = 100*SGLimits<T>::epsilon();
452 return equivalent(v1, v2, tol, tol);
455 /// The euclidean distance of the two vectors
459 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
460 { return norm(v1 - v2); }
462 /// The squared euclidean distance of the two vectors
466 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
467 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
469 // calculate the projection of u along the direction of d.
473 projection(const SGVec3<T>& u, const SGVec3<T>& d)
477 if (SGLimits<T>::min() < denom) return u;
478 else return d * (dot(u, d) / denom);
485 isNaN(const SGVec3<T>& v)
487 return SGMisc<T>::isNaN(v(0)) ||
488 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
492 /// Output to an ostream
493 template<typename char_type, typename traits_type, typename T>
495 std::basic_ostream<char_type, traits_type>&
496 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
497 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
501 toVec3f(const SGVec3d& v)
502 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
506 toVec3d(const SGVec3f& v)
507 { return SGVec3d(v(0), v(1), v(2)); }
509 #ifndef NO_OPENSCENEGRAPH_INTERFACE
512 toSG(const osg::Vec3d& v)
513 { return SGVec3d(v[0], v[1], v[2]); }
517 toSG(const osg::Vec3f& v)
518 { return SGVec3f(v[0], v[1], v[2]); }
522 toOsg(const SGVec3d& v)
523 { return osg::Vec3d(v[0], v[1], v[2]); }
527 toOsg(const SGVec3f& v)
528 { return osg::Vec3f(v[0], v[1], v[2]); }