vtk-py z-Buffer可见算法
阅读原文时间:2023年07月09日阅读:2

C++版例子:

https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/SelectVisiblePoints/

优点:

  • Simple to use
  • Can be implemented easily in object or image sapce
  • Can be executed quickly, even with many polygons

缺点

  • Takes up a lot of memory
  • Can't do transparent surfaces without additional code

示例用法:

自定义输入资源,左键旋转,右键显示当前可见点。

import vtk

# 自定义动作
class MyInteractor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, parent=None):
self.AddObserver("RightButtonPressEvent", self.RightButtonPressEvent)

 def SetVisibleFilter(self, vis):  
     self.VisibleFilter = vis

 def RightButtonPressEvent(self, obj, event):  
     self.VisibleFilter.Update()

     print("number of visible points: ", self.VisibleFilter.GetOutput().GetNumberOfPoints())

     mapper = vtk.vtkPolyDataMapper()  
     mapper.SetInputData(self.VisibleFilter.GetOutput())  
     actor = vtk.vtkActor()  
     actor.SetMapper(mapper)  
     actor.GetProperty().SetPointSize(2)  
     actor.GetProperty().SetColor(1, 0, 0)  
     self.GetDefaultRenderer().AddActor(actor)

     self.OnRightButtonDown()  
     return

def CreateScene(mesh):
renWin = vtk.vtkRenderWindow()
ren = vtk.vtkRenderer()

 # 设置背景颜色  
 ren.GradientBackgroundOn()  
 ren.SetBackground(0.1, 0.1, 0.1)  
 ren.SetBackground2(0.8, 0.8, 0.8)

 # 设置窗口大小  
 renWin.SetSize(600, 600)  
 renWin.AddRenderer(ren)

 iren = vtk.vtkRenderWindowInteractor()  
 iren.SetRenderWindow(renWin)

 # 添加自定义动作  
 style = MyInteractor()  
 style.SetDefaultRenderer(ren)  
 iren.SetInteractorStyle(style)

 mapper = vtk.vtkPolyDataMapper()  
 mapper.SetInputData(mesh)  
 actor = vtk.vtkLODActor()  
 actor.SetMapper(mapper)  
 ren.AddActor(actor)

 visPts = vtk.vtkSelectVisiblePoints()  
 visPts.SetInputData(mesh)  
 visPts.SetRenderer(ren)  
 style.SetVisibleFilter(visPts)

 renWin.Render()  
 iren.Initialize()  
 iren.Start()

if __name__ == "__main__":
filename = "xxxx.obj"
reader = vtk.vtkOBJReader()
reader.SetFileName(filename)
reader.Update()
mesh = reader.GetOutput()
CreateScene(mesh)

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章