在python中从VTK文件中检索面和点 [英] Retrieving facets and point from VTK file in python

查看:58
本文介绍了在python中从VTK文件中检索面和点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3d 模型的 vtk 文件,

I have a vtk file containing a 3d model,

我想提取点坐标和面.

这是一个最小的工作示例:

Here is a minimal working example:

import vtk
import numpy
from vtk.util.numpy_support import vtk_to_numpy

reader = vtk.vtkPolyDataReader()
reader.SetFileName('test.vtk')
reader.Update()

polydata = reader.GetOutput()

points = polydata.GetPoints()
array = points.GetData()
numpy_nodes = vtk_to_numpy(array)

这是因为 numpy_nodes 包含所有点的 x、y、z 坐标,但我无法检索将该模型的各个方面与相应点相关联的列表.

This works as numpy_nodescontains the x,y,z coordinates of all points, but I am at loss to retrieve the list that relates the facets of this model to the corresponding points.

我试过了:

facets= polydata.GetPolys()
array = facets.GetData()
numpy_nodes = vtk_to_numpy(array)

但是 numpy_nodes 只是一个一维数组,我希望有一个二维数组(大小为 3* 面数),其中第一维包含面的对应点的数量(如.ply 文件).

But then numpy_nodes is just a 1D array where I would expect a 2D array (size 3*number of facets) where the first dimension contains the number of the corresponding points to the facet (as in a .ply file).

欢迎任何有关如何进行的建议

Any advise on how to proceed would be welcome

推荐答案

您就快到了.为了允许不同类型(三角形、四边形等)的单元格,numpy 数组使用以下方案对信息进行编码:

You were almost there. To allow cells of different types (triangles, quads, etc.), the numpy array encodes the information with the following scheme:

numpyArray = [ n_0, id_0(0), id_0(1), ..., id_0(n0-1), 
               n_1, id_1(0), id_1(1), ..., id_1(n1-1), 
               ... 
               n_i, id_i(0), id_i(1), ..., id_1(n1-1), 
               ...
              ]

如果所有多边形都属于同一类型,即所有 in_i==n,只需重塑一维数组以获得可解释的东西:

If all polys are of the same kind, that is n_i==n for all i, simply reshape the 1D array to get something interpretable:

cells = polydata.GetPolys()
nCells = cells.GetNumberOfCells()
array = cells.GetData()
# This holds true if all polys are of the same kind, e.g. triangles.
assert(array.GetNumberOfValues()%nCells==0)
nCols = array.GetNumberOfValues()//nCells
numpy_cells = vtk_to_numpy(array)
numpy_cells = numpy_cells.reshape((-1,nCols))

numpy_cells 的第一列可以删除,因为它只包含每个单元格的点数.但其余列包含您要查找的信息.

The first column of numpy_cells can be dropped, because it contains just the number of points per cell. But the remaining columns contain the information you were looking for.

为了确定结果,将输出与收集点 ID 的传统"方式进行比较:

To be sure about the result, compare the output with the "traditional" way to collect the point ids:

def getCellIds(polydata):
    cells = polydata.GetPolys()
    ids = []
    idList = vtk.vtkIdList()
    cells.InitTraversal()
    while cells.GetNextCell(idList):
        for i in range(0, idList.GetNumberOfIds()):
            pId = idList.GetId(i)
            ids.append(pId)
    ids = np.array(ids)
    return ids

numpy_cells2 = getCellIds(polydata).reshape((-1,3))

print(numpy_cells[:10,1:])
print(numpy_cells2[:10])
assert(np.array_equal(numpy_cells[:,1:], numpy_cells2))

这篇关于在python中从VTK文件中检索面和点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆