【问题标题】:Acces vertex pos, normal and color in VTK from an OBJ import从 OBJ 导入访问 VTK 中的顶点位置、法线和颜色
【发布时间】:2017-04-13 16:27:28
【问题描述】:

我想在使用 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)

我需要能够做同样的事情,但使用顶点位置和法线(我也喜欢访问顶点颜色以将其用作过滤哪个顶点应该进行光线投射的遮罩) 我正在考虑类似的事情,但到目前为止还没有运气:S 非常感谢任何帮助;)

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

【问题讨论】:

    标签: python opengl vtk vertex


    【解决方案1】:

    我也为此苦苦挣扎。这是我在 C++ 中访问顶点和面的方法。我希望在 Python 中也是类似的——写 vtkSmartPointer 时,只关注 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 中自行计算它们。希望您能从这个答案中有所收获。

    【讨论】:

      猜你喜欢
      • 2012-12-18
      • 2015-10-29
      • 2020-12-16
      • 1970-01-01
      • 2016-02-16
      • 2015-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多