diff --git a/vcg/complex/intersection.h b/vcg/complex/intersection.h index b717c598..8fea47a7 100644 --- a/vcg/complex/intersection.h +++ b/vcg/complex/intersection.h @@ -5,7 +5,8 @@ #include #include - +#include +using namespace std; namespace vcg{ @@ -66,55 +67,97 @@ bool Intersect( GridType & grid,Plane3 plane, vector -bool Intersection( TriMeshType & m, Plane3 pl,EdgeMeshType & em,double& ave_length, - typename GridStaticPtr *grid, - typename vector< typename GridStaticPtr::Cell* >& cells){ - typedef typename TriMeshType::FaceContainer FaceContainer; - typedef GridStaticPtr GridType; - EdgeMeshType::VertexIterator vi; - TriMeshType::FaceIterator fi; - vector v; - v.clear(); - Intersect(*grid,pl,cells); - Segment3 seg; - ave_length = 0.0; - vector::iterator ic; - GridType::Cell fs,ls; - for(ic = cells.begin(); ic != cells.end();++ic) - { - grid->Grid(*ic,fs,ls); - GridType::Link * lk = fs; - while(lk != ls){ - TriMeshType::FaceType & face = *(lk->Elem()); - if(!face.IsS()) - { - face.SetS(); - v.push_back(&face); - if(vcg::Intersection(pl,face,seg))// intersezione piano triangolo - { - face.SetS(); - // add to em - ave_length+=seg.Length(); - vcg::edge::Allocator::AddEdges(em,1); - vi = vcg::edge::Allocator::AddVertices(em,2); - (*vi).P() = seg.P0(); - em.edges.back().V(0) = &(*vi); - vi++; - (*vi).P() = seg.P1(); - em.edges.back().V(1) = &(*vi); - } - }//endif - lk++; - }//end while - } - ave_length/=em.en; - vector::iterator v_i; - for(v_i=v.begin(); v_i!=v.end(); ++v_i) (*v_i)->ClearS(); +bool Intersection( TriMeshType & m, + Plane3 pl, + EdgeMeshType & em, + double& ave_length, + typename GridStaticPtr *grid, + typename vector< typename GridStaticPtr::Cell* >& cells) +{ + typedef typename TriMeshType::FaceContainer FaceContainer; + typedef GridStaticPtr GridType; + EdgeMeshType::VertexIterator vi; + TriMeshType::FaceIterator fi; + vector v; + v.clear(); + Intersect(*grid,pl,cells); + Segment3 seg; + ave_length = 0.0; + vector::iterator ic; + GridType::Cell fs,ls; + for(ic = cells.begin(); ic != cells.end();++ic) + { + grid->Grid(*ic,fs,ls); + GridType::Link * lk = fs; + while(lk != ls){ + TriMeshType::FaceType & face = *(lk->Elem()); + if(!face.IsS()) + { + face.SetS(); + v.push_back(&face); + if(vcg::Intersection(pl,face,seg))// intersezione piano triangolo + { + face.SetS(); + // add to em + ave_length+=seg.Length(); + vcg::edge::Allocator::AddEdges(em,1); + vi = vcg::edge::Allocator::AddVertices(em,2); + (*vi).P() = seg.P0(); + em.edges.back().V(0) = &(*vi); + vi++; + (*vi).P() = seg.P1(); + em.edges.back().V(1) = &(*vi); + } + }//endif + lk++; + }//end while + } + ave_length/=em.en; + vector::iterator v_i; + for(v_i=v.begin(); v_i!=v.end(); ++v_i) (*v_i)->ClearS(); - return true; + return true; +} +/*****************************************************************/ +/*INTERSECTION RAY - MESH */ +/* */ +/* Intersection between a Ray and a Mesh. Returns a 3D Pointset! */ +/*****************************************************************/ +template < typename TriMeshType, class ScalarType> +bool IntersectionRayMesh( + /* Input Mesh */ TriMeshType * m, + /* Ray */ const Line3 & ray, + /* Intersect Point */ Point3 & hitPoint) +{ + //typedef typename TriMeshType::FaceContainer FaceContainer; + TriMeshType::FaceIterator fi; + bool hit=false; + + if(m==0) return false; + + //TriMeshType::FaceIterator fi; + //vector::iterator fi; + + ScalarType bar1,bar2,dist; + Point3 p1; + Point3 p2; + Point3 p3; + for(fi = m->face.begin(); fi != m->face.end(); ++fi) + { + p1=vcg::Point3( (*fi).P(0).X() ,(*fi).P(0).Y(),(*fi).P(0).Z() ); + p2=vcg::Point3( (*fi).P(1).X() ,(*fi).P(1).Y(),(*fi).P(1).Z() ); + p3=vcg::Point3( (*fi).P(2).X() ,(*fi).P(2).Y(),(*fi).P(2).Z() ); + if(Intersection(ray,p1,p2,p3,bar1,bar2,dist)) + { + hitPoint= p1*(1-bar1-bar2) + p2*bar1 + p3*bar2; + hit=true; + } } + + return hit; +} /*@}*/ } // end namespace vcg \ No newline at end of file