直接上代码
from vtkmodules.all import * class MyRubberBand(vtkInteractorStyleRubberBand2D): def __init__(self): self.viewer = None self.AddObserver("LeftButtonPressEvent", self.OnLeftButtonDown) self.AddObserver("LeftButtonReleaseEvent",self.OnLeftButtonUp) self.AddObserver('MouseMoveEvent',self.mouseMove) self.draw = False self.startPoint = [0,0,0] self.curPoint = [0,0,0] self.lastPoint = [0,0,0] def mouseMove(self,obj,event): if self.draw == True: self.curPoint[:2] = self.GetInteractor().GetEventPosition() self.curPoint[2] = 0 self.drawLine() self.lastPoint[:2] = self.GetInteractor().GetEventPosition() self.lastPoint[2] = 0 p = self.GetInteractor().GetEventPosition() print(p) def OnLeftButtonDown(self,obj,event): # vtkInteractorStyleRubberBand2D.OnLeftButtonDown() self.draw = True self.startPoint[:2] = self.GetInteractor().GetEventPosition() self.startPoint[2] = 0 self.curPoint[:2] = self.startPoint[:2] self.curPoint[2] = 0 self.lastPoint[:2] = self.startPoint[:2] self.lastPoint[2] = 0 def OnLeftButtonUp(self,obj,event): self.draw = False def drawLine(self): self.viewer.GetRenderer().SetDisplayPoint(self.lastPoint) self.viewer.GetRenderer().DisplayToView() self.viewer.GetRenderer().ViewToWorld() start = self.viewer.GetRenderer().GetWorldPoint() self.viewer.GetRenderer().SetDisplayPoint(self.curPoint) self.viewer.GetRenderer().DisplayToView() self.viewer.GetRenderer().ViewToWorld() end = self.viewer.GetRenderer().GetWorldPoint() point1 = [0, 0, 0.1] point2 = [0, 0, 0.1] point1[0] = start[0] if start[0] > end[0] else end[0] point1[1] = start[1] if start[1] <= end[1] else end[1] point2[0] = start[0] if start[0] > end[0] else end[0] point2[1] = start[1] if start[1] > end[1] else end[1] print('a-----a') print(point1) print(point2) print('b-----b') self.SetLine(point1, point2) def SetRender(self,render): self.viewer = render def SetLine(self,point1,point2): lineSource = vtkLineSource() lineSource.SetPoint1(point1) lineSource.SetPoint2(point2) lineSource.Update() mapper = vtkPolyDataMapper() mapper.SetInputConnection(lineSource.GetOutputPort()) actor = vtkActor() actor.SetMapper(mapper) actor.GetProperty().SetLineWidth(10) actor.GetProperty().SetColor(1.0,0.0,0.0) self.viewer.GetRenderer().AddActor(actor) self.viewer.Render() if __name__ == '__main__': colors = vtkNamedColors() render = vtkRenderer() iren = vtkRenderWindowInteractor() reader1 = vtkJPEGReader() reader1.SetFileName('1.jpg') # reader1.SetDataOrigin(0,0,0) reader1.Update() render.SetBackground(colors.GetColor3d('darkgreen')) viewer = vtkImageViewer2() viewer.SetInputConnection(reader1.GetOutputPort()) viewer.SetRenderer(render) viewer.SetSize(640, 480) viewer.SetColorLevel(500) viewer.SetColorWindow(2000) viewer.SetSlice(40) viewer.SetSliceOrientationToXY() viewer.Render() viewer.GetRenderer().SetBackground(1, 1, 1) viewer.GetRenderWindow().SetWindowName("ImageViewer2D") rwi =vtkRenderWindowInteractor() viewer.SetupInteractor(rwi) g_style = MyRubberBand() g_style.SetInteractor(rwi) rwi.SetInteractorStyle(g_style) g_style.SetRender(viewer) rwi.Start()
实现了画笔功能,不过还有欠缺,横向画的时候有bug