OSDN Git Service

PLYLoader バイナリメッシュモードに対応
authorqw_fuku <fkhideaki@gmail.com>
Sun, 17 Jul 2016 07:24:06 +0000 (16:24 +0900)
committerqw_fuku <fkhideaki@gmail.com>
Sun, 17 Jul 2016 07:24:06 +0000 (16:24 +0900)
Src/LibQtGeoViewerCore/Format/Ply/PLYHeader.cpp
Src/LibQtGeoViewerCore/Format/Ply/PLYHeader.h
Src/LibQtGeoViewerCore/Format/Ply/PLYMesh.cpp
Src/LibQtGeoViewerCore/Format/Ply/PLYMesh.h

index 19c71f9..94e24e2 100644 (file)
@@ -21,6 +21,7 @@ void VertPropIdx::Clear(void)
        cR = -1;
        cG = -1;
        cB = -1;
+       cA = -1;
 }
 
 int VertPropIdx::GetMaxPropIdx(void) const
@@ -35,6 +36,7 @@ int VertPropIdx::GetMaxPropIdx(void) const
        max_vprop = (std::max)(max_vprop, cR);
        max_vprop = (std::max)(max_vprop, cG);
        max_vprop = (std::max)(max_vprop, cB);
+       max_vprop = (std::max)(max_vprop, cA);
        max_vprop += 1;
 
        return max_vprop;
@@ -83,6 +85,17 @@ bool PlyHeader::HasCol(void) const
        return (br && bg && bb);
 }
 
+int PlyHeader::NumColorElems(void) const
+{
+       if (!HasCol())
+               return 0;
+
+       if (m_VPI.cA == -1)
+               return 3;
+       else
+               return 4;
+}
+
 
 }
 }
index 429d5dc..48d1461 100644 (file)
@@ -26,6 +26,7 @@ public:
        int cR;
        int cG;
        int cB;
+       int cA;
 };
 
 
@@ -38,6 +39,7 @@ public:
        int GetMaxPropIdx(void) const;
        bool HasNormal(void) const;
        bool HasCol(void) const;
+       int NumColorElems(void) const;
 
 public:
        std::string m_FormatType;
index 5bc5e11..31a8e41 100644 (file)
@@ -94,38 +94,40 @@ bool PlyMesh::ReadHeader(std::istream& ist)
        bool IsInVertProp = false;
        int VertPropCount = 0;
 
-       for(;;)
+       for (;;)
        {
-               if(!GetNextLine(ist, s))
+               if (!GetNextLine(ist, s))
                        return false;
 
-               if(CheckHeader(s, "end_header"))
+               if (CheckHeader(s, "end_header"))
                        return true;
 
-               if(IsInVertProp)
+               if (IsInVertProp)
                {
-                       if(CheckHeader(s, "property "))
+                       if (CheckHeader(s, "property "))
                        {
                                std::string type = s.substr(s.find_last_of(" ") + 1);
 
-                               if(type == "x")
+                               if (type == "x")
                                        m_Header.m_VPI.vX = VertPropCount;
-                               else if(type == "y")
+                               else if (type == "y")
                                        m_Header.m_VPI.vY = VertPropCount;
-                               else if(type == "z")
+                               else if (type == "z")
                                        m_Header.m_VPI.vZ = VertPropCount;
-                               else if(type == "nx")
+                               else if (type == "nx")
                                        m_Header.m_VPI.nX = VertPropCount;
-                               else if(type == "ny")
+                               else if (type == "ny")
                                        m_Header.m_VPI.nY = VertPropCount;
-                               else if(type == "nz")
+                               else if (type == "nz")
                                        m_Header.m_VPI.nZ = VertPropCount;
-                               else if(type == "red")
+                               else if (type == "red")
                                        m_Header.m_VPI.cR = VertPropCount;
-                               else if(type == "green")
+                               else if (type == "green")
                                        m_Header.m_VPI.cG = VertPropCount;
-                               else if(type == "blue")
+                               else if (type == "blue")
                                        m_Header.m_VPI.cB = VertPropCount;
+                               else if (type == "alpha")
+                                       m_Header.m_VPI.cA = VertPropCount;
 
                                VertPropCount++;
                                continue;
@@ -152,21 +154,19 @@ bool PlyMesh::ReadHeader(std::istream& ist)
                        m_Header.m_NumFaces = ToInt(s.substr(strlen("element face ")));
                }
        }
-
-       return false;
 }
 
 bool PlyMesh::CheckHeader(const std::string& line, const char* header) const
 {
-       if(line == header)
+       if (line == header)
                return true;
 
-       for(size_t i = 0; i < line.length(); ++i)
+       for (size_t i = 0; i < line.length(); ++i)
        {
-               if(header[i] == '\0')
+               if (header[i] == '\0')
                        return true;
 
-               if(line[i] != header[i])
+               if (line[i] != header[i])
                        return false;
        }
 
@@ -188,14 +188,21 @@ bool PlyMesh::ReadBinary( std::istream& ist , bool is_little_endian)
        for (size_t i = 0; i < m_Header.m_NumVerts; ++i)
        {
                ist.read((char*)m_Verts[i].v(), sizeof(float) * 3);
-               
+               if (!is_little_endian)
+                       ToBigEndian(m_Verts[i]);
+
                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)
+                               ToBigEndian(m_Normals[i]);
+               }
 
-               if (!is_little_endian)
-                       ToBigEndian(m_Verts[i]);
+               if (hasCol)
+               {
+                       int nc = m_Header.NumColorElems();
+                       ist.read((char*)m_Col[i].v(), sizeof(char) * nc);
+               }
        }
 
        m_Faces.resize(m_Header.m_NumFaces);
@@ -254,16 +261,18 @@ bool PlyMesh::ReadAscii( std::istream& ist )
                                m_Col[i].y = ReadColorElem(is);
                        else if (j == m_Header.m_VPI.cB)
                                m_Col[i].z = ReadColorElem(is);
+                       else if (j == m_Header.m_VPI.cA)
+                               m_Col[i].w = ReadColorElem(is);
                }
        }
 
        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)
        {
                int num_v;
                ist >> num_v;
                m_Faces[i].m_Vid.resize(num_v);
-               for(int j = 0; j < num_v; ++j)
+               for (int j = 0; j < num_v; ++j)
                {
                        ist >> m_Faces[i].m_Vid[j];
                }
@@ -281,10 +290,10 @@ unsigned char PlyMesh::ReadColorElem(std::istream& is)
 
 bool PlyMesh::GetNextLine(std::istream& ist, std::string& s) const
 {
-       while(!ist.eof())
+       while (!ist.eof())
        {
                std::getline(ist, s);
-               if(!s.empty())
+               if (!s.empty())
                        return true;
        }
 
@@ -299,11 +308,11 @@ bool PlyMesh::Save( std::ostream& ost ) const
 
 bool PlyMesh::Save( const std::string& i_Filename ) const
 {
-       std::ofstream ofs( i_Filename.c_str() , std::ios::binary );
-       if( !ofs.is_open() )
+       std::ofstream ofs(i_Filename.c_str(), std::ios::binary);
+       if (!ofs.is_open())
                return false;
 
-       return Save( ofs );
+       return Save(ofs);
 }
 
 bool PlyMesh::ConvertToBaseMesh( BaseMesh& o_mesh ) const
index 9805a35..57f09ce 100644 (file)
@@ -51,7 +51,7 @@ public:
        PlyHeader m_Header;
        std::vector<lm::vec3f> m_Verts;
        std::vector<lm::vec3f> m_Normals;
-       std::vector<lgr::color3b> m_Col;
+       std::vector<lgr::color4b> m_Col;
        std::vector<PlyFace> m_Faces;
 };