ITK  6.0.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  * https://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"
25 
26 #include <fstream>
27 
28 namespace itk
29 {
37 class ITKIOMeshFreeSurfer_EXPORT FreeSurferAsciiMeshIO : public MeshIOBase
38 {
39 public:
40  ITK_DISALLOW_COPY_AND_MOVE(FreeSurferAsciiMeshIO);
41 
47 
49 
51  itkNewMacro(Self);
52 
54  itkOverrideGetNameOfClassMacro(FreeSurferAsciiMeshIO);
55 
56  /*-------- This part of the interfaces deals with reading data. ----- */
57 
63  bool
64  CanReadFile(const char * fileName) override;
65 
67  void
68  ReadMeshInformation() override;
69 
71  void
72  ReadPoints(void * buffer) override;
73 
74  void
75  ReadCells(void * buffer) override;
76 
77  void
78  ReadPointData(void * buffer) override;
79 
80  void
81  ReadCellData(void * buffer) override;
82 
83  /*-------- This part of the interfaces deals with writing data. ----- */
84 
90  bool
91  CanWriteFile(const char * fileName) override;
92 
94  void
95  WriteMeshInformation() override;
96 
99  void
100  WritePoints(void * buffer) override;
101 
102  void
103  WriteCells(void * buffer) override;
104 
105  void
106  WritePointData(void * buffer) override;
107 
108  void
109  WriteCellData(void * buffer) override;
110 
111  void
112  Write() override;
113 
114 protected:
116  template <typename T>
117  void
118  WritePoints(T * buffer, std::ofstream & outputFile, T label = T{})
119  {
120  outputFile.precision(6);
121  SizeValueType index = 0;
122  for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
123  {
124  for (unsigned int jj = 0; jj < this->m_PointDimension; ++jj)
125  {
126  outputFile << std::fixed << buffer[index++] << " ";
127  }
128  outputFile << label << '\n';
129  }
130  }
133  template <typename T>
134  void
135  WriteCells(T * buffer, std::ofstream & outputFile, T label = T{})
136  {
137  constexpr unsigned int numberOfCellPoints = 3;
138  SizeValueType index = 0;
139 
140  const auto data = make_unique_for_overwrite<T[]>(this->m_NumberOfCells * numberOfCellPoints);
141 
142  ReadCellsBuffer(buffer, data.get());
143 
144  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ++ii)
145  {
146  for (unsigned int jj = 0; jj < numberOfCellPoints; ++jj)
147  {
148  outputFile << data[index++] << " ";
149  }
150  outputFile << label << '\n';
151  }
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::FreeSurferAsciiMeshIO::WritePoints
void WritePoints(T *buffer, std::ofstream &outputFile, T label=T{})
Definition: itkFreeSurferAsciiMeshIO.h:118
itk::MeshIOBase::SizeValueType
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:89
itk::MeshIOBase
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:72
itk::SmartPointer< const Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::FreeSurferAsciiMeshIO
This class defines how to read and write freesurfer ASCII surface format. To use IO factory,...
Definition: itkFreeSurferAsciiMeshIO.h:37
itk::FreeSurferAsciiMeshIO::ReadCellsBuffer
void ReadCellsBuffer(TInput *input, TOutput *output)
Definition: itkFreeSurferAsciiMeshIO.h:157
itkMeshIOBase.h
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29
itkMakeUniqueForOverwrite.h
itk::FreeSurferAsciiMeshIO::WriteCells
void WriteCells(T *buffer, std::ofstream &outputFile, T label=T{})
Definition: itkFreeSurferAsciiMeshIO.h:135
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:86