# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Operations for [N, 4] numpy arrays representing bounding boxes."""
import numpy as np


def area(boxes):
    """Computes area of boxes.

    Args:
        boxes (np.ndarray): A numpy array with shape [N, 4] holding N boxes.

    Returns:
        np.ndarray: A numpy array with shape [N*1] representing box areas.
    """
    return (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])


def intersection(boxes1, boxes2):
    """Compute pairwise intersection areas between boxes.

    Args:
        boxes1 (np.ndarray): A numpy array with shape [N, 4] holding N boxes.
        boxes2 (np.ndarray): A numpy array with shape [M, 4] holding M boxes.

    Returns:
        np.ndarray: A numpy array with shape [N*M] representing pairwise
        intersection area.
    """
    [y_min1, x_min1, y_max1, x_max1] = np.split(boxes1, 4, axis=1)
    [y_min2, x_min2, y_max2, x_max2] = np.split(boxes2, 4, axis=1)

    all_pairs_min_ymax = np.minimum(y_max1, np.transpose(y_max2))
    all_pairs_max_ymin = np.maximum(y_min1, np.transpose(y_min2))
    intersect_heights = np.maximum(
        np.zeros(all_pairs_max_ymin.shape),
        all_pairs_min_ymax - all_pairs_max_ymin)
    all_pairs_min_xmax = np.minimum(x_max1, np.transpose(x_max2))
    all_pairs_max_xmin = np.maximum(x_min1, np.transpose(x_min2))
    intersect_widths = np.maximum(
        np.zeros(all_pairs_max_xmin.shape),
        all_pairs_min_xmax - all_pairs_max_xmin)
    return intersect_heights * intersect_widths


def iou(boxes1, boxes2):
    """Computes pairwise intersection-over-union between box collections.

    Args:
        boxes1 (np.ndarray): A numpy array with shape [N, 4] holding N boxes.
        boxes2 (np.ndarray): A numpy array with shape [M, 4] holding M boxes.

    Returns:
        np.ndarray: A numpy array with shape [N, M] representing pairwise iou
        scores.
    """
    intersect = intersection(boxes1, boxes2)
    area1 = area(boxes1)
    area2 = area(boxes2)
    union = np.expand_dims(
        area1, axis=1) + np.expand_dims(
            area2, axis=0) - intersect
    return intersect / union


def ioa(boxes1, boxes2):
    """Computes pairwise intersection-over-area between box collections.

    Intersection-over-area (ioa) between two boxes box1 and box2 is defined as
    their intersection area over box2's area. Note that ioa is not symmetric,
    that is, IOA(box1, box2) != IOA(box2, box1).

    Args:
        boxes1 (np.ndarray): A numpy array with shape [N, 4] holding N boxes.
        boxes2 (np.ndarray): A numpy array with shape [M, 4] holding N boxes.

    Returns:
        np.ndarray: A numpy array with shape [N, M] representing pairwise ioa
        scores.
    """
    intersect = intersection(boxes1, boxes2)
    areas = np.expand_dims(area(boxes2), axis=0)
    return intersect / areas
