- fixed "if there is color both vertex/face colors are enabled even if there is just one of them" bug

This commit is contained in:
Guido Ranzuglia 2016-10-13 04:28:00 +02:00
parent 8b90ba72a3
commit 20fa52181f
2 changed files with 708 additions and 703 deletions

View File

@ -111,7 +111,7 @@ static void WedgeTexFromVertexTex(ComputeMeshType &m)
{
(*fi).WT(i).U() = (*fi).V(i)->T().U();
(*fi).WT(i).V() = (*fi).V(i)->T().V();
//(*fi).WT(i).N() = 0;
(*fi).WT(i).N() = 0;
}
}
}

View File

@ -29,19 +29,19 @@
#include <wrap/io_trimesh/io_fan_tessellator.h>
namespace vcg {
namespace tri {
namespace io {
namespace tri {
namespace io {
// /** \addtogroup */
// /* @{ */
/**
// /** \addtogroup */
// /* @{ */
/**
This class encapsulate a filter for importing OFF meshes.
A basic description of the OFF file format can be found at http://www.geomview.org/docs/html/geomview_41.html
*/
template<class MESH_TYPE>
class ImporterOFF
{
public:
template<class MESH_TYPE>
class ImporterOFF
{
public:
typedef typename MESH_TYPE::VertexType VertexType;
typedef typename MESH_TYPE::VertexIterator VertexIterator;
@ -53,9 +53,11 @@ public:
typedef typename MESH_TYPE::ScalarType ScalarType;
// OFF codes
enum OFFCodes {NoError=0, CantOpen, InvalidFile,
enum OFFCodes {
NoError = 0, CantOpen, InvalidFile,
InvalidFile_MissingOFF,
UnsupportedFormat, ErrorNotTriangularFace,ErrorHighDimension,ErrorDegenerateFace};
UnsupportedFormat, ErrorNotTriangularFace, ErrorHighDimension, ErrorDegenerateFace
};
/*!
* Standard call for knowing the meaning of an error code
@ -70,7 +72,7 @@ public:
"Invalid file: OFF file should have in the first line the OFF keyword as a first token",
"Unsupported format", "Face with more than 3 vertices","File with high dimensional vertexes are not supported", "Error Degenerate Face with less than 3 vertices" };
if(message_code>6 || message_code<0)
if (message_code > 6 || message_code < 0)
return "Unknown error";
else
return error_msg[message_code];
@ -87,24 +89,24 @@ public:
{
// To obtain the loading mask all the file must be parsed
// to distinguish between per-vertex and per-face color attribute.
loadmask=0;
loadmask = 0;
MESH_TYPE dummyMesh;
return (Open(dummyMesh, filename, loadmask)==NoError);
return (Open(dummyMesh, filename, loadmask) == NoError);
}
static int Open(MESH_TYPE &mesh, const char *filename,CallBackPos *cb=0)
static int Open(MESH_TYPE &mesh, const char *filename, CallBackPos *cb = 0)
{
int loadmask;
return Open(mesh,filename,loadmask,cb);
return Open(mesh, filename, loadmask, cb);
}
static int OpenMem(MESH_TYPE &mesh, const char *mem, size_t sz, int &loadmask,
CallBackPos *cb=0)
CallBackPos *cb = 0)
{
std::string str;
str.append(mem,sz);
str.append(mem, sz);
std::istringstream strm(str);
return OpenStream(mesh,strm,loadmask,cb);
return OpenStream(mesh, strm, loadmask, cb);
}
/*!
@ -115,20 +117,20 @@ public:
* \return the operation result
*/
static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask,
CallBackPos *cb=0)
CallBackPos *cb = 0)
{
std::ifstream stream(filename);
if (stream.fail())
return CantOpen;
return OpenStream(mesh,stream,loadmask,cb);
return OpenStream(mesh, stream, loadmask, cb);
}
static int OpenStream(MESH_TYPE &mesh, std::istream &stream, int &loadmask,
CallBackPos *cb=0)
CallBackPos *cb = 0)
{
std::vector< std::string > tokens;
TokenizeNextLine(stream, tokens);
if(tokens.empty()) return InvalidFile_MissingOFF;
if (tokens.empty()) return InvalidFile_MissingOFF;
bool isNormalDefined = false;
bool isColorDefined = false;
@ -164,21 +166,26 @@ public:
std::string header = tokens[0];
if (header.rfind("OFF") != std::basic_string<char>::npos)
{ // the OFF string is in the header go on parsing it.
for (int u = static_cast<int>(header.rfind("OFF")-1); u>=0; u--)
for (int u = static_cast<int>(header.rfind("OFF") - 1); u >= 0; u--)
{
if (header[u] == 'C') isColorDefined = true;
else if (header[u] == 'N') isNormalDefined = true;
else if (u>0 && header[u-1] == 'S' && header[u] == 'T') isTexCoordDefined = true;
else if (header[u] == '4') homogeneousComponents = true;
else if (header[u] == 'n') return ErrorHighDimension;
if (header[u] == 'C')
isColorDefined = true;
else if (header[u] == 'N')
isNormalDefined = true;
else if (u > 0 && header[u - 1] == 'S' && header[u] == 'T')
isTexCoordDefined = true;
else if (header[u] == '4')
homogeneousComponents = true;
else if (header[u] == 'n')
return ErrorHighDimension;
}
}
else return InvalidFile_MissingOFF;
// If the file is slightly malformed and it has nvert and nface AFTER the OFF string instead of in the next line
// we manage it here...
if(tokens.size()==1) TokenizeNextLine(stream, tokens);
else tokens.erase(tokens.begin(),tokens.begin()+1);
if (tokens.size() == 1) TokenizeNextLine(stream, tokens);
else tokens.erase(tokens.begin(), tokens.begin() + 1);
// Update loading mask
///////////////////////////////////////
@ -187,7 +194,7 @@ public:
if (isNormalDefined) loadmask |= Mask::IOM_VERTNORMAL;
if (isTexCoordDefined) loadmask |= Mask::IOM_VERTTEXCOORD;
if (isColorDefined) { loadmask |= Mask::IOM_VERTCOLOR;loadmask |= Mask::IOM_FACECOLOR;}
//if (isColorDefined) { loadmask |= Mask::IOM_VERTCOLOR;loadmask |= Mask::IOM_FACECOLOR;}
//if(onlyMaskFlag) return NoError;
@ -196,7 +203,7 @@ public:
mesh.Clear();
// check on next 2 lines to detect corrupted files
if(tokens.size() < 3)
if (tokens.size() < 3)
return InvalidFile;
unsigned int nVertices, nFaces, nEdges;
@ -218,13 +225,13 @@ public:
TokenizeNextLine(stream, tokens);
size_t k = 0; // next token to read
for (unsigned int i=0; i<nVertices; i++, v_iter++)
for (unsigned int i = 0; i < nVertices; i++, v_iter++)
{
if (cb && (i%1000)==0)
cb(i*50/nVertices, "Vertex Loading");
if (cb && (i % 1000) == 0)
cb(i * 50 / nVertices, "Vertex Loading");
// Read 3 vertex coordinates
for (unsigned int j=0; j<3; j++)
for (unsigned int j = 0; j < 3; j++)
{
// Go to next line when needed
if (k == tokens.size()) // if EOL
@ -236,14 +243,14 @@ public:
}
// Read vertex coordinate
(*v_iter).P()[j] = (ScalarType) atof(tokens[k].c_str());
(*v_iter).P()[j] = (ScalarType)atof(tokens[k].c_str());
k++;
}
if (isNormalDefined)
{
// Read 3 normal coordinates
for (unsigned int j=0; j<3; j++)
for (unsigned int j = 0; j < 3; j++)
{
// Go to next line when needed
if (k == tokens.size()) // if EOL
@ -255,7 +262,7 @@ public:
}
// Read normal coordinate
(*v_iter).N()[j] = (ScalarType) atof(tokens[k].c_str());
(*v_iter).N()[j] = (ScalarType)atof(tokens[k].c_str());
k++;
}
}
@ -297,9 +304,9 @@ public:
unsigned char r =
static_cast<unsigned char>(atoi(tokens[k].c_str()));
unsigned char g =
static_cast<unsigned char>(atoi(tokens[k+1].c_str()));
static_cast<unsigned char>(atoi(tokens[k + 1].c_str()));
unsigned char b =
static_cast<unsigned char>(atoi(tokens[k+2].c_str()));
static_cast<unsigned char>(atoi(tokens[k + 2].c_str()));
vcg::Color4b color(r, g, b, 255);
(*v_iter).C().Import(color);
@ -308,8 +315,8 @@ public:
{
// floats
float r = static_cast<float>(atof(tokens[k].c_str()));
float g = static_cast<float>(atof(tokens[k+1].c_str()));
float b = static_cast<float>(atof(tokens[k+2].c_str()));
float g = static_cast<float>(atof(tokens[k + 1].c_str()));
float b = static_cast<float>(atof(tokens[k + 2].c_str()));
vcg::Color4f color(r, g, b, 1.0);
(*v_iter).C().Import(color);
@ -324,11 +331,11 @@ public:
unsigned char r =
static_cast<unsigned char>(atoi(tokens[k].c_str()));
unsigned char g =
static_cast<unsigned char>(atoi(tokens[k+1].c_str()));
static_cast<unsigned char>(atoi(tokens[k + 1].c_str()));
unsigned char b =
static_cast<unsigned char>(atoi(tokens[k+2].c_str()));
static_cast<unsigned char>(atoi(tokens[k + 2].c_str()));
unsigned char a =
static_cast<unsigned char>(atoi(tokens[k+3].c_str()));
static_cast<unsigned char>(atoi(tokens[k + 3].c_str()));
Color4b color(r, g, b, a);
(*v_iter).C().Import(color);
@ -337,9 +344,9 @@ public:
{
// floats
float r = static_cast<float>(atof(tokens[k].c_str()));
float g = static_cast<float>(atof(tokens[k+1].c_str()));
float b = static_cast<float>(atof(tokens[k+2].c_str()));
float a = static_cast<float>(atof(tokens[k+3].c_str()));
float g = static_cast<float>(atof(tokens[k + 1].c_str()));
float b = static_cast<float>(atof(tokens[k + 2].c_str()));
float a = static_cast<float>(atof(tokens[k + 3].c_str()));
vcg::Color4f color(r, g, b, a);
(*v_iter).C().Import(color);
@ -352,7 +359,7 @@ public:
if (isTexCoordDefined)
{
for (unsigned int j=0; j<2; j++)
for (unsigned int j = 0; j < 2; j++)
{
// Go to next line when needed
if (k == tokens.size()) // if EOL
@ -377,16 +384,16 @@ public:
// READ FACES
//////////////////////////////////////////////////////
if(FaceType::HasPolyInfo())
if (FaceType::HasPolyInfo())
{
for (unsigned int f=0; f < nFaces; f++)
for (unsigned int f = 0; f < nFaces; f++)
{
if(cb && (f%1000)==0) cb(50+f*50/nFaces,"Face Loading");
if (cb && (f % 1000) == 0) cb(50 + f * 50 / nFaces, "Face Loading");
TokenizeNextLine(stream, tokens);
int vert_per_face = atoi(tokens[0].c_str());
std::vector<int> vInd(vert_per_face);
k = 1;
for (int j=0; j < vert_per_face; j++)
for (int j = 0; j < vert_per_face; j++)
{
if (k == tokens.size()) // if EOL // Go to next line when needed
{
@ -397,36 +404,36 @@ public:
vInd[j] = atoi(tokens[k].c_str());
k++;
}
if(vert_per_face==3)
Allocator<MESH_TYPE>::AddFace(mesh, &mesh.vert[ vInd[0] ], &mesh.vert[ vInd[1] ], &mesh.vert[ vInd[2] ]);
if (vert_per_face == 3)
Allocator<MESH_TYPE>::AddFace(mesh, &mesh.vert[vInd[0]], &mesh.vert[vInd[1]], &mesh.vert[vInd[2]]);
if(vert_per_face==4)
Allocator<MESH_TYPE>::AddQuadFace(mesh, &mesh.vert[ vInd[0] ], &mesh.vert[ vInd[1] ], &mesh.vert[ vInd[2] ],&mesh.vert[ vInd[3] ]);
if (vert_per_face == 4)
Allocator<MESH_TYPE>::AddQuadFace(mesh, &mesh.vert[vInd[0]], &mesh.vert[vInd[1]], &mesh.vert[vInd[2]], &mesh.vert[vInd[3]]);
}
}
else // Standard Triangular Mesh Loading
{
Allocator<MESH_TYPE>::AddFaces(mesh, nFaces);
unsigned int f0=0;
unsigned int f0 = 0;
// Initial call to the QuadTriangulate with an empty vector to just reset the static set of existing diagonals
std::vector<VertexPointer> qtmp;
BitQuad<MESH_TYPE>::QuadTriangulate(qtmp);
for (unsigned int f=0; f < nFaces; f++)
for (unsigned int f = 0; f < nFaces; f++)
{
f0 = f;
if (stream.fail())
return InvalidFile;
if(cb && (f%1000)==0)
cb(50+f*50/nFaces,"Face Loading");
if (cb && (f % 1000) == 0)
cb(50 + f * 50 / nFaces, "Face Loading");
TokenizeNextLine(stream, tokens);
int vert_per_face = atoi(tokens[0].c_str());
if(vert_per_face < 3)
if (vert_per_face < 3)
return ErrorDegenerateFace;
k = 1;
if (vert_per_face == 3)
@ -440,19 +447,19 @@ public:
k = 0;
}
mesh.face[f].V(j) = &(mesh.vert[ atoi(tokens[k].c_str()) ]);
mesh.face[f].V(j) = &(mesh.vert[atoi(tokens[k].c_str())]);
k++;
}
}
else
{
// The face must be triangulated
unsigned int trigs = vert_per_face-3; // number of extra faces to add
unsigned int trigs = vert_per_face - 3; // number of extra faces to add
nFaces += trigs;
Allocator<MESH_TYPE>::AddFaces(mesh, trigs);
std::vector<int> vertIndices(vert_per_face);
std::vector<vcg::Point3f > polygonVect(vert_per_face); // vec of polygon loops used for the triangulation of polygonal face
for (int j=0; j < vert_per_face; j++)
for (int j = 0; j < vert_per_face; j++)
{
if (k == tokens.size()) // if EOL // Go to next line when needed
{
@ -461,27 +468,27 @@ public:
k = 0;
}
vertIndices[j] = atoi(tokens[k].c_str());
polygonVect[j].Import<ScalarType> (mesh.vert[ vertIndices[j] ].P());
polygonVect[j].Import<ScalarType>(mesh.vert[vertIndices[j]].P());
k++;
}
if(vert_per_face==4)
if (vert_per_face == 4)
{ // To well triangulate use the bitquad support function that reorders vertex for a simple fan
std::vector<VertexPointer> q(4);
for(int qqi=0;qqi<4;++qqi)
q[qqi]=& mesh.vert[vertIndices[qqi]];
for (int qqi = 0; qqi < 4; ++qqi)
q[qqi] = &mesh.vert[vertIndices[qqi]];
BitQuad<MESH_TYPE>::QuadTriangulate(q);
for(int qqi=0;qqi<4;++qqi)
vertIndices[qqi] = q[qqi]- & mesh.vert[0];
for (int qqi = 0; qqi < 4; ++qqi)
vertIndices[qqi] = q[qqi] - &mesh.vert[0];
// build a two face fan
for (int j=0; j<2; j++)
for (int j = 0; j < 2; j++)
{
mesh.face[f+j].V(0) = &(mesh.vert[ vertIndices[0 ] ]);
mesh.face[f+j].V(1) = &(mesh.vert[ vertIndices[1+j] ]);
mesh.face[f+j].V(2) = &(mesh.vert[ vertIndices[2+j] ]);
mesh.face[f + j].V(0) = &(mesh.vert[vertIndices[0]]);
mesh.face[f + j].V(1) = &(mesh.vert[vertIndices[1 + j]]);
mesh.face[f + j].V(2) = &(mesh.vert[vertIndices[2 + j]]);
if (tri::HasPerFaceFlags(mesh)) {
// tag internal polygonal edges as "faux"
if (j>0) mesh.face[f+j].SetF(0);
if (j<vert_per_face-3) mesh.face[f+j].SetF(2);
if (j > 0) mesh.face[f + j].SetF(0);
if (j < vert_per_face - 3) mesh.face[f + j].SetF(2);
loadmask |= Mask::IOM_BITPOLYGONAL;
}
}
@ -499,100 +506,99 @@ public:
//qDebug("Warning: using fan tessellation for a polygon of %i vertices",vertexesPerFace);
tri::io::FanTessellator(loopVect, indexTriangulatedVect);
#endif
for (size_t j=0; j<indexTriangulatedVect.size(); j+=3)
for (size_t j = 0; j < indexTriangulatedVect.size(); j += 3)
{
mesh.face[f+j/3].V(0) = &(mesh.vert[ vertIndices[ indexTriangulatedVect[j+0] ] ]);
mesh.face[f+j/3].V(1) = &(mesh.vert[ vertIndices[ indexTriangulatedVect[j+1] ] ]);
mesh.face[f+j/3].V(2) = &(mesh.vert[ vertIndices[ indexTriangulatedVect[j+2] ] ]);
mesh.face[f + j / 3].V(0) = &(mesh.vert[vertIndices[indexTriangulatedVect[j + 0]]]);
mesh.face[f + j / 3].V(1) = &(mesh.vert[vertIndices[indexTriangulatedVect[j + 1]]]);
mesh.face[f + j / 3].V(2) = &(mesh.vert[vertIndices[indexTriangulatedVect[j + 2]]]);
// To correctly set Faux edges we have to clear the faux bit for all the edges that do not correspond to consecutive vertices
// Consecutivity is in the space of the index of the polygon.
for(int qq=0;qq<3;++qq)
for (int qq = 0; qq < 3; ++qq)
{
if( (indexTriangulatedVect[j+qq]+1)%vert_per_face == indexTriangulatedVect[j+(qq+1)%3])
mesh.face[f+j/3].ClearF(qq);
else mesh.face[f+j/3].SetF(qq);
if ((indexTriangulatedVect[j + qq] + 1) % vert_per_face == indexTriangulatedVect[j + (qq + 1) % 3])
mesh.face[f + j / 3].ClearF(qq);
else mesh.face[f + j / 3].SetF(qq);
}
}
}
f+=trigs;
f += trigs;
}
// NOTE: It is assumed that colored face takes exactly one text line
// (otherwise it is impossible to parse color information since
// color components can vary)
size_t color_elements = tokens.size() - vert_per_face-1;
isColorDefined |= (color_elements>0);
if(isColorDefined) loadmask |= Mask::IOM_FACECOLOR;
size_t color_elements = tokens.size() - vert_per_face - 1;
//isColorDefined |= (color_elements>0);
//if(isColorDefined) loadmask |= Mask::IOM_FACECOLOR;
if( (color_elements>0) && tri::HasPerFaceColor(mesh) )
{
// set per-face color attribute
if (color_elements > 0)
{
loadmask |= Mask::IOM_FACECOLOR;
if (tri::HasPerFaceColor(mesh))
{
switch (color_elements)
{
case 0:
{
for ( ; f0<=f; f0++)
for (; f0 <= f; f0++)
mesh.face[f0].C().Import(vcg::Color4f(.666f, .666f, .666f, .666f));
break;
}
case 1:
{
for ( ; f0<=f; f0++)
mesh.face[f0].C().Import( ColorMap( atoi(tokens[vert_per_face+1].c_str()) ) );
for (; f0 <= f; f0++)
mesh.face[f0].C().Import(ColorMap(atoi(tokens[vert_per_face + 1].c_str())));
break;
}
case 3:
{
if (tokens[vert_per_face+1].find('.')==std::string::npos) // if there is a float there is a dot
if (tokens[vert_per_face + 1].find('.') == std::string::npos) // if there is a float there is a dot
{
Color4b cc(Color4b::White);
cc[0] = (unsigned char)atoi( tokens[vert_per_face+1].c_str() );
cc[1] = (unsigned char)atoi( tokens[vert_per_face+2].c_str() );
cc[2] = (unsigned char)atoi( tokens[vert_per_face+3].c_str() );
for ( ; f0<=f; f0++)
mesh.face[f0].C()=cc;
cc[0] = (unsigned char)atoi(tokens[vert_per_face + 1].c_str());
cc[1] = (unsigned char)atoi(tokens[vert_per_face + 2].c_str());
cc[2] = (unsigned char)atoi(tokens[vert_per_face + 3].c_str());
for (; f0 <= f; f0++)
mesh.face[f0].C() = cc;
}
else
{
float color[3];
color[0] = (float) atof( tokens[vert_per_face+1].c_str() );
color[1] = (float) atof( tokens[vert_per_face+2].c_str() );
color[2] = (float) atof( tokens[vert_per_face+3].c_str() );
for ( ; f0<=f; f0++)
color[0] = (float)atof(tokens[vert_per_face + 1].c_str());
color[1] = (float)atof(tokens[vert_per_face + 2].c_str());
color[2] = (float)atof(tokens[vert_per_face + 3].c_str());
for (; f0 <= f; f0++)
mesh.face[f0].C().Import(vcg::Color4f(color[0], color[1], color[2], 1.0f));
}
break;
}
case 4:
{
if (tokens[vert_per_face+1].find('.')==std::string::npos) // if it is a float there is a dot
if (tokens[vert_per_face + 1].find('.') == std::string::npos) // if it is a float there is a dot
{
Color4b cc;
cc[0] = (unsigned char) atoi(tokens[vert_per_face+1].c_str());
cc[1] = (unsigned char) atoi(tokens[vert_per_face+2].c_str());
cc[2] = (unsigned char) atoi(tokens[vert_per_face+3].c_str());
cc[3] = (unsigned char) atoi(tokens[vert_per_face+4].c_str());
for ( ; f0<=f; f0++)
mesh.face[f0].C()=cc;
cc[0] = (unsigned char)atoi(tokens[vert_per_face + 1].c_str());
cc[1] = (unsigned char)atoi(tokens[vert_per_face + 2].c_str());
cc[2] = (unsigned char)atoi(tokens[vert_per_face + 3].c_str());
cc[3] = (unsigned char)atoi(tokens[vert_per_face + 4].c_str());
for (; f0 <= f; f0++)
mesh.face[f0].C() = cc;
}
else
{
float color[4];
color[0] = float( atof(tokens[vert_per_face+1].c_str()) );
color[1] = float( atof(tokens[vert_per_face+2].c_str()) );
color[2] = float( atof(tokens[vert_per_face+3].c_str()) );
color[3] = float( atof(tokens[vert_per_face+4].c_str()) );
for ( ; f0<=f; f0++)
color[0] = float(atof(tokens[vert_per_face + 1].c_str()));
color[1] = float(atof(tokens[vert_per_face + 2].c_str()));
color[2] = float(atof(tokens[vert_per_face + 3].c_str()));
color[3] = float(atof(tokens[vert_per_face + 4].c_str()));
for (; f0 <= f; f0++)
mesh.face[f0].C().Import(vcg::Color4f(color[0], color[1], color[2], color[3]));
}
break;
}
} //end switch
}
} // end if (isColorDefined)
} // end of for f=...
}
@ -600,7 +606,7 @@ public:
} // end Open
protected:
protected:
/*!
* Read the next valid line and parses it into "tokens", allowing the tokens to be read one at a time.
@ -612,7 +618,7 @@ protected:
std::string line;
do
std::getline(stream, line, '\n');
while ((line[0] == '#' || line.length()==0 || line[0]=='\r' ) && (!stream.eof()));
while ((line[0] == '#' || line.length() == 0 || line[0] == '\r') && (!stream.eof()));
size_t from = 0;
size_t to = 0;
@ -620,18 +626,17 @@ protected:
tokens.clear();
do
{
while (from!=length && (line[from]==' ' || line[from] == '\t' || line[from] == '\r'))
while (from != length && (line[from] == ' ' || line[from] == '\t' || line[from] == '\r'))
from++;
if(from!=length)
if (from != length)
{
to = from+1;
while ( to!=length && (((line[to]!=' ') && (line[to] != '\t')) || (line[to] == '\r')))
to = from + 1;
while (to != length && (((line[to] != ' ') && (line[to] != '\t')) || (line[to] == '\r')))
to++;
tokens.push_back(line.substr(from, to-from).c_str());
tokens.push_back(line.substr(from, to - from).c_str());
from = to;
}
}
while (from<length);
} while (from < length);
} // end Tokenize
/*!
@ -794,10 +799,10 @@ protected:
};
return Color4f(colorMap[i][0], colorMap[i][1], colorMap[i][2], colorMap[i][3]);
}
};
// /*! @} */
} //namespace io
}//namespace tri
};
// /*! @} */
} //namespace io
}//namespace tri
} // namespace vcg
#endif //__VCGLIB_IMPORT_OFF