Loading...
Searching...
No Matches
STL.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#ifndef __BITPIT_STL_HPP__
26#define __BITPIT_STL_HPP__
27
28#include <array>
29#include <vector>
30#include <string>
31#include <sstream>
32#include <fstream>
33#include <iostream>
34
35#include "line_stream.hpp"
36
37namespace bitpit {
38
39class STLBase {
40
41public:
42 enum Format {
43 FormatUnknown = -1,
44 FormatASCII,
45 FormatBinary
46 };
47
48 virtual ~STLBase() = default;
49
50 const std::string & getFilename() const;
51 Format getFormat() const;
52
53protected:
54 typedef uint8_t BINARY_UINT8;
55 typedef uint16_t BINARY_UINT16;
56 typedef uint32_t BINARY_UINT32;
57 typedef float BINARY_REAL32;
58
59 static const std::size_t BINARY_HEADER_SIZE;
60 static const std::size_t BINARY_MINIMUM_SIZE;
61 static const std::size_t BINARY_FACET_SIZE;
62
63 static const std::string ASCII_SOLID_BEGIN;
64 static const std::string ASCII_SOLID_END;
65 static const std::string ASCII_FACET_BEGIN;
66 static const std::string ASCII_FACET_END;
67 static const std::string ASCII_FILE_BEGIN;
68 static const std::string ASCII_FILE_END;
69 static const std::size_t ASCII_MINIMUM_SIZE;
70
71 STLBase(const std::string &filename);
72 STLBase(const std::string &filename, Format format);
73
74 void setFilename(const std::string &filename);
75 void setFormat(Format format);
76
77private:
78 std::string m_filename;
79 Format m_format;
80
81};
82
83class STLReader : public STLBase {
84
85public:
92 int nSolids;
94 std::vector<bool> solidValid;
95 std::vector<std::array<bool, 6>> solidErrors;
97 std::vector<std::string> solidNames;
98 std::vector<std::size_t> solidFacetCount;
99 std::vector<std::size_t> solidVertexCount;
100 };
101
102 static Format detectFormat(const std::string &filename);
103
104 STLReader(const std::string &filename, Format format = FormatUnknown);
105
106 int inspect(InspectionInfo *info);
107 void displayInspectionInfo(const InspectionInfo &info, std::ostream &out) const;
108
109 int readBegin();
110 int readEnd();
111
112 int readSolid(std::string *name, std::size_t *nV, std::size_t *nT,
113 std::vector<std::array<double, 3>> *V, std::vector<std::array<double, 3>> *N,
114 std::vector<std::array<std::size_t, 3>> *T);
115
116 int readSolid(const std::string &solid, std::string *name, std::size_t *nV, std::size_t *nT,
117 std::vector<std::array<double, 3>> *V, std::vector<std::array<double, 3>> *N,
118 std::vector<std::array<std::size_t, 3>> *T);
119
120 int readHeader(std::string *name, std::size_t *nT);
121 int readHeader(const std::string &solid, std::string *name, std::size_t *nT);
122
123 int readFooter();
124 int readFooter(const std::string &solid);
125
126 int readFacet(std::array<double, 3> *V0, std::array<double, 3> *V1,
127 std::array<double, 3> *V2, std::array<double, 3> *N);
128
129private:
130 std::ifstream m_fileHandle;
132 LineStream m_lineStream;
134 int inspectASCII(InspectionInfo *info);
135 int inspectSolidASCII(std::size_t *nFactes, std::array<bool, 6> *errors);
136 int inspectFacetASCII(std::array<bool, 6> *errors);
137
138 int inspectBinary(InspectionInfo *info);
139
140 int readHeaderASCII(const std::string &solid, std::string *name, std::size_t *nT);
141
142 int readFooterASCII(const std::string &solid);
143
144 int readFacetASCII(std::array<double, 3> *V0, std::array<double, 3> *V1,
145 std::array<double, 3> *V2, std::array<double, 3> *N);
146
147 int readHeaderBinary(std::string *name, std::size_t *nT);
148
149 int readFooterBinary();
150
151 int readFacetBinary(std::array<double, 3> *V0, std::array<double, 3> *V1,
152 std::array<double, 3> *V2, std::array<double, 3> *N);
153
154};
155
156class STLWriter : public STLBase {
157
158public:
159 enum WriteMode {
160 WriteOverwrite,
161 WriteAppend
162 };
163
164 STLWriter(const std::string &filename, Format format);
165
166 int writeBegin(WriteMode writeMode, bool partialWrite = false);
167 int writeEnd();
168
169 int writeSolid(const std::string &name, std::size_t nV, std::size_t nT,
170 const std::vector<std::vector<double>> &V, const std::vector<std::vector<double>> &N,
171 const std::vector<std::vector<std::size_t>> &T);
172
173 int writeSolid(const std::string &name, std::size_t nV, std::size_t nT,
174 const std::vector<std::array<double, 3>> &V, const std::vector<std::array<double, 3>> &N,
175 const std::vector<std::array<std::size_t, 3>> &T);
176
177 int writeHeader(const std::string &name, std::size_t nT);
178
179 int writeFooter(const std::string &name);
180
181 int writeFacet(const std::array<double, 3> &V0, const std::array<double, 3> &V1,
182 const std::array<double, 3> &V2, const std::array<double, 3> &N);
183
184private:
185 std::ofstream m_fileHandle;
187 int writeHeaderASCII(const std::string &name, std::size_t nT);
188
189 int writeFooterASCII(const std::string &name);
190
191 int writeFacetASCII(const std::array<double, 3> &V0, const std::array<double, 3> &V1,
192 const std::array<double, 3> &V2, const std::array<double, 3> &N);
193
194 int writeHeaderBinary(const std::string &name, std::size_t nT);
195
196 int writeFooterBinary(const std::string &name);
197
198 int writeFacetBinary(const std::array<double, 3> &V0, const std::array<double, 3> &V1,
199 const std::array<double, 3> &V2, const std::array<double, 3> &N);
200
201};
202
203}
204
205#endif
LineStream defines a stream for reading lines.
Base class for the STL writer and the STL reader.
Definition STL.hpp:39
void setFormat(Format format)
Definition STL.cpp:121
void setFilename(const std::string &filename)
Definition STL.cpp:101
STLBase(const std::string &filename)
Definition STL.cpp:68
Format getFormat() const
Definition STL.cpp:111
const std::string & getFilename() const
Definition STL.cpp:91
Class for reading ASCII and binary STL files.
Definition STL.hpp:83
int readFacet(std::array< double, 3 > *V0, std::array< double, 3 > *V1, std::array< double, 3 > *V2, std::array< double, 3 > *N)
Definition STL.cpp:942
int readBegin()
Definition STL.cpp:714
int readFooter()
Definition STL.cpp:896
int inspect(InspectionInfo *info)
Definition STL.cpp:307
void displayInspectionInfo(const InspectionInfo &info, std::ostream &out) const
Definition STL.cpp:656
int readHeader(std::string *name, std::size_t *nT)
Definition STL.cpp:845
STLReader(const std::string &filename, Format format=FormatUnknown)
Definition STL.cpp:137
int readSolid(std::string *name, std::size_t *nV, std::size_t *nT, std::vector< std::array< double, 3 > > *V, std::vector< std::array< double, 3 > > *N, std::vector< std::array< std::size_t, 3 > > *T)
Definition STL.cpp:759
static Format detectFormat(const std::string &filename)
Definition STL.cpp:167
Class for writing ASCII and binary STL files.
Definition STL.hpp:156
int writeFacet(const std::array< double, 3 > &V0, const std::array< double, 3 > &V1, const std::array< double, 3 > &V2, const std::array< double, 3 > &N)
Definition STL.cpp:1542
int writeFooter(const std::string &name)
Definition STL.cpp:1515
int writeHeader(const std::string &name, std::size_t nT)
Definition STL.cpp:1491
STLWriter(const std::string &filename, Format format)
Definition STL.cpp:1346
int writeBegin(WriteMode writeMode, bool partialWrite=false)
Definition STL.cpp:1362
Structure holding inspection information.
Definition STL.hpp:91
std::vector< std::string > solidNames
Definition STL.hpp:97
std::vector< std::size_t > solidFacetCount
Definition STL.hpp:98
std::vector< std::size_t > solidVertexCount
Definition STL.hpp:99
std::vector< std::array< bool, 6 > > solidErrors
Definition STL.hpp:95
std::vector< bool > solidValid
Definition STL.hpp:94
--- layout: doxygen_footer ---