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

package rec.core.recognizers;

import edu.mit.sketch.geom.Ellipse;
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.WCircle;
import rec.core.widgets.WLine;
import rec.geo.GPoint;
import rec.sys.Recognizer;
import rec.sys.Squiggle;
import rec.sys.Surface;
import rec.sys.Widget;


/**
 * 
 * <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: ToolkitCircleRecognizer.java,v 1.2 2003/03/06 00:40:47 moltmans Exp $
 **/

public class ToolkitCircleRecognizer extends Recognizer
{
  
  public ToolkitCircleRecognizer() {}

  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
    if( c == null ) {
      return false;
    }
    
    if( c.isEllipse() ) {
      // now make sure that it is more or less round since we can't
      // really deal with ellipses.
      Ellipse ellipse = c.getEllipseApproximation();
      double roundness = Math.abs( ellipse.getWidth() / ellipse.getHeight() ) - 1.0;
      if( roundness - 1 < 0.4 ) {
	WCircle circle = new WCircle( ellipse.getBounds() );
	s.replaceWidget( squiggle, circle );
	return true;
      }
    }
    
    return false;
  }

  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.WLine" );

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

