'''
Created on Aug 31, 2012
@version: 0.1
@author: Dilum Bandara
@email: dilumb@engr.colostate.edu

   Copyright 2012 H. M. N. Dilum Bandara and Anura P. Jayasumana, Colorado State
   University

   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.
'''

import math
from Zone import Zone
import Config as Config


def distPP(point1, point2):
    '''
    Distance between 2 points
    @param point1: Location 1
    @param point2: Location 2
    @return: Distance 
    '''
    z1 = Zone(point1, point1) #Convert to a zone
    z2 = Zone(point2, point2)
    return distZZ(z1, z2)


def distPZ(point, zone):
    '''
    Distance between a point and a zone
    @param point1: Location of point
    @param zone: Zone
    @return: Distance 
    '''
    z1 = Zone(point, point) #Convert to a zone
    return distZZ(z1, zone)


def distZZ(zone1, zone2):
    '''
    Distance between two zones
    @param zone1: Zone1
    @param zone2: Zone2
    @return: Distance 
    '''
    total = 0
    for i in range(Config.NUM_DIMENSIONS):
        dMin = zone1.max[i] if zone1.max[i] < zone2.max[i] else zone2.max[i]
        dMax = zone1.min[i] if zone1.min[i] > zone2.min[i] else zone2.min[i]
        d1 = dMax - dMin
        if d1 < 0: d1 = 0

        dMax = zone1.max[i] if zone1.max[i] > zone2.max[i] else zone2.max[i]
        dMin = zone1.min[i] if zone1.min[i] < zone2.min[i] else zone2.min[i]
        d2r = Config.SIZE_TORUS[i] - dMax #Distance to right border
        d2 = d2r + dMin #- 0 Left border is 0
        
        total += min(d1*d1, d2 * d2) #Consider closer one
    return  math.sqrt(total)


def inRange(point1, point2, nodeRange):
    '''
    Are 2 points within given range
    @param point1: Location 1
    @param point2: Location 2
    @param nodeRange: Range 
    @return: True or False
    '''
    if distPP(point1, point2) <= nodeRange: return True
    else: return False

    
def inZone(point, zone, considerBoundary = False):
    '''
    Check whether given point is within given zone. Check zone boundary
    @param point: Point to be checked
    @param zone: Zone. Range of a torus is given as [min, max)
    @return: Success or failure  
    '''
    match = True
    for i in range(Config.NUM_DIMENSIONS):
        #If at edge or suppose to consider boundary as overlapping
        if point[i] == Config.SIZE_TORUS[i] or considerBoundary == True:
            if not ((zone.min[i] <= point[i]) and (zone.max[i] >= point[i])): 
                match = False
        else:
            if not ((zone.min[i] <= point[i]) and (zone.max[i] > point[i])): 
                match = False                
        if match == False: return match #At least not a match in 1 dimension
    return match