ITK  4.13.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:
42  typedef STLMeshIO Self;
46 
48  itkNewMacro(Self);
49 
51  itkTypeMacro(STLMeshIO, MeshIOBase);
52 
60  virtual bool CanReadFile(const char *FileNameToRead) ITK_OVERRIDE;
61 
63  virtual void Read();
64 
66  virtual void ReadMeshInformation() ITK_OVERRIDE;
67 
69  virtual void ReadPoints(void *buffer) ITK_OVERRIDE;
70 
72  virtual void ReadCells(void *buffer) ITK_OVERRIDE;
73 
75  virtual bool GetUpdatePoints() const ITK_OVERRIDE;
76 
78  virtual bool GetUpdateCells() const ITK_OVERRIDE;
79 
83  virtual void ReadPointData( void * itkNotUsed(buffer) ) ITK_OVERRIDE {};
84  virtual void ReadCellData( void * itkNotUsed(buffer) ) ITK_OVERRIDE {};
86 
87  /*-------- This part of the interfaces deals with writing data. ----- */
93  virtual bool CanWriteFile(const char *FileNameToWrite) ITK_OVERRIDE;
94 
96  virtual void WriteMeshInformation() ITK_OVERRIDE;
97 
99  virtual void Write() ITK_OVERRIDE;
100 
107  virtual void WritePoints( void * buffer ) ITK_OVERRIDE;
108 
125  virtual void WriteCells(void *buffer) ITK_OVERRIDE;
126 
130  virtual void WritePointData( void * itkNotUsed(buffer) ) ITK_OVERRIDE {};
131  virtual void WriteCellData( void * itkNotUsed(buffer) ) ITK_OVERRIDE {};
133 
134 protected:
135  STLMeshIO();
136  virtual ~STLMeshIO() {}
137 
138  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
139 
142  template< typename TPointsBuffer >
143  void WritePointsTyped( const TPointsBuffer * const buffer )
144  {
145 
146  const unsigned int pointDimension = this->GetPointDimension();
147 
148  if ( pointDimension != 3 )
149  {
150  itkExceptionMacro("STL only supports 3D points");
151  }
152 
153  const TPointsBuffer * pointCoordinates = buffer;
154 
155  this->m_Points.clear();
156 
157  const IdentifierType numberOfPoints = this->GetNumberOfPoints();
158 
159  this->m_Points.resize(numberOfPoints);
160 
161  for( IdentifierType pi = 0; pi < numberOfPoints; ++pi )
162  {
163  for( unsigned int i = 0; i < pointDimension; ++i )
164  {
165  m_Points[pi][i] = static_cast< PointValueType >( *pointCoordinates++ );
166  }
167  }
168 
169  }
170 
171 
172 private:
173  ITK_DISALLOW_COPY_AND_ASSIGN(STLMeshIO);
174 
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  typedef float PointValueType; // type to represent point coordinates
181 
185 
186  typedef std::vector< PointType > PointContainerType;
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  typedef std::map< PointType, IdentifierType, PointCompare > PointsMapType;
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  typedef std::vector< TripletType > CellsVectorType;
266 
267 };
268 } // end namespace itk
269 
270 #endif // itkSTLMeshIO_h
Light weight base class for most itk classes.
STLMeshIO Self
Definition: itkSTLMeshIO.h:42
void WritePointsTyped(const TPointsBuffer *const buffer)
Definition: itkSTLMeshIO.h:143
unsigned int m_PointInTriangleCounter
Definition: itkSTLMeshIO.h:262
SmartPointer< Self > Pointer
Definition: itkSTLMeshIO.h:44
Vector< PointValueType, 3 > VectorType
Definition: itkSTLMeshIO.h:183
unsigned int m_InputLineNumber
Definition: itkSTLMeshIO.h:223
static ITK_CONSTEXPR_VAR double pi
Definition: itkMath.h:66
PointsMapType m_PointsMap
Definition: itkSTLMeshIO.h:247
CellsVectorType m_CellsVector
Definition: itkSTLMeshIO.h:265
virtual void WriteCellData(void *) override
Definition: itkSTLMeshIO.h:131
This class defines how to read and write STL file format.
Definition: itkSTLMeshIO.h:38
std::vector< TripletType > CellsVectorType
Definition: itkSTLMeshIO.h:264
Point< PointValueType, 3 > PointType
Definition: itkSTLMeshIO.h:182
std::vector< PointType > PointContainerType
Definition: itkSTLMeshIO.h:186
SizeValueType IdentifierType
Definition: itkIntTypes.h:147
PointContainerType m_Points
Definition: itkSTLMeshIO.h:221
KWIML_INT_int16_t int16_t
Definition: itkIntTypes.h:84
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
IdentifierType m_LatestPointId
Definition: itkSTLMeshIO.h:250
MeshIOBase Superclass
Definition: itkSTLMeshIO.h:43
bool operator()(const PointType &p1, const PointType &p2) const
Definition: itkSTLMeshIO.h:227
std::map< PointType, IdentifierType, PointCompare > PointsMapType
Definition: itkSTLMeshIO.h:245
std::ifstream m_InputStream
Definition: itkSTLMeshIO.h:176
TripletType m_TrianglePointIds
Definition: itkSTLMeshIO.h:261
Control indentation during Print() invocation.
Definition: itkIndent.h:49
CovariantVector< PointValueType, 3 > NormalType
Definition: itkSTLMeshIO.h:184
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:69
virtual void ReadCellData(void *) override
Definition: itkSTLMeshIO.h:84
std::ofstream m_OutputStream
Definition: itkSTLMeshIO.h:173
std::string m_InputLine
Definition: itkSTLMeshIO.h:178
KWIML_INT_int32_t int32_t
Definition: itkIntTypes.h:86
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:52
float PointValueType
Definition: itkSTLMeshIO.h:180
A templated class holding a n-Dimensional covariant vector.
SmartPointer< const Self > ConstPointer
Definition: itkSTLMeshIO.h:45
virtual ~STLMeshIO()
Definition: itkSTLMeshIO.h:136