diff --git a/vcg/space/intersection2.h b/vcg/space/intersection2.h index 9a6a3a41..62103087 100644 --- a/vcg/space/intersection2.h +++ b/vcg/space/intersection2.h @@ -21,7 +21,7 @@ * * ****************************************************************************/ /**************************************************************************** - + History ****************************************************************************/ @@ -29,9 +29,11 @@ #ifndef __VCGLIB_INTERSECTION_2 #define __VCGLIB_INTERSECTION_2 - +#include +#include #include #include +#include @@ -50,6 +52,65 @@ template return (((p0-p1)^(p2-p1))<=0); } +///return if exist the intersection point +///between 2 lines in a 2d plane +template +inline bool LineLineIntersection(const vcg::Line2 & l0, + const vcg::Line2 & l1, + Point2 &p) +{ + const SCALAR_TYPE EPSILON= SCALAR_TYPE(1e-8); + ///first line + SCALAR_TYPE x1=l0.Origin().X(); + SCALAR_TYPE y1=l0.Origin().Y(); + SCALAR_TYPE x2=x1+l0.Direction().X(); + SCALAR_TYPE y2=y1+l0.Direction().Y(); + + ///second line + SCALAR_TYPE x3=l1.Origin().X(); + SCALAR_TYPE y3=l1.Origin().Y(); + SCALAR_TYPE x4=x3+l1.Direction().X(); + SCALAR_TYPE y4=y3+l1.Direction().Y(); + + ///then find intersection + + ///denominator + SCALAR_TYPE den=((x1-x2)*(y3-y4))-((y1-y2)*(x3-x4)); + if (fabs(den) +inline bool LineSegmentIntersection(const vcg::Line2 & line, + const vcg::Segment2 &seg, + Point2 &p_inters) +{ + ///first compute intersection between lines + vcg::Line2 line2; + line2.SetOrigin(seg.P0()); + vcg::Point2 dir=seg.P1()-seg.P0(); + dir.Normalize(); + line2.SetDirection(dir); + if(!LineLineIntersection(line,line2,p_inters)) + return false; + ///then test if intersection point is nearest + ///to both extremes then lenght of the segment + SCALAR_TYPE d0=(seg.P1()-p_inters).Norm(); + SCALAR_TYPE d1=(seg.P0()-p_inters).Norm(); + SCALAR_TYPE lenght=(seg.P0()-seg.P1()).Norm(); + return ((d0 inline bool Intersection( const Triangle2 & t,const Point2 & p)