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 maskImageWithMesh(
    image,
    mesh,
    filter_with_field=None,
    verbose=0):

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

    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
def computePseudoProlateSpheroidalCoordinatesAndBasisForBiV(
        points,
        iarray_regions,
        farray_c,
        farray_l,
        farray_eL,
        pdata_endLV,
        pdata_endRV,
        pdata_epi,
        iarray_part_id=None,
        verbose=1):

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

    myVTK.myPrint(verbose, "Computing surface cell normals...")

    pdata_endLV = myVTK.addPDataNormals(
        pdata=pdata_endLV,
        verbose=verbose-1)
    pdata_endRV = myVTK.addPDataNormals(
        pdata=pdata_endRV,
        verbose=verbose-1)
    pdata_epi = myVTK.addPDataNormals(
        pdata=pdata_epi,
        verbose=verbose)

    myVTK.myPrint(verbose, "Initializing surface 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)

    myVTK.myPrint(verbose, "Computing local prolate spheroidal directions...")

    n_points = points.GetNumberOfPoints()

    farray_rr = myVTK.createFloatArray("rr", 1, n_points)
    farray_cc = myVTK.createFloatArray("cc", 1, n_points)
    farray_ll = myVTK.createFloatArray("ll", 1, n_points)

    farray_eRR = myVTK.createFloatArray("eRR", 3, n_points)
    farray_eCC = myVTK.createFloatArray("eCC", 3, n_points)
    farray_eLL = myVTK.createFloatArray("eLL", 3, n_points)

    c_lst_FWLV = numpy.array([farray_c.GetTuple(k_point)[0] for k_point in xrange(n_points) if (iarray_regions.GetTuple(k_point)[0] == 0)])
    (c_avg_FWLV, c_std_FWLV) = myVTK.computeMeanStddevAngles(
        angles=c_lst_FWLV,
        angles_in_degrees=False,
        angles_in_pm_pi=False)
    myVTK.myPrint(verbose, "c_avg_FWLV = " + str(c_avg_FWLV))
    c_lst_FWLV = (((c_lst_FWLV-c_avg_FWLV+math.pi)%(2*math.pi))-math.pi+c_avg_FWLV)
    c_min_FWLV = min(c_lst_FWLV)
    c_max_FWLV = max(c_lst_FWLV)
    myVTK.myPrint(verbose, "c_min_FWLV = " + str(c_min_FWLV))
    myVTK.myPrint(verbose, "c_max_FWLV = " + str(c_max_FWLV))

    c_lst_S = numpy.array([farray_c.GetTuple(k_point)[0] for k_point in xrange(n_points) if (iarray_regions.GetTuple(k_point)[0] == 1)])
    (c_avg_S, c_std_S) = myVTK.computeMeanStddevAngles(
        angles=c_lst_S,
        angles_in_degrees=False,
        angles_in_pm_pi=False)
    myVTK.myPrint(verbose, "c_avg_S = " + str(c_avg_S))
    c_lst_S = (((c_lst_S-c_avg_S+math.pi)%(2*math.pi))-math.pi+c_avg_S)
    c_min_S = min(c_lst_S)
    c_max_S = max(c_lst_S)
    myVTK.myPrint(verbose, "c_min_S = " + str(c_min_S))
    myVTK.myPrint(verbose, "c_max_S = " + str(c_max_S))

    c_lst_FWRV = numpy.array([farray_c.GetTuple(k_point)[0] for k_point in xrange(n_points) if (iarray_regions.GetTuple(k_point)[0] == 2)])
    (c_avg_FWRV, c_std_FWRV) = myVTK.computeMeanStddevAngles(
        angles=c_lst_FWRV,
        angles_in_degrees=False,
        angles_in_pm_pi=False)
    myVTK.myPrint(verbose, "c_avg_FWRV = " + str(c_avg_FWRV))
    c_lst_FWRV = (((c_lst_FWRV-c_avg_FWRV+math.pi)%(2*math.pi))-math.pi+c_avg_FWRV)
    c_min_FWRV = min(c_lst_FWRV)
    c_max_FWRV = max(c_lst_FWRV)
    myVTK.myPrint(verbose, "c_min_FWRV = " + str(c_min_FWRV))
    myVTK.myPrint(verbose, "c_max_FWRV = " + str(c_max_FWRV))

    l_lst = [farray_l.GetTuple(k_point)[0] for k_point in xrange(n_points)]
    l_min = min(l_lst)
    l_max = max(l_lst)

    for k_point in xrange(n_points):
        if (iarray_part_id is not None) and (int(iarray_part_id.GetTuple(k_point)[0]) > 0):
            rr = 0.
            cc = 0.
            ll = 0.
            eRR = [1.,0.,0.]
            eCC = [0.,1.,0.]
            eLL = [0.,0.,1.]

        else:
            point = numpy.array(points.GetPoint(k_point))
            region_id = iarray_regions.GetTuple(k_point)[0]

            if (region_id == 0):
                cell_locator_endLV.FindClosestPoint(
                    point,
                    closest_point_endLV,
                    generic_cell,
                    cellId_endLV,
                    subId,
                    dist_endLV)
                cell_locator_epi.FindClosestPoint(
                    point,
                    closest_point_epi,
                    generic_cell,
                    cellId_epi,
                    subId,
                    dist_epi)

                rr = dist_endLV/(dist_endLV+dist_epi)

                c = farray_c.GetTuple(k_point)[0]
                c = (((c-c_avg_FWLV+math.pi)%(2*math.pi))-math.pi+c_avg_FWLV)
                cc = (c-c_min_FWLV) / (c_max_FWLV-c_min_FWLV)

                l = farray_l.GetTuple(k_point)[0]
                ll = (l-l_min) / (l_max-l_min)

                normal_endLV = numpy.reshape(pdata_endLV.GetCellData().GetNormals().GetTuple(cellId_endLV), (3))
                normal_epi = numpy.reshape(pdata_epi.GetCellData().GetNormals().GetTuple(cellId_epi), (3))
                eRR  = (1.-rr) * normal_endLV + rr * normal_epi
                eRR /= numpy.linalg.norm(eRR)

                eL = numpy.reshape(farray_eL.GetTuple(k_point), (3))
                eCC  = numpy.cross(eL, eRR)
                eCC /= numpy.linalg.norm(eCC)

                eLL  = numpy.cross(eRR, eCC)
            elif (region_id == 1):
                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)

                rr = dist_endLV/(dist_endLV+dist_endRV)

                c = farray_c.GetTuple(k_point)[0]
                c = (((c-c_avg_S+math.pi)%(2*math.pi))-math.pi+c_avg_S)
                cc = (c-c_min_S) / (c_max_S-c_min_S)

                l = farray_l.GetTuple(k_point)[0]
                ll = (l-l_min) / (l_max-l_min)

                normal_endLV = numpy.reshape(pdata_endLV.GetCellData().GetNormals().GetTuple(cellId_endLV), (3))
                normal_endRV = numpy.reshape(pdata_endRV.GetCellData().GetNormals().GetTuple(cellId_endRV), (3))
                eRR  = (1.-rr) * normal_endLV - rr * normal_endRV
                eRR /= numpy.linalg.norm(eRR)

                eL = numpy.reshape(farray_eL.GetTuple(k_point), (3))
                eCC  = numpy.cross(eL, eRR)
                eCC /= numpy.linalg.norm(eCC)

                eLL = numpy.cross(eRR, eCC)
            if (region_id == 2):
                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)

                rr = dist_endRV/(dist_endRV+dist_epi)

                c = farray_c.GetTuple(k_point)[0]
                c = (((c-c_avg_FWRV+math.pi)%(2*math.pi))-math.pi+c_avg_FWRV)
                cc = (c-c_min_FWRV) / (c_max_FWRV-c_min_FWRV)

                l = farray_l.GetTuple(k_point)[0]
                ll = (l-l_min) / (l_max-l_min)

                normal_endRV = numpy.reshape(pdata_endRV.GetCellData().GetNormals().GetTuple(cellId_endRV), (3))
                normal_epi = numpy.reshape(pdata_epi.GetCellData().GetNormals().GetTuple(cellId_epi), (3))
                eRR  = (1.-rr) * normal_endRV + rr * normal_epi
                eRR /= numpy.linalg.norm(eRR)

                eL = numpy.reshape(farray_eL.GetTuple(k_point), (3))
                eCC  = numpy.cross(eL, eRR)
                eCC /= numpy.linalg.norm(eCC)

                eLL = numpy.cross(eRR, eCC)

        farray_rr.InsertTuple(k_point, [rr])
        farray_cc.InsertTuple(k_point, [cc])
        farray_ll.InsertTuple(k_point, [ll])
        farray_eRR.InsertTuple(k_point, eRR)
        farray_eCC.InsertTuple(k_point, eCC)
        farray_eLL.InsertTuple(k_point, eLL)

    return (farray_rr,
            farray_cc,
            farray_ll,
            farray_eRR,
            farray_eCC,
            farray_eLL)
def computePseudoProlateSpheroidalCoordinatesAndBasisForLV(
        points,
        farray_c,
        farray_l,
        farray_eL,
        pdata_end,
        pdata_epi,
        iarray_part_id=None,
        verbose=1):

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

    myVTK.myPrint(verbose, "Computing surface cell normals...")

    pdata_end = myVTK.addPDataNormals(
        pdata=pdata_end,
        verbose=verbose-1)
    pdata_epi = myVTK.addPDataNormals(
        pdata=pdata_epi,
        verbose=verbose)

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

    (cell_locator_end,
     closest_point_end,
     generic_cell,
     cellId_end,
     subId,
     dist_end) = myVTK.getCellLocator(
         mesh=pdata_end,
         verbose=verbose-1)
    (cell_locator_epi,
     closest_point_epi,
     generic_cell,
     cellId_epi,
     subId,
     dist_epi) = myVTK.getCellLocator(
         mesh=pdata_epi,
         verbose=verbose-1)

    myVTK.myPrint(verbose, "Computing local prolate spheroidal directions...")

    n_points = points.GetNumberOfPoints()

    farray_rr = myVTK.createFloatArray("rr", 1, n_points)
    farray_cc = myVTK.createFloatArray("cc", 1, n_points)
    farray_ll = myVTK.createFloatArray("ll", 1, n_points)

    farray_eRR = myVTK.createFloatArray("eRR", 3, n_points)
    farray_eCC = myVTK.createFloatArray("eCC", 3, n_points)
    farray_eLL = myVTK.createFloatArray("eLL", 3, n_points)

    if (n_points == 0):
        return (farray_rr,
                farray_cc,
                farray_ll,
                farray_eRR,
                farray_eCC,
                farray_eLL)

    c_lst = [farray_c.GetTuple(k_point)[0] for k_point in xrange(n_points)]
    c_min = min(c_lst)
    c_max = max(c_lst)

    l_lst = [farray_l.GetTuple(k_point)[0] for k_point in xrange(n_points)]
    l_min = min(l_lst)
    l_max = max(l_lst)

    for k_point in xrange(n_points):
        if (iarray_part_id is not None) and (int(iarray_part_id.GetTuple(k_point)[0]) > 0):
            rr = 0.
            cc = 0.
            ll = 0.
            eRR = [1.,0.,0.]
            eCC = [0.,1.,0.]
            eLL = [0.,0.,1.]

        else:
            point = numpy.array(points.GetPoint(k_point))
            cell_locator_end.FindClosestPoint(
                point,
                closest_point_end,
                generic_cell,
                cellId_end,
                subId,
                dist_end)
            cell_locator_epi.FindClosestPoint(
                point,
                closest_point_epi,
                generic_cell,
                cellId_epi,
                subId,
                dist_epi)

            rr = dist_end/(dist_end+dist_epi)

            c = farray_c.GetTuple(k_point)[0]
            cc = (c-c_min) / (c_max-c_min)

            l = farray_l.GetTuple(k_point)[0]
            ll = (l-l_min) / (l_max-l_min)

            normal_end = numpy.reshape(pdata_end.GetCellData().GetNormals().GetTuple(cellId_end), (3))
            normal_epi = numpy.reshape(pdata_epi.GetCellData().GetNormals().GetTuple(cellId_epi), (3))
            eRR  = (1.-rr) * normal_end + rr * normal_epi
            eRR /= numpy.linalg.norm(eRR)

            eL = numpy.reshape(farray_eL.GetTuple(k_point), (3))
            eCC  = numpy.cross(eL, eRR)
            eCC /= numpy.linalg.norm(eCC)

            eLL = numpy.cross(eRR, eCC)

        farray_rr.InsertTuple(k_point, [rr])
        farray_cc.InsertTuple(k_point, [cc])
        farray_ll.InsertTuple(k_point, [ll])
        farray_eRR.InsertTuple(k_point, eRR)
        farray_eCC.InsertTuple(k_point, eCC)
        farray_eLL.InsertTuple(k_point, eLL)

    return (farray_rr,
            farray_cc,
            farray_ll,
            farray_eRR,
            farray_eCC,
            farray_eLL)
def computePseudoProlateSpheroidalCoordinatesAndBasisForLV(
        points,
        farray_c,
        farray_l,
        farray_eL,
        pdata_end,
        pdata_epi,
        iarray_part_id=None,
        verbose=0):

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

    myVTK.myPrint(verbose - 1, "Computing surface cell normals...")

    pdata_end = myVTK.addPDataNormals(pdata=pdata_end, verbose=verbose - 1)
    pdata_epi = myVTK.addPDataNormals(pdata=pdata_epi, verbose=verbose - 1)

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

    (cell_locator_end, closest_point_end, generic_cell, cellId_end, subId,
     dist_end) = myVTK.getCellLocator(mesh=pdata_end, verbose=verbose - 1)
    (cell_locator_epi, closest_point_epi, generic_cell, cellId_epi, subId,
     dist_epi) = myVTK.getCellLocator(mesh=pdata_epi, verbose=verbose - 1)

    myVTK.myPrint(verbose - 1,
                  "Computing local prolate spheroidal directions...")

    n_points = points.GetNumberOfPoints()

    farray_rr = myVTK.createFloatArray("rr", 1, n_points)
    farray_cc = myVTK.createFloatArray("cc", 1, n_points)
    farray_ll = myVTK.createFloatArray("ll", 1, n_points)

    farray_eRR = myVTK.createFloatArray("eRR", 3, n_points)
    farray_eCC = myVTK.createFloatArray("eCC", 3, n_points)
    farray_eLL = myVTK.createFloatArray("eLL", 3, n_points)

    if (n_points == 0):
        return (farray_rr, farray_cc, farray_ll, farray_eRR, farray_eCC,
                farray_eLL)

    c_lst = [farray_c.GetTuple1(k_point) for k_point in xrange(n_points)]
    c_min = min(c_lst)
    c_max = max(c_lst)

    l_lst = [farray_l.GetTuple1(k_point) for k_point in xrange(n_points)]
    l_min = min(l_lst)
    l_max = max(l_lst)

    pdata_end_normals = pdata_end.GetCellData().GetNormals()
    pdata_epi_normals = pdata_epi.GetCellData().GetNormals()
    pdata_end_normal = numpy.empty(3)
    pdata_epi_normal = numpy.empty(3)

    eL = numpy.empty(3)

    point = numpy.empty(3)
    for k_point in xrange(n_points):
        if (iarray_part_id
                is not None) and (int(iarray_part_id.GetTuple1(k_point)) > 0):
            rr = 0.
            cc = 0.
            ll = 0.
            eRR = numpy.array([1., 0., 0.])
            eCC = numpy.array([0., 1., 0.])
            eLL = numpy.array([0., 0., 1.])

        else:
            points.GetPoint(k_point, point)
            cell_locator_end.FindClosestPoint(point, closest_point_end,
                                              generic_cell, cellId_end, subId,
                                              dist_end)
            cell_locator_epi.FindClosestPoint(point, closest_point_epi,
                                              generic_cell, cellId_epi, subId,
                                              dist_epi)

            rr = dist_end / (dist_end + dist_epi)

            c = farray_c.GetTuple1(k_point)
            cc = (c - c_min) / (c_max - c_min)

            l = farray_l.GetTuple1(k_point)
            ll = (l - l_min) / (l_max - l_min)

            pdata_end_normals.GetTuple(cellId_end, pdata_end_normal)
            pdata_epi_normals.GetTuple(cellId_epi, pdata_epi_normal)
            eRR = (1. - rr) * pdata_end_normal + rr * pdata_epi_normal
            eRR /= numpy.linalg.norm(eRR)

            farray_eL.GetTuple(k_point, eL)
            eCC = numpy.cross(eL, eRR)
            eCC /= numpy.linalg.norm(eCC)

            eLL = numpy.cross(eRR, eCC)

        farray_rr.SetTuple1(k_point, rr)
        farray_cc.SetTuple1(k_point, cc)
        farray_ll.SetTuple1(k_point, ll)
        farray_eRR.SetTuple(k_point, eRR)
        farray_eCC.SetTuple(k_point, eCC)
        farray_eLL.SetTuple(k_point, eLL)

    return (farray_rr, farray_cc, farray_ll, farray_eRR, farray_eCC,
            farray_eLL)
def computePseudoProlateSpheroidalCoordinatesAndBasisForBiV(
        points,
        iarray_regions,
        farray_c,
        farray_l,
        farray_eL,
        pdata_endLV,
        pdata_endRV,
        pdata_epi,
        iarray_part_id=None,
        verbose=0):

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

    myVTK.myPrint(verbose - 1, "Computing surface cell normals...")

    pdata_endLV = myVTK.addPDataNormals(pdata=pdata_endLV, verbose=verbose - 1)
    pdata_endRV = myVTK.addPDataNormals(pdata=pdata_endRV, verbose=verbose - 1)
    pdata_epi = myVTK.addPDataNormals(pdata=pdata_epi, verbose=verbose - 1)

    myVTK.myPrint(verbose - 1, "Initializing surface 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)

    myVTK.myPrint(verbose - 1,
                  "Computing local prolate spheroidal directions...")

    n_points = points.GetNumberOfPoints()

    farray_rr = myVTK.createFloatArray("rr", 1, n_points)
    farray_cc = myVTK.createFloatArray("cc", 1, n_points)
    farray_ll = myVTK.createFloatArray("ll", 1, n_points)

    farray_eRR = myVTK.createFloatArray("eRR", 3, n_points)
    farray_eCC = myVTK.createFloatArray("eCC", 3, n_points)
    farray_eLL = myVTK.createFloatArray("eLL", 3, n_points)

    c_lst_FWLV = numpy.array([
        farray_c.GetTuple1(k_point) for k_point in xrange(n_points)
        if (iarray_regions.GetTuple1(k_point) == 0)
    ])
    (c_avg_FWLV,
     c_std_FWLV) = myVTK.computeMeanStddevAngles(angles=c_lst_FWLV,
                                                 angles_in_degrees=False,
                                                 angles_in_pm_pi=False)
    myVTK.myPrint(verbose - 1, "c_avg_FWLV = " + str(c_avg_FWLV))
    c_lst_FWLV = (((c_lst_FWLV - c_avg_FWLV + math.pi) % (2 * math.pi)) -
                  math.pi + c_avg_FWLV)
    c_min_FWLV = min(c_lst_FWLV)
    c_max_FWLV = max(c_lst_FWLV)
    myVTK.myPrint(verbose - 1, "c_min_FWLV = " + str(c_min_FWLV))
    myVTK.myPrint(verbose - 1, "c_max_FWLV = " + str(c_max_FWLV))

    c_lst_S = numpy.array([
        farray_c.GetTuple1(k_point) for k_point in xrange(n_points)
        if (iarray_regions.GetTuple1(k_point) == 1)
    ])
    (c_avg_S, c_std_S) = myVTK.computeMeanStddevAngles(angles=c_lst_S,
                                                       angles_in_degrees=False,
                                                       angles_in_pm_pi=False)
    myVTK.myPrint(verbose - 1, "c_avg_S = " + str(c_avg_S))
    c_lst_S = (((c_lst_S - c_avg_S + math.pi) % (2 * math.pi)) - math.pi +
               c_avg_S)
    c_min_S = min(c_lst_S)
    c_max_S = max(c_lst_S)
    myVTK.myPrint(verbose - 1, "c_min_S = " + str(c_min_S))
    myVTK.myPrint(verbose - 1, "c_max_S = " + str(c_max_S))

    c_lst_FWRV = numpy.array([
        farray_c.GetTuple1(k_point) for k_point in xrange(n_points)
        if (iarray_regions.GetTuple1(k_point) == 2)
    ])
    (c_avg_FWRV,
     c_std_FWRV) = myVTK.computeMeanStddevAngles(angles=c_lst_FWRV,
                                                 angles_in_degrees=False,
                                                 angles_in_pm_pi=False)
    myVTK.myPrint(verbose - 1, "c_avg_FWRV = " + str(c_avg_FWRV))
    c_lst_FWRV = (((c_lst_FWRV - c_avg_FWRV + math.pi) % (2 * math.pi)) -
                  math.pi + c_avg_FWRV)
    c_min_FWRV = min(c_lst_FWRV)
    c_max_FWRV = max(c_lst_FWRV)
    myVTK.myPrint(verbose - 1, "c_min_FWRV = " + str(c_min_FWRV))
    myVTK.myPrint(verbose - 1, "c_max_FWRV = " + str(c_max_FWRV))

    l_lst = [farray_l.GetTuple1(k_point) for k_point in xrange(n_points)]
    l_min = min(l_lst)
    l_max = max(l_lst)

    point = numpy.empty(3)
    for k_point in xrange(n_points):
        if (iarray_part_id
                is not None) and (int(iarray_part_id.GetTuple1(k_point)) > 0):
            rr = 0.
            cc = 0.
            ll = 0.
            eRR = [1., 0., 0.]
            eCC = [0., 1., 0.]
            eLL = [0., 0., 1.]

        else:
            points.GetPoint(k_point, point)
            region_id = iarray_regions.GetTuple1(k_point)

            if (region_id == 0):
                cell_locator_endLV.FindClosestPoint(point, closest_point_endLV,
                                                    generic_cell, cellId_endLV,
                                                    subId, dist_endLV)
                cell_locator_epi.FindClosestPoint(point, closest_point_epi,
                                                  generic_cell, cellId_epi,
                                                  subId, dist_epi)

                rr = dist_endLV / (dist_endLV + dist_epi)

                c = farray_c.GetTuple1(k_point)
                c = (((c - c_avg_FWLV + math.pi) % (2 * math.pi)) - math.pi +
                     c_avg_FWLV)
                cc = (c - c_min_FWLV) / (c_max_FWLV - c_min_FWLV)

                l = farray_l.GetTuple1(k_point)
                ll = (l - l_min) / (l_max - l_min)

                normal_endLV = numpy.reshape(
                    pdata_endLV.GetCellData().GetNormals().GetTuple(
                        cellId_endLV), (3))
                normal_epi = numpy.reshape(
                    pdata_epi.GetCellData().GetNormals().GetTuple(cellId_epi),
                    (3))
                eRR = (1. - rr) * normal_endLV + rr * normal_epi
                eRR /= numpy.linalg.norm(eRR)

                eL = numpy.reshape(farray_eL.GetTuple(k_point), (3))
                eCC = numpy.cross(eL, eRR)
                eCC /= numpy.linalg.norm(eCC)

                eLL = numpy.cross(eRR, eCC)
            elif (region_id == 1):
                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)

                rr = dist_endLV / (dist_endLV + dist_endRV)

                c = farray_c.GetTuple1(k_point)
                c = (((c - c_avg_S + math.pi) % (2 * math.pi)) - math.pi +
                     c_avg_S)
                cc = (c - c_min_S) / (c_max_S - c_min_S)

                l = farray_l.GetTuple1(k_point)
                ll = (l - l_min) / (l_max - l_min)

                normal_endLV = numpy.reshape(
                    pdata_endLV.GetCellData().GetNormals().GetTuple(
                        cellId_endLV), (3))
                normal_endRV = numpy.reshape(
                    pdata_endRV.GetCellData().GetNormals().GetTuple(
                        cellId_endRV), (3))
                eRR = (1. - rr) * normal_endLV - rr * normal_endRV
                eRR /= numpy.linalg.norm(eRR)

                eL = numpy.reshape(farray_eL.GetTuple(k_point), (3))
                eCC = numpy.cross(eL, eRR)
                eCC /= numpy.linalg.norm(eCC)

                eLL = numpy.cross(eRR, eCC)
            if (region_id == 2):
                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)

                rr = dist_endRV / (dist_endRV + dist_epi)

                c = farray_c.GetTuple1(k_point)
                c = (((c - c_avg_FWRV + math.pi) % (2 * math.pi)) - math.pi +
                     c_avg_FWRV)
                cc = (c - c_min_FWRV) / (c_max_FWRV - c_min_FWRV)

                l = farray_l.GetTuple1(k_point)
                ll = (l - l_min) / (l_max - l_min)

                normal_endRV = numpy.reshape(
                    pdata_endRV.GetCellData().GetNormals().GetTuple(
                        cellId_endRV), (3))
                normal_epi = numpy.reshape(
                    pdata_epi.GetCellData().GetNormals().GetTuple(cellId_epi),
                    (3))
                eRR = (1. - rr) * normal_endRV + rr * normal_epi
                eRR /= numpy.linalg.norm(eRR)

                eL = numpy.reshape(farray_eL.GetTuple(k_point), (3))
                eCC = numpy.cross(eL, eRR)
                eCC /= numpy.linalg.norm(eCC)

                eLL = numpy.cross(eRR, eCC)

        farray_rr.SetTuple1(k_point, rr)
        farray_cc.SetTuple1(k_point, cc)
        farray_ll.SetTuple1(k_point, ll)
        farray_eRR.SetTuple(k_point, eRR)
        farray_eCC.SetTuple(k_point, eCC)
        farray_eLL.SetTuple(k_point, eLL)

    return (farray_rr, farray_cc, farray_ll, farray_eRR, farray_eCC,
            farray_eLL)
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