File: simplemeshprovider.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (233 lines) | stat: -rw-r--r-- 6,166 bytes parent folder | download | duplicates (2)
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
#ifndef SIMPLEMESHPROVIDER_H
#define SIMPLEMESHPROVIDER_H
#include <wrap/io_trimesh/alnParser.h>
#include <list>
#include <vector>
#include <vcg/space/box3.h>
#include <wrap/ply/plystuff.h>
#include <wrap/io_trimesh/import.h>

/*
 * A mesh provider class has the simpler role of passing the set of meshes to be merged to the surface reconstrcution algorithm. 
 * The only reason for this abstraction is that, plymc can work in a out-of-core way and the loading of the needed range maps can be optimized with a high level caching system. 
 */
template<class TriMeshType>
class MinimalMeshProvider
{
private:
 
  std::vector< std::string > nameVec;  
  std::vector< TriMeshType * > meshVec;
  std::vector<vcg::Matrix44f> trVec;
  std::vector<float> weightVec;		        // weight tot be applied to each mesh.
  vcg::Box3f fullBBox;

public:
  bool Find(const std::string &name,  TriMeshType * &sm) 
  {
    for(int i=0;i<nameVec.size();++i) 
      if(nameVec[i]==name) {
        sm=meshVec[i]; 
        return true;
      } 
    sm=0; return false;
  }
  
};

/**
 * Cache based Loading of  meshes to avoid reloading an processing of the same mesh multiple times. 
 * 
 */

namespace vcg {

template<class TriMeshType>
class MeshCache
{
  class Pair
  {
  public:
    Pair(){used=0;}
    TriMeshType *M;
    std::string Name;
    int used; // 'data' dell'ultimo accesso. si butta fuori quello lru
  };
  
  std::list<Pair> MV;
  
public:
  void clear();
  
  MeshCache() {MeshCacheSize=6;}
  ~MeshCache() {
    typename std::list<Pair>::iterator mi;
    for(mi=MV.begin();mi!=MV.end();++mi)
      delete (*mi).M;
  }
  

  /**
   * @brief Find load a mesh form the cache if it is in or from the disk otherwise
   * @param name what mesh to find
   * @param sm the pointer loaded mesh
   * @return true if the mesh was already in cache
   * 
   */  
  bool Find(const std::string &name,  TriMeshType * &sm)
  {
    typename std::list<Pair>::iterator mi;
    typename std::list<Pair>::iterator oldest; // quello che e' piu' tempo che non viene acceduto.
    int last;
    
    last = std::numeric_limits<int>::max();
    oldest = MV.begin();
    
    for(mi=MV.begin();mi!=MV.end();++mi)
    {
      if((*mi).used<last)
      {
        last=(*mi).used;
        oldest=mi;
      }
      if((*mi).Name==name) {
        sm=(*mi).M;
        (*mi).used++;
        return true;
      }
    }
    
    // we have not found the requested mesh
    // either allocate a new mesh or give back a previous mesh.
    
    if(MV.size()>MeshCacheSize)	{
      sm=(*oldest).M;
      (*oldest).used=0;
      (*oldest).Name=name;
    }	else	{
      MV.push_back(Pair());
      MV.back().Name=name;
      MV.back().M=new TriMeshType();
      sm=MV.back().M;
    }
    return false;
  }
  
  
  size_t MeshCacheSize;
  size_t size() const {return MV.size();}
};

template<class TriMeshType>
class SimpleMeshProvider
{
private:
  std::vector< std::string > meshnames;
  std::vector<vcg::Matrix44f> TrV;
  std::vector<float> WV;		        // weight tot be applied to each mesh.
  std::vector<vcg::Box3f> BBV;	    // bbox of the transformed meshes..
  vcg::Box3f fullBBox;
  MeshCache<TriMeshType> MC;
  
public:
  
  int size() {return meshnames.size();}
  
  int getCacheSize() {return MC.MeshCacheSize;}
  int setCacheSize(size_t newsize)
  {
    if(newsize == MC.MeshCacheSize)
      return MC.MeshCacheSize;
    if(newsize <= 0)
      return MC.MeshCacheSize;
    
    MC.MeshCacheSize = newsize;
    return newsize;
  }
  
  bool openALN (const char* alnName)
  {
    vector<RangeMap> rmaps;
    ALNParser::ParseALN(rmaps, alnName);
    
    for(size_t i=0; i<rmaps.size(); i++)
      AddSingleMesh(rmaps[i].filename.c_str(), rmaps[i].trasformation, rmaps[i].quality);
    
    return true;
  }
  
  bool AddSingleMesh(const char* meshName, const Matrix44f &tr= Matrix44f::Identity(), float meshWeight=1)
  {
    assert(WV.size()==meshnames.size() && TrV.size() == WV.size());
    TrV.push_back(tr);
    meshnames.push_back(meshName);
    WV.push_back(meshWeight);
    BBV.push_back(Box3f());
    return true;
  }
    
  vcg::Box3f bb(int i) {return BBV[i];}
  vcg::Box3f fullBB(){ return fullBBox;}
  vcg::Matrix44f Tr(int i) const  {return TrV[i];}
  std::string MeshName(int i) const {return meshnames[i];}
  float W(int i) const {return WV[i];}
  
  void Clear()
  {
    meshnames.clear();
    TrV.clear();
    WV.clear();
    BBV.clear();
    fullBBox.SetNull();
    MC.clear();
  }
  
  bool Find(int i, TriMeshType * &sm)
  {
    return MC.Find(meshnames[i],sm);
  }
  
  bool InitBBox()
  {
    fullBBox.SetNull();    
    for(int i=0;i<int(meshnames.size());++i)
    {
      bool ret;      
      printf("bbox scanning %4i/%i [%16s]      \r",i+1,(int)meshnames.size(), meshnames[i].c_str());
      if(tri::io::Importer<TriMeshType>::FileExtension(meshnames[i],"PLY") || tri::io::Importer<TriMeshType>::FileExtension(meshnames[i],"ply"))
      {
        ret=ply::ScanBBox(meshnames[i].c_str(),BBV[i],TrV[i],true,0);
      }
      else
      {
        printf("Trying to import a non-ply file %s\n",meshnames[i].c_str());fflush(stdout);
        TriMeshType m;
        ret = (tri::io::Importer<TriMeshType>::Open(m,meshnames[i].c_str()) == tri::io::Importer<TriMeshType>::E_NOERROR);
        tri::UpdatePosition<TriMeshType>::Matrix(m,TrV[i]);
        tri::UpdateBounding<TriMeshType>::Box(m);
        BBV[i].Import(m.bbox);
      }
      if( ! ret)
      {
        printf("\n\nwarning:\n file '%s' not found\n",meshnames[i].c_str());fflush(stdout);
        continue;
      }
      fullBBox.Add(BBV[i]);
    }
    return true;
  }
  
};

class SVertex;
class SFace;
class SUsedTypes: public vcg::UsedTypes < vcg::Use<SVertex>::AsVertexType,
    vcg::Use<SFace  >::AsFaceType >{};

class SVertex     : public Vertex< SUsedTypes, vertex::Coord3f, vertex::Normal3f,vertex::VFAdj, vertex::BitFlags, vertex::Color4b, vertex::Qualityf>{};
class SFace       : public Face< SUsedTypes, face::VertexRef, face::Normal3f,face::Qualityf, face::VFAdj, face::BitFlags> {};
class SMesh       : public tri::TriMesh< std::vector< SVertex>, std::vector< SFace > > {};

}

#endif // SIMPLEMESHPROVIDER_H