////////////////////////////////////////////////////////////////////////////////
//																			  //
// AUTHOR:      Tevfik Metin Sezgin	                        				  //
//				Massachusetts Institute of Technology						  //
//				Department of Electrical Engineering and Computer Science	  //
//				Artificial Intelligence Laboratory							  //
//																			  //
// E-MAIL:		mtsezgin@ai.mit.edu, mtsezgin@mit.edu						  //
//																			  //
// COPYRIGHT:   Tevfik Metin Sezgin                    						  //
//              All rights reserved. This code can not be copied, modified,   //
//              or distributed in whole or partially without the written 	  //
//				permission of the author. Also see the COPYRIGHT file.		  //
//																			  //
////////////////////////////////////////////////////////////////////////////////

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


import java.awt.Component;
import java.awt.Container;

import java.util.Vector;

/**
  *
  * Implements some methods not found in jdk 1.1
  *
  **/
public class
Util
{
    /**
    *
    * Print array
    *
    **/
    public static void
    printArray( Object[] object, String name )
    {
		for ( int i=0; i<object.length; i++ ) {
			System.out.println( name + "[" + i + "] = " + object[i] );
		}
    }


    /**
    *
    * Print array
    *
    **/
    public static void
    printArray( double data[], String name )
    {
		for ( int i=0; i<data.length; i++ ) {
			System.out.println( name + "[" + i + "] = " + data[i] );
		}
    }


    /**
    *
    * Print array
    *
    **/
    public static void
    printArray( int data[], String name )
    {
		for ( int i=0; i<data.length; i++ ) {
			System.out.println( name + "[" + i + "] = " + data[i] );
		}
    }


    /**
    *
    * Print array
    *
    **/
    public static void
    printArray( long data[], String name )
    {
		for ( int i=0; i<data.length; i++ ) {
			System.out.println( name + "[" + i + "] = " + data[i] );
		}
    }


    /**
    *
    * Print arrays side by side
    *
    **/
    public static void
    printArrays( double data1[], String name1, double data2[], String name2 )
    {
		for ( int i=0; i<data1.length; i++ ) {
			System.out.println( name1 + "[" + i + "] = " + data1[i] + " " +
								name2 + "[" + i + "] = " + data2[i] );
		}
    }


    /**
    *
    * Print arrays side by side
    *
    **/
    public static void
    printArrays( double data1[], String name1, long data2[], String name2 )
    {
		for ( int i=0; i<data1.length; i++ ) {
			System.out.println( name1 + "[" + i + "] = " + data1[i] + " " +
								name2 + "[" + i + "] = " + data2[i] );
		}
    }


    /**
    *
    * Print arrays side by side
    *
    **/
    public static void
    printArrays( long data1[], String name1, double data2[], String name2 )
    {
		for ( int i=0; i<data1.length; i++ ) {
			System.out.println( name1 + "[" + i + "] = " + data1[i] + " " +
								name2 + "[" + i + "] = " + data2[i] );
		}
    }


    /**
    *
    * Print arrays side by side
    *
    **/
    public static void
    printArrays( int data1[], String name1, int data2[], String name2 )
    {
		for ( int i=0; i<data1.length; i++ ) {
			System.out.println( name1 + "[" + i + "] = " + data1[i] + " " +
								name2 + "[" + i + "] = " + data2[i] );
		}
    }


    /**
    *
    * Print arrays side by side
    *
    **/
    public static void
    printArrays( long data1[], String name1, long data2[], String name2 )
    {
		for ( int i=0; i<data1.length; i++ ) {
			System.out.println( name1 + "[" + i + "] = " + data1[i] + " " +
								name2 + "[" + i + "] = " + data2[i] );
		}
    }


    /**
    *
    * Print to string array
    *
    **/
    public static String[]
    printToStringArray( double data[], String name )
    {
		String result[] = new String[data.length];
		for ( int i=0; i<data.length; i++ ) {
			result[i] = name + "[" + i + "] = " + data[i];
		}
		
		return result;
    }


    /**
    *
    * Print to string array
    *
    **/
    public static String[]
    printToStringArray( long data[], String name )
    {
		String result[] = new String[data.length];
		for ( int i=0; i<data.length; i++ ) {
			result[i] = name + "[" + i + "] = " + data[i];
		}
		
		return result;
    }

	
    /**
    * 
    * Convolve input using the filter. The length of the filter
	* must be less than or equal to that of input, and it should be 
	* and odd number. The center factor in the filter corresponds
	* to the current element in the input. The end points are 
	* hadled separately and scaled.
    *
    **/
    public static double[]
    convolve( double input[], double filter[] )
    {
		double filter_sum = 0.0;
		double result[]   = new double[input.length];
		int    l          = (filter.length-1)/2;

		for ( int i=0; i<filter.length; i++ ) {
			filter_sum += filter[i];
		}

		for ( int i=l; i<input.length-l; i++ ) {
			result[i] = 0.0;
			for ( int j=0; j<filter.length; j++ ) {
				result[i] += filter[j]*input[i-l+j];
			}
			result[i] /= filter_sum;
		}
		
		double partial_sum = 0.0;
		for ( int i=0; i<l; i++ ) {
			partial_sum = 0.0;
			result[i]   = 0.0;
			for ( int j=0; j<filter.length; j++ ) {
				if ( i-l+j >= 0 ) {
					result[i]   += filter[j]*input[i-l+j];
					partial_sum += filter[j];
				}
			}
			result[i] /= partial_sum;
		}
		for ( int i=input.length-l; i<input.length; i++ ) {
			partial_sum = 0.0;
			result[i]   = 0.0;
			for ( int j=0; j<filter.length; j++ ) {
				if ( i-l+j < input.length ) {
					result[i]   += filter[j]*input[i-l+j];
					partial_sum += filter[j];
				}
			}
			result[i] /= partial_sum;
		}
		
		return result;
	}
}


/** 
  * 
  * $Log: GraphicsUtil.java,v $
  * Revision 1.4  2000/06/03 01:52:33  mtsezgin
  * *** empty log message ***
  *
  * Revision 1.3  2000/04/28 04:45:03  mtsezgin
  *
  * Now each GeometricObject keeps the mouse input that was previously
  * discarded. User can switch between seeing the recognized mode and
  * the raw mode. setDataPoints( Polygon points ) and getDataPoints()
  * are added to GeometricObject, and all the implementors are modified
  * accordingly.
  *
  * Revision 1.2  2000/04/25 22:15:29  mtsezgin
  *
  * Added more utility functions.
  *
  * Revision 1.1.1.1  2000/04/01 03:07:07  mtsezgin
  * Imported sources
  *
  * Revision 1.2  2000/03/31 22:41:03  mtsezgin
  *
  * Started Log tracking.
  *
  *  
  **/
