rewriting of the SegmentPointSquaredDistance, now it is much much much shorter.

This commit is contained in:
Paolo Cignoni 2012-03-14 15:57:37 +00:00
parent ad417602bd
commit b57a747441
1 changed files with 8 additions and 27 deletions

View File

@ -224,35 +224,16 @@ void LineLineDistance(const vcg::Line3<ScalarType> &mLine0,
template <class ScalarType> template <class ScalarType>
void SegmentPointSquaredDistance( Segment3<ScalarType> s, void SegmentPointSquaredDistance( Segment3<ScalarType> s,
const Point3<ScalarType> & p, const Point3<ScalarType> & p,
Point3< ScalarType > &clos, Point3< ScalarType > &closest,
ScalarType &sqr_dist) ScalarType &sqr_dist)
{ {
///create a line Point3<ScalarType> e = s.P1()-s.P0();
vcg::Line3<ScalarType> l; ScalarType t = ((p-s.P0())*e)/e.SquaredNorm();
l.Set(s.P0(),s.P1()-s.P0()); if(t<0) t = 0;
l.Normalize(); else if(t>1) t = 1;
closest = s.P0()+e*t;
///take the closest point on a Line sqr_dist = SquaredDistance(p,closest);
vcg::LinePointSquaredDistance(l,p,clos,sqr_dist); assert(!math::IsNAN(sqr_dist));
///quick test with a BB
vcg::Box3<ScalarType> b;
b.Add(s.P0());
b.Add(s.P1());
///if the point is in the BB since it is ALSO along the line
///means that it is also along the SEGMENT
if (b.IsIn(clos))
return;
else
{
///check the extremes
ScalarType d0=(s.P0()-p).SquaredNorm();
ScalarType d1=(s.P1()-p).SquaredNorm();
if (d0<d1)
clos= (s.P0());
else
clos= (s.P1());
}
} }
/* /*