def _cell_derivatives(narray, dataset, attribute_type, filter): # basic error checking if not dataset: raise RuntimeError, 'Need a dataset to compute gradients' if len(narray.shape) == 1: narray = narray.reshape((narray.shape[0], 1)) if narray.shape[0] != dataset.GetNumberOfPoints(): raise RuntimeError, 'The number of points does not match the number of tuples in the array' ncomp = narray.shape[1] if attribute_type == 'scalars' and ncomp != 1: raise RuntimeError, 'This function expects scalars.' if attribute_type == 'vectors' and ncomp != 3: raise RuntimeError, 'This function expects vectors.' # create a dataset with only our array but the same geometry/topology ds = dataset.NewInstance() ds.CopyStructure(dataset.VTKObject) # numpy_to_vtk converts only contiguous arrays if not narray.flags.contiguous: narray = narray.copy() varray = numpy_support.numpy_to_vtk(narray) if attribute_type == 'scalars': varray.SetName('scalars') ds.GetPointData().SetScalars(varray) else: varray.SetName('vectors') ds.GetPointData().SetVectors(varray) # setup and execute the pipeline # filter (vtkCellDerivatives) must have all of its properties # set filter.SetInput(ds) ds.UnRegister(None) c2p = vtk.vtkCellDataToPointData() c2p.SetInputConnection(filter.GetOutputPort()) c2p.Update() return c2p.GetOutput().GetPointData()
def gradient(narray, dataset=None): "Computes the gradient of a point-centered scalar array over a given dataset." if not dataset: dataset = narray.DataSet if not dataset: raise RuntimeError, 'Need a dataset to compute gradients' if len(narray.shape) == 1: narray = narray.reshape((narray.shape[0], 1)) if narray.shape[0] != narray.GetNumberOfTuples(): raise RuntimeError, 'The number of points does not match the number of tuples in the array' if narray.shape[1] > 1: raise RuntimeError, 'Gradient only works with 1 component arrays' ds = dataset.NewInstance() ds.CopyStructure(dataset.VTKObject) # numpy_to_vtk converts only contiguous arrays if not narray.flags.contiguous: narray = narray.copy() varray = numpy_support.numpy_to_vtk(narray) varray.SetName('scalars') ds.GetPointData().SetScalars(varray) cd = vtk.vtkCellDerivatives() cd.SetInput(ds) ds.UnRegister(None) c2p = vtk.vtkCellDataToPointData() c2p.SetInputConnection(cd.GetOutputPort()) c2p.Update() retVal = c2p.GetOutput().GetPointData().GetVectors() try: if narray.GetName(): retVal.SetName("gradient of " + narray.GetName()) else: retVal.SetName("gradient") except AttributeError: retVal.SetName("gradient") return dataset_adapter.vtkDataArrayToVTKArray(retVal, dataset)
def _cell_derivatives (narray, dataset, attribute_type, filter): if not dataset : raise RuntimeError, 'Need a dataset to compute _cell_derivatives.' # Reshape n dimensional vector to n by 1 matrix if len(narray.shape) == 1 : narray = narray.reshape((narray.shape[0], 1)) ncomp = narray.shape[1] if attribute_type == 'scalars' and ncomp != 1 : raise RuntimeError, 'This function expects scalars.'\ 'Input shape ' + narray.shape if attribute_type == 'vectors' and ncomp != 3 : raise RuntimeError, 'This function expects vectors.'\ 'Input shape ' + narray.shape # numpy_to_vtk converts only contiguous arrays if not narray.flags.contiguous : narray = narray.copy() varray = numpy_support.numpy_to_vtk(narray) if attribute_type == 'scalars': varray.SetName('scalars') else : varray.SetName('vectors') # create a dataset with only our array but the same geometry/topology ds = dataset.NewInstance() ds.UnRegister(None) ds.CopyStructure(dataset.VTKObject) if dataset_adapter.ArrayAssociation.FIELD == narray.Association : raise RuntimeError, 'Unknown data association. Data should be associated with points or cells.' if dataset_adapter.ArrayAssociation.POINT == narray.Association : # Work on point data if narray.shape[0] != dataset.GetNumberOfPoints() : raise RuntimeError, 'The number of points does not match the number of tuples in the array' if attribute_type == 'scalars': ds.GetPointData().SetScalars(varray) else : ds.GetPointData().SetVectors(varray) elif dataset_adapter.ArrayAssociation.CELL == narray.Association : # Work on cell data if narray.shape[0] != dataset.GetNumberOfCells() : raise RuntimeError, 'The number of does not match the number of tuples in the array' # Since vtkCellDerivatives only works with point data, we need to convert # the cell data to point data first. ds2 = dataset.NewInstance() ds2.UnRegister(None) ds2.CopyStructure(dataset.VTKObject) if attribute_type == 'scalars' : ds2.GetCellData().SetScalars(varray) else : ds2.GetCellData().SetVectors(varray) c2p = vtk.vtkCellDataToPointData() c2p.SetInputData(ds2) c2p.Update() # Set the output to the ds dataset if attribute_type == 'scalars': ds.GetPointData().SetScalars(c2p.GetOutput().GetPointData().GetScalars()) else: ds.GetPointData().SetVectors(c2p.GetOutput().GetPointData().GetVectors()) filter.SetInputData(ds) if dataset_adapter.ArrayAssociation.POINT == narray.Association : # Since the data is associated with cell and the query is on points # we have to convert to point data before returning c2p = vtk.vtkCellDataToPointData() c2p.SetInputConnection(filter.GetOutputPort()) c2p.Update() return c2p.GetOutput().GetPointData() elif dataset_adapter.ArrayAssociation.CELL == narray.Association : filter.Update() return filter.GetOutput().GetCellData() else : # We shall never reach here raise RuntimeError, 'Unknown data association. Data should be associated with points or cells.'