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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
|
/*=========================================================================
Program: Visualization Toolkit
Module: $RCSfile: vtkDemandDrivenPipeline.cxx,v $
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 "vtkDataObject.h"
#include "vtkDataSet.h"
#include "vtkGarbageCollector.h"
#include "vtkHierarchicalBoxDataSet.h"
#include "vtkHierarchicalDataSet.h"
#include "vtkInformation.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 "vtkImageData.h"
#include "vtkPolyData.h"
#include "vtkRectilinearGrid.h"
#include "vtkStructuredGrid.h"
#include "vtkStructuredPoints.h"
#include "vtkUnstructuredGrid.h"
#include <vtkstd/vector>
vtkCxxRevisionMacro(vtkDemandDrivenPipeline, "$Revision: 1.37.4.5 $");
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);
//----------------------------------------------------------------------------
vtkDemandDrivenPipeline::vtkDemandDrivenPipeline()
{
this->InfoRequest = 0;
this->DataObjectRequest = 0;
this->DataRequest = 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;
info->Get(vtkExecutive::PRODUCER(),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())
{
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 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())
{
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())
{
// 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.
int result = this->CallAlgorithm(request, vtkExecutive::RequestDownstream,
inInfo, outInfo);
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()))
{
// The output port specifies a data type. Make sure the data
// object exists and is of the right type.
if(!data || !data->IsA(dt))
{
// Try to create an instance of the correct type.
data = this->NewDataObject(dt);
this->SetOutputData(port, data, outInfo);
if(data)
{
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(const char* dt = info->Get(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE()))
{
// 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 " << dt
<< " is required.");
return 0;
}
// The input must be of required type or NULL.
if(input && !input->IsA(dt))
{
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 " << dt
<< " 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->GetAttribute(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(vtkDataArray* 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()))
{
int arrayNumComponents =
field->Get(vtkDataObject::FIELD_NUMBER_OF_COMPONENTS());
if(array->GetNumberOfComponents() != arrayNumComponents)
{
return 0;
}
}
// Enforce number of tuples. This should really only be used for
// field data (not point or cell data).
if(field->Has(vtkDataObject::FIELD_NUMBER_OF_TUPLES()))
{
int arrayNumTuples = field->Get(vtkDataObject::FIELD_NUMBER_OF_TUPLES());
if(array->GetNumberOfTuples() != arrayNumTuples)
{
return 0;
}
}
return 1;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputIsOptional(int port)
{
if(vtkInformation* info = this->Algorithm->GetInputPortInformation(port))
{
return info->Get(vtkAlgorithm::INPUT_IS_OPTIONAL());
}
return 0;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::InputIsRepeatable(int port)
{
if(vtkInformation* info = this->Algorithm->GetInputPortInformation(port))
{
return info->Get(vtkAlgorithm::INPUT_IS_REPEATABLE());
}
return 0;
}
//----------------------------------------------------------------------------
vtkDataObject* vtkDemandDrivenPipeline::NewDataObject(const char* type)
{
// Check for some standard types and then try the instantiator.
if(strcmp(type, "vtkImageData") == 0)
{
return vtkImageData::New();
}
else if(strcmp(type, "vtkPolyData") == 0)
{
return vtkPolyData::New();
}
else if(strcmp(type, "vtkRectilinearGrid") == 0)
{
return vtkRectilinearGrid::New();
}
else if(strcmp(type, "vtkStructuredGrid") == 0)
{
return vtkStructuredGrid::New();
}
else if(strcmp(type, "vtkStructuredPoints") == 0)
{
return vtkStructuredPoints::New();
}
else if(strcmp(type, "vtkUnstructuredGrid") == 0)
{
return vtkUnstructuredGrid::New();
}
else if(strcmp(type, "vtkHierarchicalDataSet") == 0)
{
return vtkHierarchicalDataSet::New();
}
else if(strcmp(type, "vtkHierarchicalBoxDataSet") == 0)
{
return vtkHierarchicalBoxDataSet::New();
}
else if(vtkObject* obj = vtkInstantiator::CreateInstance(type))
{
vtkDataObject* data = vtkDataObject::SafeDownCast(obj);
if(!data)
{
obj->Delete();
}
return data;
}
else
{
return 0;
}
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline
::NeedToExecuteData(int outputPort,
vtkInformationVector** inInfoVec,
vtkInformationVector* outInfoVec)
{
// If the filter parameters or input have been modified since the
// last execution then we must execute. This is a shortcut for most
// filters since all outputs will have the same UpdateTime. This
// also handles the case in which there are no outputs.
if(this->PipelineMTime > this->DataTime.GetMTime())
{
return 1;
}
if(outputPort >= 0)
{
// If the output on the port making the request is out-of-date
// then we must execute.
vtkInformation* info = outInfoVec->GetInformationObject(outputPort);
vtkDataObject* data = info->Get(vtkDataObject::DATA_OBJECT());
if(!data || this->PipelineMTime > data->GetUpdateTime())
{
return 1;
}
}
else
{
// No port is specified. Check all ports.
for(int i=0; i < this->Algorithm->GetNumberOfOutputPorts(); ++i)
{
if(this->NeedToExecuteData(i,inInfoVec,outInfoVec))
{
return 1;
}
}
}
// We do not need to execute.
return 0;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::SetReleaseDataFlag(int port, int n)
{
if(!this->OutputPortIndexInRange(port, "set release data flag on"))
{
return 0;
}
vtkInformation* info = this->GetOutputInformation(port);
if(this->GetReleaseDataFlag(port) != n)
{
info->Set(RELEASE_DATA(), n);
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
int vtkDemandDrivenPipeline::GetReleaseDataFlag(int port)
{
if(!this->OutputPortIndexInRange(port, "get release data flag from"))
{
return 0;
}
vtkInformation* info = this->GetOutputInformation(port);
if(!info->Has(RELEASE_DATA()))
{
info->Set(RELEASE_DATA(), 0);
}
return info->Get(RELEASE_DATA());
}
|