ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkOBJMeshIO.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 __itkOBJMeshIO_h
00020 #define __itkOBJMeshIO_h
00021 
00022 #include "itkMeshIOBase.h"
00023 
00024 #include <fstream>
00025 
00026 namespace itk
00027 {
00034 class ITK_EXPORT OBJMeshIO:public MeshIOBase
00035 {
00036 public:
00038   typedef OBJMeshIO                  Self;
00039   typedef MeshIOBase                 Superclass;
00040   typedef SmartPointer< const Self > ConstPointer;
00041   typedef SmartPointer< Self >       Pointer;
00042 
00043   typedef Superclass::SizeValueType    SizeValueType;
00044 
00046   itkNewMacro(Self);
00047 
00049   itkTypeMacro(OBJMeshIO, MeshIOBase);
00050 
00051   /*-------- This part of the interfaces deals with reading data. ----- */
00052 
00058   virtual bool CanReadFile(const char *FileNameToRead);
00059 
00061   virtual void ReadMeshInformation();
00062 
00064   virtual void ReadPoints(void *buffer);
00065 
00066   virtual void ReadCells(void *buffer);
00067 
00068   virtual void ReadPointData(void *buffer);
00069 
00070   virtual void ReadCellData(void *buffer);
00071 
00072   /*-------- This part of the interfaces deals with writing data. ----- */
00073 
00079   virtual bool CanWriteFile(const char *FileNameToWrite);
00080 
00082   virtual void WriteMeshInformation();
00083 
00086   virtual void WritePoints(void *buffer);
00087 
00088   virtual void WriteCells(void *buffer);
00089 
00090   virtual void WritePointData(void *buffer);
00091 
00092   virtual void WriteCellData(void *buffer);
00093 
00094   virtual void Write();
00095 
00096 protected:
00098   template< typename T >
00099   void WritePoints(T *buffer, std::ofstream & outputFile)
00100   {
00101     SizeValueType index = itk::NumericTraits< SizeValueType >::Zero;
00102 
00103     for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
00104       {
00105       outputFile << "v ";
00106       for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
00107         {
00108         outputFile << buffer[index++] << "  ";
00109         }
00110       outputFile << '\n';
00111       }
00112   }
00113 
00114   template< typename T >
00115   void WriteCells(T *buffer, std::ofstream & outputFile)
00116   {
00117     SizeValueType index = itk::NumericTraits< SizeValueType >::Zero;
00118 
00119     for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
00120       {
00121       outputFile << "f ";
00122       index++;
00123       unsigned int numberOfCellPoints = static_cast< unsigned int >( buffer[index++] );
00124 
00125       for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ )
00126         {
00127         outputFile << buffer[index++] + 1 << "  ";
00128         }
00129       outputFile << '\n';
00130       }
00131   }
00132 
00134   template< typename T >
00135   void WritePointData(T *buffer, std::ofstream & outputFile)
00136   {
00137     SizeValueType index = itk::NumericTraits< SizeValueType >::Zero;
00138 
00139     for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
00140       {
00141       outputFile << "vn ";
00142       for ( unsigned int jj = 0; jj < this->m_PointDimension; jj++ )
00143         {
00144         outputFile << buffer[index++] << "  ";
00145         }
00146 
00147       outputFile << '\n';
00148       }
00149   }
00150 
00151 protected:
00152   OBJMeshIO();
00153   virtual ~OBJMeshIO(){}
00154 
00155   void PrintSelf(std::ostream & os, Indent indent) const;
00156 
00157   void OpenFile();
00158 
00159   void CloseFile();
00160 
00161 private:
00162   OBJMeshIO(const Self &);      // purposely not implemented
00163   void operator=(const Self &); // purposely not implemented
00164 
00165   std::ifstream  m_InputFile;
00166   std::streampos m_PointsStartPosition;  // file position for points rlative to
00167                                          // std::ios::beg
00168 };
00169 } // end namespace itk
00170 
00171 #endif
00172