forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkAlgorithm.cxx
1363 lines (1187 loc) · 40.6 KB
/
vtkAlgorithm.cxx
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
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
865
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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
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"
#include "vtkCellData.h"
#include "vtkCommand.h"
#include "vtkDataArray.h"
#include "vtkDataObject.h"
#include "vtkDataSet.h"
#include "vtkErrorCode.h"
#include "vtkFieldData.h"
#include "vtkGarbageCollector.h"
#include "vtkGraph.h"
#include "vtkInformation.h"
#include "vtkInformationExecutivePortKey.h"
#include "vtkInformationExecutivePortVectorKey.h"
#include "vtkInformationInformationVectorKey.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationStringKey.h"
#include "vtkInformationStringVectorKey.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkTable.h"
#include <vtkstd/set>
#include <vtkstd/vector>
vtkStandardNewMacro(vtkAlgorithm);
vtkCxxSetObjectMacro(vtkAlgorithm,Information,vtkInformation);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_REQUIRED_DATA_TYPE, StringVector);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_IS_OPTIONAL, Integer);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_IS_REPEATABLE, Integer);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_REQUIRED_FIELDS, InformationVector);
vtkInformationKeyMacro(vtkAlgorithm, PORT_REQUIREMENTS_FILLED, Integer);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_PORT, Integer);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_CONNECTION, Integer);
vtkInformationKeyMacro(vtkAlgorithm, INPUT_ARRAYS_TO_PROCESS, InformationVector);
vtkInformationKeyMacro(vtkAlgorithm, PRESERVES_DATASET, Integer);
vtkInformationKeyMacro(vtkAlgorithm, PRESERVES_GEOMETRY, Integer);
vtkInformationKeyMacro(vtkAlgorithm, PRESERVES_BOUNDS, Integer);
vtkInformationKeyMacro(vtkAlgorithm, PRESERVES_TOPOLOGY, Integer);
vtkInformationKeyMacro(vtkAlgorithm, PRESERVES_ATTRIBUTES, Integer);
vtkInformationKeyMacro(vtkAlgorithm, PRESERVES_RANGES, Integer);
vtkExecutive* vtkAlgorithm::DefaultExecutivePrototype = 0;
//----------------------------------------------------------------------------
class vtkAlgorithmInternals
{
public:
// Proxy object instances for use in establishing connections from
// the output ports to other algorithms.
vtkstd::vector< vtkSmartPointer<vtkAlgorithmOutput> > Outputs;
};
//----------------------------------------------------------------------------
class vtkAlgorithmToExecutiveFriendship
{
public:
static void SetAlgorithm(vtkExecutive* executive, vtkAlgorithm* algorithm)
{
executive->SetAlgorithm(algorithm);
}
};
//----------------------------------------------------------------------------
vtkAlgorithm::vtkAlgorithm()
{
this->AbortExecute = 0;
this->ErrorCode = 0;
this->Progress = 0.0;
this->ProgressText = NULL;
this->Executive = 0;
this->InputPortInformation = vtkInformationVector::New();
this->OutputPortInformation = vtkInformationVector::New();
this->AlgorithmInternal = new vtkAlgorithmInternals;
this->Information = vtkInformation::New();
this->Information->Register(this);
this->Information->Delete();
}
//----------------------------------------------------------------------------
vtkAlgorithm::~vtkAlgorithm()
{
this->SetInformation(0);
if(this->Executive)
{
this->Executive->UnRegister(this);
this->Executive = 0;
}
this->InputPortInformation->Delete();
this->OutputPortInformation->Delete();
delete this->AlgorithmInternal;
delete [] this->ProgressText;
this->ProgressText = NULL;
}
//----------------------------------------------------------------------------
// 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)
{
this->Progress = amount;
this->InvokeEvent(vtkCommand::ProgressEvent,static_cast<void *>(&amount));
}
//----------------------------------------------------------------------------
vtkInformation *vtkAlgorithm
::GetInputArrayFieldInformation(int idx, vtkInformationVector **inputVector)
{
// first get out association
vtkInformation *info = this->GetInputArrayInformation(idx);
// 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);
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);
}
//----------------------------------------------------------------------------
vtkInformation *vtkAlgorithm::GetInputArrayInformation(int idx)
{
// add this info into the algorithms info object
vtkInformationVector *inArrayVec =
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;
}
//----------------------------------------------------------------------------
void vtkAlgorithm::SetInputArrayToProcess(int idx, vtkInformation *inInfo)
{
vtkInformation *info = this->GetInputArrayInformation(idx);
info->Copy(inInfo,1);
this->Modified();
}
//----------------------------------------------------------------------------
void vtkAlgorithm::SetInputArrayToProcess(int idx, int port, int connection,
int fieldAssociation,
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
info->Remove(vtkDataObject::FIELD_NAME());
this->Modified();
}
//----------------------------------------------------------------------------
void vtkAlgorithm::SetInputArrayToProcess(
int idx, int port, int connection,
const char* fieldAssociation,
const char* fieldAttributeTypeOrName)
{
if (!fieldAssociation)
{
vtkErrorMacro("Association is requied");
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++)
{
if (strcmp(fieldAssociation,
vtkDataObject::GetAssociationTypeAsString(i)) == 0)
{
association = i;
break;
}
}
if (association == -1)
{
vtkErrorMacro("Unrecognized association type: " << fieldAssociation);
return;
}
int attributeType = -1;
for (i=0; i<vtkDataSetAttributes::NUM_ATTRIBUTES; i++)
{
if (strcmp(fieldAttributeTypeOrName,
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);
}
//----------------------------------------------------------------------------
void vtkAlgorithm::SetInputArrayToProcess(int idx, int port, int connection,
int fieldAssociation,
const char *name)
{
// ignore empty string
if (!name || name[0] == '\0')
{
return;
}
vtkInformation *info = this->GetInputArrayInformation(idx);
// remove fieldAttr if there is one
info->Remove(vtkDataObject::FIELD_ATTRIBUTE_TYPE());
// Check to see whether the current input array matches -
// 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;
}
info->Set(INPUT_PORT(), port);
info->Set(INPUT_CONNECTION(), connection);
info->Set(vtkDataObject::FIELD_ASSOCIATION(),fieldAssociation);
info->Set(vtkDataObject::FIELD_NAME(),name);
this->Modified();
}
//----------------------------------------------------------------------------
vtkDataArray *vtkAlgorithm::GetInputArrayToProcess(
int idx, vtkInformationVector **inputVector)
{
return vtkDataArray::SafeDownCast(
this->GetInputAbstractArrayToProcess(idx, inputVector));
}
//----------------------------------------------------------------------------
vtkDataArray *vtkAlgorithm::GetInputArrayToProcess(
int idx, int connection, vtkInformationVector **inputVector)
{
return vtkDataArray::SafeDownCast(this->GetInputAbstractArrayToProcess(
idx, connection, inputVector));
}
//----------------------------------------------------------------------------
vtkDataArray *vtkAlgorithm::GetInputArrayToProcess(
int idx, vtkDataObject* input)
{
return vtkDataArray::SafeDownCast(this->GetInputAbstractArrayToProcess(idx,
input));
}
//----------------------------------------------------------------------------
vtkAbstractArray *vtkAlgorithm::GetInputAbstractArrayToProcess(
int idx, vtkInformationVector **inputVector)
{
vtkInformationVector *inArrayVec =
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());
return this->GetInputAbstractArrayToProcess(idx, connection, inputVector);
}
//----------------------------------------------------------------------------
vtkAbstractArray* vtkAlgorithm::GetInputAbstractArrayToProcess(
int idx, int connection, vtkInformationVector **inputVector)
{
vtkInformationVector *inArrayVec =
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());
vtkInformation *inInfo = inputVector[port]->GetInformationObject(connection);
vtkDataObject *input = inInfo->Get(vtkDataObject::DATA_OBJECT());
return this->GetInputAbstractArrayToProcess(idx, input);
}
//----------------------------------------------------------------------------
vtkAbstractArray* vtkAlgorithm::GetInputAbstractArrayToProcess(
int idx, vtkDataObject* input)
{
if (!input)
{
return NULL;
}
vtkInformationVector *inArrayVec =
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());
if (inArrayInfo->Has(vtkDataObject::FIELD_NAME()))
{
const char *name = inArrayInfo->Get(vtkDataObject::FIELD_NAME());
if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_NONE)
{
vtkFieldData *fd = input->GetFieldData();
return fd->GetAbstractArray(name);
}
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);
}
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)
{
fd = inputG->GetVertexData();
}
else
{
fd = inputG->GetEdgeData();
}
return fd->GetAbstractArray(name);
}
if (vtkGraph::SafeDownCast(input) &&
fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS)
{
return vtkGraph::SafeDownCast(input)->
GetVertexData()->GetAbstractArray(name);
}
vtkDataSet *inputDS = vtkDataSet::SafeDownCast(input);
if (!inputDS)
{
vtkErrorMacro("Attempt to get point or cell data from a data object");
return NULL;
}
if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS)
{
return inputDS->GetPointData()->GetAbstractArray(name);
}
if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS
&& inputDS->GetPointData()->GetAbstractArray(name))
{
return inputDS->GetPointData()->GetAbstractArray(name);
}
return inputDS->GetCellData()->GetAbstractArray(name);
}
else
{
vtkDataSet *inputDS = vtkDataSet::SafeDownCast(input);
if (!inputDS)
{
vtkErrorMacro("Attempt to get point or cell data from a data object");
return NULL;
}
int fType = inArrayInfo->Get(vtkDataObject::FIELD_ATTRIBUTE_TYPE());
if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS)
{
return inputDS->GetPointData()->GetAbstractAttribute(fType);
}
if (fieldAssoc == vtkDataObject::FIELD_ASSOCIATION_POINTS_THEN_CELLS
&& inputDS->GetPointData()->GetAbstractAttribute(fType))
{
return inputDS->GetPointData()->GetAbstractAttribute(fType);
}
return inputDS->GetCellData()->GetAbstractAttribute(fType);
}
}
//----------------------------------------------------------------------------
void vtkAlgorithm::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
if(this->HasExecutive())
{
os << indent << "Executive: " << this->Executive << "\n";
}
else
{
os << indent << "Executive: (none)\n";
}
os << indent << "ErrorCode: " <<
vtkErrorCode::GetStringFromErrorCode(this->ErrorCode) << endl;
if ( this->Information )
{
os << indent << "Information: " << this->Information << "\n";
}
else
{
os << indent << "Information: (none)\n";
}
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";
}
}
//----------------------------------------------------------------------------
int vtkAlgorithm::HasExecutive()
{
return this->Executive ? 1 : 0;
}
//----------------------------------------------------------------------------
vtkExecutive* vtkAlgorithm::GetExecutive()
{
// Create the default executive if we do not have one already.
if(!this->HasExecutive())
{
vtkExecutive* e = this->CreateDefaultExecutive();
this->SetExecutive(e);
e->Delete();
}
return this->Executive;
}
//----------------------------------------------------------------------------
void vtkAlgorithm::SetExecutive(vtkExecutive* newExecutive)
{
vtkExecutive* oldExecutive = this->Executive;
if(newExecutive != oldExecutive)
{
if(newExecutive)
{
newExecutive->Register(this);
vtkAlgorithmToExecutiveFriendship::SetAlgorithm(newExecutive, this);
}
this->Executive = newExecutive;
if(oldExecutive)
{
vtkAlgorithmToExecutiveFriendship::SetAlgorithm(oldExecutive, 0);
oldExecutive->UnRegister(this);
}
}
}
//----------------------------------------------------------------------------
int vtkAlgorithm::ProcessRequest(vtkInformation* /* request */,
vtkInformationVector**,
vtkInformationVector*)
{
return 1;
}
//----------------------------------------------------------------------------
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;
}
//----------------------------------------------------------------------------
int vtkAlgorithm::ModifyRequest(vtkInformation* /*request*/, int /*when*/)
{
return 1;
}
//----------------------------------------------------------------------------
int vtkAlgorithm::GetNumberOfInputPorts()
{
return this->InputPortInformation->GetNumberOfInformationObjects();
}
//----------------------------------------------------------------------------
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)
{
this->SetNumberOfInputConnections(i, 0);
}
// Set the number of input port information objects.
this->InputPortInformation->SetNumberOfInformationObjects(n);
}
//----------------------------------------------------------------------------
int vtkAlgorithm::GetNumberOfOutputPorts()
{
return this->OutputPortInformation->GetNumberOfInformationObjects();
}
//----------------------------------------------------------------------------
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)
{
// 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.
vtkExecutive** consumers = vtkExecutive::CONSUMERS()->GetExecutives(info);
int* consumerPorts = vtkExecutive::CONSUMERS()->GetPorts(info);
int consumerCount = vtkExecutive::CONSUMERS()->Length(info);
for(int j=0; j < consumerCount; ++j)
{
vtkInformationVector* inputs =
consumers[j]->GetInputInformation(consumerPorts[j]);
inputs->Remove(info);
}
// Remove this producer's references to all consumers on this port.
vtkExecutive::CONSUMERS()->Remove(info);
}
// Set the number of output port information objects.
this->OutputPortInformation->SetNumberOfInformationObjects(n);
// Set the number of connection proxy objects.
this->AlgorithmInternal->Outputs.resize(n);
}
//----------------------------------------------------------------------------
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();
}
}
// Return ths information object.
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();
}
}
// Return ths information object.
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)
{
// Make sure the index of the output port is in range.
if(index < 0 || index >= this->GetNumberOfOutputPorts())
{
vtkErrorMacro("Attempt to " << (action?action:"access")
<< " output port index " << index
<< " for an algorithm with "
<< this->GetNumberOfOutputPorts() << " output ports.");
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
void vtkAlgorithm::SetDefaultExecutivePrototype(vtkExecutive* proto)
{
if (vtkAlgorithm::DefaultExecutivePrototype == proto)
{
return;
}
if (vtkAlgorithm::DefaultExecutivePrototype)
{
vtkAlgorithm::DefaultExecutivePrototype->UnRegister(0);
vtkAlgorithm::DefaultExecutivePrototype = 0;
}
if (proto)
{
proto->Register(0);
}
vtkAlgorithm::DefaultExecutivePrototype = proto;
}
//----------------------------------------------------------------------------
vtkExecutive* vtkAlgorithm::CreateDefaultExecutive()
{
if (vtkAlgorithm::DefaultExecutivePrototype)
{
return vtkAlgorithm::DefaultExecutivePrototype->NewInstance();
}
return vtkStreamingDemandDrivenPipeline::New();
}
//----------------------------------------------------------------------------
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)
{
return this->GetExecutive()->GetOutputData(port);
}
//----------------------------------------------------------------------------
vtkDataObject *vtkAlgorithm::GetInputDataObject(int port,
int connection)
{
return this->GetExecutive()->GetInputData(port,connection);
}
//----------------------------------------------------------------------------
void vtkAlgorithm::RemoveAllInputs()
{
this->SetInputConnection(0, 0);
}
//----------------------------------------------------------------------------
void vtkAlgorithm::SetInputConnection(vtkAlgorithmOutput* input)
{
this->SetInputConnection(0,input);
}
//----------------------------------------------------------------------------
void vtkAlgorithm::SetInputConnection(int port, vtkAlgorithmOutput* input)
{
if(!this->InputPortIndexInRange(port, "connect"))
{
return;
}
// Get the producer/consumer pair for the connection.
vtkExecutive* producer =
(input && input->GetProducer())? input->GetProducer()->GetExecutive() : 0;
int producerPort = producer? input->GetIndex() : 0;
vtkExecutive* consumer = this->GetExecutive();
int consumerPort = port;
// Get the vector of connected input information objects.
vtkInformationVector* inputs = consumer->GetInputInformation(consumerPort);
// Get the information object from the producer of the new input.
vtkInformation* newInfo =
producer? producer->GetOutputInformation(producerPort) : 0;
// Check if the connection is already present.
if(!newInfo && inputs->GetNumberOfInformationObjects() == 0)
{
return;
}
else if(newInfo == inputs->GetInformationObject(0) &&
inputs->GetNumberOfInformationObjects() == 1)
{
return;
}
// The connection is not present.
vtkDebugMacro("Setting connection to input port index " << consumerPort
<< " from output port index " << producerPort
<< " on algorithm "
<< (producer? producer->GetAlgorithm()->GetClassName() : "")
<< "(" << (producer? producer->GetAlgorithm() : 0) << ").");
// Add this consumer to the new input's list of consumers.
if(newInfo)
{
vtkExecutive::CONSUMERS()->Append(newInfo, consumer, consumerPort);
}
// Remove this consumer from all old inputs' lists of consumers.
for(int i=0; i < inputs->GetNumberOfInformationObjects(); ++i)
{
if(vtkInformation* oldInfo = inputs->GetInformationObject(i))
{
vtkExecutive::CONSUMERS()->Remove(oldInfo, consumer, consumerPort);
}
}
// Make the new input the only connection.
if(newInfo)
{
inputs->SetInformationObject(0, newInfo);
inputs->SetNumberOfInformationObjects(1);
}
else
{
inputs->SetNumberOfInformationObjects(0);
}
// This algorithm has been modified.
this->Modified();
}
//----------------------------------------------------------------------------
void vtkAlgorithm::AddInputConnection(vtkAlgorithmOutput* input)
{
this->AddInputConnection(0, input);
}
void vtkAlgorithm::AddInputConnection(int port, vtkAlgorithmOutput* input)
{
if(!this->InputPortIndexInRange(port, "connect"))
{
return;
}
// If there is no input do nothing.
if(!input || !input->GetProducer())
{
return;
}
// Get the producer/consumer pair for the connection.
vtkExecutive* producer = input->GetProducer()->GetExecutive();
int producerPort = input->GetIndex();
vtkExecutive* consumer = this->GetExecutive();
int consumerPort = port;
// Get the vector of connected input information objects.
vtkInformationVector* inputs = consumer->GetInputInformation(consumerPort);
// Add the new connection.
vtkDebugMacro("Adding connection to input port index " << consumerPort
<< " from output port index " << producerPort
<< " on algorithm "
<< (producer? producer->GetAlgorithm()->GetClassName() : "")
<< "(" << (producer? producer->GetAlgorithm() : 0) << ").");
// Get the information object from the producer of the new input.
vtkInformation* newInfo = producer->GetOutputInformation(producerPort);
// Add this consumer to the input's list of consumers.
vtkExecutive::CONSUMERS()->Append(newInfo, consumer, consumerPort);
// Add the information object to the list of inputs.
inputs->Append(newInfo);
// This algorithm has been modified.
this->Modified();
}
//----------------------------------------------------------------------------
void vtkAlgorithm::RemoveInputConnection(int port, vtkAlgorithmOutput* input)
{
if(!this->InputPortIndexInRange(port, "disconnect"))
{
return;
}
// If there is no input do nothing.
if(!input || !input->GetProducer())
{
return;
}
// Get the producer/consumer pair for the connection.
vtkExecutive* producer = input->GetProducer()->GetExecutive();
int producerPort = input->GetIndex();
vtkExecutive* consumer = this->GetExecutive();
int consumerPort = port;
// Get the vector of connected input information objects.
vtkInformationVector* inputs = consumer->GetInputInformation(consumerPort);
// Remove the connection.
vtkDebugMacro("Removing connection to input port index " << consumerPort
<< " from output port index " << producerPort
<< " on algorithm "
<< (producer? producer->GetAlgorithm()->GetClassName() : "")
<< "(" << (producer? producer->GetAlgorithm() : 0) << ").");
// Get the information object from the producer of the old input.
vtkInformation* oldInfo = producer->GetOutputInformation(producerPort);
// Remove this consumer from the old input's list of consumers.
vtkExecutive::CONSUMERS()->Remove(oldInfo, consumer, consumerPort);
// Remove the information object from the list of inputs.
inputs->Remove(oldInfo);
// This algorithm has been modified.
this->Modified();
}
//----------------------------------------------------------------------------