package yi.iu.processor.clc;

import yi.iu.processor.clc.*;
import yi.iu.processor.*;
import yi.iu.ui.*;
import yi.iu.processor.ort.*;
import yi.util.ImageUtil;

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

public abstract class ConsistentLineCluster extends yi.iu.processor.ImageProcessor
{
    static int testCluster=-1;
    //public static double STANDARDLEN=15;
    //public static double MAXWEIGHT=1.5;
    public static int TCS=4;


    public static int filterSmallClusters(LineCluster[] lcs, int minS)
    {
        double standardLen= LineCluster.STANDARDLEN;
        double maxW= LineCluster.MAXWEIGHT;
        int count=0;
        for (int i=0;i<lcs.length;i++) {
            if (lcs[i].weightedSize(standardLen,maxW)<minS)
                lcs[i].removeAll();
            else if (lcs[i].size()<minS)
                lcs[i].removeAll();
            else count++;
        }
        return count;
    }

    public static int filterSmallClusters(LineCluster[] lcs)
    {
        return filterSmallClusters(lcs,TCS);
    }


    public static void drawLineClusters(String name, Dimension d,
        LineCluster[] lcs, int testCluster)
    {
        int i, j;
        int[] label= new int[d.width*d.height];
        for (i=0;i<label.length;i++) label[i]=-1;
        for (i=0;i<lcs.length;i++) {
            //System.err.println(i+"-"+lc[i].size());
            if (testCluster>=0&&i!=testCluster) continue;
            if (lcs[i]==null) continue;
            for (j=0;j<lcs[i].size();j++) {
                ORTLine line= lcs[i].getLineAt(j);
                DrawUtil.ort2image(label, d, line, i, 1);
            }
        }
        int[] cluster_img2= yi.util.ImageUtil.makeNewImageFromLabel_back(label);
        (new SegFrame(name, cluster_img2, d.width, d.height, label)).show();
    }

    public static void drawLineClusters(String name, int[] img, Dimension d, LineCluster[] lcs, int testCluster)
    {
        int i, j;
        int[] label= new int[d.width*d.height];
        for (i=0;i<label.length;i++) label[i]=-1;
        for (i=0;i<lcs.length;i++) {
            //System.err.println(i+"-"+lc[i].size());
            if (testCluster>=0&&i!=testCluster) continue;
            if (lcs[i]==null) continue;
            for (j=0;j<lcs[i].size();j++) {
                ORTLine line= lcs[i].getLineAt(j);
                DrawUtil.ort2image(label, d, line, i, 1);
            }
        }
        int[] cluster_img2= yi.util.ImageUtil.makeNewImageFromLabel_back(label);
        for (i=0;i<img.length;i++) if (cluster_img2[i]!=0xffffffff) img[i]=cluster_img2[i];
        (new SegFrame(name, img, d.width, d.height, label)).show();
    }

    public static int[] drawLineClusters_nodisplay(String name, int[] img,
        Dimension d, LineCluster[] lcs, int testCluster)
    {
        int i, j;
        int[] label= new int[d.width*d.height];
        for (i=0;i<label.length;i++) label[i]=-1;
        for (i=0;i<lcs.length;i++) {
            //System.err.println(i+"-"+lc[i].size());
            if (testCluster>=0&&i!=testCluster) continue;
            if (lcs[i]==null) continue;
            for (j=0;j<lcs[i].size();j++) {
                ORTLine line= lcs[i].getLineAt(j);
                DrawUtil.ort2image(label, d, line, i, 1);
            }
        }
        int[] cluster_img2= yi.util.ImageUtil.makeNewImageFromLabel_back(label);
        for (i=0;i<img.length;i++) if (cluster_img2[i]!=0xffffffff) img[i]=cluster_img2[i];
        return cluster_img2;
    }

    public static void drawLineClusters(int[] demo, Dimension d, LineCluster[] lc, int testCluster, int c)
    {
        int i,j;
        //draw the lines
        for (i=0;i<lc.length;i++) {
            if (testCluster>=0&&i!=testCluster) continue;
            if (lc[i]==null) continue;
            for (j=0;j<lc[i].size();j++) {
                ORTLine line= lc[i].getLineAt(j);
                DrawUtil.ort2image(demo, d, line, c, 1);
            }
        }
    }

    public static void drawLineClusters(int[] demo, Dimension d, Vector lc, int c)
    {
        int i,j;
        //draw the lines
        for (i=0;i<lc.size();i++) {
            ORTLine line= (ORTLine)lc.elementAt(i);
            DrawUtil.ort2image(demo, d, line, c, 1);
        }
    }

    public static void drawLineClusters(String name, Dimension d, Vector lc, int c)
    {
        int i,j;
        int[] demo= new int[d.width*d.height];
        for (i=0;i<demo.length;i++) demo[i]=0xff000000;
        //draw the lines
        for (i=0;i<lc.size();i++) {
            ORTLine line= (ORTLine)lc.elementAt(i);
            DrawUtil.ort2image(demo, d, line, c, 1);
        }
        (new DisplayFrame(name, demo, d)).show();
    }

    public static int realClusterNum(LineCluster[] lcs) {
        int size=0;
        for (int i=0;i<lcs.length;i++) if (lcs[i].size()>0) size++;
        return size;
    }

}



