ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkFreeSurferBinaryMeshIO.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 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_ASSIGN(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 CanReadFile(const char *FileNameToRead) override;
66 
68  void ReadMeshInformation() override;
69 
71  void ReadPoints(void *buffer) override;
72 
73  void ReadCells(void *buffer) override;
74 
75  void ReadPointData(void *buffer) override;
76 
77  void ReadCellData(void *buffer) override;
78 
79  /*-------- This part of the interfaces deals with writing data. ----- */
80 
86  bool CanWriteFile(const char *FileNameToWrite) override;
87 
89  void WriteMeshInformation() override;
90 
93  void WritePoints(void *buffer) override;
94 
95  void WriteCells(void *buffer) override;
96 
97  void WritePointData(void *buffer) override;
98 
99  void WriteCellData(void *buffer) override;
100 
101  void Write() override;
102 
103 protected:
105  template< typename T >
106  void WritePoints(T *buffer, std::ofstream & outputFile)
107  {
108  auto * data = new float[this->m_NumberOfPoints * this->m_PointDimension];
109 
110  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
111  {
112  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
113  {
114  data[ii * this->m_PointDimension + jj] = static_cast< float >( buffer[ii * this->m_PointDimension + jj] );
115  }
116  }
117 
118  itk::ByteSwapper< float >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
119  delete[] data;
120  }
121 
123  template< typename T >
124  void WriteCells(T *buffer, std::ofstream & outputFile)
125  {
126  constexpr itk::uint32_t numberOfCellPoints = 3;
127 
128  auto * data = new itk::uint32_t[this->m_NumberOfCells * numberOfCellPoints];
129 
130  ReadCellsBuffer(buffer, data);
131  itk::ByteSwapper< itk::uint32_t >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfCells * numberOfCellPoints, &outputFile);
132 
133  delete[] data;
134  }
135 
137  template< typename TInput, typename TOutput >
138  void ReadCellsBuffer(TInput *input, TOutput *output)
139  {
140  if ( input && output )
141  {
142  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
143  {
144  for ( unsigned int jj = 0; jj < 3; jj++ )
145  {
146 
149  output[ii * 3 + jj] = static_cast< TOutput >( input[5 * ii + jj + 2] );
150  }
151  }
152  }
153  }
154 
156  template< typename T >
157  void WritePointData(T *buffer, std::ofstream & outputFile)
158  {
159  auto * data = new float[this->m_NumberOfPointPixels];
160 
161  for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
162  {
163  data[ii] = static_cast< float >( buffer[ii] );
164  }
165 
166  itk::ByteSwapper< float >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPointPixels, &outputFile);
167  delete[] data;
168  }
169 
170 protected:
172  ~FreeSurferBinaryMeshIO() override;
173 
174  void PrintSelf(std::ostream & os, Indent indent) const override;
175 
176  void OpenFile();
177 
178  void CloseFile();
179 
180 private:
181  StreamOffsetType m_FilePosition{0};
182  itk::uint32_t m_FileTypeIdentifier{0};
183  std::ifstream m_InputFile;
184 };
185 } // end namespace itk
186 
187 #endif
Light weight base class for most itk classes.
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:86
unsigned long SizeValueType
Definition: itkIntTypes.h:83
void WritePoints(T *buffer, std::ofstream &outputFile)
void WritePointData(T *buffer, std::ofstream &outputFile)
std::streamoff StreamOffsetType
Definition: itkMeshIOBase.h:84
void WriteCells(T *buffer, std::ofstream &outputFile)
::uint32_t uint32_t
Definition: itkIntTypes.h:33
void ReadCellsBuffer(TInput *input, TOutput *output)
Control indentation during Print() invocation.
Definition: itkIndent.h:49
static void SwapWriteRangeFromSystemToBigEndian(T *p, int num, OStreamType *fp)
This class defins how to read Freesurfer binary surface file format. To use IO factory, define the suffix as *.fsb.
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:69
Base class for most ITK classes.
Definition: itkObject.h:60