From d4ad0bd6f6235c33528c1b86611cd33d60e6e375 Mon Sep 17 00:00:00 2001 From: nicopietroni <nico.pietroni@isti.cnr.it> Date: Fri, 25 May 2012 13:32:48 +0000 Subject: [PATCH] added check if length of segment is zero then function SegmentPointDistance still works --- vcg/space/distance3.h | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/vcg/space/distance3.h b/vcg/space/distance3.h index b0f46fa2..0c9916cc 100644 --- a/vcg/space/distance3.h +++ b/vcg/space/distance3.h @@ -227,13 +227,23 @@ void SegmentPointSquaredDistance( Segment3<ScalarType> s, Point3< ScalarType > &closest, ScalarType &sqr_dist) { - Point3<ScalarType> e = s.P1()-s.P0(); - ScalarType t = ((p-s.P0())*e)/e.SquaredNorm(); - if(t<0) t = 0; - else if(t>1) t = 1; - closest = s.P0()+e*t; - sqr_dist = SquaredDistance(p,closest); - assert(!math::IsNAN(sqr_dist)); + Point3<ScalarType> e = s.P1()-s.P0(); + ScalarType EPS=0.00000001; + if (e.Norm()<EPS) + { + Point3<ScalarType> AvP=(s.P0()+s.P1())/2.0; + closest=AvP; + sqr_dist=(AvP-p).Norm(); + } + else + { + ScalarType t = ((p-s.P0())*e)/e.SquaredNorm(); + if(t<0) t = 0; + else if(t>1) t = 1; + closest = s.P0()+e*t; + sqr_dist = SquaredDistance(p,closest); + assert(!math::IsNAN(sqr_dist)); + } } /*