ITK  4.9.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 "ITKIOMeshExport.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 ITKIOMesh_EXPORT FreeSurferBinaryMeshIO:public MeshIOBase
39 {
40 public:
46 
49 
51  itkNewMacro(Self);
52 
54  itkTypeMacro(FreeSurferBinaryMeshIO, MeshIOBase);
55 
56  /*-------- This part of the interfaces deals with reading data. ----- */
57 
63  virtual bool CanReadFile(const char *FileNameToRead) ITK_OVERRIDE;
64 
66  virtual void ReadMeshInformation() ITK_OVERRIDE;
67 
69  virtual void ReadPoints(void *buffer) ITK_OVERRIDE;
70 
71  virtual void ReadCells(void *buffer) ITK_OVERRIDE;
72 
73  virtual void ReadPointData(void *buffer) ITK_OVERRIDE;
74 
75  virtual void ReadCellData(void *buffer) ITK_OVERRIDE;
76 
77  /*-------- This part of the interfaces deals with writing data. ----- */
78 
84  virtual bool CanWriteFile(const char *FileNameToWrite) ITK_OVERRIDE;
85 
87  virtual void WriteMeshInformation() ITK_OVERRIDE;
88 
91  virtual void WritePoints(void *buffer) ITK_OVERRIDE;
92 
93  virtual void WriteCells(void *buffer) ITK_OVERRIDE;
94 
95  virtual void WritePointData(void *buffer) ITK_OVERRIDE;
96 
97  virtual void WriteCellData(void *buffer) ITK_OVERRIDE;
98 
99  virtual void Write() ITK_OVERRIDE;
100 
101 protected:
103  template< typename T >
104  void WritePoints(T *buffer, std::ofstream & outputFile)
105  {
106  float *data = new float[this->m_NumberOfPoints * this->m_PointDimension];
107 
108  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
109  {
110  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
111  {
112  data[ii * this->m_PointDimension + jj] = static_cast< float >( buffer[ii * this->m_PointDimension + jj] );
113  }
114  }
115 
116  itk::ByteSwapper< float >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
117  delete[] data;
118  }
119 
121  template< typename T >
122  void WriteCells(T *buffer, std::ofstream & outputFile)
123  {
124  const itk::uint32_t numberOfCellPoints = 3;
125 
126  itk::uint32_t *data = new itk::uint32_t[this->m_NumberOfCells * numberOfCellPoints];
127 
128  ReadCellsBuffer(buffer, data);
129  itk::ByteSwapper< itk::uint32_t >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfCells * numberOfCellPoints, &outputFile);
130 
131  delete[] data;
132  }
133 
135  template< typename TInput, typename TOutput >
136  void ReadCellsBuffer(TInput *input, TOutput *output)
137  {
138  if ( input && output )
139  {
140  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
141  {
142  for ( unsigned int jj = 0; jj < 3; jj++ )
143  {
144 
147  output[ii * 3 + jj] = static_cast< TOutput >( input[5 * ii + jj + 2] );
148  }
149  }
150  }
151  }
152 
154  template< typename T >
155  void WritePointData(T *buffer, std::ofstream & outputFile)
156  {
157  float *data = new float[this->m_NumberOfPointPixels];
158 
159  for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
160  {
161  data[ii] = static_cast< float >( buffer[ii] );
162  }
163 
164  itk::ByteSwapper< float >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPointPixels, &outputFile);
165  delete[] data;
166  }
167 
168 protected:
171 
172  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
173 
174  void OpenFile();
175 
176  void CloseFile();
177 
178 private:
179  FreeSurferBinaryMeshIO(const Self &) ITK_DELETE_FUNCTION;
180  void operator=(const Self &) ITK_DELETE_FUNCTION;
181 
182  StreamOffsetType m_FilePosition;
183  itk::uint32_t m_FileTypeIdentifier;
184  std::ifstream m_InputFile;
185 };
186 } // end namespace itk
187 
188 #endif
SmartPointer< const Self > ConstPointer
Light weight base class for most itk classes.
std::streamoff StreamOffsetType
Definition: itkMeshIOBase.h:82
KWIML_INT_uint32_t uint32_t
Definition: itkIntTypes.h:87
unsigned long SizeValueType
Definition: itkIntTypes.h:143
void WritePointData(T *buffer, std::ofstream &outputFile)
Superclass::StreamOffsetType StreamOffsetType
void WriteCells(T *buffer, std::ofstream &outputFile)
Superclass::SizeValueType SizeValueType
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