1 // Copyright (C) 2006 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.
25 struct SGVec3Storage {
26 /// Readonly raw storage interface
27 const T (&data(void) const)[3]
29 /// Readonly raw storage interface
41 struct SGVec3Storage<float> : public osg::Vec3f {
42 /// Access raw data by index, the index is unchecked
43 const float (&data(void) const)[3]
44 { return osg::Vec3f::_v; }
45 /// Access raw data by index, the index is unchecked
46 float (&data(void))[3]
47 { return osg::Vec3f::_v; }
49 const osg::Vec3f& osg() const
56 struct SGVec3Storage<double> : public osg::Vec3d {
57 /// Access raw data by index, the index is unchecked
58 const double (&data(void) const)[3]
59 { return osg::Vec3d::_v; }
60 /// Access raw data by index, the index is unchecked
61 double (&data(void))[3]
62 { return osg::Vec3d::_v; }
64 const osg::Vec3d& osg() const
72 class SGVec3 : protected SGVec3Storage<T> {
76 /// Default constructor. Does not initialize at all.
77 /// If you need them zero initialized, use SGVec3::zeros()
80 /// Initialize with nans in the debug build, that will guarantee to have
81 /// a fast uninitialized default constructor in the release but shows up
82 /// uninitialized values in the debug build very fast ...
84 for (unsigned i = 0; i < 3; ++i)
85 data()[i] = SGLimits<T>::quiet_NaN();
88 /// Constructor. Initialize by the given values
90 { data()[0] = x; data()[1] = y; data()[2] = z; }
91 /// Constructor. Initialize by the content of a plain array,
92 /// make sure it has at least 3 elements
93 explicit SGVec3(const T* d)
94 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
96 explicit SGVec3(const SGVec3<S>& d)
97 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
98 explicit SGVec3(const osg::Vec3f& d)
99 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
100 explicit SGVec3(const osg::Vec3d& d)
101 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
102 explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
103 { data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
105 /// Access by index, the index is unchecked
106 const T& operator()(unsigned i) const
107 { return data()[i]; }
108 /// Access by index, the index is unchecked
109 T& operator()(unsigned i)
110 { return data()[i]; }
112 /// Access raw data by index, the index is unchecked
113 const T& operator[](unsigned i) const
114 { return data()[i]; }
115 /// Access raw data by index, the index is unchecked
116 T& operator[](unsigned i)
117 { return data()[i]; }
119 /// Access the x component
120 const T& x(void) const
121 { return data()[0]; }
122 /// Access the x component
124 { return data()[0]; }
125 /// Access the y component
126 const T& y(void) const
127 { return data()[1]; }
128 /// Access the y component
130 { return data()[1]; }
131 /// Access the z component
132 const T& z(void) const
133 { return data()[2]; }
134 /// Access the z component
136 { return data()[2]; }
138 /// Get the data pointer
139 using SGVec3Storage<T>::data;
141 /// Readonly interface function to ssg's sgVec3/sgdVec3
142 const T (&sg(void) const)[3]
144 /// Interface function to ssg's sgVec3/sgdVec3
148 /// Interface function to osg's Vec3*
149 using SGVec3Storage<T>::osg;
152 SGVec3& operator+=(const SGVec3& v)
153 { data()[0] += v(0); data()[1] += v(1); data()[2] += v(2); return *this; }
154 /// Inplace subtraction
155 SGVec3& operator-=(const SGVec3& v)
156 { data()[0] -= v(0); data()[1] -= v(1); data()[2] -= v(2); return *this; }
157 /// Inplace scalar multiplication
159 SGVec3& operator*=(S s)
160 { data()[0] *= s; data()[1] *= s; data()[2] *= s; return *this; }
161 /// Inplace scalar multiplication by 1/s
163 SGVec3& operator/=(S s)
164 { return operator*=(1/T(s)); }
166 /// Return an all zero vector
167 static SGVec3 zeros(void)
168 { return SGVec3(0, 0, 0); }
169 /// Return unit vectors
170 static SGVec3 e1(void)
171 { return SGVec3(1, 0, 0); }
172 static SGVec3 e2(void)
173 { return SGVec3(0, 1, 0); }
174 static SGVec3 e3(void)
175 { return SGVec3(0, 0, 1); }
177 /// Constructor. Initialize by a geodetic coordinate
178 /// Note that this conversion is relatively expensive to compute
179 static SGVec3 fromGeod(const SGGeod& geod);
180 /// Constructor. Initialize by a geocentric coordinate
181 /// Note that this conversion is relatively expensive to compute
182 static SGVec3 fromGeoc(const SGGeoc& geoc);
188 SGVec3<double>::fromGeod(const SGGeod& geod)
191 SGGeodesy::SGGeodToCart(geod, cart);
198 SGVec3<float>::fromGeod(const SGGeod& geod)
201 SGGeodesy::SGGeodToCart(geod, cart);
202 return SGVec3<float>(cart(0), cart(1), cart(2));
208 SGVec3<double>::fromGeoc(const SGGeoc& geoc)
211 SGGeodesy::SGGeocToCart(geoc, cart);
218 SGVec3<float>::fromGeoc(const SGGeoc& geoc)
221 SGGeodesy::SGGeocToCart(geoc, cart);
222 return SGVec3<float>(cart(0), cart(1), cart(2));
225 /// Unary +, do nothing ...
229 operator+(const SGVec3<T>& v)
232 /// Unary -, do nearly nothing
236 operator-(const SGVec3<T>& v)
237 { return SGVec3<T>(-v(0), -v(1), -v(2)); }
243 operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
244 { return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
250 operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
251 { return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
253 /// Scalar multiplication
254 template<typename S, typename T>
257 operator*(S s, const SGVec3<T>& v)
258 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
260 /// Scalar multiplication
261 template<typename S, typename T>
264 operator*(const SGVec3<T>& v, S s)
265 { return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
267 /// multiplication as a multiplicator, that is assume that the first vector
268 /// represents a 3x3 diagonal matrix with the diagonal elements in the vector.
269 /// Then the result is the product of that matrix times the second vector.
273 mult(const SGVec3<T>& v1, const SGVec3<T>& v2)
274 { return SGVec3<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2)); }
276 /// component wise min
280 min(const SGVec3<T>& v1, const SGVec3<T>& v2)
282 return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
283 SGMisc<T>::min(v1(1), v2(1)),
284 SGMisc<T>::min(v1(2), v2(2)));
286 template<typename S, typename T>
289 min(const SGVec3<T>& v, S s)
291 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
292 SGMisc<T>::min(s, v(1)),
293 SGMisc<T>::min(s, v(2)));
295 template<typename S, typename T>
298 min(S s, const SGVec3<T>& v)
300 return SGVec3<T>(SGMisc<T>::min(s, v(0)),
301 SGMisc<T>::min(s, v(1)),
302 SGMisc<T>::min(s, v(2)));
305 /// component wise max
309 max(const SGVec3<T>& v1, const SGVec3<T>& v2)
311 return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
312 SGMisc<T>::max(v1(1), v2(1)),
313 SGMisc<T>::max(v1(2), v2(2)));
315 template<typename S, typename T>
318 max(const SGVec3<T>& v, S s)
320 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
321 SGMisc<T>::max(s, v(1)),
322 SGMisc<T>::max(s, v(2)));
324 template<typename S, typename T>
327 max(S s, const SGVec3<T>& v)
329 return SGVec3<T>(SGMisc<T>::max(s, v(0)),
330 SGMisc<T>::max(s, v(1)),
331 SGMisc<T>::max(s, v(2)));
334 /// Scalar dot product
338 dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
339 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
341 /// The euclidean norm of the vector, that is what most people call length
345 norm(const SGVec3<T>& v)
346 { return sqrt(dot(v, v)); }
348 /// The euclidean norm of the vector, that is what most people call length
352 length(const SGVec3<T>& v)
353 { return sqrt(dot(v, v)); }
355 /// The 1-norm of the vector, this one is the fastest length function we
356 /// can implement on modern cpu's
360 norm1(const SGVec3<T>& v)
361 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
363 /// The inf-norm of the vector
367 normI(const SGVec3<T>& v)
368 { return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
370 /// Vector cross product
374 cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
376 return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
377 v1(2)*v2(0) - v1(0)*v2(2),
378 v1(0)*v2(1) - v1(1)*v2(0));
381 /// return any normalized vector perpendicular to v
385 perpendicular(const SGVec3<T>& v)
387 T absv1 = fabs(v(0));
388 T absv2 = fabs(v(1));
389 T absv3 = fabs(v(2));
391 if (absv2 < absv1 && absv3 < absv1) {
393 return (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
394 } else if (absv3 < absv2) {
396 return (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
397 } else if (SGLimits<T>::min() < absv3) {
399 return (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
401 // the all zero case ...
402 return SGVec3<T>(0, 0, 0);
406 /// The euclidean norm of the vector, that is what most people call length
410 normalize(const SGVec3<T>& v)
411 { return (1/norm(v))*v; }
413 /// Return true if exactly the same
417 operator==(const SGVec3<T>& v1, const SGVec3<T>& v2)
418 { return v1(0) == v2(0) && v1(1) == v2(1) && v1(2) == v2(2); }
420 /// Return true if not exactly the same
424 operator!=(const SGVec3<T>& v1, const SGVec3<T>& v2)
425 { return ! (v1 == v2); }
427 /// Return true if smaller, good for putting that into a std::map
431 operator<(const SGVec3<T>& v1, const SGVec3<T>& v2)
433 if (v1(0) < v2(0)) return true;
434 else if (v2(0) < v1(0)) return false;
435 else if (v1(1) < v2(1)) return true;
436 else if (v2(1) < v1(1)) return false;
437 else return (v1(2) < v2(2));
443 operator<=(const SGVec3<T>& v1, const SGVec3<T>& v2)
445 if (v1(0) < v2(0)) return true;
446 else if (v2(0) < v1(0)) return false;
447 else if (v1(1) < v2(1)) return true;
448 else if (v2(1) < v1(1)) return false;
449 else return (v1(2) <= v2(2));
455 operator>(const SGVec3<T>& v1, const SGVec3<T>& v2)
456 { return operator<(v2, v1); }
461 operator>=(const SGVec3<T>& v1, const SGVec3<T>& v2)
462 { return operator<=(v2, v1); }
464 /// Return true if equal to the relative tolerance tol
468 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol, T atol)
469 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)) + atol; }
471 /// Return true if equal to the relative tolerance tol
475 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2, T rtol)
476 { return norm1(v1 - v2) < rtol*(norm1(v1) + norm1(v2)); }
478 /// Return true if about equal to roundoff of the underlying type
482 equivalent(const SGVec3<T>& v1, const SGVec3<T>& v2)
484 T tol = 100*SGLimits<T>::epsilon();
485 return equivalent(v1, v2, tol, tol);
488 /// The euclidean distance of the two vectors
492 dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
493 { return norm(v1 - v2); }
495 /// The squared euclidean distance of the two vectors
499 distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
500 { SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
506 isNaN(const SGVec3<T>& v)
508 return SGMisc<T>::isNaN(v(0)) ||
509 SGMisc<T>::isNaN(v(1)) || SGMisc<T>::isNaN(v(2));
513 /// Output to an ostream
514 template<typename char_type, typename traits_type, typename T>
516 std::basic_ostream<char_type, traits_type>&
517 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
518 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << " ]"; }
522 toVec3f(const SGVec3d& v)
523 { return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
527 toVec3d(const SGVec3f& v)
528 { return SGVec3d(v(0), v(1), v(2)); }