ITK  5.2.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  * 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 itkFreeSurferBinaryMeshIO_h
20 #define itkFreeSurferBinaryMeshIO_h
21 #include "ITKIOMeshFreeSurferExport.h"
22 
23 #include "itkByteSwapper.h"
24 #include "itkMeshIOBase.h"
25 #include "itkIntTypes.h"
26 
27 #include <fstream>
28 
29 namespace itk
30 {
38 class ITKIOMeshFreeSurfer_EXPORT FreeSurferBinaryMeshIO : public MeshIOBase
39 {
40 public:
41  ITK_DISALLOW_COPY_AND_MOVE(FreeSurferBinaryMeshIO);
42 
48 
51 
53  itkNewMacro(Self);
54 
56  itkTypeMacro(FreeSurferBinaryMeshIO, MeshIOBase);
57 
58  /*-------- This part of the interfaces deals with reading data. ----- */
59 
65  bool
66  CanReadFile(const char * fileName) override;
67 
69  void
70  ReadMeshInformation() override;
71 
73  void
74  ReadPoints(void * buffer) override;
75 
76  void
77  ReadCells(void * buffer) override;
78 
79  void
80  ReadPointData(void * buffer) override;
81 
82  void
83  ReadCellData(void * buffer) override;
84 
85  /*-------- This part of the interfaces deals with writing data. ----- */
86 
92  bool
93  CanWriteFile(const char * fileName) override;
94 
96  void
97  WriteMeshInformation() override;
98 
101  void
102  WritePoints(void * buffer) override;
103 
104  void
105  WriteCells(void * buffer) override;
106 
107  void
108  WritePointData(void * buffer) override;
109 
110  void
111  WriteCellData(void * buffer) override;
112 
113  void
114  Write() override;
115 
116 protected:
118  template <typename T>
119  void
120  WritePoints(T * buffer, std::ofstream & outputFile)
121  {
122  auto * data = new float[this->m_NumberOfPoints * this->m_PointDimension];
123 
124  for (SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++)
125  {
126  for (unsigned int jj = 0; jj < this->m_PointDimension; jj++)
127  {
128  data[ii * this->m_PointDimension + jj] = static_cast<float>(buffer[ii * this->m_PointDimension + jj]);
129  }
130  }
131 
133  data, this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
134  delete[] data;
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  auto * data = new itk::uint32_t[this->m_NumberOfCells * numberOfCellPoints];
145 
146  ReadCellsBuffer(buffer, data);
148  data, this->m_NumberOfCells * numberOfCellPoints, &outputFile);
149 
150  delete[] data;
151  }
152 
154  template <typename TInput, typename TOutput>
155  void
156  ReadCellsBuffer(TInput * input, TOutput * output)
157  {
158  if (input && output)
159  {
160  for (SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++)
161  {
162  for (unsigned int jj = 0; jj < 3; jj++)
163  {
164 
167  output[ii * 3 + jj] = static_cast<TOutput>(input[5 * ii + jj + 2]);
168  }
169  }
170  }
171  }
172 
174  template <typename T>
175  void
176  WritePointData(T * buffer, std::ofstream & outputFile)
177  {
178  auto * data = new float[this->m_NumberOfPointPixels];
179 
180  for (SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++)
181  {
182  data[ii] = static_cast<float>(buffer[ii]);
183  }
184 
185  itk::ByteSwapper<float>::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPointPixels, &outputFile);
186  delete[] data;
187  }
188 
189 protected:
191  ~FreeSurferBinaryMeshIO() override;
192 
193  void
194  PrintSelf(std::ostream & os, Indent indent) const override;
195 
196  void
197  OpenFile();
198 
199  void
200  CloseFile();
201 
202 private:
203  StreamOffsetType m_FilePosition{ 0 };
204  itk::uint32_t m_FileTypeIdentifier{ 0 };
205  std::ifstream m_InputFile;
206 };
207 } // end namespace itk
208 
209 #endif
itk::FreeSurferBinaryMeshIO::ReadCellsBuffer
void ReadCellsBuffer(TInput *input, TOutput *output)
Definition: itkFreeSurferBinaryMeshIO.h:156
itkByteSwapper.h
itk::MeshIOBase::SizeValueType
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:88
itk::FreeSurferBinaryMeshIO::m_InputFile
std::ifstream m_InputFile
Definition: itkFreeSurferBinaryMeshIO.h:205
itk::MeshIOBase
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:71
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:59
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:38
itkMeshIOBase.h
itk::FreeSurferBinaryMeshIO::WritePoints
void WritePoints(T *buffer, std::ofstream &outputFile)
Definition: itkFreeSurferBinaryMeshIO.h:120
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::uint32_t
::uint32_t uint32_t
Definition: itkIntTypes.h:33
itk::ByteSwapper::SwapWriteRangeFromSystemToBigEndian
static void SwapWriteRangeFromSystemToBigEndian(T *p, int num, OStreamType *fp)
itk::FreeSurferBinaryMeshIO::WritePointData
void WritePointData(T *buffer, std::ofstream &outputFile)
Definition: itkFreeSurferBinaryMeshIO.h:176
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::MeshIOBase::StreamOffsetType
std::streamoff StreamOffsetType
Definition: itkMeshIOBase.h:86
fileName
std::string fileName
Definition: itkTestDriverInclude.h:118