def get_box_coords_index(P, box_coords):
    """
    
    Given a grid of length P and the coordinates of a bounding box, returns the indices of the grid cells that
    correspond to the upper-left and lower-right corners of the bounding box.
    
    Args:
    - P (int): the length of the grid
    - box_coords (np.array of shape (4,)): the normalized coordinates of the bounding box, in the form [x1, y1, x2, y2]
    
    Returns:
    - ul_idx (int): the index of the grid cell that corresponds to the upper-left corner of the bounding box
    - lr_idx (int): the index of the grid cell that corresponds to the lower-right corner of the bounding box
    """
    # pdb.set_trace()
    # Compute the size of each cell in the grid
    cell_size = 1.0 / P
    
    # Compute the indices of the grid cells that correspond to the upper-left and lower-right corners of the bounding box
    ul_x = int(np.floor(max(box_coords[0], 0) / cell_size))
    ul_y = int(np.floor(max(box_coords[1], 0) / cell_size))
    ul_idx = ul_x + ul_y * P
    
    lr_x = int(np.floor(min(box_coords[2], 0.99999) / cell_size))
    lr_y = int(np.floor(min(box_coords[3], 0.99999) / cell_size))
    lr_idx = lr_x + lr_y * P
    
    return ul_idx, lr_idx

    
    Given a grid of length P and the coordinates of a bounding box, returns the indices of the grid cells that
    correspond to the upper-left and lower-right corners of the bounding box.
    
    Args:
    - P (int): the length of the grid
    - box_coords (np.array of shape (4,)): the normalized coordinates of the bounding box, in the form [x1, y1, x2, y2]
    
    Returns:
    - ul_idx (int): the index of the grid cell that corresponds to the upper-left corner of the bounding box
    - lr_idx (int): the index of the grid cell that corresponds to the lower-right corner of the bounding box
    
kosmos-2/unilm/data/vl/obj_utils.py