def pickPoint(displayPoint, view, obj=None, pickType='points', tolerance=0.01, returnNormal=False): assert pickType in ('points', 'cells', 'render') view = view or app.getCurrentRenderView() assert view if isinstance(obj, str): obj = om.findObjectByName(obj) assert obj if pickType == 'render': picker = vtk.vtkPropPicker() else: picker = vtk.vtkPointPicker() if pickType == 'points' else vtk.vtkCellPicker() picker.SetTolerance(tolerance) if obj: if isinstance(obj, list): for o in obj: picker.AddPickList(o.actor) obj = None else: picker.AddPickList(obj.actor) picker.PickFromListOn() picker.Pick(displayPoint[0], displayPoint[1], 0, view.renderer()) pickedProp = picker.GetViewProp() pickedPoint = np.array(picker.GetPickPosition()) pickedDataset = pickedProp.GetMapper().GetInput() if isinstance(pickedProp, vtk.vtkActor) else None pickedNormal = np.zeros(3) if returnNormal: if pickType == 'cells': pickedNormal = np.array(picker.GetPickNormal()) elif pickType == 'points' and pickedDataset: pointId = picker.GetPointId() normals = pickedDataset.GetPointData().GetNormals() if normals: pickedNormal = np.array(normals.GetTuple3(pointId)) if obj: if returnNormal: return (pickedPoint, pickedNormal) if pickedProp else (None, None) else: return pickedPoint if pickedProp else None else: return (pickedPoint, pickedProp, pickedDataset, pickedNormal) if returnNormal else (pickedPoint, pickedProp, pickedDataset)
def pickImage(displayPoint, view, obj=None): picker = vtk.vtkCellPicker() if isinstance(obj, str): obj = om.findObjectByName(obj) assert obj if obj: picker.AddPickList(obj.actor) picker.PickFromListOn() picker.Pick(displayPoint[0], displayPoint[1], 0, view.renderer()) pickedDataset = picker.GetDataSet() if obj: return picker.GetPointIJK() else: return pickedDataset, picker.GetPointIJK()