4 #include "../BaseMesh.h"
6 #include <C2/util/stream_util.h>
16 StlMesh::StlMesh(void)
21 void StlMesh::Clear(void)
27 void StlMesh::ClearMessage(void)
29 for (int i = 0; i < MSG_LENGTH; ++i)
35 bool StlMesh::LoadBinary(std::istream& in)
39 size_t fileSize = util::GetStreamSize(in);
41 in.read(m_Message, MSG_LENGTH);
43 unsigned int NumFaces;
44 in.read((char*)&NumFaces, sizeof(unsigned int));
46 const size_t expectedBinaryFileSize = NumFaces * 50 + 84;
48 if (expectedBinaryFileSize != fileSize)
51 m_Faces.resize(NumFaces);
52 for (size_t i = 0; i < NumFaces; ++i)
54 char padding[2] = {0, 0};
56 StlFace& f = m_Faces[i];
57 in.read((char*)f.m_Normal.v(), sizeof(float) * 3);
58 in.read((char*)f.m_Verts[0].v(), sizeof(float) * 3);
59 in.read((char*)f.m_Verts[1].v(), sizeof(float) * 3);
60 in.read((char*)f.m_Verts[2].v(), sizeof(float) * 3);
67 bool StlMesh::LoadAscii(std::istream& in)
69 if (!IsExistValidAsciiHeader(in))
74 in.seekg(0, std::ios::beg);
87 std::istringstream is(ws);
93 lm::vec3f o(0.0f, 0.0f, 0.0f);
100 lm::vec3f& n = f.m_Normal;
101 is >> n.x >> n.y >> n.z;
103 else if (s == "endloop")
105 m_Faces.push_back(f);
107 else if (s == "vertex")
109 lm::vec3f& v = f.m_Verts[vcount++];
110 is >> v.x >> v.y >> v.z;
112 else if (s == "endfacet")
115 else if (s == "outer")
118 else if (s == "endsolid")
130 bool StlMesh::IsExistValidAsciiHeader(std::istream& in)
132 in.read(m_Message, MSG_LENGTH);
135 std::string top = m_Message;
138 return top == "solid";
141 bool StlMesh::Load(std::istream& ist)
146 ist.seekg(0, std::ios::beg);
154 bool StlMesh::Load(const std::string& filename)
156 std::ifstream ifs(filename.c_str(), std::ios::binary);
163 bool StlMesh::SaveBinary(std::ostream& ost) const
165 unsigned int NumFaces = (unsigned int)m_Faces.size();
166 ost.write(m_Message, MSG_LENGTH);
167 ost.write((char*)&NumFaces, sizeof(unsigned int));
169 char padding[2] = {0, 0};
171 for (const StlFace& f : m_Faces)
173 ost.write((char*)f.m_Normal.v(), sizeof(float) * 3);
174 ost.write((char*)f.m_Verts[0].v(), sizeof(float) * 3);
175 ost.write((char*)f.m_Verts[1].v(), sizeof(float) * 3);
176 ost.write((char*)f.m_Verts[2].v(), sizeof(float) * 3);
177 ost.write(padding, 2);
183 bool StlMesh::SaveBinary(const std::string& filename) const
185 std::ofstream ofs(filename.c_str(), std::ios::binary);
189 return SaveBinary(ofs);
192 bool StlMesh::SaveAscii(std::ostream& ost) const
194 ost << "solid " << m_Message << std::endl;
196 for (const StlFace& f : m_Faces)
198 WriteAscciiVert(ost, "facet normal", f.m_Normal);
199 ost << "outer loop" << std::endl;
200 WriteAscciiVert(ost, "vertex", f.m_Verts[0]);
201 WriteAscciiVert(ost, "vertex", f.m_Verts[1]);
202 WriteAscciiVert(ost, "vertex", f.m_Verts[2]);
203 ost << "endloop" << std::endl;
204 ost << "endfacet" << std::endl;
207 ost << "endsolid" << std::endl;
212 bool StlMesh::SaveAscii(const std::string& filename) const
214 std::ofstream ofs(filename.c_str());
218 return SaveAscii(ofs);
221 void StlMesh::WriteAscciiVert(std::ostream& out, const char* head, const lm::vec3f& v) const
230 bool StlMesh::ConvertToBaseMesh(BaseMesh& bm) const
234 bm.m_Verts.resize(m_Faces.size() * 3);
235 bm.m_Normals.resize(m_Faces.size());
236 bm.m_Faces.resize(m_Faces.size());
238 for (size_t i = 0; i < m_Faces.size(); ++i)
240 const StlFace& f = m_Faces[i];
242 bm.m_Verts[i * 3 + 0] = f.m_Verts[0];
243 bm.m_Verts[i * 3 + 1] = f.m_Verts[1];
244 bm.m_Verts[i * 3 + 2] = f.m_Verts[2];
246 bm.m_Normals[i] = f.m_Normal;
248 BaseFace& bf = bm.m_Faces[i];
249 bf.m_VertIds.resize(3);
250 bf.m_NormIds.resize(3);
252 bf.m_VertIds[0] = (int)(i * 3 + 0);
253 bf.m_VertIds[1] = (int)(i * 3 + 1);
254 bf.m_VertIds[2] = (int)(i * 3 + 2);
256 bf.m_NormIds[0] = (int)i;
257 bf.m_NormIds[1] = (int)i;
258 bf.m_NormIds[2] = (int)i;
264 //!
\83T
\83C
\83Y0
\82Ì
\96@
\90ü
\82ð
\8aÜ
\82Þ
\8fê
\8d\87\82Étrue
\82ð
\95Ô
\82·
265 bool StlMesh::ExistZeroLengthNormal(void) const
267 for (const StlFace& f : m_Faces)
269 if (f.m_Normal.is_zero())