ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkFreeSurferAsciiMeshIO.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 __itkFreeSurferAsciiMeshIO_h
20 #define __itkFreeSurferAsciiMeshIO_h
21 
22 #include "itkMeshIOBase.h"
23 
24 #include <fstream>
25 
26 namespace itk
27 {
35 class ITK_EXPORT FreeSurferAsciiMeshIO:public MeshIOBase
36 {
37 public:
43 
45 
47  itkNewMacro(Self);
48 
50  itkTypeMacro(FreeSurferAsciiMeshIO, 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 WritePoints(T *buffer, std::ofstream & outputFile, T label = itk::NumericTraits< T >::Zero)
101  {
102  outputFile.precision(6);
103  SizeValueType index = 0;
104  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
105  {
106  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
107  {
108  outputFile << std::fixed << buffer[index++] << " ";
109  }
110  outputFile << label << '\n';
111  }
112  }
114 
115  template< typename T >
116  void WriteCells(T *buffer, std::ofstream & outputFile, T label = itk::NumericTraits< T >::Zero)
117  {
118  const unsigned int numberOfCellPoints = 3;
119  SizeValueType index = 0;
120 
121  T *data = new T[this->m_NumberOfCells * numberOfCellPoints];
122 
123  ReadCellsBuffer(buffer, data);
124 
125  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
126  {
127  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
128  {
129  outputFile << data[index++] << " ";
130  }
131  outputFile << label << '\n';
132  }
133  delete[] data;
134  }
135 
137  template< typename TInput, typename TOutput >
138  void ReadCellsBuffer(TInput *input, TOutput *output)
139  {
140  if ( input && output )
141  {
142  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
143  {
144  for ( unsigned int jj = 0; jj < 3; jj++ )
145  {
146 
148  output[ii * 3 + jj] = static_cast< TOutput >( input[5 * ii + jj + 2] );
149  }
150  }
151  }
152  }
153 
154 protected:
157 
158  void PrintSelf(std::ostream & os, Indent indent) const;
159 
160  void OpenFile();
161 
162  void CloseFile();
163 
164 private:
165  FreeSurferAsciiMeshIO(const Self &); // purposely not implemented
166  void operator=(const Self &); // purposely not implemented
167 
168  std::ifstream m_InputFile;
169 };
170 } // end namespace itk
171 
172 #endif
173