package yi.iu.processor.clc;

import yi.iu.processor.clc.*;

import yi.iu.processor.*;
import yi.iu.processor.building.*;
import yi.iu.processor.ort.*;
import yi.iu.processor.filter.GaussianSmooth;
import yi.iu.ui.*;
import yi.iu.processor.color.*;
import yi.util.io.*;
import yi.util.ImageUtil;

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

/**
 * Construct spatially consistent line clusters
 * @depracated replaced by CLC_Spatial
 */
public class CSOSSCLC extends yi.iu.processor.clc.ConsistentLineCluster
{
    int interTest1= -1;
    int interTest2= -1;
    int last= -1;

    public int getArguNum() { return 4;}

    public void setArgument(int i, String st)
    {
        if (st.length()==0) return;
        if (i==0) testCluster= Integer.valueOf(st).intValue();
        if (i==1) interTest1= Integer.valueOf(st).intValue();
        if (i==2) interTest2= Integer.valueOf(st).intValue();
        if (i==3) last= Integer.valueOf(st).intValue();
    }

    public String getArgument(int i)
    {
        if (i==0) return (new Integer(testCluster)).toString();
        if (i==1) return (new Integer(interTest1)).toString();
        if (i==2) return (new Integer(interTest2)).toString();
        if (i==3) return (new Integer(last)).toString();
        return null;
    }

    public String getArguName(int i)
    {
        if (i==0) return "TestCluster";
        if (i==1) return "OutputCluster1";
        if (i==2) return "OutputCluster2";
        if (i==3) return "last mon";
        return null;
    }

    public int[] getProcessed(int source[], Dimension d)
    {
        return null;
    }

    public LineCluster[] getProcessed_ex(int source[], Dimension d, Object ref[])
    {
        int i,j;

        //get the line clusters (classified by color information) stored in lc
        CSOCLC cclc= new CSOCLC();
        LineCluster[] lc= cclc.getProcessed_ex(source, d, ref);
        int[] dominant_colors= (int[])ref[0];
        int cNum=dominant_colors.length;
        Vector lines= (Vector)ref[1];
        int[][] lineColor= (int[][])ref[2];
        ref[3]=lc;

        if (last>=0) drawLineClusters("last", d, lc, last);
        for (i=0;i<lc.length;i++) {
            lc[i].refineByPerspectiveRelation();
        }

        //filter the small line clusters
        int size1= this.realClusterNum(lc);
        //get rid of the small clusters
        int size2= filterSmallClusters(lc);
        System.err.println("filter the small clusters after spective: "+size1+"-->"+size2);


        if (last>=0) drawLineClusters("last", d, lc, last);

        //refine the clusters by spatial relationship
        Vector rst = new Vector();
        for (i=0;i<lc.length;i++) {
            Vector nlc= lc[i].splitBySpatialRelation(0.18,0.6);
            for (j=0;j<nlc.size();j++) {
                if (interTest1>=0&&rst.size()==interTest1)
                    System.err.println("output#"+interTest1+" from"+j);
                rst.addElement(nlc.elementAt(j));
            }
            if (i==last) {
                System.err.println("monitor:"+(rst.size()-nlc.size())+" - "+(rst.size()-1));
                LineCluster[] mon= new LineCluster[nlc.size()];
                for (int k=0;k<nlc.size();k++) {
                    mon[k]=(LineCluster)nlc.elementAt(k);
                    System.err.println("  "+(rst.size()-nlc.size()+k)+" size:"+mon[k].size());
                }
                drawLineClusters("last", d, mon, -1);
            }
        }

        LineCluster[] nlcs= new LineCluster[rst.size()];
        for (i=0;i<nlcs.length;i++) nlcs[i]=(LineCluster)rst.elementAt(i);

        //filter the small line clusters
        size1= this.realClusterNum(nlcs);
        //get rid of the small clusters
        size2= filterSmallClusters(nlcs);
        System.err.println("filter the small clusters after spatial: "+size1+"-->"+size2);

        return nlcs;
    }

    public int[] getProcessedPseudoColor(int source[], Dimension d)
    {
        int i,j;

        //get the line clusters (classified by color information) stored in lc
        Object[] ref= new Object[4];
        CSOCLC cclc= new CSOCLC();
        LineCluster[] nlcs= getProcessed_ex(source, d, ref);
        int[] dominant_colors= (int[])ref[0];
        int cNum=dominant_colors.length;
        Vector lines= (Vector)ref[1];
        int[][] lineColor= (int[][])ref[2];
        LineCluster[] lc= (LineCluster[])ref[3];

        drawLineClusters("post-spatial", d, nlcs, testCluster);

        int[] demo= ImageUtil.makeBackground(source);
        //for (i=0;i<source.length;i++) demo[i]=0xff000000;
        drawLineClusters(demo, d, lc, testCluster, 0xffffffff);
        //mark out the spatial-consistent converged lines
        drawLineClusters(demo, d, nlcs, testCluster, 0xff00ff00);
        (new DisplayFrame("demo", demo, d)).show();

        if (interTest1!= -1)nlcs[interTest1].output();
        if (interTest2!= -1)nlcs[interTest2].output();

        return demo;
    }



    public static void main(String[] args)
    {
LineCluster lc= new LineCluster(23,141,23);
lc.insert(new ORTLine(79.0,-115.0,-31.0,-114.0));
lc.insert(new ORTLine(142.0,-116.0,226.0,-116.0));
lc.insert(new ORTLine(-51.0,-113.0,-34.0,-113.0));
lc.insert(new ORTLine(116.0,-78.0,93.0,-77.0));
lc.insert(new ORTLine(-182.0,-80.0,-186.0,-78.0));
lc.insert(new ORTLine(78.0,-78.0,62.0,-76.0));
lc.insert(new ORTLine(189.0,-79.0,166.0,-78.0));
lc.insert(new ORTLine(42.0,-77.0,18.0,-76.0));
lc.insert(new ORTLine(151.0,-79.0,129.0,-78.0));
lc.insert(new ORTLine(226.0,-82.0,211.0,-79.0));
lc.insert(new ORTLine(-37.0,-76.0,-56.0,-75.0));
lc.insert(new ORTLine(4.0,-77.0,-10.0,-76.0));
lc.insert(new ORTLine(-107.0,-75.0,-118.0,-75.0));
lc.insert(new ORTLine(-12.0,-78.0,-17.0,-76.0));
lc.insert(new ORTLine(202.0,-78.0,207.0,-78.0));
lc.insert(new ORTLine(-82.0,-75.0,-67.0,-76.0));
lc.insert(new ORTLine(171.0,-72.0,167.0,-71.0));
lc.insert(new ORTLine(-159.0,-73.0,-165.0,-73.0));
lc.insert(new ORTLine(-155.0,-74.0,-140.0,-73.0));
lc.insert(new ORTLine(-129.0,-74.0,-122.0,-75.0));
lc.insert(new ORTLine(197.0,-23.0,203.0,-23.0));
lc.insert(new ORTLine(85.0,-22.0,91.0,-22.0));
lc.insert(new ORTLine(-28.0,-23.0,-24.0,-24.0));
lc.insert(new ORTLine(-28.0,-23.0,-24.0,-24.0));
lc.insert(new ORTLine(-103.0,-19.0,-99.0,-19.0));
lc.insert(new ORTLine(-98.0,-16.0,-103.0,-15.0));
lc.insert(new ORTLine(-104.0,-12.0,-97.0,-12.0));
lc.insert(new ORTLine(-140.0,-19.0,-135.0,-17.0));
lc.insert(new ORTLine(142.0,-70.0,136.0,-68.0));
lc.insert(new ORTLine(177.0,-69.0,167.0,-69.0));
lc.insert(new ORTLine(101.0,-68.0,94.0,-68.0));
lc.insert(new ORTLine(235.0,-24.0,239.0,-24.0));
lc.insert(new ORTLine(235.0,-24.0,239.0,-24.0));
lc.insert(new ORTLine(-12.0,-66.0,-18.0,-66.0));
lc.insert(new ORTLine(19.0,-67.0,25.0,-67.0));
lc.insert(new ORTLine(-85.0,-65.0,-92.0,-65.0));
lc.insert(new ORTLine(-166.0,-64.0,-159.0,-64.0));
lc.insert(new ORTLine(248.0,-26.0,255.0,-24.0));
lc.insert(new ORTLine(160.0,-23.0,166.0,-23.0));
lc.insert(new ORTLine(166.0,-26.0,161.0,-26.0));
lc.insert(new ORTLine(166.0,-26.0,161.0,-26.0));
lc.insert(new ORTLine(124.0,-27.0,130.0,-28.0));
lc.insert(new ORTLine(-135.0,-14.0,-142.0,-14.0));
lc.insert(new ORTLine(-135.0,-14.0,-142.0,-14.0));
lc.insert(new ORTLine(-177.0,-18.0,-173.0,-19.0));
lc.insert(new ORTLine(10.0,-21.0,16.0,-21.0));
lc.insert(new ORTLine(124.0,-22.0,129.0,-24.0));
lc.insert(new ORTLine(-61.0,-14.0,-67.0,-15.0));
lc.insert(new ORTLine(-189.0,-17.0,-195.0,-18.0));
lc.insert(new ORTLine(159.0,-18.0,166.0,-18.0));
lc.insert(new ORTLine(165.0,-16.0,158.0,-16.0));
lc.insert(new ORTLine(198.0,-19.0,203.0,-18.0));
lc.insert(new ORTLine(84.0,-17.0,91.0,-17.0));
lc.insert(new ORTLine(90.0,-15.0,84.0,-15.0));
lc.insert(new ORTLine(122.0,-17.0,128.0,-17.0));
lc.insert(new ORTLine(122.0,-17.0,128.0,-17.0));
lc.insert(new ORTLine(9.0,-16.0,16.0,-16.0));
lc.insert(new ORTLine(9.0,-16.0,16.0,-16.0));
lc.insert(new ORTLine(15.0,-14.0,9.0,-14.0));
lc.insert(new ORTLine(46.0,-16.0,53.0,-17.0));
lc.insert(new ORTLine(198.0,-16.0,202.0,-16.0));
lc.insert(new ORTLine(-29.0,-16.0,-23.0,-16.0));
lc.insert(new ORTLine(128.0,-15.0,121.0,-15.0));
lc.insert(new ORTLine(-188.0,-13.0,-195.0,-13.0));
lc.insert(new ORTLine(-194.0,-11.0,-188.0,-11.0));
lc.insert(new ORTLine(-21.0,-13.0,-29.0,-13.0));
lc.insert(new ORTLine(53.0,-14.0,46.0,-14.0));
lc.insert(new ORTLine(-221.0,21.0,-231.0,22.0));
lc.insert(new ORTLine(12.0,50.0,17.0,51.0));
lc.insert(new ORTLine(12.0,50.0,17.0,51.0));
lc.insert(new ORTLine(-182.0,63.0,-198.0,63.0));
lc.insert(new ORTLine(91.0,45.0,87.0,46.0));
lc.insert(new ORTLine(-181.0,60.0,-199.0,60.0));
lc.insert(new ORTLine(-256.0,42.0,-262.0,43.0));
lc.insert(new ORTLine(-187.0,57.0,-181.0,53.0));
lc.insert(new ORTLine(95.0,46.0,86.0,50.0));
lc.insert(new ORTLine(310.0,48.0,304.0,48.0));
lc.insert(new ORTLine(11.0,58.0,17.0,58.0));
lc.insert(new ORTLine(16.0,90.0,9.0,91.0));
lc.insert(new ORTLine(10.0,61.0,17.0,61.0));
lc.insert(new ORTLine(-104.0,51.0,-97.0,49.0));
lc.insert(new ORTLine(-228.0,62.0,-240.0,62.0));
lc.insert(new ORTLine(48.0,60.0,55.0,60.0));
lc.insert(new ORTLine(-63.0,61.0,-69.0,63.0));
lc.insert(new ORTLine(-101.0,62.0,-106.0,62.0));
lc.insert(new ORTLine(-140.0,62.0,-146.0,64.0));
lc.insert(new ORTLine(294.0,79.0,300.0,79.0));
lc.insert(new ORTLine(-197.0,90.0,-188.0,90.0));
lc.insert(new ORTLine(89.0,94.0,95.0,95.0));
lc.insert(new ORTLine(74.0,97.0,59.0,98.0));
lc.insert(new ORTLine(91.0,97.0,96.0,99.0));
lc.insert(new ORTLine(-108.0,102.0,-104.0,103.0));
lc.insert(new ORTLine(-108.0,102.0,-104.0,103.0));
lc.insert(new ORTLine(20.0,107.0,-13.0,107.0));
lc.insert(new ORTLine(13.0,103.0,7.0,104.0));
lc.insert(new ORTLine(13.0,103.0,7.0,104.0));
lc.insert(new ORTLine(-54.0,104.0,-58.0,105.0));
lc.insert(new ORTLine(89.0,103.0,99.0,103.0));
lc.insert(new ORTLine(316.0,104.0,277.0,104.0));
lc.insert(new ORTLine(275.0,107.0,316.0,107.0));
lc.insert(new ORTLine(-80.0,106.0,-110.0,106.0));
lc.insert(new ORTLine(108.0,106.0,112.0,107.0));
lc.insert(new ORTLine(50.0,106.0,32.0,107.0));
lc.insert(new ORTLine(52.0,109.0,32.0,109.0));
lc.insert(new ORTLine(52.0,109.0,32.0,109.0));
lc.insert(new ORTLine(69.0,106.0,90.0,107.0));
lc.insert(new ORTLine(-105.0,109.0,-55.0,110.0));
lc.insert(new ORTLine(-56.0,108.0,-78.0,107.0));
lc.insert(new ORTLine(-30.0,110.0,-37.0,110.0));
lc.insert(new ORTLine(-30.0,110.0,-37.0,110.0));
lc.insert(new ORTLine(19.0,109.0,-17.0,105.0));
lc.insert(new ORTLine(19.0,109.0,-17.0,105.0));
lc.insert(new ORTLine(-18.0,114.0,37.0,114.0));
lc.insert(new ORTLine(-33.0,112.0,-27.0,112.0));
lc.insert(new ORTLine(-33.0,112.0,-27.0,112.0));
lc.insert(new ORTLine(-18.0,112.0,53.0,112.0));
lc.insert(new ORTLine(69.0,112.0,80.0,113.0));
lc.insert(new ORTLine(-29.0,114.0,-34.0,114.0));
lc.insert(new ORTLine(-18.0,117.0,58.0,117.0));
lc.insert(new ORTLine(-18.0,117.0,58.0,117.0));
lc.insert(new ORTLine(57.0,119.0,-20.0,119.0));
lc.insert(new ORTLine(57.0,119.0,-20.0,119.0));
lc.insert(new ORTLine(-27.0,116.0,-36.0,119.0));
lc.insert(new ORTLine(-27.0,116.0,-36.0,119.0));
lc.insert(new ORTLine(82.0,118.0,69.0,117.0));
lc.insert(new ORTLine(82.0,118.0,69.0,117.0));
lc.insert(new ORTLine(71.0,119.0,79.0,119.0));
lc.insert(new ORTLine(71.0,119.0,79.0,119.0));
lc.insert(new ORTLine(59.0,121.0,55.0,122.0));
lc.insert(new ORTLine(52.0,122.0,-24.0,121.0));
lc.insert(new ORTLine(52.0,122.0,-24.0,121.0));
lc.insert(new ORTLine(55.0,127.0,-104.0,126.0));
lc.insert(new ORTLine(66.0,127.0,84.0,127.0));
lc.insert(new ORTLine(66.0,127.0,84.0,127.0));
lc.insert(new ORTLine(-97.0,129.0,12.0,131.0));
lc.insert(new ORTLine(-97.0,129.0,12.0,131.0));
lc.insert(new ORTLine(-98.0,132.0,6.0,134.0));
lc.insert(new ORTLine(-98.0,132.0,6.0,134.0));
lc.insert(new ORTLine(-251.0,131.0,-258.0,132.0));
lc.insert(new ORTLine(-83.0,139.0,269.0,141.0));
lc.insert(new ORTLine(73.0,139.0,109.0,139.0));
lc.insert(new ORTLine(-318.0,166.0,-262.0,167.0));
lc.insert(new ORTLine(-264.0,170.0,-318.0,169.0));
lc.insert(new ORTLine(200.0,208.0,188.0,210.0));
lc.insert(new ORTLine(185.0,208.0,176.0,208.0));
lc.insert(new ORTLine(201.0,29.0,206.0,26.0));
lc.insert(new ORTLine(201.0,29.0,206.0,26.0));
lc.insert(new ORTLine(166.0,42.0,162.0,44.0));
lc.insert(new ORTLine(166.0,42.0,162.0,44.0));
lc.insert(new ORTLine(164.0,46.0,169.0,44.0));

        Dimension d= new Dimension(640,480);
        int[] demo= new int[d.width*d.height];
        for (int i=0;i<demo.length;i++) demo[i]=0xffffffff;
/*
        for (int i=0;i<lc.size();i++) {
            ORTLine line= lc.getLineAt(i);
            DrawUtil.ort2image(demo, d, line, 0xff000000, 1);
        }
*/
        System.err.println("size before perspective: "+lc.size());
        lc.refineByPerspectiveRelation();

        for (int i=0;i<lc.size();i++) {
            ORTLine line= lc.getLineAt(i);
            DrawUtil.ort2image(demo, d, line, 0xff000000, 1);
        }
        (new DisplayFrame("demo", demo, d)).show();


        //lc.refinedBySpatialRelationRepeatly(0.27);
        //lc.lines= lc.refinedBySpatialRelation_test(lc.lines);

        Vector newlcs2=lc.splitBySpatialRelation(0.18, 0.6);

        LineCluster[] rst2= new LineCluster[newlcs2.size()];
        for (int i=0;i<newlcs2.size();i++) rst2[i]=(LineCluster)newlcs2.elementAt(i);
        ConsistentLineCluster.drawLineClusters("split-vh", d, rst2, -1);


    }

}