Merge branch 'mathias/intersect'

This commit is contained in:
Tim Moore 2010-05-06 10:43:27 +02:00
commit da07871bc6
2 changed files with 11 additions and 17 deletions

View File

@ -71,6 +71,14 @@ public:
(pt[2] > center[2]) ? _min[2] : _max[2]);
}
// return the closest point to pt still in the box
template<typename S>
SGVec3<T> getClosestPoint(const SGVec3<S>& pt) const
{
return SGVec3<T>((pt[0] < _min[0]) ? _min[0] : ((_max[0] < pt[0]) ? _max[0] : T(pt[0])),
(pt[1] < _min[1]) ? _min[1] : ((_max[1] < pt[1]) ? _max[1] : T(pt[1])),
(pt[2] < _min[2]) ? _min[2] : ((_max[2] < pt[2]) ? _max[2] : T(pt[2])));
}
// Only works for floating point types
SGVec3<T> getCenter() const

View File

@ -38,25 +38,11 @@ intersects(const SGBox<T1>& box, const SGSphere<T2>& sphere)
{
if (sphere.empty())
return false;
// Is more or less trivially included in the next tests
// if (box.empty())
// return false;
if (sphere.getCenter().x() < box.getMin().x() - sphere.getRadius())
return false;
if (sphere.getCenter().y() < box.getMin().y() - sphere.getRadius())
return false;
if (sphere.getCenter().z() < box.getMin().z() - sphere.getRadius())
if (box.empty())
return false;
if (box.getMax().x() + sphere.getRadius() < sphere.getCenter().x())
return false;
if (box.getMax().y() + sphere.getRadius() < sphere.getCenter().y())
return false;
if (box.getMax().z() + sphere.getRadius() < sphere.getCenter().z())
return false;
return true;
SGVec3<T1> closest = box.getClosestPoint(sphere.getCenter());
return distSqr(closest, SGVec3<T1>(sphere.getCenter())) <= sphere.getRadius2();
}
// make it symmetric
template<typename T1, typename T2>