From 2ebecdde24b79c4cacf5ea41cbfb0437e78035e6 Mon Sep 17 00:00:00 2001 From: cnr-isti-vclab Date: Tue, 12 Jun 2007 15:40:41 +0000 Subject: [PATCH] Added method that computes the minimum distance between a segment and a point --- vcg/space/segment3.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/vcg/space/segment3.h b/vcg/space/segment3.h index de26dda3..8eccb70b 100644 --- a/vcg/space/segment3.h +++ b/vcg/space/segment3.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.7 2006/06/06 14:35:31 zifnab1974 +Changes for compilation on linux AMD64. Some remarks: Linux filenames are case-sensitive. _fileno and _filelength do not exist on linux + Revision 1.6 2006/03/07 16:39:38 pietroni compiled and corrected ClosestPoint function @@ -153,6 +156,28 @@ typedef Segment3 Segment3i; typedef Segment3 Segment3f; typedef Segment3 Segment3d; +/* +* Computes the minimum distance between a segment and a point +* @param[in] segment The input segment +* @param[in] p The input point +* @return The distance between the segment and the point p +*/ +template < class ScalarType > +ScalarType SquaredDistance(Segment3< ScalarType > &segment, Point3< ScalarType > &p) +{ + typedef typename vcg::Point3< ScalarType > Point3t; + + Point3t dir = (segment.P1()-segment.P0()).Normalize(); + ScalarType h = dir * (p-segment.P0()); + if (h<=ScalarType(0.0)) return vcg::SquaredDistance(p, segment.P0()); + else if (h>=segment.Length()) return vcg::SquaredDistance(p, segment.P1()); + else + { + dir = segment.P0() + dir*h; + return vcg::SquaredDistance(p, dir); + } +}; //end of Distance method + template Point3 ClosestPoint( Segment3 s, const Point3 & p) {