ITK  6.0.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
30namespace itk
31{
39class ITKIOMeshFreeSurfer_EXPORT FreeSurferBinaryMeshIO : public MeshIOBase
40{
41public:
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
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
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
117protected:
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
186protected:
189
190 void
191 PrintSelf(std::ostream & os, Indent indent) const override;
192
193 void
195
196 void
198
199private:
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
static void SwapWriteRangeFromSystemToBigEndian(const T *p, int num, OStreamType *fp)
This class defines how to read Freesurfer binary surface file format. To use IO factory,...
void WritePoints(T *buffer, std::ofstream &outputFile)
void PrintSelf(std::ostream &os, Indent indent) const override
void ReadMeshInformation() override
void ReadCells(void *buffer) override
void WriteMeshInformation() override
void ReadPoints(void *buffer) override
void WriteCellData(void *buffer) override
void ReadCellData(void *buffer) override
void WritePoints(void *buffer) override
void WriteCells(T *buffer, std::ofstream &outputFile)
void WriteCells(void *buffer) override
void ReadCellsBuffer(TInput *input, TOutput *output)
void WritePointData(T *buffer, std::ofstream &outputFile)
bool CanReadFile(const char *fileName) override
void ReadPointData(void *buffer) override
void WritePointData(void *buffer) override
bool CanWriteFile(const char *fileName) override
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Light weight base class for most itk classes.
Abstract superclass defines mesh IO interface.
Definition: itkMeshIOBase.h:73
IdentifierType SizeValueType
Definition: itkMeshIOBase.h:89
std::streamoff StreamOffsetType
Definition: itkMeshIOBase.h:87
Base class for most ITK classes.
Definition: itkObject.h:62
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition: itkIntTypes.h:86