在 vtk 中绘制多个平面 python api
Draw multiple planes in vtk python api
我正在尝试使用 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()
我刚刚意识到它可以像下面显示的那样简单地完成。
'''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()
您可能想看看 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()
我正在尝试使用 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()
我刚刚意识到它可以像下面显示的那样简单地完成。
'''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()
您可能想看看 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()