package yi.or;

import java.awt.*;
import java.util.Vector;

import yi.iu.processor.ort.*;
import yi.util.io.OutputResult;

public class LineGroup
{
    static boolean VERBOSE= false;
    static int LINEINFOLEN=6;

    //arguments for line recognition
    static double SIGMA=1;
    static double LOW=0.6;
    static double HIGH=0.8;
    static int MINLEN=7;
    static int JUMP=2;

    public Vector lines=null;
    int width, height;

    public LineGroup() {lines=new Vector();}

    public LineGroup(int[] source, Dimension d, double _sigma, double _low, double high, int _minlen, int _jump)
    {
        width= d.width;height=d.height;
        Vector[] segs= (new Fex( _sigma, _low, high, _minlen, _jump)).fex(source, d);
        Vector seg_lines= segs[0];

        int shift_x= d.width/2;
        int shift_y=d.height/2;

        int len= seg_lines.size();
        if (VERBOSE) System.err.println("total lines: "+len);

        lines= new Vector();
        for (int i=0 ; i<len; i++ ) {
            ORTLine line = (ORTLine)( seg_lines.elementAt(i));
            double[] dline= new double[LINEINFOLEN];
            dline[0]= line.Start.Col+shift_x;
            dline[1]= line.Start.Row+shift_y;
            dline[2]= line.End.Col+shift_x;
            dline[3]= line.End.Row+shift_y;
            dline[4]= line.Length;
            dline[5]= line.Theta;
            lines.addElement(dline);
        }
    }
    public LineGroup(int[] source, Dimension d)
    {
        this(source, d, SIGMA, LOW, HIGH, MINLEN, JUMP);
    }
    public LineGroup(Vector _lines, int _w, int _h)
    {
        lines= _lines; width= _w; height= _h;
    }

    public int size() {
        if (lines==null) return 0;
        return lines.size();
    }

    public LineGroup rangeFilter(double[][] range)
    {
        Vector rst= new Vector();

        int line_num= lines.size();
        for (int i=0;i<line_num;i++) {
            boolean ok= true;
            double[] line= (double[]) lines.elementAt(i);
            for (int j=0;j<LINEINFOLEN;j++) {
                if (range[j]==null) continue;
                if ((line[j]<range[j][0])||(line[j]>=range[j][1])) {
                    ok=false;
                    break;
                }
            }
            if (ok) rst.addElement(line);
        }
        return new LineGroup(rst,width,height);
    }

    Region getRegion(Vector lines)
    {
        int shift_x= width/2;
        int shift_y= height/2;

        int len= lines.size();
        Vector ort_lines= new Vector();
        for (int i=0 ; i<len; i++ ) {
            double[] line= (double[]) lines.elementAt(i);
            double x1= line[0]-shift_x;
            double y1= line[1]-shift_y;
            double x2= line[2]-shift_x;
            double y2= line[3]-shift_y;

            ort_lines.addElement(new ORTLine(x1, y1, x2, y2));
        }
        //OutputResult.outputResult("lines", rst);
        Vector arcs= new Vector();
        char[][] label= DrawUtil.ort2image( new Dimension(width,height), ort_lines,  arcs);
        int[] label_1= yi.util.ImageUtil.twoD2oneD(label);
        Region rgn_rst= new Region(width,height);
        for (int i=0;i<label_1.length;i++)
            if (label_1[i]>0) rgn_rst.setPixel(i);
        return rgn_rst;
    }

    Region getRegion()
    {
        return getRegion(lines);
    }

    double getSumLen()
    {
        int len= lines.size();
        double sum=0;
        for (int i=0 ; i<len; i++ ) {
            double[] line= (double[]) lines.elementAt(i);
            sum+= line[4];
        }
        return sum;
    }

    double distance(double x1,double y1, double x2, double y2) {
        double dx=x2-x1, dy=y2-y1;
        return Math.sqrt(dx*dx+dy*dy);
    }

    void output(String name, Vector l)
    {
        double[][] ll= new double[l.size()][];
        for (int i=0;i<ll.length;i++) ll[i]=(double[]) l.elementAt(i);
        OutputResult.outputResult(name, ll);
    }

    void output(String name)
    {
        output(name, lines);
    }

    void display()
    {
        Region r= getRegion();
        r.display();
    }


}