bool Line::intersect( float angle, const Box3f& box ) const { if (box.isEmpty()) return false; const Vec3f &max = box.getMax(), &min = box.getMin(); float fuzz = 0.0; int i; if (angle < 0.0) { fuzz = - angle; } else { // Find the farthest point on the bounding box (where the pick // cone will be largest). The amount of fuzz at this point will // be the minimum we can use. Expand the box by that amount and // do an intersection. double tanA = tan(angle); for(i = 0; i < 8; i++) { Vec3f point(i & 01 ? min[0] : max[0], i & 02 ? min[1] : max[1], i & 04 ? min[2] : max[2]); // how far is point from line origin?? Vec3f diff(point - getPosition()); double thisFuzz = sqrt(diff.dot(diff)) * tanA; if (thisFuzz > fuzz) fuzz = float(thisFuzz); } } Box3f fuzzBox = box; fuzzBox.extendBy(Vec3f(min[0] - fuzz, min[1] - fuzz, min[2] - fuzz)); fuzzBox.extendBy(Vec3f(max[0] + fuzz, max[1] + fuzz, max[2] + fuzz)); Vec3f scratch1, scratch2; return intersect(fuzzBox, scratch1, scratch2); }
void Sphere::circumscribe( const Box3f& box ) { m_center = 0.5f * (box.getMin() + box.getMax()); m_radius = (box.getMax() - m_center).getLength(); }
bool Line::intersect( const Box3f& box, Vec3f& enter, Vec3f& exit ) const { if (box.isEmpty()) { return false; } const Vec3f &pos = getPosition(), &dir = getDirection(); const Vec3f &max = box.getMax(), &min = box.getMin(); Vec3f points[8], inter, bary; Plane plane; int i, v0, v1, v2; bool front = false, valid, validIntersection = false; // // First, check the distance from the ray to the center // of the box. If that distance is greater than 1/2 // the diagonal distance, there is no intersection // diff is the vector from the closest point on the ray to the center // dist2 is the square of the distance from the ray to the center // radi2 is the square of 1/2 the diagonal length of the bounding box // float t = (box.getCenter() - pos).dot(dir); Vec3f diff(pos + dir * t - box.getCenter()); float dist2 = diff.dot(diff); float radi2 = (max - min).dot(max - min) * 0.25f; if (dist2 > radi2) { return false; } // set up the eight coords of the corners of the box for(i = 0; i < 8; i++) { points[i].setValue(i & 01 ? min[0] : max[0], i & 02 ? min[1] : max[1], i & 04 ? min[2] : max[2]); } // intersect the 12 triangles. for(i = 0; i < 12; i++) { switch(i) { case 0: v0 = 2; v1 = 1; v2 = 0; break; // +z case 1: v0 = 2; v1 = 3; v2 = 1; break; case 2: v0 = 4; v1 = 5; v2 = 6; break; // -z case 3: v0 = 6; v1 = 5; v2 = 7; break; case 4: v0 = 0; v1 = 6; v2 = 2; break; // -x case 5: v0 = 0; v1 = 4; v2 = 6; break; case 6: v0 = 1; v1 = 3; v2 = 7; break; // +x case 7: v0 = 1; v1 = 7; v2 = 5; break; case 8: v0 = 1; v1 = 4; v2 = 0; break; // -y case 9: v0 = 1; v1 = 5; v2 = 4; break; case 10: v0 = 2; v1 = 7; v2 = 3; break; // +y case 11: v0 = 2; v1 = 6; v2 = 7; break; default: assert(false && "Must never happened"); v0 = v1 = v2 = 0; // to remove a warning. } valid = intersect( points[v0], points[v1], points[v2], inter, bary, front); if ( valid ) { if (front) { enter = inter; validIntersection = valid; } else { exit = inter; validIntersection = valid; } } } return validIntersection; }
bool Box3f::operator ==( const Box3f& b2 ) const { return ( (getMin() == b2.getMin()) && (getMax() == b2.getMax()) ); }