From f949a83faf02f6abbb012d682b6aa70a79f08caa Mon Sep 17 00:00:00 2001 From: nicopietroni Date: Thu, 1 Jun 2006 08:38:02 +0000 Subject: [PATCH] Added functions: - Intersection_Segment_Triangle - Intersection_Plane_Box - Intersection_Triangle_Box --- vcg/space/intersection3.h | 125 ++++++++++++++++++++++++++++++++++---- 1 file changed, 114 insertions(+), 11 deletions(-) diff --git a/vcg/space/intersection3.h b/vcg/space/intersection3.h index fb034f67..23a7ee07 100644 --- a/vcg/space/intersection3.h +++ b/vcg/space/intersection3.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.23 2006/03/29 07:53:36 cignoni +Missing ';' (thx Maarten) + Revision 1.22 2006/03/20 14:42:49 pietroni IntersectionSegmentPlane and Intersection_Segment_Box functions Added @@ -479,38 +482,43 @@ bool Intersection_Ray_Box( const Box3 & box, const Ray3 & r, Point3 & c } // segment-box return fist intersection found from p0 to p1 -template -bool Intersection_Segment_Box( const Box3 & box, const Segment3 & s, Point3 & coord ) +template +bool Intersection_Segment_Box( const Box3 & box, + const Segment3 & s, + Point3 & coord ) { //as first perform box-box intersection - Box3 test; + Box3 test; test.Add(s.P0()); test.Add(s.P1()); if (!test.Collide(box)) return false; else { - Line3 l; - Point3 dir=s.P1()-s.P0(); + Line3 l; + Point3 dir=s.P1()-s.P0(); dir.Normalize(); l.SetOrigin(s.P0()); l.SetDirection(dir); - if(Intersection_Line_Box(box,l,coord)) + if(Intersection_Line_Box(box,l,coord)) return (test.IsIn(coord)); return false; } -} +} // segment-box intersection , return number of intersections and intersection points -template -int Intersection_Segment_Box( const Box3 & box, const Segment3 & s, Point3 & coord0, Point3 & coord1 ) +template +int Intersection_Segment_Box( const Box3 & box, + const Segment3 & s, + Point3 & coord0, + Point3 & coord1 ) { int num=0; - Segment3 test= s; + Segment3 test= s; if (Intersection_Segment_Box(box,test,coord0 )) { num++; - Point3 swap=test.P0(); + Point3 swap=test.P0(); test.P0()=test.P1(); test.P1()=swap; if (Intersection_Segment_Box(box,test,coord1 )) @@ -519,6 +527,101 @@ int Intersection_Segment_Box( const Box3 & box, const Segment3 & s, Point3 return num; } + +// segment-triangle intersection +template +bool Intersection_Segment_Triangle( const vcg::Segment3 & seg, + const Point3 & vert0, + const Point3 & vert1, const + Point3 & vert2, + ScalarType & a ,ScalarType & b, ScalarType & dist) +{ + //first set both directions of ray + vcg::Ray3 ray; + vcg::Point3 dir; + dir=(seg.P1()-seg.P0()); + dir.Normalize(); + ray.Set(seg.P0(),dir); + + //then control for each direction the interseciton with triangle + if ((Intersection(ray,vert0,vert1,vert2,a,b,dist)) + ||(Intersection(ray,vert0,vert2,vert1,a,b,dist))) + return (dist<(seg.P1()-seg.P0()).Norm()); + else + return(false); +} + +template +bool Intersection_Plane_Box(const vcg::Plane3 &pl, + vcg::Box3 &bbox) +{ + + typedef typename Segment3 SegmentType; + typedef typename vcg::Point3 CoordType; + SegmentType diag[4]; + + CoordType intersection; + //find the 4 diagonals + diag[0]=SegmentType(bbox.P(0),bbox.P(7)); + diag[1]=SegmentType(bbox.P(1),bbox.P(6)); + diag[2]=SegmentType(bbox.P(2),bbox.P(5)); + diag[3]=SegmentType(bbox.P(3),bbox.P(4)); + ScalarType a,b,dist; + for (int i=0;i<3;i++) + //call intersection of segment and plane + if (vcg::Intersection(pl,diag[i],intersection)) + return true; + return false; +} + +template +bool Intersection_Triangle_Box( vcg::Box3 &bbox, + const vcg::Point3 &p0, + const vcg::Point3 &p1, + const vcg::Point3 &p2) +{ + typedef typename vcg::Point3 CoordType; + CoordType intersection; + + /// control bounding box collision + vcg::Box3 test; + test.SetNull(); + test.Add(p0); + test.Add(p1); + test.Add(p2); + if (!test.Collide(bbox)) + return false; + /// control if each point is inside the bouding box + if ((bbox.IsIn(p0))||(bbox.IsIn(p1))||(bbox.IsIn(p2))) + return true; + + /////control plane of the triangle with bbox + //vcg::Plane3 plTri=vcg::Plane3(); + //plTri.Init(p0,p1,p2); + //if (!Intersection_Plane_Box(plTri,bbox)) + // return false; + + ///then control intersection of segments with box + if (Intersection_Segment_Box(bbox,vcg::Segment3(p0,p1),intersection)|| + Intersection_Segment_Box(bbox,vcg::Segment3(p1,p2),intersection)|| + Intersection_Segment_Box(bbox,vcg::Segment3(p2,p0),intersection)) + return true; + ///control intersection of diagonal of the cube with triangle + + Segment3 diag[4]; + + diag[0]=Segment3(bbox.P(0),bbox.P(7)); + diag[1]=Segment3(bbox.P(1),bbox.P(6)); + diag[2]=Segment3(bbox.P(2),bbox.P(5)); + diag[3]=Segment3(bbox.P(3),bbox.P(4)); + ScalarType a,b,dist; + for (int i=0;i<3;i++) + if (Intersection_Segment_Triangle(diag[i],p0,p1,p2,a,b,dist)) + return true; + + return false; +} + template bool Intersection (const Plane3 & plane0, const Plane3 & plane1, Line3 & line)