使用python vtk 生成立体网格

效果:

 points = vtk.vtkPoints()
    cells = vtk.vtkCellArray()
    polydata = vtk.vtkPolyData()
    mapper = vtk.vtkPolyDataMapper()

    rangeX = [-10,10]
    rangeY = [-10,10]
    rangeZ = [-10,10]

    intervalX = 2
    intervalY = 2
    intervalZ = 2

    for gridZ in range(rangeZ[0],rangeZ[1] + intervalZ,intervalZ):
        for gridX in range(rangeX[0],rangeX[1] + intervalX,intervalX):
            lineStart = [gridX, rangeY[0],gridZ]
            lineEnd = [gridX,rangeY[1],gridZ]

            pointIdStart = points.InsertNextPoint(lineStart)
            pointIdEnd = points.InsertNextPoint(lineEnd)

            singleLineCell = [pointIdStart,pointIdEnd]
            cells.InsertNextCell(2,singleLineCell)

        for gridY in range(rangeY[0],rangeY[1] +intervalY,intervalY):
            lineStart = [gridY, rangeX[0],gridZ]
            lineEnd = [gridY,rangeX[1],gridZ]

            pointIdStart = points.InsertNextPoint(lineStart)
            pointIdEnd = points.InsertNextPoint(lineEnd)

            singleLineCell = [pointIdStart,pointIdEnd]
            cells.InsertNextCell(2,singleLineCell)

    for gridY in range(rangeY[0], rangeY[1] + intervalY , intervalY):
        for gridZ in range(rangeZ[0], rangeZ[1] + intervalZ, intervalZ):
            lineStart = [rangeX[0], gridY, gridZ]
            lineEnd = [rangeX[1], gridY, gridZ]

            pointIdStart = points.InsertNextPoint(lineStart)
            pointIdEnd = points.InsertNextPoint(lineEnd)

            singleLineCell = [pointIdStart, pointIdEnd]
            cells.InsertNextCell(2, singleLineCell)

        for gridX in range(rangeX[0], rangeX[1] +intervalX, intervalX):
            lineStart = [gridX, gridY, rangeZ[0]]
            lineEnd = [gridX, gridY, rangeZ[1]]

            pointIdStart = points.InsertNextPoint(lineStart)
            pointIdEnd = points.InsertNextPoint(lineEnd)

            singleLineCell = [pointIdStart, pointIdEnd]
            cells.InsertNextCell(2, singleLineCell)

    polydata.SetLines(cells)
    polydata.SetPoints(points)
    mapper.SetInputData(polydata)

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)

    ren1 = vtk.vtkRenderer()
    ren1.AddActor(actor)
    ren1.SetBackground(0.1,0.2,0.4)

    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren1);
    renWin.SetSize(300, 300);

    #enderWindowInteractor
    iren = vtk.vtkRenderWindowInteractor();
    iren.SetRenderWindow(renWin);
    style = vtk.vtkInteractorStyleTrackballCamera();
    iren.SetInteractorStyle(style);

    renWin.SetSize(600, 600);
    renWin.Render();
    iren.Initialize()
    iren.Start()

  

 代码:

posted @ 2022-04-01 19:35  丢了木剑的温华  阅读(770)  评论(0编辑  收藏  举报