import numpy as np
from gamera.core import *

class Distance():
    '''Vypocet vzdalenosti mezi jednotlivymi segmenty
    
    vystup:
    
    z - absolutni hodnota vzdalenosti od stredu obou segmentu
    
    angle - uhel mezi centry segmentu ve stupnich
    
    ul_x,ul_y - horni levy roh spojenych sementu
    
    lr_x,lr_y - spodni pravy roh spojenych segmentu
    
    d - segment
    
    zup - vzdalenost od vrchu prvniho segmenu od stredu druheho segmentu
    
    angleup  - vzdalenost od vrchu prvniho segmenu od stredu druheho segmentu
    
    zdown - vzdalenost od spodu prvniho segmenu od stredu druheho segmentu
    
    anledown - uhel od spodu prvniho segmenu od stredu druheho segmentu
    '''
    def __init__(self, ccs1, ccs2):
        self.data = []
        for i, c in enumerate(ccs1):
            row = []
            for j, d in enumerate(ccs2):
                #data = [c.ul_x - d.lr_x, c.lr_x - d.ul_x, c.ul_y - d.lr_y, c.lr_y - d.ul_y]
                x = (d.center_x - c.center_x)
                y = (d.center_y - c.center_y)
                yup = (d.center_y - c.ul_y)
                ydown = (d.center_y - c.lr_y)
                angle = np.arctan2(x, y) * (180/np.pi)
                angleup = np.arctan2(x, yup) * (180 / np.pi)
                angledown = np.arctan2(x, ydown) * (180 / np.pi)
                z = np.sqrt((x * x) + (y * y))
                zup = np.sqrt((x * x) + (yup * yup))
                zdown = np.sqrt((x * x) + (ydown * ydown))
                touch = (abs(x) <= ((c.width + d.width)/2)) and (abs(y) <= ((c.height + d.height)/2))
                ul_x = min(c.ul_x, d.ul_x)
                ul_y = min(c.ul_x, d.ul_x)
                lr_x = max(c.lr_x, d.lr_x)
                lr_y = max(c.lr_y, d.lr_y)
                row.append([z, angle, touch, ul_x, ul_y, lr_x, lr_y, d, zup, angleup, zdown, angledown])
                #overlap = (data[0] < 0 and data[1] > 0 and data[2] < 0 and data[3] > 0)
            self.data.append(row)