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

// $Id: SpatialPoolBuilder.java,v 1.6 2003/03/06 00:40:48 moltmans Exp $
// Time-stamp: <2000-03-11 18:54:48 moltmans>
package rec.core;



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

public class SpatialPoolBuilder extends PoolBuilder 
{
  public SpatialPoolBuilder()
  {
    super();
  }


  /**
   * Called by Surface on each relevant Recognizer.
   *
   * @param w   The last thing added to the Surface, the root Widget
   *     which caused recognition to begin again.
   *
   * 
   */
  protected Collection findRelevantWidgets( Surface s, WidgetPool wp, 
					    Widget sw )
  {
    Collection relevant = new HashSet();
    
    if ( sw instanceof VisibleWidget ) {
      VisibleWidget spark = (VisibleWidget)sw;
    
    
      relevant.addAll( s.getSurfacePool().getWidgets() );
      
      Location spark_l = spark.getLocation();
      GPoint spark_c = spark_l.getCenter();
      double spark_r = spark_l.getRadius();
      
      Iterator it = wp.iterator();
      while( it.hasNext() ) {
	Widget w = (Widget)it.next();
	if ( w.isRoot() ) {
	  if ( w instanceof VisibleWidget ) {
	    Location l = ((VisibleWidget)w).getLocation();
	    GPoint c = l.getCenter();
	    double r = l.getRadius();
	    if ( Geometry.distance( spark_c, c ) < r + spark_r ) {
	      relevant.add( w );
	      Collection allCh = w.getAllChildren();
	      //  Iterator itch = allCh.iterator();
//  	      while ( itch.hasNext() ) {
//  		Console.writeln( itch.next() + " is relevant" );
	      //}
	      relevant.addAll( allCh );
	    }
	  }
	}
      }
    }

    return relevant;
  }

}
