/** 
  * 
  * See the end of the file for the log of changes.
  * 
  * $Author: mtsezgin $
  * $Date: 2000/06/03 01:52:32 $   
  * $Revision: 1.4 $
  * $Headers$
  * $Id: Filter.java,v 1.4 2000/06/03 01:52:32 mtsezgin Exp $     
  * $Name:  $   
  * $Locker:  $
  * $Source: /mit/mtsezgin/CVS/Repository/REC/Filter.java,v $
  *  
  **/


import java.util.Enumeration;
import java.util.Vector;


/**
  *
  * This class manages the objects recognized by the Tablet.
  * When Tablet adds the recognized objects using the addObject 
  * to the ObjectManager, the ObjectManager passes the input throug
  * this filter first, and then adds the object. Later depending
  * on the rest of the input, the Filter may remove the previously
  * added objects from the ObjectManager.
  *
  **/

class Filter
{
    /**
    *
    * The vector for holding the line sequences.
    *
    **/    
    private Vector line_sequences;

    /**
    *
    * The object manager associated with this object.
    *
    **/    
    private ObjectManager object_manager;

    /**
    *
    * The constructor.
    *
    **/    
    Filter( ObjectManager object_manager )
    {
        line_sequences = new Vector( 10, 0 );
        this.object_manager = object_manager;
    }

    /**
    *
    * Filters the object.
    *
    * Known eksik: Fails to recognize multiple sequences separated
    * by non-sequence objects. Should be modified to recognize 
    * the case where a sequence of lines is followed by another
    * line that is not a part of the sequence.
    *
    **/
    public Vector
    filter( Object object )
    {
        Vector return_vector = new Vector();
        
        // Sorry about this if.. -Metin
        if ( ( object instanceof Line )        && 
             ( ( line_sequences.isEmpty() )    ||
               ( ( !line_sequences.isEmpty() ) &&
                 ( (((Line)object).getTimeStamp() - 
                    ((Line)line_sequences.lastElement()).getTimeStamp()) <
                    Tablet.sequence_delay ) &&
                 ( GeometryUtil.linesParallel( 
                     ((Line)line_sequences.lastElement()), 
                    ((Line)object), 
                    Math.PI/6 ) )
                 ) ) ) {
            System.out.println( "\nIF TAKEN" );
            System.out.println( "object.getTimeStamp() " +
                ((Line)object).getTimeStamp() );
            if ( line_sequences.size() != 0 )
            System.out.println( "line_sequences.lastElement().getTimeStamp() " +
                ((Line)line_sequences.lastElement()).getTimeStamp() );
            
            line_sequences.addElement( object );
        } else {
            System.out.println( "\nELSE TAKEN" );
            Vector shade_lines;
            Line   current    = null;
            Line   next       = null;
            int    last_start = 0;
            int    length     = 0;
            long   time_stamp = 0;
            for ( int i=0; i<line_sequences.size()-1; i++ ) {
                current = (Line)line_sequences.elementAt( i );
                next    = (Line)line_sequences.elementAt( i+1 );
                if ( (( next.getTimeStamp() - current.getTimeStamp() ) <
                      Tablet.sequence_delay ) &&
                     ( GeometryUtil.linesParallel( next, current, Math.PI/6 ))){
                     length++;
                } else {
                    length     = 0;
                    last_start = i+1;
                }
            }
            
            
            if ( length > 2 ) {
                shade_lines = new Vector();
                time_stamp  = ((Line)line_sequences.elementAt(
                                  last_start)).getTimeStamp();
                // + 1 For catching the last line
                for ( int i=last_start; i<last_start+length+1; i++ ) {
                    shade_lines.addElement( line_sequences.elementAt( i ) );
                    object_manager.removeObject( line_sequences.elementAt(i) );
                }
                Shade shade = new Shade( shade_lines );
                shade.setTimeStamp( time_stamp );

                return_vector.addElement( shade );
                return_vector.addElement( object );
                line_sequences.clear();
                return return_vector;
            }
            line_sequences.clear();
        }
        
        return_vector.addElement( object );
        return return_vector;
    }    
}


/** 
  * 
  * $Log: Filter.java,v $
  * Revision 1.4  2000/06/03 01:52:32  mtsezgin
  * *** empty log message ***
  *
  * Revision 1.3  2000/05/26 20:41:27  mtsezgin
  *
  * Fixed the bug in shade recognition. Now if the user draws a line
  * that is not parallel to the previous ones, or that is drawn
  * significantly later than the last line, we stop buffering and
  * process the buffered lines.
  *
  * Revision 1.2  2000/04/20 03:59:52  mtsezgin
  * *** empty log message ***
  *
  * Revision 1.1  2000/04/13 06:16:02  mtsezgin
  *
  * Just started, and for now it filters out Shade objects using the timing data.
  *
  *  
  **/
