#include "../inc/shared.h"

const bool LineAABBIntersect( const class Line &l, const class AABB &b )
{
  /* ALGORITHM: Use the separating axis 
  theorem to see if the line segment 
  and the box overlap. A line 
  segment is a degenerate OBB. */

  const Vec3 T = b.center - l.center;
  Vec3 v;
  Scalar r;

  //do any of the principal axes form a separating axis?
  if( fabs(T[0]) > b.extents[0] + l.hl*fabs(l.dir[0]) )
    return false;

  if( fabs(T[1]) > b.extents[1] + l.hl*fabs(l.dir[1]) )
    return false;

  if( fabs(T[2]) > b.extents[2] + l.hl*fabs(l.dir[2]) )
    return false;

  /* NOTE: Since the separating axis is
  perpendicular to the line in these
  last four cases, the line does not
  contribute to the projection. */

  //l.cross(x-axis)?
  r = b.extents[1]*fabs(l.dir[2]) + b.extents[2]*fabs(l.dir[1]);
  if( fabs(T[1]*l.dir[2] - T[2]*l.dir[1]) > r )
    return false;

  //l.cross(y-axis)?
  r = b.extents[0]*fabs(l.dir[2]) + b.extents[2]*fabs(l.dir[0]);
  if( fabs(T[2]*l.dir[0] - T[0]*l.dir[2]) > r )
    return false;

  //l.cross(z-axis)?
  r = b.extents[0]*fabs(l.dir[1]) + b.extents[1]*fabs(l.dir[0]);
  if( fabs(T[0]*l.dir[1] - T[1]*l.dir[0]) > r )
    return false;

  return true;
}


