ITK  4.4.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 
22 #include "itkMeshIOBase.h"
23 
24 #include <fstream>
25 
26 namespace itk
27 {
34 class ITK_EXPORT OFFMeshIO:public MeshIOBase
35 {
36 public:
38  typedef OFFMeshIO Self;
42 
44  typedef Superclass::StreamOffsetType StreamOffsetType;
45 
47  itkNewMacro(Self);
48 
50  itkTypeMacro(OFFMeshIO, MeshIOBase);
51 
52  /*-------- This part of the interfaces deals with reading data. ----- */
53 
59  virtual bool CanReadFile(const char *FileNameToRead);
60 
62  virtual void ReadMeshInformation();
63 
65  virtual void ReadPoints(void *buffer);
66 
67  virtual void ReadCells(void *buffer);
68 
69  virtual void ReadPointData(void *buffer);
70 
71  virtual void ReadCellData(void *buffer);
72 
73  /*-------- This part of the interfaces deals with writing data. ----- */
74 
80  virtual bool CanWriteFile(const char *FileNameToWrite);
81 
83  virtual void WriteMeshInformation();
84 
87  virtual void WritePoints(void *buffer);
88 
89  virtual void WriteCells(void *buffer);
90 
91  virtual void WritePointData(void *buffer);
92 
93  virtual void WriteCellData(void *buffer);
94 
95  virtual void Write();
96 
97 protected:
99  template< typename T >
100  void ReadCellsBufferAsAscii(T *buffer, std::ifstream & inputFile)
101  {
102  SizeValueType index = 0;
103  unsigned int numberOfPoints = 0;
104  std::string line;
105 
106  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
107  {
108  inputFile >> numberOfPoints;
109  buffer[index++] = static_cast< T >( numberOfPoints );
110  for ( unsigned int jj = 0; jj < numberOfPoints; jj++ )
111  {
112  inputFile >> buffer[index++];
113  }
114  std::getline(inputFile, line, '\n');
115  }
116  }
117 
121  template< typename TInput, typename TOutput >
122  void ReadCellsBuffer(TInput *input, TOutput *output)
123  {
124  if ( input && output )
125  {
126  SizeValueType indInput = 0;
127  SizeValueType indOutput = 0;
128  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
129  {
130  indInput++; // ignore the cell type
131  unsigned int numberOfPoints = static_cast< unsigned int >( input[indInput++] );
132  output[indOutput++] = static_cast< TOutput >( numberOfPoints );
133  for ( unsigned int jj = 0; jj < numberOfPoints; jj++ )
134  {
135  output[indOutput++] = static_cast< TOutput >( input[indInput++] );
136  }
137  }
138  }
139  }
141 
142  template< typename T >
143  void WriteCellsAsAscii(T *buffer, std::ofstream & outputFile)
144  {
145  SizeValueType index = 0;
146 
147  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
148  {
149  index++;
150  unsigned int numberOfCellPoints = static_cast< unsigned int >( buffer[index++] );
151  outputFile << numberOfCellPoints << " ";
152 
153  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
154  {
155  outputFile << buffer[index++] << " ";
156  }
157 
158  outputFile << '\n';
159  }
160  }
161 
162  template< typename TOutput, typename TInput >
163  void WriteCellsAsBinary(TInput *buffer, std::ofstream & outputFile)
164  {
165  TOutput *data = new TOutput[m_CellBufferSize - this->m_NumberOfCells];
166 
167  ReadCellsBuffer(buffer, data);
168  WriteBufferAsBinary< TOutput >(data, outputFile, m_CellBufferSize - this->m_NumberOfCells);
169 
170  delete[] data;
171  }
172 
173 protected:
174  OFFMeshIO();
175  virtual ~OFFMeshIO(){}
176 
177  void PrintSelf(std::ostream & os, Indent indent) const;
178 
179  void OpenFile();
180 
181  void CloseFile();
182 
183 private:
184  OFFMeshIO(const Self &); // purposely not implemented
185  void operator=(const Self &); // purposely not implemented
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
194