簡體   English   中英

VTK在python中渲染2D網格

[英]VTK rendering 2D mesh in python

所以我正在嘗試使用 vtk(在 python 中)渲染一個 2D 網格。 我有一個包含所有點的元組列表,還有一個包含每個單元格點的元組列表。 只是為了實驗,我嘗試創建一個包含 4 個元素的正方形的 polydata 對象並渲染它,但我最終得到了這個:

正方形

我希望它顯示連接節點的線(如線框)而不是實心正方形。這是生成上圖的代碼:

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()

我還想知道是否可以將網格線作為渲染窗口的背景,而不是黑色,就像這樣:

網格線

提前致謝!

您可以使用 MeshActor.GetProperty().SetRepresentationToWireframe() ( https://www.vtk.org/doc/nightly/html/classvtkProperty.html#a2a4bdf2f46dc499ead4011024eddde5c ) 將 actor 渲染為線框或 MeshActor()。 (True) 將其渲染為實心,邊緣渲染為線條。

關於渲染窗口背景,我不知道。

感謝@MafiaSkafia,我創建了我正在尋找的東西,用於 3D 目的的 2D 網格,也許有人也會尋找這樣的東西。

# plane
planeSource = vtk.vtkPlaneSource()
planeSource.SetOrigin(-100.0, -100.0, 0.0)
# planeSource.SetNormal(0.0, 0.0, 1.0)
planeSource.SetResolution(100,100)
planeSource.SetPoint1(100.0,-100.0,0.0)
planeSource.SetPoint2(-100.0,100.0,0.0)
planeSource.Update()

plane = planeSource.GetOutput()

# Create a mapper and actor
mapperP = vtk.vtkPolyDataMapper()
mapperP.SetInputData(plane)

actorP = vtk.vtkActor()
actorP.SetMapper(mapperP)
actorP.GetProperty().SetColor(0,0,0)
actorP.GetProperty().EdgeVisibilityOn() # showing mesh
actorP.GetProperty().SetEdgeColor(1,1,1)
actorP.GetProperty().SetOpacity(0.2) # transparency
...
renderer.AddActor(actorP)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM