From 94cb8fca1de9f925396a7f82ff4d6a87cf86f031 Mon Sep 17 00:00:00 2001 From: nicopietroni Date: Wed, 5 Mar 2008 11:45:36 +0000 Subject: [PATCH] added DistancePoint2Box2 --- vcg/space/box2.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/vcg/space/box2.h b/vcg/space/box2.h index 87ac868f..b9ca48c6 100644 --- a/vcg/space/box2.h +++ b/vcg/space/box2.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.8 2007/07/02 10:01:00 corsini +fix area + Revision 1.7 2006/10/07 16:50:26 m_di_benedetto Added Dim() method. @@ -234,6 +237,54 @@ public: } }; // end class definition +template +ScalarType DistancePoint2Box2(const Point2 &test, + const Box2 &bbox) +{ + ///test possible position respect to bounding box + if (!bbox.IsIN(test)){ + if ((test.X()<=bbox.min.X())&&(test.Y()<=bbox.min.Y())) + return ((test-bbox.min).Norm()); + else + if ((test.X()>=bbox.min.X())&& + (test.X()<=bbox.max.X())&& + (test.Y()<=bbox.min.Y())) + return (bbox.min.Y()-test.Y()); + else + if ((test.X()>=bbox.max.X())&& + (test.Y()<=bbox.min.Y())) + return ((test-vcg::Point2(bbox.max.X(),bbox.min.Y())).Norm()); + else + if ((test.Y()>=bbox.min.Y())&& + (test.Y()<=bbox.max.Y())&& + (test.X()>=bbox.max.X())) + return (test.X()-bbox.max.X()); + else + if ((test.X()>=bbox.max.X())&&(test.Y()>=bbox.max.Y())) + return ((test-bbox.max).Norm()); + else + if ((test.X()>=bbox.min.X())&& + (test.X()<=bbox.max.X())&& + (test.Y()>=bbox.max.Y())) + return (test.Y()-bbox.max.Y()); + else + if ((test.X()<=bbox.min.X())&& + (test.Y()>=bbox.max.Y())) + return ((test-vcg::Point2(bbox.min.X(),bbox.max.Y())).Norm()); + else + if ((test.X()<=bbox.min.X())&& + (test.Y()<=bbox.max.Y())&& + (test.Y()>=bbox.min.Y())) + return (bbox.min.X()-test.X()); + } + else + { + //return minimum distance + ScalarType dx=std::min(fabs(test.X()-bbox.min.X()),fabs(bbox.max.X()-test.X())); + ScalarType dy=std::min(fabs(test.Y()-bbox.min.Y()),fabs(bbox.max.Y()-test.Y())); + return(std::min(dx,dy)); + } +} /// Specificazione di box of short typedef Box2 Box2s;