def createCellLocator(a_FileName, a_LocatorType=None, a_Legacy='vtp'): if a_FileName.endswith('vtu'): reader = vtk.vtkXMLUnstructuredGridReader() elif a_FileName.endswith('vtp'): reader = vtk.vtkXMLPolyDataReader() elif a_FileName.endswith('.vtk'): if a_Legacy == 'none': print("Need To Specify Data Type For Legacy Files") sys.exit() elif a_Legacy == 'vtu': reader = vtk.vtkUnstructuredGridReader() elif a_Legacy == 'vtp': reader = vtk.vtkPolyDataReader() else: print("Unsupported File Extension") sys.exit() reader.SetFileName(a_FileName) reader.Update() if a_LocatorType is None: locator = vtk.vtkCellTreeLocator() else: if a_LocatorType == 'oct': locator = vtk.vtkCellLocator() elif a_LocatorType == 'tre': locator = vtk.vtkCellTreeLocator() elif a_LocatorType == 'bsp': locator = vtk.vtkModifiedBSPTree() locator.SetDataSet(reader.GetOutput()) locator.BuildLocator() return locator
def __init__(self, filename): ''' It loads the vtk file. ''' # PolyData object of Pyvista # All its point arrays are converted to cell arrays. self.mesh = pv.read(filename).point_data_to_cell_data() # A locator is built to get the cells inside certain bounds in # other functions of the trajectory class. self.locator = vtk.vtkCellTreeLocator() self.locator.SetDataSet(self.mesh) self.locator.BuildLocator()
def __init__(self, dset): self.dset = dset.VTKObject self.xyz = [-10000, -20000, -30000] self.pids = vtk.reference([0] * 10) self.nverts = -1 self.pc = [0] * 3 self.wts = [0] * 10 self.gc = vtk.vtkGenericCell() self.sid = 2 if self.dset.IsA('vtkUnstructuredGrid'): self.locator = vtk.vtkCellTreeLocator() self.locator.SetDataSet(dset.VTKObject) self.locator.BuildLocator() self.is_vtu = True else: self.is_vtu = False
def findCellsWithin(self, xbounds=(), ybounds=(), zbounds=(), c=None): """ Find cells that are within specified bounds. Setting a color will add a vtk array to colorize these cells. """ if len(xbounds) == 6: bnds = xbounds else: bnds = list(self.bounds()) if len(xbounds) == 2: bnds[0] = xbounds[0] bnds[1] = xbounds[1] if len(ybounds) == 2: bnds[2] = ybounds[0] bnds[3] = ybounds[1] if len(zbounds) == 2: bnds[4] = zbounds[0] bnds[5] = zbounds[1] cellIds = vtk.vtkIdList() self.cell_locator = vtk.vtkCellTreeLocator() self.cell_locator.SetDataSet(self.polydata()) self.cell_locator.BuildLocator() self.cell_locator.FindCellsWithinBounds(bnds, cellIds) if c is not None: cellData = vtk.vtkUnsignedCharArray() cellData.SetNumberOfComponents(3) cellData.SetName('CellsWithinBoundsColor') cellData.SetNumberOfTuples(self.polydata(False).GetNumberOfCells()) defcol = np.array(self.color()) * 255 for i in range(cellData.GetNumberOfTuples()): cellData.InsertTuple(i, defcol) self.polydata(False).GetCellData().SetScalars(cellData) self._mapper.ScalarVisibilityOn() flagcol = np.array(colors.getColor(c)) * 255 cids = [] for i in range(cellIds.GetNumberOfIds()): cid = cellIds.GetId(i) if c is not None: cellData.InsertTuple(cid, flagcol) cids.append(cid) return np.array(cids)
output = pl3d.GetOutput().GetBlock(0) ps = vtk.vtkPlaneSource() ps.SetXResolution(4) ps.SetYResolution(4) ps.SetOrigin(2, -2, 26) ps.SetPoint1(2, 2, 26) ps.SetPoint2(2, -2, 32) psMapper = vtk.vtkPolyDataMapper() psMapper.SetInputConnection(ps.GetOutputPort()) psActor = vtk.vtkActor() psActor.SetMapper(psMapper) psActor.GetProperty().SetRepresentationToWireframe() # Use the vtkCellTreeLocator rk4 = vtk.vtkRungeKutta4() treeLoc = vtk.vtkCellTreeLocator() ivp = vtk.vtkCompositeInterpolatedVelocityField() ivp.SetFindCellStrategy(vtk.vtkCellLocatorStrategy()) ivp.GetFindCellStrategy().SetCellLocator(treeLoc) streamer = vtk.vtkStreamTracer() streamer.SetInputData(output) streamer.SetSourceData(ps.GetOutput()) streamer.SetMaximumPropagation(100) streamer.SetInitialIntegrationStep(.2) streamer.SetIntegrationDirectionToForward() streamer.SetComputeVorticity(1) streamer.SetIntegrator(rk4) streamer.SetInterpolatorPrototype(ivp) rf = vtk.vtkRibbonFilter() rf.SetInputConnection(streamer.GetOutputPort())
opTime = timer.GetElapsedTime() print(" Find cell probing: {0}".format(opTime)) # Time the deletion of the locator. The standard locator is quite slow due # to fragmented memory. timer.StartTimer() del locator timer.StopTimer() time2 = timer.GetElapsedTime() print(" Delete Cell Locator: {0}".format(time2)) print(" Cell Locator (Total): {0}".format(time + opTime + time2)) print("\n") ############################################################# # Time the creation and building of the cell tree locator locator1 = vtk.vtkCellTreeLocator() locator1.SetDataSet(output) locator1.AutomaticOn() timer.StartTimer() locator1.BuildLocator() timer.StopTimer() time = timer.GetElapsedTime() print("Build Cell Tree Locator: {0}".format(time)) # Probe the dataset with FindClosestPoint() and time it timer.StartTimer() for i in range(0, numProbes): treeClosest.SetId( i, locator1.FindCell(ProbeCells.GetPoint(i), 0.001, genCell, pc, weights))
opTime = timer.GetElapsedTime() print(" Find cell probing: {0}".format(opTime)) # Time the deletion of the locator. The standard locator is quite slow due # to fragmented memory. timer.StartTimer() del locator timer.StopTimer() time2 = timer.GetElapsedTime() print(" Delete Cell Locator: {0}".format(time2)) print(" Cell Locator (Total): {0}".format(time+opTime+time2)) print("\n") ############################################################# # Time the creation and building of the cell tree locator locator1 = vtk.vtkCellTreeLocator() locator1.SetDataSet(output) locator1.AutomaticOn() timer.StartTimer() locator1.BuildLocator() timer.StopTimer() time = timer.GetElapsedTime() print("Build Cell Tree Locator: {0}".format(time)) # Probe the dataset with FindClosestPoint() and time it timer.StartTimer() for i in range (0,numProbes): treeClosest.SetId(i, locator1.FindCell(ProbeCells.GetPoint(i),0.001,genCell,pc,weights)) timer.StopTimer() opTime = timer.GetElapsedTime()