package yi.iu.processor.symmetry;

import java.awt.*;
import java.util.Vector;
import yi.iu.processor.*;
import yi.iu.processor.contour.*;
import yi.iu.ui.DisplayFrame;

public class ContourSymmetryCal extends ContourBasedSymProcessor
{
    public ContourSymmetryCal() {super();}
    public ContourSymmetryCal(String edgeDectorName) {super(edgeDectorName); }

    public int[] getProcessed(int source[], Dimension d, int maxwidth)
    {
        ImageProcessor ip= null;

        try {
	        Class ipc= Class.forName(edgeDector);
	        ip= (ImageProcessor)ipc.newInstance();
	    } catch (Exception e) {
	        System.err.println("Can't found that edge dector!");
	        return null;
	    }

        int[] contour= ip.getProcessed(source, d);
        if (display_contour) {
          int[] contour_demo = yi.util.ImageUtil.makeNewImageFromBinary(contour);
          DisplayFrame f= new DisplayFrame("final result", contour_demo, d.width, d.height);
          f.show();
        }

        //mdContourExtractor mdce= new mdContourExtractor();
        //int[] contour= mdce.getProcessed(source, d);
        int width=d.width, height=d.height;

        int[] rst= new int[width];
        for (int i=0;i<height;i++)
            for (int j=0;j<width;j++) {
            	double score=0;
                for (int k=1;k<width;k++) {
                    if (k*2+1>maxwidth) break;
                    int l=j-k; if (l<0) continue;
                    int r=j+k; if (r>=width) continue;
                    // method1: if the color is same then the symmetry count++
                    //if (contour[i*width+l]==contour[i*width+r]) rst[j]++;
                    // method2: if both the pixels are the edge then ...
                    // method3: consider the possible vehicle width
                    /*
                    if (contour[i*width+l]!=0&&contour[i*width+r]!=0) {
                    	double x=(double)k/POSSIBLEWIDTH;
                    	//if (k<0.5) score+= 2*x*x;
                    	if (k<1) score+= (x-1)*(x-1);
                    	else if (k<1.5) score+= (1-2*(x-1)*(x-1));
                    	else score+= 1/(x-1)/(x-1)/8;
                    }

                }
                rst[j]+=Math.round(score*10);
                */
                	if (contour[i*width+l]!=0&&contour[i*width+r]!=0) {
                    	rst[j]++;
                    }
                }
            }
        // we may not do the next loop.
        // it divide the symmetry count by the pixels number which makes the count
        /*
        for (int i=1;i<width-1;i++) {
            int num=width-1-i;
            if (num>i) num=i;
            rst[i]=rst[i]/num;
        }
        */

        d.width= rst.length; d.height=1;
        return rst;
    }

    Vector[] getInfo(int source[], Dimension d)
    {
   	    ImageProcessor ip= null;

        try {
	        Class ipc= Class.forName(edgeDector);
	        ip= (ImageProcessor)ipc.newInstance();
	    } catch (Exception e) {
	        System.err.println("Can't found that edge dector!");
	        System.exit(0);
	    }

        int[] contour= ip.getProcessed(source, d);
        int width=d.width, height=d.height;

        Vector[] info= new Vector[width];
        for (int i=0; i<width; i++) info[i]= new Vector();

        for (int i=0;i<height;i++)
            for (int j=0;j<width;j++) {
                for (int k=1;k<width;k++) {
                    int l=j-k; if (l<0) continue;
                    int r=j+k; if (r>=width) continue;
                    // method1: if the color is same then the symmetry count++
                    //if (contour[i*width+l]==contour[i*width+r]) rst[j]++;
                    // method2: if both the pixels are the edge then ...
                    if (contour[i*width+l]!=0&&contour[i*width+r]!=0) {
                        info[j].addElement(new Point(l,i));
                        info[j].addElement(new Point(r,i));
                    }

                }
            }
        // we may not do the next loop.
        // it divide the symmetry count by the pixels number which makes the count
        /*
        for (int i=1;i<width-1;i++) {
            int num=width-1-i;
            if (num>i) num=i;
            rst[i]=rst[i]/num;
        }
        */
        d.width= info.length; d.height=1;
        return info;

    }

}