diff --git a/vcg/space/intersection2.h b/vcg/space/intersection2.h index 073f3ce2..c081c200 100644 --- a/vcg/space/intersection2.h +++ b/vcg/space/intersection2.h @@ -187,39 +187,38 @@ namespace vcg { return ((d0 inline bool SegmentSegmentIntersection(const vcg::Segment2 &seg0, const vcg::Segment2 &seg1, Point2 &p_inters) { - vcg::Line2 l0,l1; + const SCALAR_TYPE Eps= SCALAR_TYPE(1e-8); + SCALAR_TYPE lambda0,lambda1; + const Point2 & p0 = seg0.P0(); + const Point2 & p1 = seg0.P1(); + const Point2 & p2 = seg1.P0(); + const Point2 & p3 = seg1.P1(); - l0.SetOrigin(seg0.P0()); - vcg::Point2 dir0=seg0.P1()-seg0.P0(); - dir0.Normalize(); - l0.SetDirection(dir0); + SCALAR_TYPE a = (p1-p0)[0]; + SCALAR_TYPE b = (p2-p3)[0]; + SCALAR_TYPE c = (p1-p0)[1]; + SCALAR_TYPE d = (p2-p3)[1]; - l1.SetOrigin(seg1.P0()); - vcg::Point2 dir1=seg1.P1()-seg1.P0(); - dir1.Normalize(); - l1.SetDirection(dir1); - 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(); + SCALAR_TYPE e = (p2-p0)[0]; + SCALAR_TYPE f = (p2-p0)[1]; - if ((d0>len0)||(d1>len1)) + SCALAR_TYPE det = a*d-b*c; + + lambda0 = (d*e-b*f)/det; + lambda1 = (-c*e+a*f)/det; + if (fabs(det)= 0.0 && lambda0 <= 1.0 && lambda1 >= 0.0 && lambda1 <= 1.0)) 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; - + p_inters = p0*(1-lambda0)+p1*lambda0; return true; - } /// interseciton between point and triangle template