C# 如何从矩阵X4计算角度XYZ

C# 如何从矩阵X4计算角度XYZ,c#,vector,matrix,angle,decomposition,C#,Vector,Matrix,Angle,Decomposition,我试图通过分解矩阵来确定矩阵中的X,Y,Z角。我使用的是.NET4.5C 我创建了一个测试来检查以下内容: 如果我创建一个仅具有标识值的矩阵x4x4 将矩阵旋转45度 分解矩阵并计算返回的四元数,给出x、y、z角 检查输出的X值是否与输入的45度相匹配 我得到以下结果: X:0.5 Y:0 Z:0 我期待着: X:0.45 Y:0 Z:0 测试代码 我已经创建了自己的Matrix4x4、Vector3D和Angle3D结构,如下面的示例所示 我的Matrix4绕x旋转方法如下: publ

我试图通过分解矩阵来确定矩阵中的X,Y,Z角。我使用的是.NET4.5C

我创建了一个测试来检查以下内容:

如果我创建一个仅具有标识值的矩阵x4x4 将矩阵旋转45度 分解矩阵并计算返回的四元数,给出x、y、z角 检查输出的X值是否与输入的45度相匹配 我得到以下结果: X:0.5 Y:0 Z:0

我期待着: X:0.45 Y:0 Z:0

测试代码

我已经创建了自己的Matrix4x4、Vector3D和Angle3D结构,如下面的示例所示

我的Matrix4绕x旋转方法如下:

    public static Matrix4x4 RotationAroundX(double degrees)
    {
        // [1, 0,  0,   0]
        // [0, cos,-sin,0]
        // [0, sin,cos, 0]
        // [0, 0,  0,   1]

        // convert degrees to radians.
        double radians = DoubleExtensions.DegreesToRadians(degrees);

        // return matrix.
        var matrixTransformed = Matrix4x4.Identity;

        matrixTransformed.M22 = (float)Math.Cos(radians);
        matrixTransformed.M23 = (float)-(Math.Sin(radians));

        matrixTransformed.M32 = (float)Math.Sin(radians);
        matrixTransformed.M33 = (float)Math.Cos(radians);

        //return matrix;
        return matrixTransformed;
    }
    public void DecomposeNoScaling(out Quaternion rotation, out Vector3D translation)
    {
        translation.X = this[1, 4];
        translation.Y = this[2, 4];
        translation.Z = this[3, 4];

        rotation = new Quaternion(new Matrix3x3(this));
    }
Angle3D angles = new Angle3D(quatDecomposed.X, quatDecomposed.Y, quatDecomposed.Z);
我的分解无缩放方法如下:

    public static Matrix4x4 RotationAroundX(double degrees)
    {
        // [1, 0,  0,   0]
        // [0, cos,-sin,0]
        // [0, sin,cos, 0]
        // [0, 0,  0,   1]

        // convert degrees to radians.
        double radians = DoubleExtensions.DegreesToRadians(degrees);

        // return matrix.
        var matrixTransformed = Matrix4x4.Identity;

        matrixTransformed.M22 = (float)Math.Cos(radians);
        matrixTransformed.M23 = (float)-(Math.Sin(radians));

        matrixTransformed.M32 = (float)Math.Sin(radians);
        matrixTransformed.M33 = (float)Math.Cos(radians);

        //return matrix;
        return matrixTransformed;
    }
    public void DecomposeNoScaling(out Quaternion rotation, out Vector3D translation)
    {
        translation.X = this[1, 4];
        translation.Y = this[2, 4];
        translation.Z = this[3, 4];

        rotation = new Quaternion(new Matrix3x3(this));
    }
Angle3D angles = new Angle3D(quatDecomposed.X, quatDecomposed.Y, quatDecomposed.Z);
我希望得到的是Matrix4x4中包含的角度,我这样做如下:

    public static Matrix4x4 RotationAroundX(double degrees)
    {
        // [1, 0,  0,   0]
        // [0, cos,-sin,0]
        // [0, sin,cos, 0]
        // [0, 0,  0,   1]

        // convert degrees to radians.
        double radians = DoubleExtensions.DegreesToRadians(degrees);

        // return matrix.
        var matrixTransformed = Matrix4x4.Identity;

        matrixTransformed.M22 = (float)Math.Cos(radians);
        matrixTransformed.M23 = (float)-(Math.Sin(radians));

        matrixTransformed.M32 = (float)Math.Sin(radians);
        matrixTransformed.M33 = (float)Math.Cos(radians);

        //return matrix;
        return matrixTransformed;
    }
    public void DecomposeNoScaling(out Quaternion rotation, out Vector3D translation)
    {
        translation.X = this[1, 4];
        translation.Y = this[2, 4];
        translation.Z = this[3, 4];

        rotation = new Quaternion(new Matrix3x3(this));
    }
Angle3D angles = new Angle3D(quatDecomposed.X, quatDecomposed.Y, quatDecomposed.Z);
谁能看出我做错了什么?我真正想计算的是矩阵x4x4中的欧拉角,按ZYX顺序


提前谢谢

矩阵的最后一行不应该是1吗

[10]

[0 cos-sin0]

[0 sin cos 0]

[01]


最后一行最后一列应该是1

以防万一其他人需要知道,这是我如何直接从矩阵中获得欧拉角的:

    public static Angle3D GetAngles(Matrix4x4 source)
    {
        double thetaX, thetaY, thetaZ = 0.0;
        thetaX = Math.Asin(source.M32);

        if (thetaX < (Math.PI / 2))
        {
            if (thetaX > (-Math.PI / 2))
            {
                thetaZ = Math.Atan2(-source.M12, source.M22);
                thetaY = Math.Atan2(-source.M31, source.M33);
            }
            else
            {
                thetaZ = -Math.Atan2(-source.M13, source.M11);
                thetaY = 0;
            }
        }
        else
        {
            thetaZ = Math.Atan2(source.M13, source.M11);
            thetaY = 0;
        }

        // Create return object.
        Angle3D angles = new Angle3D(thetaX, thetaY, thetaZ);

        // Convert to degrees.;
        angles.Format = AngleFormat.Degrees;

        // Return angles.
        return angles;
    }

抱歉,这是我评论中的一个错误!