我在Python-VTK(7)中有一个简单的程序,它有三个2D的磁盘,可以点击和移动。
我通过制作自定义交互器样式来实现这一点,我在其中将所选元素存储在LeftButtonClick
事件上,更改先前所选元素在MouseMove
事件上的位置,并将所选元素设置为{{ 1}} None
事件。
这里是完整的VTK代码(理想情况下)应该这样做:
LeftButtonRelease
结果如下:
即,无论我选择哪个磁盘,import vtk
class CustomInteractor(vtk.vtkInteractorStyleTrackballCamera):
def __init__(self, renderer, renWin):
self.AddObserver('LeftButtonPressEvent', self.OnLeftButtonDown)
self.AddObserver('LeftButtonReleaseEvent', self.OnLeftButtonRelease)
self.AddObserver('MouseMoveEvent', self.OnMouseMove)
self.renderer = renderer
self.chosenPiece = None
self.renWin = renWin
def OnLeftButtonRelease(self, obj, eventType):
self.chosenPiece = None
vtk.vtkInteractorStyleTrackballCamera.OnLeftButtonUp(self)
def OnLeftButtonDown(self, obj, eventType):
clickPos = self.GetInteractor().GetEventPosition()
picker = vtk.vtkPropPicker()
picker.Pick(clickPos[0], clickPos[1], 0, self.renderer)
actor = picker.GetActor2D()
self.chosenPiece = actor
vtk.vtkInteractorStyleTrackballCamera.OnLeftButtonDown(self)
def OnMouseMove(self, obj, eventType):
if self.chosenPiece is not None:
mousePos = self.GetInteractor().GetEventPosition()
self.chosenPiece.SetPosition(mousePos[0], mousePos[1])
self.renWin.Render()
else :
vtk.vtkInteractorStyleTrackballCamera.OnMouseMove(self)
def buildDiskActor(inner_radius = 10, outer_radius = 20, position = (100, 20), color = (1, 1, 1)):
disk = vtk.vtkDiskSource()
disk.SetInnerRadius(inner_radius)
disk.SetOuterRadius(outer_radius)
disk.SetRadialResolution(100)
disk.SetCircumferentialResolution(100)
disk.Update()
mapper = vtk.vtkPolyDataMapper2D()
mapper.SetInputConnection(disk.GetOutputPort())
actor = vtk.vtkActor2D()
actor.SetMapper(mapper)
actor.SetPosition(position[0], position[1])
actor.GetProperty().SetColor(color)
return actor
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(600, 600)
elements = [
buildDiskActor(position = (200, 400), color=(1, 0, 0)),
buildDiskActor(position = (400, 400), color=(0, 1, 0)),
buildDiskActor(position = (300, 200), color=(0, 0, 1)),
]
for actor in elements:
ren.AddActor(actor)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
inStyle = CustomInteractor(ren, renWin)
iren.SetInteractorStyle(inStyle)
renWin.Render()
iren.Start()
都只选择绿色磁盘。
出于某种原因,当我使用Python2-VTK6.3.0而不是Python3-VTK7时,事情按预期工作。这是VTK7中的错误吗?或者实施是否以某种方式改变了?还是别的什么?
答案 0 :(得分:0)
VTK 7 switched to a new rendering backend. In 7.0, the rendering backend has a bug with picking 2D actors.
The bug is fixed in 7.1; see Kitware/VTK@5559c11
.