Ejemplo n.º 1
0
def mulArrays(array1, array2, array3=None, verbose=0):

    myVTK.myPrint(verbose, "*** mulArrays ***")

    n_components = array1.GetNumberOfComponents()
    assert (array2.GetNumberOfComponents() == n_components)

    n_tuples = array1.GetNumberOfTuples()
    assert (array2.GetNumberOfTuples() == n_tuples)

    array_type = type(array1.GetTuple(0)[0])
    assert (array_type in [int, float])
    assert (type(array2.GetTuple(0)[0]) is array_type)

    if (array3 is None):
        array3 = myVTK.createArray(name="",
                                   n_components=n_components,
                                   n_tuples=n_tuples,
                                   array_type=array_type)
    else:
        assert (array3.GetNumberOfComponents() == n_components)
        assert (array3.GetNumberOfTuples() == n_tuples)
        assert (type(array3.GetTuple(0)[0]) is array_type)

    for k_tuple in xrange(n_tuples):
        array3.SetTuple(
            k_tuple,
            numpy.array(array1.GetTuple(k_tuple)) *
            numpy.array(array2.GetTuple(k_tuple)))

    return array3
Ejemplo n.º 2
0
def subArrays(
    array1,
    array2,
    array3=None,
    verbose=0):

    mypy.my_print(verbose, "*** subArrays ***")

    n_components = array1.GetNumberOfComponents()
    assert (array2.GetNumberOfComponents() == n_components)

    n_tuples = array1.GetNumberOfTuples()
    assert (array2.GetNumberOfTuples() == n_tuples)

    array_type = type(array1.GetTuple(0)[0])
    assert (array_type in [int, float])
    assert (type(array2.GetTuple(0)[0]) is array_type)

    if (array3 is None):
        array3 = myvtk.createArray(
            name="",
            n_components=n_components,
            n_tuples=n_tuples,
            array_type=array_type)
    else:
        assert (array3.GetNumberOfComponents() == n_components)
        assert (array3.GetNumberOfTuples() == n_tuples)
        assert (type(array3.GetTuple(0)[0]) is array_type)

    for k_tuple in xrange(n_tuples):
        array3.SetTuple(
            k_tuple,
            numpy.array(array1.GetTuple(k_tuple)) - numpy.array(array2.GetTuple(k_tuple)))

    return array3
Ejemplo n.º 3
0
def subArrays(array1, array2, array3=None, verbose=0):

    mypy.my_print(verbose, "*** subArrays ***")

    n_components = array1.GetNumberOfComponents()
    assert (array2.GetNumberOfComponents() == n_components)
    mypy.my_print(verbose - 1, "n_components = " + str(n_components))

    n_tuples = array1.GetNumberOfTuples()
    assert (array2.GetNumberOfTuples() == n_tuples)
    mypy.my_print(verbose - 1, "n_tuples = " + str(n_tuples))

    array_type = type(array1.GetTuple(0)[0])
    assert (array_type in [int, float])
    assert (type(array2.GetTuple(0)[0]) is array_type)
    mypy.my_print(verbose - 1, "array_type = " + str(array_type))

    ugrid1 = vtk.vtkUnstructuredGrid()
    ugrid2 = vtk.vtkUnstructuredGrid()
    ugrid1.GetPointData().AddArray(array1)
    ugrid2.GetPointData().AddArray(array2)

    ugrid1 = dsa.WrapDataObject(ugrid1)
    ugrid2 = dsa.WrapDataObject(ugrid2)

    np_array1 = ugrid1.PointData[array1.GetName()]
    np_array2 = ugrid2.PointData[array2.GetName()]

    if (array3 is None):
        array3 = myvtk.createArray(name="",
                                   n_components=n_components,
                                   n_tuples=n_tuples,
                                   array_type=array_type)
    else:
        assert (array3.GetNumberOfComponents() == n_components)
        assert (array3.GetNumberOfTuples() == n_tuples)
        assert (type(array3.GetTuple(0)[0]) is array_type)

    ugrid3 = vtk.vtkUnstructuredGrid()
    ugrid3.GetPointData().AddArray(array3)
    ugrid3 = dsa.WrapDataObject(ugrid3)
    np_array3 = ugrid3.PointData[array3.GetName()]

    np_array3[:] = np_array1[:] - np_array2[:]

    return array3
Ejemplo n.º 4
0
def addArrays(
    array1,
    array2,
    array3=None,
    verbose=0):

    mypy.my_print(verbose, "*** addArrays ***")

    n_components = array1.GetNumberOfComponents()
    assert (array2.GetNumberOfComponents() == n_components)
    mypy.my_print(verbose-1, "n_components = "+str(n_components))

    n_tuples = array1.GetNumberOfTuples()
    assert (array2.GetNumberOfTuples() == n_tuples)
    mypy.my_print(verbose-1, "n_tuples = "+str(n_tuples))

    array_type = type(array1.GetTuple(0)[0])
    assert (array_type in [int, float])
    assert (type(array2.GetTuple(0)[0]) is array_type)
    mypy.my_print(verbose-1, "array_type = "+str(array_type))

    if (array3 is None):
        array3 = myvtk.createArray(
            name="",
            n_components=n_components,
            n_tuples=n_tuples,
            array_type=array_type)
    else:
        assert (array3.GetNumberOfComponents() == n_components)
        assert (array3.GetNumberOfTuples() == n_tuples)
        assert (type(array3.GetTuple(0)[0]) is array_type)

    for k_tuple in xrange(n_tuples):
        mypy.my_print(verbose-2, "k_tuple = "+str(k_tuple))

        array3.SetTuple(
            k_tuple,
            numpy.array(array1.GetTuple(k_tuple)) + numpy.array(array2.GetTuple(k_tuple)))

    return array3
Ejemplo n.º 5
0
def addArrays(
    array1,
    array2,
    array3=None,
    verbose=0):

    myVTK.myPrint(verbose, "*** addArrays ***")

    n_components = array1.GetNumberOfComponents()
    assert (array2.GetNumberOfComponents() == n_components)
    if (verbose >= 2): print "n_components = "+str(n_components)

    n_tuples = array1.GetNumberOfTuples()
    assert (array2.GetNumberOfTuples() == n_tuples)
    if (verbose >= 2): print "n_tuples = "+str(n_tuples)

    array_type = type(array1.GetTuple(0)[0])
    assert (array_type in [int, float])
    assert (type(array2.GetTuple(0)[0]) is array_type)
    if (verbose >= 2): print "array_type = "+str(array_type)

    if (array3 is None):
        array3 = myVTK.createArray(
            name="",
            n_components=n_components,
            n_tuples=n_tuples,
            array_type=array_type)
    else:
        assert (array3.GetNumberOfComponents() == n_components)
        assert (array3.GetNumberOfTuples() == n_tuples)
        assert (type(array3.GetTuple(0)[0]) is array_type)

    for k_tuple in xrange(n_tuples):
        if (verbose >= 2): print "k_tuple = "+str(k_tuple)

        array3.SetTuple(
            k_tuple,
            numpy.array(array1.GetTuple(k_tuple)) + numpy.array(array2.GetTuple(k_tuple)))

    return array3
def mapDataSetToCellData(
        mesh_from,
        type_of_support,
        mesh_to,
        farray_names,
        type_of_mapping="PointsWithinRadius",
        radius_is_relative=True,
        radius=0.5,
        n_closest_points=3,
        threshold_dist=None,
        threshold_val_min=None,
        threshold_val_max=None,
        verbose=0):

    myVTK.myPrint(verbose, "*** mapDataSetToCellData ***")

    if (type_of_support == "point"):
        datapoints = mesh_from.GetPoints()
        dataset = mesh_from.GetPointData()
        point_locator = myVTK.getPointLocator(
            mesh_from,
            verbose=verbose-1)
    elif (type_of_support == "cell"):
        datapoints = myVTK.getCellCenters(
            mesh=mesh_from,
            verbose=verbose-1)
        dataset = mesh_from.GetCellData()
        point_locator = myVTK.getPointLocator(
            datapoints,
            verbose=verbose-1)
    else:
        assert (0)

    pdata_cell_centers_to = myVTK.getCellCenters(mesh_to)
    n_cells = mesh_to.GetNumberOfCells()

    farrays_avg = {}
    farrays_std = {}
    for farray_name in farray_names:
        assert (dataset.HasArray(farray_name)), "mesh has no array named "+farray_name+". Aborting."

        farray_type = dataset.GetArray(farray_name).GetDataTypeAsString()
        farray_n_components = dataset.GetArray(farray_name).GetNumberOfComponents()
        farrays_avg[farray_name] = myVTK.createArray(farray_name+"_avg",
            farray_n_components,
            n_cells,
            farray_type)
        farrays_std[farray_name] = myVTK.createArray(farray_name+"_std",
            farray_n_components,
            n_cells,
            farray_type)

    points_within_radius = vtk.vtkIdList()

    for k_cell in xrange(n_cells):

        if (type_of_mapping == "ClosestPoints"):
            point_locator.FindClosestNPoints(
                n_closest_points,
                pdata_cell_centers_to.GetPoint(k_cell),
                points_within_radius)
        elif (type_of_mapping == "PointsWithinRadius"):
            if (radius_is_relative):
                l = (mesh_to.GetCell(k_cell).GetLength2())**(0.5)
                actual_radius = l*radius
            else:
                actual_radius = radius
            point_locator.FindPointsWithinRadius(
                actual_radius,
                pdata_cell_centers_to.GetPoint(k_cell),
                points_within_radius)
        else:
            assert (0)

        #points_within_radius = myVTK.findPointsInCell(mesh_from.GetPoints(), mesh_to.GetCell(k_cell))

        for farray_name in farray_names:
            if (points_within_radius.GetNumberOfIds()):
                values = [numpy.array(dataset.GetArray(farray_name).GetTuple(points_within_radius.GetId(k_id))) for k_id in xrange(points_within_radius.GetNumberOfIds()) if (threshold_dist is None) or (numpy.linalg.norm(numpy.array(datapoints.GetPoint(points_within_radius.GetId(k_id)))-numpy.array(pdata_cell_centers_to.GetPoint(k_cell))) < threshold_dist)]
                #print "values = "+str(values)
                if (threshold_val_min != None):
                    values = [value for value in values if (numpy.linalg.norm(value) > threshold_val_min)]
                if (threshold_val_max != None):
                    values = [value for value in values if (numpy.linalg.norm(value) < threshold_val_max)]
                #print "values = "+str(values)
                if (len(values)):
                    avg = numpy.mean(values, 0)
                    std = numpy.std(values, 0)
                else:
                    avg = [0]*farray_n_components
                    std = [0]*farray_n_components
            else:
                avg = [0]*farray_n_components
                std = [0]*farray_n_components
            farrays_avg[farray_name].SetTuple(k_cell, avg)
            farrays_std[farray_name].SetTuple(k_cell, std)

    for farray_name in farray_names:
        mesh_to.GetCellData().AddArray(farrays_avg[farray_name])
        mesh_to.GetCellData().AddArray(farrays_std[farray_name])
Ejemplo n.º 7
0
def mapDataSetToCellData(mesh_from,
                         type_of_support,
                         mesh_to,
                         farray_names,
                         type_of_mapping="PointsWithinRadius",
                         radius_is_relative=True,
                         radius=0.5,
                         n_closest_points=3,
                         threshold_dist=None,
                         threshold_val_min=None,
                         threshold_val_max=None,
                         verbose=0):

    myVTK.myPrint(verbose, "*** mapDataSetToCellData ***")

    if (type_of_support == "point"):
        datapoints = mesh_from.GetPoints()
        dataset = mesh_from.GetPointData()
        point_locator = myVTK.getPointLocator(mesh_from, verbose=verbose - 1)
    elif (type_of_support == "cell"):
        datapoints = myVTK.getCellCenters(mesh=mesh_from, verbose=verbose - 1)
        dataset = mesh_from.GetCellData()
        point_locator = myVTK.getPointLocator(datapoints, verbose=verbose - 1)
    else:
        assert (0)

    pdata_cell_centers_to = myVTK.getCellCenters(mesh_to)
    n_cells = mesh_to.GetNumberOfCells()

    farrays_avg = {}
    farrays_std = {}
    for farray_name in farray_names:
        assert (dataset.HasArray(farray_name)
                ), "mesh has no array named " + farray_name + ". Aborting."

        farray_type = dataset.GetArray(farray_name).GetDataTypeAsString()
        farray_n_components = dataset.GetArray(
            farray_name).GetNumberOfComponents()
        farrays_avg[farray_name] = myVTK.createArray(farray_name + "_avg",
                                                     farray_n_components,
                                                     n_cells, farray_type)
        farrays_std[farray_name] = myVTK.createArray(farray_name + "_std",
                                                     farray_n_components,
                                                     n_cells, farray_type)

    points_within_radius = vtk.vtkIdList()

    for k_cell in xrange(n_cells):

        if (type_of_mapping == "ClosestPoints"):
            point_locator.FindClosestNPoints(
                n_closest_points, pdata_cell_centers_to.GetPoint(k_cell),
                points_within_radius)
        elif (type_of_mapping == "PointsWithinRadius"):
            if (radius_is_relative):
                l = (mesh_to.GetCell(k_cell).GetLength2())**(0.5)
                actual_radius = l * radius
            else:
                actual_radius = radius
            point_locator.FindPointsWithinRadius(
                actual_radius, pdata_cell_centers_to.GetPoint(k_cell),
                points_within_radius)
        else:
            assert (0)

        #points_within_radius = myVTK.findPointsInCell(mesh_from.GetPoints(), mesh_to.GetCell(k_cell))

        for farray_name in farray_names:
            if (points_within_radius.GetNumberOfIds()):
                values = [
                    numpy.array(
                        dataset.GetArray(farray_name).GetTuple(
                            points_within_radius.GetId(k_id)))
                    for k_id in xrange(points_within_radius.GetNumberOfIds())
                    if (threshold_dist is None) or (numpy.linalg.norm(
                        numpy.array(
                            datapoints.GetPoint(
                                points_within_radius.GetId(k_id))) -
                        numpy.array(pdata_cell_centers_to.GetPoint(k_cell))) <
                                                    threshold_dist)
                ]
                #print "values = "+str(values)
                if (threshold_val_min != None):
                    values = [
                        value for value in values
                        if (numpy.linalg.norm(value) > threshold_val_min)
                    ]
                if (threshold_val_max != None):
                    values = [
                        value for value in values
                        if (numpy.linalg.norm(value) < threshold_val_max)
                    ]
                #print "values = "+str(values)
                if (len(values)):
                    avg = numpy.mean(values, 0)
                    std = numpy.std(values, 0)
                else:
                    avg = [0] * farray_n_components
                    std = [0] * farray_n_components
            else:
                avg = [0] * farray_n_components
                std = [0] * farray_n_components
            farrays_avg[farray_name].SetTuple(k_cell, avg)
            farrays_std[farray_name].SetTuple(k_cell, std)

    for farray_name in farray_names:
        mesh_to.GetCellData().AddArray(farrays_avg[farray_name])
        mesh_to.GetCellData().AddArray(farrays_std[farray_name])
def addMappingToPointData(mesh_from,
                          type_of_support,
                          mesh_to,
                          farray_names,
                          radius=1.,
                          threshold_min=None,
                          threshold_max=None,
                          verbose=0):

    mypy.my_print(verbose, "*** addMappingToPointData ***")

    if (type_of_support == "point"):
        dataset = mesh_from.GetPointData()
        point_locator = myvtk.getPointLocator(mesh_from)
    elif (type_of_support == "cell"):
        dataset = mesh_from.GetPointData()
        pdata_cell_centers_fr = myvtk.getCellCenters(mesh=mesh_from,
                                                     verbose=verbose - 1)
        point_locator = myvtk.getPointLocator(pdata_cell_centers_fr)

    n_points = mesh_to.GetNumberOfPoints()

    farrays_avg = {}
    farrays_std = {}
    farrays_rat = {}
    for farray_name in farray_names:
        assert (dataset.HasArray(farray_name)),\
            "mesh has no array named "+farray_name+". Aborting."

        farray_type = dataset.GetArray(farray_name).GetDataTypeAsString()
        farray_n_components = dataset.GetArray(
            farray_name).GetNumberOfComponents()
        farrays_avg[farray_name] = myvtk.createArray(farray_name + "_avg",
                                                     farray_n_components,
                                                     n_points, farray_type)
        farrays_std[farray_name] = myvtk.createArray(farray_name + "_std",
                                                     farray_n_components,
                                                     n_points, farray_type)
        farrays_rat[farray_name] = myvtk.createArray(farray_name + "_rat",
                                                     farray_n_components,
                                                     n_points, farray_type)

    points_within_radius = vtk.vtkIdList()

    for k_point in range(n_points):
        #point_locator.FindClosestNPoints(
        #3,
        #mesh_to.GetPoints().GetPoint(k_point),
        #points_within_radius)
        point_locator.FindPointsWithinRadius(
            radius,
            mesh_to.GetPoints().GetPoint(k_point), points_within_radius)

        for farray_name in farray_names:
            farray_n_components = dataset.GetArray(
                farray_name).GetNumberOfComponents()
            if (points_within_radius.GetNumberOfIds() > 0):
                values = [
                    numpy.array(
                        dataset.GetArray(farray_name).GetTuple(
                            points_within_radius.GetId(k_id)))
                    for k_id in range(points_within_radius.GetNumberOfIds())
                ]
                #print "values = "+str(values)
                if (threshold_min != None):
                    values = [
                        value for value in values
                        if (numpy.linalg.norm(value) > threshold_min)
                    ]
                if (threshold_max != None):
                    values = [
                        value for value in values
                        if (numpy.linalg.norm(value) < threshold_max)
                    ]
                #print "values = "+str(values)
                if (len(values) > 0):
                    avg = numpy.mean(values, 0)
                    std = numpy.std(values, 0)
                    if (avg != 0).all():
                        rat = std / avg
                    else:
                        rat = [0] * farray_n_components
                else:
                    avg = [0] * farray_n_components
                    std = [0] * farray_n_components
                    rat = [0] * farray_n_components
            else:
                avg = [0] * farray_n_components
                std = [0] * farray_n_components
                rat = [0] * farray_n_components
            farrays_avg[farray_name].SetTuple(k_point, avg)
            farrays_std[farray_name].SetTuple(k_point, std)
            farrays_rat[farray_name].SetTuple(k_point, rat)

    for farray_name in farray_names:
        mesh_to.GetPointData().AddArray(farrays_avg[farray_name])
        mesh_to.GetPointData().AddArray(farrays_std[farray_name])
        mesh_to.GetPointData().AddArray(farrays_rat[farray_name])