ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkSTLMeshIO.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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  * http://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 "IOSTLExport.h"
22 
23 #include "itkMeshIOBase.h"
24 
25 #include <fstream>
26 #include <set>
27 
28 namespace itk
29 {
38 class IOSTL_EXPORT STLMeshIO : public MeshIOBase
39 {
40 public:
41  ITK_DISALLOW_COPY_AND_ASSIGN(STLMeshIO);
42 
44  using Self = STLMeshIO;
48 
50  itkNewMacro(Self);
51 
53  itkTypeMacro(STLMeshIO, MeshIOBase);
54 
62  bool CanReadFile(const char *FileNameToRead) override;
63 
65  virtual void Read();
66 
68  void ReadMeshInformation() override;
69 
71  void ReadPoints(void *buffer) override;
72 
74  void ReadCells(void *buffer) override;
75 
77  bool GetUpdatePoints() const override;
78 
80  bool GetUpdateCells() const override;
81 
85  void ReadPointData( void * itkNotUsed(buffer) ) override {};
86  void ReadCellData( void * itkNotUsed(buffer) ) override {};
88 
89  /*-------- This part of the interfaces deals with writing data. ----- */
95  bool CanWriteFile(const char *FileNameToWrite) override;
96 
98  void WriteMeshInformation() override;
99 
101  void Write() override;
102 
109  void WritePoints( void * buffer ) override;
110 
127  void WriteCells(void *buffer) override;
128 
132  void WritePointData( void * itkNotUsed(buffer) ) override {};
133  void WriteCellData( void * itkNotUsed(buffer) ) override {};
135 
136 protected:
137  STLMeshIO();
138  ~STLMeshIO() override {}
139 
140  void PrintSelf(std::ostream & os, Indent indent) const override;
141 
144  template< typename TPointsBuffer >
145  void WritePointsTyped( const TPointsBuffer * const buffer )
146  {
147 
148  const unsigned int pointDimension = this->GetPointDimension();
149 
150  if ( pointDimension != 3 )
151  {
152  itkExceptionMacro("STL only supports 3D points");
153  }
154 
155  const TPointsBuffer * pointCoordinates = buffer;
156 
157  this->m_Points.clear();
158 
159  const IdentifierType numberOfPoints = this->GetNumberOfPoints();
160 
161  this->m_Points.resize(numberOfPoints);
162 
163  for( IdentifierType pi = 0; pi < numberOfPoints; ++pi )
164  {
165  for( unsigned int i = 0; i < pointDimension; ++i )
166  {
167  m_Points[pi][i] = static_cast< PointValueType >( *pointCoordinates++ );
168  }
169  }
170 
171  }
172 
173 
174 private:
175  std::ofstream m_OutputStream; // output file
176  std::ifstream m_InputStream; // input file
177 
178  std::string m_InputLine; // helper during reading
179 
180  using PointValueType = float; // type to represent point coordinates
181 
185 
186  using PointContainerType = std::vector< PointType >;
187 
189  void WriteInt32AsBinary(int32_t value);
190  void WriteInt16AsBinary(int16_t value);
191  void WriteNormalAsBinary(const NormalType & normal);
192  void WritePointAsBinary(const PointType & point);
194 
196  void ReadMeshInternalFromAscii();
197  void ReadMeshInternalFromBinary();
199 
201  void ReadInt32AsBinary(int32_t & value);
202  void ReadInt16AsBinary(int16_t & value);
203  void ReadNormalAsBinary(NormalType & normal);
204  void ReadPointAsBinary(PointType & point);
206 
208  void ReadStringFromAscii(const std::string & keyword);
209  void ReadPointAsAscii(PointType & point);
210  bool CheckStringFromAscii(const std::string & expected);
212 
214  virtual void WriteCellsAsAscii(void *buffer);
215  virtual void WriteCellsAsBinary(void *buffer);
217 
219  void InsertPointIntoSet( const PointType & point );
220 
222 
223  unsigned int m_InputLineNumber;
224 
226  {
227  bool operator()(const PointType & p1, const PointType & p2) const
228  {
229  if( p1[0] != p2[0] )
230  {
231  return p1[0] < p2[0];
232  }
233  else
234  {
235  if( p1[1] != p2[1] )
236  {
237  return p1[1] < p2[1];
238  }
239  }
240 
241  return p1[2] < p2[2];
242  }
243  };
244 
245  using PointsMapType = std::map< PointType, IdentifierType, PointCompare >;
246 
248 
249  // Helper variable to put Ids in points as they are read
251 
252  // Triplet to hold the Ids of points in a triagle as they are being read
254  {
255  public:
259  };
260 
263 
264  using CellsVectorType = std::vector< TripletType >;
266 
267 };
268 } // end namespace itk
269 
270 #endif // itkSTLMeshIO_h
void ReadCellData(void *) override
Definition: itkSTLMeshIO.h:86
Light weight base class for most itk classes.
void WritePointsTyped(const TPointsBuffer *const buffer)
Definition: itkSTLMeshIO.h:145
unsigned int m_PointInTriangleCounter
Definition: itkSTLMeshIO.h:262
unsigned int m_InputLineNumber
Definition: itkSTLMeshIO.h:223
static constexpr double pi
Definition: itkMath.h:63
PointsMapType m_PointsMap
Definition: itkSTLMeshIO.h:247
CellsVectorType m_CellsVector
Definition: itkSTLMeshIO.h:265
void ReadPointData(void *) override
Definition: itkSTLMeshIO.h:85
This class defines how to read and write STL file format.
Definition: itkSTLMeshIO.h:38
std::map< PointType, IdentifierType, PointCompare > PointsMapType
Definition: itkSTLMeshIO.h:245
void WriteCellData(void *) override
Definition: itkSTLMeshIO.h:133
PointContainerType m_Points
Definition: itkSTLMeshIO.h:221
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
IdentifierType m_LatestPointId
Definition: itkSTLMeshIO.h:250
void WritePointData(void *) override
Definition: itkSTLMeshIO.h:132
std::vector< TripletType > CellsVectorType
Definition: itkSTLMeshIO.h:264
bool operator()(const PointType &p1, const PointType &p2) const
Definition: itkSTLMeshIO.h:227
SizeValueType IdentifierType
Definition: itkIntTypes.h:87
std::ifstream m_InputStream
Definition: itkSTLMeshIO.h:176
TripletType m_TrianglePointIds
Definition: itkSTLMeshIO.h:261
float PointValueType
Definition: itkSTLMeshIO.h:180
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:69
::int32_t int32_t
Definition: itkIntTypes.h:32
std::ofstream m_OutputStream
Definition: itkSTLMeshIO.h:175
Base class for most ITK classes.
Definition: itkObject.h:60
std::string m_InputLine
Definition: itkSTLMeshIO.h:178
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:52
A templated class holding a n-Dimensional covariant vector.
std::vector< PointType > PointContainerType
Definition: itkSTLMeshIO.h:186
~STLMeshIO() override
Definition: itkSTLMeshIO.h:138
::int16_t int16_t
Definition: itkIntTypes.h:30