ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkFreeSurferAsciiMeshIO.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 __itkFreeSurferAsciiMeshIO_h
00020 #define __itkFreeSurferAsciiMeshIO_h
00021 
00022 #include "itkMeshIOBase.h"
00023 
00024 #include <fstream>
00025 
00026 namespace itk
00027 {
00035 class ITK_EXPORT FreeSurferAsciiMeshIO:public MeshIOBase
00036 {
00037 public:
00039   typedef FreeSurferAsciiMeshIO      Self;
00040   typedef MeshIOBase                 Superclass;
00041   typedef SmartPointer< const Self > ConstPointer;
00042   typedef SmartPointer< Self >       Pointer;
00043 
00044   typedef Superclass::SizeValueType    SizeValueType;
00045 
00047   itkNewMacro(Self);
00048 
00050   itkTypeMacro(FreeSurferAsciiMeshIO, MeshIOBase);
00051 
00052   /*-------- This part of the interfaces deals with reading data. ----- */
00053 
00059   virtual bool CanReadFile(const char *FileNameToRead);
00060 
00062   virtual void ReadMeshInformation();
00063 
00065   virtual void ReadPoints(void *buffer);
00066 
00067   virtual void ReadCells(void *buffer);
00068 
00069   virtual void ReadPointData(void *buffer);
00070 
00071   virtual void ReadCellData(void *buffer);
00072 
00073   /*-------- This part of the interfaces deals with writing data. ----- */
00074 
00080   virtual bool CanWriteFile(const char *FileNameToWrite);
00081 
00083   virtual void WriteMeshInformation();
00084 
00087   virtual void WritePoints(void *buffer);
00088 
00089   virtual void WriteCells(void *buffer);
00090 
00091   virtual void WritePointData(void *buffer);
00092 
00093   virtual void WriteCellData(void *buffer);
00094 
00095   virtual void Write();
00096 
00097 protected:
00099   template< typename T >
00100   void WritePoints(T *buffer, std::ofstream & outputFile, T label = itk::NumericTraits< T >::Zero)
00101   {
00102     outputFile.precision(6);
00103     SizeValueType index = 0;
00104     for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
00105       {
00106       for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
00107         {
00108         outputFile << std::fixed << buffer[index++] << "  ";
00109         }
00110       outputFile << label << '\n';
00111       }
00112   }
00114 
00115   template< typename T >
00116   void WriteCells(T *buffer, std::ofstream & outputFile, T label = itk::NumericTraits< T >::Zero)
00117   {
00118     const unsigned int numberOfCellPoints = 3;
00119     SizeValueType      index = 0;
00120 
00121     T *data = new T[this->m_NumberOfCells * numberOfCellPoints];
00122 
00123     ReadCellsBuffer(buffer, data);
00124 
00125     for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
00126       {
00127       for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
00128         {
00129         outputFile << data[index++] << "  ";
00130         }
00131       outputFile << label << '\n';
00132       }
00133     delete[] data;
00134   }
00135 
00137   template< typename TInput, typename TOutput >
00138   void ReadCellsBuffer(TInput *input, TOutput *output)
00139     {
00140     if ( input && output )
00141       {
00142       for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
00143         {
00144         for ( unsigned int jj = 0; jj < 3; jj++ )
00145           {
00146 
00148           output[ii * 3 + jj] = static_cast< TOutput >( input[5 * ii + jj + 2] );
00149           }
00150         }
00151       }
00152     }
00153 
00154 protected:
00155   FreeSurferAsciiMeshIO();
00156   virtual ~FreeSurferAsciiMeshIO(){}
00157 
00158   void PrintSelf(std::ostream & os, Indent indent) const;
00159 
00160   void OpenFile();
00161 
00162   void CloseFile();
00163 
00164 private:
00165   FreeSurferAsciiMeshIO(const Self &); // purposely not implemented
00166   void operator=(const Self &); // purposely not implemented
00167 
00168   std::ifstream m_InputFile;
00169 };
00170 } // end namespace itk
00171 
00172 #endif
00173