vtkDataReader.cxx 88.9 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
1 2
/*=========================================================================

Ken Martin's avatar
Ken Martin committed
3
  Program:   Visualization Toolkit
Ken Martin's avatar
Ken Martin committed
4
  Module:    vtkDataReader.cxx
Will Schroeder's avatar
Will Schroeder committed
5

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Ken Martin's avatar
Ken Martin committed
9

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Will Schroeder's avatar
Will Schroeder committed
13 14

=========================================================================*/
Ken Martin's avatar
Ken Martin committed
15
#include "vtkDataReader.h"
16

17
#include "vtkBitArray.h"
18 19
#include "vtkByteSwap.h"
#include "vtkCellData.h"
20 21
#include "vtkCharArray.h"
#include "vtkDoubleArray.h"
22 23
#include "vtkErrorCode.h"
#include "vtkFieldData.h"
24
#include "vtkFloatArray.h"
25
#include "vtkGraph.h"
26
#include "vtkIdTypeArray.h"
27
#include "vtkInformation.h"
28 29 30 31 32 33 34 35 36
#include "vtkInformationDoubleKey.h"
#include "vtkInformationDoubleVectorKey.h"
#include "vtkInformationIdTypeKey.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationIntegerVectorKey.h"
#include "vtkInformationKeyLookup.h"
#include "vtkInformationStringKey.h"
#include "vtkInformationStringVectorKey.h"
#include "vtkInformationUnsignedLongKey.h"
37
#include "vtkInformationVector.h"
38
#include "vtkIntArray.h"
39
#include "vtkLegacyReaderVersion.h"
40 41
#include "vtkLongArray.h"
#include "vtkLookupTable.h"
42
#include "vtkObjectFactory.h"
43 44 45 46
#include "vtkPointData.h"
#include "vtkPointSet.h"
#include "vtkRectilinearGrid.h"
#include "vtkShortArray.h"
47
#include "vtkStreamingDemandDrivenPipeline.h"
48
#include "vtkStringArray.h"
49
#include "vtkTable.h"
50
#include "vtkTypeInt64Array.h"
51
#include "vtkUnicodeStringArray.h"
52 53 54 55
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedIntArray.h"
#include "vtkUnsignedLongArray.h"
#include "vtkUnsignedShortArray.h"
56
#include "vtkVariantArray.h"
57
#include <sstream>
58

59 60
#include "vtkTypeUInt64Array.h"

Sean McBride's avatar
Sean McBride committed
61
#include <cctype>
62
#include <sys/stat.h>
Brad King's avatar
Brad King committed
63

64 65 66 67 68 69 70
// I need a safe way to read a line of arbitrary length.  It exists on
// some platforms but not others so I'm afraid I have to write it
// myself.
// This function is also defined in Infovis/vtkDelimitedTextReader.cxx,
// so it would be nice to put this in a common file.
static int my_getline(istream& stream, vtkStdString &output, char delim='\n');

Brad King's avatar
Brad King committed
71
vtkStandardNewMacro(vtkDataReader);
72

73 74
vtkCxxSetObjectMacro(vtkDataReader, InputArray, vtkCharArray);

75 76 77 78 79 80 81
// this undef is required on the hp. vtkMutexLock ends up including
// /usr/inclue/dce/cma_ux.h which has the gall to #define read as cma_read

#ifdef read
#undef read
#endif

Will Schroeder's avatar
Will Schroeder committed
82
// Construct object.
Ken Martin's avatar
Ken Martin committed
83
vtkDataReader::vtkDataReader()
Will Schroeder's avatar
Will Schroeder committed
84
{
85
  this->FileType = VTK_ASCII;
Will Schroeder's avatar
Will Schroeder committed
86
  this->FileName = NULL;
Will Schroeder's avatar
Will Schroeder committed
87 88 89 90 91 92
  this->ScalarsName = NULL;
  this->VectorsName = NULL;
  this->TensorsName = NULL;
  this->NormalsName = NULL;
  this->TCoordsName = NULL;
  this->LookupTableName = NULL;
93
  this->FieldDataName = NULL;
Will Schroeder's avatar
Will Schroeder committed
94
  this->ScalarLut = NULL;
Ken Martin's avatar
Ken Martin committed
95
  this->InputString = NULL;
96
  this->InputStringLength = 0;
Ken Martin's avatar
Ken Martin committed
97 98
  this->InputStringPos = 0;
  this->ReadFromInputString = 0;
Ken Martin's avatar
Ken Martin committed
99
  this->IS = NULL;
Ken Martin's avatar
Ken Martin committed
100
  this->Header = NULL;
101

102 103
  this->InputArray = 0;

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
  this->NumberOfScalarsInFile = 0;
  this->ScalarsNameInFile = NULL;
  this->ScalarsNameAllocSize = 0;
  this->NumberOfVectorsInFile = 0;
  this->VectorsNameInFile = NULL;
  this->VectorsNameAllocSize = 0;
  this->NumberOfTensorsInFile = 0;
  this->TensorsNameInFile = NULL;
  this->TensorsNameAllocSize = 0;
  this->NumberOfTCoordsInFile = 0;
  this->TCoordsNameInFile = NULL;
  this->TCoordsNameAllocSize = 0;
  this->NumberOfNormalsInFile = 0;
  this->NormalsNameInFile = NULL;
  this->NormalsNameAllocSize = 0;
  this->NumberOfFieldDataInFile = 0;
  this->FieldDataNameInFile = NULL;
  this->FieldDataNameAllocSize = 0;
Charles Law's avatar
Charles Law committed
122 123 124 125 126 127 128 129

  this->ReadAllScalars = 0;
  this->ReadAllVectors = 0;
  this->ReadAllNormals = 0;
  this->ReadAllTensors = 0;
  this->ReadAllColorScalars = 0;
  this->ReadAllTCoords = 0;
  this->ReadAllFields = 0;
130 131
  this->FileMajorVersion = 0;
  this->FileMinorVersion = 0;
132 133 134

  this->SetNumberOfInputPorts(0);
  this->SetNumberOfOutputPorts(1);
135
}
Will Schroeder's avatar
Will Schroeder committed
136

Ken Martin's avatar
Ken Martin committed
137
vtkDataReader::~vtkDataReader()
Will Schroeder's avatar
Will Schroeder committed
138
{
139 140 141 142 143 144 145 146 147 148 149
  delete [] this->FileName;
  delete [] this->ScalarsName;
  delete [] this->VectorsName;
  delete [] this->TensorsName;
  delete [] this->NormalsName;
  delete [] this->TCoordsName;
  delete [] this->LookupTableName;
  delete [] this->FieldDataName;
  delete [] this->ScalarLut;
  delete [] this->InputString;
  delete [] this->Header;
150

151
  this->SetInputArray(0);
152
  this->InitializeCharacteristics();
153
  delete this->IS;
Will Schroeder's avatar
Will Schroeder committed
154 155
}

156
void vtkDataReader::SetInputString(const char *in)
157 158
{
  int len = 0;
159 160
  if (in != NULL)
    {
161
    len = static_cast<int>(strlen(in));
162
    }
163
  this->SetInputString(in, len);
164 165
}

166
void vtkDataReader::SetBinaryInputString(const char *in, int len)
Ken Martin's avatar
Ken Martin committed
167
{
168
  this->SetInputString(in, len);
Ken Martin's avatar
Ken Martin committed
169
}
170

171
void vtkDataReader::SetInputString(const char *in, int len)
172
{
173 174
  if (this->Debug)
    {
175 176
    vtkDebugMacro(<< "SetInputString len: " << len
      << " in: " << (in ? in : "(null)"));
177 178
    }

179
  if (this->InputString && in && strncmp(in, this->InputString, len) == 0)
Charles Law's avatar
Charles Law committed
180 181 182
    {
    return;
    }
183

184
  delete [] this->InputString;
185 186 187

  if (in && len>0)
    {
188 189 190 191 192
    // Add a NULL terminator so that GetInputString
    // callers (from wrapped languages) get a valid
    // C string in *ALL* cases...
    //
    this->InputString = new char[len+1];
193
    memcpy(this->InputString,in,len);
194
    this->InputString[len] = 0;
195
    this->InputStringLength = len;
196 197 198 199
    }
   else
    {
    this->InputString = NULL;
200
    this->InputStringLength = 0;
201 202 203 204
    }

  this->Modified();
}
205

Ken Martin's avatar
Ken Martin committed
206 207 208 209
// Internal function to read in a line up to 256 characters.
// Returns zero if there was an error.
int vtkDataReader::ReadLine(char result[256])
{
Ken Martin's avatar
Ken Martin committed
210
  this->IS->getline(result,256);
211
  if (this->IS->fail())
Bill Lorensen's avatar
Bill Lorensen committed
212
    {
213
    if (this->IS->eof())
214 215 216 217 218 219 220 221 222
      {
      return 0;
      }
    if (this->IS->gcount() == 255)
      {
      // Read 256 chars; ignoring the rest of the line.
      this->IS->clear();
      this->IS->ignore(VTK_INT_MAX, '\n');
      }
Bill Lorensen's avatar
Bill Lorensen committed
223
    }
Ken Martin's avatar
Ken Martin committed
224
  return 1;
Ken Martin's avatar
Ken Martin committed
225 226 227 228 229 230
}

// Internal function to read in a string up to 256 characters.
// Returns zero if there was an error.
int vtkDataReader::ReadString(char result[256])
{
231
  this->IS->width(256);
Ken Martin's avatar
Ken Martin committed
232
  *this->IS >> result;
Bill Lorensen's avatar
Bill Lorensen committed
233 234 235 236
  if (this->IS->fail())
    {
    return 0;
    }
Ken Martin's avatar
Ken Martin committed
237
  return 1;
Will Schroeder's avatar
Will Schroeder committed
238 239
}

Ken Martin's avatar
Ken Martin committed
240 241
// Internal function to read in an integer value.
// Returns zero if there was an error.
242
int vtkDataReader::Read(char *result)
Ken Martin's avatar
Ken Martin committed
243
{
Will Schroeder's avatar
Will Schroeder committed
244 245
  int intData;
  *this->IS >> intData;
Bill Lorensen's avatar
Bill Lorensen committed
246 247 248 249
  if (this->IS->fail())
    {
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
250 251

  *result = (char) intData;
Ken Martin's avatar
Ken Martin committed
252
  return 1;
Ken Martin's avatar
Ken Martin committed
253 254
}

255
int vtkDataReader::Read(unsigned char *result)
Ken Martin's avatar
Ken Martin committed
256
{
Will Schroeder's avatar
Will Schroeder committed
257 258
  int intData;
  *this->IS >> intData;
Bill Lorensen's avatar
Bill Lorensen committed
259 260 261 262
  if (this->IS->fail())
    {
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
263 264

  *result = (unsigned char) intData;
265 266 267 268 269 270
  return 1;
}

int vtkDataReader::Read(short *result)
{
  *this->IS >> *result;
Bill Lorensen's avatar
Bill Lorensen committed
271 272 273 274
  if (this->IS->fail())
    {
    return 0;
    }
275
  return 1;
Ken Martin's avatar
Ken Martin committed
276 277
}

278
int vtkDataReader::Read(unsigned short *result)
Ken Martin's avatar
Ken Martin committed
279
{
280
  *this->IS >> *result;
Bill Lorensen's avatar
Bill Lorensen committed
281 282 283 284
  if (this->IS->fail())
    {
    return 0;
    }
285
  return 1;
Ken Martin's avatar
Ken Martin committed
286 287
}

288
int vtkDataReader::Read(int *result)
Ken Martin's avatar
Ken Martin committed
289
{
290
  *this->IS >> *result;
Bill Lorensen's avatar
Bill Lorensen committed
291 292 293 294
  if (this->IS->fail())
    {
    return 0;
    }
295
  return 1;
Ken Martin's avatar
Ken Martin committed
296 297
}

298 299 300
int vtkDataReader::Read(unsigned int *result)
{
  *this->IS >> *result;
Bill Lorensen's avatar
Bill Lorensen committed
301 302 303 304
  if (this->IS->fail())
    {
    return 0;
    }
305 306 307 308 309 310
  return 1;
}

int vtkDataReader::Read(long *result)
{
  *this->IS >> *result;
Bill Lorensen's avatar
Bill Lorensen committed
311 312 313 314
  if (this->IS->fail())
    {
    return 0;
    }
315 316 317 318 319 320
  return 1;
}

int vtkDataReader::Read(unsigned long *result)
{
  *this->IS >> *result;
Bill Lorensen's avatar
Bill Lorensen committed
321 322 323 324
  if (this->IS->fail())
    {
    return 0;
    }
325 326 327
  return 1;
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
int vtkDataReader::Read(long long *result)
{
  *this->IS >> *result;
  if (this->IS->fail())
    {
    return 0;
    }
  return 1;
}

int vtkDataReader::Read(unsigned long long *result)
{
  *this->IS >> *result;
  if (this->IS->fail())
    {
    return 0;
    }
  return 1;
}

348 349 350
int vtkDataReader::Read(float *result)
{
  *this->IS >> *result;
Bill Lorensen's avatar
Bill Lorensen committed
351 352 353 354
  if (this->IS->fail())
    {
    return 0;
    }
355 356 357 358
  return 1;
}

int vtkDataReader::Read(double *result)
Will Schroeder's avatar
Will Schroeder committed
359
{
Ken Martin's avatar
Ken Martin committed
360
  *this->IS >> *result;
361
  if (this->IS->fail())
Bill Lorensen's avatar
Bill Lorensen committed
362 363 364
    {
    return 0;
    }
Ken Martin's avatar
Ken Martin committed
365
  return 1;
Ken Martin's avatar
Ken Martin committed
366 367
}

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
size_t vtkDataReader::Peek(char *str, size_t n)
{
  if (n == 0)
    {
    return 0;
    }

  this->IS->read(str, n);
  std::streamsize len = this->IS->gcount();

  if (!*this->IS)
    {
    this->IS->clear();
    }

  this->IS->seekg(-len, std::ios_base::cur);

  return len;
}
387

Ken Martin's avatar
Ken Martin committed
388 389 390
// Open a vtk data file. Returns zero if error.
int vtkDataReader::OpenVTKFile()
{
391 392 393 394
  if ( this->IS != NULL )
    {
    this->CloseVTKFile ();
    }
Ken Martin's avatar
Ken Martin committed
395 396
  if (this->ReadFromInputString)
    {
397 398 399
    if (this->InputArray)
      {
      vtkDebugMacro(<< "Reading from InputArray");
400
      std::string str(this->InputArray->GetPointer(0),
401 402
        static_cast<size_t>( this->InputArray->GetNumberOfTuples()  *
                             this->InputArray->GetNumberOfComponents()) );
403
      this->IS = new std::istringstream(str);
404 405 406
      return 1;
      }
    else if (this->InputString)
Ken Martin's avatar
Ken Martin committed
407 408
      {
      vtkDebugMacro(<< "Reading from InputString");
409
      std::string str(this->InputString, this->InputStringLength);
410
      this->IS = new std::istringstream(str);
Ken Martin's avatar
Ken Martin committed
411 412 413 414 415 416 417
      return 1;
      }
    }
  else
    {
    vtkDebugMacro(<< "Opening vtk file");

418
    if ( !this->FileName || (strlen(this->FileName) == 0))
Ken Martin's avatar
Ken Martin committed
419 420
      {
      vtkErrorMacro(<< "No file specified!");
Andy Cedilnik's avatar
Andy Cedilnik committed
421
      this->SetErrorCode( vtkErrorCode::NoFileNameError );
Ken Martin's avatar
Ken Martin committed
422
      return 0;
423
      }
424 425 426 427

    // first make sure the file exists, this prevents an empty file from
    // being created on older compilers
    struct stat fs;
428
    if (stat(this->FileName, &fs) != 0)
429 430 431 432 433
      {
      vtkErrorMacro(<< "Unable to open file: "<< this->FileName);
      this->SetErrorCode( vtkErrorCode::CannotOpenFileError );
      return 0;
      }
434
    this->IS = new ifstream(this->FileName, ios::in | ios::binary);
435
    if (this->IS->fail())
Ken Martin's avatar
Ken Martin committed
436
      {
Will Schroeder's avatar
Will Schroeder committed
437
      vtkErrorMacro(<< "Unable to open file: "<< this->FileName);
438 439
      delete this->IS;
      this->IS = NULL;
Andy Cedilnik's avatar
Andy Cedilnik committed
440
      this->SetErrorCode( vtkErrorCode::CannotOpenFileError );
Ken Martin's avatar
Ken Martin committed
441 442 443 444 445 446 447 448 449 450 451 452 453
      return 0;
      }
    return 1;
    }

  return 0;
}

// Read the header of a vtk data file. Returns 0 if error.
int vtkDataReader::ReadHeader()
{
  char line[256];

Ken Martin's avatar
Ken Martin committed
454
  vtkDebugMacro(<< "Reading vtk file header");
Ken Martin's avatar
Ken Martin committed
455 456 457
  //
  // read header
  //
Ken Martin's avatar
Ken Martin committed
458
  if (!this->ReadLine(line))
Will Schroeder's avatar
Will Schroeder committed
459
    {
460
    vtkErrorMacro(<<"Premature EOF reading first line! " << " for file: "
461
                  << (this->FileName?this->FileName:"(Null FileName)"));
Andy Cedilnik's avatar
Andy Cedilnik committed
462
    this->SetErrorCode( vtkErrorCode::PrematureEndOfFileError );
Will Schroeder's avatar
Will Schroeder committed
463 464
    return 0;
    }
465 466
  const int VERSION_PREFIX_LENGTH = 22;
  if ( strncmp ("# vtk DataFile Version", line, VERSION_PREFIX_LENGTH) )
Will Schroeder's avatar
Will Schroeder committed
467
    {
468
    vtkErrorMacro(<< "Unrecognized file type: "<< line << " for file: "
469
                  << (this->FileName?this->FileName:"(Null FileName)"));
470

Andy Cedilnik's avatar
Andy Cedilnik committed
471
    this->SetErrorCode( vtkErrorCode::UnrecognizedFileTypeError );
Will Schroeder's avatar
Will Schroeder committed
472 473
    return 0;
    }
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
  if (sscanf (line + VERSION_PREFIX_LENGTH,
              "%d.%d", &this->FileMajorVersion, &this->FileMinorVersion) != 2)
    {
    vtkWarningMacro(<< "Cannot read file version: " << line << " for file: "
                    << (this->FileName?this->FileName:"(Null FileName)"));
    this->FileMajorVersion = 0;
    this->FileMinorVersion = 0;
    }
  if (this->FileMajorVersion > vtkLegacyReaderMajorVersion ||
      (this->FileMajorVersion == vtkLegacyReaderMajorVersion &&
       this->FileMinorVersion > vtkLegacyReaderMinorVersion))
    {
    // newer file than the reader version
    vtkWarningMacro(
      << "Reading file version: " << this->FileMajorVersion
      << "." << this->FileMinorVersion << " with older reader version "
      << vtkLegacyReaderMajorVersion << "." << vtkLegacyReaderMinorVersion);
    }

Ken Martin's avatar
Ken Martin committed
493 494 495
  //
  // read title
  //
Ken Martin's avatar
Ken Martin committed
496
  if (!this->ReadLine(line))
Will Schroeder's avatar
Will Schroeder committed
497
    {
498
    vtkErrorMacro(<<"Premature EOF reading title! " << " for file: "
499
                  << (this->FileName?this->FileName:"(Null FileName)"));
Andy Cedilnik's avatar
Andy Cedilnik committed
500
    this->SetErrorCode( vtkErrorCode::PrematureEndOfFileError );
Will Schroeder's avatar
Will Schroeder committed
501 502
    return 0;
    }
503
  delete [] this->Header;
504
  this->Header = new char[strlen(line) + 1];
505 506
  strcpy (this->Header, line);

Ken Martin's avatar
Ken Martin committed
507
  vtkDebugMacro(<< "Reading vtk file entitled: " << line);
Ken Martin's avatar
Ken Martin committed
508 509 510
  //
  // read type
  //
Ken Martin's avatar
Ken Martin committed
511
  if (!this->ReadString(line))
Will Schroeder's avatar
Will Schroeder committed
512
    {
513
    vtkErrorMacro(<<"Premature EOF reading file type!" << " for file: "
514
                  << (this->FileName?this->FileName:"(Null FileName)"));
Andy Cedilnik's avatar
Andy Cedilnik committed
515
    this->SetErrorCode( vtkErrorCode::PrematureEndOfFileError );
Will Schroeder's avatar
Will Schroeder committed
516 517
    return 0;
    }
Will Schroeder's avatar
Will Schroeder committed
518

519
  if ( !strncmp(this->LowerCase(line), "ascii", 5) )
Bill Lorensen's avatar
Bill Lorensen committed
520 521 522
    {
    this->FileType = VTK_ASCII;
    }
523
  else if ( !strncmp(line, "binary", 6) )
Bill Lorensen's avatar
Bill Lorensen committed
524 525 526
    {
    this->FileType = VTK_BINARY;
    }
Will Schroeder's avatar
Will Schroeder committed
527 528
  else
    {
529
    vtkErrorMacro(<< "Unrecognized file type: "<< line << " for file: "
530
                  << (this->FileName?this->FileName:"(Null FileName)"));
Ken Martin's avatar
Ken Martin committed
531
    this->FileType = 0;
Andy Cedilnik's avatar
Andy Cedilnik committed
532
    this->SetErrorCode( vtkErrorCode::UnrecognizedFileTypeError );
Will Schroeder's avatar
Will Schroeder committed
533 534 535
    return 0;
    }

536
  // if this is a binary file we need to make sure that we opened it
Ken Martin's avatar
Ken Martin committed
537
  // as a binary file.
538
  if (this->FileType == VTK_BINARY && this->ReadFromInputString == 0)
Ken Martin's avatar
Ken Martin committed
539 540 541
    {
    vtkDebugMacro(<< "Opening vtk file as binary");
    delete this->IS;
Andy Cedilnik's avatar
Andy Cedilnik committed
542
    this->IS = 0;
543
#ifdef _WIN32
544
    this->IS = new ifstream(this->FileName, ios::in | ios::binary);
545
#else
546
    this->IS = new ifstream(this->FileName, ios::in);
547
#endif
548
    if (this->IS->fail())
Ken Martin's avatar
Ken Martin committed
549
      {
Will Schroeder's avatar
Will Schroeder committed
550
      vtkErrorMacro(<< "Unable to open file: "<< this->FileName);
551 552
      delete this->IS;
      this->IS = NULL;
Andy Cedilnik's avatar
Andy Cedilnik committed
553
      this->SetErrorCode( vtkErrorCode::CannotOpenFileError );
Ken Martin's avatar
Ken Martin committed
554 555 556 557 558 559 560
      return 0;
      }
    // read up to the same point in the file
    this->ReadLine(line);
    this->ReadLine(line);
    this->ReadString(line);
    }
Will Schroeder's avatar
Will Schroeder committed
561

562 563
  float progress=this->GetProgress();
  this->UpdateProgress(progress + 0.5*(1.0 - progress));
564

Will Schroeder's avatar
Will Schroeder committed
565 566 567
  return 1;
}

568 569 570
int vtkDataReader::IsFileValid(const char *dstype)
{
  char line[1024];
571

572 573 574 575
  if (!dstype)
    {
    return 0;
    }
576

577
  if (!this->OpenVTKFile() || !this->ReadHeader())
Bill Lorensen's avatar
Bill Lorensen committed
578
    {
579
    this->CloseVTKFile ();
Bill Lorensen's avatar
Bill Lorensen committed
580 581
    return 0;
    }
582 583 584 585 586

  if (!this->ReadString(line))
    {
    vtkErrorMacro(<<"Data file ends prematurely!");
    this->CloseVTKFile ();
Andy Cedilnik's avatar
Andy Cedilnik committed
587
    this->SetErrorCode( vtkErrorCode::PrematureEndOfFileError );
588 589 590 591 592 593 594 595 596
    return 0;
    }

  if ( !strncmp(this->LowerCase(line),"dataset",(unsigned long)7) )
    {
    if (!this->ReadString(line))
      {
      vtkErrorMacro(<<"Data file ends prematurely!");
      this->CloseVTKFile ();
Andy Cedilnik's avatar
Andy Cedilnik committed
597
      this->SetErrorCode( vtkErrorCode::PrematureEndOfFileError );
598
      return 0;
599
      }
600 601 602 603 604 605 606 607 608
    if (strncmp(this->LowerCase(line),dstype,strlen(dstype)))
      {
      this->CloseVTKFile ();
      return 0;
      }
    // everything looks good
    this->CloseVTKFile();
    return 1;
    }
609 610

  this->CloseVTKFile ();
611 612 613
  return 0;
}

614
// Read the cell data of a vtk data file. The number of cells (from the
615 616 617 618 619 620 621 622
// dataset) must match the number of cells defined in cell attributes (unless
// no geometry was defined).
int vtkDataReader::ReadCellData(vtkDataSet *ds, int numCells)
{
  char line[256];
  vtkDataSetAttributes *a=ds->GetCellData();

  vtkDebugMacro(<< "Reading vtk cell data");
623

624 625 626 627 628 629 630 631 632 633
  //
  // Read keywords until end-of-file
  //
  while (this->ReadString(line))
    {
    //
    // read scalar data
    //
    if ( ! strncmp(this->LowerCase(line), "scalars", 7) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
634
      if ( ! this->ReadScalarData(a, numCells) )
635 636 637
        {
        return 0;
        }
638 639 640 641 642 643
      }
    //
    // read vector data
    //
    else if ( ! strncmp(line, "vectors", 7) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
644
      if ( ! this->ReadVectorData(a, numCells) )
645 646 647
        {
        return 0;
        }
648 649 650 651 652 653
      }
    //
    // read 3x3 tensor data
    //
    else if ( ! strncmp(line, "tensors", 7) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
654
      if ( ! this->ReadTensorData(a, numCells) )
655 656 657
        {
        return 0;
        }
658 659 660 661 662 663
      }
    //
    // read normals data
    //
    else if ( ! strncmp(line, "normals", 7) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
664
      if ( ! this->ReadNormalData(a, numCells) )
665 666 667
        {
        return 0;
        }
668 669 670 671 672 673
      }
    //
    // read texture coordinates data
    //
    else if ( ! strncmp(line, "texture_coordinates", 19) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
674
      if ( ! this->ReadTCoordsData(a, numCells) )
675 676 677
        {
        return 0;
        }
678 679
      }
    //
680 681 682 683 684 685 686 687 688 689
    // read the global id data
    //
    else if ( ! strncmp(line, "global_ids", 10) )
      {
      if ( ! this->ReadGlobalIds(a, numCells) )
        {
        return 0;
        }
      }
    //
690 691
    // read the pedigree id data
    //
692
    else if ( ! strncmp(line, "pedigree_ids", 12) )
693 694 695 696 697 698 699
      {
      if ( ! this->ReadPedigreeIds(a, numCells) )
        {
        return 0;
        }
      }
    //
700 701 702 703
    // read color scalars data
    //
    else if ( ! strncmp(line, "color_scalars", 13) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
704
      if ( ! this->ReadCoScalarData(a, numCells) )
705 706 707
        {
        return 0;
        }
708 709 710 711 712 713
      }
    //
    // read lookup table. Associate with scalar data.
    //
    else if ( ! strncmp(line, "lookup_table", 12) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
714
      if ( ! this->ReadLutData(a) )
715 716 717
        {
        return 0;
        }
718 719 720 721 722 723 724
      }
    //
    // read field of data
    //
    else if ( ! strncmp(line, "field", 5) )
      {
      vtkFieldData *f;
725
      if ( ! (f=this->ReadFieldData(CELL_DATA)) )
726 727 728
        {
        return 0;
        }
729
      for(int i=0; i<f->GetNumberOfArrays(); i++)
730
        {
731
        a->AddArray(f->GetAbstractArray(i));
732
        }
733 734
      f->Delete();
      }
735 736 737 738 739 740 741 742 743 744 745 746 747 748
    //
    // maybe bumped into point data
    //
    else if ( ! strncmp(line, "point_data", 10) )
      {
      int npts;
      if (!this->Read(&npts))
        {
        vtkErrorMacro(<<"Cannot read point data!");
        return 0;
        }

      this->ReadPointData(ds, npts);
      }
749 750 751

    else
      {
752
      vtkErrorMacro(<< "Unsupported cell attribute type: " << line
753
                    << " for file: " << (this->FileName?this->FileName:"(Null FileName)"));
754 755 756 757 758 759 760 761
      return 0;
      }
    }

  return 1;
}


762
// Read the point data of a vtk data file. The number of points (from the
Will Schroeder's avatar
Will Schroeder committed
763 764
// dataset) must match the number of points defined in point attributes (unless
// no geometry was defined).
Ken Martin's avatar
Ken Martin committed
765
int vtkDataReader::ReadPointData(vtkDataSet *ds, int numPts)
Will Schroeder's avatar
Will Schroeder committed
766
{
Ken Martin's avatar
Ken Martin committed
767
  char line[256];
768
  vtkDataSetAttributes *a=ds->GetPointData();
Will Schroeder's avatar
Will Schroeder committed
769

Ken Martin's avatar
Ken Martin committed
770
  vtkDebugMacro(<< "Reading vtk point data");
771

Ken Martin's avatar
Ken Martin committed
772 773 774
  //
  // Read keywords until end-of-file
  //
Ken Martin's avatar
Ken Martin committed
775
  while (this->ReadString(line))
Will Schroeder's avatar
Will Schroeder committed
776
    {
Ken Martin's avatar
Ken Martin committed
777 778 779
    //
    // read scalar data
    //
Will Schroeder's avatar
Will Schroeder committed
780 781
    if ( ! strncmp(this->LowerCase(line), "scalars", 7) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
782
      if ( ! this->ReadScalarData(a, numPts) )
783 784 785
        {
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
786
      }
Ken Martin's avatar
Ken Martin committed
787 788 789
    //
    // read vector data
    //
Will Schroeder's avatar
Will Schroeder committed
790 791
    else if ( ! strncmp(line, "vectors", 7) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
792
      if ( ! this->ReadVectorData(a, numPts) )
793 794 795
        {
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
796
      }
Ken Martin's avatar
Ken Martin committed
797 798 799
    //
    // read 3x3 tensor data
    //
Will Schroeder's avatar
Will Schroeder committed
800 801
    else if ( ! strncmp(line, "tensors", 7) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
802
      if ( ! this->ReadTensorData(a, numPts) )
803 804 805
        {
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
806
      }
Ken Martin's avatar
Ken Martin committed
807 808 809
    //
    // read normals data
    //
Will Schroeder's avatar
Will Schroeder committed
810 811
    else if ( ! strncmp(line, "normals", 7) )
      {
812

Bill Lorensen's avatar
Bill Lorensen committed
813
      if ( ! this->ReadNormalData(a, numPts) )
814 815 816
        {
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
817
      }
Ken Martin's avatar
Ken Martin committed
818 819 820
    //
    // read texture coordinates data
    //
Will Schroeder's avatar
Will Schroeder committed
821 822
    else if ( ! strncmp(line, "texture_coordinates", 19) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
823
      if ( ! this->ReadTCoordsData(a, numPts) )
824 825 826
        {
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
827
      }
Ken Martin's avatar
Ken Martin committed
828
    //
829 830 831 832 833 834 835 836 837 838
    // read the global id data
    //
    else if ( ! strncmp(line, "global_ids", 10) )
      {
      if ( ! this->ReadGlobalIds(a, numPts) )
        {
        return 0;
        }
      }
    //
839 840
    // read the pedigree id data
    //
841
    else if ( ! strncmp(line, "pedigree_ids", 12) )
842 843 844 845 846 847 848
      {
      if ( ! this->ReadPedigreeIds(a, numPts) )
        {
        return 0;
        }
      }
    //
849 850 851 852 853 854 855 856 857 858
    // read the edge flags data
    //
    else if ( ! strncmp(line, "edge_flags", 10) )
      {
      if ( ! this->ReadEdgeFlags(a, numPts) )
        {
        return 0;
        }
      }
    //
Ken Martin's avatar
Ken Martin committed
859 860
    // read color scalars data
    //
Will Schroeder's avatar
Will Schroeder committed
861 862
    else if ( ! strncmp(line, "color_scalars", 13) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
863
      if ( ! this->ReadCoScalarData(a, numPts) )
864 865 866
        {
        return 0;
        }
Will Schroeder's avatar
Will Schroeder committed
867
      }
Ken Martin's avatar
Ken Martin committed
868 869 870
    //
    // read lookup table. Associate with scalar data.
    //
Will Schroeder's avatar
Will Schroeder committed
871 872
    else if ( ! strncmp(line, "lookup_table", 12) )
      {
Bill Lorensen's avatar
Bill Lorensen committed
873
      if ( ! this->ReadLutData(a) )
874 875 876
        {
        return 0;
        }
877 878 879 880 881 882 883
      }
    //
    // read field of data
    //
    else if ( ! strncmp(line, "field", 5) )
      {
      vtkFieldData *f;
884
      if ( ! (f=this->ReadFieldData(POINT_DATA)) )
885 886 887
        {
        return 0;
        }
888
      for(int i=0; i<f->GetNumberOfArrays(); i++)
889
        {
890
        a->AddArray(f->GetAbstractArray(i));
891
        }
892
      f->Delete();
Will Schroeder's avatar
Will Schroeder committed
893
      }
894 895 896 897 898 899 900 901 902 903 904 905 906 907
    //
    // maybe bumped into cell data
    //
    else if ( ! strncmp(line, "cell_data", 9) )
      {
      int ncells;
      if (!this->Read(&ncells))
        {
        vtkErrorMacro(<<"Cannot read cell data!");
        return 0;
        }

      this->ReadCellData(ds, ncells);
      }
Will Schroeder's avatar
Will Schroeder committed
908 909 910

    else
      {
911
      vtkErrorMacro(<< "Unsupported point attribute type: " << line
912
                    << " for file: " << (this->FileName?this->FileName:"(Null FileName)"));
Will Schroeder's avatar
Will Schroeder committed
913 914 915
      return 0;
      }
    }
Will Schroeder's avatar
Will Schroeder committed
916
  return 1;
Will Schroeder's avatar
Will Schroeder committed
917 918
}

919