def createArray(
        array_name,
        n_components=1,
        n_tuples=0,
        array_type="float",
        init_to_zero=0,
        verbose=1):

    assert (type(array_type) in [type, str]), "array_type must be a type or a str. Aborting."

    if (type(array_type) is type):
        assert (array_type in [float, int]), "if a type, array_type must be equal to float or int. Aborting."

        if (array_type == float):
            return myVTK.createFloatArray(
                       name=array_name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type == int):
            return myVTK.createIntArray(
                       name=array_name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
    elif (type(array_type) is str):
        assert (array_type in ["double", "float", "int", "short"]), "if a str, array_type must be equal to double, float, int or short. Aborting."

        if (array_type == "float") or (array_type == "double"):
            return myVTK.createFloatArray(
                       name=array_name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type == "int"):
            return myVTK.createIntArray(
                       name=array_name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type == "short"):
            return myVTK.createShortArray(
                       name=array_name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
Exemplo n.º 2
0
def computeSectorsForLV(farray_rr,
                        farray_cc,
                        farray_ll,
                        n_r=1,
                        n_c=1,
                        n_l=1,
                        iarray_part_id=None,
                        verbose=0):

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

    n_cells = farray_rr.GetNumberOfTuples()

    iarray_sector = myVTK.createIntArray("sector_id", 1, n_cells)

    for k_cell in range(n_cells):
        if (iarray_part_id
                is not None) and (int(iarray_part_id.GetTuple1(k_cell)) > 0):
            sector_id = -1

        else:
            rr = farray_rr.GetTuple1(k_cell)
            cc = farray_cc.GetTuple1(k_cell)
            ll = farray_ll.GetTuple1(k_cell)

            k_r = int(rr * n_r / 1.000001)
            k_c = int(cc * n_c / 1.000001)
            k_l = int((1. - ll) * n_l / 1.000001)

            sector_id = k_l * n_c * n_r + k_c * n_r + k_r

        iarray_sector.SetTuple1(k_cell, sector_id)

    return iarray_sector
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 computeRegionsForBiV(points, pdata_endLV, pdata_endRV, pdata_epi, verbose=0):

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

    myVTK.myPrint(verbose, "Initializing cell locators...")

    (cell_locator_endLV, closest_point_endLV, generic_cell, cellId_endLV, subId, dist_endLV) = myVTK.getCellLocator(
        mesh=pdata_endLV, verbose=verbose - 1
    )
    (cell_locator_endRV, closest_point_endRV, generic_cell, cellId_endRV, subId, dist_endRV) = myVTK.getCellLocator(
        mesh=pdata_endRV, verbose=verbose - 1
    )
    (cell_locator_epi, closest_point_epi, generic_cell, cellId_epi, subId, dist_epi) = myVTK.getCellLocator(
        mesh=pdata_epi, verbose=verbose - 1
    )

    n_points = points.GetNumberOfPoints()

    iarray_region = myVTK.createIntArray("region_id", 1, n_points)

    for k_point in range(n_points):
        point = numpy.array(points.GetPoint(k_point))
        cell_locator_endLV.FindClosestPoint(point, closest_point_endLV, generic_cell, cellId_endLV, subId, dist_endLV)
        cell_locator_endRV.FindClosestPoint(point, closest_point_endRV, generic_cell, cellId_endRV, subId, dist_endRV)
        cell_locator_epi.FindClosestPoint(point, closest_point_epi, generic_cell, cellId_epi, subId, dist_epi)

        if dist_endRV == max(dist_endLV, dist_endRV, dist_epi):
            iarray_region.SetTuple(k_point, [0])
        elif dist_epi == max(dist_endLV, dist_endRV, dist_epi):
            iarray_region.SetTuple(k_point, [1])
        elif dist_endLV == max(dist_endLV, dist_endRV, dist_epi):
            iarray_region.SetTuple(k_point, [2])

    return iarray_region
def computeSectorsForLV(
        farray_rr,
        farray_cc,
        farray_ll,
        n_r=1,
        n_c=1,
        n_l=1,
        iarray_part_id=None,
        verbose=0):

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

    n_cells = farray_rr.GetNumberOfTuples()

    iarray_sector = myVTK.createIntArray("sector_id", 1, n_cells)

    for k_cell in range(n_cells):
        if (iarray_part_id is not None) and (int(iarray_part_id.GetTuple1(k_cell)) > 0):
            sector_id = -1

        else:
            rr = farray_rr.GetTuple1(k_cell)
            cc = farray_cc.GetTuple1(k_cell)
            ll = farray_ll.GetTuple1(k_cell)

            k_r = int(rr*n_r/1.000001)
            k_c = int(cc*n_c/1.000001)
            k_l = int((1.-ll)*n_l/1.000001)

            sector_id = k_l * n_c * n_r + k_c * n_r + k_r

        iarray_sector.SetTuple1(k_cell, sector_id)

    return iarray_sector
Exemplo n.º 6
0
def computeSectorsForBiV(iarray_regions,
                         farray_rr,
                         farray_cc,
                         farray_ll,
                         n_r=[1] * 3,
                         n_c=[1] * 3,
                         n_l=[1] * 3,
                         iarray_part_id=None,
                         verbose=0):

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

    n_cells = iarray_regions.GetNumberOfTuples()
    assert (farray_rr.GetNumberOfTuples() == n_cells)
    assert (farray_cc.GetNumberOfTuples() == n_cells)
    assert (farray_ll.GetNumberOfTuples() == n_cells)

    iarray_sector = myVTK.createIntArray("sector_id", 1, n_cells)

    for k_cell in range(n_cells):
        if (iarray_part_id
                is not None) and (int(iarray_part_id.GetTuple1(k_cell)) > 0):
            sector_id = -1

        else:
            region_id = int(iarray_regions.GetTuple1(k_cell))

            rr = farray_rr.GetTuple1(k_cell)
            cc = farray_cc.GetTuple1(k_cell)
            ll = farray_ll.GetTuple1(k_cell)

            k_r = int(rr * n_r[region_id] / 1.000001)
            k_c = int(cc * n_c[region_id] / 1.000001)
            k_l = int((1. - ll) * n_l[region_id] / 1.000001)

            sector_id = k_l * n_c[region_id] * n_r[region_id] \
                      + k_c * n_r[region_id] \
                      + k_r

            if (region_id >= 1):
                sector_id += n_r[0] * n_c[0] * n_l[0]
            if (region_id >= 2):
                sector_id += n_r[1] * n_c[1] * n_l[1]

        iarray_sector.SetTuple1(k_cell, sector_id)

    return iarray_sector
def computeSectorsForBiV(
        iarray_regions,
        farray_rr,
        farray_cc,
        farray_ll,
        n_r=[1]*3,
        n_c=[1]*3,
        n_l=[1]*3,
        iarray_part_id=None,
        verbose=0):

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

    n_cells = iarray_regions.GetNumberOfTuples()
    assert (farray_rr.GetNumberOfTuples() == n_cells)
    assert (farray_cc.GetNumberOfTuples() == n_cells)
    assert (farray_ll.GetNumberOfTuples() == n_cells)

    iarray_sector = myVTK.createIntArray("sector_id", 1, n_cells)

    for k_cell in range(n_cells):
        if (iarray_part_id is not None) and (int(iarray_part_id.GetTuple1(k_cell)) > 0):
            sector_id = -1

        else:
            region_id = int(iarray_regions.GetTuple1(k_cell))

            rr = farray_rr.GetTuple1(k_cell)
            cc = farray_cc.GetTuple1(k_cell)
            ll = farray_ll.GetTuple1(k_cell)

            k_r = int(    rr *n_r[region_id]/1.000001)
            k_c = int(    cc *n_c[region_id]/1.000001)
            k_l = int((1.-ll)*n_l[region_id]/1.000001)

            sector_id = k_l * n_c[region_id] * n_r[region_id] \
                      + k_c * n_r[region_id] \
                      + k_r

            if (region_id >= 1):
                sector_id += n_r[0] * n_c[0] * n_l[0]
            if (region_id >= 2):
                sector_id += n_r[1] * n_c[1] * n_l[1]

        iarray_sector.SetTuple1(k_cell, sector_id)

    return iarray_sector
def computeRegionsForBiV(points,
                         pdata_endLV,
                         pdata_endRV,
                         pdata_epi,
                         verbose=0):

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

    myVTK.myPrint(verbose - 1, "Initializing cell locators...")

    (cell_locator_endLV, closest_point_endLV, generic_cell, cellId_endLV,
     subId, dist_endLV) = myVTK.getCellLocator(mesh=pdata_endLV,
                                               verbose=verbose - 1)
    (cell_locator_endRV, closest_point_endRV, generic_cell, cellId_endRV,
     subId, dist_endRV) = myVTK.getCellLocator(mesh=pdata_endRV,
                                               verbose=verbose - 1)
    (cell_locator_epi, closest_point_epi, generic_cell, cellId_epi, subId,
     dist_epi) = myVTK.getCellLocator(mesh=pdata_epi, verbose=verbose - 1)

    n_points = points.GetNumberOfPoints()

    iarray_region = myVTK.createIntArray("region_id", 1, n_points)

    point = numpy.empty(3)
    for k_point in range(n_points):
        points.GetPoint(k_point, point)
        cell_locator_endLV.FindClosestPoint(point, closest_point_endLV,
                                            generic_cell, cellId_endLV, subId,
                                            dist_endLV)
        cell_locator_endRV.FindClosestPoint(point, closest_point_endRV,
                                            generic_cell, cellId_endRV, subId,
                                            dist_endRV)
        cell_locator_epi.FindClosestPoint(point, closest_point_epi,
                                          generic_cell, cellId_epi, subId,
                                          dist_epi)

        if (dist_endRV == max(dist_endLV, dist_endRV, dist_epi)):
            iarray_region.SetTuple1(k_point, 0)
        elif (dist_epi == max(dist_endLV, dist_endRV, dist_epi)):
            iarray_region.SetTuple1(k_point, 1)
        elif (dist_endLV == max(dist_endLV, dist_endRV, dist_epi)):
            iarray_region.SetTuple1(k_point, 2)

    return iarray_region
def readMatLabImage(
        filename,
        field_name,
        field_type,
        spacing=[1.,1.,1.],
        verbose=1):

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

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

    data = scipy.io.loadmat(filename)[field_name]

    n_pixels_x = len(data)
    n_pixels_y = len(data[0])
    n_pixels_z = len(data[0][0])

    n_pixels = n_pixels_x * n_pixels_y * n_pixels_z

    #points = vtk.vtkPoints()
    #points.SetNumberOfPoints(n_pixels)

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

    if (field_type == "int"):
        array_data = myVTK.createIntArray(field_name, 1, n_pixels)
    elif (field_type == "double"):
        array_data = myVTK.createFloatArray(field_name, 1, n_pixels)

    k_pixel = 0
    for k_z in xrange(n_pixels_z):
        for k_y in xrange(n_pixels_y):
            for k_x in xrange(n_pixels_x):
                #points.InsertPoint(k_pixel, [(k_x+0.5)/n_pixels_x,\
                                               #(k_y+0.5)/n_pixels_y,\
                                               #(k_z+0.5)/n_pixels_z])

                #cell.GetPointIds().SetId(0, k_pixel)
                #cell_array.InsertNextCell(cell)

                #array_data.InsertTuple(k_pixel, [data[n_pixels_y-1-k_y][n_pixels_x-1-k_x][k_z]])
                #array_data.InsertTuple(k_pixel, [data[n_pixels_y-1-k_y][k_x][k_z]])
                #array_data.InsertTuple(k_pixel, [data[k_y][n_pixels_x-1-k_x][k_z]])
                array_data.InsertTuple(k_pixel, [data[k_y][k_x][k_z]])
                #array_data.InsertTuple(k_pixel, [data[n_pixels_x-1-k_x][n_pixels_y-1-k_y][k_z]])
                #array_data.InsertTuple(k_pixel, [data[n_pixels_x-1-k_x][k_y][k_z]])
                #array_data.InsertTuple(k_pixel, [data[k_x][n_pixels_y-1-k_y][k_z]])
                #array_data.InsertTuple(k_pixel, [data[k_x][k_y][k_z]])

                k_pixel += 1

    #ugrid = vtk.vtkUnstructuredGrid()
    #ugrid.SetPoints(points)
    #ugrid.SetCells(vtk.VTK_VERTEX, cell_array)
    #ugrid.GetCellData().AddArray(array_data)
    #writeXMLUGrid(ugrid, case + ".vtu")

    image = vtk.vtkImageData()
    image.SetExtent(0, n_pixels_x-1, 0, n_pixels_y-1, 0, n_pixels_z-1)
    image.SetSpacing(spacing)
    image.GetPointData().AddArray(array_data)

    return image
Exemplo n.º 10
0
def createArray(name,
                n_components=1,
                n_tuples=0,
                array_type="float",
                init_to_zero=0,
                verbose=0):

    assert (type(array_type)
            in (type, str)), "array_type must be a type or a str. Aborting."

    if (type(array_type) is type):
        assert (array_type in (float, int)), "Wrong array type. Aborting."

        if (array_type == float):
            return myvtk.createFloatArray(name=name,
                                          n_components=n_components,
                                          n_tuples=n_tuples,
                                          init_to_zero=init_to_zero,
                                          verbose=verbose - 1)
        elif (array_type == int):
            return myvtk.createIntArray(name=name,
                                        n_components=n_components,
                                        n_tuples=n_tuples,
                                        init_to_zero=init_to_zero,
                                        verbose=verbose - 1)
    elif (type(array_type) is str):
        assert (array_type
                in ("double", "float", "long", "unsigned long", "int",
                    "unsigned int", "short", "unsigned short", "char",
                    "unsigned char", "int64", "uint64", "int32", "uint32",
                    "int16", "uint16", "int8",
                    "uint8")), "Wrong array type. Aborting."

        if (array_type == "float"):
            return myvtk.createFloatArray(name=name,
                                          n_components=n_components,
                                          n_tuples=n_tuples,
                                          init_to_zero=init_to_zero,
                                          verbose=verbose - 1)
        elif (array_type == "double"):
            return myvtk.createDoubleArray(name=name,
                                           n_components=n_components,
                                           n_tuples=n_tuples,
                                           init_to_zero=init_to_zero,
                                           verbose=verbose - 1)
        elif (array_type in ("long", "int64")):
            return myvtk.createLongArray(name=name,
                                         n_components=n_components,
                                         n_tuples=n_tuples,
                                         init_to_zero=init_to_zero,
                                         verbose=verbose - 1)
        elif (array_type in ("unsigned long", "uint64")):
            return myvtk.createUnsignedLongArray(name=name,
                                                 n_components=n_components,
                                                 n_tuples=n_tuples,
                                                 init_to_zero=init_to_zero,
                                                 verbose=verbose - 1)
        elif (array_type in ("int", "int32")):
            return myvtk.createIntArray(name=name,
                                        n_components=n_components,
                                        n_tuples=n_tuples,
                                        init_to_zero=init_to_zero,
                                        verbose=verbose - 1)
        elif (array_type in ("unsigned int", "uint32")):
            return myvtk.createUnsignedIntArray(name=name,
                                                n_components=n_components,
                                                n_tuples=n_tuples,
                                                init_to_zero=init_to_zero,
                                                verbose=verbose - 1)
        elif (array_type in ("short", "int16")):
            return myvtk.createShortArray(name=name,
                                          n_components=n_components,
                                          n_tuples=n_tuples,
                                          init_to_zero=init_to_zero,
                                          verbose=verbose - 1)
        elif (array_type in ("unsigned short", "uint16")):
            return myvtk.createUnsignedShortArray(name=name,
                                                  n_components=n_components,
                                                  n_tuples=n_tuples,
                                                  init_to_zero=init_to_zero,
                                                  verbose=verbose - 1)
        elif (array_type in ("char", "int8")):
            return myvtk.createCharArray(name=name,
                                         n_components=n_components,
                                         n_tuples=n_tuples,
                                         init_to_zero=init_to_zero,
                                         verbose=verbose - 1)
        elif (array_type in ("unsigned char", "uint8")):
            return myvtk.createUnsignedCharArray(name=name,
                                                 n_components=n_components,
                                                 n_tuples=n_tuples,
                                                 init_to_zero=init_to_zero,
                                                 verbose=verbose - 1)
def readMatLabImage(
        filename,
        field_name,
        field_type,
        spacing=[1.,1.,1.],
        verbose=0):

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

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

    import scipy
    data = scipy.io.loadmat(filename)[field_name]

    n_pixels_x = len(data)
    n_pixels_y = len(data[0])
    n_pixels_z = len(data[0][0])

    n_pixels = n_pixels_x * n_pixels_y * n_pixels_z

    #points = vtk.vtkPoints()
    #points.SetNumberOfPoints(n_pixels)

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

    if (field_type == "int"):
        array_data = myvtk.createIntArray(
            name=field_name,
            n_components=1,
            n_tuples=n_pixels)
    elif (field_type == "double"):
        array_data = myvtk.createFloatArray(
            name=field_name,
            n_components=1,
            n_tuples=n_pixels)

    k_pixel = 0
    for k_z in range(n_pixels_z):
        for k_y in range(n_pixels_y):
            for k_x in range(n_pixels_x):
                #points.InsertPoint(k_pixel, [(k_x+0.5)/n_pixels_x,\
                                               #(k_y+0.5)/n_pixels_y,\
                                               #(k_z+0.5)/n_pixels_z])

                #cell.GetPointIds().SetId(0, k_pixel)
                #cell_array.InsertNextCell(cell)

                #array_data.SetTuple1(k_pixel, data[n_pixels_y-1-k_y][n_pixels_x-1-k_x][k_z])
                #array_data.SetTuple1(k_pixel, data[n_pixels_y-1-k_y][k_x][k_z])
                #array_data.SetTuple1(k_pixel, data[k_y][n_pixels_x-1-k_x][k_z])
                array_data.SetTuple1(k_pixel, data[k_y][k_x][k_z])
                #array_data.SetTuple1(k_pixel, data[n_pixels_x-1-k_x][n_pixels_y-1-k_y][k_z])
                #array_data.SetTuple1(k_pixel, data[n_pixels_x-1-k_x][k_y][k_z])
                #array_data.SetTuple1(k_pixel, data[k_x][n_pixels_y-1-k_y][k_z])
                #array_data.SetTuple1(k_pixel, data[k_x][k_y][k_z])

                k_pixel += 1

    #ugrid = vtk.vtkUnstructuredGrid()
    #ugrid.SetPoints(points)
    #ugrid.SetCells(vtk.VTK_VERTEX, cell_array)
    #ugrid.GetCellData().AddArray(array_data)
    #writeXMLUGrid(ugrid, case+".vtu")

    image = vtk.vtkImageData()
    image.SetExtent(0, n_pixels_x-1, 0, n_pixels_y-1, 0, n_pixels_z-1)
    image.SetSpacing(spacing)
    image.GetPointData().AddArray(array_data)

    return image
Exemplo n.º 12
0
def createArray(
        name,
        n_components=1,
        n_tuples=0,
        array_type="float",
        init_to_zero=0,
        verbose=0):

    assert (type(array_type) in (type, str)), "array_type must be a type or a str. Aborting."

    if (type(array_type) is type):
        assert (array_type in (float, int)), "Wrong array type. Aborting."

        if (array_type == float):
            return myvtk.createFloatArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type == int):
            return myvtk.createIntArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
    elif (type(array_type) is str):
        assert (array_type in ("double", "float", "long", "unsigned long", "int", "unsigned int", "short", "unsigned short", "char", "unsigned char", "int64", "uint64", "int32", "uint32", "int16", "uint16", "int8", "uint8")), "Wrong array type. Aborting."

        if (array_type in ("float", "double")):
            return myvtk.createFloatArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type in ("long", "int64")):
            return myvtk.createLongArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type in ("unsigned long", "uint64")):
            return myvtk.createUnsignedLongArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type in ("int", "int32")):
            return myvtk.createIntArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type in ("unsigned int", "uint32")):
            return myvtk.createUnsignedIntArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type in ("short", "int16")):
            return myvtk.createShortArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type in ("unsigned short", "uint16")):
            return myvtk.createUnsignedShortArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type in ("char", "int8")):
            return myvtk.createCharArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)
        elif (array_type in ("unsigned char", "uint8")):
            return myvtk.createUnsignedCharArray(
                       name=name,
                       n_components=n_components,
                       n_tuples=n_tuples,
                       init_to_zero=init_to_zero,
                       verbose=verbose-1)