从OBJ导入中获取VTK中的顶点pos,normal和color

时间:2017-04-03 21:10:19

标签: python opengl vtk vertex

我想在使用vtkOBJReader导入的两个OBJ网格之间进行光线投射。我想从顶点到第二个网格的法线方向做一个光线投射。

但是我不知道如何从第一个网格访问顶点(和参数)。我对vtk中的单元格和点以及过滤器的概念感到有点困惑。

到目前为止,我设法做的是创建一个vtkCellCenters对象并检索正常并指向它以进行我的光线投射,但这不是我想要的......

以下是我如何访问单元格中心和正常启动我的rayCast:

import vtk

OBJ_SCALE = 100.
ColorBackground = [0.0, 0.0, 0.0]
FirstobjPath = r"...my Path to the first OBJ file..."

reader = vtk.vtkOBJReader()
reader.SetFileName(FirstobjPath)

# I scale up object for better precision
transform = vtk.vtkTransform()
transform.Scale(OBJ_SCALE, OBJ_SCALE, OBJ_SCALE)
transformPData = vtk.vtkTransformPolyDataFilter()
transformPData.SetTransform(transform)
transformPData.SetInputConnection(reader.GetOutputPort())

# I transform poly to triangle for proper indexing
triangles = vtk.vtkTriangleFilter()
triangles.SetInputConnection(transformPData.GetOutputPort())

# Here is how I get my cell data
cellCenterCalc = vtk.vtkCellCenters()
cellCenterCalc.SetInputConnection(triangles.GetOutputPort())
cellCenterCalc.Update()

# I can get the point center with this
pointsCellCenters = cellCenterCalc.GetOutput(0)
# and the normals with this
normalsCalcScan = vtk.vtkPolyDataNormals()
normalsCalcScan.SetInputConnection(triangles.GetOutputPort())


mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(triangles.GetOutputPort())   
actor = vtk.vtkActor()
actor.SetMapper(mapper)
ren = vtk.vtkRenderer()
ren.SetBackground(ColorBackground)
ren.AddActor(actor)

我需要能够做同样的事情但是使用顶点pos和法线(我也喜欢访问顶点颜色以将其用作过滤哪个顶点应该进行光线投射的掩码) 我在想这样的事情,但到目前为止还没有运气:任何帮助都非常感激;)

polyData = triangles.GetOutput()
polyData.GetCellData().GetScalars("Colors")

1 个答案:

答案 0 :(得分:0)

我也很挣扎。以下是我在C ++中访问顶点和面的方法。我希望在Python中它类似 - 当写成 vtkSmartPointer< vtkXXXX> ,只关注 vtkXXXX

void accessEachVertex( const vtkSmartPointer<vtkPolyData>& mesh )
{   
    vtkSmartPointer<vtkPoints> vertices = mesh->GetPoints();
    vtkSmartPointer<vtkDataArray> verticesArray = vertices->GetData();

    long long numberOfVertices = vertices->GetNumberOfPoints();

    // access 3D coordinate [x, y, z] of each vertex 
    for( int i = 0; i < numberOfVertices; i++ )
    {
        float x = verticesArray->GetComponent(i, 0);
        float y = verticesArray->GetComponent(i, 1);
        float z = verticesArray->GetComponent(i, 2);

        ....
    }
}

void accessEachFace( const vtkSmartPointer<vtkPolyData>& mesh )
{
    int  numberOfFaces = mesh->GetNumberOfCells();

    // acces each mesh face. A face is defined by the indices 
    // of the participating vertices.
    // this mesh has triangle faces - therefore three vertices.
    for( int i = 0; i < numberOfFaces; i++)
    {
        vtkSmartPointer<vtkIdList> face = vtkSmartPointer<vtkIdList>::New();
        mesh->GetCellPoints(i,face);
        int v0Idx = face->GetId(0);
        int v1Idx = face->GetId(1);
        int v2Idx = face->GetId(2);

        // see the accessEachVertex function
        // to read the coordinate of vertex v0Idx,
        // v1Idx or v2Idx
    }
}

以下是example他们如何访问不同类型的法线。老实说,我对vtk中的法线有些麻烦。在某些情况下,他们只是缺席或更有可能,我无法找到他们:)无论如何,我在computeVertexNormalsTrivial计算他们自己。希望你能从这个答案中得到一些东西。