// AxisAlignedBox.cpp: implementation of the AxisAlignedBox class. // ////////////////////////////////////////////////////////////////////// #include "AxisAlignedBox.h" using namespace Geometry; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// AxisAlignedBox::AxisAlignedBox() { } AxisAlignedBox::~AxisAlignedBox() { } AxisAlignedBox::AxisAlignedBox(Point mins, Vector dims) : mins(mins), dims(dims) { } double AxisAlignedBox::min_distance_from(const Point &p, Point &closest_surf_pt) { // No, this is plain wrong. It only gets the minimum distance to the // 6 face planes..not to the cube. ASSERT(FALSE); // Don't call this function. Vector d; d = p - this->mins; double closest_abs_distance = this->distance_to_side_plane(p, 0, 0); double closest_signed_distance = Utils::abs_d(closest_abs_distance); closest_surf_pt = this->projected_point(p, 0, 0); // Go through each dimension, looking for the closest for( int dim = 0; dim < 3; dim++ ) { // Check each side for( int side = 0; side < 2; side++ ) { const double signed_distance = this->distance_to_side_plane(p, dim, side); const double abs_distance = Utils::abs_d(signed_distance); if( abs_distance < closest_abs_distance ) { closest_abs_distance = abs_distance; closest_signed_distance = signed_distance; closest_surf_pt = this->projected_point(p, dim, side); } } } return closest_signed_distance; } bool AxisAlignedBox::contains_point(const Point &p) { Vector d; d = p - this->mins; return ( ( 0 <= d[0] && d[0] <= this->dims(0) ) && ( 0 <= d[1] && d[1] <= this->dims(1) ) && ( 0 <= d[2] && d[2] <= this->dims(2) )); } Point AxisAlignedBox::projected_point(const Point &p, int dim, int side) { Point projected; projected = p; if( side == 0 ) { projected[dim] = this->mins(dim); } else if( side == 1) { projected[dim] = this->maxs(dim); } else { ASSERT(false); } return p; } // Disitance is positive if the point is within the box double AxisAlignedBox::distance_to_side_plane(const Point &p, int dim, int side) { if( side == 0 ) { return p(dim) - this->mins(dim); } else if(side == 1) { return this->maxs(dim) - p(dim); } else { ASSERT(false); return -1; } } double AxisAlignedBox::maxs(int dim) { return (this->mins(dim) + this->dims(dim)); } void AxisAlignedBox::run_unit_tests() { AxisAlignedBox box( Point(-1,-1,-1), Vector(2, 2, 2)); Point closest; double min_dist; min_dist = box.min_distance_from( Point(0,0,0), closest ); ASSERT( min_dist == 1.0 ); min_dist = box.min_distance_from( Point(1,0,0), closest ); ASSERT( min_dist == 0.0 ); min_dist = box.min_distance_from( Point(-1,0,0), closest ); ASSERT( min_dist == 0.0 ); min_dist = box.min_distance_from( Point(-0.5,0.5,-0.75), closest ); ASSERT( min_dist == 0.25 ); min_dist = box.min_distance_from( Point(-0.5,0.5,-1.25), closest ); ASSERT( min_dist == -0.25 ); }