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

package rec.fsm;

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	EdgeSelfRecognizer.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 EdgeSelfRecognizer extends Aligner
{

  public EdgeSelfRecognizer()
  {
  }

  /////////////////////////////////////////////////////////////////////////////
  
  /**
   * 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;
    Console.writeln( "EdgeSelf online" );
    Console.indent();

    boolean changed = false;
    int counter = 0;

    Iterator e = s.getWidgetsIterator();
    while( e.hasNext() )
      {
	Object o = e.next();
	if ( o instanceof rec.fsm.Node )
	  {
	    //Console.writeln( "examining node #" + ++counter + ": " + o );
	    Node node = (Node)o;

	    if ( Geometry.distance( node.getCenter(), c.getCenter() )
		 <= ( c.getRadius() + node.getRadius() ) )
	      {
		//make a self edge.
		Edge ed = new Edge( node, s );
		s.replaceWidget( c, ed );

		forceSnap( s, ed, node, true );
		forceSnap( s, ed, node, false );

		Console.outdent();
		return true;
	      }
	  }
      }
    
    Console.outdent();
    return false;
  }



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

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

    return( v.iterator() );
  }

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

    return v.iterator();
  }
}

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