Bug fixed in the ClosestPoint2Box2 function

This commit is contained in:
Gianpaolo Palma 2011-03-21 17:03:34 +00:00
parent e90c417d2b
commit e3d8489f0f
1 changed files with 17 additions and 17 deletions

View File

@ -321,31 +321,31 @@ ScalarType DistancePoint2Box2(const Point2<ScalarType> &test,
template <class ScalarType>
Point2<ScalarType> ClosestPoint2Box2(const Point2<ScalarType> &test,
const Box2<ScalarType> &bbox)
const Box2<ScalarType> &bbox)
{
Segment2<ScalarType> Segs[4];
Segs[0].P0()=bbox.min;
Segs[0].P1()=vcg::Point2<ScalarType>(bbox.max.X(),bbox.min.Y());
Segs[0].P0() = bbox.min;
Segs[0].P1() = vcg::Point2<ScalarType>(bbox.max.X(), bbox.min.Y());
Segs[1].P0()=Segs[0].P(1);
Segs[1].P1()=bbox.max;
Segs[1].P0() = Segs[0].P(1);
Segs[1].P1() = bbox.max;
Segs[2].P0()=Segs[1].P(1);
Segs[2].P1()=vcg::Point2<ScalarType>(bbox.min.X(),bbox.max.Y());
Segs[2].P0() = Segs[1].P(1);
Segs[2].P1() = vcg::Point2<ScalarType>(bbox.min.X(), bbox.max.Y());
Segs[3].P0()=Segs[2].P(1);
Segs[3].P1()=bbox.min;
Segs[3].P0() = Segs[2].P(1);
Segs[3].P1() = bbox.min;
Point2<ScalarType> closest=ClosestPoint(Segs[0],test);
ScalarType minDist=(closest-test).Norm();
for (int i=0;i<4;i++)
Point2<ScalarType> closest = ClosestPoint(Segs[0], test);
ScalarType minDist = (closest-test).Norm();
for (int i = 1; i < 4; i++)
{
Point2<ScalarType> test=ClosestPoint(Segs[i],test);
ScalarType dist=(closest-test).Norm();
if (dist<minDist)
Point2<ScalarType> point = ClosestPoint(Segs[i], test);
ScalarType dist = (test - point).Norm();
if (dist < minDist)
{
minDist=dist;
closest=test;
minDist = dist;
closest = point;
}
}
return closest;