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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	LineRecognizer.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
//              Adapted from code written by Luke Weisman.
//              luke@ai.mit.edu
//
// DATE:	Jun 98
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Drawing recognition
//
// HISTORY:     Jun 98  archon  created
//
//              Sep 98  archon
//              Modified to fit with new recognition model.
//
//              Nov 98  archon
//              Now aware of new rec.geo package.
//
////////////////////////////////////////////////////////////////////////////////

package rec.core.recognizers;

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

public class LineRecognizer extends Recognizer
{
  public static boolean isLine( rec.sys.Squiggle ns )
  {
    // archon:
    //
    // Replaced old code with new code.

    int num = ns.getNumberOfPoints();

    // 2 points at least!
    if( num < 2 )
      return( false );

    GSegment s = new GSegment( ns.getFirstPoint(), ns.getLastPoint() );

    for( int i = 0; i < num; ++i ) {

      GPoint p = ns.getPointAt( i );
      GPoint perp = s.perpendicular( p );

      if( Geometry.distance( p, perp ) > 10.0 )
	return( false );
    }

    return( true );

  }

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

  public LineRecognizer()
  {
  }

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

  public boolean recognize( Surface s, Widget w )
  {
    rec.sys.Squiggle st = (rec.sys.Squiggle)w;

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

/*
    if(Geometry.distance( st.getFirstPoint(), st.getLastPoint() ) < 50 )
      return false;
*/

    if ( isLine( st ) ) {

      //      Console.writeln( "line online" );
      
      WLine l = new WLine( st );

      s.replaceWidget( st, l );

      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() );
  }
}

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