在vtk python api中绘制多个平面

时间:2016-01-12 14:18:57

标签: python vtk volume-rendering

我正在尝试使用python中的vtk渲染室内场景,其中包括地板和墙壁以及像家具一样的盒子。我想到了如何在vtk中绘制单个平面(参见下面的代码)。但我无法弄清楚如何绘制多个平面。还有办法给这架飞机上色吗?

import vtk

'''VTK'''
planes = vtk.vtkPlaneSource()
planes.SetCenter(1.0, 0.0, 0.0)
planes.SetNormal(1.0, 0.0, 1.0)
# planes.SetExtent(1,100,1,100,7,7);


# mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(planes.GetOutput())

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

'''RENDER'''
renderer = vtk.vtkRenderer()
# Background
renderer.SetBackground(1., 1., 1.)
# Add actor to the scene
renderer.AddActor(actor)

'''Render window'''
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)

'''Interactor'''
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
renderWindowInteractor.SetRenderWindow(renderWindow)

'''Add camera coordinates'''
axes = vtk.vtkAxesActor()
widget = vtk.vtkOrientationMarkerWidget()
widget.SetOutlineColor(0.9300, 0.5700, 0.1300)
widget.SetOrientationMarker(axes)
widget.SetInteractor(renderWindowInteractor)
widget.SetViewport(0.0, 0.0, 0.4, 0.4)
widget.SetEnabled(1)
widget.InteractiveOn()

# Reset camera
renderer.ResetCamera()

'''Begin interaction'''
renderWindow.Render()
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()
renderWindowInteractor.Start()

2 个答案:

答案 0 :(得分:3)

我刚刚意识到它可以像下面这样轻松完成。

'''VTK'''
plane1 = vtk.vtkPlaneSource()
plane1.SetCenter(1.0, 0.0, 0.0)
plane1.SetNormal(1.0, 0.0, 0.0)
# planes.SetExtent(1,100,1,100,7,7);

'''VTK'''
plane2 = vtk.vtkPlaneSource()
plane2.SetCenter(1.0, 2.0, 0.0)
plane2.SetNormal(0.0, 1.0, 0.0)
# planes.SetExtent(1,100,1,100,7,7);

# mapper
mapper1 = vtk.vtkPolyDataMapper()
mapper1.SetInput(plane1.GetOutput())

# mapper
mapper2 = vtk.vtkPolyDataMapper()
mapper2.SetInput(plane2.GetOutput())

# actor
actor1 = vtk.vtkActor()
actor1.SetMapper(mapper1)

actor2 = vtk.vtkActor()
actor2.SetMapper(mapper2)


'''RENDER'''
renderer = vtk.vtkRenderer()
# Background
renderer.SetBackground(1., 1., 1.)
# Add actor to the scene
renderer.AddActor(actor1)
renderer.AddActor(actor2)

'''Render window'''
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)

'''Interactor'''
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
renderWindowInteractor.SetRenderWindow(renderWindow)

'''Add camera coordinates'''
axes = vtk.vtkAxesActor()
widget = vtk.vtkOrientationMarkerWidget()
widget.SetOutlineColor(0.9300, 0.5700, 0.1300)
widget.SetOrientationMarker(axes)
widget.SetInteractor(renderWindowInteractor)
widget.SetViewport(0.0, 0.0, 0.4, 0.4)
widget.SetEnabled(1)
widget.InteractiveOn()

# Reset camera
renderer.ResetCamera()

'''Begin interaction'''
renderWindow.Render()
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()
renderWindowInteractor.Start()

答案 1 :(得分:0)

您可能想签出PyVista。只需最少的代码,所有这些操作都变得非常简单:

import pyvista as pv

plane1 = pv.Plane(center=(1.0, 0.0, 0.0), 
                  direction=(1.0, 0.0, 0.0))
plane2 = pv.Plane(center=(1.0, 2.0, 0.0), 
                  direction=(0.0, 1.0, 0.0))

plotter = pv.Plotter()
plotter.add_mesh(plane1, color='blue')
plotter.add_mesh(plane2, color='red')
plotter.show_grid()
plotter.show()

enter image description here