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

package rec.fsm;

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	AcceptRecognizer.java
//
// AUTHOR:      Luke Weisman
//              luke@ai.mit.edu
//
// DATE:	June, 1998
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Drawing recognition system
//
///////////////////////////////////////////////////////////////////////////////


import java.util.*;
import rec.util.*;
import rec.sys.*;
import rec.core.*;
import rec.geo.*;


public class AcceptRecognizer extends Aligner
{

  public AcceptRecognizer()
  {
  }

  /////////////////////////////////////////////////////////////////////////////
  
  /**
   * Each new stroke generated gets put into the pipe here.
   *     It will remove and add objects to the 'out' target
   *     as it deems fit.
   *
   * @return a boolean if changes were made.
   */
  public boolean recognize( Surface s, Widget w )
  {
    WCircle c = (WCircle)w;

    GeometryCalculator gc = new GeometryCalculator( s );
    GeoFunction gf = new GeoFunction() {
      public boolean check( VisibleWidget vw )
	{
	  if ( vw instanceof Node )
	    return true;

	  return false;
	}
    };

    Node node = (Node)gc.findClosestWithCheck( c.getCenter(), gf );
    
    if ( node != null )
      {
	if ( !node.isAccept()
	     && ( Geometry.distance( node.getCenter(), c.getCenter() )
		 <= ( node.getRadius() / 2 ) ) )
	  {
	    //Console.writeln( "Accept node found!" );
	    
	    node.setAccept( true );
	    s.removeWidget( w );

	    return true;
	  }
      }

    return false;
  }



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

    v.addElement( "rec.core.WCircle" );

    return( v.iterator() );
  }

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

    return v.iterator();
  }
}

////////////////////////////////////////////////////////////////////////////////
