vtkParticleReader.cxx 25.7 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkParticleReader.cxx

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.
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.
13 14 15

=========================================================================*/
#include "vtkParticleReader.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
16

17 18
#include "vtkSmartPointer.h"
#include "vtkStreamingDemandDrivenPipeline.h"
19 20
#include "vtkInformation.h"
#include "vtkInformationVector.h"
21
#include "vtkObjectFactory.h"
22
#include "vtkByteSwap.h"
23
#include "vtkPointData.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
24 25
#include "vtkPoints.h"
#include "vtkPolyData.h"
26 27 28 29 30
#include "vtkCellArray.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"

31 32 33 34
#include <vtkstd/algorithm>
#include <vtkstd/vector>

vtkCxxRevisionMacro(vtkParticleReader, "1.23");
Brad King's avatar
Brad King committed
35
vtkStandardNewMacro(vtkParticleReader);
36

37 38 39 40 41 42 43 44 45 46 47 48
namespace {
  // .NAME ParseLine - Read a line of four values of type T filtering out comments.
  // .SECTION Description
  // The ParseLine operator scans a string and returns 0 if it finds a 
  // comment symbol. Otherwise it returns 1 and four values corresponding 
  // to the position of the particle (x,y,z) and a scalar s asociated with 
  // the particle. It is a good idea to set the values of val to some 
  // predefined value before calling the operator.
  template < typename T > 
    class ParseLine
  {
  public:
49
    ParseLine (): LookForEndString(false) {};
50 51 52 53 54 55 56 57
    int operator () (vtkstd::string &s, T val[4])
    {
      // Skip over comment lines.
      vtkstd::string::iterator itr;
      vtkstd::string tgt("/*");
      itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
      if ( itr != s.end() )
        {
58
        LookForEndString = true;
59 60
        //continue;
        }
61
      if ( LookForEndString )
62 63 64 65
        {
        tgt = "*/";
        itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
        if ( itr != s.end() )
66 67 68
          {
          LookForEndString = false;
          }
69 70 71 72 73 74 75
        //continue;
        return 0;
        }

      tgt = "//";
      itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
      if ( itr != s.end() )
76
        {
77
        return 0;
78
        }
79 80 81
      tgt = "%";
      itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
      if ( itr != s.end() )
82
        {
83
        return 0;
84
        }
85 86 87
      tgt = "#";
      itr = vtkstd::search(s.begin(),s.end(),tgt.begin(),tgt.end());
      if ( itr != s.end() )
88
        {
89
        return 0;
90
        }
91 92 93 94
      // If comma delimited, replace with tab
      vtkstd::replace(s.begin(),s.end(),',','\t');

      // We have data.
95 96
      strstream is;
      is << s.c_str() << ends;// no istringstream in VTK
97 98 99 100
      is >> val[0] >> val[1] >> val[2] >> val[3];

      return 1;
    }
101

102
  private:
103
    bool LookForEndString;
104 105 106 107 108 109 110
    
  };
  
 // The number of times we output a progress message.
 int const quantum = 20;
 // The ratio of high ASCII characters to low ASCII characeters.
 double hiToLowASCII = 0.1;
111

112
}
113
//----------------------------------------------------------------------------
114 115 116 117 118 119
vtkParticleReader::vtkParticleReader() :
  FileName(NULL)
  , File(NULL)
  , HasScalar(1)
  , FileType(FILE_TYPE_IS_UNKNOWN)
  , DataType(VTK_FLOAT)
120 121
  , Alliquot(0)
  , Count(0)
122
{
123
  this->SetNumberOfInputPorts(0);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
}

//----------------------------------------------------------------------------
vtkParticleReader::~vtkParticleReader()
{ 
  if (this->File)
    {
    this->File->close();
    delete this->File;
    this->File = NULL;
    }
  
  if (this->FileName)
    {
    delete [] this->FileName;
    this->FileName = NULL;
    }
}

//----------------------------------------------------------------------------
void vtkParticleReader::OpenFile()
{
  if (!this->FileName)
    {
    vtkErrorMacro(<<"FileName must be specified.");
    return;
    }

152
  // If the file was open close it.
153 154 155 156 157 158 159
  if (this->File)
    {
    this->File->close();
    delete this->File;
    this->File = NULL;
    }
  
160
  // Open the new file.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  vtkDebugMacro(<< "Initialize: opening file " << this->FileName);
#ifdef _WIN32
  this->File = new ifstream(this->FileName, ios::in | ios::binary);
#else
  this->File = new ifstream(this->FileName, ios::in);
#endif
  if (! this->File || this->File->fail())
    {
    vtkErrorMacro(<< "Initialize: Could not open file " << 
    this->FileName);
    return;
    }
}

//----------------------------------------------------------------------------
176 177 178 179
int vtkParticleReader::RequestInformation(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **vtkNotUsed(inputVector),
  vtkInformationVector *outputVector)
180
{
181 182
  // get the info object
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
183

184 185
  outInfo->Set(vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES(),
               -1);
186

187 188
  return 1;
}
189 190

//----------------------------------------------------------------------------
191 192 193 194
int vtkParticleReader::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **vtkNotUsed(inputVector),
  vtkInformationVector *outputVector)
195
{
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  if (!this->FileName)
    {
    vtkErrorMacro(<<"FileName must be specified.");
    return 0;
    }
  
  this->OpenFile();
  int ft = this->FileType;
  if ( ft == FILE_TYPE_IS_UNKNOWN )
    {
      ft = DetermineFileType();
      if ( ft == FILE_TYPE_IS_UNKNOWN )
        {
        vtkErrorMacro(<< "File type cannot be determined.");
        return 0;
        }
    }

  switch ( ft )
  {
  case FILE_TYPE_IS_TEXT:
    switch ( this->DataType )
    {
    case VTK_FLOAT:
      return ProduceOutputFromTextFileFloat(outputVector);
      break;
    case VTK_DOUBLE:
      return ProduceOutputFromTextFileDouble(outputVector);
      break;
    default:
      {
      vtkErrorMacro(<<"Only float or double data can be processed.");
      return 0;
      }
    }
  case FILE_TYPE_IS_BINARY:
    switch ( this->DataType )
    {
    case VTK_FLOAT:
      return ProduceOutputFromBinaryFileFloat(outputVector);
      break;
    case VTK_DOUBLE:
      return ProduceOutputFromBinaryFileDouble(outputVector);
      break;
    default:
      {
      vtkErrorMacro(<<"Only float or double data can be processed.");
      return 0;
      }
    }
  default:
    {
    vtkErrorMacro(<<"The file type was not able to be determined.");
    return 0;
    }
  }
}

//----------------------------------------------------------------------------
int vtkParticleReader::DetermineFileType()
{
  // This function assumes that the file has been opened.

  this->File->seekg(0,ios::end);
  if (this->File->fail())
    {
    vtkErrorMacro("Could not seek to end of file.");
    return FILE_TYPE_IS_UNKNOWN;
    }
  size_t fileLength = this->File->tellg();
  if ( fileLength == 0 )
    {
    vtkErrorMacro("File is empty.");
    return FILE_TYPE_IS_UNKNOWN;
    }
  
  this->File->seekg(0,ios::beg);
  if (this->File->fail())
    {
    vtkErrorMacro("Could not seek to start of file.");
    return FILE_TYPE_IS_UNKNOWN;
    }

  size_t sampleSize = fileLength < 5000 ? fileLength: 5000;
  // cout << "File length: " << fileLength << " Sample size: " << sampleSize << endl;
  vtkstd::vector <unsigned char> s;
  for ( size_t i = 0; i < sampleSize; ++i )
    {
284 285 286
    char c;
    this->File->read(&c,sizeof(char));
    s.push_back(c);
287
    }
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  // If read terminated prematurely then it may have detected
  // a premature EOF character in the data.
  // Assume that the file type is undetermined in this case.
  if ( s.size() != sampleSize )
    {
    // cout << "Premature termination" << endl;
    return FILE_TYPE_IS_UNKNOWN;
    }

  size_t charsCounted = 0;
  size_t zero = 0;
  size_t conventionalASCII = 0;
  size_t extendedASCII = 0;
  size_t controlASCII = 0;
  size_t otherASCII = 0;
  for ( size_t j = 0; j < s.size(); ++j )
  {
    if ( s[j] == '\0' )
306
      {
307 308 309
      zero++;
      charsCounted++;
      continue;
310
      }
311 312 313 314 315 316 317 318 319 320 321
    // Conventional ASCII characters.
    if ( s[j] > 0x1f && s[j] < 0x80 )
     {
     conventionalASCII++;
     charsCounted++;
     continue;
     }
    // Extended ASCII characters may have been used.
    if ( s[j] > 0x7f )
      {
      extendedASCII++;
322
      charsCounted++;
323 324 325 326 327 328 329 330 331 332 333 334
      continue;
      }
    // Control characters.
    if ( s[j] == '\n' || s[j] == '\r' || s[j] == '\t' || s[j] == '\f' )
      {
      controlASCII++;
      charsCounted++;
      continue;
      }
    otherASCII++;
    charsCounted++;
  }
335 336 337 338 339
//    cout << "Zero: " << zero << " Conventional ASCII:" << conventionalASCII <<
//       " Extended ASCII: " << extendedASCII << " Control ASCII: " << controlASCII << 
//       " Other characters: " << otherASCII << 
//       " Total counted: " << charsCounted << endl;

340 341 342 343 344 345 346 347 348 349 350 351 352
  // NULL shouldn't ever appear in a text file.
  if ( zero != 0 || otherASCII > 0 || conventionalASCII == 0 )
    {
    return FILE_TYPE_IS_BINARY;
    }
  if ( (double)extendedASCII / (double) conventionalASCII < hiToLowASCII )
    {
    return FILE_TYPE_IS_TEXT;
    }
  else
    {
    return FILE_TYPE_IS_BINARY;
    }
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
}

//----------------------------------------------------------------------------
int vtkParticleReader::ProduceOutputFromTextFileDouble(vtkInformationVector *outputVector)
{
  // Get the size of the file.
  this->File->seekg(0,ios::end);
  if (this->File->fail())
    {
    vtkErrorMacro("Could not seek to end of file.");
    return 0;
    }

  size_t fileLength = (unsigned long)this->File->tellg();
  size_t bytesRead = 0;

  vtkstd::string s;
  
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  points->SetDataTypeToDouble();
  points->Reset();

  vtkSmartPointer<vtkDoubleArray> scalars = vtkSmartPointer<vtkDoubleArray>::New();
  scalars->Reset();
  scalars->SetName("Scalar");

  this->File->seekg(0,ios::beg);
  
381 382
  this->Alliquot = fileLength / quantum;
  this->Count = 1;
383 384
  ParseLine<double> pl;
  while ( vtkstd::getline(*this->File,s,'\n') )
385
    {
386 387 388
    if ( s.size() != 0 )
      {
      bytesRead += s.size();
389
      this->DoProgressUpdate( bytesRead, fileLength );
390 391 392 393 394 395
      double val[4];
      val[0]=val[1]=val[2]=val[3]=0;
      if ( pl(s,val) )
        {
        points->InsertNextPoint(val[0], val[1], val[2]);
        if ( this->HasScalar)
396
          {
397
          scalars->InsertNextValue(val[3]);
398
          }
399
        }
400
      }
401 402 403 404 405
    }

  vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
  vertices->Reset();

406 407
  this->NumberOfPoints = points->GetNumberOfPoints();
  for( vtkIdType j = 0; j < (vtkIdType)this->NumberOfPoints; ++j )
408 409 410 411 412 413
    {
    vertices->InsertNextCell( 1 );
    vertices->InsertCellPoint( j );
    }

   // get the info object
414 415 416 417 418
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  // get the ouptut
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
  
  output->SetPoints(points);
  output->SetVerts(vertices);
  if ( this->HasScalar )
    output->GetPointData()->SetScalars(scalars);

  output->Modified();

  return 1;
}

//----------------------------------------------------------------------------
int vtkParticleReader::ProduceOutputFromTextFileFloat(vtkInformationVector *outputVector)
{
  // Get the size of the file.
  this->File->seekg(0,ios::end);
  if (this->File->fail())
    {
    vtkErrorMacro("Could not seek to end of file.");
    return 0;
    }

  size_t fileLength = (unsigned long)this->File->tellg();
  size_t bytesRead = 0;

  vtkstd::string s;
  
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  points->SetDataTypeToFloat();
  points->Reset();

  vtkSmartPointer<vtkFloatArray> scalars = vtkSmartPointer<vtkFloatArray>::New();
  scalars->Reset();
  scalars->SetName("Scalar");

  this->File->seekg(0,ios::beg);
  
456 457
  this->Alliquot = fileLength / quantum;
  this->Count = 1;
458 459 460 461 462 463
  ParseLine<float> pl;
  while ( vtkstd::getline(*this->File,s,'\n') )
  {
    if ( s.size() != 0 )
      {
      bytesRead += s.size();
464
      this->DoProgressUpdate( bytesRead, fileLength );
465 466 467 468 469 470 471
      
      float val[4];
      val[0]=val[1]=val[2]=val[3]=0;
      if ( pl(s,val) )
        {
        points->InsertNextPoint(val[0], val[1], val[2]);
        if ( this->HasScalar)
472
          {
473
          scalars->InsertNextValue(val[3]);
474
          }
475 476 477 478 479 480 481
        }
    }
  }

  vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New();
  vertices->Reset();
  
482 483
  this->NumberOfPoints = points->GetNumberOfPoints();
  for( vtkIdType j = 0; j < (vtkIdType)this->NumberOfPoints; ++j )
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
    {
    vertices->InsertNextCell( 1 );
    vertices->InsertCellPoint( j );
    }

   // get the info object
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  // get the ouptut
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));
  
  output->SetPoints(points);
  output->SetVerts(vertices);
  if ( this->HasScalar )
499
    {
500
    output->GetPointData()->SetScalars(scalars);
501
    }
502 503 504 505 506 507 508 509 510 511 512

  output->Modified();

  return 1;
}



//----------------------------------------------------------------------------
int vtkParticleReader::ProduceOutputFromBinaryFileDouble(vtkInformationVector *outputVector)
{
513

514 515
  unsigned long fileLength, start, next, length, ptIdx, cellPtIdx;
  unsigned long cellLength;
516
  int piece, numPieces;
517
  double *data, *ptr;
518 519 520 521

  if (!this->FileName)
    {
    vtkErrorMacro(<<"FileName must be specified.");
522
    return 0;
523 524 525 526 527 528
    }
  
  this->OpenFile();
    
  // Get the size of the header from the size of the image
  this->File->seekg(0,ios::end);
529 530 531
  if (this->File->fail())
    {
    vtkErrorMacro("Could not seek to end of file.");
532
    return 0;
533 534
    }

535
  fileLength = (unsigned long)this->File->tellg();
536
  if ( this->HasScalar )
537 538 539
    {
    this->NumberOfPoints = fileLength / (4 * sizeof(double));
    }
540
  else
541 542 543
    {
    this->NumberOfPoints = fileLength / (3 * sizeof(double));
    }
544 545 546

  // get the info object
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
547

548 549 550 551
  piece =
    outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
  numPieces =
    outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
552

553
  if ((unsigned long)numPieces > this->NumberOfPoints)
554
    {
555
    numPieces = (int)(this->NumberOfPoints);
556
    }
557
  if (numPieces <= 0 || piece < 0 || piece >= numPieces)
558
    {
559
    return 0;
560 561
    }

562 563
  start = piece * this->NumberOfPoints / numPieces;
  next = (piece+1) * this->NumberOfPoints / numPieces;
564 565 566

  length = next - start;

567
  if ( this->HasScalar )
568
    {
569
    data = new double[length * 4];
570
    }
571
  else
572
    {
573
    data = new double[length * 3];
574
    }
575 576

  // Seek to the first point in the file.
577
  if ( this->HasScalar )
578
    {
579
    this->File->seekg(start*4*sizeof(double), ios::beg);
580
    }
581
  else
582
    {
583
    this->File->seekg(start*3*sizeof(double), ios::beg);
584
    }
585 586
  if (this->File->fail())
    {
587
    vtkErrorMacro(<< "File operation failed: Seeking to " << start*4);
588
    delete [] data;
589
    return 0;
590 591 592
    }

  // Read the data.
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
  if ( this->HasScalar )
    {
    this->File->read((char *)data, length*4*sizeof(double));
    if ( static_cast<unsigned long>(this->File->gcount()) != 
         static_cast<unsigned long>(length*4*sizeof(double))
       // On apple read to eof returns fail
#ifndef __APPLE_CC__     
       || this->File->fail()
#endif // __APPLE_CC__     
       )
      {
      vtkErrorMacro("Could not read points: " << start 
             << " to " << next-1);
      delete [] data;
      return 0;
      }
    }
  else
    {
    this->File->read((char *)data, length*3*sizeof(double));
    if ( static_cast<unsigned long>(this->File->gcount()) != 
         static_cast<unsigned long>(length*3*sizeof(double))
       // On apple read to eof returns fail
616
#ifndef __APPLE_CC__     
617
       || this->File->fail()
618
#endif // __APPLE_CC__     
619 620 621 622 623 624 625 626 627 628 629 630 631
       )
      {
      vtkErrorMacro("Could not read points: " << start 
             << " to " << next-1);
      delete [] data;
      return 0;
      }
    }
  
  // Swap bytes if necessary.
  if (this->GetSwapBytes())
    {
    if ( this->HasScalar )
632
      {
633
      vtkByteSwap::SwapVoidRange(data, length*4, sizeof(double));
634
      }
635
    else
636
      {
637
      vtkByteSwap::SwapVoidRange(data, length*3, sizeof(double));
638
      }
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
    }

  this->UpdateProgress(0.5);
  
  ptr = data;

  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();  
  points->SetNumberOfPoints(length);
  vtkSmartPointer<vtkFloatArray> array = vtkSmartPointer<vtkFloatArray>::New();  
  array->SetName("Scalar");
  vtkSmartPointer<vtkCellArray> verts = vtkSmartPointer<vtkCellArray>::New();  
  
  // Each cell will have 1000 points.  Leave a little extra space just in case.
  // We break up the cell this way so that the render will check for aborts
  // at a reasonable rate.
  verts->Allocate((int)((float)length * 1.002));
  // Keep adding cells until we run out of points.
  ptIdx = 0;
  int count = 1;
  double len = length;
  while (length > 0)
660
    {
661
    if ( count % 10 == 0 )
662
      {
663
     this->UpdateProgress(0.5+((count * 1000.0)/len)/2.0);
664
      }
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    count++;
    cellLength = 1000;
    if (cellLength > length)
      {
      cellLength = length;
      }
    length = length - cellLength;
    verts->InsertNextCell((int)cellLength);
    for (cellPtIdx = 0; cellPtIdx < cellLength; ++cellPtIdx)
      {    
      points->SetPoint(ptIdx, ptr[0], ptr[1], ptr[2]);
      if ( this->HasScalar )
        {
        array->InsertNextValue(ptr[3]);
        ptr += 4;
        }
      else
682
        {
683
        ptr += 3;
684
        }
685 686 687 688 689 690 691 692 693 694 695 696 697
      verts->InsertCellPoint(ptIdx);
      ++ptIdx;
      }
    }
  delete [] data;

  // get the ouptut
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

  output->SetPoints(points);
  output->SetVerts(verts);
  if ( this->HasScalar )
698
    {
699
    output->GetPointData()->SetScalars(array);
700
    }
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731

  return 1;
}

//----------------------------------------------------------------------------
int vtkParticleReader::ProduceOutputFromBinaryFileFloat(vtkInformationVector *outputVector)
{

  unsigned long fileLength, start, next, length, ptIdx, cellPtIdx;
  unsigned long cellLength;
  int piece, numPieces;
  float *data, *ptr;

  if (!this->FileName)
    {
    vtkErrorMacro(<<"FileName must be specified.");
    return 0;
    }
  
  this->OpenFile();
    
  // Get the size of the header from the size of the image
  this->File->seekg(0,ios::end);
  if (this->File->fail())
    {
    vtkErrorMacro("Could not seek to end of file.");
    return 0;
    }

  fileLength = (unsigned long)this->File->tellg();
  if ( this->HasScalar )
732 733 734
    {
    this->NumberOfPoints = fileLength / (4 * sizeof(float));
    }
735
  else
736 737 738
    {
    this->NumberOfPoints = fileLength / (3 * sizeof(float));
    }
739 740 741 742 743 744 745 746 747 748


  // get the info object
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

  piece =
    outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
  numPieces =
    outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());

749
  if ((unsigned long)numPieces > this->NumberOfPoints)
750
    {
751
    numPieces = (int)(this->NumberOfPoints);
752 753 754 755 756 757
    }
  if (numPieces <= 0 || piece < 0 || piece >= numPieces)
    {
    return 0;
    }

758 759
  start = piece * this->NumberOfPoints / numPieces;
  next = (piece+1) * this->NumberOfPoints / numPieces;
760 761 762 763

  length = next - start;

  if ( this->HasScalar )
764
    {
765
    data = new float[length * 4];
766
    }
767
  else
768
    {
769
    data = new float[length * 3];
770
    }
771 772 773 774
  

  // Seek to the first point in the file.
  if ( this->HasScalar )
775
    {
776
    this->File->seekg(start*4*sizeof(float), ios::beg);
777
    }
778
  else
779
    {
780
    this->File->seekg(start*3*sizeof(float), ios::beg);
781
    }
782 783 784
  if (this->File->fail())
    {
    vtkErrorMacro(<< "File operation failed: Seeking to " << start*4);
785
    delete [] data;
786
    return 0;
787
    }
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823

  // Read the data.
  if ( this->HasScalar )
    {
    this->File->read((char *)data, length*4*sizeof(float));
    if ( static_cast<unsigned long>(this->File->gcount()) != 
         static_cast<unsigned long>(length*4*sizeof(float))
       // On apple read to eof returns fail
#ifndef __APPLE_CC__     
       || this->File->fail()
#endif // __APPLE_CC__     
       )
      {
      vtkErrorMacro("Could not read points: " << start 
             << " to " << next-1);
      delete [] data;
      return 0;
      }
    }
  else
    {
    this->File->read((char *)data, length*3*sizeof(float));
    if ( static_cast<unsigned long>(this->File->gcount()) != 
         static_cast<unsigned long>(length*3*sizeof(float))
       // On apple read to eof returns fail
#ifndef __APPLE_CC__     
       || this->File->fail()
#endif // __APPLE_CC__     
       )
      {
      vtkErrorMacro("Could not read points: " << start 
             << " to " << next-1);
      delete [] data;
      return 0;
      }
    }
824
  
825 826 827
  // Swap bytes if necessary.
  if (this->GetSwapBytes())
    {
828
    if ( this->HasScalar )
829
      {
830
      vtkByteSwap::SwapVoidRange(data, length*4, sizeof(float));
831
      }
832
    else
833
      {
834
      vtkByteSwap::SwapVoidRange(data, length*3, sizeof(float));
835
      }
836 837
    }

838 839
  this->UpdateProgress(0.5);
  
840
  ptr = data;
841 842

  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();  
843
  points->SetNumberOfPoints(length);
844 845 846 847
  vtkSmartPointer<vtkFloatArray> array = vtkSmartPointer<vtkFloatArray>::New();  
  array->SetName("Scalar");
  vtkSmartPointer<vtkCellArray> verts = vtkSmartPointer<vtkCellArray>::New();  
  
848 849 850 851 852 853
  // Each cell will have 1000 points.  Leave a little extra space just in case.
  // We break up the cell this way so that the render will check for aborts
  // at a reasonable rate.
  verts->Allocate((int)((float)length * 1.002));
  // Keep adding cells until we run out of points.
  ptIdx = 0;
854 855
  int count = 1;
  double len = length;
856
  while (length > 0)
857
    {
858
    if ( count % 10 == 0 )
859
      {
860
     this->UpdateProgress(0.5+((count * 1000.0)/len)/2.0);
861
      }
862
    count++;
863 864 865 866 867 868 869 870
    cellLength = 1000;
    if (cellLength > length)
      {
      cellLength = length;
      }
    length = length - cellLength;
    verts->InsertNextCell((int)cellLength);
    for (cellPtIdx = 0; cellPtIdx < cellLength; ++cellPtIdx)
871
      {    
872
      points->SetPoint(ptIdx, ptr[0], ptr[1], ptr[2]);
873 874 875 876 877 878
      if ( this->HasScalar )
        {
        array->InsertNextValue(ptr[3]);
        ptr += 4;
        }
      else
879
        {
880
        ptr += 3;
881
        }
882 883 884
      verts->InsertCellPoint(ptIdx);
      ++ptIdx;
      }
885 886 887
    }
  delete [] data;

888 889 890 891
  // get the ouptut
  vtkPolyData *output = vtkPolyData::SafeDownCast(
    outInfo->Get(vtkDataObject::DATA_OBJECT()));

892 893
  output->SetPoints(points);
  output->SetVerts(verts);
894
  if ( this->HasScalar )
895
    {
896
    output->GetPointData()->SetScalars(array);
897
    }
898

899 900
  return 1;
}
901

902 903 904
//----------------------------------------------------------------------------
void vtkParticleReader::DoProgressUpdate( size_t & bytesRead, size_t & fileLength )
{
905
  if ( bytesRead > this->Alliquot )
906 907
    {
    this->UpdateProgress( bytesRead/(double)fileLength );
908 909
    this->Count++;
    this->Alliquot = fileLength / quantum * this->Count;
910 911 912
    }
}

913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
//----------------------------------------------------------------------------
void vtkParticleReader::SetDataByteOrderToBigEndian()
{
#ifndef VTK_WORDS_BIGENDIAN
  this->SwapBytesOn();
#else
  this->SwapBytesOff();
#endif
}

//----------------------------------------------------------------------------
void vtkParticleReader::SetDataByteOrderToLittleEndian()
{
#ifdef VTK_WORDS_BIGENDIAN
  this->SwapBytesOn();
#else
  this->SwapBytesOff();
#endif
}

933
//----------------------------------------------------------------------------
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
void vtkParticleReader::SetDataByteOrder(int byteOrder)
{
  if ( byteOrder == VTK_FILE_BYTE_ORDER_BIG_ENDIAN )
    {
    this->SetDataByteOrderToBigEndian();
    }
  else
    {
    this->SetDataByteOrderToLittleEndian();
    }
}

//----------------------------------------------------------------------------
int vtkParticleReader::GetDataByteOrder()
{
#ifdef VTK_WORDS_BIGENDIAN
  if ( this->SwapBytes )
    {
    return VTK_FILE_BYTE_ORDER_LITTLE_ENDIAN;
    }
  else
    {
    return VTK_FILE_BYTE_ORDER_BIG_ENDIAN;
    }
#else
  if ( this->SwapBytes )
    {
    return VTK_FILE_BYTE_ORDER_BIG_ENDIAN;
    }
  else
    {
    return VTK_FILE_BYTE_ORDER_LITTLE_ENDIAN;
    }
#endif
}

//----------------------------------------------------------------------------
const char *vtkParticleReader::GetDataByteOrderAsString()
{
#ifdef VTK_WORDS_BIGENDIAN
  if ( this->SwapBytes )
    {
    return "LittleEndian";
    }
  else
    {
    return "BigEndian";
    }
#else
  if ( this->SwapBytes )
    {
    return "BigEndian";
    }
  else
    {
    return "LittleEndian";
    }
#endif
}

//----------------------------------------------------------------------------
void vtkParticleReader::PrintSelf(ostream& os, vtkIndent indent)
{
Brad King's avatar
Brad King committed
997
  this->Superclass::PrintSelf(os,indent);
998 999 1000 1001

  os << indent << "FileName: " <<
    (this->FileName ? this->FileName : "(none)") << "\n";
  os << indent << "Swap Bytes: " << (this->SwapBytes ? "On\n" : "Off\n");
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
  os << indent << "Has Scalar: " << (this->HasScalar ? "On\n" : "Off\n");
  switch ( this->FileType )
  {
  case FILE_TYPE_IS_UNKNOWN:
    os << indent << "File type is unknown (The class automatically determines the file type).\n";
    break;
  case FILE_TYPE_IS_TEXT:
    os << indent << "File type is text.\n";
    break;
  case FILE_TYPE_IS_BINARY:
    os << indent << "File type is binary.\n";
    break;
  default:
    os << indent << "File type should never have this value: " << this->FileType << "\n";
    break;
  }
  switch ( this->DataType )
  {
  case VTK_FLOAT:
    os << indent << "Data type is float.\n";
    break;
  case VTK_DOUBLE:
    os << indent << "Data type is double.\n";
    break;
  default:
    os << indent << "Data type should never have this value: " << this->DataType << "\n";
    break;
  }
1030 1031 1032
  os << indent << "NumberOfPoints: " << this->NumberOfPoints << "\n";
  os << indent << "Alliquot: " << (unsigned int)this->Alliquot << "\n";
  os << indent << "Count: " << (unsigned int)this->Count << "\n";
1033 1034

}