VTK在python中渲染2D网格

时间:2018-06-17 22:51:07

标签: python mesh vtk

所以我试图使用vtk渲染2D网格(在python中)。我有一份清单 包含所有点的元组以及包含该元组的元组列表 每个细胞的点。只是为了实验,我试图创建一个polydata对象 一个有4个元素的正方形并渲染它,但我最终得到了这个:

square

我希望它显示连接节点的线条(如线框) 而不是实心方形.. 这是生成上图的代码:

def main2():

    #Array of vectors containing the coordinates of each point
    nodes = np.array([[0, 0, 0], [1, 0, 0], [2, 0, 0], [2, 1, 0], [2, 2, 0], 
                      [1, 2, 0], [0, 2, 0], [0, 1, 0], [1, 1, 0]])

    #Array of tuples containing the nodes correspondent of each element
    elements = np.array([(0, 1, 8, 7), (7, 8, 5, 6), (1, 2, 3, 8), (8, 3, 4, 
                        5)])

    #Make the building blocks of polyData attributes
    Mesh = vtk.vtkPolyData()
    Points = vtk.vtkPoints()
    Cells = vtk.vtkCellArray()  

    #Load the point and cell's attributes
    for i in range(len(nodes)):
        Points.InsertPoint(i, nodes[i])

    for i in range(len(elements)):
        Cells.InsertNextCell(mkVtkIdList(elements[i]))

    #Assign pieces to vtkPolyData
    Mesh.SetPoints(Points)
    Mesh.SetPolys(Cells)

    #Mapping the whole thing
    MeshMapper = vtk.vtkPolyDataMapper()
    if vtk.VTK_MAJOR_VERSION <= 5:
        MeshMapper.SetInput(Mesh)
    else:
        MeshMapper.SetInputData(Mesh)

    #Create an actor
    MeshActor = vtk.vtkActor()
    MeshActor.SetMapper(MeshMapper)

    #Rendering Stuff
    camera = vtk.vtkCamera()
    camera.SetPosition(1,1,1)
    camera.SetFocalPoint(0,0,0)

    renderer = vtk.vtkRenderer()
    renWin   = vtk.vtkRenderWindow()
    renWin.AddRenderer(renderer)

    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    renderer.AddActor(MeshActor)
    renderer.SetActiveCamera(camera)
    renderer.ResetCamera()
    renderer.SetBackground(1,1,1)

    renWin.SetSize(300,300)

    #Interact with data
    renWin.Render()
    iren.Start()


main2()

我还想知道是否可以使用网格线 渲染窗口的背景,而不是黑色,就像这样:

gridline

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以使用MeshActor.GetProperty()。SetRepresentationToWireframe()(https://www.vtk.org/doc/nightly/html/classvtkProperty.html#a2a4bdf2f46dc499ead4011024eddde5c)将actor渲染为线框,或者使用MeshActor.GetProperty()。SetEdgeVisibility(True)将其渲染为实体,边缘渲染为线条

关于渲染窗口背景,我不知道。

答案 1 :(得分:0)

感谢@MafiaSkafia,我创建了我正在寻找的东西,用于 3D 目的的 2D 网格,也许有人也会寻找这样的东西。

// Start import required js
import './node_modules/jquery/dist/jquery';
import './node_modules/bootstrap/dist/js/bootstrap.bundle';
import './node_modules/owl.carousel/dist/owl.carousel';
// End import required js

// Start import required css
import './node_modules/bootstrap/dist/css/bootstrap.min.css';
import './node_modules/owl.carousel/dist/assets/owl.carousel.min.css';
import './node_modules/owl.carousel/dist/assets/owl.theme.default.css';
// End import required css

enter image description here