"""
Generate base anchors on index 0
"""

import numpy as np
import math


def expand_anchors(base_anchors, feat_height, feat_width, feat_stride):
    # generate proposals from shifted anchors
    shift_x = np.arange(0, feat_width) * feat_stride
    shift_y = np.arange(0, feat_height) * feat_stride
    shift_x, shift_y = np.meshgrid(shift_x, shift_y)
    shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()
    # add A anchors (1, A, 4) to
    # cell k shifts(k, 1, 4) to
    # get shift anchors (K, A, 4)
    A = base_anchors.shape[0]
    K = shifts.shape[0]
    base_anchors_all = base_anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))
    base_anchors_all = base_anchors_all.reshape((K * A, 4))
    return base_anchors_all


def generate_anchors(base_size, ratios, scales):
    if isinstance(base_size, list) or isinstance(base_size, tuple):
        w = base_size[0]
        h = base_size[1]
    else:
        w = h = base_size
    size = w * h
    x_ctr = 0.5 * (w - 1.0)
    y_ctr = 0.5 * (h - 1.0)
    anchors = []
    for ratio in ratios:
        for scale in scales:
            size_ratio = math.floor(size / ratio)
            new_w = math.floor(math.sqrt(size_ratio) + 0.5) * scale
            new_h = math.floor(new_w / scale * ratio + 0.5) * scale
            anchors.append([x_ctr - 0.5 * (new_w - 1.0),
                            y_ctr - 0.5 * (new_h - 1.0),
                            x_ctr + 0.5 * (new_w - 1.0),
                            y_ctr + 0.5 * (new_h - 1.0)])
    return np.array(anchors)


def generate_anchors_1(base_size, ratios, scales):
    """
    Generate anchor (reference) windows by enumerating aspect ratios X
    scales wrt a reference (0, 0, 15, 15) window.
    """
    if isinstance(base_size, list) or isinstance(base_size, tuple):
        base_anchor = np.array([1, 1, base_size[0], base_size[1]]) - 1
    else:
        base_anchor = np.array([1, 1, base_size, base_size]) - 1
    ratio_anchors = _ratio_enum(base_anchor, ratios)
    anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)
                         for i in xrange(ratio_anchors.shape[0])])
    return anchors


def _whctrs(anchor):
    """
    Return width, height, x center, and y center for an anchor (window).
    """

    w = anchor[2] - anchor[0] + 1
    h = anchor[3] - anchor[1] + 1
    x_ctr = anchor[0] + 0.5 * (w - 1)
    y_ctr = anchor[1] + 0.5 * (h - 1)
    return w, h, x_ctr, y_ctr


def _mkanchors(ws, hs, x_ctr, y_ctr):
    """
    Given a vector of widths (ws) and heights (hs) around a center
    (x_ctr, y_ctr), output a set of anchors (windows).
    """

    ws = ws[:, np.newaxis]
    hs = hs[:, np.newaxis]
    anchors = np.hstack((x_ctr - 0.5 * (ws - 1),
                         y_ctr - 0.5 * (hs - 1),
                         x_ctr + 0.5 * (ws - 1),
                         y_ctr + 0.5 * (hs - 1)))
    return anchors


def _ratio_enum(anchor, ratios):
    """
    Enumerate a set of anchors for each aspect ratio wrt an anchor.
    """

    w, h, x_ctr, y_ctr = _whctrs(anchor)
    size = w * h
    size_ratios = size / ratios
    ws = np.round(np.sqrt(size_ratios))
    hs = np.round(ws * ratios)
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
    return anchors


def _scale_enum(anchor, scales):
    """
    Enumerate a set of anchors for each scale wrt an anchor.
    """

    w, h, x_ctr, y_ctr = _whctrs(anchor)
    ws = w * scales
    hs = h * scales
    anchors = _mkanchors(ws, hs, x_ctr, y_ctr)
    return anchors








