ITK  5.2.0
Insight Toolkit
itkFreeSurferAsciiMeshIO.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 : public MeshIOBase
37 {
38 public:
39  ITK_DISALLOW_COPY_AND_MOVE(FreeSurferAsciiMeshIO);
40 
46 
48 
50  itkNewMacro(Self);
51 
53  itkTypeMacro(FreeSurferAsciiMeshIO, MeshIOBase);
54 
55  /*-------- This part of the interfaces deals with reading data. ----- */
56 
62  bool
63  CanReadFile(const char * fileName) override;
64 
66  void
67  ReadMeshInformation() override;
68 
70  void
71  ReadPoints(void * buffer) override;
72 
73  void
74  ReadCells(void * buffer) override;
75 
76  void
77  ReadPointData(void * buffer) override;
78 
79  void
80  ReadCellData(void * buffer) override;
81 
82  /*-------- This part of the interfaces deals with writing data. ----- */
83 
89  bool
90  CanWriteFile(const char * fileName) override;
91 
93  void
94  WriteMeshInformation() override;
95 
98  void
99  WritePoints(void * buffer) override;
100 
101  void
102  WriteCells(void * buffer) override;
103 
104  void
105  WritePointData(void * buffer) override;
106 
107  void
108  WriteCellData(void * buffer) override;
109 
110  void
111  Write() override;
112 
113 protected:
115  template <typename T>
116  void
117  WritePoints(T * buffer, std::ofstream & outputFile, T label = itk::NumericTraits<T>::ZeroValue())
118  {
119  outputFile.precision(6);
120  SizeValueType index = 0;
121  for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++)
122  {
123  for (unsigned int jj = 0; jj < this->m_PointDimension; jj++)
124  {
125  outputFile << std::fixed << buffer[index++] << " ";
126  }
127  outputFile << label << '\n';
128  }
129  }
131 
132  template <typename T>
133  void
134  WriteCells(T * buffer, std::ofstream & outputFile, T label = itk::NumericTraits<T>::ZeroValue())
135  {
136  constexpr unsigned int numberOfCellPoints = 3;
137  SizeValueType index = 0;
138 
139  auto * data = new T[this->m_NumberOfCells * numberOfCellPoints];
140 
141  ReadCellsBuffer(buffer, data);
142 
143  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++)
144  {
145  for (unsigned int jj = 0; jj < numberOfCellPoints; jj++)
146  {
147  outputFile << data[index++] << " ";
148  }
149  outputFile << label << '\n';
150  }
151  delete[] data;
152  }
153 
155  template <typename TInput, typename TOutput>
156  void
157  ReadCellsBuffer(TInput * input, TOutput * output)
158  {
159  if (input && output)
160  {
161  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++)
162  {
163  for (unsigned int jj = 0; jj < 3; jj++)
164  {
165 
168  output[ii * 3 + jj] = static_cast<TOutput>(input[5 * ii + jj + 2]);
169  }
170  }
171  }
172  }
173 
174 protected:
176  ~FreeSurferAsciiMeshIO() override;
177  void
178  PrintSelf(std::ostream & os, Indent indent) const override;
179 
180  void
181  OpenFile();
182 
183  void
184  CloseFile();
185 
186 private:
187  std::ifstream m_InputFile;
188 };
189 } // end namespace itk
190 
191 #endif
itk::MeshIOBase::SizeValueType
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:88
itk::MeshIOBase
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:71
itk::FreeSurferAsciiMeshIO::WritePoints
void WritePoints(T *buffer, std::ofstream &outputFile, T label=itk::NumericTraits< T >::ZeroValue())
Definition: itkFreeSurferAsciiMeshIO.h:117
itk::SmartPointer< const Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::FreeSurferAsciiMeshIO::m_InputFile
std::ifstream m_InputFile
Definition: itkFreeSurferAsciiMeshIO.h:187
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::FreeSurferAsciiMeshIO
This class defines how to read and write freesurfer ASCII surface format. To use IO factory,...
Definition: itkFreeSurferAsciiMeshIO.h:36
itk::FreeSurferAsciiMeshIO::ReadCellsBuffer
void ReadCellsBuffer(TInput *input, TOutput *output)
Definition: itkFreeSurferAsciiMeshIO.h:157
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itkMeshIOBase.h
itk::FreeSurferAsciiMeshIO::WriteCells
void WriteCells(T *buffer, std::ofstream &outputFile, T label=itk::NumericTraits< T >::ZeroValue())
Definition: itkFreeSurferAsciiMeshIO.h:134
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
fileName
std::string fileName
Definition: itkTestDriverInclude.h:118