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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	rec.sys.SquiggleParser.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	08-XX-98
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:     rec.sys.Squiggle parser
//
// HISTORY:     11-15-98 archon
//              Now aware of new rec.geo package
//
////////////////////////////////////////////////////////////////////////////////

package rec.core;

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

public final class SquiggleParser {

  public static boolean isCurve( rec.sys.Squiggle st )
  {
    int numPoints = st.getNumberOfPoints();

    if( numPoints <= 2 )
      return( false );

    GPoint prev = st.getPointAt( 0 );
    GPoint mid = st.getPointAt( 1 );
    GPoint curr = st.getPointAt( 2 );

    double ang = Geometry.angleBetween( prev, mid, curr );

    // Resembles a line not a curve.

    if( ang == Math.PI )
      return( false );

    // Only three points, so it resembles a curve.

    if( numPoints == 3 )
      return( true );

    // The other cases.

    boolean isLeftOriented = true;

    if( ang > Math.PI )
      isLeftOriented = false;

    for( int i = 3; i < numPoints; ++i ) {

      prev = mid;
      mid = curr;
      curr = st.getPointAt( i );

      if( (new GSegment( new GPoint( mid ),
			 new GPoint( curr ) )).getLength()
	  <= 5.0 )
	continue;

      if( !isValid( Geometry.angleBetween( prev, mid, curr ),
		    isLeftOriented ) )
	return( false );

      if( !isValid( Geometry.angleBetween( mid, curr,
					   st.getPointAt( 0 ) ),
		    isLeftOriented ) )
	return( false );

      if( !isValid( Geometry.angleBetween( curr,
					   st.getPointAt( 0 ),
					   st.getPointAt( 1 ) ),
		    isLeftOriented ) )
	return( false );
    }

    return( true );
  }

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

  private static boolean isValid( double ang,
				  boolean isLeftOriented )
  {
    if( ang == Math.PI )
      return( false );

    if( !( (ang < Math.PI && isLeftOriented)
	   || (ang > Math.PI && !isLeftOriented) ) )
      return( false );

    return( true );
  }
}

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