added IntersectionRayMesh

This commit is contained in:
Paolo Cignoni 2005-02-15 15:00:26 +00:00
parent 2fe93647ab
commit 099bae2d71
1 changed files with 90 additions and 47 deletions

View File

@ -5,7 +5,8 @@
#include<vcg/space/intersection3.h>
#include<vcg/space/index/grid_static_ptr.h>
#include<vcg/complex/trimesh/base.h>
using namespace std;
namespace vcg{
@ -66,55 +67,97 @@ bool Intersect( GridType & grid,Plane3<ScalarType> plane, vector<typename Grid
words there are 2*n vertices where n is the number of segments fo the mesh. You can run vcg::edge::Unify to unify
the vertices closer that a given value epsilon. Note that, due to subtraction error during triangle plane intersection,
it is not safe to put epsilon to 0.
*/
// TODO si dovrebbe considerare la topologia face-face della trimesh per derivare quella della edge mesh..
*/
template < typename TriMeshType, typename EdgeMeshType, class ScalarType>
bool Intersection( TriMeshType & m, Plane3<ScalarType> pl,EdgeMeshType & em,double& ave_length,
typename GridStaticPtr<typename TriMeshType::FaceContainer> *grid,
typename vector< typename GridStaticPtr<typename TriMeshType::FaceContainer>::Cell* >& cells){
typedef typename TriMeshType::FaceContainer FaceContainer;
typedef GridStaticPtr<FaceContainer> GridType;
EdgeMeshType::VertexIterator vi;
TriMeshType::FaceIterator fi;
vector<TriMeshType::FaceType*> v;
v.clear();
Intersect(*grid,pl,cells);
Segment3<ScalarType> seg;
ave_length = 0.0;
vector<GridType::Cell*>::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<EdgeMeshType>::AddEdges(em,1);
vi = vcg::edge::Allocator<EdgeMeshType>::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<TriMeshType::FaceType*>::iterator v_i;
for(v_i=v.begin(); v_i!=v.end(); ++v_i) (*v_i)->ClearS();
bool Intersection( TriMeshType & m,
Plane3<ScalarType> pl,
EdgeMeshType & em,
double& ave_length,
typename GridStaticPtr<typename TriMeshType::FaceContainer> *grid,
typename vector< typename GridStaticPtr<typename TriMeshType::FaceContainer>::Cell* >& cells)
{
typedef typename TriMeshType::FaceContainer FaceContainer;
typedef GridStaticPtr<FaceContainer> GridType;
EdgeMeshType::VertexIterator vi;
TriMeshType::FaceIterator fi;
vector<TriMeshType::FaceType*> v;
v.clear();
Intersect(*grid,pl,cells);
Segment3<ScalarType> seg;
ave_length = 0.0;
vector<GridType::Cell*>::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<EdgeMeshType>::AddEdges(em,1);
vi = vcg::edge::Allocator<EdgeMeshType>::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<TriMeshType::FaceType*>::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<ScalarType> & ray,
/* Intersect Point */ Point3<ScalarType> & hitPoint)
{
//typedef typename TriMeshType::FaceContainer FaceContainer;
TriMeshType::FaceIterator fi;
bool hit=false;
if(m==0) return false;
//TriMeshType::FaceIterator fi;
//vector<TriMeshType::FaceType*>::iterator fi;
ScalarType bar1,bar2,dist;
Point3<ScalarType> p1;
Point3<ScalarType> p2;
Point3<ScalarType> p3;
for(fi = m->face.begin(); fi != m->face.end(); ++fi)
{
p1=vcg::Point3<ScalarType>( (*fi).P(0).X() ,(*fi).P(0).Y(),(*fi).P(0).Z() );
p2=vcg::Point3<ScalarType>( (*fi).P(1).X() ,(*fi).P(1).Y(),(*fi).P(1).Z() );
p3=vcg::Point3<ScalarType>( (*fi).P(2).X() ,(*fi).P(2).Y(),(*fi).P(2).Z() );
if(Intersection<ScalarType>(ray,p1,p2,p3,bar1,bar2,dist))
{
hitPoint= p1*(1-bar1-bar2) + p2*bar1 + p3*bar2;
hit=true;
}
}
return hit;
}
/*@}*/
} // end namespace vcg