vtkAlgorithm.cxx 58.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkAlgorithm.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 "vtkAlgorithm.h"

#include "vtkAlgorithmOutput.h"
18
#include "vtkCellData.h"
19
20
#include "vtkCollection.h"
#include "vtkCollectionIterator.h"
Ken Martin's avatar
Ken Martin committed
21
#include "vtkCommand.h"
22
#include "vtkDataArray.h"
Ken Martin's avatar
Ken Martin committed
23
#include "vtkDataObject.h"
24
#include "vtkDataSet.h"
25
#include "vtkErrorCode.h"
26
#include "vtkFieldData.h"
27
#include "vtkGarbageCollector.h"
28
#include "vtkGraph.h"
29
#include "vtkInformation.h"
30
31
#include "vtkInformationExecutivePortKey.h"
#include "vtkInformationExecutivePortVectorKey.h"
32
33
34
#include "vtkInformationInformationVectorKey.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationStringKey.h"
35
#include "vtkInformationStringVectorKey.h"
36
37
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
38
#include "vtkPointData.h"
39
#include "vtkProgressObserver.h"
40
#include "vtkSmartPointer.h"
41
#include "vtkCompositeDataPipeline.h"
42
#include "vtkTable.h"
43
#include "vtkTrivialProducer.h"
44
#include "vtkNew.h"
45

46
47
#include <set>
#include <vector>
48
49
50

vtkStandardNewMacro(vtkAlgorithm);

51
52
vtkCxxSetObjectMacro(vtkAlgorithm,Information,vtkInformation);

53
vtkInformationKeyMacro(vtkAlgorithm, INPUT_REQUIRED_DATA_TYPE, StringVector);
54
55
56
vtkInformationKeyMacro(vtkAlgorithm, INPUT_IS_OPTIONAL, Integer);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_IS_REPEATABLE, Integer);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_REQUIRED_FIELDS, InformationVector);
57
vtkInformationKeyMacro(vtkAlgorithm, PORT_REQUIREMENTS_FILLED, Integer);
58
59
60
vtkInformationKeyMacro(vtkAlgorithm, INPUT_PORT, Integer);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_CONNECTION, Integer);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_ARRAYS_TO_PROCESS, InformationVector);
61
vtkInformationKeyMacro(vtkAlgorithm, CAN_PRODUCE_SUB_EXTENT, Integer);
Berk Geveci's avatar
Berk Geveci committed
62
vtkInformationKeyMacro(vtkAlgorithm, CAN_HANDLE_PIECE_REQUEST, Integer);
63
64

vtkExecutive* vtkAlgorithm::DefaultExecutivePrototype = 0;
65

66
67
68
69
70
71
//----------------------------------------------------------------------------
class vtkAlgorithmInternals
{
public:
  // Proxy object instances for use in establishing connections from
  // the output ports to other algorithms.
72
  std::vector< vtkSmartPointer<vtkAlgorithmOutput> > Outputs;
73
74
75
76
77
78
};

//----------------------------------------------------------------------------
class vtkAlgorithmToExecutiveFriendship
{
public:
79
  static void SetAlgorithm(vtkExecutive* executive, vtkAlgorithm* algorithm)
80
    {
81
    executive->SetAlgorithm(algorithm);
82
83
84
85
86
87
    }
};

//----------------------------------------------------------------------------
vtkAlgorithm::vtkAlgorithm()
{
Ken Martin's avatar
Ken Martin committed
88
  this->AbortExecute = 0;
89
  this->ErrorCode = 0;
Ken Martin's avatar
Ken Martin committed
90
91
  this->Progress = 0.0;
  this->ProgressText = NULL;
92
  this->Executive = 0;
93
  this->ProgressObserver = 0;
94
95
  this->InputPortInformation = vtkInformationVector::New();
  this->OutputPortInformation = vtkInformationVector::New();
96
  this->AlgorithmInternal = new vtkAlgorithmInternals;
97
  this->Information = vtkInformation::New();
Charles Law's avatar
Charles Law committed
98
99
  this->Information->Register(this);
  this->Information->Delete();
100
101
102
103
104
}

//----------------------------------------------------------------------------
vtkAlgorithm::~vtkAlgorithm()
{
105
  this->SetInformation(0);
106
107
108
109
110
  if(this->Executive)
    {
    this->Executive->UnRegister(this);
    this->Executive = 0;
    }
111
112
113
114
115
  if (this->ProgressObserver)
    {
    this->ProgressObserver->UnRegister(this);
    this->ProgressObserver = 0;
    }
116
117
  this->InputPortInformation->Delete();
  this->OutputPortInformation->Delete();
118
  delete this->AlgorithmInternal;
Ken Martin's avatar
Ken Martin committed
119
120
  delete [] this->ProgressText;
  this->ProgressText = NULL;
121
122
}

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//----------------------------------------------------------------------------
void vtkAlgorithm::SetProgressObserver(vtkProgressObserver* po)
{
  // This intentionally does not modify the algorithm as it
  // is usually done by executives during execution and we don't
  // want the filter to change its mtime during execution.
  if (po != this->ProgressObserver)
    {
    if (this->ProgressObserver)
      {
      this->ProgressObserver->UnRegister(this);
      }
    this->ProgressObserver = po;
    if (po)
      {
      po->Register(this);
      }
    }
}

143
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
144
145
146
147
148
// Update the progress of the process object. If a ProgressMethod exists,
// executes it. Then set the Progress ivar to amount. The parameter amount
// should range between (0,1).
void vtkAlgorithm::UpdateProgress(double amount)
{
149
150
151
152
153
154
155
156
157
  if (this->ProgressObserver)
    {
    this->ProgressObserver->UpdateProgress(amount);
    }
  else
    {
    this->Progress = amount;
    this->InvokeEvent(vtkCommand::ProgressEvent,static_cast<void *>(&amount));
    }
Ken Martin's avatar
Ken Martin committed
158
159
160
}


161
//----------------------------------------------------------------------------
162
163
164
165
166
vtkInformation *vtkAlgorithm
::GetInputArrayFieldInformation(int idx, vtkInformationVector **inputVector)
{
  // first get out association
  vtkInformation *info = this->GetInputArrayInformation(idx);
167

168
169
170
171
172
  // then get the actual info object from the pinfo
  int port = info->Get(INPUT_PORT());
  int connection = info->Get(INPUT_CONNECTION());
  int fieldAssoc = info->Get(vtkDataObject::FIELD_ASSOCIATION());
  vtkInformation *inInfo = inputVector[port]->GetInformationObject(connection);
173

174
175
176
177
178
179
180
181
182
  if (info->Has(vtkDataObject::FIELD_NAME()))
    {
    const char *name = info->Get(vtkDataObject::FIELD_NAME());
    return vtkDataObject::GetNamedFieldInformation(inInfo, fieldAssoc, name);
    }
  int fType = info->Get(vtkDataObject::FIELD_ATTRIBUTE_TYPE());
  return vtkDataObject::GetActiveFieldInformation(inInfo, fieldAssoc, fType);
}

183
//----------------------------------------------------------------------------
184
185
186
vtkInformation *vtkAlgorithm::GetInputArrayInformation(int idx)
{
  // add this info into the algorithms info object
187
  vtkInformationVector *inArrayVec =
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
    this->Information->Get(INPUT_ARRAYS_TO_PROCESS());
  if (!inArrayVec)
    {
    inArrayVec = vtkInformationVector::New();
    this->Information->Set(INPUT_ARRAYS_TO_PROCESS(),inArrayVec);
    inArrayVec->Delete();
    }
  vtkInformation *inArrayInfo = inArrayVec->GetInformationObject(idx);
  if (!inArrayInfo)
    {
    inArrayInfo = vtkInformation::New();
    inArrayVec->SetInformationObject(idx,inArrayInfo);
    inArrayInfo->Delete();
    }
  return inArrayInfo;
}

205
//----------------------------------------------------------------------------
206
207
208
209
210
211
212
void vtkAlgorithm::SetInputArrayToProcess(int idx, vtkInformation *inInfo)
{
  vtkInformation *info = this->GetInputArrayInformation(idx);
  info->Copy(inInfo,1);
  this->Modified();
}

213
//----------------------------------------------------------------------------
214
215
void vtkAlgorithm::SetInputArrayToProcess(int idx, int port, int connection,
                                          int fieldAssociation,
216
217
218
219
220
221
222
223
224
225
                                          int attributeType)
{
  vtkInformation *info = this->GetInputArrayInformation(idx);

  info->Set(INPUT_PORT(), port);
  info->Set(INPUT_CONNECTION(), connection);
  info->Set(vtkDataObject::FIELD_ASSOCIATION(),fieldAssociation);
  info->Set(vtkDataObject::FIELD_ATTRIBUTE_TYPE(), attributeType);

  // remove name if there is one
226
  info->Remove(vtkDataObject::FIELD_NAME());
227
228

  this->Modified();
229
230
}

231
232
//----------------------------------------------------------------------------
void vtkAlgorithm::SetInputArrayToProcess(
233
234
  int idx, int port, int connection,
  const char* fieldAssociation,
235
236
237
238
  const char* fieldAttributeTypeOrName)
{
  if (!fieldAssociation)
    {
Ben Boeckel's avatar
Ben Boeckel committed
239
    vtkErrorMacro("Association is required");
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    return;
    }
  if (!fieldAttributeTypeOrName)
    {
    vtkErrorMacro("Attribute type or array name is required");
    return ;
    }

  int i;

  // Try to convert the string argument to an enum value
  int association = -1;
  for (i=0; i<vtkDataObject::NUMBER_OF_ASSOCIATIONS; i++)
    {
254
    if (strcmp(fieldAssociation,
255
256
257
258
259
260
261
262
263
264
265
               vtkDataObject::GetAssociationTypeAsString(i)) == 0)
      {
      association = i;
      break;
      }
    }
  if (association == -1)
    {
    vtkErrorMacro("Unrecognized association type: " << fieldAssociation);
    return;
    }
266

267
268
269
  int attributeType = -1;
  for (i=0; i<vtkDataSetAttributes::NUM_ATTRIBUTES; i++)
    {
270
    if (strcmp(fieldAttributeTypeOrName,
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
               vtkDataSetAttributes::GetLongAttributeTypeAsString(i)) == 0)
      {
      attributeType = i;
      break;
      }
    }
  if (attributeType == -1)
    {
    // Set by association and array name
    this->SetInputArrayToProcess(
      idx, port, connection, association, fieldAttributeTypeOrName);
    return;
    }

  // Set by association and attribute type
  this->SetInputArrayToProcess(
    idx, port, connection, association, attributeType);
}

290
//----------------------------------------------------------------------------
291
292
void vtkAlgorithm::SetInputArrayToProcess(int idx, int port, int connection,
                                          int fieldAssociation,
293
294
                                          const char *name)
{
295
296
  // ignore NULL string
  if (!name)
297
298
299
    {
    return;
    }
300

301
  vtkInformation *info = this->GetInputArrayInformation(idx);
302
303
304

  // remove fieldAttr if there is one
  info->Remove(vtkDataObject::FIELD_ATTRIBUTE_TYPE());
305
306

  // Check to see whether the current input array matches -
307
308
309
310
311
312
313
314
315
316
317
  // if so we're done.
  if(info->Has(vtkDataObject::FIELD_NAME()) &&
    info->Get(INPUT_PORT()) == port &&
    info->Get(INPUT_CONNECTION()) == connection &&
    info->Get(vtkDataObject::FIELD_ASSOCIATION()) == fieldAssociation &&
    info->Get(vtkDataObject::FIELD_NAME()) &&
    strcmp(info->Get(vtkDataObject::FIELD_NAME()), name)==0)
    {
    return;
    }

318
319
320
321
322
  info->Set(INPUT_PORT(), port);
  info->Set(INPUT_CONNECTION(), connection);
  info->Set(vtkDataObject::FIELD_ASSOCIATION(),fieldAssociation);
  info->Set(vtkDataObject::FIELD_NAME(),name);

323
  this->Modified();
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

//----------------------------------------------------------------------------
int vtkAlgorithm::GetInputArrayAssociation
  (int idx, vtkInformationVector **inputVector)
{
  int association = vtkDataObject::FIELD_ASSOCIATION_NONE;
  this->GetInputArrayToProcess(idx, inputVector, association);
  return association;
}

//----------------------------------------------------------------------------
int vtkAlgorithm::GetInputArrayAssociation
  (int idx, int connection, vtkInformationVector **inputVector)
{
  int association = vtkDataObject::FIELD_ASSOCIATION_NONE;
  this->GetInputArrayToProcess(idx, connection, inputVector, association);
  return association;
}

//----------------------------------------------------------------------------
int vtkAlgorithm::GetInputArrayAssociation(int idx, vtkDataObject* input)
{
  int association = vtkDataObject::FIELD_ASSOCIATION_NONE;
  this->GetInputArrayToProcess(idx, input, association);
  return association;
}

352
353
354
//----------------------------------------------------------------------------
vtkDataArray *vtkAlgorithm::GetInputArrayToProcess(
  int idx, vtkInformationVector **inputVector)
355
356
357
358
359
360
361
362
{
  int association = vtkDataObject::FIELD_ASSOCIATION_NONE;
  return this->GetInputArrayToProcess(idx, inputVector, association);
}

//----------------------------------------------------------------------------
vtkDataArray* vtkAlgorithm::GetInputArrayToProcess
  (int idx, vtkInformationVector **inputVector, int& association)
363
364
{
  return vtkDataArray::SafeDownCast(
365
    this->GetInputAbstractArrayToProcess(idx, inputVector, association));
366
367
368
369
370
}

//----------------------------------------------------------------------------
vtkDataArray *vtkAlgorithm::GetInputArrayToProcess(
  int idx, int connection, vtkInformationVector **inputVector)
371
372
373
374
375
376
377
378
{
  int association = vtkDataObject::FIELD_ASSOCIATION_NONE;
  return this->GetInputArrayToProcess(idx, connection, inputVector, association);
}

//----------------------------------------------------------------------------
vtkDataArray* vtkAlgorithm::GetInputArrayToProcess
  (int idx, int connection, vtkInformationVector **inputVector, int& association)
379
380
{
  return vtkDataArray::SafeDownCast(this->GetInputAbstractArrayToProcess(
381
      idx, connection, inputVector, association));
382
}
383

384
//----------------------------------------------------------------------------
385
vtkDataArray *vtkAlgorithm::GetInputArrayToProcess(
386
  int idx, vtkDataObject* input)
387
388
389
390
391
392
393
394
395
{
  int association = vtkDataObject::FIELD_ASSOCIATION_NONE;
  return this->GetInputArrayToProcess(idx, input, association);
}

//----------------------------------------------------------------------------
vtkDataArray* vtkAlgorithm::GetInputArrayToProcess(int idx,
                                                   vtkDataObject* input,
                                                   int& association)
396
397
{
  return vtkDataArray::SafeDownCast(this->GetInputAbstractArrayToProcess(idx,
398
      input, association));
399
400
401
402
}

//----------------------------------------------------------------------------
vtkAbstractArray *vtkAlgorithm::GetInputAbstractArrayToProcess(
403
  int idx, vtkInformationVector **inputVector)
404
{
405
406
407
408
409
410
411
412
413
  int association = vtkDataObject::FIELD_ASSOCIATION_NONE;
  return this->GetInputAbstractArrayToProcess(idx, inputVector, association);
}

//----------------------------------------------------------------------------
vtkAbstractArray* vtkAlgorithm::GetInputAbstractArrayToProcess
  (int idx, vtkInformationVector **inputVector, int& association)
{
  vtkInformationVector *inArrayVec =
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
    this->Information->Get(INPUT_ARRAYS_TO_PROCESS());
  if (!inArrayVec)
    {
    vtkErrorMacro
      ("Attempt to get an input array for an index that has not been specified");
    return NULL;
    }
  vtkInformation *inArrayInfo = inArrayVec->GetInformationObject(idx);
  if (!inArrayInfo)
    {
    vtkErrorMacro
      ("Attempt to get an input array for an index that has not been specified");
    return NULL;
    }

  int connection = inArrayInfo->Get(INPUT_CONNECTION());
430
431
432
433
434
435
436
437
438
439
440
  return this->GetInputAbstractArrayToProcess
    (idx, connection, inputVector, association);
}

//----------------------------------------------------------------------------
vtkAbstractArray* vtkAlgorithm::GetInputAbstractArrayToProcess
  (int idx, int connection, vtkInformationVector **inputVector)
{
  int association = vtkDataObject::FIELD_ASSOCIATION_NONE;
  return this->GetInputAbstractArrayToProcess
    (idx, connection, inputVector, association);
441
442
443
}

//----------------------------------------------------------------------------
444
vtkAbstractArray* vtkAlgorithm::GetInputAbstractArrayToProcess(
445
  int idx, int connection, vtkInformationVector **inputVector, int& association)
446
{
447
  vtkInformationVector *inArrayVec =
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
    this->Information->Get(INPUT_ARRAYS_TO_PROCESS());
  if (!inArrayVec)
    {
    vtkErrorMacro
      ("Attempt to get an input array for an index that has not been specified");
    return NULL;
    }
  vtkInformation *inArrayInfo = inArrayVec->GetInformationObject(idx);
  if (!inArrayInfo)
    {
    vtkErrorMacro
      ("Attempt to get an input array for an index that has not been specified");
    return NULL;
    }

  int port = inArrayInfo->Get(INPUT_PORT());
464
465
  vtkInformation *inInfo = inputVector[port]->GetInformationObject(connection);
  vtkDataObject *input = inInfo->Get(vtkDataObject::DATA_OBJECT());
466

467
  return this->GetInputAbstractArrayToProcess(idx, input, association);
468
469
470
}

//----------------------------------------------------------------------------
471
472
473
474
475
476
477
478
479
480
vtkAbstractArray* vtkAlgorithm::GetInputAbstractArrayToProcess
  (int idx, vtkDataObject* input)
{
  int association = vtkDataObject::FIELD_ASSOCIATION_NONE;
  return this->GetInputAbstractArrayToProcess(idx, input, association);
}

//----------------------------------------------------------------------------
vtkAbstractArray *vtkAlgorithm::GetInputAbstractArrayToProcess(
  int idx, vtkDataObject* input, int& association)
481
482
483
484
485
486
{
  if (!input)
    {
    return NULL;
    }

487
  vtkInformationVector *inArrayVec =
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
    this->Information->Get(INPUT_ARRAYS_TO_PROCESS());
  if (!inArrayVec)
    {
    vtkErrorMacro
      ("Attempt to get an input array for an index that has not been specified");
    return NULL;
    }
  vtkInformation *inArrayInfo = inArrayVec->GetInformationObject(idx);
  if (!inArrayInfo)
    {
    vtkErrorMacro
      ("Attempt to get an input array for an index that has not been specified");
    return NULL;
    }

  int fieldAssoc = inArrayInfo->Get(vtkDataObject::FIELD_ASSOCIATION());
504
505
  association = fieldAssoc;

506
507
508
  if (inArrayInfo->Has(vtkDataObject::FIELD_NAME()))
    {
    const char *name = inArrayInfo->Get(vtkDataObject::FIELD_NAME());
509

510
511
512
    if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_NONE)
      {
      vtkFieldData *fd = input->GetFieldData();
513
      return fd->GetAbstractArray(name);
514
      }
515

516
517
518
519
520
521
522
523
524
525
526
527
    if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_ROWS)
      {
      vtkTable *inputT = vtkTable::SafeDownCast(input);
      if (!inputT)
        {
        vtkErrorMacro("Attempt to get row data from a non-table");
        return NULL;
        }
      vtkFieldData *fd = inputT->GetRowData();
      return fd->GetAbstractArray(name);
      }

528
529
530
531
532
533
534
535
536
537
538
539
    if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_VERTICES ||
        fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_EDGES)
      {
      vtkGraph *inputG = vtkGraph::SafeDownCast(input);
      if (!inputG)
        {
        vtkErrorMacro("Attempt to get vertex or edge data from a non-graph");
        return NULL;
        }
      vtkFieldData *fd = 0;
      if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_VERTICES)
        {
540
        association = vtkDataObject::FIELD_ASSOCIATION_VERTICES;
541
542
543
544
        fd = inputG->GetVertexData();
        }
      else
        {
545
        association = vtkDataObject::FIELD_ASSOCIATION_EDGES;
546
547
548
549
550
        fd = inputG->GetEdgeData();
        }
      return fd->GetAbstractArray(name);
      }

551
    if (vtkGraph::SafeDownCast(input) &&
552
553
554
555
556
557
        fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS)
      {
      return vtkGraph::SafeDownCast(input)->
        GetVertexData()->GetAbstractArray(name);
      }

558
559
560
561
562
563
    vtkDataSet *inputDS = vtkDataSet::SafeDownCast(input);
    if (!inputDS)
      {
      vtkErrorMacro("Attempt to get point or cell data from a data object");
      return NULL;
      }
564

565
566
    if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS)
      {
567
      return inputDS->GetPointData()->GetAbstractArray(name);
568
      }
569
    if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS
570
        && inputDS->GetPointData()->GetAbstractArray(name))
571
      {
572
      association = vtkDataObject::FIELD_ASSOCIATION_POINTS;
573
      return inputDS->GetPointData()->GetAbstractArray(name);
574
      }
575
576

    association = vtkDataObject::FIELD_ASSOCIATION_CELLS;
577
    return inputDS->GetCellData()->GetAbstractArray(name);
578
    }
579
  else if (inArrayInfo->Has(vtkDataObject::FIELD_ATTRIBUTE_TYPE()))
580
581
582
583
584
585
586
    {
    vtkDataSet *inputDS = vtkDataSet::SafeDownCast(input);
    if (!inputDS)
      {
      vtkErrorMacro("Attempt to get point or cell data from a data object");
      return NULL;
      }
587
    int fType = inArrayInfo->Get(vtkDataObject::FIELD_ATTRIBUTE_TYPE());
588
589
    if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS)
      {
590
      return inputDS->GetPointData()->GetAbstractAttribute(fType);
591
      }
592
    if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS
593
        && inputDS->GetPointData()->GetAbstractAttribute(fType))
594
      {
595
      association = vtkDataObject::FIELD_ASSOCIATION_POINTS;
596
      return inputDS->GetPointData()->GetAbstractAttribute(fType);
597
      }
598
599

    association = vtkDataObject::FIELD_ASSOCIATION_CELLS;
600
    return inputDS->GetCellData()->GetAbstractAttribute(fType);
601
    }
602
603
604
605
  else
    {
    return NULL;
    }
606
607
608
}


609
610
611
612
613
614
//----------------------------------------------------------------------------
void vtkAlgorithm::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  if(this->HasExecutive())
    {
615
    os << indent << "Executive: " << this->Executive << "\n";
616
617
618
619
620
    }
  else
    {
    os << indent << "Executive: (none)\n";
    }
621
622

  os << indent << "ErrorCode: " <<
623
    vtkErrorCode::GetStringFromErrorCode(this->ErrorCode) << endl;
624

625
626
627
628
629
630
631
632
633
  if ( this->Information )
    {
    os << indent << "Information: " << this->Information << "\n";
    }
  else
    {
    os << indent << "Information: (none)\n";
    }

Ken Martin's avatar
Ken Martin committed
634
635
636
637
638
639
640
641
642
643
  os << indent << "AbortExecute: " << (this->AbortExecute ? "On\n" : "Off\n");
  os << indent << "Progress: " << this->Progress << "\n";
  if ( this->ProgressText )
    {
    os << indent << "Progress Text: " << this->ProgressText << "\n";
    }
  else
    {
    os << indent << "Progress Text: (None)\n";
    }
644
645
646
647
648
}

//----------------------------------------------------------------------------
int vtkAlgorithm::HasExecutive()
{
649
  return this->Executive ? 1 : 0;
650
651
652
653
654
655
656
657
}

//----------------------------------------------------------------------------
vtkExecutive* vtkAlgorithm::GetExecutive()
{
  // Create the default executive if we do not have one already.
  if(!this->HasExecutive())
    {
658
    vtkExecutive* e = this->CreateDefaultExecutive();
659
660
661
    this->SetExecutive(e);
    e->Delete();
    }
662
  return this->Executive;
663
664
665
}

//----------------------------------------------------------------------------
666
void vtkAlgorithm::SetExecutive(vtkExecutive* newExecutive)
667
{
668
669
  vtkExecutive* oldExecutive = this->Executive;
  if(newExecutive != oldExecutive)
670
    {
671
    if(newExecutive)
672
      {
673
674
675
676
677
678
679
680
      newExecutive->Register(this);
      vtkAlgorithmToExecutiveFriendship::SetAlgorithm(newExecutive, this);
      }
    this->Executive = newExecutive;
    if(oldExecutive)
      {
      vtkAlgorithmToExecutiveFriendship::SetAlgorithm(oldExecutive, 0);
      oldExecutive->UnRegister(this);
681
682
683
684
      }
    }
}

685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
//----------------------------------------------------------------------------
int vtkAlgorithm::ProcessRequest(vtkInformation* request,
                                 vtkCollection* inInfo,
                                 vtkInformationVector* outInfo)
{
  vtkSmartPointer<vtkCollectionIterator> iter;
  iter.TakeReference(inInfo->NewIterator());

  std::vector<vtkInformationVector*> ivectors;
  for (iter->GoToFirstItem(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
    {
    vtkInformationVector* iv = vtkInformationVector::SafeDownCast(
      iter->GetCurrentObject());
    if (!iv)
      {
      return 0;
      }
    ivectors.push_back(iv);
    }
  if (ivectors.empty())
    {
    return this->ProcessRequest(request, (vtkInformationVector**)0, outInfo);
    }
  else
    {
    return this->ProcessRequest(request, &ivectors[0], outInfo);
    }
}

714
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
715
int vtkAlgorithm::ProcessRequest(vtkInformation* /* request */,
716
717
                                 vtkInformationVector**,
                                 vtkInformationVector*)
718
{
719
  return 1;
720
721
}

722
723
724
725
726
727
728
729
730
731
732
733
//----------------------------------------------------------------------------
int vtkAlgorithm::ComputePipelineMTime(vtkInformation* /* request */,
                                       vtkInformationVector**,
                                       vtkInformationVector*,
                                       int /* requestFromOutputPort */,
                                       unsigned long* mtime)
{
  // By default algorithms contribute only their own modified time.
  *mtime = this->GetMTime();
  return 1;
}

734
735
736
737
738
739
//----------------------------------------------------------------------------
int vtkAlgorithm::ModifyRequest(vtkInformation* /*request*/, int /*when*/)
{
  return 1;
}

740
741
742
//----------------------------------------------------------------------------
int vtkAlgorithm::GetNumberOfInputPorts()
{
743
  return this->InputPortInformation->GetNumberOfInformationObjects();
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
}

//----------------------------------------------------------------------------
void vtkAlgorithm::SetNumberOfInputPorts(int n)
{
  // Sanity check.
  if(n < 0)
    {
    vtkErrorMacro("Attempt to set number of input ports to " << n);
    n = 0;
    }

  // We must remove all connections from ports that are removed.
  for(int i=n; i < this->GetNumberOfInputPorts(); ++i)
    {
759
    this->SetNumberOfInputConnections(i, 0);
760
    }
761
762
763

  // Set the number of input port information objects.
  this->InputPortInformation->SetNumberOfInformationObjects(n);
764
765
766
767
768
}

//----------------------------------------------------------------------------
int vtkAlgorithm::GetNumberOfOutputPorts()
{
769
  return this->OutputPortInformation->GetNumberOfInformationObjects();
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
}

//----------------------------------------------------------------------------
void vtkAlgorithm::SetNumberOfOutputPorts(int n)
{
  // Sanity check.
  if(n < 0)
    {
    vtkErrorMacro("Attempt to set number of output ports to " << n);
    n = 0;
    }

  // We must remove all connections from ports that are removed.
  for(int i=n; i < this->GetNumberOfOutputPorts(); ++i)
    {
785
786
787
788
789
    // Get the producer and its output information for this port.
    vtkExecutive* producer = this->GetExecutive();
    vtkInformation* info = producer->GetOutputInformation(i);

    // Remove all consumers' references to this producer on this port.
790
791
792
    vtkExecutive** consumers = vtkExecutive::CONSUMERS()->GetExecutives(info);
    int* consumerPorts = vtkExecutive::CONSUMERS()->GetPorts(info);
    int consumerCount = vtkExecutive::CONSUMERS()->Length(info);
793
    for(int j=0; j < consumerCount; ++j)
794
795
      {
      vtkInformationVector* inputs =
796
        consumers[j]->GetInputInformation(consumerPorts[j]);
797
798
799
800
      inputs->Remove(info);
      }

    // Remove this producer's references to all consumers on this port.
801
    vtkExecutive::CONSUMERS()->Remove(info);
802
    }
803
804
805
806
807

  // Set the number of output port information objects.
  this->OutputPortInformation->SetNumberOfInformationObjects(n);

  // Set the number of connection proxy objects.
808
809
810
811
  this->AlgorithmInternal->Outputs.resize(n);
}

//----------------------------------------------------------------------------
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
vtkInformation* vtkAlgorithm::GetInputPortInformation(int port)
{
  if(!this->InputPortIndexInRange(port, "get information object for"))
    {
    return 0;
    }

  // Get the input port information object.
  vtkInformation* info =
    this->InputPortInformation->GetInformationObject(port);

  // Fill it if it has not yet been filled.
  if(!info->Has(PORT_REQUIREMENTS_FILLED()))
    {
    if(this->FillInputPortInformation(port, info))
      {
      info->Set(PORT_REQUIREMENTS_FILLED(), 1);
      }
    else
      {
      info->Clear();
      }
    }

Kyle Lutz's avatar
Kyle Lutz committed
836
  // Return the information object.
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
  return info;
}

//----------------------------------------------------------------------------
vtkInformation* vtkAlgorithm::GetOutputPortInformation(int port)
{
  if(!this->OutputPortIndexInRange(port, "get information object for"))
    {
    return 0;
    }

  // Get the output port information object.
  vtkInformation* info =
    this->OutputPortInformation->GetInformationObject(port);

  // Fill it if it has not yet been filled.
  if(!info->Has(PORT_REQUIREMENTS_FILLED()))
    {
    if(this->FillOutputPortInformation(port, info))
      {
      info->Set(PORT_REQUIREMENTS_FILLED(), 1);
      }
    else
      {
      info->Clear();
      }
    }

Kyle Lutz's avatar
Kyle Lutz committed
865
  // Return the information object.
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
  return info;
}

//----------------------------------------------------------------------------
int vtkAlgorithm::FillInputPortInformation(int, vtkInformation*)
{
  vtkErrorMacro("FillInputPortInformation is not implemented.");
  return 0;
}

//----------------------------------------------------------------------------
int vtkAlgorithm::FillOutputPortInformation(int, vtkInformation*)
{
  vtkErrorMacro("FillOutputPortInformation is not implemented.");
  return 0;
}


//----------------------------------------------------------------------------
int vtkAlgorithm::InputPortIndexInRange(int index, const char* action)
{
  // Make sure the index of the input port is in range.
  if(index < 0 || index >= this->GetNumberOfInputPorts())
    {
    vtkErrorMacro("Attempt to " << (action?action:"access")
                  << " input port index " << index
                  << " for an algorithm with "
                  << this->GetNumberOfInputPorts() << " input ports.");
    return 0;
    }
  return 1;
}

//----------------------------------------------------------------------------
int vtkAlgorithm::OutputPortIndexInRange(int index, const char* action)
901
{
902
903
  // Make sure the index of the output port is in range.
  if(index < 0 || index >= this->GetNumberOfOutputPorts())
904
    {
905
906
907
908
    vtkErrorMacro("Attempt to " << (action?action:"access")
                  << " output port index " << index
                  << " for an algorithm with "
                  << this->GetNumberOfOutputPorts() << " output ports.");
909
910
    return 0;
    }
911
912
913
  return 1;
}

914
915
916
917
918
919
920
921
922
923
924
925
//----------------------------------------------------------------------------
void vtkAlgorithm::SetDefaultExecutivePrototype(vtkExecutive* proto)
{
  if (vtkAlgorithm::DefaultExecutivePrototype == proto)
    {
    return;
    }
  if (vtkAlgorithm::DefaultExecutivePrototype)
    {
    vtkAlgorithm::DefaultExecutivePrototype->UnRegister(0);
    vtkAlgorithm::DefaultExecutivePrototype = 0;
    }
926
927
928
929
  if (proto)
    {
    proto->Register(0);
    }
930
931
932
  vtkAlgorithm::DefaultExecutivePrototype = proto;
}

933
934
935
//----------------------------------------------------------------------------
vtkExecutive* vtkAlgorithm::CreateDefaultExecutive()
{
936
937
938
939
  if (vtkAlgorithm::DefaultExecutivePrototype)
    {
    return vtkAlgorithm::DefaultExecutivePrototype->NewInstance();
    }
940
  return vtkCompositeDataPipeline::New();
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
}

//----------------------------------------------------------------------------
void vtkAlgorithm::Register(vtkObjectBase* o)
{
  this->RegisterInternal(o, 1);
}

//----------------------------------------------------------------------------
void vtkAlgorithm::UnRegister(vtkObjectBase* o)
{
  this->UnRegisterInternal(o, 1);
}

//----------------------------------------------------------------------------
void vtkAlgorithm::ReportReferences(vtkGarbageCollector* collector)
{
  this->Superclass::ReportReferences(collector);
  vtkGarbageCollectorReport(collector, this->Executive, "Executive");
}



/// These are convenience methods to forward to the executive


//----------------------------------------------------------------------------
vtkDataObject* vtkAlgorithm::GetOutputDataObject(int port)
{
970
  return this->GetExecutive()->GetOutputData(port);
971
972
}

973
974
975
976
977
978
979
//----------------------------------------------------------------------------
vtkDataObject *vtkAlgorithm::GetInputDataObject(int port,
                                                int connection)
{
  return this->GetExecutive()->GetInputData(port,connection);
}

980
981
982
983
984
985
//----------------------------------------------------------------------------
void vtkAlgorithm::RemoveAllInputs()
{
  this->SetInputConnection(0, 0);
}

986
987
988
989
990
991
//----------------------------------------------------------------------------
void vtkAlgorithm::RemoveAllInputConnections(int port)
{
  this->SetInputConnection(port, 0);
}

992
//----------------------------------------------------------------------------
993
994
995
996
void vtkAlgorithm::SetInputConnection(vtkAlgorithmOutput* input)
{
  this->SetInputConnection(0,input);
}
Mathieu Malaterre's avatar
Mathieu Malaterre committed
997
998

//----------------------------------------------------------------------------
999
void vtkAlgorithm::SetInputConnection(int port, vtkAlgorithmOutput* input)
1000
{
For faster browsing, not all history is shown. View entire blame