ITK  4.8.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);
61 
63  virtual void Read();
64 
66  virtual void ReadMeshInformation();
67 
69  virtual void ReadPoints(void *buffer);
70 
72  virtual void ReadCells(void *buffer);
73 
75  virtual bool GetUpdatePoints() const;
76 
78  virtual bool GetUpdateCells() const;
79 
83  virtual void ReadPointData( void * itkNotUsed(buffer) ) {};
84  virtual void ReadCellData( void * itkNotUsed(buffer) ) {};
86 
87  /*-------- This part of the interfaces deals with writing data. ----- */
93  virtual bool CanWriteFile(const char *FileNameToWrite);
94 
96  virtual void WriteMeshInformation();
97 
99  virtual void Write();
100 
107  virtual void WritePoints( void * buffer );
108 
125  virtual void WriteCells(void *buffer);
126 
130  virtual void WritePointData( void * itkNotUsed(buffer) ) {};
131  virtual void WriteCellData( void * itkNotUsed(buffer) ) {};
133 
134 protected:
135  STLMeshIO();
136  virtual ~STLMeshIO() {}
137 
138  void PrintSelf(std::ostream & os, Indent indent) const;
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  STLMeshIO(const Self &); // purposely not implemented
174  void operator=(const Self &); // purposely not implemented
175 
176  std::ofstream m_OutputStream; // output file
177  std::ifstream m_InputStream; // input file
178 
179  std::string m_InputLine; // helper during reading
180 
181  typedef float PointValueType; // type to represent point coordinates
182 
186 
187  typedef std::vector< PointType > PointContainerType;
188 
190  void WriteInt32AsBinary(int32_t value);
191  void WriteInt16AsBinary(int16_t value);
192  void WriteNormalAsBinary(const NormalType & normal);
193  void WritePointAsBinary(const PointType & point);
195 
197  void ReadMeshInternalFromAscii();
198  void ReadMeshInternalFromBinary();
200 
202  void ReadInt32AsBinary(int32_t & value);
203  void ReadInt16AsBinary(int16_t & value);
204  void ReadNormalAsBinary(NormalType & normal);
205  void ReadPointAsBinary(PointType & point);
207 
209  void ReadStringFromAscii(const std::string & keyword);
210  void ReadPointAsAscii(PointType & point);
211  bool CheckStringFromAscii(const std::string & expected);
213 
215  virtual void WriteCellsAsAscii(void *buffer);
216  virtual void WriteCellsAsBinary(void *buffer);
218 
220  void InsertPointIntoSet( const PointType & point );
221 
223 
224  unsigned int m_InputLineNumber;
225 
227  {
228  bool operator()(const PointType & p1, const PointType & p2) const
229  {
230  if( p1[0] != p2[0] )
231  {
232  return p1[0] < p2[0];
233  }
234  else
235  {
236  if( p1[1] != p2[1] )
237  {
238  return p1[1] < p2[1];
239  }
240  }
241 
242  return p1[2] < p2[2];
243  }
244  };
245 
246  typedef std::map< PointType, IdentifierType, PointCompare > PointsMapType;
247 
249 
250  // Helper variable to put Ids in points as they are read
252 
253  // Triplet to hold the Ids of points in a triagle as they are being read
255  {
256  public:
260  };
261 
264 
265  typedef std::vector< TripletType > CellsVectorType;
267 
268 };
269 } // end namespace itk
270 
271 #endif // itkSTLMeshIO_h
virtual void ReadPointData(void *)
Definition: itkSTLMeshIO.h:83
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:263
SmartPointer< Self > Pointer
Definition: itkSTLMeshIO.h:44
Vector< PointValueType, 3 > VectorType
Definition: itkSTLMeshIO.h:184
static const double pi
Definition: itkMath.h:55
unsigned int m_InputLineNumber
Definition: itkSTLMeshIO.h:224
virtual void ReadCellData(void *)
Definition: itkSTLMeshIO.h:84
PointsMapType m_PointsMap
Definition: itkSTLMeshIO.h:248
CellsVectorType m_CellsVector
Definition: itkSTLMeshIO.h:266
This class defines how to read and write STL file format.
Definition: itkSTLMeshIO.h:38
std::vector< TripletType > CellsVectorType
Definition: itkSTLMeshIO.h:265
Point< PointValueType, 3 > PointType
Definition: itkSTLMeshIO.h:183
std::vector< PointType > PointContainerType
Definition: itkSTLMeshIO.h:187
SizeValueType IdentifierType
Definition: itkIntTypes.h:147
PointContainerType m_Points
Definition: itkSTLMeshIO.h:222
A templated class holding a n-Dimensional vector.
Definition: itkVector.h:62
IdentifierType m_LatestPointId
Definition: itkSTLMeshIO.h:251
MeshIOBase Superclass
Definition: itkSTLMeshIO.h:43
bool operator()(const PointType &p1, const PointType &p2) const
Definition: itkSTLMeshIO.h:228
std::map< PointType, IdentifierType, PointCompare > PointsMapType
Definition: itkSTLMeshIO.h:246
std::ifstream m_InputStream
Definition: itkSTLMeshIO.h:177
TripletType m_TrianglePointIds
Definition: itkSTLMeshIO.h:262
virtual void WritePointData(void *)
Definition: itkSTLMeshIO.h:130
typedef::itksysFundamentalType_Int32 int32_t
Definition: itkIntTypes.h:86
Control indentation during Print() invocation.
Definition: itkIndent.h:49
CovariantVector< PointValueType, 3 > NormalType
Definition: itkSTLMeshIO.h:185
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:69
typedef::itksysFundamentalType_Int16 int16_t
Definition: itkIntTypes.h:84
std::ofstream m_OutputStream
Definition: itkSTLMeshIO.h:176
std::string m_InputLine
Definition: itkSTLMeshIO.h:179
virtual void WriteCellData(void *)
Definition: itkSTLMeshIO.h:131
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:51
float PointValueType
Definition: itkSTLMeshIO.h:181
A templated class holding a n-Dimensional covariant vector.
SmartPointer< const Self > ConstPointer
Definition: itkSTLMeshIO.h:45
virtual ~STLMeshIO()
Definition: itkSTLMeshIO.h:136