python vtk 画线_如何使用vtk选择线中的点?

importvtk,os,sysimportnumpyasnpfromPyQt5.QtWidgetsimport*fromvtk.qt.QVTKRenderWindowInteractorimportQVTKRenderWindowInteractorfromvtk.util.numpy_supportimportvtk_to_numpy,numpy_to_vtkdefnumpyToVtk(data,type=vtk.VTK_FLOAT):flat_data_array=data.transpose(2,1,0).flatten()vtk_data_array=numpy_to_vtk(flat_data_array)vtk_data=numpy_to_vtk(num_array=vtk_data_array,deep=True,array_type=type)img=vtk.vtkImageData()img.GetPointData().SetScalars(vtk_data)img.SetDimensions(data.shape)img.SetOrigin(0,0,0)img.SetSpacing(1,1,1)returnimgclassourInteractor(vtk.vtkInteractorStyleTrackballCamera):def__init__(self,renderer=None,renWindow=None):super(ourInteractor,self).__init__()self.AddObserver("RightButtonReleaseEvent",self.OnRightButtonUp)self.ren=renderer

self.renWin=renWindowdefOnRightButtonUp(self,obj,event):super(ourInteractor,self).OnRightButtonUp()pos=self.GetInteractor().GetEventPosition()coordinate=vtk.vtkCoordinate()coordinate.SetCoordinateSystemToDisplay()coordinate.SetValue(pos[0],pos[1],0)worldCoor=coordinate.GetComputedWorldValue(self.GetInteractor().GetRenderWindow().GetRenderers().GetFirstRenderer())print('screen coor: ',pos,'world coor: ',worldCoor)points=vtk.vtkPoints()vertices=vtk.vtkCellArray()id=points.InsertNextPoint(worldCoor[0],worldCoor[1],worldCoor[2])vertices.InsertNextCell(1)vertices.InsertCellPoint(id)point=vtk.vtkPolyData()point.SetPoints(points)point.SetVerts(vertices)mapper=vtk.vtkPolyDataMapper()mapper.SetInputData(point)actor=vtk.vtkActor()actor.SetMapper(mapper)actor.GetProperty().SetPointSize(10)actor.GetProperty().SetColor(0,1,0)self.ren.AddActor(actor)self.renWin.Render()classAirwaySkeleton(QMainWindow):def__init__(self,parent=None):super(AirwaySkeleton,self).__init__(parent=parent)self.setWindowTitle("Airway Skeleton")widget=QWidget()self.setCentralWidget(widget)layout=QHBoxLayout()layout.setContentsMargins(0,0,0,0)widget.setLayout(layout)self.mainLayout=layout

frame=QFrame()vtkWidget=QVTKRenderWindowInteractor(frame)self.mainLayout.addWidget(vtkWidget)ren=vtk.vtkRenderer()vtkWidget.GetRenderWindow().AddRenderer(ren)iren=vtkWidget.GetRenderWindow().GetInteractor()style=ourInteractor(renderer=ren,renWindow=vtkWidget.GetRenderWindow())iren.SetInteractorStyle(style)ren.SetBackground(0,0,0)self.ren=ren

mask=np.zeros(shape=[200,200,200],dtype=np.uint8)mask[20:80,50:150,50:150]=1mask[80:150,80:120,80:120]=1mask[150:170,50:150,50:150]=1xs=np.arange(20,170,0.1)line=[]forxinxs:line.append([x,100,100])actors=self.createActorsForLines([np.array(line)])vtkMask=numpyToVtk(data=mask,type=vtk.VTK_CHAR)mesh=self.maskToMesh(vtkMask)mapper=vtk.vtkPolyDataMapper()mapper.SetInputConnection(mesh.GetOutputPort())mapper.ScalarVisibilityOff()actor=vtk.vtkLODActor()actor.SetMapper(mapper)actor.GetProperty().SetColor(1,1,1)actor.GetProperty().SetOpacity(0.4)self.ren.AddActor(actor)forlineActorinactors:self.ren.AddActor(lineActor)self.renWin=vtkWidget.GetRenderWindow()iren.Initialize()self.iren=irendefmaskToMesh(self,mask):contour=vtk.vtkDiscreteMarchingCubes()contour.SetInputData(mask)contour.SetValue(0,1)contour.Update()smoother=vtk.vtkWindowedSincPolyDataFilter()smoother.SetInputConnection(contour.GetOutputPort())smoother.SetNumberOfIterations(30)smoother.BoundarySmoothingOff()smoother.NonManifoldSmoothingOn()smoother.NormalizeCoordinatesOn()smoother.Update()triangleCellNormals=vtk.vtkPolyDataNormals()triangleCellNormals.SetInputConnection(smoother.GetOutputPort())triangleCellNormals.ComputeCellNormalsOn()triangleCellNormals.ComputePointNormalsOff()triangleCellNormals.ConsistencyOn()triangleCellNormals.AutoOrientNormalsOn()triangleCellNormals.Update()returntriangleCellNormalsdefcreateActorsForLines(self,lines):actors=[]endPoints=vtk.vtkPoints()forlineinlines:n=line.shape[0]endPoints.InsertNextPoint(line[0,0],line[0,1],line[0,2])endPoints.InsertNextPoint(line[-1,0],line[-1,1],line[-1,2])points=vtk.vtkPoints()vtkLines=vtk.vtkCellArray()vtkLines.InsertNextCell(n)foriinrange(n):points.InsertNextPoint(line[i,0],line[i,1],line[i,2])vtkLines.InsertCellPoint(i)polygonPolyData=vtk.vtkPolyData()polygonPolyData.SetPoints(points)polygonPolyData.SetLines(vtkLines)mapper=vtk.vtkPolyDataMapper()mapper.SetInputData(polygonPolyData)actor=vtk.vtkActor()actor.SetMapper(mapper)actor.GetProperty().SetColor(1,0,0)actors.append(actor)polyData=vtk.vtkPolyData()polyData.SetPoints(endPoints)sphereSource=vtk.vtkSphereSource()sphereSource.SetRadius(1)glyph3D=vtk.vtkGlyph3D()glyph3D.SetSourceConnection(sphereSource.GetOutputPort())glyph3D.SetInputData(polyData)glyph3D.Update()mapper=vtk.vtkPolyDataMapper()mapper.SetInputConnection(glyph3D.GetOutputPort())actor=vtk.vtkActor()actor.SetMapper(mapper)actor.GetProperty().SetColor(0,0,1)actors.append(actor)returnactorsif__name__=='__main__':app=QApplication(sys.argv)window=AirwaySkeleton()window.show()sys.exit(app.exec_())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值