简体   繁体   English

从python中的VTK文件中检索facet和point

[英]Retrieving facets and point from VTK file in python

I have a vtk file containing a 3d model, 我有一个包含3d模型的vtk文件,

I would like to extract the point coordinates and the facets. 我想提取点坐标和刻面。

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)

This works as numpy_nodes contains 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. 这可以作为numpy_nodes包含所有点的x,y,z坐标,但我无法检索将此模型的方面与相应点相关联的列表。

I tried: 我试过了:

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

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). 但是numpy_nodes只是一个一维数组,我期望一个二维数组(大小为3 *小平面的数量),其中第一个维度包含facet对应点的数量(如.ply文件中)。

Any advise on how to proceed would be welcome 任何有关如何进行的建议都将受到欢迎

You were almost there. 你快到了。 To allow cells of different types (triangles, quads, etc.), the numpy array encodes the information with the following scheme: 为了允许不同类型的单元格(三角形,四边形等),numpy数组使用以下方案对信息进行编码:

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), 
               ...
              ]

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: 如果所有多边形都是同一类型,对于所有i来说都是n_i==n ,只需重新整形1D数组以获得可解释的内容:

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))

The first column of numpy_cells can be dropped, because it contains just the number of points per cell. 可以删除numpy_cells的第一列,因为它只包含每个单元格的点数。 But the remaining columns contain the information you were looking for. 但其余列包含您要查找的信息。

To be sure about the result, compare the output with the "traditional" way to collect the point ids: 为了确保结果,请将输出与“传统”方法进行比较以收集点ID:

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))

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

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