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

////////////////////////////////////////////////////////////////////////////////
//
// FILE:	Slice.java
//
// AUTHOR:	Manoj D. Muzumdar
//		archon@ai.mit.edu
//
// DATE:	07-XX-98
//
// COPYRIGHT:	Massachusetts Institute of Technology
//		All rights reserved.
//
// PURPOSE:	Location approximation
//
// HISTORY:     11-15-98 archon
//              Now aware of new rec.geo package
//              Made mutable
//
////////////////////////////////////////////////////////////////////////////////

package rec.loc;

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

/**
 * A Slice is a mutable representation of a degree range
 *     of a Space.
 */
public class Slice
{
  public Slice( Space s )
  {
    m_Space = new Space( s );
    m_BeginDegree = 0.0;
    m_EndDegree = 360.0;
  }

  public Slice( Space s, double beginDegree, double endDegree )
  {
    Assert.check( beginDegree >= 0.0 && beginDegree <= 360.0,
		  "beginning degree is out of bounds" );

    Assert.check( endDegree >= 0.0 && beginDegree <= 360.0,
		  "ending degree is out of bounds" );

    Assert.check( beginDegree <= endDegree,
      "beginning degree is greater than ending degree" );

    m_Space = new Space( s );
    m_BeginDegree = beginDegree;
    m_EndDegree = endDegree;
  }

  public Slice( Slice s )
  {
    this( s.m_Space, s.m_BeginDegree, s.m_EndDegree );
  }

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

  public void setCenter( GPoint gp )
  {
    m_Space.setCenter( gp );
  }

  public GPoint getCenter()
  {
    return( m_Space.getCenter() );
  }

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

  public Space getSpace()
  {
    return( m_Space );
  }

  public double getBeginDegree()
  {
    return( m_BeginDegree );
  }

  public double getEndDegree()
  {
    return( m_EndDegree );
  }

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

  public boolean intersects( Slice s )
  {
    if( !m_Space.intersects( s.m_Space ) )
      return( false );

    double d = Degree.fromRadians( Geometry.angle( m_Space.getCenter(),
						   s.m_Space.getCenter() ) );

    return( d >= m_BeginDegree && d <= m_EndDegree );
  }

  public boolean contains( Locatable l )
  {
    if( !m_Space.contains( l ) )
      return( false );

    double d = Degree.fromRadians( Geometry.angle(
      m_Space.getCenter(),
      l.getLocation().getCenter() ) );

    return( d >= m_BeginDegree && d <= m_EndDegree );
  }

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

  private Space m_Space;
  private double m_BeginDegree;
  private double m_EndDegree;
}

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