ITK  5.4.0
Insight Toolkit
itkFreeSurferBinaryMeshIO.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 itkFreeSurferBinaryMeshIO_h
20 #define itkFreeSurferBinaryMeshIO_h
21 #include "ITKIOMeshFreeSurferExport.h"
22 
23 #include "itkByteSwapper.h"
24 #include "itkMeshIOBase.h"
25 #include "itkIntTypes.h"
27 
28 #include <fstream>
29 
30 namespace itk
31 {
39 class ITKIOMeshFreeSurfer_EXPORT FreeSurferBinaryMeshIO : public MeshIOBase
40 {
41 public:
42  ITK_DISALLOW_COPY_AND_MOVE(FreeSurferBinaryMeshIO);
43 
49 
52 
54  itkNewMacro(Self);
55 
57  itkOverrideGetNameOfClassMacro(FreeSurferBinaryMeshIO);
58 
59  /*-------- This part of the interfaces deals with reading data. ----- */
60 
66  bool
67  CanReadFile(const char * fileName) override;
68 
70  void
71  ReadMeshInformation() override;
72 
74  void
75  ReadPoints(void * buffer) override;
76 
77  void
78  ReadCells(void * buffer) override;
79 
80  void
81  ReadPointData(void * buffer) override;
82 
83  void
84  ReadCellData(void * buffer) override;
85 
86  /*-------- This part of the interfaces deals with writing data. ----- */
87 
93  bool
94  CanWriteFile(const char * fileName) override;
95 
97  void
98  WriteMeshInformation() override;
99 
102  void
103  WritePoints(void * buffer) override;
104 
105  void
106  WriteCells(void * buffer) override;
107 
108  void
109  WritePointData(void * buffer) override;
110 
111  void
112  WriteCellData(void * buffer) override;
113 
114  void
115  Write() override;
116 
117 protected:
119  template <typename T>
120  void
121  WritePoints(T * buffer, std::ofstream & outputFile)
122  {
123  const auto data = make_unique_for_overwrite<float[]>(this->m_NumberOfPoints * this->m_PointDimension);
124 
125  for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ++ii)
126  {
127  for (unsigned int jj = 0; jj < this->m_PointDimension; ++jj)
128  {
129  data[ii * this->m_PointDimension + jj] = static_cast<float>(buffer[ii * this->m_PointDimension + jj]);
130  }
131  }
132 
134  data.get(), this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
135  }
136 
138  template <typename T>
139  void
140  WriteCells(T * buffer, std::ofstream & outputFile)
141  {
142  constexpr itk::uint32_t numberOfCellPoints = 3;
143 
144  const auto data = make_unique_for_overwrite<itk::uint32_t[]>(this->m_NumberOfCells * numberOfCellPoints);
145 
146  ReadCellsBuffer(buffer, data.get());
148  data.get(), this->m_NumberOfCells * numberOfCellPoints, &outputFile);
149  }
150 
152  template <typename TInput, typename TOutput>
153  void
154  ReadCellsBuffer(TInput * input, TOutput * output)
155  {
156  if (input && output)
157  {
158  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ++ii)
159  {
160  for (unsigned int jj = 0; jj < 3; ++jj)
161  {
162 
165  output[ii * 3 + jj] = static_cast<TOutput>(input[5 * ii + jj + 2]);
166  }
167  }
168  }
169  }
170 
172  template <typename T>
173  void
174  WritePointData(T * buffer, std::ofstream & outputFile)
175  {
176  const auto data = make_unique_for_overwrite<float[]>(this->m_NumberOfPointPixels);
177 
178  for (SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ++ii)
179  {
180  data[ii] = static_cast<float>(buffer[ii]);
181  }
182 
183  itk::ByteSwapper<float>::SwapWriteRangeFromSystemToBigEndian(data.get(), this->m_NumberOfPointPixels, &outputFile);
184  }
185 
186 protected:
188  ~FreeSurferBinaryMeshIO() override;
189 
190  void
191  PrintSelf(std::ostream & os, Indent indent) const override;
192 
193  void
194  OpenFile();
195 
196  void
197  CloseFile();
198 
199 private:
200  StreamOffsetType m_FilePosition{ 0 };
201  itk::uint32_t m_FileTypeIdentifier{ 0 };
202  std::ifstream m_InputFile{};
203 };
204 } // end namespace itk
205 
206 #endif
itk::FreeSurferBinaryMeshIO::ReadCellsBuffer
void ReadCellsBuffer(TInput *input, TOutput *output)
Definition: itkFreeSurferBinaryMeshIO.h:154
itkByteSwapper.h
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
itkIntTypes.h
itk::FreeSurferBinaryMeshIO::WriteCells
void WriteCells(T *buffer, std::ofstream &outputFile)
Definition: itkFreeSurferBinaryMeshIO.h:140
itk::FreeSurferBinaryMeshIO
This class defines how to read Freesurfer binary surface file format. To use IO factory,...
Definition: itkFreeSurferBinaryMeshIO.h:39
itkMeshIOBase.h
itk::FreeSurferBinaryMeshIO::WritePoints
void WritePoints(T *buffer, std::ofstream &outputFile)
Definition: itkFreeSurferBinaryMeshIO.h:121
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itkMakeUniqueForOverwrite.h
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::ByteSwapper::SwapWriteRangeFromSystemToBigEndian
static void SwapWriteRangeFromSystemToBigEndian(const T *p, int num, OStreamType *fp)
itk::FreeSurferBinaryMeshIO::WritePointData
void WritePointData(T *buffer, std::ofstream &outputFile)
Definition: itkFreeSurferBinaryMeshIO.h:174
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::MeshIOBase::StreamOffsetType
std::streamoff StreamOffsetType
Definition: itkMeshIOBase.h:87