从OBJ imp访问VTK中的顶点位置、法线和颜色

2024-07-08 16:17:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

到目前为止,我所做的是创建一个vtkCellCenters对象并从中检索法线和点来进行光线投射,但这不是我真正想要的。。。在

以下是如何访问“细胞中心”和“法线”以启动光线投射:

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)

我需要能够做同样的事情,但顶点位置和法线(我也喜欢访问顶点颜色,将其用作过滤哪个顶点应执行光线投射的遮罩) 我在想这样的事情,但目前还没有运气:非常感谢任何帮助;)

^{pr2}$

Tags: theobj网格transformreader光线scalevtk
1条回答
网友
1楼 · 发布于 2024-07-08 16:17:24

我也在挣扎。这里是我如何访问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来计算它们。希望你能从这个答案中有所收获。在

相关问题 更多 >

    热门问题