def vertex_normal (dataset) : "Returns the vertex normal of each point in a dataset." if not dataset : raise RuntimeError, 'Need a dataset to compute vertex_normal' ds = dataset.NewInstance() ds.UnRegister(None) ds.CopyStructure(dataset.VTKObject) filter = vtkPolyDataNormals() filter.SetInputData(ds) filter.ComputeCellNormalsOff() filter.ComputePointNormalsOn() filter.SetFeatureAngle(180) filter.SplittingOff() filter.ConsistencyOff() filter.AutoOrientNormalsOff() filter.FlipNormalsOff() filter.NonManifoldTraversalOff() filter.Update() varray = filter.GetOutput().GetPointData().GetNormals() ans = dataset_adapter.vtkDataArrayToVTKArray(varray, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = dataset_adapter.ArrayAssociation.POINT return ans
def _cell_quality (dataset, quality) : if not dataset : raise RuntimeError, 'Need a dataset to compute _cell_quality' # create a dataset with only our array but the same geometry/topology ds = dataset.NewInstance() ds.UnRegister(None) ds.CopyStructure(dataset.VTKObject) filter = vtkCellQuality() filter.SetInputData(ds) if "area" == quality : filter.SetQualityMeasureToArea() elif "aspect" == quality : filter.SetQualityMeasureToAspectRatio() elif "aspect_gamma" == quality : filter.SetQualityMeasureToAspectGamma() elif "condition" == quality : filter.SetQualityMeasureToCondition() elif "diagonal" == quality : filter.SetQualityMeasureToDiagonal() elif "jacobian" == quality : filter.SetQualityMeasureToJacobian() elif "max_angle" == quality : filter.SetQualityMeasureToMaxAngle() elif "shear" == quality : filter.SetQualityMeasureToShear() elif "skew" == quality : filter.SetQualityMeasureToSkew() elif "min_angle" == quality : filter.SetQualityMeasureToMinAngle() elif "volume" == quality : filter.SetQualityMeasureToVolume() else : raise RuntimeError, 'Unknown cell quality ['+quality+'].' filter.Update() varray = filter.GetOutput().GetCellData().GetArray("CellQuality") ans = dataset_adapter.vtkDataArrayToVTKArray(varray, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = dataset_adapter.ArrayAssociation.CELL return ans
def gradient(narray, dataset=None): "Returns the gradient of an array of scalars/vectors." if not dataset: dataset = narray.DataSet() if not dataset: raise RuntimeError, 'Need a dataset to compute gradient' ncomp = narray.shape[1] if ncomp != 1 and ncomp != 3: raise RuntimeError, 'Gradient only works with scalars (1 component) and vectors (3 component)'\ 'Input shape ' + narray.shape cd = vtkCellDerivatives() if ncomp == 1 : attribute_type = 'scalars' else : attribute_type = 'vectors' dsa = _cell_derivatives(narray, dataset, attribute_type, cd) if ncomp == 1 : retVal = dsa.GetVectors() else : retVal = dsa.GetTensors() try: if narray.GetName() : retVal.SetName("gradient of " + narray.GetName()) else : retVal.SetName("gradient") except AttributeError : retVal.SetName("gradient") ans = dataset_adapter.vtkDataArrayToVTKArray(retVal, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = narray.Association return ans
def _cell_quality (dataset, quality) : if not dataset : raise RuntimeError, 'Need a dataset to compute _cell_quality' # create a dataset with only our array but the same geometry/topology ds = dataset.NewInstance() ds.UnRegister(None) ds.CopyStructure(dataset.VTKObject) filter = vtkCellQuality() filter.SetInputData(ds) if "area" == quality : filter.SetQualityMeasureToArea() elif "aspect" == quality : filter.SetQualityMeasureToAspectRatio() elif "aspect_gamma" == quality : filter.SetQualityMeasureToAspectGamma() elif "condition" == quality : filter.SetQualityMeasureToCondition() elif "diagonal" == quality : filter.SetQualityMeasureToDiagonal() elif "jacobian" == quality : filter.SetQualityMeasureToJacobian() elif "max_angle" == quality : filter.SetQualityMeasureToMaxAngle() elif "shear" == quality : filter.SetQualityMeasureToShear() elif "skew" == quality : filter.SetQualityMeasureToSkew() elif "min_angle" == quality : filter.SetQualityMeasureToMinAngle() elif "volume" == quality : filter.SetQualityMeasureToVolume() else : raise RuntimeError, 'Unknown cell quality ['+quality+'].' filter.Update() varray = filter.GetOutput().GetCellData().GetArray("CellQuality") ans = dataset_adapter.vtkDataArrayToVTKArray(varray, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = dataset_adapter.ArrayAssociation.CELL return ans
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' ncomp = narray.shape[1] if ncomp != 1 and ncomp != 3: raise RuntimeError, 'Gradient only works with scalars (1 component) and vectors (3 component)' cd = vtk.vtkCellDerivatives() if ncomp == 1: attribute_type = 'scalars' else: attribute_type = 'vectors' dsa = _cell_derivatives(narray, dataset, attribute_type, cd) if ncomp == 1: retVal = dsa.GetVectors() else: retVal = dsa.GetTensors() 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 vertex_normal (dataset) : "Returns the vertex normal of each point in a dataset." if not dataset : raise RuntimeError, 'Need a dataset to compute vertex_normal' ds = dataset.NewInstance() ds.UnRegister(None) ds.CopyStructure(dataset.VTKObject) filter = vtkPolyDataNormals() filter.SetInputData(ds) filter.ComputeCellNormalsOff() filter.ComputePointNormalsOn() filter.SetFeatureAngle(180) filter.SplittingOff() filter.ConsistencyOff() filter.AutoOrientNormalsOff() filter.FlipNormalsOff() filter.NonManifoldTraversalOff() filter.Update() varray = filter.GetOutput().GetPointData().GetNormals() ans = dataset_adapter.vtkDataArrayToVTKArray(varray, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = dataset_adapter.ArrayAssociation.POINT return ans
def gradient(narray, dataset=None): "Returns the gradient of an array of scalars/vectors." if not dataset: dataset = narray.DataSet() if not dataset: raise RuntimeError, 'Need a dataset to compute gradient' ncomp = narray.shape[1] if ncomp != 1 and ncomp != 3: raise RuntimeError, 'Gradient only works with scalars (1 component) and vectors (3 component)'\ 'Input shape ' + narray.shape cd = vtkCellDerivatives() if ncomp == 1 : attribute_type = 'scalars' else : attribute_type = 'vectors' dsa = _cell_derivatives(narray, dataset, attribute_type, cd) if ncomp == 1 : retVal = dsa.GetVectors() else : retVal = dsa.GetTensors() try: if narray.GetName() : retVal.SetName("gradient of " + narray.GetName()) else : retVal.SetName("gradient") except AttributeError : retVal.SetName("gradient") ans = dataset_adapter.vtkDataArrayToVTKArray(retVal, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = narray.Association return ans
def strain(narray, dataset=None): if not dataset: dataset = narray.DataSet() cd = vtk.vtkCellDerivatives() cd.SetTensorModeToComputeStrain() dsa = _cell_derivatives(narray, dataset, 'vectors', cd) retVal = dsa.GetTensors() retVal.SetName("strain") return dataset_adapter.vtkDataArrayToVTKArray(retVal, dataset)
def curl(narray, dataset=None): if not dataset: dataset = narray.DataSet() cd = vtk.vtkCellDerivatives() cd.SetVectorModeToComputeVorticity() dsa = _cell_derivatives(narray, dataset, 'vectors', cd) retVal = dsa.GetVectors() retVal.SetName("vorticity") return dataset_adapter.vtkDataArrayToVTKArray(retVal, dataset)
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 strain (narray, dataset=None) : "Returns the strain of an array of 3D vectors." if not dataset : dataset = narray.DataSet() if not dataset : raise RuntimeError, 'Need a dataset to compute strain' if 2 != narray.ndim or 3 != narray.shape[1] : raise RuntimeError, 'strain only works with an array of 3D vectors'\ 'Input shape ' + narray.shape cd = vtkCellDerivatives() cd.SetTensorModeToComputeStrain() dsa = _cell_derivatives(narray, dataset, 'vectors', cd) retVal = dsa.GetTensors() retVal.SetName("strain") ans = dataset_adapter.vtkDataArrayToVTKArray(retVal, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = narray.Association return ans
def strain (narray, dataset=None) : "Returns the strain of an array of 3D vectors." if not dataset : dataset = narray.DataSet() if not dataset : raise RuntimeError, 'Need a dataset to compute strain' if 2 != narray.ndim or 3 != narray.shape[1] : raise RuntimeError, 'strain only works with an array of 3D vectors'\ 'Input shape ' + narray.shape cd = vtkCellDerivatives() cd.SetTensorModeToComputeStrain() dsa = _cell_derivatives(narray, dataset, 'vectors', cd) retVal = dsa.GetTensors() retVal.SetName("strain") ans = dataset_adapter.vtkDataArrayToVTKArray(retVal, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = narray.Association return ans
def curl (narray, dataset=None): "Returns the curl of an array of 3D vectors." if not dataset : dataset = narray.DataSet() if not dataset : raise RuntimeError, 'Need a dataset to compute curl.' if narray.ndim != 2 or narray.shape[1] != 3 : raise RuntimeError, 'Curl only works with an array of 3D vectors.'\ 'Input shape ' + narray.shape cd = vtk.vtkCellDerivatives() cd.SetVectorModeToComputeVorticity() dsa = _cell_derivatives(narray, dataset, 'vectors', cd) retVal = dsa.GetVectors() retVal.SetName("vorticity") ans = dataset_adapter.vtkDataArrayToVTKArray(retVal, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = narray.Association return ans
def _matrix_math_filter (narray, operation) : if operation not in ['Determinant', 'Inverse', 'Eigenvalue', 'Eigenvector'] : raise RuntimeError, 'Unknown quality measure ['+operation+']'+\ 'Supported are [Determinant, Inverse, Eigenvalue, Eigenvector]' dataset = narray.DataSet() if not dataset : raise RuntimeError, 'narray is not associated with a dataset.' if dataset_adapter.ArrayAssociation.FIELD == narray.Association : raise RuntimeError, 'Unknown data association. Data should be associated with points or cells.' if narray.ndim != 3 : raise RuntimeError, operation+' only works for an array of matrices(3D array).'\ 'Input shape ' + narray.shape elif narray.shape[1] != narray.shape[2] : raise RuntimeError, operation+' requires an array of 2D square matrices.'\ 'Input shape ' + narray.shape # numpy_to_vtk converts only contiguous arrays if not narray.flags.contiguous : narray = narray.copy() # Reshape is necessary because numpy_support.numpy_to_vtk only works with 2D or # less arrays. nrows = narray.shape[0] ncols = narray.shape[1] * narray.shape[2] narray = narray.reshape(nrows, ncols) ds = dataset.NewInstance() ds.UnRegister(None) ds.ShallowCopy(dataset.VTKObject) varray = numpy_support.numpy_to_vtk(narray) varray.SetName('tensors') filter = vtkMatrixMathFilter() if operation == 'Determinant' : filter.SetOperationToDeterminant() elif operation == 'Inverse' : filter.SetOperationToInverse() elif operation == 'Eigenvalue' : filter.SetOperationToEigenvalue() elif operation == 'Eigenvector' : filter.SetOperationToEigenvector() if dataset_adapter.ArrayAssociation.POINT == narray.Association : ds.GetPointData().SetTensors(varray) # filter.SetQualityTypeToPointQuality() elif dataset_adapter.ArrayAssociation.CELL == narray.Association : ds.GetCellData().SetTensors(varray) # filter.SetQualityTypeToCellQuality() filter.SetInputData(ds) filter.Update() if dataset_adapter.ArrayAssociation.POINT == narray.Association : varray = filter.GetOutput().GetPointData().GetArray(operation) elif dataset_adapter.ArrayAssociation.CELL == narray.Association : varray = filter.GetOutput().GetCellData().GetArray(operation) ans = dataset_adapter.vtkDataArrayToVTKArray(varray, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = narray.Association return ans
def _matrix_math_filter (narray, operation) : if operation not in ['Determinant', 'Inverse', 'Eigenvalue', 'Eigenvector'] : raise RuntimeError, 'Unknown quality measure ['+operation+']'+\ 'Supported are [Determinant, Inverse, Eigenvalue, Eigenvector]' dataset = narray.DataSet() if not dataset : raise RuntimeError, 'narray is not associated with a dataset.' if dataset_adapter.ArrayAssociation.FIELD == narray.Association : raise RuntimeError, 'Unknown data association. Data should be associated with points or cells.' if narray.ndim != 3 : raise RuntimeError, operation+' only works for an array of matrices(3D array).'\ 'Input shape ' + narray.shape elif narray.shape[1] != narray.shape[2] : raise RuntimeError, operation+' requires an array of 2D square matrices.'\ 'Input shape ' + narray.shape # numpy_to_vtk converts only contiguous arrays if not narray.flags.contiguous : narray = narray.copy() # Reshape is necessary because numpy_support.numpy_to_vtk only works with 2D or # less arrays. nrows = narray.shape[0] ncols = narray.shape[1] * narray.shape[2] narray = narray.reshape(nrows, ncols) ds = dataset.NewInstance() ds.UnRegister(None) ds.ShallowCopy(dataset.VTKObject) varray = numpy_support.numpy_to_vtk(narray) varray.SetName('tensors') filter = vtkMatrixMathFilter() if operation == 'Determinant' : filter.SetOperationToDeterminant() elif operation == 'Inverse' : filter.SetOperationToInverse() elif operation == 'Eigenvalue' : filter.SetOperationToEigenvalue() elif operation == 'Eigenvector' : filter.SetOperationToEigenvector() if dataset_adapter.ArrayAssociation.POINT == narray.Association : ds.GetPointData().SetTensors(varray) # filter.SetQualityTypeToPointQuality() elif dataset_adapter.ArrayAssociation.CELL == narray.Association : ds.GetCellData().SetTensors(varray) # filter.SetQualityTypeToCellQuality() filter.SetInputData(ds) filter.Update() if dataset_adapter.ArrayAssociation.POINT == narray.Association : varray = filter.GetOutput().GetPointData().GetArray(operation) elif dataset_adapter.ArrayAssociation.CELL == narray.Association : varray = filter.GetOutput().GetCellData().GetArray(operation) ans = dataset_adapter.vtkDataArrayToVTKArray(varray, dataset) # The association information has been lost over the vtk filter # we must reconstruct it otherwise lower pipeline will be broken. ans.Association = narray.Association return ans