ITK  4.13.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 "ITKIOMeshExport.h"
22 
23 #include "itkMeshIOBase.h"
24 
25 #include <fstream>
26 
27 namespace itk
28 {
35 class ITKIOMesh_EXPORT OFFMeshIO:public MeshIOBase
36 {
37 public:
39  typedef OFFMeshIO Self;
43 
46 
48  itkNewMacro(Self);
49 
51  itkTypeMacro(OFFMeshIO, MeshIOBase);
52 
53  /*-------- This part of the interfaces deals with reading data. ----- */
54 
60  virtual bool CanReadFile(const char *FileNameToRead) ITK_OVERRIDE;
61 
63  virtual void ReadMeshInformation() ITK_OVERRIDE;
64 
66  virtual void ReadPoints(void *buffer) ITK_OVERRIDE;
67 
68  virtual void ReadCells(void *buffer) ITK_OVERRIDE;
69 
70  virtual void ReadPointData(void *buffer) ITK_OVERRIDE;
71 
72  virtual void ReadCellData(void *buffer) ITK_OVERRIDE;
73 
74  /*-------- This part of the interfaces deals with writing data. ----- */
75 
81  virtual bool CanWriteFile(const char *FileNameToWrite) ITK_OVERRIDE;
82 
84  virtual void WriteMeshInformation() ITK_OVERRIDE;
85 
88  virtual void WritePoints(void *buffer) ITK_OVERRIDE;
89 
90  virtual void WriteCells(void *buffer) ITK_OVERRIDE;
91 
92  virtual void WritePointData(void *buffer) ITK_OVERRIDE;
93 
94  virtual void WriteCellData(void *buffer) ITK_OVERRIDE;
95 
96  virtual void Write() ITK_OVERRIDE;
97 
98 protected:
100  template< typename T >
101  void ReadCellsBufferAsAscii(T *buffer, std::ifstream & inputFile)
102  {
103  SizeValueType index = 0;
104  unsigned int numberOfPoints = 0;
105  std::string line;
106 
107  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
108  {
109  inputFile >> numberOfPoints;
110  buffer[index++] = static_cast< T >( numberOfPoints );
111  for ( unsigned int jj = 0; jj < numberOfPoints; jj++ )
112  {
113  inputFile >> buffer[index++];
114  }
115  std::getline(inputFile, line, '\n');
116  }
117  }
118 
122  template< typename TInput, typename TOutput >
123  void ReadCellsBuffer(TInput *input, TOutput *output)
124  {
125  if ( input && output )
126  {
127  SizeValueType indInput = 0;
128  SizeValueType indOutput = 0;
129  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
130  {
131  indInput++; // ignore the cell type
132  unsigned int numberOfPoints = static_cast< unsigned int >( input[indInput++] );
133  output[indOutput++] = static_cast< TOutput >( numberOfPoints );
134  for ( unsigned int jj = 0; jj < numberOfPoints; jj++ )
135  {
136  output[indOutput++] = static_cast< TOutput >( input[indInput++] );
137  }
138  }
139  }
140  }
142 
143  template< typename T >
144  void WriteCellsAsAscii(T *buffer, std::ofstream & outputFile)
145  {
146  SizeValueType index = 0;
147 
148  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
149  {
150  index++;
151  unsigned int numberOfCellPoints = static_cast< unsigned int >( buffer[index++] );
152  outputFile << numberOfCellPoints << " ";
153 
154  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
155  {
156  outputFile << buffer[index++] << " ";
157  }
158 
159  outputFile << '\n';
160  }
161  }
162 
163  template< typename TOutput, typename TInput >
164  void WriteCellsAsBinary(TInput *buffer, std::ofstream & outputFile)
165  {
166  TOutput *data = new TOutput[m_CellBufferSize - this->m_NumberOfCells];
167 
168  ReadCellsBuffer(buffer, data);
169  WriteBufferAsBinary< TOutput >(data, outputFile, m_CellBufferSize - this->m_NumberOfCells);
170 
171  delete[] data;
172  }
173 
174 protected:
175  OFFMeshIO();
176  virtual ~OFFMeshIO() ITK_OVERRIDE;
177 
178  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
179 
180  void OpenFile();
181 
182  void CloseFile();
183 
184 private:
185  ITK_DISALLOW_COPY_AND_ASSIGN(OFFMeshIO);
186 
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:123
Light weight base class for most itk classes.
std::streamoff StreamOffsetType
Definition: itkMeshIOBase.h:82
OFFMeshIO Self
Definition: itkOFFMeshIO.h:39
this class defines how to read and write Object file format.
Definition: itkOFFMeshIO.h:35
SmartPointer< Self > Pointer
Definition: itkOFFMeshIO.h:42
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:84
unsigned long SizeValueType
Definition: itkIntTypes.h:143
void WriteCellsAsAscii(T *buffer, std::ofstream &outputFile)
Definition: itkOFFMeshIO.h:144
Superclass::StreamOffsetType StreamOffsetType
Definition: itkOFFMeshIO.h:45
MeshIOBase Superclass
Definition: itkOFFMeshIO.h:40
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:69
Superclass::SizeValueType SizeValueType
Definition: itkOFFMeshIO.h:44
SmartPointer< const Self > ConstPointer
Definition: itkOFFMeshIO.h:41
void WriteCellsAsBinary(TInput *buffer, std::ofstream &outputFile)
Definition: itkOFFMeshIO.h:164