ITK  4.3.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 
22 #include "itkByteSwapper.h"
23 #include "itkMeshIOBase.h"
24 #include "itkIntTypes.h"
25 
26 #include <fstream>
27 
28 namespace itk
29 {
37 class ITK_EXPORT FreeSurferBinaryMeshIO:public MeshIOBase
38 {
39 public:
45 
48 
50  itkNewMacro(Self);
51 
53  itkTypeMacro(FreeSurferBinaryMeshIO, MeshIOBase);
54 
55  /*-------- This part of the interfaces deals with reading data. ----- */
56 
62  virtual bool CanReadFile(const char *FileNameToRead);
63 
65  virtual void ReadMeshInformation();
66 
68  virtual void ReadPoints(void *buffer);
69 
70  virtual void ReadCells(void *buffer);
71 
72  virtual void ReadPointData(void *buffer);
73 
74  virtual void ReadCellData(void *buffer);
75 
76  /*-------- This part of the interfaces deals with writing data. ----- */
77 
83  virtual bool CanWriteFile(const char *FileNameToWrite);
84 
86  virtual void WriteMeshInformation();
87 
90  virtual void WritePoints(void *buffer);
91 
92  virtual void WriteCells(void *buffer);
93 
94  virtual void WritePointData(void *buffer);
95 
96  virtual void WriteCellData(void *buffer);
97 
98  virtual void Write();
99 
100 protected:
102  template< typename T >
103  void WritePoints(T *buffer, std::ofstream & outputFile)
104  {
105  float *data = new float[this->m_NumberOfPoints * this->m_PointDimension];
106 
107  for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
108  {
109  for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
110  {
111  data[ii * this->m_PointDimension + jj] = static_cast< float >( buffer[ii * this->m_PointDimension + jj] );
112  }
113  }
114 
115  itk::ByteSwapper< float >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
116  delete[] data;
117  }
118 
120  template< typename T >
121  void WriteCells(T *buffer, std::ofstream & outputFile)
122  {
123  const itk::uint32_t numberOfCellPoints = 3;
124 
125  itk::uint32_t *data = new itk::uint32_t[this->m_NumberOfCells * numberOfCellPoints];
126 
127  ReadCellsBuffer(buffer, data);
128  itk::ByteSwapper< itk::uint32_t >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfCells * numberOfCellPoints, &outputFile);
129 
130  delete[] data;
131  }
132 
134  template< typename TInput, typename TOutput >
135  void ReadCellsBuffer(TInput *input, TOutput *output)
136  {
137  if ( input && output )
138  {
139  for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
140  {
141  for ( unsigned int jj = 0; jj < 3; jj++ )
142  {
143 
146  output[ii * 3 + jj] = static_cast< TOutput >( input[5 * ii + jj + 2] );
147  }
148  }
149  }
150  }
151 
153  template< typename T >
154  void WritePointData(T *buffer, std::ofstream & outputFile)
155  {
156  float *data = new float[this->m_NumberOfPointPixels];
157 
158  for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
159  {
160  data[ii] = static_cast< float >( buffer[ii] );
161  }
162 
163  itk::ByteSwapper< float >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPointPixels, &outputFile);
164  delete[] data;
165  }
166 
167 protected:
170 
171  void PrintSelf(std::ostream & os, Indent indent) const;
172 
173  void OpenFile();
174 
175  void CloseFile();
176 
177 private:
178  FreeSurferBinaryMeshIO(const Self &); // purposely not implemented
179  void operator=(const Self &); // purposely not implemented
180 
183  std::ifstream m_InputFile;
184 };
185 } // end namespace itk
186 
187 #endif
188