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
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();
44 /// Constructor. Initialize by the given values
46 { data()[0] = x; data()[1] = y; data()[2] = z; }
47 /// Constructor. Initialize by the content of a plain array,
48 /// make sure it has at least 3 elements
49 explicit SGVec3(const T* d)
50 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
52 explicit SGVec3(const SGVec3<S>& d)
53 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
54 explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
55 { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
57 /// Access by index, the index is unchecked
58 const T& operator()(unsigned i) const
60 /// Access by index, the index is unchecked
61 T& operator()(unsigned i)
64 /// Access raw data by index, the index is unchecked
65 const T& operator[](unsigned i) const
67 /// Access raw data by index, the index is unchecked
68 T& operator[](unsigned i)
71 /// Access the x component
72 const T& x(void) const
74 /// Access the x component
77 /// Access the y component
78 const T& y(void) const
80 /// Access the y component
83 /// Access the z component
84 const T& z(void) const
86 /// Access the z component
90 /// Readonly raw storage interface
91 const T (&data(void) const)[3]
93 /// Readonly raw storage interface
98 SGVec3& operator+=(const SGVec3& v)
99 { data()[0] += v(0); data()[1] += v(1); data()[2] += v(2); return *this; }
100 /// Inplace subtraction
101 SGVec3& operator-=(const SGVec3& v)
102 { data()[0] -= v(0); data()[1] -= v(1); data()[2] -= v(2); return *this; }
103 /// Inplace scalar multiplication
105 SGVec3& operator*=(S s)
106 { data()[0] *= s; data()[1] *= s; data()[2] *= s; return *this; }
107 /// Inplace scalar multiplication by 1/s
109 SGVec3& operator/=(S s)
110 { return operator*=(1/T(s)); }
112 /// Return an all zero vector
113 static SGVec3 zeros(void)
114 { return SGVec3(0, 0, 0); }
115 /// Return unit vectors
116 static SGVec3 e1(void)
117 { return SGVec3(1, 0, 0); }
118 static SGVec3 e2(void)
119 { return SGVec3(0, 1, 0); }
120 static SGVec3 e3(void)
121 { return SGVec3(0, 0, 1); }
123 /// Constructor. Initialize by a geodetic coordinate
124 /// Note that this conversion is relatively expensive to compute
125 static SGVec3 fromGeod(const SGGeod& geod);
126 /// Constructor. Initialize by a geocentric coordinate
127 /// Note that this conversion is relatively expensive to compute
128 static SGVec3 fromGeoc(const SGGeoc& geoc);
137 SGVec3<double>::fromGeod(const SGGeod& geod)
140 SGGeodesy::SGGeodToCart(geod, cart);
147 SGVec3<float>::fromGeod(const SGGeod& geod)
150 SGGeodesy::SGGeodToCart(geod, cart);
151 return SGVec3<float>(cart(0), cart(1), cart(2));
157 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
160 SGGeodesy::SGGeocToCart(geoc, cart);
167 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
170 SGGeodesy::SGGeocToCart(geoc, cart);
171 return SGVec3<float>(cart(0), cart(1), cart(2));
174 /// Unary +, do nothing ...
178 operator+(const SGVec3<T>& v)
181 /// Unary -, do nearly nothing
185 operator-(const SGVec3<T>& v)
186 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
192 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
193 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
199 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
200 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
202 /// Scalar multiplication
203 template<typename S, typename T>
206 operator*(S s, const SGVec3<T>& v)
207 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
209 /// Scalar multiplication
210 template<typename S, typename T>
213 operator*(const SGVec3<T>& v, S s)
214 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
216 /// multiplication as a multiplicator, that is assume that the first vector
217 /// represents a 3x3 diagonal matrix with the diagonal elements in the vector.
218 /// Then the result is the product of that matrix times the second vector.
222 mult(const SGVec3<T>& v1, const SGVec3<T>& v2)
223 { return SGVec3<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2)); }
225 /// component wise min
229 min(const SGVec3<T>& v1, const SGVec3<T>& v2)
231 return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
232 SGMisc<T>::min(v1(1), v2(1)),
233 SGMisc<T>::min(v1(2), v2(2)));
235 template<typename S, typename T>
238 min(const SGVec3<T>& v, S s)
240 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
241 SGMisc<T>::min(s, v(1)),
242 SGMisc<T>::min(s, v(2)));
244 template<typename S, typename T>
247 min(S s, const SGVec3<T>& v)
249 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
250 SGMisc<T>::min(s, v(1)),
251 SGMisc<T>::min(s, v(2)));
254 /// component wise max
258 max(const SGVec3<T>& v1, const SGVec3<T>& v2)
260 return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
261 SGMisc<T>::max(v1(1), v2(1)),
262 SGMisc<T>::max(v1(2), v2(2)));
264 template<typename S, typename T>
267 max(const SGVec3<T>& v, S s)
269 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
270 SGMisc<T>::max(s, v(1)),
271 SGMisc<T>::max(s, v(2)));
273 template<typename S, typename T>
276 max(S s, const SGVec3<T>& v)
278 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
279 SGMisc<T>::max(s, v(1)),
280 SGMisc<T>::max(s, v(2)));
283 /// Scalar dot product
287 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
288 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
290 /// The euclidean norm of the vector, that is what most people call length
294 norm(const SGVec3<T>& v)
295 { return sqrt(dot(v, v)); }
297 /// The euclidean norm of the vector, that is what most people call length
301 length(const SGVec3<T>& v)
302 { return sqrt(dot(v, v)); }
304 /// The 1-norm of the vector, this one is the fastest length function we
305 /// can implement on modern cpu's
309 norm1(const SGVec3<T>& v)
310 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
312 /// The inf-norm of the vector
316 normI(const SGVec3<T>& v)
317 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
319 /// Vector cross product
323 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
325 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
326 v1(2)*v2(0) - v1(0)*v2(2),
327 v1(0)*v2(1) - v1(1)*v2(0));
330 /// return any normalized vector perpendicular to v
334 perpendicular(const SGVec3<T>& v)
336 T absv1 = fabs(v(0));
337 T absv2 = fabs(v(1));
338 T absv3 = fabs(v(2));
340 if (absv2 < absv1 && absv3 < absv1) {
342 return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
343 } else if (absv3 < absv2) {
345 return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
346 } else if (SGLimits<T>::min() < absv3) {
348 return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
350 // the all zero case ...
351 return SGVec3<T>(0, 0, 0);
355 /// Construct a unit vector in the given direction.
356 /// or the zero vector if the input vector is zero.
360 normalize(const SGVec3<T>& v)
363 if (normv <= SGLimits<T>::min())
364 return SGVec3<T>::zeros();
368 /// Return true if exactly the same
372 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
373 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
375 /// Return true if not exactly the same
379 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
380 { return ! (v1 == v2); }
382 /// Return true if smaller, good for putting that into a std::map
386 operator<(const SGVec3<T>& v1, const SGVec3<T>& v2)
388 if (v1(0) < v2(0)) return true;
389 else if (v2(0) < v1(0)) return false;
390 else if (v1(1) < v2(1)) return true;
391 else if (v2(1) < v1(1)) return false;
392 else return (v1(2) < v2(2));
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)
411 { return operator<(v2, v1); }
416 operator>=(const SGVec3<T>& v1, const SGVec3<T>& v2)
417 { return operator<=(v2, v1); }
419 /// Return true if equal to the relative tolerance tol
423 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
424 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
426 /// Return true if equal to the relative tolerance tol
430 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
431 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
433 /// Return true if about equal to roundoff of the underlying type
437 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
439 T tol = 100*SGLimits<T>::epsilon();
440 return equivalent(v1, v2, tol, tol);
443 /// The euclidean distance of the two vectors
447 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
448 { return norm(v1 - v2); }
450 /// The squared euclidean distance of the two vectors
454 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
455 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
457 // calculate the projection of u along the direction of d.
461 projection(const SGVec3<T>& u, const SGVec3<T>& d)
465 if (SGLimits<T>::min() < denom) return u;
466 else return d * (dot(u, d) / denom);
473 isNaN(const SGVec3<T>& v)
475 return SGMisc<T>::isNaN(v(0)) ||
476 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
480 /// Output to an ostream
481 template<typename char_type, typename traits_type, typename T>
483 std::basic_ostream<char_type, traits_type>&
484 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
485 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
489 toVec3f(const SGVec3d& v)
490 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
494 toVec3d(const SGVec3f& v)
495 { return SGVec3d(v(0), v(1), v(2)); }
497 #ifndef NO_OPENSCENEGRAPH_INTERFACE
500 toSG(const osg::Vec3d& v)
501 { return SGVec3d(v[0], v[1], v[2]); }
505 toSG(const osg::Vec3f& v)
506 { return SGVec3f(v[0], v[1], v[2]); }
510 toOsg(const SGVec3d& v)
511 { return osg::Vec3d(v[0], v[1], v[2]); }
515 toOsg(const SGVec3f& v)
516 { return osg::Vec3f(v[0], v[1], v[2]); }