Why are the values of Euler Angle and quaternion conversion different?

classic Classic list List threaded Threaded
2 messages Options
Mirro187 Mirro187
Reply | Threaded
Open this post in threaded view
|

Why are the values of Euler Angle and quaternion conversion different?

//Why is this 0,90,0 converted to be 180,90,180 by QuatToHPR function?What's wrong with the QuatToHPR function?

osg::Quat roaQuat = SceneGeoTool::getSceneGeoToolHandler()->HPRToQuat(0,90,0);
roaQuat {_v=0x0000000000145bc8 {-0.00000000000000000, -0.70710678118654757, -0.00000000000000000, -0.70710678118654757} } osg::Quat
//
double roll = 0.0, pitch = 0.0, yaw = 0.0;
SceneGeoTool::getSceneGeoToolHandler()->QuatToHPR(roaQuat,roll, pitch, yaw);
roll 180.00000000000000 double
pitch 90.000000000000000 double
yaw 180.00000000000000 double

osg::Quat SceneGeoTool::HPRToQuat(const double& roll, const double& pitch, const double& yaw, bool isReversal)
{
        double temp_yaw   = osg::DegreesToRadians(yaw);
        double temp_pitch = osg::DegreesToRadians(pitch);
        double temp_roll  = osg::DegreesToRadians(roll);
        osg::Quat res;
        // Abbreviations for the various angular functions
        double cy = cos(temp_yaw * 0.5);
        double sy = sin(temp_yaw * 0.5);
        double cp = cos(temp_pitch * 0.5);
        double sp = sin(temp_pitch * 0.5);
        double cr = cos(temp_roll * 0.5);
        double sr = sin(temp_roll * 0.5);
        //
        res.w() = cr * cp * cy + sr * sp * sy;
        res.x() = sr * cp * cy - cr * sp * sy;
        res.y() = cr * sp * cy + sr * cp * sy;
        res.z() = cr * cp * sy - sr * sp * cy;
        //旋转180°
        if (isReversal)
        {
                res *= -1;
        }
        return res;
}

void SceneGeoTool::QuatToHPR(osg::Quat q, double& roll, double& pitch, double& yaw)
{
        // roll (x-axis rotation)
        double sinr_cosp = 2 * (q.w() * q.x() + q.y() * q.z());
        double cosr_cosp = 1 - 2 * (q.x() * q.x() + q.y() * q.y());
        roll = osg::RadiansToDegrees(std::atan2(sinr_cosp, cosr_cosp));

        // pitch (y-axis rotation)
        double sinp = 2 * (q.w() * q.y() - q.z() * q.x());
        if (std::abs(sinp) >= 1)
                pitch = osg::RadiansToDegrees(std::copysign(osg::PI / 2, sinp)); // use 90 degrees if out of range
        else
                pitch = osg::RadiansToDegrees(std::asin(sinp));

        // yaw (z-axis rotation)
        double siny_cosp = 2 * (q.w() * q.z() + q.x() * q.y());
        double cosy_cosp = 1 - 2 * (q.y() * q.y() + q.z() * q.z());
        yaw = osg::RadiansToDegrees(std::atan2(siny_cosp, cosy_cosp));
}
liugaijin liugaijin
Reply | Threaded
Open this post in threaded view
|

Re: Why are the values of Euler Angle and quaternion conversion different?

I don not know why. I found some codes as yours in different website, got the same differency.
finally In my codes, I use the Eigen Matrix Library.
you can try it.