ITK  4.8.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 #include "ITKIOMeshExport.h"
22 
23 #include "itkMeshIOBase.h"
24 
25 #include <fstream>
26 
27 namespace itk
28 {
36 class ITKIOMesh_EXPORT FreeSurferAsciiMeshIO:public MeshIOBase
37 {
38 public:
44 
46 
48  itkNewMacro(Self);
49 
51  itkTypeMacro(FreeSurferAsciiMeshIO, 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 WritePoints(T *buffer, std::ofstream & outputFile, T label = itk::NumericTraits< T >::ZeroValue())
102  {
103  outputFile.precision(6);
104  SizeValueType index = 0;
105  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
106  {
107  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
108  {
109  outputFile << std::fixed << buffer[index++] << " ";
110  }
111  outputFile << label << '\n';
112  }
113  }
115 
116  template< typename T >
117  void WriteCells(T *buffer, std::ofstream & outputFile, T label = itk::NumericTraits< T >::ZeroValue())
118  {
119  const unsigned int numberOfCellPoints = 3;
120  SizeValueType index = 0;
121 
122  T *data = new T[this->m_NumberOfCells * numberOfCellPoints];
123 
124  ReadCellsBuffer(buffer, data);
125 
126  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
127  {
128  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
129  {
130  outputFile << data[index++] << " ";
131  }
132  outputFile << label << '\n';
133  }
134  delete[] data;
135  }
136 
138  template< typename TInput, typename TOutput >
139  void ReadCellsBuffer(TInput *input, TOutput *output)
140  {
141  if ( input && output )
142  {
143  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
144  {
145  for ( unsigned int jj = 0; jj < 3; jj++ )
146  {
147 
149  output[ii * 3 + jj] = static_cast< TOutput >( input[5 * ii + jj + 2] );
150  }
151  }
152  }
153  }
154 
155 protected:
158 
159  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
160 
161  void OpenFile();
162 
163  void CloseFile();
164 
165 private:
166  FreeSurferAsciiMeshIO(const Self &); // purposely not implemented
167  void operator=(const Self &); // purposely not implemented
168 
169  std::ifstream m_InputFile;
170 };
171 } // end namespace itk
172 
173 #endif
Light weight base class for most itk classes.
void WriteCells(T *buffer, std::ofstream &outputFile, T label=itk::NumericTraits< T >::ZeroValue())
SmartPointer< const Self > ConstPointer
void ReadCellsBuffer(TInput *input, TOutput *output)
unsigned long SizeValueType
Definition: itkIntTypes.h:143
Superclass::SizeValueType SizeValueType
This class defines how to read and write freesurfer ASCII surface format. To use IO factory...
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:69
Define additional traits for native types such as int or float.