#ifndef CIRCLED2_H
#define CIRCLED2_H

#include <boxOBBhalfspaceD2.h>
#include <line.h>


/*!
\brief Circle and OBB intersection.
*/
template< typename PT, typename PD >
class circleD2
{
public:

  /** The circles center. */
  PT center;
  /** Circles radius. */
  PD radius;

  /** Construct a circle. */
  circleD2
  (
    PT const & center_,
    PD const radius_
  )
    : center(center_), radius(radius_) {}

  /** Do the circle and box intersect? */
  boolc intersects( boxOBBhalfspaceD2<PT,PD> & B );
};

//---------------------------------------------------------
// Implementation

template< typename PT, typename PD >
boolc circleD2<PT,PD>::intersects( boxOBBhalfspaceD2<PT,PD> & B )
{
  PT pi[4];
  B.cornerpoints(pi[0],pi[1],pi[2],pi[3]);

  PD t;

  bool inside=true;
  for (uint i=0; i<4; ++i)
  {
    line<PT,PD> L(pi[(i+1)%4],pi[i],true);
    PT normal(-L.nml.y,L.nml.x);
    if (normal.dot(center-L.pos)>=0.0)
    {
      inside=false;

      L.nearestpointcapped(t,center);
      if ((L(t)-center).dot()<=radius*radius)
        return true;
    }
  }

  return inside;
}


#endif



