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