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
}

//----------------------------------------------------------------------------
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];
    }
145

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  // 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

  // Setup the output's cell arrays.
204 205 206 207
  vtkCellArray* outVerts = vtkCellArray::New();
  vtkCellArray* outLines = vtkCellArray::New();
  vtkCellArray* outStrips = vtkCellArray::New();
  vtkCellArray* outPolys = vtkCellArray::New();
208

209 210 211 212
  output->SetVerts(outVerts);
  output->SetLines(outLines);
  output->SetStrips(outStrips);
  output->SetPolys(outPolys);
213

214 215 216 217 218 219 220 221 222
  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
  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;
    }
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
  // 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;
      }
    }
275

276 277 278 279 280 281
  return 1;
}

//----------------------------------------------------------------------------
void vtkXMLPolyDataReader::SetupNextPiece()
{
282
  this->Superclass::SetupNextPiece();
283
  this->StartVert += this->NumberOfVerts[this->Piece];
284
  this->StartLine += this->NumberOfLines[this->Piece];
285 286 287 288 289 290 291
  this->StartStrip += this->NumberOfStrips[this->Piece];
  this->StartPoly += this->NumberOfPolys[this->Piece];
}

//----------------------------------------------------------------------------
int vtkXMLPolyDataReader::ReadPieceData()
{
292 293 294 295 296 297
  // 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));
298

299 300 301 302 303 304 305 306 307 308
  // 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;
    }
309

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
  // 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
    };
329

330 331
  // Set the range of progress for the superclass.
  this->SetProgressRange(progressRange, 0, fractions);
332

333
  // 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
  // Set the range of progress for the Verts.
  this->SetProgressRange(progressRange, 1, fractions);
343

344
  // Read the Verts.
345 346
  vtkXMLDataElement* eVerts = this->VertElements[this->Piece];
  if(eVerts)
347
    {
348
//    int needToRead = this->CellsNeedToReadTimeStep(eNested,
349 350
//      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
  // Set the range of progress for the Lines.
  this->SetProgressRange(progressRange, 2, fractions);
365

366
  // Read the Lines.
367 368
  vtkXMLDataElement* eLines = this->LineElements[this->Piece];
  if(eLines)
369
    {
370
//    int needToRead = this->CellsNeedToReadTimeStep(eNested,
371 372
//      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
  // Set the range of progress for the Strips.
  this->SetProgressRange(progressRange, 3, fractions);
387

388
  // Read the Strips.
389 390
  vtkXMLDataElement* eStrips = this->StripElements[this->Piece];
  if(eStrips)
391
    {
392
//    int needToRead = this->CellsNeedToReadTimeStep(eNested,
393 394
//      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
  // Set the range of progress for the Polys.
  this->SetProgressRange(progressRange, 4, fractions);
409

410
  // Read the Polys.
411 412
  vtkXMLDataElement* ePolys = this->PolyElements[this->Piece];
  if(ePolys)
413
    {
414
//    int needToRead = this->CellsNeedToReadTimeStep(eNested,
415 416
//      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
        return 0;
425
        }
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
      1
    };
452

453
  vtkIdType components = outArray->GetNumberOfComponents();
454

455 456
  // Set range of progress for the Verts.
  this->SetProgressRange(progressRange, 0, fractions);
457

458 459 460
  // Read the cell data for the Verts in the piece.
  vtkIdType inStartCell = 0;
  vtkIdType outStartCell = this->StartVert;
461
  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
  // Set range of progress for the Lines.
  this->SetProgressRange(progressRange, 1, fractions);
470

471 472 473
  // Read the cell data for the Lines in the piece.
  inStartCell += numCells;
  outStartCell = this->TotalNumberOfVerts + this->StartLine;
474
  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
  // Set range of progress for the Strips.
  this->SetProgressRange(progressRange, 2, fractions);
483

484 485 486 487
  // Read the cell data for the Strips in the piece.
  inStartCell += numCells;
  outStartCell = (this->TotalNumberOfVerts + this->TotalNumberOfLines +
                  this->StartStrip);
488
  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
  // Set range of progress for the Polys.
  this->SetProgressRange(progressRange, 3, fractions);
497

498 499 500 501
  // Read the cell data for the Polys in the piece.
  inStartCell += numCells;
  outStartCell = (this->TotalNumberOfVerts + this->TotalNumberOfLines +
                  this->TotalNumberOfStrips + this->StartPoly);
502
  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;
}