ITK  5.2.0
Insight Toolkit
itkOFFMeshIO.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  * 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 
19 #ifndef itkOFFMeshIO_h
20 #define itkOFFMeshIO_h
21 #include "ITKIOMeshOFFExport.h"
22 
23 #include "itkMeshIOBase.h"
24 
25 #include <fstream>
26 
27 namespace itk
28 {
36 class ITKIOMeshOFF_EXPORT OFFMeshIO : public MeshIOBase
37 {
38 public:
39  ITK_DISALLOW_COPY_AND_MOVE(OFFMeshIO);
40 
42  using Self = OFFMeshIO;
46 
49 
51  itkNewMacro(Self);
52 
54  itkTypeMacro(OFFMeshIO, MeshIOBase);
55 
56  /*-------- This part of the interfaces deals with reading data. ----- */
57 
63  bool
64  CanReadFile(const char * fileName) override;
65 
67  void
68  ReadMeshInformation() override;
69 
71  void
72  ReadPoints(void * buffer) override;
73 
74  void
75  ReadCells(void * buffer) override;
76 
77  void
78  ReadPointData(void * buffer) override;
79 
80  void
81  ReadCellData(void * buffer) override;
82 
83  /*-------- This part of the interfaces deals with writing data. ----- */
84 
90  bool
91  CanWriteFile(const char * fileName) override;
92 
94  void
95  WriteMeshInformation() override;
96 
99  void
100  WritePoints(void * buffer) override;
101 
102  void
103  WriteCells(void * buffer) override;
104 
105  void
106  WritePointData(void * buffer) override;
107 
108  void
109  WriteCellData(void * buffer) override;
110 
111  void
112  Write() override;
113 
114 protected:
116  template <typename T>
117  void
118  ReadCellsBufferAsAscii(T * buffer, std::ifstream & inputFile)
119  {
120  SizeValueType index = 0;
121  unsigned int numberOfPoints = 0;
122  std::string line;
123 
124  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++)
125  {
126  inputFile >> numberOfPoints;
127  buffer[index++] = static_cast<T>(numberOfPoints);
128  for (unsigned int jj = 0; jj < numberOfPoints; jj++)
129  {
130  inputFile >> buffer[index++];
131  }
132  std::getline(inputFile, line, '\n');
133  }
134  }
135 
139  template <typename TInput, typename TOutput>
140  void
141  ReadCellsBuffer(TInput * input, TOutput * output)
142  {
143  if (input && output)
144  {
145  SizeValueType indInput = 0;
146  SizeValueType indOutput = 0;
147  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++)
148  {
149  indInput++; // ignore the cell type
150  auto numberOfPoints = static_cast<unsigned int>(input[indInput++]);
151  output[indOutput++] = static_cast<TOutput>(numberOfPoints);
152  for (unsigned int jj = 0; jj < numberOfPoints; jj++)
153  {
154  output[indOutput++] = static_cast<TOutput>(input[indInput++]);
155  }
156  }
157  }
158  }
160 
161  template <typename T>
162  void
163  WriteCellsAsAscii(T * buffer, std::ofstream & outputFile)
164  {
165  SizeValueType index = 0;
166 
167  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++)
168  {
169  index++;
170  auto numberOfCellPoints = static_cast<unsigned int>(buffer[index++]);
171  outputFile << numberOfCellPoints << " ";
172 
173  for (unsigned int jj = 0; jj < numberOfCellPoints; jj++)
174  {
175  outputFile << buffer[index++] << " ";
176  }
177 
178  outputFile << '\n';
179  }
180  }
181 
182  template <typename TOutput, typename TInput>
183  void
184  WriteCellsAsBinary(TInput * buffer, std::ofstream & outputFile)
185  {
186  auto * data = new TOutput[m_CellBufferSize - this->m_NumberOfCells];
187 
188  ReadCellsBuffer(buffer, data);
189  WriteBufferAsBinary<TOutput>(data, outputFile, m_CellBufferSize - this->m_NumberOfCells);
190 
191  delete[] data;
192  }
193 
194 protected:
195  OFFMeshIO();
196  ~OFFMeshIO() override;
197 
198  void
199  PrintSelf(std::ostream & os, Indent indent) const override;
200 
201  void
202  OpenFile();
203 
204  void
205  CloseFile();
206 
207 private:
208  std::ifstream m_InputFile;
209  StreamOffsetType m_PointsStartPosition; // file position for points rlative to std::ios::beg
210  bool m_TriangleCellType; // if all cells are trinalge it is true. otherwise, it is false.
211 };
212 } // end namespace itk
213 
214 #endif
itk::OFFMeshIO::m_PointsStartPosition
StreamOffsetType m_PointsStartPosition
Definition: itkOFFMeshIO.h:209
itk::MeshIOBase::SizeValueType
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:88
itk::OFFMeshIO::ReadCellsBufferAsAscii
void ReadCellsBufferAsAscii(T *buffer, std::ifstream &inputFile)
Definition: itkOFFMeshIO.h:118
itk::MeshIOBase
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:71
itk::OFFMeshIO::WriteCellsAsAscii
void WriteCellsAsAscii(T *buffer, std::ofstream &outputFile)
Definition: itkOFFMeshIO.h:163
itk::OFFMeshIO::ReadCellsBuffer
void ReadCellsBuffer(TInput *input, TOutput *output)
Definition: itkOFFMeshIO.h:141
itk::OFFMeshIO
this class defines how to read and write Object file format.
Definition: itkOFFMeshIO.h:36
itk::SmartPointer< const Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::OFFMeshIO::m_TriangleCellType
bool m_TriangleCellType
Definition: itkOFFMeshIO.h:210
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::OFFMeshIO::m_InputFile
std::ifstream m_InputFile
Definition: itkOFFMeshIO.h:208
itkMeshIOBase.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::OFFMeshIO::WriteCellsAsBinary
void WriteCellsAsBinary(TInput *buffer, std::ofstream &outputFile)
Definition: itkOFFMeshIO.h:184
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::MeshIOBase::StreamOffsetType
std::streamoff StreamOffsetType
Definition: itkMeshIOBase.h:86
fileName
std::string fileName
Definition: itkTestDriverInclude.h:118