package yi.or;

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

import yi.util.io.OutputResult;

public class NormLineGroup extends LineGroup
{
    //the properties about the region
    int cx,cy;      //the centeral x, and y of the region
    int rw,rh;      //the width and height of the region
    
	public NormLineGroup(int source[], Dimension d)
	{
		super(source, d);
	}
	public NormLineGroup(int source[], Dimension d, double _sigma, double _low, double high, int _minlen, int _jump)
	{
		super(source, d, _sigma, _low, high, _minlen, _jump);
	}
    public NormLineGroup(Vector _lines, int _w, int _h, int _cx, int _cy, int _rw, int _rh)
    {
        super(_lines,_w,_h);
        cx=_cx; cy=_cy; rw=_rw; rh=_rh;
    }
		
    public NormLineGroup(LineGroup lg, Region rgn)
    {
        width=lg.width;height=lg.height;
        //get the information about the region
        //central x and central y
        Point pos= rgn.position(1);
        cx=pos.x; cy=pos.y;
        //region width and region height;
        Rectangle ext= rgn.extent(1, 2);     //consider 2%-98%
        rw= ext.width; rh= ext.height;
        if (rw==0 ||rh==0) return;
        double nor_l= Math.sqrt(rw*rh);
        if (VERBOSE) System.err.println("the region info: rw="+rw+" rh="+rh+" nor_l="+nor_l);
        
        //translate the lines
        int len= lg.lines.size();
        for (int i=0 ; i<len; i++ ) {
            double[] line = (double[])lg.lines.elementAt(i);
            double x1= line[0];
            double y1= line[1];
            double x2= line[2];
            double y2= line[3];
            //make the (x1,y1) the closer point to the center between the two end points
            if (distance(x1,y1,cx,cy)>distance(x2,y2,cx,cy)) {
                double temp= x1;
                x1=x2;x2=temp;
                temp=y1;
                y1=y2;y2=temp;
            }
            double	length= line[4];
            double	theta= line[5];
            
            if (length<0.01) {
                System.err.println("len: "+length+"  "+x1+","+y1+"->"+x2+","+y2);
            }
            double[] rst= new double[6];
            rst[0]= (x1-cx)/rw;
            rst[1]= (y1-cy)/rh;
            rst[2]= (x2-cx)/rw;
            rst[3]= (y2-cy)/rh;
            rst[4]= length/nor_l;
            rst[5]= theta;
            lines.addElement(rst);
        }
    }
    
    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 NormLineGroup(rst,width,height,cx,cy,rw,rh);
    }
    
    Region getRegion()
    {
        
        Vector un_norm= getUnnormLines();
        double[][] l= new double[un_norm.size()][];
        return getRegion(un_norm);
    }
    
    Vector getUnnormLines()
    {
        double nor_l= Math.sqrt(rw*rh);

        Vector un_norm= new Vector();
        for (int i=0 ; i<lines.size(); i++ ) {
            double[] line= (double[]) lines.elementAt(i);
            double x1= line[0];
            double y1= line[1];
            double x2= line[2];
            double y2= line[3];
            double len= line[4];
            
            double _x1= x1*rw+cx;
            double _y1= y1*rh+cy;
            double _x2= x2*rw+cx;
            double _y2= y2*rh+cy;
            double _len= len*nor_l;
            
            double[] un= {_x1,_y1,_x2,_y2,_len,line[5]};
            un_norm.addElement(un);
        }
        return un_norm;
    }
	
}