You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.0 KiB
Python
110 lines
3.0 KiB
Python
import vtk
|
|
|
|
|
|
def renderOne():
|
|
|
|
# 创建一个立方体的源对象
|
|
cubeSource = vtk.vtkCubeSource()
|
|
cubeSource.SetXLength(10.0) # 设置立方体的X轴长度
|
|
cubeSource.SetYLength(10.0) # 设置立方体的Y轴长度
|
|
cubeSource.SetZLength(10.0) # 设置立方体的Z轴长度
|
|
cubeSource.Update() # 更新立方体源以生成数据
|
|
|
|
# 创建一个多边形映射器
|
|
mapper = vtk.vtkPolyDataMapper()
|
|
mapper.SetInputConnection(
|
|
cubeSource.GetOutputPort()
|
|
) # 设置映射器的输入为立方体源的输出
|
|
|
|
# 创建一个立方体actor
|
|
cubeActor = vtk.vtkActor()
|
|
cubeActor.SetMapper(mapper) # 设置立方体actor的映射器
|
|
|
|
# 创建一个渲染器、一个窗口和一个渲染器集合
|
|
ren = vtk.vtkRenderer()
|
|
renWin = vtk.vtkRenderWindow()
|
|
renWin.AddRenderer(ren)
|
|
iren = vtk.vtkRenderWindowInteractor()
|
|
iren.SetRenderWindow(renWin)
|
|
|
|
# 将立方体actor添加到渲染器
|
|
ren.AddActor(cubeActor)
|
|
ren.SetBackground(0, 0, 0) # 设置渲染器的背景颜色
|
|
|
|
# 渲染窗口大小并显示
|
|
renWin.SetSize(300, 300)
|
|
renWin.Render()
|
|
|
|
# 启动交互器
|
|
iren.Initialize()
|
|
iren.Start()
|
|
|
|
|
|
def renderTwo():
|
|
import vtk
|
|
|
|
# 创建一个点集合
|
|
points = vtk.vtkPoints()
|
|
points.InsertPoint(0, 0, 0, 0)
|
|
points.InsertPoint(1, 1, 0, 0)
|
|
points.InsertPoint(2, 1, 1, 0)
|
|
points.InsertPoint(3, 0, 1, 0)
|
|
points.InsertPoint(4, 0, 0, 1)
|
|
points.InsertPoint(5, 1, 0, 1)
|
|
points.InsertPoint(6, 1, 1, 1)
|
|
points.InsertPoint(7, 0, 1, 1)
|
|
|
|
# 创建一个单元数组,定义单元格
|
|
polys = vtk.vtkCellArray()
|
|
polys.InsertNextCell(4)
|
|
polys.InsertCellPoint(0)
|
|
polys.InsertCellPoint(1)
|
|
polys.InsertCellPoint(3)
|
|
polys.InsertCellPoint(2)
|
|
polys.InsertNextCell(4)
|
|
polys.InsertCellPoint(4)
|
|
polys.InsertCellPoint(5)
|
|
polys.InsertCellPoint(7)
|
|
polys.InsertCellPoint(6)
|
|
polys.InsertNextCell(4)
|
|
polys.InsertCellPoint(0)
|
|
polys.InsertCellPoint(4)
|
|
polys.InsertCellPoint(6)
|
|
polys.InsertCellPoint(2)
|
|
polys.InsertNextCell(4)
|
|
polys.InsertCellPoint(1)
|
|
polys.InsertCellPoint(5)
|
|
polys.InsertCellPoint(7)
|
|
polys.InsertCellPoint(3)
|
|
|
|
# 创建一个点集合和单元格数组的3D网格
|
|
grid = vtk.vtkUnstructuredGrid()
|
|
grid.SetPoints(points)
|
|
grid.SetPolys(polys)
|
|
|
|
# 创建一个映射器和actor
|
|
mapper = vtk.vtkDataSetMapper()
|
|
mapper.SetInputData(grid)
|
|
|
|
actor = vtk.vtkActor()
|
|
actor.SetMapper(mapper)
|
|
|
|
# 创建一个渲染器、相机和窗口
|
|
renderer = vtk.vtkRenderer()
|
|
renderWindow = vtk.vtkRenderWindow()
|
|
renderWindow.AddRenderer(renderer)
|
|
|
|
interactor = vtk.vtkRenderWindowInteractor()
|
|
interactor.SetRenderWindow(renderWindow)
|
|
|
|
# 添加actor到渲染器并设置视图
|
|
renderer.AddActor(actor)
|
|
renderer.SetBackground(0.3, 0.3, 0.3)
|
|
|
|
# 渲染窗口并启动事件循环
|
|
renderWindow.Render()
|
|
interactor.Start()
|
|
|
|
|
|
renderTwo()
|