from typing import List
from typing import Tuple
import math


def fill_bins(results: List[Tuple[str, float, float]],
              bins: List[Tuple[float, float]]
              ) -> List[Tuple[float, float, int]]:
    """
    Fill a bin with the number of images within position thresholds.

    :param results: list of error evaluation (image name, translation error, rotation error)
    :param bins: list of translation and rotation thresholds
    :return: number of images in every pair of (translation,rotation) error
    """
    assert isinstance(results, list)
    assert isinstance(bins, list)

    all_positions = [(translation_error, rotation_error) for name, translation_error, rotation_error in results]
    filled_bins = []
    for a_bin in bins:
        trans_threshold = a_bin[0]
        rot_threshold = a_bin[1]
        number_of_images_in_bin = 0
        for translation_error, rotation_error in all_positions:
            if (math.isnan(rot_threshold) or rot_threshold < 0) and translation_error <= trans_threshold:
                number_of_images_in_bin += 1
            elif translation_error <= trans_threshold and rotation_error <= rot_threshold:
                number_of_images_in_bin += 1
        filled_bins.append((trans_threshold, rot_threshold, number_of_images_in_bin))
    return filled_bins