ITK
4.1.0
Insight Segmentation and Registration Toolkit
|
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 __itkOFFMeshIO_h 00020 #define __itkOFFMeshIO_h 00021 00022 #include "itkMeshIOBase.h" 00023 00024 #include <fstream> 00025 00026 namespace itk 00027 { 00034 class ITK_EXPORT OFFMeshIO:public MeshIOBase 00035 { 00036 public: 00038 typedef OFFMeshIO Self; 00039 typedef MeshIOBase Superclass; 00040 typedef SmartPointer< const Self > ConstPointer; 00041 typedef SmartPointer< Self > Pointer; 00042 00043 typedef Superclass::SizeValueType SizeValueType; 00044 typedef Superclass::StreamOffsetType StreamOffsetType; 00045 00047 itkNewMacro(Self); 00048 00050 itkTypeMacro(OFFMeshIO, 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 ReadCellsBufferAsAscii(T *buffer, std::ifstream & inputFile) 00101 { 00102 SizeValueType index = 0; 00103 unsigned int numberOfPoints = 0; 00104 std::string line; 00105 00106 for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ ) 00107 { 00108 inputFile >> numberOfPoints; 00109 buffer[index++] = static_cast< T >( numberOfPoints ); 00110 for ( unsigned int jj = 0; jj < numberOfPoints; jj++ ) 00111 { 00112 inputFile >> buffer[index++]; 00113 } 00114 std::getline(inputFile, line, '\n'); 00115 } 00116 } 00117 00121 template< typename TInput, typename TOutput > 00122 void ReadCellsBuffer(TInput *input, TOutput *output) 00123 { 00124 if ( input && output ) 00125 { 00126 SizeValueType indInput = 0; 00127 SizeValueType indOutput = 0; 00128 for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ ) 00129 { 00130 indInput++; // ignore the cell type 00131 unsigned int numberOfPoints = static_cast< unsigned int >( input[indInput++] ); 00132 output[indOutput++] = static_cast< TOutput >( numberOfPoints ); 00133 for ( unsigned int jj = 0; jj < numberOfPoints; jj++ ) 00134 { 00135 output[indOutput++] = static_cast< TOutput >( input[indInput++] ); 00136 } 00137 } 00138 } 00139 } 00141 00142 template< typename T > 00143 void WriteCellsAsAscii(T *buffer, std::ofstream & outputFile) 00144 { 00145 SizeValueType index = 0; 00146 00147 for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ ) 00148 { 00149 index++; 00150 unsigned int numberOfCellPoints = static_cast< unsigned int >( buffer[index++] ); 00151 outputFile << numberOfCellPoints << " "; 00152 00153 for ( unsigned int jj = 0; jj < numberOfCellPoints; jj++ ) 00154 { 00155 outputFile << buffer[index++] << " "; 00156 } 00157 00158 outputFile << '\n'; 00159 } 00160 } 00161 00162 template< typename TOutput, typename TInput > 00163 void WriteCellsAsBinary(TInput *buffer, std::ofstream & outputFile) 00164 { 00165 TOutput *data = new TOutput[m_CellBufferSize - this->m_NumberOfCells]; 00166 00167 ReadCellsBuffer(buffer, data); 00168 WriteBufferAsBinary< TOutput >(data, outputFile, m_CellBufferSize - this->m_NumberOfCells); 00169 00170 delete[] data; 00171 } 00172 00173 protected: 00174 OFFMeshIO(); 00175 virtual ~OFFMeshIO(){} 00176 00177 void PrintSelf(std::ostream & os, Indent indent) const; 00178 00179 void OpenFile(); 00180 00181 void CloseFile(); 00182 00183 private: 00184 OFFMeshIO(const Self &); // purposely not implemented 00185 void operator=(const Self &); // purposely not implemented 00186 00187 std::ifstream m_InputFile; 00188 StreamOffsetType m_PointsStartPosition; // file position for points rlative to std::ios::beg 00189 bool m_TriangleCellType; // if all cells are trinalge it is true. otherwise, it is false. 00190 }; 00191 } // end namespace itk 00192 00193 #endif 00194