File: vtkAppendFilter.cxx

package info (click to toggle)
vtk 5.8.0-13
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 130,524 kB
  • sloc: cpp: 1,129,256; ansic: 708,203; tcl: 48,526; python: 20,875; xml: 6,779; yacc: 4,208; perl: 3,121; java: 2,788; lex: 931; sh: 660; asm: 471; makefile: 299
file content (627 lines) | stat: -rw-r--r-- 18,808 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
114
115
116
117
118
119
120
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
284
285
286
287
288
289
290
291
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkAppendFilter.cxx

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

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkAppendFilter.h"

#include "vtkCell.h"
#include "vtkCellData.h"
#include "vtkDataSetAttributes.h"
#include "vtkDataSetCollection.h"
#include "vtkExecutive.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkUnstructuredGrid.h"
#include "vtkIncrementalOctreePointLocator.h"

vtkStandardNewMacro(vtkAppendFilter);

//----------------------------------------------------------------------------
vtkAppendFilter::vtkAppendFilter()
{
  this->InputList = NULL;
  this->MergePoints = 0;
}

//----------------------------------------------------------------------------
vtkAppendFilter::~vtkAppendFilter()
{
  if (this->InputList != NULL)
    {
    this->InputList->Delete();
    this->InputList = NULL;
    }
}

//----------------------------------------------------------------------------
vtkDataSet *vtkAppendFilter::GetInput(int idx)
{
  if (idx >= this->GetNumberOfInputConnections(0) || idx < 0)
    {
    return NULL;
    }
  
  return vtkDataSet::SafeDownCast(
    this->GetExecutive()->GetInputData(0, idx));
}

//----------------------------------------------------------------------------
// Remove a dataset from the list of data to append.
void vtkAppendFilter::RemoveInput(vtkDataSet *ds)
{
  vtkAlgorithmOutput *algOutput = 0;
  if (ds)
    {
    algOutput = ds->GetProducerPort();
    }

  this->RemoveInputConnection(0, algOutput);
}

//----------------------------------------------------------------------------
vtkDataSetCollection *vtkAppendFilter::GetInputList()
{
  int idx;
  
  if (this->InputList)
    {
    this->InputList->Delete();
    }
  this->InputList = vtkDataSetCollection::New();
  
  for (idx = 0; idx < this->GetNumberOfInputConnections(0); ++idx)
    {
    if (this->GetInput(idx))
      {
      this->InputList->AddItem(this->GetInput(idx));
      }
    }  
  
  return this->InputList;
}

//----------------------------------------------------------------------------
// Append data sets into single unstructured grid
int vtkAppendFilter::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **inputVector,
  vtkInformationVector *outputVector)
{
   if (this->MergePoints == 1 &&
     inputVector[0]->GetNumberOfInformationObjects() > 0 )
     {
     vtkDataSet * tempData = 
     vtkDataSet::SafeDownCast
                 (  inputVector[0]->GetInformationObject( 0 )
                                  ->Get( vtkDataObject::DATA_OBJECT() )  );
     if (  tempData && tempData->GetCellData() &&
          !tempData->GetCellData()->GetArray( "vtkGhostLevels" )  ) 
       {
       tempData = NULL;
       return this->AppendBlocksWithPointLocator( inputVector, outputVector );
       }
     }
  
  // get the output info object
  vtkInformation *outInfo = outputVector->GetInformationObject(0);

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

  vtkIdType numPts, numCells, ptOffset;
  int   tenth, count, abort=0;
  float decimal;
  vtkPoints *newPts;
  vtkPointData *pd;
  vtkCellData *cd;
  vtkIdList *ptIds, *newPtIds;
  int i, idx;
  vtkDataSet *ds;
  vtkIdType ptId, cellId, newCellId;
  vtkPointData *outputPD = output->GetPointData();
  vtkCellData *outputCD = output->GetCellData();

  vtkDebugMacro(<<"Appending data together");

  // Loop over all data sets, checking to see what data is common to 
  // all inputs. Note that data is common if 1) it is the same attribute 
  // type (scalar, vector, etc.), 2) it is the same native type (int, 
  // float, etc.), and 3) if a data array in a field, if it has the same name.
  count   = 0;
  decimal = 0.0;

  numPts = 0;
  numCells = 0;

  int numInputs = inputVector[0]->GetNumberOfInformationObjects();
  vtkDataSetAttributes::FieldList ptList(numInputs);
  vtkDataSetAttributes::FieldList cellList(numInputs);
  int firstPD=1;
  int firstCD=1;
  vtkInformation *inInfo = 0;
  for (idx = 0; idx < numInputs; ++idx)
    {
    inInfo = inputVector[0]->GetInformationObject(idx);
    ds = 0;
    if (inInfo)
      {
      ds = vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
      }
    if (ds != NULL)
      {
      if ( ds->GetNumberOfPoints() <= 0 && ds->GetNumberOfCells() <= 0 )
        {
        continue; //no input, just skip
        }

      numPts += ds->GetNumberOfPoints();
      numCells += ds->GetNumberOfCells();

      pd = ds->GetPointData();
      if ( firstPD )
        {
        ptList.InitializeFieldList(pd);
        firstPD = 0;
        }
      else
        {
        ptList.IntersectFieldList(pd);
        }
      
      cd = ds->GetCellData();
      if ( firstCD )
        {
        cellList.InitializeFieldList(cd);
        firstCD = 0;
        }
      else
        {
        cellList.IntersectFieldList(cd);
        }
      }//if non-empty dataset
    }//for all inputs

  if ( numPts < 1)
    {
    vtkDebugMacro(<<"No data to append!");
    return 1;
    }
  
  // Now can allocate memory
  output->Allocate(numCells); //allocate storage for geometry/topology
  outputPD->CopyGlobalIdsOn();
  outputPD->CopyAllocate(ptList,numPts);
  outputCD->CopyGlobalIdsOn();
  outputCD->CopyAllocate(cellList,numCells);

  newPts = vtkPoints::New();
  newPts->SetNumberOfPoints(numPts);
  ptIds = vtkIdList::New(); ptIds->Allocate(VTK_CELL_SIZE);
  newPtIds = vtkIdList::New(); newPtIds->Allocate(VTK_CELL_SIZE);
  
  // Append each input dataset together
  //
  tenth = (numPts + numCells)/10 + 1;
  ptOffset=0;
  int inputCount = 0; // Since empty inputs are not in the list.
  for (idx = 0; idx < numInputs && !abort; ++idx)
    {
    inInfo = inputVector[0]->GetInformationObject(idx);
    ds = 0;
    if (inInfo)
      {
      ds = vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
      }
    if ( ds != NULL &&
         (ds->GetNumberOfPoints() > 0 || ds->GetNumberOfCells() > 0) )
      {
      numPts = ds->GetNumberOfPoints();
      numCells = ds->GetNumberOfCells();
      pd = ds->GetPointData();
      
      // copy points and point data
      for (ptId=0; ptId < numPts && !abort; ptId++)
        {
        newPts->SetPoint(ptId+ptOffset,ds->GetPoint(ptId));
        outputPD->CopyData(ptList,pd,inputCount,ptId,ptId+ptOffset);
        
        // Update progress
        count++;
        if ( !(count % tenth) ) 
          {
          decimal += 0.1;
          this->UpdateProgress (decimal);
          abort = this->GetAbortExecute();
          }
        }
      
      cd = ds->GetCellData();
      // copy cell and cell data
      vtkUnstructuredGrid *ug = vtkUnstructuredGrid::SafeDownCast(ds);
      for (cellId=0; cellId < numCells && !abort; cellId++)
        {
        if (ug && ds->GetCellType(cellId) == VTK_POLYHEDRON )
          {
          vtkIdType nfaces, *facePtIds;
          ug->GetFaceStream(cellId,nfaces,facePtIds);
          for(vtkIdType id=0; id < nfaces; ++id)
            {
            vtkIdType nPoints = *facePtIds;
            newPtIds->InsertNextId(nPoints);
            for (vtkIdType j=0; j <= nPoints; ++j)
              {
              newPtIds->InsertNextId(*(++facePtIds)+ptOffset);
              }
            }
          output->InsertNextCell(VTK_POLYHEDRON,nfaces,newPtIds->GetPointer(0));
          }
        else
          {
          ds->GetCellPoints(cellId, ptIds);
          newPtIds->Reset ();
          for (i=0; i < ptIds->GetNumberOfIds(); i++)
            {
            newPtIds->InsertId(i,ptIds->GetId(i)+ptOffset);
            }
          newCellId = output->InsertNextCell(ds->GetCellType(cellId),newPtIds);
          outputCD->CopyData(cellList,cd,inputCount,cellId,newCellId);
          }
        
        // Update progress
        count++;
        if ( !(count % tenth) ) 
          {
          decimal += 0.1;
          this->UpdateProgress (decimal);
          abort = this->GetAbortExecute();
          }
        }
      ptOffset+=numPts;
      ++inputCount;
      }
    }
  
  // Update ourselves and release memory
  //
  output->SetPoints(newPts);
  newPts->Delete();
  ptIds->Delete();
  newPtIds->Delete();

  output->Squeeze();
  return 1;
}
  
//----------------------------------------------------------------------------
int vtkAppendFilter::AppendBlocksWithPointLocator
 ( vtkInformationVector ** inputVector, vtkInformationVector  * outputVector )
{
  vtkInformation      * outInf = outputVector->GetInformationObject( 0 );
  vtkUnstructuredGrid * output = 
  vtkUnstructuredGrid::SafeDownCast
                       (  outInf->Get( vtkDataObject::DATA_OBJECT() )  );
  outInf = NULL;

  int            i, idx, tenth, count, abort = 0;
  float          decimal;
  double       * localBox    = NULL;
  double         dataBbox[6] = { VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
                                 VTK_DOUBLE_MAX, VTK_DOUBLE_MIN,
                                 VTK_DOUBLE_MAX, VTK_DOUBLE_MIN
                               };
  vtkIdType      ptId,   cellId;
  vtkIdType      numPts, numCells;
  vtkIdType      inputCount, ptOffset, cellOffset;           
  vtkIdList    * ptIds    = NULL;
  vtkIdList    * newPtIds = NULL;
  vtkPoints    * newPts   = NULL;
  vtkDataSet   * ds       = NULL;
  vtkCellData  * cd       = NULL;
  vtkCellData  * outputCD = output->GetCellData();
  vtkPointData * pd       = NULL;
  vtkPointData * outputPD = output->GetPointData();
                          
  vtkDebugMacro( <<"Appending data together" );

  // Loop over all data sets, checking to see what data is common to 
  // all inputs. Note that data is common if 1) it is the same attribute 
  // type (scalar, vector, etc.), 2) it is the same native type (int, 
  // float, etc.), and 3) if a data array in a field, if it has the same name.
  count    = 0;
  numPts   = 0;
  decimal  = 0.0;
  numCells = 0;
  
  int   firstPD   = 1;
  int   firstCD   = 1;
  int   numInputs = inputVector[0]->GetNumberOfInformationObjects();
  vtkInformation  * inInfo = NULL;
  vtkDataSetAttributes::FieldList ptList  ( numInputs );
  vtkDataSetAttributes::FieldList cellList( numInputs );
  
  for ( idx = 0; idx < numInputs; idx ++ )
    {
    ds     = NULL;
    inInfo = inputVector[0]->GetInformationObject( idx );
    if (inInfo)
      {
      ds = vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
      }
      
    if ( ds != NULL )
      {
      if ( ds->GetNumberOfPoints() <= 0 && ds->GetNumberOfCells() <= 0 )
        {
        continue; //no input, just skip
        }

      numPts   += ds->GetNumberOfPoints();
      numCells += ds->GetNumberOfCells();
      
      // for merging duplicate points
      localBox    = ds->GetBounds();
      dataBbox[0] = ( localBox[0] < dataBbox[0] ) ? localBox[0] : dataBbox[0];
      dataBbox[2] = ( localBox[2] < dataBbox[2] ) ? localBox[2] : dataBbox[2];
      dataBbox[4] = ( localBox[4] < dataBbox[4] ) ? localBox[4] : dataBbox[4];
      dataBbox[1] = ( localBox[1] > dataBbox[1] ) ? localBox[1] : dataBbox[1];
      dataBbox[3] = ( localBox[3] > dataBbox[3] ) ? localBox[3] : dataBbox[3];
      dataBbox[5] = ( localBox[5] > dataBbox[5] ) ? localBox[5] : dataBbox[5];
      localBox    = NULL;

      pd = ds->GetPointData();
      if ( firstPD )
        {
        ptList.InitializeFieldList( pd );
        firstPD = 0;
        }
      else
        {
        ptList.IntersectFieldList( pd );
        }
      
      cd = ds->GetCellData();
      if ( firstCD )
        {
        cellList.InitializeFieldList( cd );
        firstCD = 0;
        }
      else
        {
        cellList.IntersectFieldList(cd);
        }
      }//if non-empty dataset
    }//for all inputs

  if ( numPts < 1 )
    {
    vtkDebugMacro( <<"No data to append!" );
    return 1;
    }
  
  // Now can allocate memory
  output->Allocate( numCells );
  newPts = vtkPoints::New();
  ptIds  = vtkIdList::New(); 
  ptIds->Allocate( VTK_CELL_SIZE );
  newPtIds = vtkIdList::New(); 
  newPtIds->Allocate( VTK_CELL_SIZE );
  
  // for merging duplicate points
  int             beInserted;
  vtkIdType       globalPtId;
  vtkIdType     * globalIdxs = new vtkIdType     [ numPts ];
  unsigned char * duplicated = new unsigned char [ numPts ];
  vtkIncrementalOctreePointLocator * ptInserter =
  vtkIncrementalOctreePointLocator::New();
  ptInserter->SetTolerance( 0.0 );
  ptInserter->InitPointInsertion( newPts, dataBbox );
  
  // append the blocks / pieces in terms of the geoemtry and topology
  count    = 0;
  tenth    = ( numPts + numCells ) / 10 + 1;
  ptOffset = 0;
  for ( idx = 0; idx < numInputs && !abort; idx ++ )
    {
    ds     = NULL;
    inInfo = inputVector[0]->GetInformationObject( idx );
    if ( inInfo )
      {
      ds = vtkDataSet::SafeDownCast
                       (  inInfo->Get( vtkDataObject::DATA_OBJECT() )  );
      }
      
    if (   ds != NULL &&
         ( ds->GetNumberOfPoints() > 0 || ds->GetNumberOfCells() > 0 )
       )
      {
      numPts   = ds->GetNumberOfPoints();
      numCells = ds->GetNumberOfCells();
      
      // copy points --- merge duplicate points if any
      for ( ptId = 0; ptId < numPts && !abort; ptId ++ )
        {
        beInserted = ptInserter->InsertUniquePoint
                     (  ds->GetPoint( ptId ),  globalPtId  );
        globalIdxs[ ptId + ptOffset ] = globalPtId;
        duplicated[ ptId + ptOffset ] = static_cast <unsigned char> 
                                        ( 1 - beInserted );
        
        count ++;
        if (  !( count % tenth )  ) 
          {
          decimal += 0.1;
          this->UpdateProgress ( decimal );
          abort    = this->GetAbortExecute();
          }
        }
      
      // copy cells --- using the new (global) point Ids
      for ( cellId = 0; cellId < numCells && !abort; cellId ++ )
        {
        newPtIds->Reset();
        if (vtkUnstructuredGrid::SafeDownCast(ds) && 
            ds->GetCellType( cellId ) == VTK_POLYHEDRON)
          {
          vtkUnstructuredGrid * ug = vtkUnstructuredGrid::SafeDownCast(ds);
          vtkIdType nfaces, *facePtIds;
          ug->GetFaceStream(cellId, nfaces, facePtIds);
          for (vtkIdType id = 0; id < nfaces; id++)
            {
            vtkIdType nPoints = facePtIds[0];
            newPtIds->InsertNextId(nPoints);
            for (vtkIdType j = 1; j <= nPoints; j++)
              {
              newPtIds->InsertNextId(globalIdxs[facePtIds[j] + ptOffset]);
              }
            facePtIds += nPoints + 1;
            }
          output->InsertNextCell( VTK_POLYHEDRON, nfaces, newPtIds->GetPointer(0));
          }
        else
          {
          ds->GetCellPoints( cellId, ptIds );
          for ( i = 0; i < ptIds->GetNumberOfIds(); i ++ )
            {
            newPtIds->InsertId(  i,  globalIdxs[ ptIds->GetId(i) + ptOffset ]  );
            }
          output->InsertNextCell(  ds->GetCellType( cellId ),  newPtIds  );
          }
        
        count ++;
        if (  !( count % tenth )  ) 
          {
          decimal += 0.1;
          this->UpdateProgress( decimal );
          abort    = this->GetAbortExecute();
          }
        }
      
      ptOffset += numPts;
      }
    }
  
  // copy the associated point data and cell data
  count      = 0;  
  numPts     = newPts->GetNumberOfPoints(); // unique points
  tenth      = ( numPts + numCells ) / 10 + 1;
  ptOffset   = 0;
  cellOffset = 0;
  inputCount = 0;
  outputPD->CopyGlobalIdsOn();
  outputPD->CopyAllocate( ptList, numPts );
  outputCD->CopyGlobalIdsOn();
  outputCD->CopyAllocate( cellList, numCells );
  
  for ( idx = 0; idx < numInputs && !abort; idx ++ )
    {
    ds     = NULL;
    inInfo = inputVector[0]->GetInformationObject( idx );
    if ( inInfo )
      {
      ds = vtkDataSet::SafeDownCast
                       (  inInfo->Get( vtkDataObject::DATA_OBJECT() )  );
      }
      
    if (   ds != NULL &&
         ( ds->GetNumberOfPoints() > 0 || ds->GetNumberOfCells() > 0 ) 
       )
      {
      numPts   = ds->GetNumberOfPoints();
      numCells = ds->GetNumberOfCells();
      
      // copy point data
      pd = ds->GetPointData();
      for ( ptId = 0; ptId < numPts && !abort; ptId ++ )
        {
        if (  duplicated[ ptId + ptOffset ]  ==  0  )
          {
          outputPD->CopyData(  ptList,  pd,  inputCount,  ptId, 
                               globalIdxs[ ptId + ptOffset ]  );
          }
        
        count ++;
        if (  !( count % tenth )  ) 
          {
          decimal += 0.1;
          this->UpdateProgress( decimal );
          abort    = this->GetAbortExecute();
          }
        }
      
      // copy cell data
      cd = ds->GetCellData();
      for ( cellId = 0; cellId < numCells && !abort; cellId ++ )
        {
        outputCD->CopyData( cellList, cd, inputCount, cellId, 
                            cellOffset + cellId );
        
        count ++;
        if (  !( count % tenth )  ) 
          {
          decimal += 0.1;
          this->UpdateProgress ( decimal );
          abort    = this->GetAbortExecute();
          }
        }
        
      inputCount ++;
      ptOffset   += numPts;
      cellOffset += numCells;
      }
    }
  
  // attach the points
  output->SetPoints( newPts );
  
  // memory deallocation
  delete [] duplicated;
  delete [] globalIdxs;
  ptInserter->Delete();
  newPtIds->Delete();
  newPts->Delete();
  ptIds->Delete();
  
  duplicated = NULL;
  globalIdxs = NULL;
  ptInserter = NULL;
  newPtIds   = NULL;
  outputPD   = NULL;
  outputCD   = NULL;
  output     = NULL;
  newPts     = NULL;
  inInfo     = NULL;
  ptIds      = NULL;
  pd         = NULL;
  cd         = NULL;
  ds         = NULL;
  
  return 1;
}

//----------------------------------------------------------------------------
int vtkAppendFilter::FillInputPortInformation(int, vtkInformation *info)
{
  info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataSet");
  info->Set(vtkAlgorithm::INPUT_IS_REPEATABLE(), 1);
  return 1;
}

//----------------------------------------------------------------------------
void vtkAppendFilter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);
  os << "MergePoints:" << (this->MergePoints?"On":"Off") << endl;
}