package areaidentification;

/**
 *
 * @author Ilias Foudalis
 */
import java.util.Vector;


public class PublicFunctions {

    public final double EARTHRAD = 6371.009;//in km

    public PublicFunctions()
    {
    }

    public double cossineSimilarity(Vector<Double> v1, Vector<Double> v2)
    {
        double nominator = dotProduct(v1,v2);
        double denominator = (vecMagnitude(v1)*vecMagnitude(v2));
        if(denominator == 0.0)
            return 0.0;
        else return fixPrecision(nominator/denominator,10000);
    }

    public double dotProduct(Vector<Double> v1, Vector<Double> v2)
    {
        double dotprod = 0.0;
        for(int i = 0; i < v1.size(); i++)
            dotprod+=fixPrecision(v1.get(i)*v2.get(i), 10000);
        return dotprod;
    }

    public double vecMagnitude(Vector<Double> a)
    {
        double sum = 0.0;
        for(int i = 0; i < a.size(); i++)
            sum+= fixPrecision(Math.pow(a.get(i),2), 10000);
        sum = fixPrecision(Math.sqrt(sum), 10000);
        return sum;
    }

    public double fixPrecision(double a, int precision)
    {
        a = a*precision;
        a = Math.floor(a);
        return a/precision;
    }

    public short[] getPosition(short id,int dimX, int dimY)
    {
        short[] pos =  new short[2];
        if(id%dimY == 0)
        {
            pos[0] = (short)(id/dimY-1);
            pos[1] = (short)(dimY-1);
        }
        else
        {
            pos[0] = (short)Math.floor(id/dimY);
            pos[1] = (short)(id%dimY-1);
        }
        return pos;
    }

    public double convertToRad(double degrees)
    {
        double rads = (Math.PI*degrees)/180.0;
        return fixPrecision(rads, 1000);
    }
    
    public Vector<Short> getNeighborsCross(short node, int dimX,int dimY)
    {
        Vector<Short> neighbors = new Vector<Short>();
        node++;
        short []pos = getPosition( node, dimX, dimY);
        
        if(pos[0]+1 < dimX )
            neighbors.add(new Short(arrayToId(pos[0]+1,pos[1],dimY)));
        if(pos[0]-1 >= 0 )
            neighbors.add(new Short(arrayToId(pos[0]-1,pos[1],dimY)));
        if(pos[1]+1 < dimY )
            neighbors.add(new Short(arrayToId(pos[0],pos[1]+1,dimY)));
        if(pos[1]-1 >= 0 )
            neighbors.add(new Short(arrayToId(pos[0],pos[1]-1,dimY)));

        
        if(pos[1]+1 == dimY)
            neighbors.add(new Short(arrayToId(pos[0], 0,dimY)));
        if(pos[1]-1 < 0)
            neighbors.add(new Short(arrayToId(pos[0],dimY-1,dimY)));
        
        
        return neighbors;
    }

    public short arrayToId(int x, int y,int dimY)
    {
        return (short)(dimY*x +(y));
    }


    public double distance3(double lat_a, double lon_a,double lat_b, double lon_b)
    {
        double  dLat = lat_a-lat_b;
        double dLon = lon_a-lon_b;
        double alpha = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(lat_a)*Math.cos(lat_b)*Math.sin(dLon/2)*Math.sin(dLon/2);
        double c = 2 * Math.atan2(Math.sqrt(alpha), Math.sqrt(1-alpha));
        return EARTHRAD*c;
    }



}

