00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef SGQuat_H
00019 #define SGQuat_H
00020
00021 #ifdef min
00022 #undef min
00023 #endif
00024
00025 #ifdef max
00026 #undef max
00027 #endif
00028
00029 #ifndef NO_OPENSCENEGRAPH_INTERFACE
00030 #include <osg/Quat>
00031 #endif
00032
00034 template<typename T>
00035 class SGQuat {
00036 public:
00037 typedef T value_type;
00038
00041 SGQuat(void)
00042 {
00046 #ifndef NDEBUG
00047 for (unsigned i = 0; i < 4; ++i)
00048 data()[i] = SGLimits<T>::quiet_NaN();
00049 #endif
00050 }
00052 SGQuat(T _x, T _y, T _z, T _w)
00053 { x() = _x; y() = _y; z() = _z; w() = _w; }
00056 explicit SGQuat(const T* d)
00057 { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
00058
00060 static SGQuat unit(void)
00061 { return fromRealImag(1, SGVec3<T>(0, 0, 0)); }
00062
00064 static SGQuat fromEulerRad(T z, T y, T x)
00065 {
00066 SGQuat q;
00067 T zd2 = T(0.5)*z; T yd2 = T(0.5)*y; T xd2 = T(0.5)*x;
00068 T Szd2 = sin(zd2); T Syd2 = sin(yd2); T Sxd2 = sin(xd2);
00069 T Czd2 = cos(zd2); T Cyd2 = cos(yd2); T Cxd2 = cos(xd2);
00070 T Cxd2Czd2 = Cxd2*Czd2; T Cxd2Szd2 = Cxd2*Szd2;
00071 T Sxd2Szd2 = Sxd2*Szd2; T Sxd2Czd2 = Sxd2*Czd2;
00072 q.w() = Cxd2Czd2*Cyd2 + Sxd2Szd2*Syd2;
00073 q.x() = Sxd2Czd2*Cyd2 - Cxd2Szd2*Syd2;
00074 q.y() = Cxd2Czd2*Syd2 + Sxd2Szd2*Cyd2;
00075 q.z() = Cxd2Szd2*Cyd2 - Sxd2Czd2*Syd2;
00076 return q;
00077 }
00078
00080 static SGQuat fromEulerDeg(T z, T y, T x)
00081 {
00082 return fromEulerRad(SGMisc<T>::deg2rad(z), SGMisc<T>::deg2rad(y),
00083 SGMisc<T>::deg2rad(x));
00084 }
00085
00087 static SGQuat fromYawPitchRoll(T y, T p, T r)
00088 { return fromEulerRad(y, p, r); }
00089
00091 static SGQuat fromYawPitchRollDeg(T y, T p, T r)
00092 { return fromEulerDeg(y, p, r); }
00093
00095 static SGQuat fromHeadAttBank(T h, T a, T b)
00096 { return fromEulerRad(h, a, b); }
00097
00099 static SGQuat fromHeadAttBankDeg(T h, T a, T b)
00100 { return fromEulerDeg(h, a, b); }
00101
00108 static SGQuat fromLonLatRad(T lon, T lat)
00109 {
00110 SGQuat q;
00111 T zd2 = T(0.5)*lon;
00112 T yd2 = T(-0.25)*SGMisc<T>::pi() - T(0.5)*lat;
00113 T Szd2 = sin(zd2);
00114 T Syd2 = sin(yd2);
00115 T Czd2 = cos(zd2);
00116 T Cyd2 = cos(yd2);
00117 q.w() = Czd2*Cyd2;
00118 q.x() = -Szd2*Syd2;
00119 q.y() = Czd2*Syd2;
00120 q.z() = Szd2*Cyd2;
00121 return q;
00122 }
00124 static SGQuat fromLonLatDeg(T lon, T lat)
00125 { return fromLonLatRad(SGMisc<T>::deg2rad(lon), SGMisc<T>::deg2rad(lat)); }
00127 static SGQuat fromLonLat(const SGGeod& geod)
00128 { return fromLonLatRad(geod.getLongitudeRad(), geod.getLatitudeRad()); }
00129
00130
00132 static SGQuat fromAngleAxis(T angle, const SGVec3<T>& axis)
00133 {
00134 T angle2 = T(0.5)*angle;
00135 return fromRealImag(cos(angle2), T(sin(angle2))*axis);
00136 }
00137
00139 static SGQuat fromAngleAxisDeg(T angle, const SGVec3<T>& axis)
00140 { return fromAngleAxis(SGMisc<T>::deg2rad(angle), axis); }
00141
00144 static SGQuat fromAngleAxis(const SGVec3<T>& axis)
00145 {
00146 T nAxis = norm(axis);
00147 if (nAxis <= SGLimits<T>::min())
00148 return SGQuat::unit();
00149 T angle2 = T(0.5)*nAxis;
00150 return fromRealImag(cos(angle2), T(sin(angle2)/nAxis)*axis);
00151 }
00152
00154 static SGQuat fromRotateTo(const SGVec3<T>& from, const SGVec3<T>& to)
00155 {
00156 T nfrom = norm(from);
00157 T nto = norm(to);
00158 if (nfrom <= SGLimits<T>::min() || nto <= SGLimits<T>::min())
00159 return SGQuat::unit();
00160
00161 return SGQuat::fromRotateToNorm((1/nfrom)*from, (1/nto)*to);
00162 }
00163
00166 static SGQuat fromRotateTo(const SGVec3<T>& v1, unsigned i1,
00167 const SGVec3<T>& v2, unsigned i2)
00168 {
00169 T nrmv1 = norm(v1);
00170 T nrmv2 = norm(v2);
00171 if (nrmv1 <= SGLimits<T>::min() || nrmv2 <= SGLimits<T>::min())
00172 return SGQuat::unit();
00173
00174 SGVec3<T> nv1 = (1/nrmv1)*v1;
00175 SGVec3<T> nv2 = (1/nrmv2)*v2;
00176 T dv1v2 = dot(nv1, nv2);
00177 if (fabs(fabs(dv1v2)-1) <= SGLimits<T>::epsilon())
00178 return SGQuat::unit();
00179
00180
00181 SGVec3<T> nto1 = SGVec3<T>::zeros();
00182 SGVec3<T> nto2 = SGVec3<T>::zeros();
00183 nto1[i1] = 1;
00184 nto2[i2] = 1;
00185
00186
00187 SGQuat q = SGQuat::fromRotateToNorm(nv1, nto1);
00188
00189
00190
00191
00192
00193
00194 nv2 = normalize(nv2 - dv1v2*nv1);
00195
00196 SGVec3<T> tnv2 = q.transform(nv2);
00197 T cosang = dot(nto2, tnv2);
00198 T cos05ang = T(0.5)+T(0.5)*cosang;
00199 if (cos05ang <= 0)
00200 cosang = 0;
00201 cos05ang = sqrt(cos05ang);
00202 T sig = dot(nto1, cross(nto2, tnv2));
00203 T sin05ang = T(0.5)-T(0.5)*cosang;
00204 if (sin05ang <= 0)
00205 sin05ang = 0;
00206 sin05ang = copysign(sqrt(sin05ang), sig);
00207 q *= SGQuat::fromRealImag(cos05ang, sin05ang*nto1);
00208
00209 return q;
00210 }
00211
00212
00213
00214
00215 static SGQuat fromChangeSign(const SGVec3<T>& v)
00216 {
00217
00218
00219 T absv1 = fabs(v(0));
00220 T absv2 = fabs(v(1));
00221 T absv3 = fabs(v(2));
00222
00223 SGVec3<T> axis;
00224 if (absv2 < absv1 && absv3 < absv1) {
00225 T quot = v(1)/v(0);
00226 axis = (1/sqrt(1+quot*quot))*SGVec3<T>(quot, -1, 0);
00227 } else if (absv1 < absv2 && absv3 < absv2) {
00228 T quot = v(2)/v(1);
00229 axis = (1/sqrt(1+quot*quot))*SGVec3<T>(0, quot, -1);
00230 } else if (absv1 < absv3 && absv2 < absv3) {
00231 T quot = v(0)/v(2);
00232 axis = (1/sqrt(1+quot*quot))*SGVec3<T>(-1, 0, quot);
00233 } else {
00234
00235 return SGQuat::unit();
00236 }
00237
00238 return SGQuat::fromRealImag(0, axis);
00239 }
00240
00242 static SGQuat fromRealImag(T r, const SGVec3<T>& i)
00243 {
00244 SGQuat q;
00245 q.w() = r;
00246 q.x() = i.x();
00247 q.y() = i.y();
00248 q.z() = i.z();
00249 return q;
00250 }
00251
00253 static SGQuat zeros(void)
00254 { return SGQuat(0, 0, 0, 0); }
00255
00257 void getEulerRad(T& zRad, T& yRad, T& xRad) const
00258 {
00259 T sqrQW = w()*w();
00260 T sqrQX = x()*x();
00261 T sqrQY = y()*y();
00262 T sqrQZ = z()*z();
00263
00264 T num = 2*(y()*z() + w()*x());
00265 T den = sqrQW - sqrQX - sqrQY + sqrQZ;
00266 if (fabs(den) <= SGLimits<T>::min() &&
00267 fabs(num) <= SGLimits<T>::min())
00268 xRad = 0;
00269 else
00270 xRad = atan2(num, den);
00271
00272 T tmp = 2*(x()*z() - w()*y());
00273 if (tmp <= -1)
00274 yRad = T(0.5)*SGMisc<T>::pi();
00275 else if (1 <= tmp)
00276 yRad = -T(0.5)*SGMisc<T>::pi();
00277 else
00278 yRad = -asin(tmp);
00279
00280 num = 2*(x()*y() + w()*z());
00281 den = sqrQW + sqrQX - sqrQY - sqrQZ;
00282 if (fabs(den) <= SGLimits<T>::min() &&
00283 fabs(num) <= SGLimits<T>::min())
00284 zRad = 0;
00285 else {
00286 T psi = atan2(num, den);
00287 if (psi < 0)
00288 psi += 2*SGMisc<T>::pi();
00289 zRad = psi;
00290 }
00291 }
00292
00294 void getEulerDeg(T& zDeg, T& yDeg, T& xDeg) const
00295 {
00296 getEulerRad(zDeg, yDeg, xDeg);
00297 zDeg = SGMisc<T>::rad2deg(zDeg);
00298 yDeg = SGMisc<T>::rad2deg(yDeg);
00299 xDeg = SGMisc<T>::rad2deg(xDeg);
00300 }
00301
00303 void getAngleAxis(T& angle, SGVec3<T>& axis) const
00304 {
00305 T nrm = norm(*this);
00306 if (nrm <= SGLimits<T>::min()) {
00307 angle = 0;
00308 axis = SGVec3<T>(0, 0, 0);
00309 } else {
00310 T rNrm = 1/nrm;
00311 angle = acos(SGMisc<T>::max(-1, SGMisc<T>::min(1, rNrm*w())));
00312 T sAng = sin(angle);
00313 if (fabs(sAng) <= SGLimits<T>::min())
00314 axis = SGVec3<T>(1, 0, 0);
00315 else
00316 axis = (rNrm/sAng)*imag(*this);
00317 angle *= 2;
00318 }
00319 }
00320
00322 void getAngleAxis(SGVec3<T>& axis) const
00323 {
00324 T angle;
00325 getAngleAxis(angle, axis);
00326 axis *= angle;
00327 }
00328
00330 const T& operator()(unsigned i) const
00331 { return data()[i]; }
00333 T& operator()(unsigned i)
00334 { return data()[i]; }
00335
00337 const T& operator[](unsigned i) const
00338 { return data()[i]; }
00340 T& operator[](unsigned i)
00341 { return data()[i]; }
00342
00344 const T& x(void) const
00345 { return data()[0]; }
00347 T& x(void)
00348 { return data()[0]; }
00350 const T& y(void) const
00351 { return data()[1]; }
00353 T& y(void)
00354 { return data()[1]; }
00356 const T& z(void) const
00357 { return data()[2]; }
00359 T& z(void)
00360 { return data()[2]; }
00362 const T& w(void) const
00363 { return data()[3]; }
00365 T& w(void)
00366 { return data()[3]; }
00367
00369 const T (&data(void) const)[4]
00370 { return _data; }
00372 T (&data(void))[4]
00373 { return _data; }
00374
00376 SGQuat& operator+=(const SGQuat& v)
00377 { data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; }
00379 SGQuat& operator-=(const SGQuat& v)
00380 { data()[0]-=v(0);data()[1]-=v(1);data()[2]-=v(2);data()[3]-=v(3);return *this; }
00382 template<typename S>
00383 SGQuat& operator*=(S s)
00384 { data()[0] *= s; data()[1] *= s; data()[2] *= s; data()[3] *= s; return *this; }
00386 template<typename S>
00387 SGQuat& operator/=(S s)
00388 { return operator*=(1/T(s)); }
00390 SGQuat& operator*=(const SGQuat& v);
00391
00394 SGVec3<T> transform(const SGVec3<T>& v) const
00395 {
00396 T r = 2/dot(*this, *this);
00397 SGVec3<T> qimag = imag(*this);
00398 T qr = real(*this);
00399 return (r*qr*qr - 1)*v + (r*dot(qimag, v))*qimag - (r*qr)*cross(qimag, v);
00400 }
00403 SGVec3<T> backTransform(const SGVec3<T>& v) const
00404 {
00405 T r = 2/dot(*this, *this);
00406 SGVec3<T> qimag = imag(*this);
00407 T qr = real(*this);
00408 return (r*qr*qr - 1)*v + (r*dot(qimag, v))*qimag + (r*qr)*cross(qimag, v);
00409 }
00410
00412 SGVec3<T> rotate(const SGVec3<T>& v) const
00413 { return backTransform(v); }
00415 SGVec3<T> rotateBack(const SGVec3<T>& v) const
00416 { return transform(v); }
00417
00419 SGQuat
00420 derivative(const SGVec3<T>& angVel) const
00421 {
00422 SGQuat deriv;
00423
00424 deriv.w() = T(0.5)*(-x()*angVel(0) - y()*angVel(1) - z()*angVel(2));
00425 deriv.x() = T(0.5)*( w()*angVel(0) - z()*angVel(1) + y()*angVel(2));
00426 deriv.y() = T(0.5)*( z()*angVel(0) + w()*angVel(1) - x()*angVel(2));
00427 deriv.z() = T(0.5)*(-y()*angVel(0) + x()*angVel(1) + w()*angVel(2));
00428
00429 return deriv;
00430 }
00431
00432 private:
00433
00434
00435 static SGQuat
00436 fromRotateToSmaller90Deg(T cosang,
00437 const SGVec3<T>& from, const SGVec3<T>& to)
00438 {
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452 T cos05ang = sqrt(T(0.5)+T(0.5)*cosang);
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463 SGVec3<T> axis = cross(to, from);
00464
00465
00466
00467
00468
00469 return SGQuat::fromRealImag( cos05ang, (1/(2*cos05ang))*axis);
00470 }
00471
00472
00473 static SGQuat
00474 fromRotateToNorm(const SGVec3<T>& from, const SGVec3<T>& to)
00475 {
00476
00477
00478
00479
00480 T cosang = dot(from, to);
00481
00482
00483 if (T(-0.5) < cosang)
00484 return SGQuat::fromRotateToSmaller90Deg(cosang, from, to);
00485
00486
00487
00488 SGQuat q1 = SGQuat::fromChangeSign(from);
00489 SGQuat q2 = SGQuat::fromRotateToSmaller90Deg(-cosang, -from, to);
00490 return q1*q2;
00491 }
00492
00493 T _data[4];
00494 };
00495
00497 template<typename T>
00498 inline
00499 const SGQuat<T>&
00500 operator+(const SGQuat<T>& v)
00501 { return v; }
00502
00504 template<typename T>
00505 inline
00506 SGQuat<T>
00507 operator-(const SGQuat<T>& v)
00508 { return SGQuat<T>(-v(0), -v(1), -v(2), -v(3)); }
00509
00511 template<typename T>
00512 inline
00513 SGQuat<T>
00514 operator+(const SGQuat<T>& v1, const SGQuat<T>& v2)
00515 { return SGQuat<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
00516
00518 template<typename T>
00519 inline
00520 SGQuat<T>
00521 operator-(const SGQuat<T>& v1, const SGQuat<T>& v2)
00522 { return SGQuat<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
00523
00525 template<typename S, typename T>
00526 inline
00527 SGQuat<T>
00528 operator*(S s, const SGQuat<T>& v)
00529 { return SGQuat<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
00530
00532 template<typename S, typename T>
00533 inline
00534 SGQuat<T>
00535 operator*(const SGQuat<T>& v, S s)
00536 { return SGQuat<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
00537
00539 template<typename T>
00540 inline
00541 SGQuat<T>
00542 operator*(const SGQuat<T>& v1, const SGQuat<T>& v2)
00543 {
00544 SGQuat<T> v;
00545 v.x() = v1.w()*v2.x() + v1.x()*v2.w() + v1.y()*v2.z() - v1.z()*v2.y();
00546 v.y() = v1.w()*v2.y() - v1.x()*v2.z() + v1.y()*v2.w() + v1.z()*v2.x();
00547 v.z() = v1.w()*v2.z() + v1.x()*v2.y() - v1.y()*v2.x() + v1.z()*v2.w();
00548 v.w() = v1.w()*v2.w() - v1.x()*v2.x() - v1.y()*v2.y() - v1.z()*v2.z();
00549 return v;
00550 }
00551
00553 template<typename T>
00554 inline
00555 SGQuat<T>&
00556 SGQuat<T>::operator*=(const SGQuat& v)
00557 { (*this) = (*this)*v; return *this; }
00558
00561 template<typename T>
00562 inline
00563 SGQuat<T>
00564 conj(const SGQuat<T>& v)
00565 { return SGQuat<T>(-v(0), -v(1), -v(2), v(3)); }
00566
00569 template<typename T>
00570 inline
00571 SGQuat<T>
00572 inverse(const SGQuat<T>& v)
00573 { return (1/dot(v, v))*SGQuat<T>(-v(0), -v(1), -v(2), v(3)); }
00574
00576 template<typename T>
00577 inline
00578 T
00579 real(const SGQuat<T>& v)
00580 { return v.w(); }
00581
00583 template<typename T>
00584 inline
00585 SGVec3<T>
00586 imag(const SGQuat<T>& v)
00587 { return SGVec3<T>(v.x(), v.y(), v.z()); }
00588
00590 template<typename T>
00591 inline
00592 T
00593 dot(const SGQuat<T>& v1, const SGQuat<T>& v2)
00594 { return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
00595
00597 template<typename T>
00598 inline
00599 T
00600 norm(const SGQuat<T>& v)
00601 { return sqrt(dot(v, v)); }
00602
00604 template<typename T>
00605 inline
00606 T
00607 length(const SGQuat<T>& v)
00608 { return sqrt(dot(v, v)); }
00609
00612 template<typename T>
00613 inline
00614 T
00615 norm1(const SGQuat<T>& v)
00616 { return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
00617
00619 template<typename T>
00620 inline
00621 SGQuat<T>
00622 normalize(const SGQuat<T>& q)
00623 { return (1/norm(q))*q; }
00624
00626 template<typename T>
00627 inline
00628 bool
00629 operator==(const SGQuat<T>& v1, const SGQuat<T>& v2)
00630 { return v1(0)==v2(0) && v1(1)==v2(1) && v1(2)==v2(2) && v1(3)==v2(3); }
00631
00633 template<typename T>
00634 inline
00635 bool
00636 operator!=(const SGQuat<T>& v1, const SGQuat<T>& v2)
00637 { return ! (v1 == v2); }
00638
00642 template<typename T>
00643 inline
00644 bool
00645 equivalent(const SGQuat<T>& v1, const SGQuat<T>& v2, T tol)
00646 { return norm1(v1 - v2) < tol*(norm1(v1) + norm1(v2)); }
00647
00651 template<typename T>
00652 inline
00653 bool
00654 equivalent(const SGQuat<T>& v1, const SGQuat<T>& v2)
00655 { return equivalent(v1, v2, 100*SGLimits<T>::epsilon()); }
00656
00657 #ifndef NDEBUG
00658 template<typename T>
00659 inline
00660 bool
00661 isNaN(const SGQuat<T>& v)
00662 {
00663 return SGMisc<T>::isNaN(v(0)) || SGMisc<T>::isNaN(v(1))
00664 || SGMisc<T>::isNaN(v(2)) || SGMisc<T>::isNaN(v(3));
00665 }
00666 #endif
00667
00670 template<typename T>
00671 inline
00672 SGQuat<T>
00673 interpolate(T t, const SGQuat<T>& src, const SGQuat<T>& dst)
00674 {
00675 T cosPhi = dot(src, dst);
00676
00677 int signCosPhi = SGMisc<T>::sign(cosPhi);
00678
00679 cosPhi = fabs(cosPhi);
00680
00681
00682
00683 if (1 <= cosPhi)
00684 return dst;
00685
00686
00687 T o = acos(cosPhi);
00688
00689
00690
00691 T scale0, scale1;
00692 if (fabs(o) <= SGLimits<T>::epsilon()) {
00693 scale0 = 1 - t;
00694 scale1 = t;
00695 } else {
00696
00697 T sino = sin(o);
00698 T so = 1/sino;
00699 scale0 = sin((1 - t)*o)*so;
00700 scale1 = sin(t*o)*so;
00701 }
00702
00703 return scale0*src + signCosPhi*scale1*dst;
00704 }
00705
00707 template<typename char_type, typename traits_type, typename T>
00708 inline
00709 std::basic_ostream<char_type, traits_type>&
00710 operator<<(std::basic_ostream<char_type, traits_type>& s, const SGQuat<T>& v)
00711 { return s << "[ " << v(0) << ", " << v(1) << ", " << v(2) << ", " << v(3) << " ]"; }
00712
00713 inline
00714 SGQuatf
00715 toQuatf(const SGQuatd& v)
00716 { return SGQuatf((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
00717
00718 inline
00719 SGQuatd
00720 toQuatd(const SGQuatf& v)
00721 { return SGQuatd(v(0), v(1), v(2), v(3)); }
00722
00723 #ifndef NO_OPENSCENEGRAPH_INTERFACE
00724 inline
00725 SGQuatd
00726 toSG(const osg::Quat& q)
00727 { return SGQuatd(q[0], q[1], q[2], q[3]); }
00728
00729 inline
00730 osg::Quat
00731 toOsg(const SGQuatd& q)
00732 { return osg::Quat(q[0], q[1], q[2], q[3]); }
00733 #endif
00734
00735 #endif