diff --git a/vcg/complex/intersection.h b/vcg/complex/intersection.h index 331912b2..ab27889a 100644 --- a/vcg/complex/intersection.h +++ b/vcg/complex/intersection.h @@ -59,7 +59,7 @@ added vcg header #include #include #include -#include +#include #include #ifndef __VCGLIB_INTERSECTION_TRI_MESH @@ -153,8 +153,8 @@ bool Intersect( GridType & grid,Plane3 plane, std::vector::AddEdges(em,1); - vi = vcg::edge::Allocator::AddVertices(em,2); + vcg::edg::Allocator::AddEdges(em,1); + vi = vcg::edg::Allocator::AddVertices(em,2); (*vi).P() = seg.P0(); em.edges.back().V(0) = &(*vi); vi++; @@ -177,7 +177,7 @@ bool Intersect( GridType & grid,Plane3 plane, std::vector::AddEdges(em,1); - vi = vcg::edge::Allocator::AddVertices(em,2); + vcg::edg::Allocator::AddEdges(em,1); + vi = vcg::edg::Allocator::AddVertices(em,2); (*vi).P() = seg.P0(); em.edges.back().V(0) = &(*vi); vi++; @@ -297,7 +297,7 @@ void IntersectionBallMesh( TriMeshType & m, const vcg::Sphere3 &bal vcg::Point3 witness; std::pair info; - if(tol == 0) tol = M_PI * ball.Radius() * ball.Radius() / 10000; + if(tol == 0) tol = M_PI * ball.Radius() * ball.Radius() / 100000; for(fi = m.face.begin(); fi != m.face.end(); ++fi) if(!(*fi).IsD() && IntersectionSphereTriangle(ball ,(*fi), witness , &info)) @@ -350,6 +350,74 @@ void IntersectionBallMesh( TriMeshType & m, const vcg::Sphere3 &bal } } + +template < typename TriMeshType, class ScalarType, class IndexingType> +void IntersectionBallMesh( IndexingType * grid, TriMeshType & m, const vcg::Sphere3 &ball, TriMeshType & res, + float tol = 0){ + + typename TriMeshType::VertexIterator v0,v1,v2; + typename std::vector::iterator cfi; + typename TriMeshType::FaceIterator fi; + std::vector closestsF,closests; + vcg::Point3 witness; + std::vector > witnesses; + std::vector distances; + std::pair info; + + if(tol == 0) tol = M_PI * ball.Radius() * ball.Radius() / 100000; + + vcg::tri::GetInSphereFace(m,*grid, ball.Center(), ball.Radius(),closestsF,distances,witnesses); + for(cfi =closestsF.begin(); cfi != closestsF.end(); ++cfi) + if(!(**cfi).IsD() && IntersectionSphereTriangle(ball ,(**cfi), witness , &info)) + closests.push_back(&(**cfi)); + + res.Clear(); + SubSet(res,closests); + int i =0; + while(i(ball ,res.face[i], witness , &info) && !allIn){ + if(vcg::DoubleArea(res.face[i]) > tol) + { + // split the face res.face[i] in four, add the four new faces to the mesh and delete the face res.face[i] + v0 = vcg::tri::Allocator::AddVertices(res,3); + fi = vcg::tri::Allocator::AddFaces(res,4); + + v1 = v0; ++v1; + v2 = v1; ++v2; + (*v0).P() = (res.face[i].P(0) + res.face[i].P(1))*0.5; + (*v1).P() = (res.face[i].P(1) + res.face[i].P(2))*0.5; + (*v2).P() = (res.face[i].P(2) + res.face[i].P(0))*0.5; + + (*fi).V(0) = res.face[i].V(0); + (*fi).V(1) = &(*v0); + (*fi).V(2) = &(*v2); + ++fi; + + (*fi).V(0) = res.face[i].V(1); + (*fi).V(1) = &(*v1); + (*fi).V(2) = &(*v0); + ++fi; + + (*fi).V(0) = &(*v0); + (*fi).V(1) = &(*v1); + (*fi).V(2) = &(*v2); + ++fi; + + (*fi).V(0) = &(*v2); + (*fi).V(1) = &(*v1); + (*fi).V(2) = res.face[i].V(2) ; + + vcg::tri::Allocator::DeleteFace(res,res.face[i]); + } + }// there was no intersection with the boundary + + if(info.first > 0.0) // closest point - radius. If >0 is outside + vcg::tri::Allocator::DeleteFace(res,res.face[i]); + ++i; + } +} + /*@}*/ } // end namespace vcg #endif