gpt4 book ai didi

python - VTK在python中渲染2D网格

转载 作者:行者123 更新时间:2023-12-01 09:19:40 25 4
gpt4 key购买 nike

所以我尝试使用vtk(在python中)渲染2D网格。我有一个 list 包含所有点的元组以及包含每个单元格的点。只是为了实验,我尝试创建一个多数据对象一个有 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

提前致谢!

最佳答案

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

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

关于python - VTK在python中渲染2D网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50901094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com