vtk-py z-Buffer可见算法
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
示例用法:
自定义输入资源,左键旋转,右键显示当前可见点。
1 import vtk 2 3 # 自定义动作 4 class MyInteractor(vtk.vtkInteractorStyleTrackballCamera): 5 def __init__(self, parent=None): 6 self.AddObserver("RightButtonPressEvent", self.RightButtonPressEvent) 7 8 def SetVisibleFilter(self, vis): 9 self.VisibleFilter = vis 10 11 def RightButtonPressEvent(self, obj, event): 12 self.VisibleFilter.Update() 13 14 print("number of visible points: ", self.VisibleFilter.GetOutput().GetNumberOfPoints()) 15 16 mapper = vtk.vtkPolyDataMapper() 17 mapper.SetInputData(self.VisibleFilter.GetOutput()) 18 actor = vtk.vtkActor() 19 actor.SetMapper(mapper) 20 actor.GetProperty().SetPointSize(2) 21 actor.GetProperty().SetColor(1, 0, 0) 22 self.GetDefaultRenderer().AddActor(actor) 23 24 self.OnRightButtonDown() 25 return 26 27 28 def CreateScene(mesh): 29 renWin = vtk.vtkRenderWindow() 30 ren = vtk.vtkRenderer() 31 32 # 设置背景颜色 33 ren.GradientBackgroundOn() 34 ren.SetBackground(0.1, 0.1, 0.1) 35 ren.SetBackground2(0.8, 0.8, 0.8) 36 37 # 设置窗口大小 38 renWin.SetSize(600, 600) 39 renWin.AddRenderer(ren) 40 41 iren = vtk.vtkRenderWindowInteractor() 42 iren.SetRenderWindow(renWin) 43 44 # 添加自定义动作 45 style = MyInteractor() 46 style.SetDefaultRenderer(ren) 47 iren.SetInteractorStyle(style) 48 49 mapper = vtk.vtkPolyDataMapper() 50 mapper.SetInputData(mesh) 51 actor = vtk.vtkLODActor() 52 actor.SetMapper(mapper) 53 ren.AddActor(actor) 54 55 visPts = vtk.vtkSelectVisiblePoints() 56 visPts.SetInputData(mesh) 57 visPts.SetRenderer(ren) 58 style.SetVisibleFilter(visPts) 59 60 renWin.Render() 61 iren.Initialize() 62 iren.Start() 63 64 65 if __name__ == "__main__": 66 filename = "xxxx.obj" 67 reader = vtk.vtkOBJReader() 68 reader.SetFileName(filename) 69 reader.Update() 70 mesh = reader.GetOutput() 71 CreateScene(mesh)