Orfeo Toolbox  3.16
itkVTKPolyDataReader.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkVTKPolyDataReader.txx,v $
5  Language: C++
6  Date: $Date: 2009-12-02 18:13:18 $
7  Version: $Revision: 1.19 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkVTKPolyDataReader_txx
18 #define __itkVTKPolyDataReader_txx
19 
20 #include "itkVTKPolyDataReader.h"
21 #include <fstream>
22 #include <stdio.h>
23 #include <string.h>
24 
25 namespace itk
26 {
27 
28 //
29 // Constructor
30 //
31 template<class TOutputMesh>
34 {
35  //
36  // Create the output
37  //
38  typename TOutputMesh::Pointer output = TOutputMesh::New();
40  this->ProcessObject::SetNthOutput(0, output.GetPointer());
41 }
42 
43 template<class TOutputMesh>
44 void
47 {
48  typename OutputMeshType::Pointer outputMesh = this->GetOutput();
49 
50  outputMesh->SetCellsAllocationMethod(
51  OutputMeshType::CellsAllocatedDynamicallyCellByCell );
52 
53  if( m_FileName == "" )
54  {
55  itkExceptionMacro(<< "No input FileName");
56  }
57 
58  //
59  // Read input file
60  //
61  std::ifstream inputFile( m_FileName.c_str() );
62 
63  if( !inputFile.is_open() )
64  {
65  itkExceptionMacro(<< "Unable to open file\n"
66  << "inputFilename= " << m_FileName );
67  }
68 
69  inputFile.imbue(std::locale::classic());
70  std::string line;
71 
72  // The first line must be "# vtk DataFile Version x.x" where x.x can
73  // vary
74  std::getline( inputFile, m_Version, '\n' );
75  if (inputFile.fail())
76  {
77  itkExceptionMacro(<< "Error reading file: " << m_FileName
78  << "\nUnexpected end-of-file trying to read first line.");
79  }
80  if (m_Version.find("# vtk DataFile Version ") == std::string::npos)
81  {
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.");
85  }
86 
87  // Next is a one line description
88  std::getline( inputFile, m_Header, '\n' );
89  if (inputFile.eof())
90  {
91  itkExceptionMacro(<< "Error reading file: " << m_FileName
92  << "\nUnexpected end-of-file trying to read header.");
93  }
94 
95  // Next is the file format
96  std::getline( inputFile, line, '\n' );
97  if (inputFile.eof())
98  {
99  itkExceptionMacro(<< "Error reading file: " << m_FileName
100  << "\nUnexpected end-of-file trying to file format.");
101  }
102  if (line.find("ASCII") == std::string::npos)
103  {
104  itkExceptionMacro(<< "Error reading file: " << m_FileName
105  << "\nFile format is " << line
106  << " but only ASCII files can be read.");
107  }
108 
109  bool foundPoints = false;
110  while( !inputFile.eof() )
111  {
112  std::getline( inputFile, line, '\n' );
113 
114  if( line.find("POINTS") != std::string::npos )
115  {
116  foundPoints = true;
117  break;
118  }
119  }
120 
121  if (!foundPoints)
122  {
123  itkExceptionMacro(<< "Error reading file: " << m_FileName
124  << "\nUnexpected end-of-file before finding POINTS.");
125  }
126  itkDebugMacro("POINTS line" << line );
127 
128  std::string pointLine( line, strlen("POINTS "), line.length() );
129  itkDebugMacro("pointLine " << pointLine );
130 
131  int numberOfPoints = -1;
132 
133  if( sscanf(pointLine.c_str(),"%d",&numberOfPoints) != 1 )
134  {
135  itkExceptionMacro(<< "Error reading file: " << m_FileName
136  << "\nFailed to read numberOfPoints.\n"
137  << " pointLine= " << pointLine );
138  }
139 
140  itkDebugMacro("numberOfPoints= " << numberOfPoints );
141 
142  if( numberOfPoints < 1 )
143  {
144  itkExceptionMacro(<< "Error reading file: " << m_FileName
145  << "numberOfPoints < 1"
146  << " numberOfPoints line= " << numberOfPoints );
147  }
148 
149  outputMesh->GetPoints()->Reserve( numberOfPoints );
150 
151  //
152  // Load the point coordinates into the itk::Mesh
153  //
154 
155  PointType point;
156 
157  for( int i=0; i < numberOfPoints; i++ )
158  {
159  inputFile >> point;
160  if (inputFile.eof())
161  {
162  itkExceptionMacro(<< "Error while reading file: " << m_FileName
163  << "\nUnexpected end-of-file trying to read points.");
164  }
165  if (inputFile.fail())
166  {
167  itkExceptionMacro(<< "Error reading file: " << m_FileName
168  << "\nInput could not be interpreted as a point.");
169  }
170  outputMesh->SetPoint( i, point );
171  }
172 
173  // Continue searching for the POLYGONS line
174  bool foundPolygons = false;
175  while( !inputFile.eof() )
176  {
177  std::getline( inputFile, line, '\n' );
178  if (line.find("POLYGONS") != std::string::npos )
179  {
180  foundPolygons = true;
181  break;
182  }
183  }
184 
185  if (!foundPolygons)
186  {
187  itkExceptionMacro(<< "Error reading file: " << m_FileName
188  << "\nUnexpected end-of-file before finding POLYGONS.");
189  }
190 
191  itkDebugMacro( "POLYGONS line" << line );
192 
193  std::string polygonLine( line, strlen("POLYGONS "), line.length() );
194  itkDebugMacro( "polygonLine " << polygonLine );
195 
196  //
197  // Read the number of polygons
198  //
199 
200  CellIdentifier numberOfPolygons = 0;
201  CellIdentifier numberOfIndices = 0;
202 
203  if( sscanf( polygonLine.c_str(), "%ld %ld", &numberOfPolygons,
204  &numberOfIndices ) != 2 )
205  {
206  itkExceptionMacro(<< "Error reading file: " << m_FileName
207  << "\nFailed to read numberOfPolygons from subline2"
208  << "\npolygonLine = " << polygonLine );
209  }
210 
211  itkDebugMacro("numberOfPolygons " << numberOfPolygons );
212  itkDebugMacro("numberOfIndices " << numberOfIndices );
213 
214  if( numberOfPolygons < 1 )
215  {
216  itkExceptionMacro(<< "Error reading file: " << m_FileName
217  << "\nnumberOfPolygons < 1\nnumberOfPolygons= "
218  << numberOfPolygons );
219  }
220 
221  if( numberOfIndices < numberOfPolygons )
222  {
223  itkExceptionMacro(<< "Error reading file: " << m_FileName
224  << "\nnumberOfIndices < numberOfPolygons\n"
225  << "numberOfIndices= " << numberOfIndices << "\n"
226  << "numberOfPolygons= " << numberOfPolygons );
227  }
228 
229  //
230  // Load the polygons into the itk::Mesh
231  //
232 
233  PointIdentifier numberOfCellPoints;
234  long ids[3];
235 
236  for(CellIdentifier i=0; i<numberOfPolygons; i++)
237  {
238  std::getline( inputFile, line, '\n' );
239  if( inputFile.eof() )
240  {
241  itkExceptionMacro(<< "Error reading file: " << m_FileName
242  << "\nFailed to read " << numberOfPolygons
243  << " polygons before the end of file."
244  << " Only read " << i+1);
245  }
246 
247  if( line.find("DATA") != std::string::npos )
248  {
249  itkExceptionMacro(<< "Error reading file: " << m_FileName
250  << "\nRead keyword DATA");
251  }
252 
253  int got;
254  if( (got = sscanf( line.c_str(), "%ld %ld %ld %ld", &numberOfCellPoints,
255  &ids[0], &ids[1], &ids[2] )) != 4 )
256  {
257  itkExceptionMacro(<< "Error reading file: " << m_FileName
258  << "\nError parsing POLYGON cell. Expected 4 items but got "
259  << got << std::endl
260  << "Line is: " << line);
261  }
262 
263  if( numberOfCellPoints != 3 )
264  {
265  itkExceptionMacro(<< "Error reading file: " << m_FileName
266  << "\nnumberOfCellPoints != 3\n"
267  << "numberOfCellPoints= " << numberOfCellPoints
268  << ". VTKPolyDataReader can only read triangles");
269  }
270 
271  if( static_cast<long>(ids[0]) < 0 ||
272  static_cast<long>(ids[1]) < 0 ||
273  static_cast<long>(ids[2]) < 0 )
274  {
275  itkExceptionMacro(<< "Error reading file: " << m_FileName
276  << "point ids must be >= 0.\n"
277  "ids=" << ids[0] << " " << ids[1] << " " << ids[2]);
278  }
279 
280  if( static_cast<long>(ids[0]) >= numberOfPoints ||
281  static_cast<long>(ids[1]) >= numberOfPoints ||
282  static_cast<long>(ids[2]) >= numberOfPoints )
283  {
284  itkExceptionMacro(<< "Error reading file: " << m_FileName
285  << "Point ids must be < number of points: "
286  << numberOfPoints
287  << "\nids= " << ids[0] << " " << ids[1] << " " << ids[2]);
288  }
289 
290  CellAutoPointer cell;
291  TriangleCellType * triangleCell = new TriangleCellType;
292  for( PointIdentifier k = 0; k < numberOfCellPoints; k++ )
293  {
294  triangleCell->SetPointId( k, ids[k] );
295  }
296 
297  cell.TakeOwnership( triangleCell );
298  outputMesh->SetCell( i, cell );
299  }
300 
301  bool foundPointData = false;
302 
303  while( !inputFile.eof() )
304  {
305  std::getline( inputFile, line, '\n' );
306 
307  if( line.find("POINT_DATA") != std::string::npos )
308  {
309  foundPointData = true;
310  break;
311  }
312  }
313 
314  if( foundPointData )
315  {
316  typedef typename OutputMeshType::PointDataContainer PointDataContainer;
317 
318  outputMesh->SetPointData( PointDataContainer::New() );
319  outputMesh->GetPointData()->Reserve( numberOfPoints );
320 
321  itkDebugMacro("POINT_DATA line" << line );
322 
323  // Skip two lines
324  if (!inputFile.eof())
325  {
326  std::getline( inputFile, line, '\n' );
327  }
328  else
329  {
330  itkExceptionMacro(<< "Error reading file: " << m_FileName
331  << "\nUnexpected end-of-file while trying to read POINT_DATA.");
332  }
333  if (!inputFile.eof())
334  {
335  std::getline( inputFile, line, '\n' );
336  }
337  else
338  {
339  itkExceptionMacro(<< "Error reading file: " << m_FileName
340  << "\nUnexpected end-of-file while trying to read POINT_DATA.");
341  }
342 
343  double pointData;
344 
345  for( int pid=0; pid < numberOfPoints; pid++ )
346  {
347  if (inputFile.eof())
348  {
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);
352  }
353  inputFile >> pointData;
354  outputMesh->SetPointData( pid, pointData );
355  }
356  }
357  inputFile.close();
358 }
359 
360 template<class TOutputMesh>
361 void
363 ::PrintSelf( std::ostream& os, Indent indent ) const
364 {
365  Superclass::PrintSelf(os,indent);
366 
367  os << indent << "FileName: " << m_FileName << std::endl;
368  os << indent << "Version: " << m_Version << std::endl;
369  os << indent << "Header: " << m_Header << std::endl;
370 }
371 
372 } //end of namespace itk
373 
374 
375 #endif

Generated at Sun Feb 3 2013 00:14:19 for Orfeo Toolbox with doxygen 1.8.1.1