Skip to content

Instantly share code, notes, and snippets.

@HaiyangXu
Created August 28, 2014 13:44
Show Gist options
  • Save HaiyangXu/bcabb294b12fda9a36c2 to your computer and use it in GitHub Desktop.
Save HaiyangXu/bcabb294b12fda9a36c2 to your computer and use it in GitHub Desktop.
Remove non-manifold edges Use VCG Library
#include <iostream>
#include <vector>
#include <vcg/complex/complex.h>
#include <wrap/io_trimesh/import.h>
#include <wrap/io_trimesh/export.h>
using namespace std;
// Forward declarations needed for creating the used types
class CVertexO;
class CEdgeO;
class CFaceO;
// Declaration of the semantic of the used types
class CUsedTypesO: public vcg::UsedTypes < vcg::Use<CVertexO>::AsVertexType,
vcg::Use<CEdgeO >::AsEdgeType,
vcg::Use<CFaceO >::AsFaceType >{};
// The Main Vertex Class
// Most of the attributes are optional and must be enabled before use.
// Each vertex needs 40 byte, on 32bit arch. and 44 byte on 64bit arch.
class CVertexO : public vcg::Vertex< CUsedTypesO,
vcg::vertex::InfoOcf, /* 4b */
vcg::vertex::Coord3f, /* 12b */
vcg::vertex::BitFlags, /* 4b */
vcg::vertex::Normal3f, /* 12b */
vcg::vertex::Qualityf, /* 4b */
vcg::vertex::Color4b, /* 4b */
vcg::vertex::VFAdjOcf, /* 0b */
vcg::vertex::MarkOcf, /* 0b */
vcg::vertex::TexCoordfOcf, /* 0b */
vcg::vertex::CurvaturefOcf, /* 0b */
vcg::vertex::CurvatureDirfOcf, /* 0b */
vcg::vertex::RadiusfOcf /* 0b */
>{
};
// The Main Edge Class
// Currently it does not contains anything.
class CEdgeO : public vcg::Edge<CUsedTypesO,
vcg::edge::BitFlags, /* 4b */
vcg::edge::EVAdj,
vcg::edge::EEAdj
>{
};
// Each face needs 32 byte, on 32bit arch. and 48 byte on 64bit arch.
class CFaceO : public vcg::Face< CUsedTypesO,
vcg::face::InfoOcf, /* 4b */
vcg::face::VertexRef, /*12b */
vcg::face::BitFlags, /* 4b */
vcg::face::Normal3f, /*12b */
vcg::face::QualityfOcf, /* 0b */
vcg::face::MarkOcf, /* 0b */
vcg::face::Color4bOcf, /* 0b */
vcg::face::FFAdjOcf, /* 0b */
vcg::face::VFAdjOcf, /* 0b */
vcg::face::WedgeTexCoordfOcf /* 0b */
> {};
class CMeshO : public vcg::tri::TriMesh< vcg::vertex::vector_ocf<CVertexO>, vcg::face::vector_ocf<CFaceO> >
{
public :
int sfn; //The number of selected faces.
int svn; //The number of selected vertices.
vcg::Matrix44f Tr; // Usually it is the identity. It is applied in rendering and filters can or cannot use it. (most of the filter will ignore this)
const vcg::Box3f &trBB()
{
static vcg::Box3f bb;
bb.SetNull();
bb.Add(Tr,bbox);
return bb;
}
};
int main(int argc, char **argv)
{
if(argc<3)
{
cout<<"Usage:\n\t RmNonManifold <mesh.ply> <out.ply>\n";
cout<<"This utility is to remove the non-manifold edges ";
return -1;
}
cout<<"Begin processing ..."<<endl;
CMeshO m;
int re=vcg::tri::io::ImporterPLY<CMeshO>::Open(m,argv[1]);
if(re!=0)
{
cerr<<vcg::tri::io::ImporterPLY<CMeshO>::ErrorMsg(re)<<endl;
return -1;
}
m.face.EnableFFAdjacency();
vcg::tri::UpdateTopology<CMeshO>::FaceFace(m);
vcg::tri::Clean<CMeshO>::CountNonManifoldEdgeFF(m,true);
CMeshO::FaceIterator fi;
CMeshO::VertexIterator vi;
vcg::tri::UpdateSelection<CMeshO>::FaceClear(m);
vcg::tri::UpdateSelection<CMeshO>::FaceFromVertexLoose(m);
for(fi=m.face.begin();fi!=m.face.end();++fi)
{
if(!(*fi).IsD() && (*fi).IsS() )
{
vcg::tri::Allocator<CMeshO>::DeleteFace(m,*fi);
}
}
for(vi=m.vert.begin();vi!=m.vert.end();++vi)
{
if(!(*vi).IsD() && (*vi).IsS() )
{
vcg::tri::Allocator<CMeshO>::DeleteVertex(m,*vi);
}
}
re=vcg::tri::io::ExporterPLY<CMeshO>::Save(m,argv[2],false);
if(re!=0)
{
cerr<<vcg::tri::io::ImporterPLY<CMeshO>::ErrorMsg(re)<<endl;
return -1;
}
cout << "Finished!" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment