创建反向旋转矩阵的问题

时间:2016-08-07 13:55:40

标签: c# matrix rotation algebra

我需要对3D多边形(一组平面有序顶点)进行三角剖分。为此,我将多边形旋转到X / Z平面,并对旋转的顶点应用耳剪裁算法。之后,我将顶点旋转回来并有一个三角形多边形。 但是,我的轮换似乎有误。 要将我的多边形旋转到X / Z平面,请执行以下操作:

首先,我取多边形的法线向量并从中创建一个反向旋转矩阵

    internal static Matrix4 CreateInverseRotationMatrix(Vector3 up) // up is normal vector of polygon
    {
        Vector3 right;
        // take the longer axis and calculate a normal vector
        if (up.X.AbsoluteValue > up.Z.AbsoluteValue)
            right = up.Cross(new Vector3(0, 0, 1));
        else
            right = up.Cross(new Vector3(1, 0, 0));

        // create the third vector of the matrix
        var backward = right.Cross(up);
        // create inverse rotation matrix
        Matrix4<Rational> m = new Matrix4<Rational>(right.X, right.Y, right.Z, 0, up.X, up.Y, up.Z, 0, backward.X, backward.Y, backward.Z, 0, 0, 0, 0, 1);
        return m;
    }

之后,我将每个顶点与反向旋转矩阵相乘:

    private List<Vector2> CreateRotatedVertexList(List<Vertex3> contourVertices, Vector3 up)
    {
        var matrix = Matrix4.CreateInverseRotationMatrix(up);
        List<Vector2> points = new List<Vector2>();
        foreach (var vertex in contourVertices)
        {
            var rotatedVertex = Matrix4.Multiply(matrix, vertex.Vector3);
            var point = new Vector2m(rotatedVertex.X, rotatedVertex.Z);
            points.Add(point);
        }

        return points;
    }

Matrix4.Multiply看起来像这样:

    public static Vector3 Multiply(Matrix4 m, Vector3 vec4)
    {
        Vector3 res = Vector3.Zero();
        res.X = m.M11 * vec4.X + m.M12 * vec4.Y + m.M13 * vec4.Z + m.M14;
        res.Y = m.M21 * vec4.X + m.M22 * vec4.Y + m.M23 * vec4.Z + m.M24;
        res.Z = m.M31 * vec4.X + m.M32 * vec4.Y + m.M33 * vec4.Z + m.M34;
        var w = m.M41*vec4.X + m.M42*vec4.Y + m.M43*vec4.Z + m.M44;
        return res;
    }

我在Blender中绘制了一个示例多边形。例如,多边形P = {( - 1 1 1),( - 0.5 1 0.5),(0 1 0),(0.5 0.5 0),(1 0 0),(1 -0.5 0.5),(1 - 1 1)}

enter image description here

旋转的多边形PR = {(0 1 -4)(0,5 1 -2,5)(1 1 -1)(0,5 1 0,5)(0 1 2)( - 1 1 2)(-2 1 2)}。

enter image description here

旋转顶点的y分量都是1,因此多边形位于X / Z平面,这是正确的。但是,与未旋转的多边形相比,旋转多边形的形状看起来明显不同,我不知道为什么。

0 个答案:

没有答案