OSDN Git Service

stlmesh asciiモードの出力を追加
authorqw_fuku <fkhideaki@gmail.com>
Tue, 5 Jun 2018 14:42:05 +0000 (23:42 +0900)
committerqw_fuku <fkhideaki@gmail.com>
Tue, 5 Jun 2018 14:42:46 +0000 (23:42 +0900)
Lib/LibGeo/Mesh/STL/STLMesh.cpp
Lib/LibGeo/Mesh/STL/STLMesh.h

index 1f7c67a..50792ab 100644 (file)
@@ -15,10 +15,7 @@ namespace lib_geo
 
 StlMesh::StlMesh(void)
 {
-       for (int i = 0; i < MSG_LENGTH; ++i)
-       {
-               m_Message[i] = 0;
-       }
+       ClearMessage();
 }
 
 void StlMesh::Clear(void)
@@ -67,7 +64,7 @@ bool StlMesh::LoadBinary(std::istream& in)
        return true;
 }
 
-bool StlMesh::LoadText(std::istream& in)
+bool StlMesh::LoadAscii(std::istream& in)
 {
        if (!IsExistValidAsciiHeader(in))
                return false;
@@ -148,89 +145,128 @@ bool StlMesh::Load(std::istream& ist)
 
        ist.seekg(0, std::ios::beg);
 
-       if (LoadText(ist))
+       if (LoadAscii(ist))
                return true;
 
        return false;
 }
 
-bool StlMesh::Load(const std::string& i_Filename)
+bool StlMesh::Load(const std::string& filename)
 {
-       std::ifstream ifs(i_Filename.c_str(), std::ios::binary);
+       std::ifstream ifs(filename.c_str(), std::ios::binary);
        if (!ifs.is_open())
                return false;
 
        return Load(ifs);
 }
 
-bool StlMesh::Save(std::ostream& ost) const
+bool StlMesh::SaveBinary(std::ostream& ost) const
 {
        unsigned int NumFaces = (unsigned int)m_Faces.size();
        ost.write(m_Message, MSG_LENGTH);
        ost.write((char*)&NumFaces, sizeof(unsigned int));
 
-       for (size_t i = 0; i < NumFaces; ++i)
-       {
-               char padding[2] = {0, 0};
+       char padding[2] = {0, 0};
 
-               ost.write((char*)m_Faces[i].m_Normal.v(), sizeof(float) * 3);
-               ost.write((char*)m_Faces[i].m_Verts[0].v(), sizeof(float) * 3);
-               ost.write((char*)m_Faces[i].m_Verts[1].v(), sizeof(float) * 3);
-               ost.write((char*)m_Faces[i].m_Verts[2].v(), sizeof(float) * 3);
+       for (const StlFace& f : m_Faces)
+       {
+               ost.write((char*)f.m_Normal.v(), sizeof(float) * 3);
+               ost.write((char*)f.m_Verts[0].v(), sizeof(float) * 3);
+               ost.write((char*)f.m_Verts[1].v(), sizeof(float) * 3);
+               ost.write((char*)f.m_Verts[2].v(), sizeof(float) * 3);
                ost.write(padding, 2);
        }
 
        return true;
 }
 
-bool StlMesh::Save(const std::string& i_Filename) const
+bool StlMesh::SaveBinary(const std::string& filename) const
+{
+       std::ofstream ofs(filename.c_str(), std::ios::binary);
+       if (!ofs.is_open())
+               return false;
+
+       return SaveBinary(ofs);
+}
+
+bool StlMesh::SaveAscii(std::ostream& ost) const
+{
+       ost << "solid " << m_Message << std::endl;
+
+       for (const StlFace& f : m_Faces)
+       {
+               WriteAscciiVert(ost, "facet normal", f.m_Normal);
+               ost << "outer loop" << std::endl;
+               WriteAscciiVert(ost, "vertex", f.m_Verts[0]);
+               WriteAscciiVert(ost, "vertex", f.m_Verts[1]);
+               WriteAscciiVert(ost, "vertex", f.m_Verts[2]);
+               ost << "endloop" << std::endl;
+               ost << "endfacet" << std::endl;
+       }
+
+       ost << "endsolid" << std::endl;
+
+       return true;
+}
+
+bool StlMesh::SaveAscii(const std::string& filename) const
 {
-       std::ofstream ofs(i_Filename.c_str(), std::ios::binary);
+       std::ofstream ofs(filename.c_str());
        if (!ofs.is_open())
                return false;
 
-       return Save(ofs);
+       return SaveAscii(ofs);
 }
 
-bool StlMesh::ConvertToBaseMesh(BaseMesh& o_mesh) const
+void StlMesh::WriteAscciiVert(std::ostream& out, const char* head, const lm::vec3f& v) const
 {
-       o_mesh.Clear();
+       out << head
+               << " " << v.x
+               << " " << v.y
+               << " " << v.z
+               << std::endl;
+}
 
-       o_mesh.m_Verts.resize(m_Faces.size() * 3);
-       o_mesh.m_Normals.resize(m_Faces.size());
-       o_mesh.m_Faces.resize(m_Faces.size());
+bool StlMesh::ConvertToBaseMesh(BaseMesh& bm) const
+{
+       bm.Clear();
+
+       bm.m_Verts.resize(m_Faces.size() * 3);
+       bm.m_Normals.resize(m_Faces.size());
+       bm.m_Faces.resize(m_Faces.size());
 
        for (size_t i = 0; i < m_Faces.size(); ++i)
        {
                const StlFace& f = m_Faces[i];
 
-               o_mesh.m_Verts[i * 3 + 0] = f.m_Verts[0];
-               o_mesh.m_Verts[i * 3 + 1] = f.m_Verts[1];
-               o_mesh.m_Verts[i * 3 + 2] = f.m_Verts[2];
+               bm.m_Verts[i * 3 + 0] = f.m_Verts[0];
+               bm.m_Verts[i * 3 + 1] = f.m_Verts[1];
+               bm.m_Verts[i * 3 + 2] = f.m_Verts[2];
 
-               o_mesh.m_Normals[i] = f.m_Normal;
+               bm.m_Normals[i] = f.m_Normal;
 
-               o_mesh.m_Faces[i].m_VertIds.resize(3);
-               o_mesh.m_Faces[i].m_NormIds.resize(3);
+               BaseFace& bf = bm.m_Faces[i];
+               bf.m_VertIds.resize(3);
+               bf.m_NormIds.resize(3);
 
-               o_mesh.m_Faces[i].m_VertIds[0] = (int)(i * 3 + 0);
-               o_mesh.m_Faces[i].m_VertIds[1] = (int)(i * 3 + 1);
-               o_mesh.m_Faces[i].m_VertIds[2] = (int)(i * 3 + 2);
+               bf.m_VertIds[0] = (int)(i * 3 + 0);
+               bf.m_VertIds[1] = (int)(i * 3 + 1);
+               bf.m_VertIds[2] = (int)(i * 3 + 2);
 
-               o_mesh.m_Faces[i].m_NormIds[0] = (int)i;
-               o_mesh.m_Faces[i].m_NormIds[1] = (int)i;
-               o_mesh.m_Faces[i].m_NormIds[2] = (int)i;
+               bf.m_NormIds[0] = (int)i;
+               bf.m_NormIds[1] = (int)i;
+               bf.m_NormIds[2] = (int)i;
        }
 
        return true;
 }
 
-//! サイズ0の法線を含む場合にtrueを返す
+//! \83T\83C\83Y0\82Ì\96@\90ü\82ð\8aÜ\82Þ\8fê\8d\87\82Étrue\82ð\95Ô\82·
 bool StlMesh::ExistZeroLengthNormal(void) const
 {
-       for (size_t i = 0; i < m_Faces.size(); ++i)
+       for (const StlFace& f : m_Faces)
        {
-               if (m_Faces[i].m_Normal.is_zero())
+               if (f.m_Normal.is_zero())
                        return true;
        }
 
index 9077589..33c6d68 100644 (file)
@@ -32,11 +32,15 @@ public:
        void Clear(void);
 
        bool Load(std::istream& ist);
-       bool Load(const std::string& i_Filename);
-       bool Save(std::ostream& ost) const;
-       bool Save(const std::string& i_Filename) const;
+       bool Load(const std::string& filename);
 
-       bool ConvertToBaseMesh(BaseMesh& o_mesh) const;
+       bool SaveBinary(std::ostream& ost) const;
+       bool SaveBinary(const std::string& filename) const;
+
+       bool SaveAscii(std::ostream& ost) const;
+       bool SaveAscii(const std::string& filename) const;
+
+       bool ConvertToBaseMesh(BaseMesh& bm) const;
 
        bool ExistZeroLengthNormal(void) const;
 
@@ -44,12 +48,14 @@ private:
        void ClearMessage(void);
 
        bool LoadBinary(std::istream& in);
-       bool LoadText(std::istream& in);
+       bool LoadAscii(std::istream& in);
 
        bool IsExistValidAsciiHeader(std::istream& in);
 
        bool IsBinarySTL(size_t fileSize);
 
+       void WriteAscciiVert(std::ostream& out, const char* head, const lm::vec3f& v) const;
+
 public:
        char m_Message[MSG_LENGTH];
        std::vector<StlFace> m_Faces;