diff --git a/vcg/space/intersection2.h b/vcg/space/intersection2.h index 6d51df88..dce93b0b 100644 --- a/vcg/space/intersection2.h +++ b/vcg/space/intersection2.h @@ -67,7 +67,7 @@ inline bool LineLineIntersection(const vcg::Line2 & l0, const vcg::Line2 & l1, Point2 &p) { - const SCALAR_TYPE EPSILON= SCALAR_TYPE(1e-8); + const SCALAR_TYPE Eps= SCALAR_TYPE(1e-8); ///first line SCALAR_TYPE x1=l0.Origin().X(); SCALAR_TYPE y1=l0.Origin().Y(); @@ -84,7 +84,7 @@ inline bool LineLineIntersection(const vcg::Line2 & l0, ///denominator SCALAR_TYPE den=((x1-x2)*(y3-y4))-((y1-y2)*(x3-x4)); - if (fabs(den) &seg0, const vcg::Segment2 &seg1, Point2 &p_inters) { - ///test intersection of bbox - vcg::Box2 bb0,bb1; - bb0.Add(seg0.P0()); - bb0.Add(seg0.P1()); - bb1.Add(seg1.P0()); - bb1.Add(seg1.P1()); - if (!bb0.Collide(bb1)) - return false; - else - { - ///first compute intersection between lines - vcg::Line2 l0,l1; + vcg::Line2 l0,l1; + + l0.SetOrigin(seg0.P0()); + vcg::Point2 dir0=seg0.P1()-seg0.P0(); + dir0.Normalize(); + l0.SetDirection(dir0); + + l1.SetOrigin(seg1.P0()); + vcg::Point2 dir1=seg1.P1()-seg1.P0(); + dir1.Normalize(); + l1.SetDirection(dir1); + bool b=LineLineIntersection(l0,l1,p_inters); + SCALAR_TYPE len0=seg0.Length(); + SCALAR_TYPE len1=seg1.Length(); + SCALAR_TYPE d0=(seg0.P0()-p_inters).Norm(); + SCALAR_TYPE d1=(seg1.P0()-p_inters).Norm(); + + if ((d0>len0)||(d1>len1)) + return false; + + vcg::Point2 dir2=p_inters-seg0.P0(); + vcg::Point2 dir3=p_inters-seg1.P0(); + if (((dir2*dir0)<0)||((dir3*dir1)<0)) + return false; + + return true; - l0.SetOrigin(seg0.P0()); - vcg::Point2 dir0=seg0.P1()-seg0.P0(); - dir0.Normalize(); - l0.SetDirection(dir0); - l1.SetOrigin(seg1.P0()); - vcg::Point2 dir1=seg1.P1()-seg1.P0(); - dir1.Normalize(); - l1.SetDirection(dir1); - return ((LineSegmentIntersection(l0,seg1,p_inters))&& - (LineSegmentIntersection(l1,seg0,p_inters))); - } } - /// interseciton between point and triangle template inline bool IsInsideTrianglePoint( const Triangle2 & t,const Point2 & p)