ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkSTLMeshIO.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkSTLMeshIO_h
19#define itkSTLMeshIO_h
20
21#include "IOMeshSTLExport.h"
22
23#include "itkMeshIOBase.h"
24
25#include <fstream>
26#include <set>
27
28namespace itk
29{
38class IOMeshSTL_EXPORT STLMeshIO : public MeshIOBase
39{
40public:
41 ITK_DISALLOW_COPY_AND_MOVE(STLMeshIO);
42
44 using Self = STLMeshIO;
48
50 itkNewMacro(Self);
51
53 itkOverrideGetNameOfClassMacro(STLMeshIO);
54
56
62 bool
63 CanReadFile(const char * fileName) override;
64
66 virtual void
68
70 void
72
74 void
75 ReadPoints(void * buffer) override;
76
78 void
79 ReadCells(void * buffer) override;
80
82 bool
83 GetUpdatePoints() const override;
84
86 bool
87 GetUpdateCells() const override;
88
92 void
93 ReadPointData(void * itkNotUsed(buffer)) override {};
94 void
95 ReadCellData(void * itkNotUsed(buffer)) override {};
96
97 /*-------- This part of the interfaces deals with writing data. ----- */
103 bool
104 CanWriteFile(const char * fileName) override;
105
107 void
109
111 void
112 Write() override;
113
120 void
121 WritePoints(void * buffer) override;
122
139 void
140 WriteCells(void * buffer) override;
141
145 void
146 WritePointData(void * itkNotUsed(buffer)) override {};
147 void
148 WriteCellData(void * itkNotUsed(buffer)) override {};
149
150protected:
152 ~STLMeshIO() override = default;
153
154 void
155 PrintSelf(std::ostream & os, Indent indent) const override;
156
159 template <typename TPointsBuffer>
160 void
161 WritePointsTyped(const TPointsBuffer * const buffer)
162 {
163
164 const unsigned int pointDimension = this->GetPointDimension();
165
166 if (pointDimension != 3)
167 {
168 itkExceptionMacro("STL only supports 3D points");
169 }
170
171 const TPointsBuffer * pointCoordinates = buffer;
172
173 this->m_Points.clear();
174
175 const IdentifierType numberOfPoints = this->GetNumberOfPoints();
176
177 this->m_Points.resize(numberOfPoints);
178
179 for (IdentifierType pi = 0; pi < numberOfPoints; ++pi)
180 {
181 for (unsigned int i = 0; i < pointDimension; ++i)
182 {
183 m_Points[pi][i] = static_cast<PointValueType>(*pointCoordinates++);
184 }
185 }
186 }
187
188
189private:
190 std::ofstream m_OutputStream; // output file
191 std::ifstream m_InputStream; // input file
192
193 std::string m_InputLine; // helper during reading
194
195 using PointValueType = float; // type to represent point coordinates
196
200
201 using PointContainerType = std::vector<PointType>;
202
204 void
205 WriteInt32AsBinary(int32_t value);
206 void
207 WriteUInt32AsBinary(uint32_t value);
208 void
209 WriteInt16AsBinary(int16_t value);
210 void
212 void
214
216 void
218 void
220
222 void
223 ReadInt32AsBinary(int32_t & value);
224 void
225 ReadUInt32AsBinary(uint32_t & value);
226 void
227 ReadInt16AsBinary(int16_t & value);
228 void
230 void
232
234 void
235 ReadStringFromAscii(const std::string & expected);
236 void
238 bool
239 CheckStringFromAscii(const std::string & expected);
240
242 virtual void
243 WriteCellsAsAscii(void * buffer);
244 virtual void
245 WriteCellsAsBinary(void * buffer);
246
248 void
250
252
253 unsigned int m_InputLineNumber;
254
256 {
257 bool
258 operator()(const PointType & p1, const PointType & p2) const
259 {
260 if (p1[0] != p2[0])
261 {
262 return p1[0] < p2[0];
263 }
264 else
265 {
266 if (p1[1] != p2[1])
267 {
268 return p1[1] < p2[1];
269 }
270 }
271
272 return p1[2] < p2[2];
273 }
274 };
275
276 using PointsMapType = std::map<PointType, IdentifierType, PointCompare>;
277
279
280 // Helper variable to put Ids in points as they are read
282
283 // Triplet to hold the Ids of points in a triagle as they are being read
291
294
295 using CellsVectorType = std::vector<TripletType>;
297};
298} // end namespace itk
299
300#endif // itkSTLMeshIO_h
A templated class holding a n-Dimensional covariant vector.
Control indentation during Print() invocation.
Definition itkIndent.h:53
virtual unsigned int GetPointDimension() const
virtual SizeValueType GetNumberOfPoints() const
A templated class holding a geometric point in n-Dimensional space.
Definition itkPoint.h:54
void ReadPointData(void *buffer) override
void PrintSelf(std::ostream &os, Indent indent) const override
std::map< PointType, IdentifierType, PointCompare > PointsMapType
virtual void WriteCellsAsAscii(void *buffer)
void ReadPointAsAscii(PointType &point)
void WriteUInt32AsBinary(uint32_t value)
bool CheckStringFromAscii(const std::string &expected)
std::string m_InputLine
virtual void Read()
void WriteCellData(void *buffer) override
PointContainerType m_Points
void WriteCells(void *buffer) override
std::ifstream m_InputStream
void ReadMeshInternalFromBinary()
void WriteMeshInformation() override
void ReadCellData(void *buffer) override
SmartPointer< const Self > ConstPointer
Vector< PointValueType, 3 > VectorType
SmartPointer< Self > Pointer
void InsertPointIntoSet(const PointType &point)
void ReadPointAsBinary(PointType &point)
STLMeshIO Self
void WritePointsTyped(const TPointsBuffer *const buffer)
std::ofstream m_OutputStream
unsigned int m_InputLineNumber
void WriteInt32AsBinary(int32_t value)
MeshIOBase Superclass
CellsVectorType m_CellsVector
bool GetUpdateCells() const override
~STLMeshIO() override=default
std::vector< TripletType > CellsVectorType
Point< PointValueType, 3 > PointType
void ReadUInt32AsBinary(uint32_t &value)
void ReadStringFromAscii(const std::string &expected)
void WriteInt16AsBinary(int16_t value)
CovariantVector< PointValueType, 3 > NormalType
void ReadCells(void *buffer) override
TripletType m_TrianglePointIds
bool CanReadFile(const char *fileName) override
IdentifierType m_LatestPointId
bool CanWriteFile(const char *fileName) override
void ReadPoints(void *buffer) override
void WritePoints(void *buffer) override
void ReadMeshInformation() override
void ReadInt16AsBinary(int16_t &value)
void ReadMeshInternalFromAscii()
unsigned int m_PointInTriangleCounter
void WriteNormalAsBinary(const NormalType &normal)
void WritePointData(void *buffer) override
void ReadInt32AsBinary(int32_t &value)
void WritePointAsBinary(const PointType &point)
virtual void WriteCellsAsBinary(void *buffer)
void ReadNormalAsBinary(NormalType &normal)
std::vector< PointType > PointContainerType
bool GetUpdatePoints() const override
PointsMapType m_PointsMap
void Write() override
Implements transparent reference counting.
A templated class holding a n-Dimensional vector.
Definition itkVector.h:63
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
SizeValueType IdentifierType
Definition itkIntTypes.h:95
bool operator()(const PointType &p1, const PointType &p2) const