// (c) MIT 2003.  All rights reserved.

package rec.core.recognizers;

import edu.mit.sketch.geom.Approximation;
import edu.mit.sketch.geom.Point;
import edu.mit.sketch.geom.Polygon;
import edu.mit.sketch.toolkit.SimpleClassifier;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;
import rec.core.widgets.WPolyLine;
import rec.geo.GPoint;
import rec.sys.Recognizer;
import rec.sys.Squiggle;
import rec.sys.Surface;
import rec.sys.Widget;
import rec.util.Assert;


/**
 * 
 * <p>
 * Created:   Mon Mar 18 15:45:38 2002<br>
 * Copyright: Copyright (C) 2002 by MIT.  All rights reserved.<br>
 * 
 * @author <a href="mailto:moltmans@ai.mit.edu">Michael Oltmans</a>
 * @version $Id: ToolkitPolyLineRecognizer.java,v 1.3 2003/03/06 00:40:47 moltmans Exp $
 **/

public class ToolkitPolyLineRecognizer extends Recognizer
{
  
  public boolean recognize( Surface s, Widget w )
  {
    Squiggle squiggle = (Squiggle)w;

    //    Console.writeln( "line recognizer activated" );

    SimpleClassifier c = squiggle.getClassifier();

    // the classifier may be null if there was not enough data for it to operate
    // also check that this is either a polygon or close to one
    if( c == null ) {
      return false;
    }

    if( ! c.isPolygon() ) {
      return false;      
    }

    Polygon polygon = c.getPolygonApproximation( 1.05 );
    Assert.notNull( polygon, "The claassifier returned a null polygon approximation" );

    if( polygon.npoints < 3 ) {
      return false;
    }

    WPolyLine polyline = new WPolyLine( polygon );
    s.replaceWidget( squiggle, polyline, true );

    return true;
  }

  public Iterator getInputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.sys.Squiggle" );

    return( v.iterator() );
  }

  public Iterator getOutputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.core.widgets.WPolyLine" );

    return( v.iterator() );
  }
  
}// ToolkitFacitedLineBreakerRecognizer

