17 #ifndef __itkVTKPolyDataReader_txx
18 #define __itkVTKPolyDataReader_txx
31 template<
class TOutputMesh>
38 typename TOutputMesh::Pointer output = TOutputMesh::New();
43 template<
class TOutputMesh>
48 typename OutputMeshType::Pointer outputMesh = this->GetOutput();
50 outputMesh->SetCellsAllocationMethod(
51 OutputMeshType::CellsAllocatedDynamicallyCellByCell );
53 if( m_FileName ==
"" )
55 itkExceptionMacro(<<
"No input FileName");
61 std::ifstream inputFile( m_FileName.c_str() );
63 if( !inputFile.is_open() )
65 itkExceptionMacro(<<
"Unable to open file\n"
66 <<
"inputFilename= " << m_FileName );
69 inputFile.imbue(std::locale::classic());
74 std::getline( inputFile, m_Version,
'\n' );
77 itkExceptionMacro(<<
"Error reading file: " << m_FileName
78 <<
"\nUnexpected end-of-file trying to read first line.");
80 if (m_Version.find(
"# vtk DataFile Version ") == std::string::npos)
82 itkExceptionMacro(<<
"Error reading file: " << m_FileName
83 <<
"\nOnly vtk legacy format files can be read."
84 <<
"\nThis file does not start with the line: # vtk DataFile Version x.x where x.x is the version.");
88 std::getline( inputFile, m_Header,
'\n' );
91 itkExceptionMacro(<<
"Error reading file: " << m_FileName
92 <<
"\nUnexpected end-of-file trying to read header.");
96 std::getline( inputFile, line,
'\n' );
99 itkExceptionMacro(<<
"Error reading file: " << m_FileName
100 <<
"\nUnexpected end-of-file trying to file format.");
102 if (line.find(
"ASCII") == std::string::npos)
104 itkExceptionMacro(<<
"Error reading file: " << m_FileName
105 <<
"\nFile format is " << line
106 <<
" but only ASCII files can be read.");
109 bool foundPoints =
false;
110 while( !inputFile.eof() )
112 std::getline( inputFile, line,
'\n' );
114 if( line.find(
"POINTS") != std::string::npos )
123 itkExceptionMacro(<<
"Error reading file: " << m_FileName
124 <<
"\nUnexpected end-of-file before finding POINTS.");
126 itkDebugMacro(
"POINTS line" << line );
128 std::string pointLine( line, strlen(
"POINTS "), line.length() );
129 itkDebugMacro(
"pointLine " << pointLine );
131 int numberOfPoints = -1;
133 if( sscanf(pointLine.c_str(),
"%d",&numberOfPoints) != 1 )
135 itkExceptionMacro(<<
"Error reading file: " << m_FileName
136 <<
"\nFailed to read numberOfPoints.\n"
137 <<
" pointLine= " << pointLine );
140 itkDebugMacro(
"numberOfPoints= " << numberOfPoints );
142 if( numberOfPoints < 1 )
144 itkExceptionMacro(<<
"Error reading file: " << m_FileName
145 <<
"numberOfPoints < 1"
146 <<
" numberOfPoints line= " << numberOfPoints );
149 outputMesh->GetPoints()->Reserve( numberOfPoints );
157 for(
int i=0; i < numberOfPoints; i++ )
162 itkExceptionMacro(<<
"Error while reading file: " << m_FileName
163 <<
"\nUnexpected end-of-file trying to read points.");
165 if (inputFile.fail())
167 itkExceptionMacro(<<
"Error reading file: " << m_FileName
168 <<
"\nInput could not be interpreted as a point.");
170 outputMesh->SetPoint( i, point );
174 bool foundPolygons =
false;
175 while( !inputFile.eof() )
177 std::getline( inputFile, line,
'\n' );
178 if (line.find(
"POLYGONS") != std::string::npos )
180 foundPolygons =
true;
187 itkExceptionMacro(<<
"Error reading file: " << m_FileName
188 <<
"\nUnexpected end-of-file before finding POLYGONS.");
191 itkDebugMacro(
"POLYGONS line" << line );
193 std::string polygonLine( line, strlen(
"POLYGONS "), line.length() );
194 itkDebugMacro(
"polygonLine " << polygonLine );
203 if( sscanf( polygonLine.c_str(),
"%ld %ld", &numberOfPolygons,
204 &numberOfIndices ) != 2 )
206 itkExceptionMacro(<<
"Error reading file: " << m_FileName
207 <<
"\nFailed to read numberOfPolygons from subline2"
208 <<
"\npolygonLine = " << polygonLine );
211 itkDebugMacro(
"numberOfPolygons " << numberOfPolygons );
212 itkDebugMacro(
"numberOfIndices " << numberOfIndices );
214 if( numberOfPolygons < 1 )
216 itkExceptionMacro(<<
"Error reading file: " << m_FileName
217 <<
"\nnumberOfPolygons < 1\nnumberOfPolygons= "
218 << numberOfPolygons );
221 if( numberOfIndices < numberOfPolygons )
223 itkExceptionMacro(<<
"Error reading file: " << m_FileName
224 <<
"\nnumberOfIndices < numberOfPolygons\n"
225 <<
"numberOfIndices= " << numberOfIndices <<
"\n"
226 <<
"numberOfPolygons= " << numberOfPolygons );
238 std::getline( inputFile, line,
'\n' );
239 if( inputFile.eof() )
241 itkExceptionMacro(<<
"Error reading file: " << m_FileName
242 <<
"\nFailed to read " << numberOfPolygons
243 <<
" polygons before the end of file."
244 <<
" Only read " << i+1);
247 if( line.find(
"DATA") != std::string::npos )
249 itkExceptionMacro(<<
"Error reading file: " << m_FileName
250 <<
"\nRead keyword DATA");
254 if( (got = sscanf( line.c_str(),
"%ld %ld %ld %ld", &numberOfCellPoints,
255 &ids[0], &ids[1], &ids[2] )) != 4 )
257 itkExceptionMacro(<<
"Error reading file: " << m_FileName
258 <<
"\nError parsing POLYGON cell. Expected 4 items but got "
260 <<
"Line is: " << line);
263 if( numberOfCellPoints != 3 )
265 itkExceptionMacro(<<
"Error reading file: " << m_FileName
266 <<
"\nnumberOfCellPoints != 3\n"
267 <<
"numberOfCellPoints= " << numberOfCellPoints
268 <<
". VTKPolyDataReader can only read triangles");
271 if( static_cast<long>(ids[0]) < 0 ||
272 static_cast<long>(ids[1]) < 0 ||
273 static_cast<long>(ids[2]) < 0 )
275 itkExceptionMacro(<<
"Error reading file: " << m_FileName
276 <<
"point ids must be >= 0.\n"
277 "ids=" << ids[0] <<
" " << ids[1] <<
" " << ids[2]);
280 if( static_cast<long>(ids[0]) >= numberOfPoints ||
281 static_cast<long>(ids[1]) >= numberOfPoints ||
282 static_cast<long>(ids[2]) >= numberOfPoints )
284 itkExceptionMacro(<<
"Error reading file: " << m_FileName
285 <<
"Point ids must be < number of points: "
287 <<
"\nids= " << ids[0] <<
" " << ids[1] <<
" " << ids[2]);
297 cell.TakeOwnership( triangleCell );
298 outputMesh->SetCell( i, cell );
301 bool foundPointData =
false;
303 while( !inputFile.eof() )
305 std::getline( inputFile, line,
'\n' );
307 if( line.find(
"POINT_DATA") != std::string::npos )
309 foundPointData =
true;
316 typedef typename OutputMeshType::PointDataContainer PointDataContainer;
318 outputMesh->SetPointData( PointDataContainer::New() );
319 outputMesh->GetPointData()->Reserve( numberOfPoints );
321 itkDebugMacro(
"POINT_DATA line" << line );
324 if (!inputFile.eof())
326 std::getline( inputFile, line,
'\n' );
330 itkExceptionMacro(<<
"Error reading file: " << m_FileName
331 <<
"\nUnexpected end-of-file while trying to read POINT_DATA.");
333 if (!inputFile.eof())
335 std::getline( inputFile, line,
'\n' );
339 itkExceptionMacro(<<
"Error reading file: " << m_FileName
340 <<
"\nUnexpected end-of-file while trying to read POINT_DATA.");
345 for(
int pid=0; pid < numberOfPoints; pid++ )
349 itkExceptionMacro(<<
"Error reading file: " << m_FileName
350 <<
"\nUnexpected end-of-file while trying to read POINT_DATA."
351 <<
"Failed while trying to reading point data for id: " << pid);
353 inputFile >> pointData;
354 outputMesh->SetPointData( pid, pointData );
360 template<
class TOutputMesh>
365 Superclass::PrintSelf(os,indent);
367 os << indent <<
"FileName: " << m_FileName << std::endl;
368 os << indent <<
"Version: " << m_Version << std::endl;
369 os << indent <<
"Header: " << m_Header << std::endl;