PageRenderTime 68ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Hybrid/vtkMNIObjectReader.cxx

https://github.com/b3c/VTK-5.8
C++ | 884 lines | 633 code | 130 blank | 121 comment | 157 complexity | b6603644c23386cf8b3b011474376765 MD5 | raw file
  1. /*=========================================================================
  2. Program: Visualization Toolkit
  3. Module: vtkMNIObjectReader.cxx
  4. Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  5. All rights reserved.
  6. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even
  8. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  9. PURPOSE. See the above copyright notice for more information.
  10. =========================================================================*/
  11. /*=========================================================================
  12. Copyright (c) 2006 Atamai, Inc.
  13. Use, modification and redistribution of the software, in source or
  14. binary forms, are permitted provided that the following terms and
  15. conditions are met:
  16. 1) Redistribution of the source code, in verbatim or modified
  17. form, must retain the above copyright notice, this license,
  18. the following disclaimer, and any notices that refer to this
  19. license and/or the following disclaimer.
  20. 2) Redistribution in binary form must include the above copyright
  21. notice, a copy of this license and the following disclaimer
  22. in the documentation or with other materials provided with the
  23. distribution.
  24. 3) Modified copies of the source code must be clearly marked as such,
  25. and must not be misrepresented as verbatim copies of the source code.
  26. THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS"
  27. WITHOUT EXPRESSED OR IMPLIED WARRANTY INCLUDING, BUT NOT LIMITED TO,
  28. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  29. PURPOSE. IN NO EVENT SHALL ANY COPYRIGHT HOLDER OR OTHER PARTY WHO MAY
  30. MODIFY AND/OR REDISTRIBUTE THE SOFTWARE UNDER THE TERMS OF THIS LICENSE
  31. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
  32. (INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR DATA BECOMING INACCURATE
  33. OR LOSS OF PROFIT OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF
  34. THE USE OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF THE
  35. POSSIBILITY OF SUCH DAMAGES.
  36. =========================================================================*/
  37. #include "vtkMNIObjectReader.h"
  38. #include "vtkObjectFactory.h"
  39. #include "vtkInformation.h"
  40. #include "vtkInformationVector.h"
  41. #include "vtkStreamingDemandDrivenPipeline.h"
  42. #include "vtkPolyData.h"
  43. #include "vtkPointData.h"
  44. #include "vtkCellData.h"
  45. #include "vtkPoints.h"
  46. #include "vtkCellArray.h"
  47. #include "vtkFloatArray.h"
  48. #include "vtkIntArray.h"
  49. #include "vtkUnsignedCharArray.h"
  50. #include "vtkProperty.h"
  51. #include "vtkMath.h"
  52. #include <ctype.h>
  53. #include <sys/types.h>
  54. #include <sys/stat.h>
  55. #include "vtkstd/string"
  56. #include "vtkstd/vector"
  57. #include "vtksys/SystemTools.hxx"
  58. #ifndef VTK_BINARY
  59. #define VTK_ASCII 1
  60. #define VTK_BINARY 2
  61. #endif
  62. //--------------------------------------------------------------------------
  63. vtkStandardNewMacro(vtkMNIObjectReader);
  64. #define VTK_MNIOBJ_LINE_LENGTH 256
  65. //-------------------------------------------------------------------------
  66. vtkMNIObjectReader::vtkMNIObjectReader()
  67. {
  68. this->SetNumberOfInputPorts(0);
  69. this->FileName = 0;
  70. this->Property = vtkProperty::New();
  71. // Whether file is binary or ASCII
  72. this->FileType = VTK_ASCII;
  73. // File line number for error reporting (ASCII only)
  74. this->LineNumber = 0;
  75. // State information for reading files
  76. this->InputStream = 0;
  77. this->LineText = new char[VTK_MNIOBJ_LINE_LENGTH];
  78. this->CharPointer = this->LineText;
  79. }
  80. //-------------------------------------------------------------------------
  81. vtkMNIObjectReader::~vtkMNIObjectReader()
  82. {
  83. if (this->Property)
  84. {
  85. this->Property->Delete();
  86. }
  87. if (this->FileName)
  88. {
  89. delete [] this->FileName;
  90. }
  91. if (this->LineText)
  92. {
  93. delete [] this->LineText;
  94. }
  95. }
  96. //-------------------------------------------------------------------------
  97. void vtkMNIObjectReader::PrintSelf(ostream& os, vtkIndent indent)
  98. {
  99. this->Superclass::PrintSelf(os,indent);
  100. os << indent << "FileName: "
  101. << (this->FileName ? this->FileName : "none") << "\n";
  102. os << indent << "Property: " << this->Property << "\n";
  103. if (this->Property)
  104. {
  105. this->Property->PrintSelf(os, indent.GetNextIndent());
  106. }
  107. }
  108. //-------------------------------------------------------------------------
  109. int vtkMNIObjectReader::CanReadFile(const char* fname)
  110. {
  111. // First make sure the file exists. This prevents an empty file
  112. // from being created on older compilers.
  113. struct stat fs;
  114. if(stat(fname, &fs) != 0)
  115. {
  116. return 0;
  117. }
  118. // Try to read the first line of the file.
  119. int status = 0;
  120. ifstream infile(fname);
  121. if (infile.good())
  122. {
  123. int objType = infile.get();
  124. if (infile.good())
  125. {
  126. objType = toupper(objType);
  127. if (objType == 'P' || objType != 'L' ||
  128. objType == 'M' || objType != 'F' ||
  129. objType == 'X' || objType != 'Q' ||
  130. objType == 'T')
  131. {
  132. status = 1;
  133. }
  134. }
  135. infile.close();
  136. }
  137. return status;
  138. }
  139. //-------------------------------------------------------------------------
  140. // Internal function to read in a line up to 256 characters and then
  141. // skip to the next line in the file.
  142. int vtkMNIObjectReader::ReadLine(char *line, unsigned int maxlen)
  143. {
  144. this->LineNumber++;
  145. istream &infile = *this->InputStream;
  146. infile.getline(line, maxlen);
  147. this->CharPointer = line;
  148. if (infile.fail())
  149. {
  150. if (infile.eof())
  151. {
  152. return 0;
  153. }
  154. if (infile.gcount() == 255)
  155. {
  156. // Read 256 chars; ignoring the rest of the line.
  157. infile.clear();
  158. infile.ignore(VTK_INT_MAX, '\n');
  159. vtkWarningMacro("Overlength line (limit is 255) in "
  160. << this->FileName << ":" << this->LineNumber);
  161. }
  162. }
  163. return 1;
  164. }
  165. //-------------------------------------------------------------------------
  166. // Skip all whitespace, reading additional lines if necessary
  167. int vtkMNIObjectReader::SkipWhitespace()
  168. {
  169. if (this->FileType == VTK_BINARY)
  170. {
  171. return 1;
  172. }
  173. // Only skip whitespace in ASCII files
  174. do
  175. {
  176. char *cp = this->CharPointer;
  177. // Skip leading whitespace
  178. while (isspace(*cp))
  179. {
  180. cp++;
  181. }
  182. if (*cp != '\0')
  183. {
  184. this->CharPointer = cp;
  185. return 1;
  186. }
  187. }
  188. while (this->ReadLine(this->LineText, VTK_MNIOBJ_LINE_LENGTH));
  189. return 0;
  190. }
  191. //-------------------------------------------------------------------------
  192. // Read floating-point values into a vtkFloatArray.
  193. int vtkMNIObjectReader::ParseValues(vtkDataArray *array, vtkIdType n)
  194. {
  195. int dataType = array->GetDataType();
  196. array->SetNumberOfTuples(n/array->GetNumberOfComponents());
  197. if (this->FileType == VTK_BINARY)
  198. {
  199. // The .obj files use native machine endianness
  200. this->InputStream->read((char *)array->GetVoidPointer(0),
  201. n*array->GetDataTypeSize());
  202. // Switch ABGR to RGBA colors
  203. if (dataType == VTK_UNSIGNED_CHAR &&
  204. array->GetNumberOfComponents() == 4)
  205. {
  206. unsigned char *data = (unsigned char *)array->GetVoidPointer(0);
  207. for (vtkIdType i = 0; i < n; i += 4)
  208. {
  209. unsigned char abgr[4];
  210. abgr[0] = data[0];
  211. abgr[1] = data[1];
  212. abgr[2] = data[2];
  213. abgr[3] = data[3];
  214. data[0] = abgr[3];
  215. data[1] = abgr[2];
  216. data[2] = abgr[1];
  217. data[3] = abgr[0];
  218. data += 4;
  219. }
  220. }
  221. return !this->InputStream->fail();
  222. }
  223. // The rest of the code is for ASCII files
  224. for (vtkIdType i = 0; i < n; i++)
  225. {
  226. if (!this->SkipWhitespace())
  227. {
  228. vtkErrorMacro("Unexpected end of file " << this->FileName
  229. << ":" << this->LineNumber);
  230. return 0;
  231. }
  232. char *cp = this->CharPointer;
  233. switch (dataType)
  234. {
  235. case VTK_FLOAT:
  236. {
  237. double val = strtod(cp, &cp);
  238. static_cast<vtkFloatArray *>(array)->SetValue(i, val);
  239. }
  240. break;
  241. case VTK_INT:
  242. {
  243. unsigned long lval = strtoul(cp, &cp, 10);
  244. if (lval > static_cast<unsigned long>(VTK_INT_MAX))
  245. {
  246. vtkErrorMacro("Value " << lval << " is too large for int "
  247. << this->FileName << ":" << this->LineNumber);
  248. return 0;
  249. }
  250. int val = static_cast<int>(lval);
  251. static_cast<vtkIntArray *>(array)->SetValue(i, val);
  252. }
  253. break;
  254. case VTK_UNSIGNED_CHAR:
  255. {
  256. double dval = strtod(cp, &cp);
  257. if (dval < 0.0 || dval > 1.0)
  258. {
  259. vtkErrorMacro("Color value must be [0..1] "
  260. << this->FileName << ":" << this->LineNumber);
  261. return 0;
  262. }
  263. unsigned char val = static_cast<unsigned char>(dval*255.0);
  264. static_cast<vtkUnsignedCharArray *>(array)->SetValue(i, val);
  265. }
  266. break;
  267. }
  268. // If nothing was read, there was a syntax error
  269. if (cp == this->CharPointer)
  270. {
  271. vtkErrorMacro("Syntax error " << this->FileName
  272. << ":" << this->LineNumber);
  273. return 0;
  274. }
  275. this->CharPointer = cp;
  276. }
  277. return 1;
  278. }
  279. //-------------------------------------------------------------------------
  280. // Read an integer value
  281. int vtkMNIObjectReader::ParseIdValue(vtkIdType *value)
  282. {
  283. if (this->FileType == VTK_BINARY)
  284. {
  285. int val;
  286. this->InputStream->read((char *)(&val), sizeof(int));
  287. *value = val;
  288. return !this->InputStream->fail();
  289. }
  290. // The rest of the code is for ASCII files
  291. if (!this->SkipWhitespace())
  292. {
  293. vtkErrorMacro("Unexpected end of file " << this->FileName
  294. << ":" << this->LineNumber);
  295. return 0;
  296. }
  297. char *cp = this->CharPointer;
  298. long lval = strtol(cp, &cp, 10);
  299. if (lval > static_cast<long>(VTK_INT_MAX) ||
  300. lval < static_cast<long>(VTK_INT_MIN))
  301. {
  302. vtkErrorMacro("Value " << lval << " is too large for int "
  303. << this->FileName << ":" << this->LineNumber);
  304. return 0;
  305. }
  306. *value = static_cast<int>(lval);
  307. // If no bytes were read, that means there was a syntax error
  308. if (cp == this->CharPointer)
  309. {
  310. vtkErrorMacro("Syntax error " << this->FileName
  311. << ":" << this->LineNumber);
  312. return 0;
  313. }
  314. this->CharPointer = cp;
  315. return 1;
  316. }
  317. //-------------------------------------------------------------------------
  318. int vtkMNIObjectReader::ReadProperty(vtkProperty *property)
  319. {
  320. vtkFloatArray *tmpArray = vtkFloatArray::New();
  321. int status = this->ParseValues(tmpArray, 5);
  322. if (status != 0)
  323. {
  324. property->SetAmbient(tmpArray->GetValue(0));
  325. property->SetDiffuse(tmpArray->GetValue(1));
  326. property->SetSpecular(tmpArray->GetValue(2));
  327. property->SetSpecularPower(tmpArray->GetValue(3));
  328. property->SetOpacity(tmpArray->GetValue(4));
  329. }
  330. tmpArray->Delete();
  331. return status;
  332. }
  333. //-------------------------------------------------------------------------
  334. int vtkMNIObjectReader::ReadLineThickness(vtkProperty *property)
  335. {
  336. vtkFloatArray *tmpArray = vtkFloatArray::New();
  337. int status = this->ParseValues(tmpArray, 1);
  338. if (status != 0)
  339. {
  340. property->SetLineWidth(tmpArray->GetValue(0));
  341. }
  342. tmpArray->Delete();
  343. return status;
  344. }
  345. //-------------------------------------------------------------------------
  346. int vtkMNIObjectReader::ReadNumberOfPoints(vtkIdType *numPoints)
  347. {
  348. int status = this->ParseIdValue(numPoints);
  349. if (status != 0)
  350. {
  351. if (*numPoints < 0)
  352. {
  353. // Don't support "compressed" data yet
  354. vtkErrorMacro("Bad number of points -> " << *numPoints << " "
  355. << this->FileName << ":" << this->LineNumber);
  356. status = 0;
  357. }
  358. else if (*numPoints > VTK_LARGE_ID/4)
  359. {
  360. vtkErrorMacro("Too many points -> " << *numPoints << " "
  361. << this->FileName << ":" << this->LineNumber);
  362. status = 0;
  363. }
  364. }
  365. return status;
  366. }
  367. //-------------------------------------------------------------------------
  368. int vtkMNIObjectReader::ReadNumberOfCells(vtkIdType *numCells)
  369. {
  370. int status = this->ParseIdValue(numCells);
  371. if (status != 0)
  372. {
  373. if (*numCells < 0)
  374. {
  375. vtkErrorMacro("Bad number of cells -> " << *numCells << " "
  376. << this->FileName << ":" << this->LineNumber);
  377. status = 0;
  378. }
  379. else if (*numCells > VTK_LARGE_ID/4)
  380. {
  381. vtkErrorMacro("Too many cells -> " << *numCells << " "
  382. << this->FileName << ":" << this->LineNumber);
  383. status = 0;
  384. }
  385. }
  386. return status;
  387. }
  388. //-------------------------------------------------------------------------
  389. int vtkMNIObjectReader::ReadPoints(vtkPolyData *data, vtkIdType numPoints)
  390. {
  391. vtkPoints *points = vtkPoints::New();
  392. int status = this->ParseValues(points->GetData(), 3*numPoints);
  393. if (status != 0)
  394. {
  395. data->SetPoints(points);
  396. }
  397. points->Delete();
  398. return status;
  399. }
  400. //-------------------------------------------------------------------------
  401. int vtkMNIObjectReader::ReadNormals(vtkPolyData *data, vtkIdType numPoints)
  402. {
  403. vtkFloatArray *normals = vtkFloatArray::New();
  404. normals->SetNumberOfComponents(3);
  405. int status = this->ParseValues(normals, 3*numPoints);
  406. if (status != 0)
  407. {
  408. data->GetPointData()->SetNormals(normals);
  409. }
  410. normals->Delete();
  411. return status;
  412. }
  413. //-------------------------------------------------------------------------
  414. int vtkMNIObjectReader::ReadColors(vtkProperty *property,
  415. vtkPolyData *data, vtkIdType numPoints,
  416. vtkIdType numCells)
  417. {
  418. // Find out what kind of coloring is used
  419. vtkIdType colorType = 0;
  420. if (this->ParseIdValue(&colorType) == 0)
  421. {
  422. return 0;
  423. }
  424. // Set the number of colors
  425. vtkIdType numColors = 1;
  426. if (colorType == 1)
  427. {
  428. numColors = numCells;
  429. }
  430. else if (colorType == 2)
  431. {
  432. numColors = numPoints;
  433. }
  434. else if (colorType != 0)
  435. {
  436. vtkErrorMacro("Color number must be 0, 1 or 2 " << this->FileName
  437. << ":" << this->LineNumber);
  438. return 0;
  439. }
  440. // Read the colors
  441. vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
  442. colors->SetName("Colors");
  443. colors->SetNumberOfComponents(4);
  444. int status = this->ParseValues(colors, 4*numColors);
  445. if (status != 0)
  446. {
  447. if (colorType == 0)
  448. {
  449. data->GetCellData()->SetScalars(0);
  450. data->GetPointData()->SetScalars(0);
  451. property->SetColor(colors->GetValue(0)/255.0,
  452. colors->GetValue(1)/255.0,
  453. colors->GetValue(2)/255.0);
  454. }
  455. else if (colorType == 1)
  456. {
  457. data->GetPointData()->SetScalars(0);
  458. data->GetCellData()->SetScalars(colors);
  459. property->SetColor(1.0, 1.0, 1.0);
  460. }
  461. else if (colorType == 2)
  462. {
  463. data->GetCellData()->SetScalars(0);
  464. data->GetPointData()->SetScalars(colors);
  465. property->SetColor(1.0, 1.0, 1.0);
  466. }
  467. }
  468. colors->Delete();
  469. return status;
  470. }
  471. //-------------------------------------------------------------------------
  472. int vtkMNIObjectReader::ReadCells(vtkPolyData *data, vtkIdType numCells,
  473. int cellType)
  474. {
  475. vtkIntArray *endIndices = vtkIntArray::New();
  476. vtkIntArray *cellIndices = vtkIntArray::New();
  477. vtkCellArray *cellArray = vtkCellArray::New();
  478. // Read the cell end indices
  479. int status = this->ParseValues(endIndices, numCells);
  480. // Read the cell point indices
  481. vtkIdType numIndices = 0;
  482. if (status != 0)
  483. {
  484. if (numCells > 0)
  485. {
  486. numIndices = endIndices->GetValue(numCells - 1);
  487. }
  488. status = this->ParseValues(cellIndices, numIndices);
  489. }
  490. // Create the cell array
  491. if (status != 0)
  492. {
  493. cellArray->GetData()->Allocate(
  494. numCells + endIndices->GetValue(numCells - 1));
  495. vtkPoints *points = data->GetPoints();
  496. vtkIdType numPoints = points->GetNumberOfPoints();
  497. vtkIdType lastEndIndex = 0;
  498. for (vtkIdType i = 0; i < numCells; i++)
  499. {
  500. vtkIdType endIndex = endIndices->GetValue(i);
  501. numIndices = endIndex - lastEndIndex;
  502. cellArray->InsertNextCell(numIndices);
  503. // Check that the index values are okay and create the cell
  504. for (vtkIdType j = 0; j < numIndices; j++)
  505. {
  506. vtkIdType idx = cellIndices->GetValue(lastEndIndex + j);
  507. if (idx > numPoints)
  508. {
  509. vtkErrorMacro("Index " << idx << " is greater than the"
  510. << " total number of points " << numPoints << " "
  511. << this->FileName);
  512. return 0;
  513. }
  514. cellArray->InsertCellPoint(idx);
  515. }
  516. lastEndIndex = endIndex;
  517. }
  518. if (cellType == VTK_POLYGON)
  519. {
  520. data->SetPolys(cellArray);
  521. }
  522. else if (cellType == VTK_POLY_LINE)
  523. {
  524. data->SetLines(cellArray);
  525. }
  526. }
  527. endIndices->Delete();
  528. cellIndices->Delete();
  529. cellArray->Delete();
  530. return status;
  531. }
  532. //-------------------------------------------------------------------------
  533. int vtkMNIObjectReader::ReadPolygonObject(vtkPolyData *output)
  534. {
  535. // Read the surface property
  536. if (this->ReadProperty(this->Property) == 0)
  537. {
  538. return 0;
  539. }
  540. // Read the number of points
  541. vtkIdType numPoints = 0;
  542. if (this->ReadNumberOfPoints(&numPoints) == 0)
  543. {
  544. return 0;
  545. }
  546. // Read the points
  547. if (this->ReadPoints(output, numPoints) == 0)
  548. {
  549. return 0;
  550. }
  551. // Read the normals
  552. if (this->ReadNormals(output, numPoints) == 0)
  553. {
  554. return 0;
  555. }
  556. // Read the number of items
  557. vtkIdType numCells = 0;
  558. if (this->ReadNumberOfCells(&numCells) == 0)
  559. {
  560. return 0;
  561. }
  562. // Read the colors
  563. if (this->ReadColors(this->Property, output, numPoints, numCells) == 0)
  564. {
  565. return 0;
  566. }
  567. // Read the cells
  568. if (this->ReadCells(output, numCells, VTK_POLYGON) == 0)
  569. {
  570. return 0;
  571. }
  572. return 1;
  573. }
  574. //-------------------------------------------------------------------------
  575. int vtkMNIObjectReader::ReadLineObject(vtkPolyData *output)
  576. {
  577. // Read the line thickness
  578. if (this->ReadLineThickness(this->Property) == 0)
  579. {
  580. return 0;
  581. }
  582. // Read the number of points
  583. vtkIdType numPoints = 0;
  584. if (this->ReadNumberOfPoints(&numPoints) == 0)
  585. {
  586. return 0;
  587. }
  588. // Read the points
  589. if (this->ReadPoints(output, numPoints) == 0)
  590. {
  591. return 0;
  592. }
  593. // Read the number of items
  594. vtkIdType numCells = 0;
  595. if (this->ReadNumberOfCells(&numCells) == 0)
  596. {
  597. return 0;
  598. }
  599. // Read the colors
  600. if (this->ReadColors(this->Property, output, numPoints, numCells) == 0)
  601. {
  602. return 0;
  603. }
  604. // Read the cells
  605. if (this->ReadCells(output, numCells, VTK_POLY_LINE) == 0)
  606. {
  607. return 0;
  608. }
  609. return 1;
  610. }
  611. //-------------------------------------------------------------------------
  612. int vtkMNIObjectReader::ReadFile(vtkPolyData *output)
  613. {
  614. // Initialize the property to default values
  615. vtkProperty *property = vtkProperty::New();
  616. this->Property->DeepCopy(property);
  617. property->Delete();
  618. // Check that the file name has been set.
  619. if (!this->FileName)
  620. {
  621. vtkErrorMacro("ReadFile: No file name has been set");
  622. return 0;
  623. }
  624. // Make sure that the file exists.
  625. struct stat fs;
  626. if(stat(this->FileName, &fs) != 0)
  627. {
  628. vtkErrorMacro("ReadFile: Can't open file " << this->FileName);
  629. return 0;
  630. }
  631. // Make sure that the file is readable.
  632. ifstream infile(this->FileName, ios::in);
  633. if (infile.fail())
  634. {
  635. vtkErrorMacro("ReadFile: Can't read the file " << this->FileName);
  636. return 0;
  637. }
  638. // Check object type
  639. int objType = infile.get();
  640. int fileType = VTK_ASCII;
  641. if (infile.fail())
  642. {
  643. vtkErrorMacro("ReadFile: I/O error for file " << this->FileName);
  644. infile.close();
  645. return 0;
  646. }
  647. if (islower(objType))
  648. {
  649. objType = toupper(objType);
  650. fileType = VTK_BINARY;
  651. }
  652. if (objType != 'P' && objType != 'L' &&
  653. objType != 'M' && objType != 'F' &&
  654. objType != 'X' && objType != 'Q' &&
  655. objType != 'T' && objType != 'V')
  656. {
  657. vtkErrorMacro("ReadFile: File is not a MNI obj file: "
  658. << this->FileName);
  659. infile.close();
  660. return 0;
  661. }
  662. #ifdef _WIN32
  663. // Re-open file as binary (only necessary on Windows)
  664. if (fileType == VTK_BINARY)
  665. {
  666. infile.close();
  667. infile.open(this->FileName, ios::in | ios::binary);
  668. infile.get();
  669. }
  670. #endif
  671. this->InputStream = &infile;
  672. this->LineNumber = 0;
  673. this->FileType = fileType;
  674. int status = 1;
  675. if (this->FileType == VTK_ASCII)
  676. {
  677. // Read the line, include the type char in line text for
  678. // use in error reporting
  679. this->LineText[0] = objType;
  680. status = this->ReadLine(&this->LineText[1], VTK_MNIOBJ_LINE_LENGTH-1);
  681. }
  682. if (status != 0)
  683. {
  684. switch (objType)
  685. {
  686. case 'P':
  687. status = this->ReadPolygonObject(output);
  688. break;
  689. case 'L':
  690. status = this->ReadLineObject(output);
  691. break;
  692. case 'M':
  693. case 'F':
  694. case 'X':
  695. case 'Q':
  696. case 'T':
  697. case 'V':
  698. {
  699. vtkErrorMacro("ReadFile: Reading of obj type \"" << (char)objType <<
  700. "\" is not supported: " << this->FileName);
  701. status = 0;
  702. }
  703. break;
  704. }
  705. }
  706. if (this->FileType == VTK_BINARY)
  707. {
  708. if (infile.fail())
  709. {
  710. if (infile.eof())
  711. {
  712. vtkErrorMacro("Premature end of binary file " << this->FileName);
  713. }
  714. else
  715. {
  716. vtkErrorMacro("Error encountered while reading " << this->FileName);
  717. }
  718. }
  719. }
  720. this->InputStream = 0;
  721. infile.close();
  722. return status;
  723. }
  724. //-------------------------------------------------------------------------
  725. int vtkMNIObjectReader::RequestData(
  726. vtkInformation *vtkNotUsed(request),
  727. vtkInformationVector **vtkNotUsed(inputVector),
  728. vtkInformationVector *outputVector)
  729. {
  730. // get the info object
  731. vtkInformation *outInfo = outputVector->GetInformationObject(0);
  732. // get the ouptut
  733. vtkPolyData *output = vtkPolyData::SafeDownCast(
  734. outInfo->Get(vtkDataObject::DATA_OBJECT()));
  735. // all of the data in the first piece.
  736. if (outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER())
  737. > 0)
  738. {
  739. return 0;
  740. }
  741. // read the file
  742. return this->ReadFile(output);
  743. }