Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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 = 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")

    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
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 6
0
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 = vtk.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