def getThresholdedUGrid(
        ugrid,
        field_support,
        field_name,
        threshold_value,
        threshold_by_upper_or_lower,
        verbose=0):

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

    threshold = vtk.vtkThreshold()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        threshold.SetInputData(ugrid)
    else:
        threshold.SetInput(ugrid)
    if (field_support == "points"):
        association = vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS
    elif (field_support == "cells"):
        association = vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS
    threshold.SetInputArrayToProcess(0, 0, 0, association, field_name)
    if (threshold_by_upper_or_lower == "upper"):
        threshold.ThresholdByUpper(threshold_value)
    elif (threshold_by_upper_or_lower == "lower"):
        threshold.ThresholdByLower(threshold_value)
    threshold.Update()
    thresholded_ugrid = threshold.GetOutput()

    return thresholded_ugrid
def readDynaDeformationGradients(
        mesh,
        hystory_files_basename,
        array_name,
        verbose=0):

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

    n_cells = mesh.GetNumberOfCells()

    history_files_names = [hystory_files_basename + '.history#' + str(num) for num in xrange(11,20)]

    F_list = [[0. for k_component in xrange(9)] for k_cell in xrange(n_cells)]

    for k_component in xrange(9):
        history_file = open(history_files_names[k_component], 'r')
        for line in history_file:
            if line.startswith('*') or line.startswith('$'): continue
            line = line.split()
            F_list[int(line[0])-1][k_component] = float(line[1])
        history_file.close()

    F_array = myvtk.createFloatArray(array_name, 9, n_cells)

    for k_cell in xrange(n_cells):
        F_array.SetTuple(k_cell, F_list[k_cell])

    mypy.my_print(verbose-1, "n_tuples = "+str(F_array.GetNumberOfTuples()))

    mesh.GetCellData().AddArray(F_array)
Exemplo n.º 3
0
def addPhysicalGroupToMesh(
        mesh,
        msh_filename,
        verbose=0):

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

    n_cells = mesh.GetNumberOfCells()

    iarray_part_id = myVTK.createIntArray(
        name="part_id",
        n_tuples=n_cells,
        n_components=1)
    mesh.GetCellData().AddArray(iarray_part_id)

    msh = open(msh_filename)
    context = ""
    for line in msh:
        if line.startswith("$Elements"):
            context = "reading element number"
            continue
        elif line.startswith("$EndElements"):
            context = ""
            continue

        if (context == "reading elements"):
            splitted_line = line.split()
            iarray_part_id.SetTuple(int(splitted_line[0])-1, [int(splitted_line[3])-1])
        elif (context == "reading element number"):
            n_elements = int(line)
            assert (n_elements == n_cells)
            context = "reading elements"
def getThresholdedUGrid(
        ugrid,
        field_support,
        field_name,
        threshold_value,
        threshold_by_upper_or_lower,
        verbose=0):

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

    threshold = vtk.vtkThreshold()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        threshold.SetInputData(ugrid)
    else:
        threshold.SetInput(ugrid)
    if (field_support == "points"):
        association = vtk.vtkDataObject.FIELD_ASSOCIATION_POINTS
    elif (field_support == "cells"):
        association = vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS
    threshold.SetInputArrayToProcess(0, 0, 0, association, field_name)
    if (threshold_by_upper_or_lower == "upper"):
        threshold.ThresholdByUpper(threshold_value)
    elif (threshold_by_upper_or_lower == "lower"):
        threshold.ThresholdByLower(threshold_value)
    threshold.Update()
    thresholded_ugrid = threshold.GetOutput()

    return thresholded_ugrid
def readAbaqusDeformationGradientsFromDAT(
        data_filename,
        verbose=0):

    mypy.my_print(verbose, "*** readAbaqusDeformationGradientsFromDAT: "+data_filename+" ***")

    farray_F = myvtk.createFloatArray("F", 9)

    data_file = open(data_filename, 'r')
    context = ""
    k_cell = 0
    for line in data_file:
        if (context == "reading deformation gradients"):
            #print line
            if ("MAXIMUM" in line):
                context = ""
                continue
            if ("OR" in line):
                splitted_line = line.split()
                assert (int(splitted_line[0]) == k_cell+1), "Wrong element number. Aborting."
                F_list = [float(splitted_line[ 3]), float(splitted_line[ 6]), float(splitted_line[7]),
                          float(splitted_line[ 9]), float(splitted_line[ 4]), float(splitted_line[8]),
                          float(splitted_line[10]), float(splitted_line[11]), float(splitted_line[5])]
                farray_F.InsertNextTuple(F_list)
                k_cell += 1

        if (line == "    ELEMENT  PT FOOT-       DG11        DG22        DG33        DG12        DG13        DG23        DG21        DG31        DG32    \n"):
            context = "reading deformation gradients"

    data_file.close()

    mypy.my_print(verbose-1, "n_tuples = "+str(farray_F.GetNumberOfTuples()))

    return farray_F
Exemplo n.º 6
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
Exemplo n.º 7
0
def getPDataSurfaceArea(pdata, verbose=0):

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

    mass_properties = myvtk.getMassProperties(pdata=pdata, verbose=verbose - 1)

    return mass_properties.GetSurfaceArea()
Exemplo n.º 8
0
def addMappingFromPointsToCells(ugrid_points, ugrid_cells, verbose=0):

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

    n_points = ugrid_points.GetNumberOfPoints()
    n_cells = ugrid_cells.GetNumberOfCells()
    #print "n_points = "+str(n_points)
    #print "n_cells = "+str(n_cells)

    (cell_locator, closest_point, generic_cell, k_cell, subId,
     dist) = getCellLocator(mesh=ugrid_cells, verbose=verbose - 1)

    iarray_k_cell = createIntArray(name="k_cell",
                                   n_components=1,
                                   n_tuples=n_points,
                                   verbose=verbose - 1)
    ugrid_points.GetPointData().AddArray(iarray_k_cell)
    ugrid_points.GetCellData().AddArray(iarray_k_cell)

    point = numpy.empty(3)
    for k_point in xrange(n_points):
        ugrid_points.GetPoint(k_point, point)

        cell_locator.FindClosestPoint(point, closest_point, generic_cell,
                                      k_cell, subId, dist)
        #k_cell = cell_locator.FindCell(point)
        #print "k_point = "+str(k_point)
        #print "k_cell = "+str(k_cell)

        iarray_k_cell.SetTuple1(k_point, k_cell)
Exemplo n.º 9
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
Exemplo n.º 10
0
def readDynaDeformationGradients(mesh,
                                 hystory_files_basename,
                                 array_name,
                                 verbose=0):

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

    n_cells = mesh.GetNumberOfCells()

    history_files_names = [
        hystory_files_basename + ".history#" + str(num)
        for num in range(11, 20)
    ]

    F_list = [[0. for k_component in range(9)] for k_cell in range(n_cells)]

    for k_component in range(9):
        history_file = open(history_files_names[k_component], "r")
        for line in history_file:
            if line.startswith("*") or line.startswith("$"): continue
            line = line.split()
            F_list[int(line[0]) - 1][k_component] = float(line[1])
        history_file.close()

    F_array = myvtk.createFloatArray(array_name, 9, n_cells)

    for k_cell in range(n_cells):
        F_array.SetTuple(k_cell, F_list[k_cell])

    mypy.my_print(verbose - 1,
                  "n_tuples = " + str(F_array.GetNumberOfTuples()))

    mesh.GetCellData().AddArray(F_array)
Exemplo n.º 11
0
def ugrid2pdata(
        ugrid,
        only_trianlges=False,
        verbose=0):

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

    filter_geometry = vtk.vtkGeometryFilter()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        filter_geometry.SetInputData(ugrid)
    else:
        filter_geometry.SetInput(ugrid)
    filter_geometry.Update()
    pdata = filter_geometry.GetOutput()

    if (only_trianlges):
        filter_triangle = vtk.vtkTriangleFilter()
        if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
            filter_triangle.SetInputData(pdata)
        else:
            filter_triangle.SetInput(pdata)
        filter_triangle.Update()
        pdata = filter_triangle.GetOutput()

    return pdata
def readAbaqusStressFromDAT(data_filename, verbose=0):

    mypy.my_print(verbose,
                  "*** readAbaqusStressFromDAT: " + data_filename + " ***")

    s_array = myvtk.createFloatArray("", 6)

    data_file = open(data_filename, 'r')
    context = ""
    k_cell = 0
    for line in data_file:
        if (context == "reading stresses"):
            #print line
            if ("MAXIMUM" in line):
                context = ""
                continue
            if ("OR" in line):
                splitted_line = line.split()
                assert (int(splitted_line[0]) == k_cell +
                        1), "Wrong element number. Aborting."
                s_list = [float(splitted_line[k]) for k in xrange(3, 9)]
                s_array.InsertNextTuple(s_list)
                k_cell += 1

        if (line ==
                "    ELEMENT  PT FOOT-       S11         S22         S33         S12         S13         S23     \n"
            ):
            context = "reading stresses"

    data_file.close()

    mypy.my_print(verbose - 1,
                  "n_tuples = " + str(s_array.GetNumberOfTuples()))

    return s_array
def readAbaqusDeformationGradientsFromDAT(
        data_filename,
        verbose=0):

    mypy.my_print(verbose, "*** readAbaqusDeformationGradientsFromDAT: "+data_filename+" ***")

    farray_F = myvtk.createFloatArray("F", 9)

    data_file = open(data_filename, "r")
    context = ""
    k_cell = 0
    for line in data_file:
        if (context == "reading deformation gradients"):
            #print line
            if ("MAXIMUM" in line):
                context = ""
                continue
            if ("OR" in line):
                splitted_line = line.split()
                assert (int(splitted_line[0]) == k_cell+1), "Wrong element number. Aborting."
                F_list = [float(splitted_line[ 3]), float(splitted_line[ 6]), float(splitted_line[7]),
                          float(splitted_line[ 9]), float(splitted_line[ 4]), float(splitted_line[8]),
                          float(splitted_line[10]), float(splitted_line[11]), float(splitted_line[5])]
                farray_F.InsertNextTuple(F_list)
                k_cell += 1

        if (line == "    ELEMENT  PT FOOT-       DG11        DG22        DG33        DG12        DG13        DG23        DG21        DG31        DG32    \n"):
            context = "reading deformation gradients"

    data_file.close()

    mypy.my_print(verbose-1, "n_tuples = "+str(farray_F.GetNumberOfTuples()))

    return farray_F
def moveMeshWithWorldMatrix(mesh, M, in_place=True, verbose=0):

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

    if (in_place):
        mesh2 = mesh
    else:
        if mesh.IsA("vtkPolyData"):
            mesh2 = vtk.vtkPolyData()
        elif mesh.IsA("vtkUnstructuredGrid"):
            mesh2 = vtk.vtkUnstructuredGrid()
        else:
            assert (0), "Not implemented. Aborting."
        mesh2.DeepCopy(mesh)

    n_points = mesh2.GetNumberOfPoints()
    points = mesh2.GetPoints()
    P = numpy.empty(4)
    P[3] = 1.
    Q = numpy.empty(4)

    for k_point in range(n_points):
        P[0:3] = points.GetPoint(k_point)
        #print P

        Q = numpy.dot(M, P)
        #print Q

        points.SetPoint(k_point, Q[0:3])

    return mesh2
Exemplo n.º 15
0
def getClippedPDataUsingPlane(pdata_mesh, plane_O, plane_N, verbose=0):

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

    plane = vtk.vtkPlane()
    plane.SetOrigin(plane_O)
    plane.SetNormal(plane_N)

    #mypy.my_print(verbose-1, "pdata_mesh.GetBounds() = "+str(pdata_mesh.GetBounds()))
    #mypy.my_print(verbose-1, "plane_O = "+str(plane_O))
    #mypy.my_print(verbose-1, "plane_N = "+str(plane_N))

    clip = vtk.vtkClipPolyData()
    clip.SetClipFunction(plane)
    clip.GenerateClippedOutputOn()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        clip.SetInputData(pdata_mesh)
    else:
        clip.SetInput(pdata_mesh)
    clip.Update()
    clipped0 = clip.GetOutput(0)
    clipped1 = clip.GetOutput(1)

    #mypy.my_print(verbose-1, "clipped0.GetNumberOfPoints() = "+str(clipped0.GetNumberOfPoints()))
    #mypy.my_print(verbose-1, "clipped1.GetNumberOfPoints() = "+str(clipped1.GetNumberOfPoints()))
    #mypy.my_print(verbose-1, "clipped0.GetNumberOfCells() = "+str(clipped0.GetNumberOfCells()))
    #mypy.my_print(verbose-1, "clipped1.GetNumberOfCells() = "+str(clipped1.GetNumberOfCells()))

    if (clipped0.GetNumberOfCells() > clipped1.GetNumberOfCells()):
        return clipped0, clipped1
    else:
        return clipped1, clipped0
Exemplo n.º 16
0
def getMassProperties(pdata, verbose=0):

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

    mass_properties = vtk.vtkMassProperties()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        mass_properties.SetInputData(pdata)
    else:
        mass_properties.SetInput(pdata)

    return mass_properties
Exemplo n.º 17
0
def getPointLocator(
        mesh,
        verbose=0):

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

    point_locator = vtk.vtkPointLocator()
    point_locator.SetDataSet(mesh)
    point_locator.Update()

    return point_locator
def getPointLocator(
        mesh,
        verbose=0):

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

    point_locator = vtk.vtkPointLocator()
    point_locator.SetDataSet(mesh)
    point_locator.Update()

    return point_locator
def getPDataSurfaceArea(
        pdata,
        verbose=0):

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

    mass_properties = myvtk.getMassProperties(
        pdata=pdata,
        verbose=verbose-1)

    return mass_properties.GetSurfaceArea()
Exemplo n.º 20
0
def writeSTL(pdata, filename, verbose=0):

    mypy.my_print(verbose, "*** writeSTL: " + filename + " ***")

    stl_writer = vtk.vtkSTLWriter()
    stl_writer.SetFileName(filename)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        stl_writer.SetInputData(pdata)
    else:
        stl_writer.SetInput(pdata)
    stl_writer.Update()
    stl_writer.Write()
Exemplo n.º 21
0
def addImageHessian(
        image,
        image_dimensionality=None,
        verbose=0):

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

    if (image_dimensionality is None):
        image_dimensionality = myvtk.getImageDimensionality(
            image=image,
            verbose=verbose-1)

    image_gradient = vtk.vtkImageGradient()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        image_gradient.SetInputData(image)
    else:
        image_gradient.SetInput(image)
    image_gradient.SetDimensionality(image_dimensionality)
    image_gradient.Update()
    image_w_gradient = image_gradient.GetOutput()

    image_append_components = vtk.vtkImageAppendComponents()
    for k_dim in xrange(image_dimensionality):
        image_extract_components = vtk.vtkImageExtractComponents()
        if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
            image_extract_components.SetInputData(image_w_gradient)
        else:
            image_extract_components.SetInput(image_w_gradient)
        image_extract_components.SetComponents(k_dim)
        image_extract_components.Update()
        image_w_gradient_component = image_extract_components.GetOutput()

        image_gradient = vtk.vtkImageGradient()
        if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
            image_gradient.SetInputData(image_w_gradient_component)
        else:
            image_gradient.SetInput(image_w_gradient_component)
        image_gradient.SetDimensionality(image_dimensionality)
        image_gradient.Update()
        image_w_hessian_component = image_gradient.GetOutput()
        if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
            image_append_components.AddInputData(image_w_hessian_component)
        else:
            image_append_components.AddInput(image_w_hessian_component)

    image_append_components.Update()
    image_w_hessian = image_append_components.GetOutput()

    name = image.GetPointData().GetScalars().GetName()
    image.GetPointData().AddArray(image_w_gradient.GetPointData().GetArray(name+"Gradient"))
    image.GetPointData().AddArray(image_w_hessian.GetPointData().GetArray(name+"GradientGradient"))
    image.GetPointData().SetActiveScalars(name+"GradientGradient")
Exemplo n.º 22
0
def getCellCenters(mesh, verbose=0):

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

    filter_cell_centers = vtk.vtkCellCenters()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        filter_cell_centers.SetInputData(mesh)
    else:
        filter_cell_centers.SetInput(mesh)
    filter_cell_centers.Update()
    cell_centers = filter_cell_centers.GetOutput()

    return cell_centers
Exemplo n.º 23
0
def getMassProperties(
        pdata,
        verbose=0):

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

    mass_properties = vtk.vtkMassProperties()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        mass_properties.SetInputData(pdata)
    else:
        mass_properties.SetInput(pdata)

    return mass_properties
Exemplo n.º 24
0
def pdata2ugrid(pdata, verbose=0):

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

    filter_append = vtk.vtkAppendFilter()
    if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
        filter_append.SetInputData(pdata)
    else:
        filter_append.SetInput(pdata)
    filter_append.Update()
    ugrid = filter_append.GetOutput()

    return ugrid
Exemplo n.º 25
0
def pdata2ugrid(pdata, verbose=0):

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

    filter_append = vtk.vtkAppendFilter()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        filter_append.SetInputData(pdata)
    else:
        filter_append.SetInput(pdata)
    filter_append.Update()
    ugrid = filter_append.GetOutput()

    return ugrid
Exemplo n.º 26
0
def addImageHessian(image, image_dimensionality=None, verbose=0):

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

    if (image_dimensionality is None):
        image_dimensionality = myvtk.getImageDimensionality(image=image,
                                                            verbose=verbose -
                                                            1)

    image_gradient = vtk.vtkImageGradient()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        image_gradient.SetInputData(image)
    else:
        image_gradient.SetInput(image)
    image_gradient.SetDimensionality(image_dimensionality)
    image_gradient.Update()
    image_w_gradient = image_gradient.GetOutput()

    image_append_components = vtk.vtkImageAppendComponents()
    for k_dim in xrange(image_dimensionality):
        image_extract_components = vtk.vtkImageExtractComponents()
        if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
            image_extract_components.SetInputData(image_w_gradient)
        else:
            image_extract_components.SetInput(image_w_gradient)
        image_extract_components.SetComponents(k_dim)
        image_extract_components.Update()
        image_w_gradient_component = image_extract_components.GetOutput()

        image_gradient = vtk.vtkImageGradient()
        if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
            image_gradient.SetInputData(image_w_gradient_component)
        else:
            image_gradient.SetInput(image_w_gradient_component)
        image_gradient.SetDimensionality(image_dimensionality)
        image_gradient.Update()
        image_w_hessian_component = image_gradient.GetOutput()
        if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
            image_append_components.AddInputData(image_w_hessian_component)
        else:
            image_append_components.AddInput(image_w_hessian_component)

    image_append_components.Update()
    image_w_hessian = image_append_components.GetOutput()

    name = image.GetPointData().GetScalars().GetName()
    image.GetPointData().AddArray(
        image_w_gradient.GetPointData().GetArray(name + "Gradient"))
    image.GetPointData().AddArray(
        image_w_hessian.GetPointData().GetArray(name + "GradientGradient"))
    image.GetPointData().SetActiveScalars(name + "GradientGradient")
def addMappingFromPointsToCells(
        ugrid_points,
        ugrid_cells,
        verbose=0):

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

    n_points = ugrid_points.GetNumberOfPoints()
    n_cells = ugrid_cells.GetNumberOfCells()
    #print "n_points = "+str(n_points)
    #print "n_cells = "+str(n_cells)

    (cell_locator,
     closest_point,
     generic_cell,
     k_cell,
     subId,
     dist) = getCellLocator(
         mesh=ugrid_cells,
         verbose=verbose-1)

    iarray_k_cell = createIntArray(
        name="k_cell",
        n_components=1,
        n_tuples=n_points,
        verbose=verbose-1)
    ugrid_points.GetPointData().AddArray(iarray_k_cell)
    ugrid_points.GetCellData().AddArray(iarray_k_cell)

    point = numpy.empty(3)
    for k_point in xrange(n_points):
        ugrid_points.GetPoint(
            k_point,
            point)

        cell_locator.FindClosestPoint(
            point,
            closest_point,
            generic_cell,
            k_cell,
            subId,
            dist)
        #k_cell = cell_locator.FindCell(point)
        #print "k_point = "+str(k_point)
        #print "k_cell = "+str(k_cell)

        iarray_k_cell.SetTuple1(
            k_point,
            k_cell)
Exemplo n.º 28
0
def rotatePointsWithCenterAndRotationMatrix(points, C, R, verbose=0):

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

    n_points = points.GetNumberOfPoints()

    point = numpy.empty(3)
    for k_point in range(n_points):
        points.GetPoint(k_point, point)
        #print point

        point = C + numpy.dot(R, point - C)
        #print new_point

        points.SetPoint(k_point, point)
Exemplo n.º 29
0
def readPNG(filename, verbose=0):

    mypy.my_print(verbose, "*** readPNG: " + filename + " ***")

    assert (os.path.isfile(filename)
            ), "Wrong filename (\"" + filename + "\"). Aborting."

    image_reader = vtk.vtkPNGReader()
    image_reader.SetFileName(filename)
    image_reader.Update()
    image = image_reader.GetOutput()

    mypy.my_print(verbose - 1, "n_points = " + str(image.GetNumberOfPoints()))

    return image
Exemplo n.º 30
0
def getCellLocator(mesh, verbose=0):

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

    cell_locator = vtk.vtkCellLocator()
    cell_locator.SetDataSet(mesh)
    cell_locator.Update()

    closest_point = [0.] * 3
    generic_cell = vtk.vtkGenericCell()
    k_cell = vtk.mutable(0)
    subId = vtk.mutable(0)
    dist = vtk.mutable(0.)

    return (cell_locator, closest_point, generic_cell, k_cell, subId, dist)
Exemplo n.º 31
0
def getThresholdedPData(pdata,
                        field_support,
                        field_name,
                        threshold_value,
                        threshold_by_upper_or_lower,
                        verbose=0):

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

    thresholded_ugrid = getThresholdedUGrid(pdata, field_support, field_name,
                                            threshold_value,
                                            threshold_by_upper_or_lower, False)
    thresholded_pdata = ugrid2pdata(thresholded_ugrid, False)

    return thresholded_pdata
Exemplo n.º 32
0
def writeSTL(
        pdata,
        filename,
        verbose=0):

    mypy.my_print(verbose, "*** writeSTL: "+filename+" ***")

    stl_writer = vtk.vtkSTLWriter()
    stl_writer.SetFileName(filename)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        stl_writer.SetInputData(pdata)
    else:
        stl_writer.SetInput(pdata)
    stl_writer.Update()
    stl_writer.Write()
Exemplo n.º 33
0
def getCellCenters(
        mesh,
        verbose=0):

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

    filter_cell_centers = vtk.vtkCellCenters()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        filter_cell_centers.SetInputData(mesh)
    else:
        filter_cell_centers.SetInput(mesh)
    filter_cell_centers.Update()
    cell_centers = filter_cell_centers.GetOutput()

    return cell_centers
def getClippedPDataUsingField(pdata_mesh, array_name, threshold_value, verbose=0):

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

    pdata_mesh.GetPointData().SetActiveScalars(array_name)
    clip_poly_data = vtk.vtkClipPolyData()
    if vtk.vtkVersion.GetVTKMajorVersion() >= 6:
        clip_poly_data.SetInputData(pdata_mesh)
    else:
        clip_poly_data.SetInput(pdata_mesh)
    clip_poly_data.SetValue(threshold_value)
    clip_poly_data.GenerateClippedOutputOn()
    clip_poly_data.Update()

    return clip_poly_data.GetOutput(0), clip_poly_data.GetOutput(1)
def getMaskedImageUsingMesh(
        image,
        mesh,
        filter_with_field=None,
        verbose=0):

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

    n_points = image.GetNumberOfPoints()
    farray_scalars_image = image.GetPointData().GetArray("scalars") # note that the field is defined at the points, not the cells

    (cell_locator,
     closest_point,
     generic_cell,
     k_cell,
     subId,
     dist) = myvtk.getCellLocator(
        mesh=mesh,
        verbose=verbose-1)

    if (filter_with_field != None):
        field = mesh.GetCellData().GetArray(filter_with_field[0])
        field_values = filter_with_field[1]

    points = vtk.vtkPoints()

    farray_scalars = myvtk.createFloatArray(
        name="scalars",
        n_components=1)

    point = numpy.empty(3)
    for k_point in xrange(n_points):
        image.GetPoint(k_point, point)

        k_cell = cell_locator.FindCell(point)
        if (k_cell == -1): continue
        if (filter_with_field != None) and (field.GetTuple1(k_cell) not in field_values): continue

        points.InsertNextPoint(point)
        farray_scalars.InsertNextTuple(farray_scalars_image.GetTuple(k_point))

    ugrid = vtk.vtkUnstructuredGrid()
    ugrid.SetPoints(points)
    ugrid.GetPointData().AddArray(farray_scalars)
    myvtk.addVertices(
        ugrid)

    return ugrid
def getMaskedImageUsingMesh(
        image,
        mesh,
        filter_with_field=None,
        verbose=0):

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

    n_points = image.GetNumberOfPoints()
    farray_scalars_image = image.GetPointData().GetArray("scalars") # note that the field is defined at the points, not the cells

    (cell_locator,
     closest_point,
     generic_cell,
     k_cell,
     subId,
     dist) = myvtk.getCellLocator(
        mesh=mesh,
        verbose=verbose-1)

    if (filter_with_field != None):
        field = mesh.GetCellData().GetArray(filter_with_field[0])
        field_values = filter_with_field[1]

    points = vtk.vtkPoints()

    farray_scalars = myvtk.createFloatArray(
        name="scalars",
        n_components=1)

    point = numpy.empty(3)
    for k_point in range(n_points):
        image.GetPoint(k_point, point)

        k_cell = cell_locator.FindCell(point)
        if (k_cell == -1): continue
        if (filter_with_field != None) and (field.GetTuple1(k_cell) not in field_values): continue

        points.InsertNextPoint(point)
        farray_scalars.InsertNextTuple(farray_scalars_image.GetTuple(k_point))

    ugrid = vtk.vtkUnstructuredGrid()
    ugrid.SetPoints(points)
    ugrid.GetPointData().AddArray(farray_scalars)
    myvtk.addVertices(
        ugrid)

    return ugrid
Exemplo n.º 37
0
def rotateVectorArray(old_array,
                      in_vecs=None,
                      R=None,
                      out_vecs=None,
                      verbose=0):

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

    n_components = old_array.GetNumberOfComponents()
    assert (
        n_components == 3), "Wrong number of components (n_components=" + str(
            n_components) + ", should be 3). Aborting."
    n_tuples = old_array.GetNumberOfTuples()
    new_array = myvtk.createFloatArray(old_array.GetName(), 3, n_tuples)
    new_vector = numpy.empty(3)
    old_vector = numpy.empty(3)

    for k_tuple in range(n_tuples):
        old_array.GetTuple(k_tuple, old_vector)

        if (in_vecs is None):
            in_R = numpy.eye(3)
        else:
            in_R = numpy.transpose(
                numpy.array([
                    in_vecs[0].GetTuple(k_tuple), in_vecs[1].GetTuple(k_tuple),
                    in_vecs[2].GetTuple(k_tuple)
                ]))

        if (R is None):
            R = numpy.eye(3)

        if (out_vecs is None):
            out_R = numpy.eye(3)
        else:
            out_R = numpy.transpose(
                numpy.array([
                    out_vecs[0].GetTuple(k_tuple),
                    out_vecs[1].GetTuple(k_tuple),
                    out_vecs[2].GetTuple(k_tuple)
                ]))

        full_R = numpy.dot(numpy.dot(numpy.transpose(in_R), R), out_R)

        new_vector[:] = numpy.dot(numpy.transpose(full_R), old_vector)
        new_array.SetTuple(k_tuple, new_vector)

    return new_array
Exemplo n.º 38
0
def moveMeshWithWorldMatrix(mesh, M, verbose=0):

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

    n_points = mesh.GetNumberOfPoints()
    points = mesh.GetPoints()
    P = numpy.empty(4)

    for k_point in xrange(n_points):
        P[0:3] = points.GetPoint(k_point)
        P[3] = 1.
        #print P

        P = numpy.dot(M, P)
        #print new_P

        points.SetPoint(k_point, P[0:3])
Exemplo n.º 39
0
def getClippedPDataUsingPlane(
    pdata_mesh,
    plane_O,
    plane_N,
    generate_clipped_output=False,  # 2017/12/01: this seems to mess up everything with the returned pointers…
    verbose=0):

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

    plane = vtk.vtkPlane()
    plane.SetOrigin(plane_O)
    plane.SetNormal(plane_N)

    #mypy.my_print(verbose-1, "pdata_mesh.GetBounds() = "+str(pdata_mesh.GetBounds()))
    #mypy.my_print(verbose-1, "plane_O = "+str(plane_O))
    #mypy.my_print(verbose-1, "plane_N = "+str(plane_N))

    clip = vtk.vtkClipPolyData()
    clip.SetClipFunction(plane)
    if (generate_clipped_output):
        clip.GenerateClippedOutputOn()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        clip.SetInputData(pdata_mesh)
    else:
        clip.SetInput(pdata_mesh)
    clip.Update()
    if (generate_clipped_output):
        clipped0 = clip.GetOutput(0)
        clipped1 = clip.GetOutput(1)
    else:
        clipped = clip.GetOutput()

    #mypy.my_print(verbose-1, "clipped0.GetNumberOfPoints() = "+str(clipped0.GetNumberOfPoints()))
    #mypy.my_print(verbose-1, "clipped1.GetNumberOfPoints() = "+str(clipped1.GetNumberOfPoints()))
    #mypy.my_print(verbose-1, "clipped0.GetNumberOfCells() = "+str(clipped0.GetNumberOfCells()))
    #mypy.my_print(verbose-1, "clipped1.GetNumberOfCells() = "+str(clipped1.GetNumberOfCells()))

    #if (myvtk.getPDataSurfaceArea(clipped0) >= myvtk.getPDataSurfaceArea(clipped1)):
    #return clipped0, clipped1
    #else:
    #return clipped1, clipped0

    if (generate_clipped_output):
        return clipped0, clipped1
    else:
        return clipped
Exemplo n.º 40
0
def addVertices(ugrid, verbose=0):

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

    cell = vtk.vtkVertex()
    cell_array = vtk.vtkCellArray()

    n_points = ugrid.GetPoints().GetNumberOfPoints()
    for k_point in xrange(n_points):
        cell.GetPointIds().SetId(0, k_point)
        cell_array.InsertNextCell(cell)

    ugrid.SetCells(vtk.VTK_VERTEX, cell_array)

    n_arrays = ugrid.GetPointData().GetNumberOfArrays()
    for k_array in xrange(n_arrays):
        ugrid.GetCellData().AddArray(ugrid.GetPointData().GetArray(k_array))
Exemplo n.º 41
0
def addPDataNormals(
        pdata,
        orient_outward=1,
        verbose=0):

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

    pdata_normals = vtk.vtkPolyDataNormals()
    pdata_normals.ComputePointNormalsOff()
    pdata_normals.ComputeCellNormalsOn()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        pdata_normals.SetInputData(pdata)
    else:
        pdata_normals.SetInput(pdata)
    pdata_normals.Update()
    pdata.GetCellData().SetNormals(pdata_normals.GetOutput().GetCellData().GetNormals())

    if (orient_outward):
        cell_centers = myvtk.getCellCenters(
            mesh=pdata,
            verbose=verbose-1)
        cell_center = numpy.empty(3)

        mesh_center = numpy.array(pdata.GetCenter())

        normals = pdata.GetCellData().GetNormals()
        normal = numpy.empty(3)

        cnt_pos = 0
        cnt_neg = 0
        for k_cell in xrange(pdata.GetNumberOfCells()):
            cell_centers.GetPoint(k_cell, cell_center)
            outward  = cell_center-mesh_center
            outward /= numpy.linalg.norm(outward)
            normals.GetTuple(k_cell, normal)
            proj = numpy.dot(outward, normal)
            if (proj > 0): cnt_pos += 1
            else:          cnt_neg += 1
        #print cnt_pos
        #print cnt_neg

        if (cnt_neg > cnt_pos):
            pdata_normals.FlipNormalsOn()
            pdata_normals.Update()
            pdata.GetCellData().SetNormals(pdata_normals.GetOutput().GetCellData().GetNormals())
def addStrainsFromDisplacements(mesh,
                                disp_array_name="Displacement",
                                defo_grad_array_name="DeformationGradient",
                                strain_array_name="Strain",
                                mesh_w_local_basis=None,
                                verbose=0):

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

    myvtk.addDeformationGradients(mesh=mesh,
                                  disp_array_name=disp_array_name,
                                  verbose=verbose - 1)
    myvtk.addStrainsFromDeformationGradients(
        mesh=mesh,
        defo_grad_array_name=defo_grad_array_name,
        strain_array_name=strain_array_name,
        mesh_w_local_basis=mesh_w_local_basis,
        verbose=verbose - 1)
Exemplo n.º 43
0
def writePData(pdata, filename, verbose=0):

    mypy.my_print(verbose, "*** writePData: " + filename + " ***")

    if (filename.endswith("vtk")):
        pdata_writer = vtk.vtkPolyDataWriter()
    elif (filename.endswith("vtp")):
        pdata_writer = vtk.vtkXMLPolyDataWriter()
    else:
        assert 0, "File must be .vtk or .vtp. Aborting."

    pdata_writer.SetFileName(filename)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        pdata_writer.SetInputData(pdata)
    else:
        pdata_writer.SetInput(pdata)
    pdata_writer.Update()
    pdata_writer.Write()
Exemplo n.º 44
0
def writeSGrid(sgrid, filename, verbose=0):

    mypy.my_print(verbose, "*** writeSGrid: " + filename + " ***")

    if ('vtk' in filename):
        sgrid_writer = vtk.vtkStructuredGridWriter()
    elif ('vts' in filename):
        sgrid_writer = vtk.vtkXMLStructuredGridWriter()
    else:
        assert 0, "File must be .vtk or .vts. Aborting."

    sgrid_writer.SetFileName(filename)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        sgrid_writer.SetInputData(sgrid)
    else:
        sgrid_writer.SetInput(sgrid)
    sgrid_writer.Update()
    sgrid_writer.Write()
Exemplo n.º 45
0
def getImageInterpolator(image, mode="linear", out_value=None, verbose=0):

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

    interpolator = vtk.vtkImageInterpolator()
    assert (mode in ("nearest", "linear", "cubic"))
    if (mode == "nearest"):
        interpolator.SetInterpolationModeToNearest()
    elif (mode == "linear"):
        interpolator.SetInterpolationModeToLinear()
    elif (mode == "cubic"):
        interpolator.SetInterpolationModeToCubic()
    if (out_value is not None):
        interpolator.SetOutValue(out_value)
    interpolator.Initialize(image)
    interpolator.Update()

    return interpolator
def getClippedPDataUsingField(pdata_mesh,
                              array_name,
                              threshold_value,
                              verbose=0):

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

    pdata_mesh.GetPointData().SetActiveScalars(array_name)
    clip_poly_data = vtk.vtkClipPolyData()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        clip_poly_data.SetInputData(pdata_mesh)
    else:
        clip_poly_data.SetInput(pdata_mesh)
    clip_poly_data.SetValue(threshold_value)
    clip_poly_data.GenerateClippedOutputOn()
    clip_poly_data.Update()

    return clip_poly_data.GetOutput(0), clip_poly_data.GetOutput(1)
Exemplo n.º 47
0
def rotatePoints(
        points,
        C,
        R,
        verbose=0):

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

    n_points = points.GetNumberOfPoints()

    point = numpy.empty(3)
    for k_point in xrange(n_points):
        points.GetPoint(k_point, point)
        #print point

        point = C + numpy.dot(R, point - C)
        #print new_point

        points.SetPoint(k_point, point)
Exemplo n.º 48
0
def addVertices(
        ugrid,
        verbose=0):

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

    cell = vtk.vtkVertex()
    cell_array = vtk.vtkCellArray()

    n_points = ugrid.GetPoints().GetNumberOfPoints()
    for k_point in xrange(n_points):
        cell.GetPointIds().SetId(0, k_point)
        cell_array.InsertNextCell(cell)

    ugrid.SetCells(vtk.VTK_VERTEX, cell_array)

    n_arrays = ugrid.GetPointData().GetNumberOfArrays()
    for k_array in xrange(n_arrays):
        ugrid.GetCellData().AddArray(ugrid.GetPointData().GetArray(k_array))
def moveMeshWithWorldMatrix(
        mesh,
        M,
        verbose=0):

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

    n_points = mesh.GetNumberOfPoints()
    points = mesh.GetPoints()
    P = numpy.empty(4)

    for k_point in xrange(n_points):
        P[0:3] = points.GetPoint(k_point)
        P[3] = 1.
        #print P

        P = numpy.dot(M, P)
        #print new_P

        points.SetPoint(k_point, P[0:3])
def getThresholdedPData(
        pdata,
        field_support,
        field_name,
        threshold_value,
        threshold_by_upper_or_lower,
        verbose=0):

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

    thresholded_ugrid = getThresholdedUGrid(
        pdata,
        field_support,
        field_name,
        threshold_value,
        threshold_by_upper_or_lower,
        False)
    thresholded_pdata = ugrid2pdata(thresholded_ugrid, False)

    return thresholded_pdata
Exemplo n.º 51
0
def writeUGrid(
        ugrid,
        filename,
        verbose=0):

    mypy.my_print(verbose, "*** writeUGrid: "+filename+" ***")

    if ('vtk' in filename):
        ugrid_writer = vtk.vtkUnstructuredGridWriter()
    elif ('vtu' in filename):
        ugrid_writer = vtk.vtkXMLUnstructuredGridWriter()
    else:
        assert 0, "File must be .vtk or .vtu. Aborting."

    ugrid_writer.SetFileName(filename)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        ugrid_writer.SetInputData(ugrid)
    else:
        ugrid_writer.SetInput(ugrid)
    ugrid_writer.Update()
    ugrid_writer.Write()
Exemplo n.º 52
0
def writePData(
        pdata,
        filename,
        verbose=0):

    mypy.my_print(verbose, "*** writePData: "+filename+" ***")

    if ('vtk' in filename):
        pdata_writer = vtk.vtkPolyDataWriter()
    elif ('vtp' in filename):
        pdata_writer = vtk.vtkXMLPolyDataWriter()
    else:
        assert 0, "File must be .vtk or .vtp. Aborting."

    pdata_writer.SetFileName(filename)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        pdata_writer.SetInputData(pdata)
    else:
        pdata_writer.SetInput(pdata)
    pdata_writer.Update()
    pdata_writer.Write()
def addDeformationGradients(
        mesh,
        disp_array_name="Displacement",
        defo_grad_array_name="DeformationGradient",
        verbose=0):

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

    mypy.my_print(min(verbose,1), "*** Warning: at some point the ordering of vector gradient components has changed, and uses C ordering instead of F. ***")
    if   (vtk.vtkVersion.GetVTKMajorVersion() >= 8):
        ordering = "C"
    elif (vtk.vtkVersion.GetVTKMajorVersion() == 7) and ((vtk.vtkVersion.GetVTKMinorVersion() > 0) or (vtk.vtkVersion.GetVTKBuildVersion() > 0)):
        ordering = "C"
    else:
        ordering = "F"

    n_cells = mesh.GetNumberOfCells()

    assert (mesh.GetPointData().HasArray(disp_array_name))
    mesh.GetPointData().SetActiveVectors(disp_array_name)
    cell_derivatives = vtk.vtkCellDerivatives()
    cell_derivatives.SetVectorModeToPassVectors()
    cell_derivatives.SetTensorModeToComputeGradient()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        cell_derivatives.SetInputData(mesh)
    else:
        cell_derivatives.SetInput(mesh)
    cell_derivatives.Update()
    farray_gu = cell_derivatives.GetOutput().GetCellData().GetArray("VectorGradient")

    farray_defo_grad = myvtk.createFloatArray(
        name=defo_grad_array_name,
        n_components=9,
        n_tuples=n_cells)
    mesh.GetCellData().AddArray(farray_defo_grad)
    I = numpy.eye(3)
    for k_cell in range(n_cells):
        GU = numpy.reshape(farray_gu.GetTuple(k_cell), (3,3), order=ordering)
        F = I + GU
        farray_defo_grad.SetTuple(k_cell, numpy.reshape(F, 9, order='C'))
Exemplo n.º 54
0
def readImage(
        filename,
        verbose=0):

    mypy.my_print(verbose, "*** readImage: "+filename+" ***")

    assert (os.path.isfile(filename)), "Wrong filename (\""+filename+"\"). Aborting."

    if ('vtk' in filename):
        image_reader = vtk.vtkImageDataReader()
    elif ('vti' in filename):
        image_reader = vtk.vtkXMLImageDataReader()
    else:
        assert 0, "File must be .vtk or .vti. Aborting."

    image_reader.SetFileName(filename)
    image_reader.Update()
    image = image_reader.GetOutput()

    mypy.my_print(verbose-1, "n_points = "+str(image.GetNumberOfPoints()))

    return image
def getImageDimensionality(image, verbose=0):

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

    extent = image.GetExtent()
    DX = extent[1] + 1 - extent[0]
    DY = extent[3] + 1 - extent[2]
    DZ = extent[5] + 1 - extent[4]
    if (DX > 1) and (DY > 1) and (DZ > 1):
        dimensionality = 3
    elif (DX > 1) and (DY > 1) and (DZ == 1):
        dimensionality = 2
    elif (DX > 1) and (DY == 1) and (DZ == 1):
        dimensionality = 1
    else:
        assert 0, "Wrong image dimensionality (" + str(extent) + ")"

    # dimensionality = sum([extent[2*k_dim+1]>extent[2*k_dim] for k_dim in range(3)])

    mypy.my_print(verbose - 1, "dimensionality = " + str(dimensionality))

    return dimensionality
Exemplo n.º 56
0
def getCellLocator(
        mesh,
        verbose=0):

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

    cell_locator = vtk.vtkCellLocator()
    cell_locator.SetDataSet(mesh)
    cell_locator.Update()

    closest_point = [0.]*3
    generic_cell = vtk.vtkGenericCell()
    k_cell = vtk.mutable(0)
    subId = vtk.mutable(0)
    dist = vtk.mutable(0.)

    return (cell_locator,
            closest_point,
            generic_cell,
            k_cell,
            subId,
            dist)
def getImageInterpolator(
        image,
        mode="linear",
        out_value=None,
        verbose=0):

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

    interpolator = vtk.vtkImageInterpolator()
    assert (mode in ("nearest", "linear", "cubic"))
    if (mode == "nearest"):
        interpolator.SetInterpolationModeToNearest()
    elif (mode == "linear"):
        interpolator.SetInterpolationModeToLinear()
    elif (mode == "cubic"):
        interpolator.SetInterpolationModeToCubic()
    if (out_value is not None):
        interpolator.SetOutValue(out_value)
    interpolator.Initialize(image)
    interpolator.Update()

    return interpolator
Exemplo n.º 58
0
def writeImage(
        image,
        filename,
        verbose=0):

    mypy.my_print(verbose, "*** writeImage: "+filename+" ***")

    if ('vtk' in filename):
        image_writer = vtk.vtkImageWriter()
    elif ('vti' in filename):
        image_writer = vtk.vtkXMLImageDataWriter()
    else:
        assert 0, "File must be .vtk or .vti. Aborting."

    mypy.my_print(verbose, "n_points = "+str(image.GetNumberOfPoints()))

    image_writer.SetFileName(filename)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        image_writer.SetInputData(image)
    else:
        image_writer.SetInput(image)
    image_writer.Update()
    image_writer.Write()
Exemplo n.º 59
0
def getPointsInCell(
        points,
        cell,
        verbose=0):

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

    ugrid_cell = vtk.vtkUnstructuredGrid()
    ugrid_cell.SetPoints(cell.GetPoints())
    cell = vtk.vtkHexahedron()
    for k_point in xrange(8): cell.GetPointIds().SetId(k_point, k_point)
    cell_array_cell = vtk.vtkCellArray()
    cell_array_cell.InsertNextCell(cell)
    ugrid_cell.SetCells(vtk.VTK_HEXAHEDRON, cell_array_cell)

    geometry_filter = vtk.vtkGeometryFilter()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        geometry_filter.SetInputData(ugrid_cell)
    else:
        geometry_filter.SetInput(ugrid_cell)
    geometry_filter.Update()
    cell_boundary = geometry_filter.GetOutput()

    pdata_points = vtk.vtkPolyData()
    pdata_points.SetPoints(points)

    enclosed_points_filter = vtk.vtkSelectEnclosedPoints()
    enclosed_points_filter.SetSurfaceData(cell_boundary)
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        enclosed_points_filter.SetInputData(pdata_points)
    else:
        enclosed_points_filter.SetInput(pdata_points)
    enclosed_points_filter.Update()

    points_in_cell = [k_point for k_point in xrange(points.GetNumberOfPoints()) if enclosed_points_filter.GetOutput().GetPointData().GetArray('SelectedPoints').GetTuple1(k_point)]
    return points_in_cell
Exemplo n.º 60
0
def addImageGradient(
        image,
        image_dimensionality=None,
        verbose=0):

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

    if (image_dimensionality is None):
        image_dimensionality = myvtk.getImageDimensionality(
            image=image,
            verbose=verbose-1)

    image_gradient = vtk.vtkImageGradient()
    if (vtk.vtkVersion.GetVTKMajorVersion() >= 6):
        image_gradient.SetInputData(image)
    else:
        image_gradient.SetInput(image)
    image_gradient.SetDimensionality(image_dimensionality)
    image_gradient.Update()
    image_w_grad = image_gradient.GetOutput()

    name = image.GetPointData().GetScalars().GetName()
    image.GetPointData().AddArray(image_w_gradient.GetPointData().GetArray(name+"Gradient"))
    image.GetPointData().SetActiveScalars(name+"Gradient")