Skip to content

Commit

Permalink
Small refactoring of import_ply
Browse files Browse the repository at this point in the history
mostly to improve readability.
Factored out the legacy functions for reading tristrips and rangemaps.
  • Loading branch information
cignoni committed Aug 23, 2024
1 parent 66d10e3 commit 2c835e4
Showing 1 changed file with 111 additions and 103 deletions.
214 changes: 111 additions & 103 deletions wrap/io_trimesh/import_ply.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,11 @@ class ImporterPLY
static int Open( OpenMeshType &m, const char * filename, PlyInfo &pi )
{
assert(filename!=0);
std::vector<VertexPointer> index;
std::vector<VertexPointer> index; // vector of vertex pointers filled at the end of the vertex loading.
LoadPly_FaceAux<ScalarType> fa;
LoadPly_EdgeAux ea;
LoadPly_TristripAux tsa;
LoadPly_VertAux<ScalarType> va;

LoadPly_RangeGridAux rga;
std::vector<int> RangeGridAuxVec;
int RangeGridCols=0;
int RangeGridRows=0;


pi.mask = 0;
bool hasIntensity = false; // the intensity is a strange way to code single channel color used sometimes in rangemap. it is a kind of color. so it do not need another entry in the IOM mask.
bool multit = false; // true if texture has a per face int spec the texture index
Expand Down Expand Up @@ -764,17 +757,16 @@ class ImporterPLY
++ei;
}
}
else if( !strcmp( pf.ElemName(i),"face") && (n>0) )/******************** FACE READING ****************************************/
/******************** FACE READING LOOP ****************************************/
else if( !strcmp( pf.ElemName(i),"face") && (n>0) )
{
int j;

FaceIterator fi=Allocator<OpenMeshType>::AddFaces(m,n);
pf.SetCurElement(i);

for(j=0;j<n;++j)
for(j=0;j<n;++j) // main reading loop
{
int k;

if(pi.cb && (j%1000)==0) pi.cb(50+j*50/n,"Face Loading");
fa.a = 255;
if( pf.Read(&fa)==-1 )
Expand Down Expand Up @@ -850,7 +842,7 @@ class ImporterPLY

if (HasPolyInfo(m))
{
for(k=0; k<fa.size; ++k)
for(int k=0; k<fa.size; ++k)
{
if( fa.v[k]<0 || fa.v[k]>=m.vn )
{
Expand All @@ -865,7 +857,7 @@ class ImporterPLY

/// Now the temporary struct 'fa' is ready to be copied into the real face '*fi'
/// This loop
for(k=0;k<3;++k)
for(int k=0;k<3;++k)
{
if( fa.v[k]<0 || fa.v[k]>=m.vn )
{
Expand Down Expand Up @@ -900,7 +892,7 @@ class ImporterPLY
if(HasPolyInfo(m)) (*fi).Alloc(3);

(*fi).V(0) = index[ fa.v[0] ];
for(k=1;k<3;++k)
for(int k=1;k<3;++k)
{
if( fa.v[2+qq]<0 || fa.v[2+qq]>=m.vn )
{
Expand All @@ -926,94 +918,16 @@ class ImporterPLY
}

}
}else if( !strcmp( pf.ElemName(i),"tristrips") )//////////////////// LETTURA TRISTRIP DI STANFORD
{
int j;
pf.SetCurElement(i);
int numvert_tmp = (int)m.vert.size();
for(j=0;j<n;++j)
{
int k;
if(pi.cb && (j%1000)==0) pi.cb(50+j*50/n,"Tristrip Face Loading");
if( pf.Read(&tsa)==-1 )
{
pi.status = PlyInfo::E_SHORTFILE;
return pi.status;
}
int remainder=0;
for(k=0;k<tsa.size-2;++k)
{
if(pi.cb && (k%1000)==0) pi.cb(50+k*50/tsa.size,"Tristrip Face Loading");
if(tsa.v[k]<0 || tsa.v[k]>=numvert_tmp ) {
pi.status = PlyInfo::E_BAD_VERT_INDEX;
return pi.status;
}
if(tsa.v[k+2]==-1)
{
k+=2;
if(k%2) remainder=0;
else remainder=1;
continue;
}
Allocator<OpenMeshType>::AddFaces(m,1);
FaceType &tf =m.face.back();
tf.V(0) = index[ tsa.v[k+0] ];
tf.V(1) = index[ tsa.v[k+1] ];
tf.V(2) = index[ tsa.v[k+2] ];
if((k+remainder)%2) std::swap (tf.V(0), tf.V(1) );
}
}
}
else if( !strcmp( pf.ElemName(i),"range_grid") )//////////////////// LETTURA RANGEMAP DI STANFORD
{
//qDebug("Starting Reading of Range Grid");
if(RangeGridCols==0) // not initialized.
{
for(size_t co=0;co< pf.comments.size();++co)
{
std::string num_cols = "num_cols";
std::string num_rows = "num_rows";
std::string &c = pf.comments[co];
std::string bufstr,bufclean;
if( num_cols == c.substr(0,num_cols.length()) )
{
bufstr = c.substr(num_cols.length()+1);
RangeGridCols = atoi(bufstr.c_str());
}
if( num_rows == c.substr(0,num_rows.length()) )
{
bufstr = c.substr(num_rows.length()+1);
RangeGridRows = atoi(bufstr.c_str());
}
}
//qDebug("Rows %i Cols %i",RangeGridRows,RangeGridCols);
}
int totPnt = RangeGridCols*RangeGridRows;
// standard reading;
pf.SetCurElement(i);
for(int j=0;j<totPnt;++j)
{
if(pi.cb && (j%1000)==0) pi.cb(50+j*50/totPnt,"RangeMap Face Loading");
if( pf.Read(&rga)==-1 )
{
//qDebug("Error after loading %i elements",j);
pi.status = PlyInfo::E_SHORTFILE;
return pi.status;
}
else
{
if(rga.num_pts == 0)
RangeGridAuxVec.push_back(-1);
else
RangeGridAuxVec.push_back(rga.pts[0]);
}
}
//qDebug("Completed the reading of %i indexes",RangeGridAuxVec.size());
tri::SparseFaceGrid(m, RangeGridAuxVec, RangeGridCols,RangeGridRows);
}
}else if( !strcmp( pf.ElemName(i),"tristrips") )//////////////////// Reading TRISTRIP (rarely used, found in old STANFORD PLY)
{
if(! PlyParseStanfordTriStrips(pf, i, n, m, index, pi) ) return pi.status;
}
else if( !strcmp( pf.ElemName(i),"range_grid") )//////////////////// Reading RANGEMAP (rarely used, found in old STANFORD PLY)
{
if(! PlyParseStanfordRangeMaps(pf, i, m, pi) ) return pi.status;
}
else
{
// Skippaggio elementi non gestiti
int n = pf.ElemNumber(i);
pf.SetCurElement(i);

Expand Down Expand Up @@ -1083,7 +997,7 @@ class ImporterPLY
}


// Caricamento camera da un ply
// Loading only a Camera from PLY
int LoadCamera(const char * filename)
{
vcg::ply::PlyFile pf;
Expand Down Expand Up @@ -1228,7 +1142,101 @@ class ImporterPLY

return true;
}

static bool PlyParseStanfordTriStrips( vcg::ply::PlyFile &pf, const int i, const int n, OpenMeshType &m, const std::vector<VertexPointer> &index, PlyInfo &pi)
{
LoadPly_TristripAux tsa;
pf.SetCurElement(i);
int numvert_tmp = (int)m.vert.size();
for(int j=0;j<n;++j)
{
int k;
if(pi.cb && (j%1000)==0) pi.cb(50+j*50/n,"Tristrip Face Loading");
if( pf.Read(&tsa)==-1 )
{
pi.status = PlyInfo::E_SHORTFILE;
return false;
}
int remainder=0;
for(k=0;k<tsa.size-2;++k)
{
if(pi.cb && (k%1000)==0) pi.cb(50+k*50/tsa.size,"Tristrip Face Loading");
if(tsa.v[k]<0 || tsa.v[k]>=numvert_tmp ) {
pi.status = PlyInfo::E_BAD_VERT_INDEX;
return false;
}
if(tsa.v[k+2]==-1)
{
k+=2;
if(k%2) remainder=0;
else remainder=1;
continue;
}
Allocator<OpenMeshType>::AddFaces(m,1);
FaceType &tf =m.face.back();
tf.V(0) = index[ tsa.v[k+0] ];
tf.V(1) = index[ tsa.v[k+1] ];
tf.V(2) = index[ tsa.v[k+2] ];
if((k+remainder)%2) std::swap (tf.V(0), tf.V(1) );
}
}
return true;
}

static bool PlyParseStanfordRangeMaps( vcg::ply::PlyFile &pf, int i, OpenMeshType &m, PlyInfo &pi)
{
LoadPly_RangeGridAux rga;
std::vector<int> RangeGridAuxVec;
int RangeGridCols=0;
int RangeGridRows=0;

//qDebug("Starting Reading of Range Grid");
if(RangeGridCols==0) // not initialized.
{
for(size_t co=0;co< pf.comments.size();++co)
{
std::string num_cols = "num_cols";
std::string num_rows = "num_rows";
std::string &c = pf.comments[co];
std::string bufstr,bufclean;
if( num_cols == c.substr(0,num_cols.length()) )
{
bufstr = c.substr(num_cols.length()+1);
RangeGridCols = atoi(bufstr.c_str());
}
if( num_rows == c.substr(0,num_rows.length()) )
{
bufstr = c.substr(num_rows.length()+1);
RangeGridRows = atoi(bufstr.c_str());
}
}
//qDebug("Rows %i Cols %i",RangeGridRows,RangeGridCols);
}
int totPnt = RangeGridCols*RangeGridRows;
// standard reading;
pf.SetCurElement(i);
for(int j=0;j<totPnt;++j)
{
if(pi.cb && (j%1000)==0) pi.cb(50+j*50/totPnt,"RangeMap Face Loading");
if( pf.Read(&rga)==-1 )
{
//qDebug("Error after loading %i elements",j);
pi.status = PlyInfo::E_SHORTFILE;
return false;
}
else
{
if(rga.num_pts == 0)
RangeGridAuxVec.push_back(-1);
else
RangeGridAuxVec.push_back(rga.pts[0]);
}
}
//qDebug("Completed the reading of %i indexes",RangeGridAuxVec.size());
tri::SparseFaceGrid(m, RangeGridAuxVec, RangeGridCols,RangeGridRows);

return true;
}


}; // end class

Expand Down

0 comments on commit 2c835e4

Please sign in to comment.