forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkDemandDrivenPipeline.cxx
1121 lines (1013 loc) · 35.8 KB
/
vtkDemandDrivenPipeline.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: vtkDemandDrivenPipeline.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 "vtkDemandDrivenPipeline.h"
#include "vtkAlgorithm.h"
#include "vtkAlgorithmOutput.h"
#include "vtkCellData.h"
#include "vtkCommand.h"
#include "vtkDataArray.h"
#include "vtkDataObject.h"
#include "vtkDataObjectTypes.h"
#include "vtkDataSet.h"
#include "vtkGarbageCollector.h"
#include "vtkInformation.h"
#include "vtkInformationExecutivePortKey.h"
#include "vtkInformationExecutivePortVectorKey.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationKeyVectorKey.h"
#include "vtkInformationRequestKey.h"
#include "vtkInformationUnsignedLongKey.h"
#include "vtkInformationVector.h"
#include "vtkInstantiator.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include <vtkstd/vector>
vtkStandardNewMacro(vtkDemandDrivenPipeline);
vtkInformationKeyMacro(vtkDemandDrivenPipeline, DATA_NOT_GENERATED, Integer);
vtkInformationKeyMacro(vtkDemandDrivenPipeline, RELEASE_DATA, Integer);
vtkInformationKeyMacro(vtkDemandDrivenPipeline, REQUEST_DATA, Request);
vtkInformationKeyMacro(vtkDemandDrivenPipeline, REQUEST_DATA_NOT_GENERATED, Request);
vtkInformationKeyMacro(vtkDemandDrivenPipeline, REQUEST_DATA_OBJECT, Request);
vtkInformationKeyMacro(vtkDemandDrivenPipeline, REQUEST_INFORMATION, Request);
vtkInformationKeyMacro(vtkDemandDrivenPipeline, REQUEST_REGENERATE_INFORMATION, Integer);
//----------------------------------------------------------------------------
vtkDemandDrivenPipeline::vtkDemandDrivenPipeline()
{
this->InfoRequest = 0;
this->DataObjectRequest = 0;
this->DataRequest = 0;
this->PipelineMTime = 0;
}
//----------------------------------------------------------------------------
vtkDemandDrivenPipeline::~vtkDemandDrivenPipeline()
{
if (this->InfoRequest)
{
this->InfoRequest->Delete();
}
if (this->DataObjectRequest)
{
this->DataObjectRequest->Delete();
}
if (this->DataRequest)
{
this->DataRequest->Delete();
}
}
//----------------------------------------------------------------------------
void vtkDemandDrivenPipeline::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "PipelineMTime: " << this->PipelineMTime << "\n";
}
//----------------------------------------------------------------------------
int
vtkDemandDrivenPipeline::ComputePipelineMTime(vtkInformation* request,
vtkInformationVector** inInfoVec,
vtkInformationVector* outInfoVec,
int requestFromOutputPort,
unsigned long* mtime)
{
// The pipeline's MTime starts with this algorithm's MTime.
// Invoke the request on the algorithm.
this->InAlgorithm = 1;
int result =
this->Algorithm->ComputePipelineMTime(request,
inInfoVec, outInfoVec,
requestFromOutputPort,
&this->PipelineMTime);
this->InAlgorithm = 0;
// If the algorithm failed report it now.
if(!result)
{
if(request)
{
vtkErrorMacro("Algorithm " << this->Algorithm->GetClassName()
<< "(" << this->Algorithm
<< ") returned failure for pipeline"
<< " modified time request from output port "
<< requestFromOutputPort<< ": " << *request);
}
else
{
vtkErrorMacro("Algorithm " << this->Algorithm->GetClassName()
<< "(" << this->Algorithm
<< ") returned failure for pipeline"
<< " modified time request from output port "
<< requestFromOutputPort<< ".");
}
return 0;
}
// Forward the request upstream if not sharing input information.
if(!this->SharedInputInformation)
{
// We want the maximum PipelineMTime of all inputs.
for(int i=0; i < this->Algorithm->GetNumberOfInputPorts(); ++i)
{
for(int j=0; j < inInfoVec[i]->GetNumberOfInformationObjects(); ++j)
{
vtkInformation* info = inInfoVec[i]->GetInformationObject(j);
// call ComputePipelineMTime on the input
vtkExecutive* e;
int producerPort;
vtkExecutive::PRODUCER()->Get(info,e,producerPort);
if(e)
{
unsigned long pmtime;
if(!e->ComputePipelineMTime(request,
e->GetInputInformation(),
e->GetOutputInformation(),
producerPort, &pmtime))
{
return 0;
}
if(pmtime > this->PipelineMTime)
{
this->PipelineMTime = pmtime;
}
}
}
}
}
*mtime = this->PipelineMTime;
return 1;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::ProcessRequest(vtkInformation* request,
vtkInformationVector** inInfoVec,
vtkInformationVector* outInfoVec)
{
// The algorithm should not invoke anything on the executive.
if(!this->CheckAlgorithm("ProcessRequest", request))
{
return 0;
}
if(this->Algorithm && request->Has(REQUEST_DATA_OBJECT()))
{
// if we are up to date then short circuit
if (this->PipelineMTime < this->DataObjectTime.GetMTime()
&& ! request->Has(REQUEST_REGENERATE_INFORMATION()))
{
return 1;
}
// Update inputs first if they are out of date
if(!this->ForwardUpstream(request))
{
return 0;
}
// Make sure our output data type is up-to-date.
int result = 1;
if(this->PipelineMTime > this->DataObjectTime.GetMTime()
|| request->Has(REQUEST_REGENERATE_INFORMATION()))
{
// Request data type from the algorithm.
result = this->ExecuteDataObject(request,inInfoVec,outInfoVec);
// Make sure the data object exists for all output ports.
for(int i=0;
result && i < outInfoVec->GetNumberOfInformationObjects(); ++i)
{
vtkInformation* info = outInfoVec->GetInformationObject(i);
if(!info->Get(vtkDataObject::DATA_OBJECT()))
{
result = 0;
}
}
if(result)
{
// Data object is now up to date.
this->DataObjectTime.Modified();
}
}
return result;
}
if(this->Algorithm && request->Has(REQUEST_INFORMATION()))
{
// if we are up to date then short circuit
if (this->PipelineMTime < this->InformationTime.GetMTime()
&& ! request->Has(REQUEST_REGENERATE_INFORMATION()))
{
return 1;
}
// Update inputs first.
if(!this->ForwardUpstream(request))
{
return 0;
}
// Make sure our output information is up-to-date.
int result = 1;
if(this->PipelineMTime > this->InformationTime.GetMTime()
|| request->Has(REQUEST_REGENERATE_INFORMATION()))
{
// Make sure input types are valid before algorithm does anything.
if(!this->InputCountIsValid(inInfoVec) ||
!this->InputTypeIsValid(inInfoVec))
{
return 0;
}
// Request information from the algorithm.
result = this->ExecuteInformation(request,inInfoVec,outInfoVec);
// Information is now up to date.
this->InformationTime.Modified();
}
return result;
}
if(this->Algorithm && request->Has(REQUEST_DATA()))
{
// Get the output port from which the request was made.
int outputPort = -1;
if(request->Has(FROM_OUTPUT_PORT()))
{
outputPort = request->Get(FROM_OUTPUT_PORT());
}
// Make sure our outputs are up-to-date.
int result = 1;
if(this->NeedToExecuteData(outputPort,inInfoVec,outInfoVec))
{
// Update inputs first.
if(!this->ForwardUpstream(request))
{
return 0;
}
// Make sure inputs are valid before algorithm does anything.
if(!this->InputCountIsValid(inInfoVec) ||
!this->InputTypeIsValid(inInfoVec) ||
!this->InputFieldsAreValid(inInfoVec))
{
return 0;
}
// Request data from the algorithm.
result = this->ExecuteData(request,inInfoVec,outInfoVec);
// Data are now up to date.
this->DataTime.Modified();
// Some filters may modify themselves while processing
// REQUEST_DATA. Since we mark the filter execution end time
// here this behavior does not cause re-execution, so it should
// be allowed. The filter is now considered up-to-date.
// However, we must prevent the REQUEST_DATA_OBJECT and
// REQUEST_INFORMATION passes from re-running, so mark them
// up-do-date also. It is up to the filter to not modify itself
// in a way that would change the result of any pass.
this->InformationTime.Modified();
this->DataObjectTime.Modified();
}
return result;
}
// Let the superclass handle other requests.
return this->Superclass::ProcessRequest(request, inInfoVec, outInfoVec);
}
//----------------------------------------------------------------------------
void vtkDemandDrivenPipeline::ResetPipelineInformation(int,
vtkInformation* info)
{
info->Remove(RELEASE_DATA());
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::Update()
{
return this->Superclass::Update();
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::Update(int port)
{
if(!this->UpdateInformation())
{
return 0;
}
if(port >= -1 && port < this->Algorithm->GetNumberOfOutputPorts())
{
return this->UpdateData(port);
}
else
{
return 1;
}
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::UpdatePipelineMTime()
{
// The algorithm should not invoke anything on the executive.
if(!this->CheckAlgorithm("UpdatePipelineMTime", 0))
{
return 0;
}
// Send the request for pipeline modified time.
unsigned long mtime;
this->ComputePipelineMTime(0,
this->GetInputInformation(),
this->GetOutputInformation(),
-1, &mtime);
return 1;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::UpdateDataObject()
{
// The algorithm should not invoke anything on the executive.
if(!this->CheckAlgorithm("UpdateDataObject", 0))
{
return 0;
}
// Update the pipeline mtime first.
if(!this->UpdatePipelineMTime())
{
return 0;
}
// Setup the request for data object creation.
if (!this->DataObjectRequest)
{
this->DataObjectRequest = vtkInformation::New();
this->DataObjectRequest->Set(REQUEST_DATA_OBJECT());
// The request is forwarded upstream through the pipeline.
this->DataObjectRequest->Set(vtkExecutive::FORWARD_DIRECTION(), vtkExecutive::RequestUpstream);
// Algorithms process this request after it is forwarded.
this->DataObjectRequest->Set(vtkExecutive::ALGORITHM_AFTER_FORWARD(), 1);
}
// Send the request.
return this->ProcessRequest(this->DataObjectRequest,
this->GetInputInformation(),
this->GetOutputInformation());
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::UpdateInformation()
{
// The algorithm should not invoke anything on the executive.
if(!this->CheckAlgorithm("UpdateInformation", 0))
{
return 0;
}
// Do the data-object creation pass before the information pass.
if(!this->UpdateDataObject())
{
return 0;
}
// Setup the request for information.
if (!this->InfoRequest)
{
this->InfoRequest = vtkInformation::New();
this->InfoRequest->Set(REQUEST_INFORMATION());
// The request is forwarded upstream through the pipeline.
this->InfoRequest->Set(vtkExecutive::FORWARD_DIRECTION(), vtkExecutive::RequestUpstream);
// Algorithms process this request after it is forwarded.
this->InfoRequest->Set(vtkExecutive::ALGORITHM_AFTER_FORWARD(), 1);
}
// Send the request.
return this->ProcessRequest(this->InfoRequest,
this->GetInputInformation(),
this->GetOutputInformation());
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::UpdateData(int outputPort)
{
// The algorithm should not invoke anything on the executive.
if(!this->CheckAlgorithm("UpdateData", 0))
{
return 0;
}
// Range check.
if(outputPort < -1 ||
outputPort >= this->Algorithm->GetNumberOfOutputPorts())
{
vtkErrorMacro("UpdateData given output port index "
<< outputPort << " on an algorithm with "
<< this->Algorithm->GetNumberOfOutputPorts()
<< " output ports.");
return 0;
}
// Setup the request for data.
if (!this->DataRequest)
{
this->DataRequest = vtkInformation::New();
this->DataRequest->Set(REQUEST_DATA());
// The request is forwarded upstream through the pipeline.
this->DataRequest->Set(vtkExecutive::FORWARD_DIRECTION(), vtkExecutive::RequestUpstream);
// Algorithms process this request after it is forwarded.
this->DataRequest->Set(vtkExecutive::ALGORITHM_AFTER_FORWARD(), 1);
}
// Send the request.
this->DataRequest->Set(FROM_OUTPUT_PORT(), outputPort);
return this->ProcessRequest(this->DataRequest,
this->GetInputInformation(),
this->GetOutputInformation());
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::ExecuteDataObject(vtkInformation* request,
vtkInformationVector** inInfo,
vtkInformationVector* outInfo)
{
// Invoke the request on the algorithm.
int result = this->CallAlgorithm(request, vtkExecutive::RequestDownstream,
inInfo, outInfo);
// Make sure a valid data object exists for all output ports.
for(int i=0; result && i < this->Algorithm->GetNumberOfOutputPorts(); ++i)
{
result = this->CheckDataObject(i, outInfo);
}
return result;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::ExecuteInformation
(vtkInformation* request,
vtkInformationVector** inInfoVec,
vtkInformationVector* outInfoVec)
{
// Give each output data object a chance to set default values in
// its pipeline information. Provide the first input's information
// to each output.
vtkInformation* inInfo = 0;
if(this->GetNumberOfInputPorts() > 0)
{
inInfo = inInfoVec[0]->GetInformationObject(0);
}
for(int i=0; i < this->Algorithm->GetNumberOfOutputPorts(); ++i)
{
vtkInformation* outInfo = outInfoVec->GetInformationObject(i);
if(vtkDataObject* outData = outInfo->Get(vtkDataObject::DATA_OBJECT()))
{
outData->CopyInformationToPipeline(request, inInfo);
}
}
// Invoke the request on the algorithm.
return this->CallAlgorithm(request, vtkExecutive::RequestDownstream,
inInfoVec, outInfoVec);
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::ExecuteData(vtkInformation* request,
vtkInformationVector** inInfo,
vtkInformationVector* outInfo)
{
this->ExecuteDataStart(request, inInfo, outInfo);
// Invoke the request on the algorithm.
// unsigned long mTimeBefore = this->Algorithm->GetMTime();
int result = this->CallAlgorithm(request, vtkExecutive::RequestDownstream,
inInfo, outInfo);
// if (mTimeBefore != this->Algorithm->GetMTime())
// {
// vtkWarningMacro(<< this->Algorithm->GetClassName()
// << " modified it's MTime during RequestData(). "
// << "This may lead to unnecessary pipeline "
// << "executions");
// }
this->ExecuteDataEnd(request, inInfo, outInfo);
return result;
}
//----------------------------------------------------------------------------
void vtkDemandDrivenPipeline::ExecuteDataStart(vtkInformation* request,
vtkInformationVector** inInfo,
vtkInformationVector* outputs)
{
int i;
// Ask the algorithm to mark outputs that it will not generate.
request->Remove(REQUEST_DATA());
request->Set(REQUEST_DATA_NOT_GENERATED());
this->CallAlgorithm(request, vtkExecutive::RequestDownstream,
inInfo, outputs);
request->Remove(REQUEST_DATA_NOT_GENERATED());
request->Set(REQUEST_DATA());
// Prepare outputs that will be generated to receive new data.
for(i=0; i < outputs->GetNumberOfInformationObjects(); ++i)
{
vtkInformation* outInfo = outputs->GetInformationObject(i);
vtkDataObject* data = outInfo->Get(vtkDataObject::DATA_OBJECT());
if(data && !outInfo->Get(DATA_NOT_GENERATED()))
{
data->PrepareForNewData();
data->CopyInformationFromPipeline(request);
}
}
// Pass the vtkDataObject's field data from the first input to all
// outputs.
if (this->GetNumberOfInputPorts() > 0)
{
vtkDataObject* input = this->GetInputData(0, 0);
if (input && input->GetFieldData())
{
for(i=0; i < outputs->GetNumberOfInformationObjects(); ++i)
{
vtkInformation* outInfo = outputs->GetInformationObject(i);
vtkDataObject* output = outInfo->Get(vtkDataObject::DATA_OBJECT());
if(output)
{
output->GetFieldData()->PassData(input->GetFieldData());
}
}
}
}
// Tell observers the algorithm is about to execute.
this->Algorithm->InvokeEvent(vtkCommand::StartEvent,NULL);
// The algorithm has not yet made any progress.
this->Algorithm->SetAbortExecute(0);
this->Algorithm->UpdateProgress(0.0);
}
//----------------------------------------------------------------------------
void vtkDemandDrivenPipeline::ExecuteDataEnd(vtkInformation* request,
vtkInformationVector** inInfoVec,
vtkInformationVector* outputs)
{
// The algorithm has either finished or aborted.
if(!this->Algorithm->GetAbortExecute())
{
this->Algorithm->UpdateProgress(1.0);
}
// Tell observers the algorithm is done executing.
this->Algorithm->InvokeEvent(vtkCommand::EndEvent,NULL);
// Tell outputs they have been generated.
this->MarkOutputsGenerated(request,inInfoVec,outputs);
// Remove any not-generated mark.
int i, j;
for(i=0; i < outputs->GetNumberOfInformationObjects(); ++i)
{
vtkInformation* outInfo = outputs->GetInformationObject(i);
outInfo->Remove(DATA_NOT_GENERATED());
}
// Release input data if requested.
for(i=0; i < this->Algorithm->GetNumberOfInputPorts(); ++i)
{
for(j=0; j < inInfoVec[i]->GetNumberOfInformationObjects(); ++j)
{
vtkInformation* inInfo = inInfoVec[i]->GetInformationObject(j);
vtkDataObject* dataObject = inInfo->Get(vtkDataObject::DATA_OBJECT());
if(dataObject && (dataObject->GetGlobalReleaseDataFlag() ||
inInfo->Get(RELEASE_DATA())))
{
dataObject->ReleaseData();
}
}
}
}
//----------------------------------------------------------------------------
void vtkDemandDrivenPipeline::MarkOutputsGenerated
(vtkInformation*,
vtkInformationVector** /* inInfoVec */,
vtkInformationVector* outputs)
{
// Tell all generated outputs that they have been generated.
for(int i=0; i < outputs->GetNumberOfInformationObjects(); ++i)
{
vtkInformation* outInfo = outputs->GetInformationObject(i);
vtkDataObject* data = outInfo->Get(vtkDataObject::DATA_OBJECT());
if(data && !outInfo->Get(DATA_NOT_GENERATED()))
{
data->DataHasBeenGenerated();
}
}
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::CheckDataObject(int port,
vtkInformationVector* outInfoVec)
{
// Check that the given output port has a valid data object.
vtkInformation* outInfo = outInfoVec->GetInformationObject(port);
vtkDataObject* data = outInfo->Get(vtkDataObject::DATA_OBJECT());
vtkInformation* portInfo = this->Algorithm->GetOutputPortInformation(port);
if(const char* dt = portInfo->Get(vtkDataObject::DATA_TYPE_NAME()))
{
int incorrectdata = data && (!data->IsA(dt) ||
(!strcmp(data->GetClassName(),"vtkTemporalDataSet") &&
strcmp(dt,"vtkTemporalDataSet")) );
// The output port specifies a data type. Make sure the data
// object exists and is of the right type.
if(!data || incorrectdata)
{
if (data)
{
vtkDebugMacro(<< "CHECKDATAOBJECT Replacing " << data->GetClassName());
}
// Try to create an instance of the correct type.
data = vtkDataObjectTypes::NewDataObject(dt);
this->SetOutputData(port, data, outInfo);
if(data)
{
vtkDebugMacro(<< "CHECKDATAOBJECT Created " << dt);
data->FastDelete();
}
}
if(!data)
{
// The algorithm has a bug and did not create the data object.
vtkErrorMacro("Algorithm " << this->Algorithm->GetClassName() << "("
<< this->Algorithm
<< ") did not create output for port " << port
<< " when asked by REQUEST_DATA_OBJECT and does not"
<< " specify a concrete DATA_TYPE_NAME.");
return 0;
}
return 1;
}
else if(data)
{
// The algorithm did not specify its output data type. Just assume
// the data object is of the correct type.
return 1;
}
else
{
// The algorithm did not specify its output data type and no
// object exists.
vtkErrorMacro("Algorithm " << this->Algorithm->GetClassName() << "("
<< this->Algorithm
<< ") did not create output for port " << port
<< " when asked by REQUEST_DATA_OBJECT and does not"
<< " specify any DATA_TYPE_NAME.");
return 0;
}
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputCountIsValid
(vtkInformationVector **inInfoVec)
{
// Check the number of connections for each port.
int result = 1;
for(int p=0; p < this->Algorithm->GetNumberOfInputPorts(); ++p)
{
if(!this->InputCountIsValid(p,inInfoVec))
{
result = 0;
}
}
return result;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputCountIsValid
(int port, vtkInformationVector **inInfoVec)
{
// Get the number of connections for this port.
if (!inInfoVec[port])
{
return 0;
}
int connections = inInfoVec[port]->GetNumberOfInformationObjects();
// If the input port is optional, there may be less than one connection.
if(!this->InputIsOptional(port) && (connections < 1))
{
vtkErrorMacro("Input port " << port << " of algorithm "
<< this->Algorithm->GetClassName()
<< "(" << this->Algorithm << ") has " << connections
<< " connections but is not optional.");
return 0;
}
// If the input port is repeatable, there may be more than one connection.
if(!this->InputIsRepeatable(port) && (connections > 1))
{
vtkErrorMacro("Input port " << port << " of algorithm "
<< this->Algorithm->GetClassName()
<< "(" << this->Algorithm << ") has " << connections
<< " connections but is not repeatable.");
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputTypeIsValid
(vtkInformationVector **inInfoVec)
{
// Check the connection types for each port.
int result = 1;
for(int p=0; p < this->Algorithm->GetNumberOfInputPorts(); ++p)
{
if(!this->InputTypeIsValid(p, inInfoVec))
{
result = 0;
}
}
return result;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputTypeIsValid
(int port, vtkInformationVector **inInfoVec)
{
// Check the type of each connection on this port.
int result = 1;
if (!inInfoVec[port])
{
return 0;
}
for(int i=0; i < inInfoVec[port]->GetNumberOfInformationObjects(); ++i)
{
if(!this->InputTypeIsValid(port, i, inInfoVec))
{
result = 0;
}
}
return result;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputTypeIsValid
(int port, int index, vtkInformationVector **inInfoVec)
{
if (!inInfoVec[port])
{
return 0;
}
vtkInformation* info = this->Algorithm->GetInputPortInformation(port);
vtkDataObject* input = this->GetInputData(port, index, inInfoVec);
// Enforce required type, if any.
if(info->Has(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE())
&& info->Length(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE()) > 0)
{
// The input cannot be NULL unless the port is optional.
if(!input && !info->Get(vtkAlgorithm::INPUT_IS_OPTIONAL()))
{
vtkErrorMacro("Input for connection index " << index
<< " on input port index " << port
<< " for algorithm " << this->Algorithm->GetClassName()
<< "(" << this->Algorithm << ") is NULL, but a "
<< info->Get(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), 0)
<< " is required.");
return 0;
}
// The input must be one of the required types or NULL.
bool foundMatch = false;
if(input)
{
int size = info->Length(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE());
for(int i = 0; i < size; ++i)
{
if(input->IsA(info->Get(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), i)))
{
foundMatch = true;
}
}
}
if(input && !foundMatch)
{
vtkErrorMacro("Input for connection index " << index
<< " on input port index " << port
<< " for algorithm " << this->Algorithm->GetClassName()
<< "(" << this->Algorithm << ") is of type "
<< input->GetClassName() << ", but a "
<< info->Get(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), 0)
<< " is required.");
return 0;
}
}
return 1;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputFieldsAreValid
(vtkInformationVector **inInfoVec)
{
// Check the fields for each port.
int result = 1;
for(int p=0; p < this->Algorithm->GetNumberOfInputPorts(); ++p)
{
if(!this->InputFieldsAreValid(p,inInfoVec))
{
result = 0;
}
}
return result;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputFieldsAreValid
(int port, vtkInformationVector **inInfoVec)
{
// Check the fields for each connection on this port.
if (!inInfoVec[port])
{
return 0;
}
int result = 1;
for(int i=0; i < inInfoVec[port]->GetNumberOfInformationObjects(); ++i)
{
if(!this->InputFieldsAreValid(port, i, inInfoVec))
{
result = 0;
}
}
return result;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputFieldsAreValid
(int port, int index, vtkInformationVector **inInfoVec)
{
vtkInformation* info = this->Algorithm->GetInputPortInformation(port);
vtkInformationVector* fields =
info->Get(vtkAlgorithm::INPUT_REQUIRED_FIELDS());
// If there are no required fields, there is nothing to check.
if(!fields)
{
return 1;
}
vtkDataObject* input = this->GetInputData(port, index, inInfoVec);
// NULL inputs do not have to have the proper fields.
if(!input)
{
return 1;
}
// Check availability of each required field.
int result = 1;
for(int i=0; i < fields->GetNumberOfInformationObjects(); ++i)
{
vtkInformation* field = fields->GetInformationObject(i);
// Decide which kinds of fields to check.
int checkPoints = 1;
int checkCells = 1;
int checkFields = 1;
if(field->Has(vtkDataObject::FIELD_ASSOCIATION()))
{
switch(field->Get(vtkDataObject::FIELD_ASSOCIATION()))
{
case vtkDataObject::FIELD_ASSOCIATION_POINTS:
checkCells = 0; checkFields = 0; break;
case vtkDataObject::FIELD_ASSOCIATION_CELLS:
checkPoints = 0; checkFields = 0; break;
case vtkDataObject::FIELD_ASSOCIATION_NONE:
checkPoints = 0; checkCells = 0; break;
}
}
// Point and cell data arrays only exist in vtkDataSet instances.
vtkDataSet* dataSet = vtkDataSet::SafeDownCast(input);
// Look for a point data, cell data, or field data array matching
// the requirements.
if(!(checkPoints && dataSet && dataSet->GetPointData() &&
this->DataSetAttributeExists(dataSet->GetPointData(), field)) &&
!(checkCells && dataSet && dataSet->GetCellData() &&
this->DataSetAttributeExists(dataSet->GetCellData(), field)) &&
!(checkFields && input && input->GetFieldData() &&
this->FieldArrayExists(input->GetFieldData(), field)))
{
/* TODO: Construct more descriptive error message from field
requirements. */
vtkErrorMacro("Required field not found in input.");
result = 0;
}
}
return result;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::DataSetAttributeExists(vtkDataSetAttributes* dsa,
vtkInformation* field)
{
if(field->Has(vtkDataObject::FIELD_ATTRIBUTE_TYPE()))
{
// A specific attribute must match the requirements.
int attrType = field->Get(vtkDataObject::FIELD_ATTRIBUTE_TYPE());
return this->ArrayIsValid(dsa->GetAbstractAttribute(attrType), field);
}
else
{
// Search for an array matching the requirements.
return this->FieldArrayExists(dsa, field);
}
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::FieldArrayExists(vtkFieldData* data,
vtkInformation* field)
{
// Search the field data instance for an array matching the requirements.
for(int a=0; a < data->GetNumberOfArrays(); ++a)
{
if(this->ArrayIsValid(data->GetArray(a), field))
{
return 1;
}
}
return 0;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::ArrayIsValid(vtkAbstractArray* array,
vtkInformation* field)
{
// Enforce existence of the array.
if(!array)
{
return 0;
}
// Enforce name of the array. This should really only be used for
// field data (not point or cell data).
if(const char* name = field->Get(vtkDataObject::FIELD_NAME()))
{
if(!array->GetName() || (strcmp(name, array->GetName()) != 0))
{
return 0;
}
}
// Enforce component type for the array.
if(field->Has(vtkDataObject::FIELD_ARRAY_TYPE()))
{
int arrayType = field->Get(vtkDataObject::FIELD_ARRAY_TYPE());
if(array->GetDataType() != arrayType)
{
return 0;
}
}
// Enforce number of components for the array.
if(field->Has(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS()))