nX = -1;
nY = -1;
nZ = -1;
+ cR = -1;
+ cG = -1;
+ cB = -1;
}
int VertPropIdx::GetMaxPropIdx(void) const
max_vprop = (std::max)(max_vprop, nX);
max_vprop = (std::max)(max_vprop, nY);
max_vprop = (std::max)(max_vprop, nZ);
+ max_vprop = (std::max)(max_vprop, cR);
+ max_vprop = (std::max)(max_vprop, cG);
+ max_vprop = (std::max)(max_vprop, cB);
max_vprop += 1;
return max_vprop;
{
m_FormatType.clear();
- m_NumVerts = 0;
+ m_NumVerts = 0;
m_VPI.Clear();
m_NumFaces = 0;
return (bx && by && bz);
}
+bool PlyHeader::HasCol(void) const
+{
+ bool br = (m_VPI.cR != -1);
+ bool bg = (m_VPI.cG != -1);
+ bool bb = (m_VPI.cB != -1);
+
+ assert(br == bg && bg == bb);
+ return (br && bg && bb);
+}
+
}
}
m_Header.Clear();
m_Verts.clear();
m_Normals.clear();
+ m_Col.clear();
m_Faces.clear();
}
m_Header.m_VPI.nY = VertPropCount;
else if(type == "nz")
m_Header.m_VPI.nZ = VertPropCount;
+ else if(type == "red")
+ m_Header.m_VPI.cR = VertPropCount;
+ else if(type == "green")
+ m_Header.m_VPI.cG = VertPropCount;
+ else if(type == "blue")
+ m_Header.m_VPI.cB = VertPropCount;
VertPropCount++;
continue;
bool PlyMesh::ReadBinary( std::istream& ist , bool is_little_endian)
{
- bool has_norm = m_Header.HasNormal();
+ bool hasNorm = m_Header.HasNormal();
+ bool hasCol = m_Header.HasCol();
m_Verts.resize(m_Header.m_NumVerts);
- if(has_norm)
+ if (hasNorm)
m_Normals.resize(m_Header.m_NumVerts);
+ if (hasCol)
+ m_Col.resize(m_Header.m_NumVerts);
- for(size_t i = 0; i < m_Header.m_NumVerts; ++i)
+ for (size_t i = 0; i < m_Header.m_NumVerts; ++i)
{
ist.read((char*)m_Verts[i].v(), sizeof(float) * 3);
-
- if(has_norm)
+
+ if (hasNorm)
ist.read((char*)m_Normals[i].v(), sizeof(float) * 3);
+ if (hasCol)
+ ist.read((char*)m_Col[i].v(), sizeof(char) * 3);
- if(!is_little_endian)
+ if (!is_little_endian)
ToBigEndian(m_Verts[i]);
}
m_Faces.resize(m_Header.m_NumFaces);
- for(size_t i = 0; i < m_Header.m_NumFaces; ++i)
+ for (size_t i = 0; i < m_Header.m_NumFaces; ++i)
{
unsigned char nf;
ist.read((char*)&nf, sizeof(unsigned char));
m_Faces[i].m_Vid.resize((int)nf);
- for(int j = 0; j < (int)nf; ++j)
+ for (int j = 0; j < (int)nf; ++j)
{
ist.read((char*)&m_Faces[i].m_Vid[j], sizeof(int));
- if(!is_little_endian)
+ if (!is_little_endian)
ToBigEndian(m_Faces[i].m_Vid[j]);
}
}
m_Verts.resize(m_Header.m_NumVerts);
if (m_Header.HasNormal())
m_Normals.resize(m_Header.m_NumVerts);
+ if (m_Header.HasCol())
+ m_Col.resize(m_Header.m_NumVerts);
int max_vprop = m_Header.GetMaxPropIdx();
std::istringstream is(s);
- for(int j = 0; j < max_vprop; ++j)
+ for (int j = 0; j < max_vprop; ++j)
{
- float f;
- is >> f;
-
if (j == m_Header.m_VPI.vX)
- {
- m_Verts[i].x = f;
- }
+ is >> m_Verts[i].x;
else if (j == m_Header.m_VPI.vY)
- {
- m_Verts[i].y = f;
- }
+ is >> m_Verts[i].y;
else if (j == m_Header.m_VPI.vZ)
- {
- m_Verts[i].z = f;
- }
+ is >> m_Verts[i].z;
else if (j == m_Header.m_VPI.nX)
- {
- m_Normals[i].x = f;
- }
+ is >> m_Normals[i].x;
else if (j == m_Header.m_VPI.nY)
- {
- m_Normals[i].y = f;
- }
+ is >> m_Normals[i].y;
else if (j == m_Header.m_VPI.nZ)
- {
- m_Normals[i].z = f;
- }
+ is >> m_Normals[i].z;
+ else if (j == m_Header.m_VPI.cR)
+ m_Col[i].x = ReadColorElem(is);
+ else if (j == m_Header.m_VPI.cG)
+ m_Col[i].y = ReadColorElem(is);
+ else if (j == m_Header.m_VPI.cB)
+ m_Col[i].z = ReadColorElem(is);
}
}
return true;
}
+unsigned char PlyMesh::ReadColorElem(std::istream& is)
+{
+ int i;
+ is >> i;
+ return (unsigned char)i;
+}
+
bool PlyMesh::GetNextLine(std::istream& ist, std::string& s) const
{
while(!ist.eof())
o_mesh.m_Verts = m_Verts;
+ o_mesh.m_Col.resize(m_Col.size());
+ for (size_t i = 0; i < m_Col.size(); ++i)
+ {
+ o_mesh.m_Col[i].x = (float)m_Col[i].x / 255.0f;
+ o_mesh.m_Col[i].y = (float)m_Col[i].y / 255.0f;
+ o_mesh.m_Col[i].z = (float)m_Col[i].z / 255.0f;
+ }
+
o_mesh.m_Faces.resize(m_Faces.size());
- for(size_t i = 0; i < m_Faces.size(); ++i)
+ for (size_t i = 0; i < m_Faces.size(); ++i)
{
o_mesh.m_Faces[i].m_VertIds = m_Faces[i].m_Vid;
}