From 9ec0e7b57181e59baf8364d4743883953a90ab82 Mon Sep 17 00:00:00 2001 From: nicopietroni Date: Thu, 25 Jan 2007 01:04:54 +0000 Subject: [PATCH] added: - RayLineIntersection - RaySegmentIntersection - SegmentSegmentIntersection --- vcg/space/intersection2.h | 74 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/vcg/space/intersection2.h b/vcg/space/intersection2.h index c741fbed..2afb3a24 100644 --- a/vcg/space/intersection2.h +++ b/vcg/space/intersection2.h @@ -30,6 +30,7 @@ #ifndef __VCGLIB_INTERSECTION_2 #define __VCGLIB_INTERSECTION_2 #include +#include #include #include #include @@ -37,6 +38,7 @@ + namespace vcg { /** \addtogroup space */ /*@{*/ @@ -90,6 +92,46 @@ inline bool LineLineIntersection(const vcg::Line2 & l0, return true; } +///return if exist the intersection point +///between 2 lines in a 2d plane +template +inline bool RayLineIntersection(const vcg::Line2 & l, + const vcg::Ray2 & r, + Point2 &p) +{ + ///construct line from ray + vcg::Line2 l_test; + l_test.Set(r.Origin(),r.Direction()); + if (!LineLineIntersection(l,l_test,p)) + return false; + Point2 dir=p-r.Origin(); + dir.Normalize(); + return (dir*r.Direction()>0); +} + + +/// interseciton between point and triangle +template +inline bool RaySegmentIntersection(const vcg::Ray2 & r, + 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(!RayLineIntersection(line2,r,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 LineSegmentIntersection(const vcg::Line2 & line, @@ -112,6 +154,38 @@ inline bool LineSegmentIntersection(const vcg::Line2 & line, return ((d0 +inline bool SegmentSegmentIntersection(const vcg::Segment2 &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; + + 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)