File: voronoi_atlas.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,124 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 29
file content (275 lines) | stat: -rw-r--r-- 10,093 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#ifndef VORONOI_ATLAS_H
#define VORONOI_ATLAS_H

#include<vcg/complex/algorithms/parametrization/poisson_solver.h>
#include<vcg/complex/algorithms/parametrization/uv_utils.h>
#include<vcg/complex/algorithms/parametrization/distortion.h>
#include<vcg/space/outline2_packer.h>
#include<vcg/space/rasterized_outline2_packer.h>
#include<vcg/complex/algorithms/update/texture.h>
#include<vcg/complex/algorithms/point_sampling.h>
#include<vcg/complex/algorithms/voronoi_processing.h>

//#include<wrap/qt/outline2_rasterizer.h>

namespace vcg {
namespace tri {

template <class MeshType>
class VoronoiAtlas
{
//private:
public:
  class VoroEdge;
  class VoroFace;
  class VoroVertex;
  struct VoroUsedTypes : public UsedTypes<	Use<VoroVertex>   ::template AsVertexType,
                                          Use<VoroEdge>     ::template AsEdgeType,
                                          Use<VoroFace>     ::template AsFaceType>{};

  class VoroVertex  : public Vertex< VoroUsedTypes, vertex::Coord3f, vertex::Normal3f, vertex::TexCoord2f, vertex::VFAdj , vertex::Qualityf, vertex::Color4b, vertex::BitFlags  >{};
  class VoroFace    : public Face<  VoroUsedTypes, face::VertexRef, face::BitFlags, face::FFAdj ,face::VFAdj , face::CurvatureDirf,face::WedgeTexCoord2f> {};
  class VoroEdge    : public Edge< VoroUsedTypes>{};
  class VoroMesh    : public tri::TriMesh< std::vector<VoroVertex>, std::vector<VoroFace> , std::vector<VoroEdge>  > {};

  typedef typename VoroMesh::FaceIterator FaceIterator;
  typedef typename VoroMesh::VertexType VertexType;
  typedef typename VoroMesh::FaceType FaceType;

  static void CollectUVBorder(VoroMesh *rm, std::vector<Point2f> &uvBorder)
  {
    tri::UpdateTopology<VoroMesh>::FaceFace(*rm);
    tri::UpdateFlags<VoroMesh>::FaceClearV(*rm);
    for(FaceIterator fi=rm->face.begin();fi!=rm->face.end();++fi)
    {
      for(int j=0;j<3;++j)
        if(face::IsBorder(*fi,j) && !(fi->IsV()))
        {
          face::Pos<FaceType> pp(&*fi,j,fi->V(j));
          assert(pp.IsBorder());
          face::Pos<FaceType> startPos = pp;
          do
          {
            uvBorder.push_back( pp.F()->WT(pp.VInd()).P() );
            pp.F()->SetV();
            pp.NextB();
          } while(pp != startPos);
        }
    }
  }

 // take a mesh and rescale its uv so that they are in the 0..1 range
 static void RegularizeTexArea(VoroMesh &m)
  {
    float areaTex=0;
    float areaGeo=0;

    vcg::Box2f UVBox = tri::UV_Utils<VoroMesh>::PerWedgeUVBox(m);
    for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
    {
      areaTex+= fabs((fi->WT(1).P() - fi->WT(0).P()) ^ (fi->WT(2).P() - fi->WT(0).P())) ;
      areaGeo+= DoubleArea(*fi);
    }

    float ratio = sqrt(areaGeo/areaTex);

    for(FaceIterator fi=m.face.begin();fi!=m.face.end();++fi)
    {
      for(int j=0;j<3;++j)
        fi->WT(j).P() = (fi->WT(j).P()-UVBox.min) *ratio;
    }
  }


public:
 struct VoronoiAtlasParam
 {
   VoronoiAtlasParam()
   {
     maxIterNum = 5;
     sampleNum=10;
     overlap=false;
   }

   struct Stat
   {
     void clear() { iterNum=totalTime=unwrapTime=voronoiTime=samplingTime=0;}
     int totalTime;
     int unwrapTime;
     int voronoiTime;
     int samplingTime;

     int regionNum;
     int iterNum;
   };

   int sampleNum;
   bool overlap;
   Stat vas;
   int maxIterNum;
   CallBackPos *cb=vcg::CErrCallBackPos;
 };

 // Main parametrization function:
 // it takes a startMesh, copy it and

  static void Build( MeshType &startMesh, MeshType &paraMesh, VoronoiAtlasParam &pp)
  {
    pp.vas.clear();
   int t0=clock();
  VoroMesh m;  // the mesh used for the processing is a copy of the passed one.
  tri::Append<VoroMesh, MeshType>::Mesh(m, startMesh);
  tri::Clean<VoroMesh>::RemoveUnreferencedVertex(m);
  tri::Allocator<VoroMesh>::CompactVertexVector(m);
  tri::Allocator<VoroMesh>::CompactFaceVector(m);

  tri::UpdateBounding<VoroMesh>::Box(m);
  std::vector<VoroMesh *> meshRegionVec;
  std::vector< std::vector<Point2f> > uvBorders;

  // Main processing loop
  do
  {
//    qDebug("************ ITERATION %i sampling mesh of %i with %i ************",pp.vas.iterNum,m.fn,pp.sampleNum);
    int st0=clock();
    std::vector<Point3f> PoissonSamples;
    float diskRadius=0;
    tri::PoissonSampling(m,PoissonSamples,pp.sampleNum,diskRadius);
    int st1=clock();
    pp.vas.samplingTime+= st1-st0;
    pp.cb(50,StrFormat("Sampling created a new mesh of %lu points\n",PoissonSamples.size()));
    EuclideanDistance<VoroMesh> edFunc;
    std::vector<VertexType *> seedVec;
    tri::VoronoiProcessing<VoroMesh>::SeedToVertexConversion(m,PoissonSamples,seedVec);
    tri::UpdateTopology<VoroMesh>::VertexFace(m);
    tri::VoronoiProcessing<VoroMesh>::ComputePerVertexSources(m,seedVec,edFunc);
    tri::VoronoiProcessing<VoroMesh>::FaceAssociateRegion(m);
    tri::VoronoiProcessing<VoroMesh>::VoronoiColoring(m,true);
    std::vector<VoroMesh *> badRegionVec;
    int st2=clock();
    pp.vas.voronoiTime+=st2-st1;
    for(size_t i=0; i<seedVec.size();++i)
    {
      VoroMesh *rm = new VoroMesh();
      int selCnt = tri::VoronoiProcessing<VoroMesh>::FaceSelectAssociateRegion(m,seedVec[i]);
       pp.cb(50,StrFormat("Region %i of %i faces",i,selCnt));
      if(selCnt==0) continue;
      assert(selCnt>0);
      if(pp.overlap){
      tri::UpdateSelection<VoroMesh>::VertexFromFaceLoose(m);
      tri::UpdateSelection<VoroMesh>::FaceFromVertexLoose(m);
      }
      tri::Append<VoroMesh,VoroMesh>::Mesh(*rm, m, true);
      int tp0=clock();
      tri::PoissonSolver<VoroMesh> PS(*rm);
      tri::UpdateBounding<VoroMesh>::Box(*rm);
      if(PS.IsFeasible())
      {
        PS.Init();
        PS.FixDefaultVertices();
        PS.SolvePoisson(false);
        tri::UpdateTexture<VoroMesh>::WedgeTexFromVertexTex(*rm);
        RegularizeTexArea(*rm);

        std::vector<Point2f> uvBorder;
        CollectUVBorder(rm,uvBorder);
        meshRegionVec.push_back(rm);
        uvBorders.push_back(uvBorder);
        int foldedCnt = tri::Distortion<VoroMesh,false>::FoldedNum(*rm);
        if( foldedCnt > rm->fn/10)
        {
          badRegionVec.push_back(rm);
//          qDebug("-- region %i Parametrized but with %i fold on %i!",i,foldedCnt,rm->fn);
        }
//        else qDebug("-- region %i Parametrized!",i);

      } else
      {
//        qDebug("-- region %i is NOT homeomorphic to a disk\n",i);
        badRegionVec.push_back(rm);
      }
      int tp1=clock();
      pp.vas.unwrapTime +=tp1-tp0;
      ++pp.vas.iterNum;
    }
//    qDebug("\n -- Completed (%i bad regions) -- \n", badRegionVec.size());
    VoroMesh *rm = new VoroMesh();
    tri::VoronoiProcessing<VoroMesh>::FaceSelectAssociateRegion(m,0);
    tri::Append<VoroMesh,VoroMesh>::Mesh(*rm, m, true);

    if(rm->fn>0)
    {
//      qDebug("ACH - unreached faces %i fn\n",rm->fn);
      badRegionVec.push_back(rm);
    }
    m.Clear();
    pp.sampleNum = 10;
    if(!badRegionVec.empty())
    {
      for(size_t i=0;i<badRegionVec.size();++i)
        if(badRegionVec[i]->fn>50)
          tri::Append<VoroMesh,VoroMesh>::Mesh(m, *badRegionVec[i], false);

      tri::Clean<VoroMesh>::RemoveDuplicateFace(m);
      tri::Clean<VoroMesh>::RemoveUnreferencedVertex(m);
      tri::Allocator<VoroMesh>::CompactVertexVector(m);
      tri::Allocator<VoroMesh>::CompactFaceVector(m);
    }
  } while (m.fn>0);

  std::vector<Similarity2f> trVec;
  Point2f finalSize;
  //PolyPacker<float>::WritePolyVec(uvBorders,"borders.poly");
  PolyPacker<float>::PackAsObjectOrientedRect(uvBorders,Point2i(1024,1024),trVec,finalSize);
//  RasterizedOutline2Packer<float,QtOutline2Rasterizer>::Parameters prp;
//  RasterizedOutline2Packer<float,QtOutline2Rasterizer>::Pack(uvBorders,Point2i(1024,1024),trVec,prp);
  // loop again over all the patches
  pp.vas.regionNum=meshRegionVec.size();
  for(size_t i=0; i<meshRegionVec.size();++i)
  {
    VoroMesh *rm = meshRegionVec[i];
    for(FaceIterator fi=rm->face.begin();fi!=rm->face.end();++fi)
    {
      for(int j=0;j<3;++j)
      {
        Point2f pp(fi->WT(j).U(),fi->WT(j).V());
        Point2f newpp=trVec[i]*pp;
        fi->WT(j).U()=newpp[0]/1024.0f;
        fi->WT(j).V()=newpp[1]/1024.0f;
      }
    }
    tri::Append<MeshType,VoroMesh>::Mesh(paraMesh, *rm, false);
  }
  int t2=clock();
  pp.vas.totalTime=t2-t0;
}
}; //end


} // end namespace vcg
} // end namespace tri


#endif // VORONOI_ATLAS_H