ITK  5.0.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 "ITKIOMeshFreeSurferExport.h"
22 
23 #include "itkMeshIOBase.h"
24 
25 #include <fstream>
26 
27 namespace itk
28 {
36 class ITKIOMeshFreeSurfer_EXPORT FreeSurferAsciiMeshIO:
37  public MeshIOBase
38 {
39 public:
40  ITK_DISALLOW_COPY_AND_ASSIGN(FreeSurferAsciiMeshIO);
41 
47 
49 
51  itkNewMacro(Self);
52 
54  itkTypeMacro(FreeSurferAsciiMeshIO, MeshIOBase);
55 
56  /*-------- This part of the interfaces deals with reading data. ----- */
57 
63  bool CanReadFile(const char *FileNameToRead) override;
64 
66  void ReadMeshInformation() override;
67 
69  void ReadPoints(void *buffer) override;
70 
71  void ReadCells(void *buffer) override;
72 
73  void ReadPointData(void *buffer) override;
74 
75  void ReadCellData(void *buffer) override;
76 
77  /*-------- This part of the interfaces deals with writing data. ----- */
78 
84  bool CanWriteFile(const char *FileNameToWrite) override;
85 
87  void WriteMeshInformation() override;
88 
91  void WritePoints(void *buffer) override;
92 
93  void WriteCells(void *buffer) override;
94 
95  void WritePointData(void *buffer) override;
96 
97  void WriteCellData(void *buffer) override;
98 
99  void Write() override;
100 
101 protected:
103  template< typename T >
104  void WritePoints(T *buffer, std::ofstream & outputFile, T label = itk::NumericTraits< T >::ZeroValue())
105  {
106  outputFile.precision(6);
107  SizeValueType index = 0;
108  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
109  {
110  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
111  {
112  outputFile << std::fixed << buffer[index++] << " ";
113  }
114  outputFile << label << '\n';
115  }
116  }
118 
119  template< typename T >
120  void WriteCells(T *buffer, std::ofstream & outputFile, T label = itk::NumericTraits< T >::ZeroValue())
121  {
122  constexpr unsigned int numberOfCellPoints = 3;
123  SizeValueType index = 0;
124 
125  auto * data = new T[this->m_NumberOfCells * numberOfCellPoints];
126 
127  ReadCellsBuffer(buffer, data);
128 
129  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
130  {
131  for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
132  {
133  outputFile << data[index++] << " ";
134  }
135  outputFile << label << '\n';
136  }
137  delete[] data;
138  }
139 
141  template< typename TInput, typename TOutput >
142  void ReadCellsBuffer(TInput *input, TOutput *output)
143  {
144  if ( input && output )
145  {
146  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
147  {
148  for ( unsigned int jj = 0; jj < 3; jj++ )
149  {
150 
152  output[ii * 3 + jj] = static_cast< TOutput >( input[5 * ii + jj + 2] );
153  }
154  }
155  }
156  }
157 
158 protected:
160  ~FreeSurferAsciiMeshIO() override;
161  void PrintSelf(std::ostream & os, Indent indent) const override;
162 
163  void OpenFile();
164 
165  void CloseFile();
166 
167 private:
168  std::ifstream m_InputFile;
169 };
170 } // end namespace itk
171 
172 #endif
Light weight base class for most itk classes.
void WriteCells(T *buffer, std::ofstream &outputFile, T label=itk::NumericTraits< T >::ZeroValue())
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:86
unsigned long SizeValueType
Definition: itkIntTypes.h:83
void ReadCellsBuffer(TInput *input, TOutput *output)
void WritePoints(T *buffer, std::ofstream &outputFile, T label=itk::NumericTraits< T >::ZeroValue())
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
Base class for most ITK classes.
Definition: itkObject.h:60