Added ClosestPoint2Box2 function

This commit is contained in:
Nico Pietroni 2010-05-20 13:58:08 +00:00
parent 4b45045c18
commit 44618a036a
1 changed files with 33 additions and 0 deletions

View File

@ -55,6 +55,7 @@ Initial commit
#include <assert.h>
#include <vcg/math/base.h>
#include <vcg/space/point2.h>
namespace vcg {
/** \addtogroup space */
@ -304,6 +305,38 @@ ScalarType DistancePoint2Box2(const Point2<ScalarType> &test,
}
}
template <class ScalarType>
Point2<ScalarType> ClosestPoint2Box2(const Point2<ScalarType> &test,
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[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[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> test=ClosestPoint(Segs[i],test);
ScalarType dist=(closest-test).Norm();
if (dist<minDist)
{
minDist=dist;
closest=test;
}
}
return closest;
}
/// Specificazione di box of short
typedef Box2<short> Box2s;
/// Specificazione di box of int