X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fmath%2FSGQuat.hxx;h=a48cbf1ab08a17c2deb8a0c35316c271248c2c00;hb=dfea3623f6549c9173fed5149da41285863fc290;hp=f05568e04b0d056a83116bba1e71c179f667c357;hpb=84dd54b33a6d8b35e57c32194b025f79245f35c4;p=simgear.git diff --git a/simgear/math/SGQuat.hxx b/simgear/math/SGQuat.hxx index f05568e0..a48cbf1a 100644 --- a/simgear/math/SGQuat.hxx +++ b/simgear/math/SGQuat.hxx @@ -1,4 +1,4 @@ -// Copyright (C) 2006 Mathias Froehlich - Mathias.Froehlich@web.de +// Copyright (C) 2006-2009 Mathias Froehlich - Mathias.Froehlich@web.de // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Library General Public @@ -26,42 +26,13 @@ #undef max #endif +#ifndef NO_OPENSCENEGRAPH_INTERFACE #include +#endif +/// Quaternion Class template -struct SGQuatStorage { - /// Readonly raw storage interface - const T (&data(void) const)[4] - { return _data; } - /// Readonly raw storage interface - T (&data(void))[4] - { return _data; } - - void osg() const - { } - -private: - T _data[4]; -}; - -template<> -struct SGQuatStorage : public osg::Quat { - /// Access raw data by index, the index is unchecked - const double (&data(void) const)[4] - { return osg::Quat::_v; } - /// Access raw data by index, the index is unchecked - double (&data(void))[4] - { return osg::Quat::_v; } - - const osg::Quat& osg() const - { return *this; } - osg::Quat& osg() - { return *this; } -}; - -/// 3D Vector Class -template -class SGQuat : protected SGQuatStorage { +class SGQuat { public: typedef T value_type; @@ -84,12 +55,10 @@ public: /// make sure it has at least 4 elements explicit SGQuat(const T* d) { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; } - explicit SGQuat(const osg::Quat& d) - { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; } /// Return a unit quaternion static SGQuat unit(void) - { return fromRealImag(1, SGVec3(0)); } + { return fromRealImag(1, SGVec3(0, 0, 0)); } /// Return a quaternion from euler angles static SGQuat fromEulerRad(T z, T y, T x) @@ -130,13 +99,17 @@ public: static SGQuat fromHeadAttBankDeg(T h, T a, T b) { return fromEulerDeg(h, a, b); } - /// Return a quaternion rotation the the horizontal local frame from given - /// longitude and latitude + /// Return a quaternion rotation from the earth centered to the + /// simulation usual horizontal local frame from given + /// longitude and latitude. + /// The horizontal local frame used in simulations is the frame with x-axis + /// pointing north, the y-axis pointing eastwards and the z axis + /// pointing downwards. static SGQuat fromLonLatRad(T lon, T lat) { SGQuat q; T zd2 = T(0.5)*lon; - T yd2 = T(-0.25)*SGMisc::pi() - T(0.5)*lat; + T yd2 = T(-0.25)*SGMisc::pi() - T(0.5)*lat; T Szd2 = sin(zd2); T Syd2 = sin(yd2); T Czd2 = cos(zd2); @@ -147,21 +120,18 @@ public: q.z() = Szd2*Cyd2; return q; } - - /// Return a quaternion rotation the the horizontal local frame from given - /// longitude and latitude + /// Like the above provided for convenience static SGQuat fromLonLatDeg(T lon, T lat) { return fromLonLatRad(SGMisc::deg2rad(lon), SGMisc::deg2rad(lat)); } - - /// Return a quaternion rotation the the horizontal local frame from given - /// longitude and latitude + /// Like the above provided for convenience static SGQuat fromLonLat(const SGGeod& geod) { return fromLonLatRad(geod.getLongitudeRad(), geod.getLatitudeRad()); } + /// Create a quaternion from the angle axis representation static SGQuat fromAngleAxis(T angle, const SGVec3& axis) { - T angle2 = 0.5*angle; + T angle2 = T(0.5)*angle; return fromRealImag(cos(angle2), T(sin(angle2))*axis); } @@ -175,19 +145,107 @@ public: { T nAxis = norm(axis); if (nAxis <= SGLimits::min()) - return SGQuat(1, 0, 0, 0); - T angle2 = 0.5*nAxis; + return SGQuat::unit(); + T angle2 = T(0.5)*nAxis; return fromRealImag(cos(angle2), T(sin(angle2)/nAxis)*axis); } + /// Return a quaternion that rotates the from vector onto the to vector. + static SGQuat fromRotateTo(const SGVec3& from, const SGVec3& to) + { + T nfrom = norm(from); + T nto = norm(to); + if (nfrom <= SGLimits::min() || nto <= SGLimits::min()) + return SGQuat::unit(); + + return SGQuat::fromRotateToNorm((1/nfrom)*from, (1/nto)*to); + } + + /// Return a quaternion that rotates v1 onto the i1-th unit vector + /// and v2 into a plane that is spanned by the i2-th and i1-th unit vector. + static SGQuat fromRotateTo(const SGVec3& v1, unsigned i1, + const SGVec3& v2, unsigned i2) + { + T nrmv1 = norm(v1); + T nrmv2 = norm(v2); + if (nrmv1 <= SGLimits::min() || nrmv2 <= SGLimits::min()) + return SGQuat::unit(); + + SGVec3 nv1 = (1/nrmv1)*v1; + SGVec3 nv2 = (1/nrmv2)*v2; + T dv1v2 = dot(nv1, nv2); + if (fabs(fabs(dv1v2)-1) <= SGLimits::epsilon()) + return SGQuat::unit(); + + // The target vector for the first rotation + SGVec3 nto1 = SGVec3::zeros(); + SGVec3 nto2 = SGVec3::zeros(); + nto1[i1] = 1; + nto2[i2] = 1; + + // The first rotation can be done with the usual routine. + SGQuat q = SGQuat::fromRotateToNorm(nv1, nto1); + + // The rotation axis for the second rotation is the + // target for the first one, so the rotation axis is nto1 + // We need to get the angle. + + // Make nv2 exactly orthogonal to nv1. + nv2 = normalize(nv2 - dv1v2*nv1); + + SGVec3 tnv2 = q.transform(nv2); + T cosang = dot(nto2, tnv2); + T cos05ang = T(0.5)+T(0.5)*cosang; + if (cos05ang <= 0) + cosang = 0; + cos05ang = sqrt(cos05ang); + T sig = dot(nto1, cross(nto2, tnv2)); + T sin05ang = T(0.5)-T(0.5)*cosang; + if (sin05ang <= 0) + sin05ang = 0; + sin05ang = copysign(sqrt(sin05ang), sig); + q *= SGQuat::fromRealImag(cos05ang, sin05ang*nto1); + + return q; + } + + + // Return a quaternion which rotates the vector given by v + // to the vector -v. Other directions are *not* preserved. + static SGQuat fromChangeSign(const SGVec3& v) + { + // The vector from points to the oposite direction than to. + // Find a vector perpendicular to the vector to. + T absv1 = fabs(v(0)); + T absv2 = fabs(v(1)); + T absv3 = fabs(v(2)); + + SGVec3 axis; + if (absv2 < absv1 && absv3 < absv1) { + T quot = v(1)/v(0); + axis = (1/sqrt(1+quot*quot))*SGVec3(quot, -1, 0); + } else if (absv1 < absv2 && absv3 < absv2) { + T quot = v(2)/v(1); + axis = (1/sqrt(1+quot*quot))*SGVec3(0, quot, -1); + } else if (absv1 < absv3 && absv2 < absv3) { + T quot = v(0)/v(2); + axis = (1/sqrt(1+quot*quot))*SGVec3(-1, 0, quot); + } else { + // The all zero case. + return SGQuat::unit(); + } + + return SGQuat::fromRealImag(0, axis); + } + /// Return a quaternion from real and imaginary part static SGQuat fromRealImag(T r, const SGVec3& i) { SGQuat q; q.w() = r; - q.x() = i(0); - q.y() = i(1); - q.z() = i(2); + q.x() = i.x(); + q.y() = i.y(); + q.z() = i.z(); return q; } @@ -198,36 +256,36 @@ public: /// write the euler angles into the references void getEulerRad(T& zRad, T& yRad, T& xRad) const { - value_type sqrQW = w()*w(); - value_type sqrQX = x()*x(); - value_type sqrQY = y()*y(); - value_type sqrQZ = z()*z(); - - value_type num = 2*(y()*z() + w()*x()); - value_type den = sqrQW - sqrQX - sqrQY + sqrQZ; - if (fabs(den) < SGLimits::min() && - fabs(num) < SGLimits::min()) + T sqrQW = w()*w(); + T sqrQX = x()*x(); + T sqrQY = y()*y(); + T sqrQZ = z()*z(); + + T num = 2*(y()*z() + w()*x()); + T den = sqrQW - sqrQX - sqrQY + sqrQZ; + if (fabs(den) <= SGLimits::min() && + fabs(num) <= SGLimits::min()) xRad = 0; else xRad = atan2(num, den); - value_type tmp = 2*(x()*z() - w()*y()); - if (tmp < -1) - yRad = 0.5*SGMisc::pi(); - else if (1 < tmp) - yRad = -0.5*SGMisc::pi(); + T tmp = 2*(x()*z() - w()*y()); + if (tmp <= -1) + yRad = T(0.5)*SGMisc::pi(); + else if (1 <= tmp) + yRad = -T(0.5)*SGMisc::pi(); else yRad = -asin(tmp); num = 2*(x()*y() + w()*z()); den = sqrQW + sqrQX - sqrQY - sqrQZ; - if (fabs(den) < SGLimits::min() && - fabs(num) < SGLimits::min()) + if (fabs(den) <= SGLimits::min() && + fabs(num) <= SGLimits::min()) zRad = 0; else { - value_type psi = atan2(num, den); + T psi = atan2(num, den); if (psi < 0) - psi += 2*SGMisc::pi(); + psi += 2*SGMisc::pi(); zRad = psi; } } @@ -245,14 +303,14 @@ public: void getAngleAxis(T& angle, SGVec3& axis) const { T nrm = norm(*this); - if (nrm < SGLimits::min()) { + if (nrm <= SGLimits::min()) { angle = 0; axis = SGVec3(0, 0, 0); } else { T rNrm = 1/nrm; angle = acos(SGMisc::max(-1, SGMisc::min(1, rNrm*w()))); T sAng = sin(angle); - if (fabs(sAng) < SGLimits::min()) + if (fabs(sAng) <= SGLimits::min()) axis = SGVec3(1, 0, 0); else axis = (rNrm/sAng)*imag(*this); @@ -308,17 +366,11 @@ public: { return data()[3]; } /// Get the data pointer - using SGQuatStorage::data; - - /// Readonly interface function to ssg's sgQuat/sgdQuat - const T (&sg(void) const)[4] - { return data(); } - /// Interface function to ssg's sgQuat/sgdQuat - T (&sg(void))[4] - { return data(); } - - /// Interface function to osg's Quat* - using SGQuatStorage::osg; + const T (&data(void) const)[4] + { return _data; } + /// Get the data pointer + T (&data(void))[4] + { return _data; } /// Inplace addition SGQuat& operator+=(const SGQuat& v) @@ -341,18 +393,18 @@ public: /// frame rotated with the quaternion SGVec3 transform(const SGVec3& v) const { - value_type r = 2/dot(*this, *this); + T r = 2/dot(*this, *this); SGVec3 qimag = imag(*this); - value_type qr = real(*this); + T qr = real(*this); return (r*qr*qr - 1)*v + (r*dot(qimag, v))*qimag - (r*qr)*cross(qimag, v); } /// Transform a vector from the coordinate frame rotated with the quaternion /// to the current coordinate frame SGVec3 backTransform(const SGVec3& v) const { - value_type r = 2/dot(*this, *this); + T r = 2/dot(*this, *this); SGVec3 qimag = imag(*this); - value_type qr = real(*this); + T qr = real(*this); return (r*qr*qr - 1)*v + (r*dot(qimag, v))*qimag + (r*qr)*cross(qimag, v); } @@ -365,17 +417,80 @@ public: /// Return the time derivative of the quaternion given the angular velocity SGQuat - derivative(const SGVec3& angVel) + derivative(const SGVec3& angVel) const { SGQuat deriv; - deriv.w() = 0.5*(-x()*angVel(0) - y()*angVel(1) - z()*angVel(2)); - deriv.x() = 0.5*( w()*angVel(0) - z()*angVel(1) + y()*angVel(2)); - deriv.y() = 0.5*( z()*angVel(0) + w()*angVel(1) - x()*angVel(2)); - deriv.z() = 0.5*(-y()*angVel(0) + x()*angVel(1) + w()*angVel(2)); + deriv.w() = T(0.5)*(-x()*angVel(0) - y()*angVel(1) - z()*angVel(2)); + deriv.x() = T(0.5)*( w()*angVel(0) - z()*angVel(1) + y()*angVel(2)); + deriv.y() = T(0.5)*( z()*angVel(0) + w()*angVel(1) - x()*angVel(2)); + deriv.z() = T(0.5)*(-y()*angVel(0) + x()*angVel(1) + w()*angVel(2)); return deriv; } + +private: + + // Private because it assumes normalized inputs. + static SGQuat + fromRotateToSmaller90Deg(T cosang, + const SGVec3& from, const SGVec3& to) + { + // In this function we assume that the angle required to rotate from + // the vector from to the vector to is <= 90 deg. + // That is done so because of possible instabilities when we rotate more + // then 90deg. + + // Note that the next comment does actually cover a *more* *general* case + // than we need in this function. That shows that this formula is even + // valid for rotations up to 180deg. + + // Because of the signs in the axis, it is sufficient to care for angles + // in the interval [-pi,pi]. That means that 0.5*angle is in the interval + // [-pi/2,pi/2]. But in that range the cosine is allways >= 0. + // So we do not need to care for egative roots in the following equation: + T cos05ang = sqrt(T(0.5)+T(0.5)*cosang); + + + // Now our assumption of angles <= 90 deg comes in play. + // For that reason, we know that cos05ang is not zero. + // It is even more, we can see from the above formula that + // sqrt(0.5) < cos05ang. + + + // Compute the rotation axis, that is + // sin(angle)*normalized rotation axis + SGVec3 axis = cross(to, from); + + // We need sin(0.5*angle)*normalized rotation axis. + // So rescale with sin(0.5*x)/sin(x). + // To do that we use the equation: + // sin(x) = 2*sin(0.5*x)*cos(0.5*x) + return SGQuat::fromRealImag( cos05ang, (1/(2*cos05ang))*axis); + } + + // Private because it assumes normalized inputs. + static SGQuat + fromRotateToNorm(const SGVec3& from, const SGVec3& to) + { + // To avoid instabilities with roundoff, we distinguish between rotations + // with more then 90deg and rotations with less than 90deg. + + // Compute the cosine of the angle. + T cosang = dot(from, to); + + // For the small ones do direct computation + if (T(-0.5) < cosang) + return SGQuat::fromRotateToSmaller90Deg(cosang, from, to); + + // For larger rotations. first rotate from to -from. + // Past that we will have a smaller angle again. + SGQuat q1 = SGQuat::fromChangeSign(from); + SGQuat q2 = SGQuat::fromRotateToSmaller90Deg(-cosang, -from, to); + return q1*q2; + } + + T _data[4]; }; /// Unary +, do nothing ... @@ -574,7 +689,7 @@ interpolate(T t, const SGQuat& src, const SGQuat& dst) // need the scales now, if the angle is very small, do linear interpolation // to avoid instabilities T scale0, scale1; - if (fabs(o) < SGLimits::epsilon()) { + if (fabs(o) <= SGLimits::epsilon()) { scale0 = 1 - t; scale1 = t; } else { @@ -605,4 +720,16 @@ SGQuatd toQuatd(const SGQuatf& v) { return SGQuatd(v(0), v(1), v(2), v(3)); } +#ifndef NO_OPENSCENEGRAPH_INTERFACE +inline +SGQuatd +toSG(const osg::Quat& q) +{ return SGQuatd(q[0], q[1], q[2], q[3]); } + +inline +osg::Quat +toOsg(const SGQuatd& q) +{ return osg::Quat(q[0], q[1], q[2], q[3]); } +#endif + #endif