vtkXMLPolyDataReader.cxx 16.4 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkXMLPolyDataReader.cxx

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

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 13 14 15 16 17 18 19 20 21
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkXMLPolyDataReader.h"
#include "vtkObjectFactory.h"
#include "vtkXMLDataElement.h"
#include "vtkPolyData.h"
#include "vtkIdTypeArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkCellArray.h"
22 23
#include "vtkInformation.h"
#include "vtkStreamingDemandDrivenPipeline.h"
24

25 26
#include <assert.h>

27 28 29 30 31 32 33 34 35 36 37 38 39
vtkStandardNewMacro(vtkXMLPolyDataReader);

//----------------------------------------------------------------------------
vtkXMLPolyDataReader::vtkXMLPolyDataReader()
{
  this->VertElements = 0;
  this->LineElements = 0;
  this->StripElements = 0;
  this->PolyElements = 0;
  this->TotalNumberOfVerts = 0;
  this->TotalNumberOfLines = 0;
  this->TotalNumberOfStrips = 0;
  this->TotalNumberOfPolys = 0;
40 41 42

  // TimeStep
  this->VertsTimeStep = -1;
Francois Bertel's avatar
Francois Bertel committed
43
  this->VertsOffset = static_cast<unsigned long>(-1);
44
  this->LinesTimeStep = -1;
Francois Bertel's avatar
Francois Bertel committed
45
  this->LinesOffset = static_cast<unsigned long>(-1);
46
  this->StripsTimeStep = -1;
Francois Bertel's avatar
Francois Bertel committed
47
  this->StripsOffset = static_cast<unsigned long>(-1);
48
  this->PolysTimeStep = -1;
Francois Bertel's avatar
Francois Bertel committed
49
  this->PolysOffset = static_cast<unsigned long>(-1);
50 51 52 53 54
}

//----------------------------------------------------------------------------
vtkXMLPolyDataReader::~vtkXMLPolyDataReader()
{
55 56 57 58
  if(this->NumberOfPieces)
    {
    this->DestroyPieces();
    }
59 60 61 62 63 64 65 66 67 68 69
}

//----------------------------------------------------------------------------
void vtkXMLPolyDataReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}

//----------------------------------------------------------------------------
vtkPolyData* vtkXMLPolyDataReader::GetOutput()
{
70
  return this->GetOutput(0);
71 72
}

73 74 75
//----------------------------------------------------------------------------
vtkPolyData* vtkXMLPolyDataReader::GetOutput(int idx)
{
76
  return vtkPolyData::SafeDownCast( this->GetOutputDataObject(idx) );
77 78
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
//----------------------------------------------------------------------------
vtkIdType vtkXMLPolyDataReader::GetNumberOfVerts()
{
  return this->TotalNumberOfVerts;
}

//----------------------------------------------------------------------------
vtkIdType vtkXMLPolyDataReader::GetNumberOfLines()
{
  return this->TotalNumberOfLines;
}

//----------------------------------------------------------------------------
vtkIdType vtkXMLPolyDataReader::GetNumberOfStrips()
{
  return this->TotalNumberOfStrips;
}

//----------------------------------------------------------------------------
vtkIdType vtkXMLPolyDataReader::GetNumberOfPolys()
{
  return this->TotalNumberOfPolys;
}

//----------------------------------------------------------------------------
const char* vtkXMLPolyDataReader::GetDataSetName()
{
  return "PolyData";
}

//----------------------------------------------------------------------------
void vtkXMLPolyDataReader::GetOutputUpdateExtent(int& piece,
                                                 int& numberOfPieces,
                                                 int& ghostLevel)
{
114 115 116 117 118 119 120
  vtkInformation* outInfo = this->GetCurrentOutputInformation();
  piece = outInfo->Get(
      vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER());
  numberOfPieces= outInfo->Get(
      vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES());
  ghostLevel = outInfo->Get(
      vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS());
121 122 123 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 152 153 154 155 156 157 158 159 160 161 162 163 164
}

//----------------------------------------------------------------------------
void vtkXMLPolyDataReader::SetupOutputTotals()
{
  this->Superclass::SetupOutputTotals();
  // Find the total size of the output.
  int i;
  this->TotalNumberOfCells = 0;
  this->TotalNumberOfVerts = 0;
  this->TotalNumberOfLines = 0;
  this->TotalNumberOfStrips = 0;
  this->TotalNumberOfPolys = 0;
  for(i=this->StartPiece; i < this->EndPiece; ++i)
    {
    this->TotalNumberOfCells += (this->NumberOfVerts[i] +
                                 this->NumberOfLines[i] +
                                 this->NumberOfStrips[i] +
                                 this->NumberOfPolys[i]);
    this->TotalNumberOfVerts += this->NumberOfVerts[i];
    this->TotalNumberOfLines += this->NumberOfLines[i];
    this->TotalNumberOfStrips += this->NumberOfStrips[i];
    this->TotalNumberOfPolys += this->NumberOfPolys[i];
    }
  
  // Data reading will start at the beginning of the output.
  this->StartVert = 0;
  this->StartLine = 0;
  this->StartStrip = 0;
  this->StartPoly = 0;
}

//----------------------------------------------------------------------------
void vtkXMLPolyDataReader::SetupPieces(int numPieces)
{
  this->Superclass::SetupPieces(numPieces);
  this->NumberOfVerts = new vtkIdType[numPieces];
  this->NumberOfLines = new vtkIdType[numPieces];
  this->NumberOfStrips = new vtkIdType[numPieces];
  this->NumberOfPolys = new vtkIdType[numPieces];
  this->VertElements = new vtkXMLDataElement*[numPieces];
  this->LineElements = new vtkXMLDataElement*[numPieces];
  this->StripElements = new vtkXMLDataElement*[numPieces];
  this->PolyElements = new vtkXMLDataElement*[numPieces];
165
  for(int i=0;i < numPieces; ++i)
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    {
    this->VertElements[i] = 0;
    this->LineElements[i] = 0;
    this->StripElements[i] = 0;
    this->PolyElements[i] = 0;
    }
}

//----------------------------------------------------------------------------
void vtkXMLPolyDataReader::DestroyPieces()
{
  delete [] this->PolyElements;
  delete [] this->StripElements;
  delete [] this->LineElements;
  delete [] this->VertElements;
  delete [] this->NumberOfPolys;
  delete [] this->NumberOfStrips;
  delete [] this->NumberOfLines;
  delete [] this->NumberOfVerts;
  this->Superclass::DestroyPieces();
}

188 189 190 191 192 193 194 195 196
//----------------------------------------------------------------------------
vtkIdType vtkXMLPolyDataReader::GetNumberOfCellsInPiece(int piece)
{
  return (this->NumberOfVerts[piece]+
          this->NumberOfLines[piece]+
          this->NumberOfStrips[piece]+
          this->NumberOfPolys[piece]);
}

197 198 199 200
//----------------------------------------------------------------------------
void vtkXMLPolyDataReader::SetupOutputData()
{
  this->Superclass::SetupOutputData();
201
  vtkPolyData* output = vtkPolyData::SafeDownCast(this->GetCurrentOutput());
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
  
  // Setup the output's cell arrays.  
  vtkCellArray* outVerts = vtkCellArray::New();
  vtkCellArray* outLines = vtkCellArray::New();
  vtkCellArray* outStrips = vtkCellArray::New();
  vtkCellArray* outPolys = vtkCellArray::New();
  
  output->SetVerts(outVerts);
  output->SetLines(outLines);
  output->SetStrips(outStrips);
  output->SetPolys(outPolys);
  
  outPolys->Delete();
  outStrips->Delete();
  outLines->Delete();
  outVerts->Delete();
}

//----------------------------------------------------------------------------
int vtkXMLPolyDataReader::ReadPiece(vtkXMLDataElement* ePiece)
{
223 224 225 226
  if(!this->Superclass::ReadPiece(ePiece))
    {
    return 0;
    }
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 284 285 286 287 288 289 290 291
  
  if(!ePiece->GetScalarAttribute("NumberOfVerts",
                                 this->NumberOfVerts[this->Piece]))
    {
    this->NumberOfVerts[this->Piece] = 0;
    }
  if(!ePiece->GetScalarAttribute("NumberOfLines",
                                 this->NumberOfLines[this->Piece]))
    {
    this->NumberOfLines[this->Piece] = 0;
    }
  if(!ePiece->GetScalarAttribute("NumberOfStrips",
                                 this->NumberOfStrips[this->Piece]))
    {
    this->NumberOfStrips[this->Piece] = 0;
    }
  if(!ePiece->GetScalarAttribute("NumberOfPolys",
                                 this->NumberOfPolys[this->Piece]))
    {
    this->NumberOfPolys[this->Piece] = 0;
    }
  
  // Find the cell elements in the piece.
  int i;
  for(i=0; i < ePiece->GetNumberOfNestedElements(); ++i)
    {
    vtkXMLDataElement* eNested = ePiece->GetNestedElement(i);
    if((strcmp(eNested->GetName(), "Verts") == 0)
       && (eNested->GetNumberOfNestedElements() > 1))
      {
      this->VertElements[this->Piece] = eNested;
      }
    if((strcmp(eNested->GetName(), "Lines") == 0)
       && (eNested->GetNumberOfNestedElements() > 1))
      {
      this->LineElements[this->Piece] = eNested;
      }
    if((strcmp(eNested->GetName(), "Strips") == 0)
       && (eNested->GetNumberOfNestedElements() > 1))
      {
      this->StripElements[this->Piece] = eNested;
      }
    if((strcmp(eNested->GetName(), "Polys") == 0)
       && (eNested->GetNumberOfNestedElements() > 1))
      {
      this->PolyElements[this->Piece] = eNested;
      }
    }
  
  return 1;
}

//----------------------------------------------------------------------------
void vtkXMLPolyDataReader::SetupNextPiece()
{
  this->Superclass::SetupNextPiece();  
  this->StartVert += this->NumberOfVerts[this->Piece];
  this->StartLine += this->NumberOfLines[this->Piece]; 
  this->StartStrip += this->NumberOfStrips[this->Piece];
  this->StartPoly += this->NumberOfPolys[this->Piece];
}

//----------------------------------------------------------------------------
int vtkXMLPolyDataReader::ReadPieceData()
{
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
  // The amount of data read by the superclass's ReadPieceData comes
  // from point/cell data and point specifications (we read cell
  // specifications here).
  vtkIdType superclassPieceSize =
    ((this->NumberOfPointArrays+1)*this->GetNumberOfPointsInPiece(this->Piece)+
     this->NumberOfCellArrays*this->GetNumberOfCellsInPiece(this->Piece));
  
  // Total amount of data in this piece comes from point/cell data
  // arrays and the point/cell specifications themselves (cell
  // specifications for vtkPolyData take two data arrays split across
  // cell types).
  vtkIdType totalPieceSize =
    superclassPieceSize + 2*this->GetNumberOfCellsInPiece(this->Piece);
  if(totalPieceSize == 0)
    {
    totalPieceSize = 1;
    }
  
  // Split the progress range based on the approximate fraction of
  // data that will be read by each step in this method.
  float progressRange[2] = {0,0};
  this->GetProgressRange(progressRange);
  float fractions[6] =
    {
      0,
      float(superclassPieceSize) / totalPieceSize,
      (float(superclassPieceSize)+
       this->NumberOfVerts[this->Piece]) / totalPieceSize,
      (float(superclassPieceSize)+
       this->NumberOfVerts[this->Piece]+
       this->NumberOfLines[this->Piece]) / totalPieceSize,
      (float(superclassPieceSize)+
       this->NumberOfVerts[this->Piece]+
       this->NumberOfLines[this->Piece]+
       this->NumberOfStrips[this->Piece]) / totalPieceSize,
      1
    };
  
  // Set the range of progress for the superclass.
  this->SetProgressRange(progressRange, 0, fractions);
  
  // Let the superclass read its data.
334 335 336 337
  if(!this->Superclass::ReadPieceData())
    {
    return 0;
    }
338
  
339
  vtkPolyData* output = vtkPolyData::SafeDownCast(this->GetCurrentOutput());
340
  
341 342 343
  // Set the range of progress for the Verts.
  this->SetProgressRange(progressRange, 1, fractions);
  
344
  // Read the Verts.
345 346
  vtkXMLDataElement* eVerts = this->VertElements[this->Piece];
  if(eVerts)
347
    {
348 349 350
//    int needToRead = this->CellsNeedToReadTimeStep(eNested, 
//      this->VertsTimeStep, this->VertsOffset);
//    if( needToRead )
351
      {
352 353 354 355 356
      // Read the array.
      if(!this->ReadCellArray(this->NumberOfVerts[this->Piece],
                              this->TotalNumberOfVerts,
                              eVerts,
                              output->GetVerts()))
357
        {
358
        return 0;
359 360
        }
      }
361
    }
362
 
363 364 365
  // Set the range of progress for the Lines.
  this->SetProgressRange(progressRange, 2, fractions);
  
366
  // Read the Lines.
367 368
  vtkXMLDataElement* eLines = this->LineElements[this->Piece];
  if(eLines)
369
    {
370 371 372
//    int needToRead = this->CellsNeedToReadTimeStep(eNested, 
//      this->LinesTimeStep, this->LinesOffset);
//    if( needToRead )
373
      {
374 375 376 377 378
      // Read the array.
      if(!this->ReadCellArray(this->NumberOfLines[this->Piece],
                              this->TotalNumberOfLines,
                              eLines,
                              output->GetLines()))
379
        {
380
        return 0;
381 382
        }
      }
383
    }
384
 
385 386 387
  // Set the range of progress for the Strips.
  this->SetProgressRange(progressRange, 3, fractions);
  
388
  // Read the Strips.
389 390
  vtkXMLDataElement* eStrips = this->StripElements[this->Piece];
  if(eStrips)
391
    {
392 393 394
//    int needToRead = this->CellsNeedToReadTimeStep(eNested, 
//      this->StripsTimeStep, this->StripsOffset);
//    if( needToRead )
395
      {
396 397 398 399 400
      // Read the array.
      if(!this->ReadCellArray(this->NumberOfStrips[this->Piece],
                              this->TotalNumberOfStrips,
                              eStrips,
                              output->GetStrips()))
401
        {
402
        return 0;
403 404
        }
      }
405
    }
406
 
407 408 409
  // Set the range of progress for the Polys.
  this->SetProgressRange(progressRange, 4, fractions);
  
410
  // Read the Polys.
411 412
  vtkXMLDataElement* ePolys = this->PolyElements[this->Piece];
  if(ePolys)
413
    {
414 415 416
//    int needToRead = this->CellsNeedToReadTimeStep(eNested, 
//      this->PolysTimeStep, this->PolysOffset);
//    if( needToRead )
417
      {
418 419 420 421 422
      // Read the array.
      if(!this->ReadCellArray(this->NumberOfPolys[this->Piece],
                              this->TotalNumberOfPolys,
                              ePolys,
                              output->GetPolys()))
423
        {
424 425
        return 0;
        }  
426 427 428
      }
    }
 
429 430 431 432 433
  return 1;
}

//----------------------------------------------------------------------------
int vtkXMLPolyDataReader::ReadArrayForCells(vtkXMLDataElement* da,
Utkarsh Ayachit's avatar
ENH:  
Utkarsh Ayachit committed
434
                                            vtkAbstractArray* outArray)
435
{
436 437 438 439
  // Split progress range according to the fraction of data that will
  // be read for each type of cell.
  float progressRange[2] = {0,0};
  this->GetProgressRange(progressRange);
440
  int total = this->TotalNumberOfCells?this->TotalNumberOfCells:1;
441 442 443
  float fractions[5] =
    {
      0,
444
      float(this->NumberOfVerts[this->Piece])/total,
445
      float(this->NumberOfVerts[this->Piece]+
446
            this->NumberOfLines[this->Piece])/total,
447 448
      float(this->NumberOfVerts[this->Piece]+
            this->NumberOfLines[this->Piece]+
449
            this->NumberOfStrips[this->Piece])/total,
450 451 452
      1
    };
  
453 454
  vtkIdType components = outArray->GetNumberOfComponents();
  
455 456 457
  // Set range of progress for the Verts.
  this->SetProgressRange(progressRange, 0, fractions);
  
458 459 460 461
  // Read the cell data for the Verts in the piece.
  vtkIdType inStartCell = 0;
  vtkIdType outStartCell = this->StartVert;
  vtkIdType numCells = this->NumberOfVerts[this->Piece];  
Utkarsh Ayachit's avatar
ENH:  
Utkarsh Ayachit committed
462 463
  if(!this->ReadArrayValues(da, outStartCell*components, outArray,
      inStartCell*components, numCells*components))
464 465 466
    {
    return 0;
    }
467
  
468 469 470
  // Set range of progress for the Lines.
  this->SetProgressRange(progressRange, 1, fractions);
  
471 472 473 474
  // Read the cell data for the Lines in the piece.
  inStartCell += numCells;
  outStartCell = this->TotalNumberOfVerts + this->StartLine;
  numCells = this->NumberOfLines[this->Piece];  
Utkarsh Ayachit's avatar
ENH:  
Utkarsh Ayachit committed
475 476
  if(!this->ReadArrayValues(da, outStartCell*components, outArray,
      inStartCell*components, numCells*components))
477 478 479
    {
    return 0;
    }
480
  
481 482 483
  // Set range of progress for the Strips.
  this->SetProgressRange(progressRange, 2, fractions);
  
484 485 486 487 488
  // Read the cell data for the Strips in the piece.
  inStartCell += numCells;
  outStartCell = (this->TotalNumberOfVerts + this->TotalNumberOfLines +
                  this->StartStrip);
  numCells = this->NumberOfStrips[this->Piece];  
Utkarsh Ayachit's avatar
ENH:  
Utkarsh Ayachit committed
489 490
  if(!this->ReadArrayValues(da, outStartCell*components, outArray,
      inStartCell*components, numCells*components))
491 492 493
    {
    return 0;
    }
494
  
495 496 497
  // Set range of progress for the Polys.
  this->SetProgressRange(progressRange, 3, fractions);
  
498 499 500 501 502
  // Read the cell data for the Polys in the piece.
  inStartCell += numCells;
  outStartCell = (this->TotalNumberOfVerts + this->TotalNumberOfLines +
                  this->TotalNumberOfStrips + this->StartPoly);
  numCells = this->NumberOfPolys[this->Piece];  
Utkarsh Ayachit's avatar
ENH:  
Utkarsh Ayachit committed
503 504
  if(!this->ReadArrayValues(da, outStartCell*components, outArray,
      inStartCell*components, numCells*components))
505 506 507
    {
    return 0;
    }
508 509 510
  
  return 1;
}
511 512 513 514 515 516 517 518 519


//----------------------------------------------------------------------------
int vtkXMLPolyDataReader::FillOutputPortInformation(int,
                                                 vtkInformation* info)
{
  info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData");
  return 1;
}