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

// -*-Mode: Java-*- 
// $Id: PulleyWheelRecognizer.java,v 1.2 2003/03/06 00:40:41 moltmans Exp $
// PulleyWheelRecognizer.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Maintainer: Michael Oltmans <moltmans@ai.mit.edu> 
// Copyright: Copyright (C) 1999 by Michael Oltmans 
// Created: <Fri Oct 15 17:56:42 1999> 
// Time-stamp: <2000-05-22 22:08:36 moltmans> 
// Keywords: 
package rec.meche;

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


public class PulleyWheelRecognizer extends Recognizer
{
  private WPulleyWheel isPulleyWheel( WCircle c1, WCircle c2 )
  {
    if ( Estimator.connects( c1.getCenter(), c2.getCenter(), 20 ) &&
	 ( c1 != c2 ) )
    {
      if ( c1.getRadius() > c2.getRadius() )
	return new WPulleyWheel( c1.getCircle() );
      else
	return new WPulleyWheel( c2.getCircle() );
    }
    return null;      
  }
  

  public PulleyWheelRecognizer()
  {
  }
 

  public boolean recognize( Surface s, Widget w )
  {
    WCircle circle = (WCircle)w;
    WPulleyWheel wheel = null;
    GeometryCalculator gc = new GeometryCalculator( s );

    Iterator it = gc.getVisibleWidgets().iterator();

    while ( it.hasNext() ) {
      VisibleWidget vw = (VisibleWidget)it.next();

      if( vw instanceof WCircle ) {
	wheel = isPulleyWheel( (WCircle)vw, circle );
	if ( wheel != null ) {
	  HashSet olds = new HashSet();
	  olds.add( circle );
	  olds.add( vw );
	  s.replaceWidget( olds, wheel, true );
	  DebugWindow.addLine( "Pull", "Found a pulley wheel" );
	  return true;
	}
      }
    }

    return false;
  }
  
  public Iterator getInputTypes()
  {
    Vector v = new Vector();

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

    return( v.iterator() );
  }

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

    v.addElement( "rec.meche.WPulleyWheel" );

    return( v.iterator() );
  }

}
