ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkFreeSurferBinaryMeshIO.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 
00019 #ifndef __itkFreeSurferBinaryMeshIO_h
00020 #define __itkFreeSurferBinaryMeshIO_h
00021 
00022 #include "itkByteSwapper.h"
00023 #include "itkMeshIOBase.h"
00024 #include "itkIntTypes.h"
00025 
00026 #include <fstream>
00027 
00028 namespace itk
00029 {
00037 class ITK_EXPORT FreeSurferBinaryMeshIO:public MeshIOBase
00038 {
00039 public:
00041   typedef FreeSurferBinaryMeshIO       Self;
00042   typedef MeshIOBase                   Superclass;
00043   typedef SmartPointer< const Self >   ConstPointer;
00044   typedef SmartPointer< Self >         Pointer;
00045 
00046   typedef Superclass::SizeValueType    SizeValueType;
00047   typedef Superclass::StreamOffsetType StreamOffsetType;
00048 
00050   itkNewMacro(Self);
00051 
00053   itkTypeMacro(FreeSurferBinaryMeshIO, MeshIOBase);
00054 
00055   /*-------- This part of the interfaces deals with reading data. ----- */
00056 
00062   virtual bool CanReadFile(const char *FileNameToRead);
00063 
00065   virtual void ReadMeshInformation();
00066 
00068   virtual void ReadPoints(void *buffer);
00069 
00070   virtual void ReadCells(void *buffer);
00071 
00072   virtual void ReadPointData(void *buffer);
00073 
00074   virtual void ReadCellData(void *buffer);
00075 
00076   /*-------- This part of the interfaces deals with writing data. ----- */
00077 
00083   virtual bool CanWriteFile(const char *FileNameToWrite);
00084 
00086   virtual void WriteMeshInformation();
00087 
00090   virtual void WritePoints(void *buffer);
00091 
00092   virtual void WriteCells(void *buffer);
00093 
00094   virtual void WritePointData(void *buffer);
00095 
00096   virtual void WriteCellData(void *buffer);
00097 
00098   virtual void Write();
00099 
00100 protected:
00102   template< typename T >
00103   void WritePoints(T *buffer, std::ofstream & outputFile)
00104   {
00105     float *data = new float[this->m_NumberOfPoints * this->m_PointDimension];
00106 
00107     for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
00108       {
00109       for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
00110         {
00111         data[ii * this->m_PointDimension + jj] = static_cast< float >( buffer[ii * this->m_PointDimension + jj] );
00112         }
00113       }
00114 
00115     itk::ByteSwapper< float >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
00116     delete[] data;
00117   }
00118 
00120   template< typename T >
00121   void WriteCells(T *buffer, std::ofstream & outputFile)
00122   {
00123     const itk::uint32_t numberOfCellPoints = 3;
00124 
00125     itk::uint32_t *data = new itk::uint32_t[this->m_NumberOfCells * numberOfCellPoints];
00126 
00127     ReadCellsBuffer(buffer, data);
00128     itk::ByteSwapper< itk::uint32_t >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfCells * numberOfCellPoints, &outputFile);
00129 
00130     delete[] data;
00131   }
00132 
00134   template< typename TInput, typename TOutput >
00135   void ReadCellsBuffer(TInput *input, TOutput *output)
00136   {
00137     if ( input && output )
00138       {
00139       for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
00140         {
00141         for ( unsigned int jj = 0; jj < 3; jj++ )
00142           {
00143 
00146           output[ii * 3 + jj] = static_cast< TOutput >( input[5 * ii + jj + 2] );
00147           }
00148         }
00149       }
00150   }
00151 
00153   template< typename T >
00154   void WritePointData(T *buffer, std::ofstream & outputFile)
00155   {
00156     float *data = new float[this->m_NumberOfPointPixels];
00157 
00158     for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
00159       {
00160       data[ii] = static_cast< float >( buffer[ii] );
00161       }
00162 
00163     itk::ByteSwapper< float >::SwapWriteRangeFromSystemToBigEndian(data, this->m_NumberOfPointPixels, &outputFile);
00164     delete[] data;
00165   }
00166 
00167 protected:
00168   FreeSurferBinaryMeshIO();
00169   virtual ~FreeSurferBinaryMeshIO(){}
00170 
00171   void PrintSelf(std::ostream & os, Indent indent) const;
00172 
00173   void OpenFile();
00174 
00175   void CloseFile();
00176 
00177 private:
00178   FreeSurferBinaryMeshIO(const Self &); // purposely not implemented
00179   void operator=(const Self &);         // purposely not implemented
00180 
00181   StreamOffsetType m_FilePosition;
00182   itk::uint32_t    m_FileTypeIdentifier;
00183   std::ifstream    m_InputFile;
00184 };
00185 } // end namespace itk
00186 
00187 #endif
00188