package yi.or;

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

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

public class OR_sailboat extends OR_boat
{
    static double MIN_LEN_SUM=1.8;       //the minmum sum of line length 2*(sum/sqrt(w*h))
    
    //arguments for line recognition
    double SIGMA=1;
    double LOW=0.6;
    double HIGH=0.8;
    int MINLEN=7;
    int JUMP=2;
    
    public String objName() {return "sailboat";}
    public Region recognition(int source[], int[][] hsi, int[][] laws, int[][] diff, Dimension d)
    {
        
        Region rgn= super.recognition(source, hsi, laws, diff, d);
        
        boolean have=false;
        
        int boats= rgn.connectComponent();
        if (boats>0) {
            LineGroup lg= new LineGroup(source, d, SIGMA, LOW, HIGH, MINLEN, JUMP);
            double[][] line_filter= getLineFilter();
            for (int i=1;i<=boats;i++) {
                //ATTENTION!!!: it is possible that the lines from the #1 region change the label of the pixel
                // so that some pixels in the #2 region could be changed to #1 when deal with it!!!
                //get the region #
                Region r= new Region(rgn, i);
                //normalize the lines according to that region
                NormLineGroup nlg= new NormLineGroup(lg, r);
                //filter the lines
                nlg= (NormLineGroup)nlg.rangeFilter(line_filter);
                //must have line remained and the total length>threshold
                if (nlg.size()>0?nlg.getSumLen()>MIN_LEN_SUM:false) {
                //if ((nlg.size()>0?nlg.getSumLen()>MIN_LEN_SUM:false)&&i==2) {
                    //System.err.println("sumlen"+nlg.getSumLen());
                    have=true;
                    Region rgn_line= nlg.getRegion();
                    rgn_line.changeLabel(1,i); //make the line to region #2
                    rgn.union(rgn_line);
                }
                else rgn.changeLabel(i,0);
            }
        }
        return rgn;
    }

    static double PI_1_4= Math.PI/4;
    static double PI_3_4= Math.PI*3/4;
    static double PI_3_8= Math.PI*3/8;
    static double PI_5_8= Math.PI*5/8;
    double[][] getLineFilter()
    {
        double [][] filter= {
            {-1, 1},
            {-5, 0},
            {-1, 1},
            {-10,0},
            null,
            {PI_3_8, PI_5_8},
        };
        return filter;
    }
}