繁体   English   中英

从matrix4d提取旋转

[英]Extract rotation from matrix4d

我从某些软件获得一个转换矩阵,并且我必须提取轴旋转。 我不确定确切的名称,但我想使用Matrix4d.rotX(a)的相反功能。

假设我有以下代码:

Matrix4d matrix = new Matrix4d();
matrix.setIdentity();
Matrix4d m = new Matrix4d();
m.rotX(a);
matrix.mul(m);
m.rotY(b);
matrix.mul(m);
m.rotZ(c);
matrix.mul(m);

我希望能够从矩阵中获取值a,b和c。 有人有想法吗? 不幸的是,我几乎没有3D方面的经验,我调查了Java vecmath软件包,但找不到任何东西。

我尝试了以下方法:

 public static float[] getRotation(Matrix4f matrix) {
        float Yaw;
        float Pitch;
        float Roll;
        if (Math.abs(matrix.m00 + 1.0f) < 0.01f )
        {
            Yaw = (float) Math.atan2(matrix.m02, matrix.m23);
            Pitch = 0;
            Roll = 0;

        }else if (Math.abs(matrix.m00 -1.0f) < 0.01f)
        {
            Yaw = (float) Math.atan2(matrix.m02, matrix.m23);
            Pitch = 0;
            Roll = 0;
        }else
        {

            Yaw = (float) Math.atan2(-matrix.m20,matrix.m00);
            Pitch = (float) Math.asin(matrix.m10);
            Roll = (float) Math.atan2(-matrix.m12,matrix.m11);
        }
        return new float[] { Yaw, Pitch, Roll};
    }

public static float[] GetRotation(Matrix4f matrix) {
        float Yaw;
        float Pitch;
        float Roll;
        if (matrix.m00 == 1.0f)
        {
            Yaw = (float) Math.atan2(matrix.m20, matrix.m32);
            Pitch = 0;
            Roll = 0;

        }else if (matrix.m00 == -1.0f)
        {
            Yaw = (float) Math.atan2(matrix.m20, matrix.m32);
            Pitch = 0;
            Roll = 0;
        }else
        {

            Yaw = (float) Math.atan2(-matrix.m02,matrix.m00);
            Pitch = (float) Math.asin(matrix.m01);
            Roll = (float) Math.atan2(-matrix.m21,matrix.m11);
        }
        return new float[] { Yaw, Pitch, Roll};
    }

但值是错误的

我找到了答案,这是使其工作的代码:

    public static float[] getRotation(Matrix4f matrix) {
            float x,y,z;
            Matrix3f m = new Matrix3f();

            matrix.get(m);
            if(Math.abs(m.m02 - 1) < 0.0000001) {
                x = (float) Math.atan2(-m.m10,m.m11);
                y = (float) (-3.1415926535897931/2);
                z = 0.0f;
            } else if(Math.abs(m.m02 + 1)< 0.0000001) {
                x = (float) Math.atan2(m.m10,m.m11);
                y = (float) (3.1415926535897931/2);
                z = 0.0f;
            } else {
                x = (float) Math.atan2(m.m12,m.m22);
                y = (float) Math.atan2(-m.m02, Math.sqrt(m.m12 * m.m12 + m.m22 * m.m22));
                z = (float) Math.atan2(m.m01,m.m00);
            }

            return new float[] {x,y,z};
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM