def grid_ids_frustum_to_ugrid_ugrid_flipped(grid, ids, frustum): 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 unused_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() ugrid_flipped = None return ugrid, ugrid_flipped
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)
# create pipeline # points = vtk.vtkBoundedPointSource() points.SetNumberOfPoints(NPts) points.ProduceRandomScalarsOn() points.ProduceCellOutputOff() points.Update() # Create a sphere implicit function sphere = vtk.vtkSphere() sphere.SetCenter(0.9,0.1,0.1) sphere.SetRadius(0.33) # Extract points within sphere extract = vtk.vtkExtractPoints() extract.SetInputConnection(points.GetOutputPort()) extract.SetImplicitFunction(sphere) # Time execution timer = vtk.vtkTimerLog() timer.StartTimer() extract.Update() timer.StopTimer() time = timer.GetElapsedTime() print("Time to remove points: {0}".format(time)) print(" Number removed: {0}".format(extract.GetNumberOfPointsRemoved())) print(" Original number of points: {0}".format(NPts)) # First output are the non-outliers extMapper = vtk.vtkPointGaussianMapper()
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')