ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkOFFMeshIO.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 
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 {
35 class ITKIOMeshOFF_EXPORT OFFMeshIO:public MeshIOBase
36 {
37 public:
38  ITK_DISALLOW_COPY_AND_ASSIGN(OFFMeshIO);
39 
41  using Self = OFFMeshIO;
45 
48 
50  itkNewMacro(Self);
51 
53  itkTypeMacro(OFFMeshIO, MeshIOBase);
54 
55  /*-------- This part of the interfaces deals with reading data. ----- */
56 
62  bool CanReadFile(const char *FileNameToRead) override;
63 
65  void ReadMeshInformation() override;
66 
68  void ReadPoints(void *buffer) override;
69 
70  void ReadCells(void *buffer) override;
71 
72  void ReadPointData(void *buffer) override;
73 
74  void ReadCellData(void *buffer) override;
75 
76  /*-------- This part of the interfaces deals with writing data. ----- */
77 
83  bool CanWriteFile(const char *FileNameToWrite) override;
84 
86  void WriteMeshInformation() override;
87 
90  void WritePoints(void *buffer) override;
91 
92  void WriteCells(void *buffer) override;
93 
94  void WritePointData(void *buffer) override;
95 
96  void WriteCellData(void *buffer) override;
97 
98  void Write() override;
99 
100 protected:
102  template< typename T >
103  void ReadCellsBufferAsAscii(T *buffer, std::ifstream & inputFile)
104  {
105  SizeValueType index = 0;
106  unsigned int numberOfPoints = 0;
107  std::string line;
108 
109  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
110  {
111  inputFile >> numberOfPoints;
112  buffer[index++] = static_cast< T >( numberOfPoints );
113  for ( unsigned int jj = 0; jj < numberOfPoints; jj++ )
114  {
115  inputFile >> buffer[index++];
116  }
117  std::getline(inputFile, line, '\n');
118  }
119  }
120 
124  template< typename TInput, typename TOutput >
125  void ReadCellsBuffer(TInput *input, TOutput *output)
126  {
127  if ( input && output )
128  {
129  SizeValueType indInput = 0;
130  SizeValueType indOutput = 0;
131  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
132  {
133  indInput++; // ignore the cell type
134  auto numberOfPoints = static_cast< unsigned int >( input[indInput++] );
135  output[indOutput++] = static_cast< TOutput >( numberOfPoints );
136  for ( unsigned int jj = 0; jj < numberOfPoints; jj++ )
137  {
138  output[indOutput++] = static_cast< TOutput >( input[indInput++] );
139  }
140  }
141  }
142  }
144 
145  template< typename T >
146  void WriteCellsAsAscii(T *buffer, std::ofstream & outputFile)
147  {
148  SizeValueType index = 0;
149 
150  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
151  {
152  index++;
153  auto numberOfCellPoints = static_cast< unsigned int >( buffer[index++] );
154  outputFile << numberOfCellPoints << " ";
155 
156  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
157  {
158  outputFile << buffer[index++] << " ";
159  }
160 
161  outputFile << '\n';
162  }
163  }
164 
165  template< typename TOutput, typename TInput >
166  void WriteCellsAsBinary(TInput *buffer, std::ofstream & outputFile)
167  {
168  auto * data = new TOutput[m_CellBufferSize - this->m_NumberOfCells];
169 
170  ReadCellsBuffer(buffer, data);
171  WriteBufferAsBinary< TOutput >(data, outputFile, m_CellBufferSize - this->m_NumberOfCells);
172 
173  delete[] data;
174  }
175 
176 protected:
177  OFFMeshIO();
178  ~OFFMeshIO() override;
179 
180  void PrintSelf(std::ostream & os, Indent indent) const override;
181 
182  void OpenFile();
183 
184  void CloseFile();
185 
186 private:
187  std::ifstream m_InputFile;
188  StreamOffsetType m_PointsStartPosition; // file position for points rlative to std::ios::beg
189  bool m_TriangleCellType; // if all cells are trinalge it is true. otherwise, it is false.
190 };
191 } // end namespace itk
192 
193 #endif
void ReadCellsBuffer(TInput *input, TOutput *output)
Definition: itkOFFMeshIO.h:125
void ReadCellsBufferAsAscii(T *buffer, std::ifstream &inputFile)
Definition: itkOFFMeshIO.h:103
Light weight base class for most itk classes.
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:86
StreamOffsetType m_PointsStartPosition
Definition: itkOFFMeshIO.h:188
unsigned long SizeValueType
Definition: itkIntTypes.h:83
this class defines how to read and write Object file format.
Definition: itkOFFMeshIO.h:35
void WriteCellsAsAscii(T *buffer, std::ofstream &outputFile)
Definition: itkOFFMeshIO.h:146
bool m_TriangleCellType
Definition: itkOFFMeshIO.h:189
std::streamoff StreamOffsetType
Definition: itkMeshIOBase.h:84
std::ifstream m_InputFile
Definition: itkOFFMeshIO.h:187
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:69
Base class for most ITK classes.
Definition: itkObject.h:60
void WriteCellsAsBinary(TInput *buffer, std::ofstream &outputFile)
Definition: itkOFFMeshIO.h:166