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

package rec.fsm;

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	SmartEdgeRecognizer.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.*;

public class SmartEdgeRecognizer extends Recognizer
{

  public SmartEdgeRecognizer()
  {
  }

  /////////////////////////////////////////////////////////////////////////////
  
  /**
   * 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 )
  {
    final WLine c = (WLine)w;


	// look for nearby lines/edges
	GeometryCalculator gc = new GeometryCalculator( s );
	GeoFunction gf = new GeoFunction() {
	  public boolean check( VisibleWidget vw )
	    {
	      if ( vw instanceof WCircle )
		{
		  WCircle cc = (WCircle)vw;
		  if ( cc.contains( c.getHead(), 5 )
		       || cc.contains( c.getTail(), 5 ) )
		    return true;
		}
	      return false;
	    }
	};

	Widget fw = gc.findWidget( gf );

	if ( fw == null )
	  return false;

	gc.recheck( gf );

    Console.writeln( "Edge online" );

    Edge edge = new Edge( c, s );
    
    s.replaceWidget( c, edge );


    return true;
  }

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

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

    return( v.iterator() );
  }

  public Iterator getOutputTypes()
  {
    Vector v = new Vector();
    v.addElement( "rec.fsm.Edge" );

    return v.iterator();
  }
}

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