vcglib/apps/sample/trimesh_hole/trimesh_hole.cpp

291 lines
6.9 KiB
C++
Raw Normal View History

2006-11-22 14:42:06 +01:00
#include <vector>
2006-12-01 09:25:53 +01:00
#include <iostream>
2006-11-22 14:42:06 +01:00
#include<vcg/space/triangle3.h>
#include<vcg/simplex/vertex/base.h>
#include<vcg/simplex/face/base.h>
2006-11-22 14:42:06 +01:00
#include<vcg/simplex/face/topology.h>
#include<vcg/complex/trimesh/base.h>
2007-01-31 16:17:36 +01:00
#include<vcg/complex/trimesh/hole.h>
#include<vcg/complex/local_optimization.h>
#include<vcg/complex/local_optimization/tri_edge_flip.h>
2007-01-31 16:17:36 +01:00
#include<vcg/complex/trimesh/smooth.h>
#include<vcg/complex/trimesh/refine.h>
2007-01-31 16:17:36 +01:00
#include<vcg/complex/trimesh/update/selection.h>
2006-11-22 14:42:06 +01:00
// topology computation
#include<vcg/complex/trimesh/update/topology.h>
#include <vcg/complex/trimesh/update/flag.h>
2006-11-29 16:17:39 +01:00
#include <vcg/complex/trimesh/update/normal.h>
2006-11-22 14:42:06 +01:00
// half edge iterators
#include<vcg/simplex/face/pos.h>
2007-01-31 16:17:36 +01:00
2006-12-01 09:25:53 +01:00
2006-11-22 14:42:06 +01:00
// input output
#include <wrap/io_trimesh/import_ply.h>
#include <wrap/io_trimesh/export_ply.h>
2006-12-01 09:25:53 +01:00
2006-11-22 14:42:06 +01:00
using namespace vcg;
2006-12-01 09:25:53 +01:00
using namespace std;
2006-11-22 14:42:06 +01:00
[ Changes in definition of TriMesh: PART I ] Note for the developers: the change to make to existing projects is very little but strictly necessary to compile. This change IS NOT backward compliant. ==== OLD ==== way to define a TriMesh: // forward declarations class MyVertex; class MyEdge; class MyFace; class MyVertex: public VertexSimp2 < MyVertex, MyEdge, MyFace, vertex::Coord3f,...other components>{}; class MyFace: public FaceSimp2 < MyVertex, MyEdge, MyFace, face::VertexRef,...other components>{}; class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{}; ==== NEW ==== way to define a TriMesh: // forward declarations class MyVertex; class MyEdge; class MyFace; // declaration of which types is used as VertexType, which type is used as FaceType and so on... class MyUsedTypes: public vcg::UsedType < vcg::Use<MyVertex>::AsVertexType, vcg::Use<MyFace>::AsFaceType>{}; class MyVertex: public Vertex < MyUsedTypes, vertex::Coord3f,...other components>{}; class MyFace: public Face < MyUsedTypes, face::VertexRef,...other components>{}; class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{}; ===== classes introduced [vcg::UsedType] : it is a class containing all the types that must be passed to the definition of Vertex, Face, Edge... This class replaces the list of typenames to pass as first templates and the need to specify the maximal simplicial. So <MyVertex, MyEdge, MyFace becomes <MyUsedTypes< and VertexSimp2 becomes Vertex [vcg::Use] : an auxiliary class to give a simple way to specify the role of a type Note 2: the order of templates parameters to vcg::UsedTypes is unimportant, e.g: class MyUsedTypes: public vcg::UsedType <vcg::Use<MyVertex>::AsVertexType, vcg::Use<MyEdge>::AsEdgeType, vcg::Use<MyFace>::AsFaceType>{}; is the same as: class MyUsedTypes: public vcg::UsedType <vcg::Use<MyFace>::AsFaceType, vcg::Use<MyEdge>::AsEdgeType, vcg::Use<MyVertex>::AsVertexType>{}; Note 3: you only need to specify the type you use. If you do not have edges you do not need to include vcg::Use<MyEdge>::AsEdgeType in the template list of UsedTypes. ==== the Part II will be a tiny change to the class TriMesh it self.
2010-03-15 11:44:40 +01:00
2006-11-22 14:42:06 +01:00
class MyFace;
class MyVertex;
[ Changes in definition of TriMesh: PART I ] Note for the developers: the change to make to existing projects is very little but strictly necessary to compile. This change IS NOT backward compliant. ==== OLD ==== way to define a TriMesh: // forward declarations class MyVertex; class MyEdge; class MyFace; class MyVertex: public VertexSimp2 < MyVertex, MyEdge, MyFace, vertex::Coord3f,...other components>{}; class MyFace: public FaceSimp2 < MyVertex, MyEdge, MyFace, face::VertexRef,...other components>{}; class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{}; ==== NEW ==== way to define a TriMesh: // forward declarations class MyVertex; class MyEdge; class MyFace; // declaration of which types is used as VertexType, which type is used as FaceType and so on... class MyUsedTypes: public vcg::UsedType < vcg::Use<MyVertex>::AsVertexType, vcg::Use<MyFace>::AsFaceType>{}; class MyVertex: public Vertex < MyUsedTypes, vertex::Coord3f,...other components>{}; class MyFace: public Face < MyUsedTypes, face::VertexRef,...other components>{}; class MyMesh: public TriMesh<vector<MyVertex>,vector<MyFace> >{}; ===== classes introduced [vcg::UsedType] : it is a class containing all the types that must be passed to the definition of Vertex, Face, Edge... This class replaces the list of typenames to pass as first templates and the need to specify the maximal simplicial. So <MyVertex, MyEdge, MyFace becomes <MyUsedTypes< and VertexSimp2 becomes Vertex [vcg::Use] : an auxiliary class to give a simple way to specify the role of a type Note 2: the order of templates parameters to vcg::UsedTypes is unimportant, e.g: class MyUsedTypes: public vcg::UsedType <vcg::Use<MyVertex>::AsVertexType, vcg::Use<MyEdge>::AsEdgeType, vcg::Use<MyFace>::AsFaceType>{}; is the same as: class MyUsedTypes: public vcg::UsedType <vcg::Use<MyFace>::AsFaceType, vcg::Use<MyEdge>::AsEdgeType, vcg::Use<MyVertex>::AsVertexType>{}; Note 3: you only need to specify the type you use. If you do not have edges you do not need to include vcg::Use<MyEdge>::AsEdgeType in the template list of UsedTypes. ==== the Part II will be a tiny change to the class TriMesh it self.
2010-03-15 11:44:40 +01:00
struct MyUsedTypes : public UsedTypes< Use<MyVertex> ::AsVertexType,
Use<MyFace> ::AsFaceType>{};
class MyVertex : public Vertex< MyUsedTypes, vertex::Coord3f, vertex::BitFlags, vertex::Normal3f, vertex::Mark, vertex::Color4b >{};
class MyFace : public Face < MyUsedTypes, face::VertexRef,face::FFAdj, face::Mark, face::BitFlags, face::Normal3f> {};
2006-11-22 14:42:06 +01:00
2006-12-01 09:25:53 +01:00
class MyMesh : public tri::TriMesh< vector<MyVertex>, vector<MyFace > >{};
2007-01-15 13:46:25 +01:00
//Delaunay
2007-01-31 16:17:36 +01:00
class MyDelaunayFlip: public vcg::tri::TriEdgeFlip< MyMesh, MyDelaunayFlip > {
public:
typedef vcg::tri::TriEdgeFlip< MyMesh, MyDelaunayFlip > TEF;
inline MyDelaunayFlip( const TEF::PosType &p, int i) :TEF(p,i){}
};
2007-01-31 16:17:36 +01:00
2006-12-01 09:25:53 +01:00
bool callback(int percent, const char *str) {
cout << "str: " << str << " " << percent << "%\r";
2006-12-01 09:25:53 +01:00
return true;
}
2006-11-22 14:42:06 +01:00
2007-01-31 16:17:36 +01:00
template <class MESH>
bool NormalTest(typename face::Pos<typename MESH::FaceType> pos)
{
//giro intorno al vertice e controllo le normali
2008-09-29 12:11:21 +02:00
typename MESH::ScalarType thr = 0.0f;
typename MESH::CoordType NdP = vcg::Normal<typename MESH::FaceType>(*pos.f);
2008-09-29 12:11:21 +02:00
typename MESH::CoordType tmp, oop, soglia = typename MESH::CoordType(thr,thr,thr);
2007-01-31 16:17:36 +01:00
face::Pos<typename MESH::FaceType> aux=pos;
do{
aux.FlipF();
aux.FlipE();
oop = Abs(tmp - ::vcg::Normal<typename MESH::FaceType>(*pos.f));
2007-01-31 16:17:36 +01:00
if(oop < soglia )return false;
}while(aux != pos && !aux.IsBorder());
return true;
}
2006-11-22 14:42:06 +01:00
int main(int argc,char ** argv){
if(argc<5)
2006-11-22 14:42:06 +01:00
{
printf(
"\n HoleFilling ("__DATE__")\n"
"Visual Computing Group I.S.T.I. C.N.R.\n"
"Usage: trimesh_hole #algorithm #size filein.ply fileout.ply \n"
2006-11-22 14:42:06 +01:00
"#algorithm: \n"
" 1) Trivial Ear \n"
2007-01-31 12:46:12 +01:00
" 2) Minimum weight Ear \n"
2006-11-22 14:42:06 +01:00
" 3) Selfintersection Ear \n"
2006-11-29 16:17:39 +01:00
" 4) Minimum weight \n"
2006-11-22 14:42:06 +01:00
);
exit(0);
}
2006-11-22 15:38:16 +01:00
2006-11-22 14:42:06 +01:00
int algorithm = atoi(argv[1]);
int holeSize = atoi(argv[2]);
2006-11-22 14:42:06 +01:00
if(algorithm < 0 && algorithm > 4)
{
2006-11-29 16:17:39 +01:00
printf("Error in algorithm's selection\n",algorithm);
2006-11-22 14:42:06 +01:00
exit(0);
}
MyMesh m;
if(tri::io::ImporterPLY<MyMesh>::Open(m,argv[3])!=0)
2006-11-22 14:42:06 +01:00
{
printf("Error reading file %s\n",argv[2]);
exit(0);
}
//update the face-face topology
2006-12-01 09:25:53 +01:00
tri::UpdateTopology<MyMesh>::FaceFace(m);
tri::UpdateNormals<MyMesh>::PerVertexPerFace(m);
2006-11-22 14:42:06 +01:00
tri::UpdateFlags<MyMesh>::FaceBorderFromFF(m);
assert(tri::Clean<MyMesh>::IsFFAdjacencyConsistent(m));
2007-01-15 13:46:25 +01:00
2007-01-31 16:17:36 +01:00
//compute the average of face area
float AVG,sumA=0.0f;
int numA=0,indice;
indice = m.face.size();
MyMesh::FaceIterator fi;
for(fi=m.face.begin();fi!=m.face.end();++fi)
{
sumA += DoubleArea(*fi)/2;
numA++;
for(int ind =0;ind<3;++ind)
fi->V(ind)->InitIMark();
}
AVG=sumA/numA;
2006-11-22 14:42:06 +01:00
switch(algorithm)
{
case 1: tri::Hole<MyMesh>::EarCuttingFill<tri::TrivialEar<MyMesh> >(m,holeSize,false); break;
case 2: tri::Hole<MyMesh>::EarCuttingFill<tri::MinimumWeightEar< MyMesh> >(m,holeSize,false,callback); break;
case 3: tri::Hole<MyMesh>::EarCuttingIntersectionFill<tri::SelfIntersectionEar< MyMesh> >(m,holeSize,false); break;
2007-01-31 16:17:36 +01:00
case 4: tri::Hole<MyMesh>::MinimumWeightFill(m,holeSize, false); tri::UpdateTopology<MyMesh>::FaceFace(m); break;
2006-11-22 14:42:06 +01:00
}
2007-01-31 16:17:36 +01:00
tri::UpdateFlags<MyMesh>::FaceBorderFromFF(m);
assert(tri::Clean<MyMesh>::IsFFAdjacencyConsistent(m));
2007-01-31 16:17:36 +01:00
printf("\nStart refinig...\n");
/*start refining */
MyMesh::VertexIterator vi;
MyMesh::FaceIterator f;
std::vector<MyMesh::FacePointer> vf;
f = m.face.begin();
f += indice;
for(; f != m.face.end();++f)
{
if(!f->IsD())
{
f->SetS();
}
}
std::vector<MyMesh::FacePointer *> FPP;
std::vector<MyMesh::FacePointer> added;
std::vector<MyMesh::FacePointer>::iterator vfit;
int i=1;
printf("\n");
for(f = m.face.begin();f!=m.face.end();++f) if(!(*f).IsD())
{
if( f->IsS() )
{
f->V(0)->IsW();
f->V(1)->IsW();
f->V(2)->IsW();
}
else
{
f->V(0)->ClearW();
f->V(1)->ClearW();
f->V(2)->ClearW();
}
}
vcg::LocalOptimization<MyMesh> Fs(m);
Fs.SetTargetMetric(0.0f);
Fs.Init<MyDelaunayFlip >();
Fs.DoOptimization();
do
{
vf.clear();
f = m.face.begin();
f += indice;
for(; f != m.face.end();++f)
{
if(f->IsS())
{
bool test= true;
for(int ind =0;ind<3;++ind)
f->V(ind)->InitIMark();
test = (DoubleArea<MyMesh::FaceType>(*f)/2) > AVG;
if(test)
{
vf.push_back(&(*f));
}
}
}
//info print
printf("\r Raffino [%d] - > %d",i,vf.size());
i++;
FPP.clear();
added.clear();
for(vfit = vf.begin(); vfit!=vf.end();++vfit)
{
FPP.push_back(&(*vfit));
}
int toadd= vf.size();
MyMesh::FaceIterator f1,f2;
f2 = tri::Allocator<MyMesh>::AddFaces(m,(toadd*2),FPP);
MyMesh::VertexIterator vertp = tri::Allocator<MyMesh>::AddVertices(m,toadd);
std::vector<MyMesh::FacePointer> added;
added.reserve(toadd);
vfit=vf.begin();
for(int i = 0; i<toadd;++i,f2++,vertp++)
{
f1=f2;
f2++;
TriSplit<MyMesh,CenterPoint<MyMesh> >(vf[i],&(*f1),&(*f2),&(*vertp),CenterPoint<MyMesh>() );
f1->SetS();
f2->SetS();
for(int itr=0;itr<3;itr++)
{
f1->V(itr)->SetW();
f2->V(itr)->SetW();
}
added.push_back( &(*f1) );
added.push_back( &(*f2) );
}
vcg::LocalOptimization<MyMesh> FlippingSession(m);
FlippingSession.SetTargetMetric(0.0f);
FlippingSession.Init<MyDelaunayFlip >();
FlippingSession.DoOptimization();
}while(!vf.empty());
vcg::LocalOptimization<MyMesh> Fiss(m);
Fiss.SetTargetMetric(0.0f);
Fiss.Init<MyDelaunayFlip >();
Fiss.DoOptimization();
/*end refining */
tri::io::ExporterPLY<MyMesh>::Save(m,"PreSmooth.ply",false);
int UBIT = MyMesh::VertexType::LastBitFlag();
f = m.face.begin();
f += indice;
for(; f != m.face.end();++f)
{
if(f->IsS())
{
for(int ind =0;ind<3;++ind){
if(NormalTest<MyMesh>(face::Pos<MyMesh::FaceType>(&(*f),ind )))
{
f->V(ind)->SetUserBit(UBIT);
}
}
f->ClearS();
}
}
for(vi=m.vert.begin();vi!=m.vert.end();++vi) if(!(*vi).IsD())
{
if( vi->IsUserBit(UBIT) )
{
(*vi).SetS();
vi->ClearUserBit(UBIT);
}
}
2008-09-29 12:11:21 +02:00
tri::Smooth<MyMesh>::VertexCoordLaplacian(m,1,true);
2007-01-31 16:17:36 +01:00
printf("\nCompleted. Saving....\n");
2007-01-31 16:17:36 +01:00
tri::io::ExporterPLY<MyMesh>::Save(m,argv[4],false);
2006-11-22 14:42:06 +01:00
return 0;
}