def calc_costs_plain(lenses,
                     disparities,
                     nb_offsets,
                     max_cost,
                     progress_hook=print):

    coarse_costs = dict()
    coarse_costs_merged = dict()
    fine_costs = dict()
    lens_variance = dict()

    num_lenses = len(lenses)
    num_comparisons = 0

    for i, lcoord in enumerate(lenses):
        nb_lenses = _rel_to_abs(lcoord, lenses, nb_offsets)
        lens = lenses[lcoord]

        if i % 5000 == 0:
            progress_hook("Processing lens {0}/{1} Coord: {2}".format(
                i, num_lenses, lcoord))

        fine, coarse, coarse_merged, lens_var = calc_costs_per_lens(
            lens, nb_lenses, disparities, max_cost, method)

        coarse_costs_merged[lcoord] = coarse_merged
        coarse_costs[lcoord] = coarse
        fine_costs[lcoord], _ = np.array(
            rtxdisp.merge_costs_additive(fine, max_cost))
        lens_variance[lcoord] = lens_var
        num_comparisons += len(fine)

        rtxdisp.assign_last_valid(fine_costs[lcoord])
    return fine_costs, coarse_costs, coarse_costs_merged, lens_variance, num_comparisons
def calc_costs_per_lens(lens, nb_lenses, disparities, max_cost, technique):

    cost, img, d = rtxdisp.lens_sweep(lens, nb_lenses, disparities, technique, max_cost=max_cost)
    coarse_costs = rtxdisp.sweep_to_shift_costs(cost, max_cost)
    coarse_costs_merged = rtxdisp.merge_costs_additive(coarse_costs, max_cost)
    lens_std = np.std(lens.img[lens.mask > 0])
  
    return cost, coarse_costs, coarse_costs_merged, lens_std
def calc_costs_selective_with_lut(lenses,
                                  disparities,
                                  nb_strategy,
                                  technique,
                                  nb_args,
                                  max_cost,
                                  refine=True,
                                  progress_hook=print):
    """
    it firstly calculates the fine and coarse depth map based on the first "circle" (HEX_OFFSETS[1]) with lenses of same focal lens
    then it adds the other lenses (based on strategy, but the first one is always the same) and either 
    - refine the values or
    - substitute the values
    
    Then it merges the costs and returns fine and coarse
    """
    coarse_costs = dict()
    coarse_costs_merged = dict()
    fine_costs = dict()
    lens_std = dict()
    num_lenses = len(lenses)
    num_targets = 0

    # 4+2
    # using four lenses from the first circle (the 4 corners of a virtual rectangle around the lens)
    # + 2 lenses that are the closest one to the center lens
    pos1 = [[-1, -1], [-1, 2], [1, 1], [1, -2], [0, -1], [0, 1]]
    pos2 = [[-1, -1], [-2, 1], [1, 1], [2, -1], [0, -1], [0, 1]]
    pos3 = [[-2, 1], [-1, 2], [2, -1], [1, -2], [0, -1], [0, 1]]

    for i, lcoord in enumerate(lenses):

        lens = lenses[lcoord]

        # some lenses have troubles, mainly the 0-type lenses, when they are far away
        # using this solution seems better
        if rtxhexgrid.hex_focal_type(lcoord) == 0:
            pos = rtxhexgrid.HEX_OFFSETS[1]
        elif rtxhexgrid.hex_focal_type(lcoord) == 1:
            pos = pos1
        elif rtxhexgrid.hex_focal_type(lcoord) == 2:
            pos = pos1
        else:
            pdb.set_trace()

        nb_lenses = _rel_to_abs(lcoord, lenses, pos)

        if i % 100 == 0:
            progress_hook("Processing lens {0}/{1} - {2}".format(
                i, num_lenses, lcoord))

        # calculate a first guess of the disparity based on the first circle
        fine, coarse, coarse_merged, lens_var = calc_costs_per_lens(
            lens, nb_lenses, disparities, max_cost, technique)
        nb_offsets, curr_disp_avg = nb_strategy(lens,
                                                lenses,
                                                coarse,
                                                disparities,
                                                max_cost=max_cost,
                                                nb_args=nb_args)

        nb_lenses = _rel_to_abs(lcoord, lenses, nb_offsets)

        if len(nb_lenses) > 0:

            #progress_hook("Refined nb: {0}".format(nb_offsets))
            fine_2, coarse_2, _, _ = calc_costs_per_lens(
                lens, nb_lenses, disparities, max_cost, technique)

            if refine is True:
                fine = np.append(fine, fine_2, axis=0)
                coarse = np.append(coarse, coarse_2, axis=0)
            else:
                fine = fine_2
                coarse = coarse_2

        num_targets += len(fine)
        coarse_costs_merged[lcoord] = rtxdisp.merge_costs_additive(
            coarse, max_cost)
        coarse_costs[lcoord] = coarse
        fine_costs[lcoord] = np.array(
            rtxdisp.merge_costs_additive(fine, max_cost))

        lens_std[lcoord] = lens_var

    progress_hook("Num comparisons: {0}".format(num_targets))

    return fine_costs, coarse_costs, coarse_costs_merged, lens_std, num_targets, 0.0