mFES - molecular Finite Element Solver  0.4
VCG.h
Go to the documentation of this file.
00001 #ifndef VCG_LIB
00002 
00003 #define VCG_LIB
00004 
00005 // VCG library
00006 #include <vcg/complex/complex.h>
00007 #include <vcg/complex/algorithms/update/topology.h>
00008 #include <vcg/complex/algorithms/update/normal.h>
00009 
00010 #include <vcg/complex/algorithms/clean.h>
00011 #include <vcg/complex/algorithms/smooth.h>
00012 
00013 // input output
00014 #include <wrap/io_trimesh/import.h>
00015 #include <wrap/io_trimesh/export_stl.h>
00016 
00017 
00018 using namespace vcg;
00019 
00020 
00021 class mFace;
00022 class mVertex;
00023 
00024 // Meshdeclaration
00025 struct mUsedTypes : public UsedTypes<   Use<mVertex>::AsVertexType, Use<mFace>::AsFaceType>{};
00026 class mVertex  : public Vertex< mUsedTypes, vertex::VFAdj, vertex::Coord3f, vertex::Normal3f, vertex::BitFlags  >{};
00027 //class mVertex  : public Vertex< mUsedTypes, vertex::Coord3f, vertex::BitFlags, vertex::Normal3f, vertex::Mark,vertex::Color4b, vertex::Qualityf,vertex::VFAdj, vertex::Curvaturef >{};
00028 
00029 class mFace    : public Face  < mUsedTypes, face::VFAdj, face::Normal3f, face::VertexRef, face::Mark, face::BitFlags, face::VertexRef > {};
00030 //class mFace    : public Face  < mUsedTypes, face::VertexRef,face::BitFlags,face::Mark, face::Normal3f, vcg::face::VFAdj, vcg::face::FFAdj>{};
00031 
00032 class mMesh    : public vcg::tri::TriMesh<vector<mVertex>, vector<mFace> > {};
00033 
00034 #include <vcg/space/point3.h>
00035 #include <vcg/complex/algorithms/clean.h>
00036 
00037 namespace vcg
00038 {
00039 template<class MeshType>
00040 std::pair<int,int>  RemoveSmallConnectedComponentsSize(MeshType &m, int maxCCSize)
00041 {
00042   std::vector< std::pair<int, typename MeshType::FacePointer> > CCV;
00043       int TotalCC=vcg::tri::Clean<MeshType>::ConnectedComponents(m, CCV);
00044       cout << "# connected components: " << TotalCC << endl;
00045                         int DeletedCC=0;
00046 
00047       tri::ConnectedIterator<MeshType> ci;
00048       for(unsigned int i=0;i<CCV.size();++i)
00049       {
00050         std::vector<typename MeshType::FacePointer> FPV;
00051         if(CCV[i].first<maxCCSize)
00052         {
00053                                         DeletedCC++;
00054           for(ci.start(m,CCV[i].second);!ci.completed();++ci)
00055             FPV.push_back(*ci);
00056 
00057           typename std::vector<typename MeshType::FacePointer>::iterator fpvi;
00058           for(fpvi=FPV.begin(); fpvi!=FPV.end(); ++fpvi)
00059                                                 tri::Allocator<MeshType>::DeleteFace(m,(**fpvi));
00060         }
00061       }
00062                         return std::pair<int,int>(TotalCC,DeletedCC);
00063 }
00064 }
00065 
00066 
00067 
00068 namespace vcg {
00069 namespace tri {
00070 
00071 template<class _MeshType>
00072 class SmallComponent
00073 {
00074 
00075 public:
00076         typedef _MeshType MeshType;
00077         typedef typename MeshType::VertexType     VertexType;
00078         typedef typename MeshType::VertexPointer  VertexPointer;
00079         typedef typename MeshType::VertexIterator VertexIterator;
00080         typedef typename MeshType::FaceType       FaceType;
00081         typedef typename MeshType::FacePointer    FacePointer;
00082         typedef typename MeshType::FaceIterator   FaceIterator;
00083 
00084         static int Select(MeshType &m, float nbFaceRatio = 0.1, bool nonClosedOnly = false)
00085         {
00086                 assert(tri::HasFFAdjacency(m) && "The small component selection procedure requires face to face adjacency.");
00087 
00088                 // the different components as a list of face pointer
00089                 std::vector< std::vector<FacePointer> > components;
00090 
00091                 for(uint faceSeed = 0; faceSeed<m.face.size(); )
00092                 {
00093                         // find the first not selected face
00094                         bool foundSeed = false;
00095                         while (faceSeed<m.face.size())
00096                         {
00097                                 mFace& f = m.face[faceSeed];
00098                                 if (!f.IsS())
00099                                 {
00100                                         if (nonClosedOnly)
00101                                         {
00102                                                 for (int k=0; k<3; ++k)
00103                                                         if (face::IsBorder(f,k))
00104                                                         {
00105                                                                 foundSeed = true;
00106                                                                 break;
00107                                                         }
00108                                         }
00109                                         else
00110                                                 foundSeed = true;
00111                                         if (foundSeed)
00112                                                 break;
00113                                 }
00114                                 ++faceSeed;
00115                         }
00116                         if (!foundSeed) // no more seed, stop
00117                                 break;
00118 
00119                         // expand the region from this face...
00120                         components.resize(components.size()+1);
00121                         std::vector<FacePointer> activefaces;
00122 
00123                         activefaces.push_back(&m.face[faceSeed]);
00124 
00125                         while (!activefaces.empty())
00126                         {
00127                                 FacePointer f = activefaces.back();
00128                                 activefaces.pop_back();
00129                                 if (f->IsS())
00130                                         continue;
00131 
00132                                 f->SetS();
00133                                 components.back().push_back(f);
00134 
00135                                 for (int k=0; k<3; ++k)
00136                                 {
00137                                         if (face::IsBorder(*f,k))
00138                                                 continue;
00139 
00140                                         FacePointer of = f->FFp(k);
00141                                         if (!of->IsS()){
00142                                                 activefaces.push_back(of);
00143                                         }
00144                                 }
00145                         }
00146                         ++faceSeed;
00147                 }
00148                 cout << "update selection" << endl;
00149                 UpdateSelection<MeshType>::FaceClear(m);
00150 
00151                 // now the segmentation is done, let's compute the absolute face count threshold
00152                 int total_selected = 0;
00153                 int maxComponent = 0;
00154                 for (uint i=0; i<components.size(); ++i)
00155                 {
00156 //                      cout << "Component " << i << " -> " << components[i].size() << "\n";
00157                         total_selected += components[i].size();
00158                         maxComponent = std::max<int>(maxComponent,components[i].size());
00159                 }
00160                 int remaining = m.face.size() - total_selected;
00161                 uint th = std::max(maxComponent,remaining) * nbFaceRatio;
00162 
00163                 int selCount = 0;
00164                 for (uint i=0; i<components.size(); ++i)
00165                 {
00166                         if (components[i].size()<th)
00167                         {
00168                                 selCount += components[i].size();
00169                                 for (uint j=0; j<components[i].size(); ++j)
00170                                         components[i][j]->SetS();
00171                         }
00172                 }
00173                 cout << selCount << endl;
00174                 return selCount;
00175         }
00176 
00177         static void DeleteFaceVert(MeshType &m)
00178         {
00179                 typename MeshType::FaceIterator fi;
00180                 typename MeshType::VertexIterator vi;
00181                 UpdateSelection<MeshType>::VertexClear(m);
00182                 UpdateSelection<MeshType>::VertexFromFaceStrict(m);
00183 
00184                 for(fi=m.face.begin();fi!=m.face.end();++fi)
00185                         if (!(*fi).IsD() && (*fi).IsS() )
00186                                 tri::Allocator<mMesh>::DeleteFace(m,*fi);
00187                 for(vi=m.vert.begin();vi!=m.vert.end();++vi)
00188                         if (!(*vi).IsD() && (*vi).IsS() )
00189                                 tri::Allocator<mMesh>::DeleteVertex(m,*vi);
00190         }
00191 
00192 }; // end class
00193 
00194 }       // End namespace
00195 }       // End namespace
00196 
00197 #endif