voronoi_atlas.h
Go to the documentation of this file.
00001 #ifndef VORONOI_ATLAS_H
00002 #define VORONOI_ATLAS_H
00003 
00004 #include<vcg/complex/algorithms/parametrization/poisson_solver.h>
00005 #include<vcg/complex/algorithms/parametrization/uv_utils.h>
00006 #include<vcg/complex/algorithms/parametrization/distortion.h>
00007 #include<vcg/space/outline2_packer.h>
00008 #include<vcg/space/rasterized_outline2_packer.h>
00009 #include<vcg/complex/algorithms/update/texture.h>
00010 #include<vcg/complex/algorithms/point_sampling.h>
00011 #include<vcg/complex/algorithms/voronoi_processing.h>
00012 
00013 //#include<wrap/qt/outline2_rasterizer.h>
00014 
00015 namespace vcg {
00016 namespace tri {
00017 
00018 template <class MeshType>
00019 class VoronoiAtlas
00020 {
00021 //private:
00022 public:
00023   class VoroEdge;
00024   class VoroFace;
00025   class VoroVertex;
00026   struct VoroUsedTypes : public UsedTypes<      Use<VoroVertex>   ::template AsVertexType,
00027                                           Use<VoroEdge>     ::template AsEdgeType,
00028                                           Use<VoroFace>     ::template AsFaceType>{};
00029 
00030   class VoroVertex  : public Vertex< VoroUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::TexCoord2f, vertex::VFAdj , vertex::Qualityf, vertex::Color4b, vertex::BitFlags  >{};
00031   class VoroFace    : public Face<  VoroUsedTypes, face::VertexRef, face::BitFlags, face::FFAdj ,face::VFAdj , face::CurvatureDirf,face::WedgeTexCoord2f> {};
00032   class VoroEdge    : public Edge< VoroUsedTypes>{};
00033   class VoroMesh    : public tri::TriMesh< std::vector<VoroVertex>, std::vector<VoroFace> , std::vector<VoroEdge>  > {};
00034 
00035   typedef typename VoroMesh::FaceIterator FaceIterator;
00036   typedef typename VoroMesh::VertexType VertexType;
00037   typedef typename VoroMesh::FaceType FaceType;
00038 
00039   static void CollectUVBorder(VoroMesh *rm, std::vector<Point2f> &uvBorder)
00040   {
00041     tri::UpdateTopology<VoroMesh>::FaceFace(*rm);
00042     tri::UpdateFlags<VoroMesh>::FaceClearV(*rm);
00043     for(FaceIterator fi=rm->face.begin();fi!=rm->face.end();++fi)
00044     {
00045       for(int j=0;j<3;++j)
00046         if(face::IsBorder(*fi,j) && !(fi->IsV()))
00047         {
00048           face::Pos<FaceType> pp(&*fi,j,fi->V(j));
00049           assert(pp.IsBorder());
00050           face::Pos<FaceType> startPos = pp;
00051           do
00052           {
00053             uvBorder.push_back( pp.F()->WT(pp.VInd()).P() );
00054             pp.F()->SetV();
00055             pp.NextB();
00056           } while(pp != startPos);
00057         }
00058     }
00059   }
00060 
00061  // take a mesh and rescale its uv so that they are in the 0..1 range
00062  static void RegularizeTexArea(VoroMesh &m)
00063   {
00064     float areaTex=0;
00065     float areaGeo=0;
00066 
00067     vcg::Box2f UVBox = tri::UV_Utils<VoroMesh>::PerWedgeUVBox(m);
00068     for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
00069     {
00070       areaTex+= fabs((fi->WT(1).P() - fi->WT(0).P()) ^ (fi->WT(2).P() - fi->WT(0).P())) ;
00071       areaGeo+= DoubleArea(*fi);
00072     }
00073 
00074     float ratio = sqrt(areaGeo/areaTex);
00075 
00076     for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
00077     {
00078       for(int j=0;j<3;++j)
00079         fi->WT(j).P() = (fi->WT(j).P()-UVBox.min) *ratio;
00080     }
00081   }
00082 
00083 
00084 public:
00085  struct VoronoiAtlasParam
00086  {
00087    VoronoiAtlasParam()
00088    {
00089      maxIterNum = 5;
00090      sampleNum=10;
00091      overlap=false;
00092    }
00093 
00094    struct Stat
00095    {
00096      void clear() { iterNum=totalTime=unwrapTime=voronoiTime=samplingTime=0;}
00097      int totalTime;
00098      int unwrapTime;
00099      int voronoiTime;
00100      int samplingTime;
00101 
00102      int regionNum;
00103      int iterNum;
00104    };
00105 
00106    int sampleNum;
00107    bool overlap;
00108    Stat vas;
00109    int maxIterNum;
00110  };
00111 
00112  // Main parametrization function:
00113  // it takes a startMesh, copy it and
00114 
00115 
00116   static void Build( MeshType &startMesh, MeshType &paraMesh, VoronoiAtlasParam &pp)
00117   {
00118     pp.vas.clear();
00119    int t0=clock();
00120   VoroMesh m;  // the mesh used for the processing is a copy of the passed one.
00121   tri::Append<VoroMesh, MeshType>::Mesh(m, startMesh);
00122   tri::Clean<VoroMesh>::RemoveUnreferencedVertex(m);
00123   tri::Allocator<VoroMesh>::CompactVertexVector(m);
00124   tri::Allocator<VoroMesh>::CompactFaceVector(m);
00125 
00126   tri::UpdateBounding<VoroMesh>::Box(m);
00127   std::vector<VoroMesh *> meshRegionVec;
00128   std::vector< std::vector<Point2f> > uvBorders;
00129 
00130   // Main processing loop
00131   do
00132   {
00133 //    qDebug("************ ITERATION %i sampling mesh of %i with %i ************",pp.vas.iterNum,m.fn,pp.sampleNum);
00134     int st0=clock();
00135     std::vector<Point3f> PoissonSamples;
00136     float diskRadius=0;
00137     tri::PoissonSampling(m,PoissonSamples,pp.sampleNum,diskRadius);
00138     int st1=clock();
00139     pp.vas.samplingTime+= st1-st0;
00140 //    qDebug("Sampling created a new mesh of %lu points\n",PoissonSamples.size());
00141     EuclideanDistance<VoroMesh> edFunc;
00142     std::vector<VertexType *> seedVec;
00143     tri::VoronoiProcessing<VoroMesh>::SeedToVertexConversion(m,PoissonSamples,seedVec);
00144     tri::UpdateTopology<VoroMesh>::VertexFace(m);
00145     tri::VoronoiProcessing<VoroMesh>::ComputePerVertexSources(m,seedVec,edFunc);
00146     tri::VoronoiProcessing<VoroMesh>::FaceAssociateRegion(m);
00147     tri::VoronoiProcessing<VoroMesh>::VoronoiColoring(m,true);
00148     std::vector<VoroMesh *> badRegionVec;
00149     int st2=clock();
00150     pp.vas.voronoiTime+=st2-st1;
00151     for(size_t i=0; i<seedVec.size();++i)
00152     {
00153       VoroMesh *rm = new VoroMesh();
00154       int selCnt = tri::VoronoiProcessing<VoroMesh>::FaceSelectAssociateRegion(m,seedVec[i]);
00155       //qDebug("Region %i of %i faces",i,selCnt);
00156       if(selCnt==0) continue;
00157       assert(selCnt>0);
00158       if(pp.overlap){
00159       tri::UpdateSelection<VoroMesh>::VertexFromFaceLoose(m);
00160       tri::UpdateSelection<VoroMesh>::FaceFromVertexLoose(m);
00161       }
00162       tri::Append<VoroMesh,VoroMesh>::Mesh(*rm, m, true);
00163       int tp0=clock();
00164       tri::PoissonSolver<VoroMesh> PS(*rm);
00165       if(PS.IsFeaseable())
00166       {
00167         PS.Init();
00168         PS.FixDefaultVertices();
00169         PS.SolvePoisson(false);
00170         tri::UpdateTexture<VoroMesh>::WedgeTexFromVertexTex(*rm);
00171         RegularizeTexArea(*rm);
00172 
00173         std::vector<Point2f> uvBorder;
00174         CollectUVBorder(rm,uvBorder);
00175         meshRegionVec.push_back(rm);
00176         uvBorders.push_back(uvBorder);
00177         int foldedCnt = tri::Distortion<VoroMesh,false>::Folded(*rm);
00178         if( foldedCnt > rm->fn/10)
00179         {
00180           badRegionVec.push_back(rm);
00181 //          qDebug("-- region %i Parametrized but with %i fold on %i!",i,foldedCnt,rm->fn);
00182         }
00183 //        else qDebug("-- region %i Parametrized!",i);
00184 
00185       } else
00186       {
00187 //        qDebug("-- region %i is NOT homeomorphic to a disk\n",i);
00188         badRegionVec.push_back(rm);
00189       }
00190       int tp1=clock();
00191       pp.vas.unwrapTime +=tp1-tp0;
00192       ++pp.vas.iterNum;
00193     }
00194 //    qDebug("\n -- Completed (%i bad regions) -- \n", badRegionVec.size());
00195     VoroMesh *rm = new VoroMesh();
00196     tri::VoronoiProcessing<VoroMesh>::FaceSelectAssociateRegion(m,0);
00197     tri::Append<VoroMesh,VoroMesh>::Mesh(*rm, m, true);
00198 
00199     if(rm->fn>0)
00200     {
00201 //      qDebug("ACH - unreached faces %i fn\n",rm->fn);
00202       badRegionVec.push_back(rm);
00203     }
00204     m.Clear();
00205     pp.sampleNum = 10;
00206     if(!badRegionVec.empty())
00207     {
00208       for(size_t i=0;i<badRegionVec.size();++i)
00209         if(badRegionVec[i]->fn>50)
00210           tri::Append<VoroMesh,VoroMesh>::Mesh(m, *badRegionVec[i], false);
00211 
00212       tri::Clean<VoroMesh>::RemoveDuplicateFace(m);
00213       tri::Clean<VoroMesh>::RemoveUnreferencedVertex(m);
00214       tri::Allocator<VoroMesh>::CompactVertexVector(m);
00215       tri::Allocator<VoroMesh>::CompactFaceVector(m);
00216     }
00217   } while (m.fn>0);
00218 
00219   std::vector<Similarity2f> trVec;
00220   Point2f finalSize;
00221   //PolyPacker<float>::WritePolyVec(uvBorders,"borders.poly");
00222   PolyPacker<float>::PackAsObjectOrientedRect(uvBorders,Point2i(1024,1024),trVec,finalSize);
00223 //  RasterizedOutline2Packer<float,QtOutline2Rasterizer>::Parameters prp;
00224 //  RasterizedOutline2Packer<float,QtOutline2Rasterizer>::Pack(uvBorders,Point2i(1024,1024),trVec,prp);
00225   // loop again over all the patches
00226   pp.vas.regionNum=meshRegionVec.size();
00227   for(size_t i=0; i<meshRegionVec.size();++i)
00228   {
00229     VoroMesh *rm = meshRegionVec[i];
00230     for(FaceIterator fi=rm->face.begin();fi!=rm->face.end();++fi)
00231     {
00232       for(int j=0;j<3;++j)
00233       {
00234         Point2f pp(fi->WT(j).U(),fi->WT(j).V());
00235         Point2f newpp=trVec[i]*pp;
00236         fi->WT(j).U()=newpp[0]/1024.0f;
00237         fi->WT(j).V()=newpp[1]/1024.0f;
00238       }
00239     }
00240     tri::Append<MeshType,VoroMesh>::Mesh(paraMesh, *rm, false);
00241   }
00242   int t2=clock();
00243   pp.vas.totalTime=t2-t0;
00244 }
00245 }; //end
00246 
00247 
00248 } // end namespace vcg
00249 } // end namespace tri
00250 
00251 
00252 #endif // VORONOI_ATLAS_H


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:38:40