如何从网格中删除面的列表及其顶点

时间:2017-07-26 15:23:42

标签: c# c++ algorithm geometry mesh

我正在尝试从网格中删除面部列表及其各自的顶点(如果它们不再被引用),但到目前为止,我只能成功删除面部列表。我们的想法是,对于每个被删除的索引,所有大于删除索引的索引都应该递减1.

网格简单地由顶点列表和索引列表(以及可选的三角形列表)表示。

目前只删除三角形的代码就是这样:

    private IMesh DeleteTriangles(IMesh input, IEnumerable<int> indices_to_delete)
    {
        var triangles_to_delete = new HashSet<int>(indices_to_delete);

        _triangles_to_keep = _mesh.Triangles.Where((t, count) =>
            !triangles_to_delete.Contains(t.A) &&
            !triangles_to_delete.Contains(t.B) &&
            !triangles_to_delete.Contains(t.C)).ToList();

       // return new mesh
    }

1 个答案:

答案 0 :(得分:0)

我最终提出的解决方案:

    public IMesh DeleteVertices(IMesh mesh, IImmutableList<int> indices_to_remove)
    {
        var indices_to_remove_set = new HashSet<int>(indices_to_remove);
        var new_vertices = new List<Vector>();
        var map = new int[mesh.Vertices.Count];

        for (var i = 0; i < mesh.Vertices.Count; i++)
        {
            if (indices_to_remove_set.Contains(i))
            {
                map[i] = -1;
            }
            else
            {
                new_vertices.Add(mesh.Vertices[i]);
                map[i] = new_vertices.Count - 1;
            }
        }

        var new_triangle_list = from triangle in mesh.Triangles
                                where map[triangle.A] != -1 &&
                                      map[triangle.B] != -1 &&
                                      map[triangle.C] != -1
                                select new IndexTriangle(
                                    map[triangle.A],
                                    map[triangle.B],
                                    map[triangle.C]);

        //build new mesh...
    }