def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkExtractSelectedFrustum(), 'Processing.', ('vtkDataSet', 'vtkSelection'), ('vtkDataSet',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def picker_callback(self,obj,event): extract = vtk.vtkExtractSelectedFrustum() fPlanes=obj.GetFrustum() #collection of planes based on unscaled display #scale frustum to account for the zaspect scaledPlanes=vtk.vtkPlanes() scaledNormals=vtk.vtkDoubleArray() scaledNormals.SetNumberOfComponents(3) scaledNormals.SetNumberOfTuples(6) scaledOrigins=vtk.vtkPoints() for j in range(6): i=fPlanes.GetPlane(j) k=i.GetOrigin() q=i.GetNormal() scaledOrigins.InsertNextPoint(k[0],k[1],k[2]/float(self.Zaspect)) scaledNormals.SetTuple(j,(q[0],q[1],q[2]*float(self.Zaspect))) scaledPlanes.SetNormals(scaledNormals) scaledPlanes.SetPoints(scaledOrigins) extract.SetFrustum(scaledPlanes) extract.SetInputData(self.vtkPntsPolyData) extract.Update() extracted = extract.GetOutput() ids = vtk.vtkIdTypeArray() ids = extracted.GetPointData().GetArray("vtkOriginalPointIds") if ids: #store them in an array for an undo operation self.lastSelectedIds=ids for i in range(ids.GetNumberOfTuples()): #turn them red self.colors.SetTuple(ids.GetValue(i),(255,0,0)) self.bool_pnt[ids.GetValue(i)]=False self.vtkPntsPolyData.GetPointData().SetScalars(self.colors) self.vtkPntsPolyData.Modified() self.ui.vtkWidget.update() #set flag on ui to show that data has been modified self.unsaved_changes=True
def __init__(self): VTKPythonAlgorithmBase.__init__(self, nInputPorts=1, inputType='vtkUnstructuredGrid', nOutputPorts=1, outputType='vtkUnstructuredGrid') self.append_filter = vtk.vtkAppendFilter() self.append_filter.ReleaseDataFlagOn() self.triangle_filter = vtk.vtkTriangleFilter() self.planes = vtk.vtkPlanes() self.ex = vtk.vtkExtractSelectedFrustum() self.ex.ReleaseDataFlagOn() self.poly_pick_data = None self.renderer = None
def picker_callback(self,obj,event): extract = vtk.vtkExtractSelectedFrustum() fPlanes=obj.GetFrustum() #collection of planes based on unscaled display #scale frustum to account for the zaspect scaledPlanes=vtk.vtkPlanes() scaledNormals=vtk.vtkDoubleArray() scaledNormals.SetNumberOfComponents(3) scaledNormals.SetNumberOfTuples(6) scaledOrigins=vtk.vtkPoints() for j in range(6): i=fPlanes.GetPlane(j) k=i.GetOrigin() q=i.GetNormal() scaledOrigins.InsertNextPoint(k[0],k[1],k[2]/float(self.Zaspect)) scaledNormals.SetTuple(j,(q[0],q[1],q[2]*float(self.Zaspect))) scaledPlanes.SetNormals(scaledNormals) scaledPlanes.SetPoints(scaledOrigins) extract.SetFrustum(scaledPlanes) extract.SetInputData(self.vtkPntsPolyData) extract.Update() extracted = extract.GetOutput() ids = vtk.vtkIdTypeArray() ids = extracted.GetPointData().GetArray("vtkOriginalPointIds") if ids: #store them in an array for an undo operation self.lastSelectedIds=ids for i in range(ids.GetNumberOfTuples()): #turn them red self.colors.SetTuple(ids.GetValue(i),(255,0,0)) self.bool_pnt[ids.GetValue(i)]=False self.vtkPntsPolyData.GetPointData().SetScalars(self.colors) self.vtkPntsPolyData.Modified() self.ui.vtkWidget.update() #set flag on ui to show that data has been modified self.unsaved_changes=True
def pickElements(self, x0, y0, x1, y1, tolerance=10): tooSmall = (abs(x0-x1) < tolerance) or (abs(y0-y1) < tolerance) if tooSmall: x0, x1 = (x0-tolerance//2), (x1+tolerance//2) y0, y1 = (y0-tolerance//2), (y1+tolerance//2) picker = vtk.vtkAreaPicker() extractor = vtk.vtkExtractSelectedFrustum() renderer = self.__rendererMesh._renderer picker.AreaPick(x0,y0,x1,y1,renderer) extractor.SetFrustum(picker.GetFrustum()) elementsBounds = self.__rendererMesh.elementsBounds camPos = renderer.GetActiveCamera().GetPosition() distanceFromCamera = lambda key: distanceBoundsToPoint(camPos, elementsBounds[key]) pickedElements = {key for key, bound in elementsBounds.items() if extractor.OverallBoundsTest(bound)} # when not box selecting, pick only the closest element if tooSmall and pickedElements: closest = min(pickedElements, key=distanceFromCamera) pickedElements.clear() pickedElements.add(closest) return pickedElements
def _pick_depth_ids(self, xmin, ymin, xmax, ymax): """ Does an area pick of all the ids inside the box, even the ones behind the front elements """ area_picker = self.parent.area_picker #area_picker.Pick() # double pick? area_picker.AreaPick(xmin, ymin, xmax, ymax, self.parent.rend) frustum = area_picker.GetFrustum() # vtkPlanes grid = self.parent.get_grid(self.name) #extract_ids = vtk.vtkExtractSelectedIds() #extract_ids.AddInputData(grid) idsname = "Ids" ids = vtk.vtkIdFilter() if isinstance(grid, vtk.vtkUnstructuredGrid): # this is typically what's called in the gui ids.SetInputData(grid) elif isinstance(grid, vtk.vtkPolyData): # pragma: no cover # this doesn't work... ids.SetCellIds(grid.GetCellData()) ids.SetPointIds(grid.GetPointData()) else: raise NotImplementedError(ids) #self.is_eids = False ids.CellIdsOn() ids.PointIdsOn() #print('is_eids=%s is_nids=%s' % (self.is_eids, self.is_nids)) if not self.is_eids: ids.CellIdsOff() if not self.is_nids: ids.PointIdsOff() #ids.FieldDataOn() ids.SetIdsArrayName(idsname) if 1: selected_frustum = vtk.vtkExtractSelectedFrustum() #selected_frustum.ShowBoundsOn() #selected_frustum.SetInsideOut(1) selected_frustum.SetFrustum(frustum) # PreserveTopologyOn: return an insidedness array # PreserveTopologyOff: return a ugrid selected_frustum.PreserveTopologyOff() #selected_frustum.PreserveTopologyOn() selected_frustum.SetInputConnection( ids.GetOutputPort()) # was grid? selected_frustum.Update() ugrid = selected_frustum.GetOutput() # we make a second frustum to remove extra points selected_frustum_flipped = vtk.vtkExtractSelectedFrustum() selected_frustum_flipped.SetInsideOut(1) selected_frustum_flipped.SetFrustum(frustum) selected_frustum_flipped.PreserveTopologyOff() selected_frustum_flipped.SetInputConnection( ids.GetOutputPort()) # was grid? selected_frustum_flipped.Update() ugrid_flipped = selected_frustum_flipped.GetOutput() else: # pragma: no cover extract_points = vtk.vtkExtractPoints() selection_node = vtk.vtkSelectionNode() selection = vtk.vtkSelection() #selection_node.SetContainingCellsOn() selection_node.Initialize() selection_node.SetFieldType(vtk.vtkSelectionNode.POINT) selection_node.SetContentType(vtk.vtkSelectionNode.INDICES) selection.AddNode(selection_node) extract_selection = vtk.vtkExtractSelection() extract_selection.SetInputData(0, grid) extract_selection.SetInputData(1, selection) # vtk 6+ extract_selection.Update() ugrid = extract_selection.GetOutput() eids = None if self.is_eids: cells = ugrid.GetCellData() if cells is not None: ids = cells.GetArray('Ids') if ids is not None: cell_ids = vtk_to_numpy(ids) assert len(cell_ids) == len(np.unique(cell_ids)) eids = self.parent.get_element_ids(self.name, cell_ids) nids = None if self.is_nids: ugrid_points, nids = self.get_inside_point_ids( ugrid, ugrid_flipped) ugrid = ugrid_points actor = self.parent.create_highlighted_actor( ugrid, representation=self.representation) self.actor = actor if self.callback is not None: self.callback(eids, nids, self.name) self.area_pick_button.setChecked(False) # TODO: it would be nice if you could do a rotation without # destroying the highlighted actor self.cleanup_observer = self.parent.setup_mouse_buttons( mode='default', left_button_down_cleanup=self.cleanup_callback)
def _pick_depth_ids(self, xmin, ymin, xmax, ymax): """ Does an area pick of all the ids inside the box, even the ones behind the front elements """ area_picker = self.parent.area_picker #area_picker.Pick() # double pick? area_picker.AreaPick(xmin, ymin, xmax, ymax, self.parent.rend) frustum = area_picker.GetFrustum() # vtkPlanes #frustum = create_box_frustum(xmin, ymin, xmax, ymax, self.parent.rend) grid = self.parent.get_grid(self.name) #extract_ids = vtk.vtkExtractSelectedIds() #extract_ids.AddInputData(grid) idsname = "Ids" ids = vtk.vtkIdFilter() if isinstance(grid, vtk.vtkUnstructuredGrid): ids.SetInputData(grid) elif isinstance(grid, vtk.vtkPolyData): # pragma: no cover # this doesn't work... ids.SetCellIds(grid.GetCellData()) ids.SetPointIds(grid.GetPointData()) else: raise NotImplementedError(ids) #self.is_eids = False ids.CellIdsOn() ids.PointIdsOn() #if not self.is_eids: #ids.CellIdsOff() #if not self.is_nids: #ids.PointIdsOff() #ids.FieldDataOn() ids.SetIdsArrayName(idsname) if 1: selected_frustum = vtk.vtkExtractSelectedFrustum() selected_frustum.SetFrustum(frustum) selected_frustum.PreserveTopologyOff( ) # don't make an unstructured grid selected_frustum.SetInputConnection( ids.GetOutputPort()) # was grid? selected_frustum.Update() ugrid = selected_frustum.GetOutput() else: # pragma: no cover extract_points = vtk.vtkExtractPoints() selection_node = vtk.vtkSelectionNode() selection = vtk.vtkSelection() selection_node.SetContainingCellsOn() selection_node.Initialize() selection_node.SetFieldType(vtkSelectionNode.POINT) selection_node.SetContentType(vtkSelectionNode.INDICES) selection.AddNode(selection_node) extract_selection = vtk.vtkExtractSelection() extract_selection.SetInputData(0, grid) extract_selection.SetInputData(1, selection) # vtk 6+ extract_selection.Update() ugrid = extract_selection.GetOutput() eids = None nids = None msg = '' if self.is_eids: cells = ugrid.GetCellData() if cells is not None: ids = cells.GetArray('Ids') if ids is not None: cell_ids = vtk_to_numpy(ids) assert len(cell_ids) == len(np.unique(cell_ids)) eids = self.parent.get_element_ids(self.name, cell_ids) if self.is_nids: points = ugrid.GetPointData() if points is not None: ids = points.GetArray('Ids') if ids is not None: point_ids = vtk_to_numpy(ids) nids = self.parent.get_node_ids(self.name, point_ids) if self.callback is not None: self.callback(eids, nids, self.name) self.area_pick_button.setChecked(False) self.parent.setup_mouse_buttons(mode='default')