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 1135 1136 1137 1138
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkProcessObject.cxx,v $
Language: C++
Date: $Date: 2007-12-23 17:59:29 $
Version: $Revision: 1.74 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
Portions of this code are covered under the VTK copyright.
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.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 notices for more information.
=========================================================================*/
#include "itkProcessObject.h"
#include "itkDataObject.h"
#include "itkObjectFactory.h"
#include "itkCommand.h"
#include <functional>
#include <algorithm>
namespace itk
{
/**
* Instantiate object with no start, end, or progress methods.
*/
ProcessObject
::ProcessObject()
{
m_NumberOfRequiredInputs = 0;
m_NumberOfRequiredOutputs = 0;
m_AbortGenerateData = false;
m_Progress = 0.0f;
m_Updating = false;
m_Threader = MultiThreader::New();
m_NumberOfThreads = m_Threader->GetNumberOfThreads();
m_ReleaseDataBeforeUpdateFlag = true;
}
/**
* This is a default implementation to make sure we have something.
* Once all the subclasses of ProcessObject provide an appopriate
* MakeOutput(), then ProcessObject::MakeOutput() can be made pure
* virtual.
*/
DataObject::Pointer
ProcessObject
::MakeOutput(unsigned int)
{
return static_cast<DataObject*>(DataObject::New().GetPointer());
}
/**
* Destructor for the ProcessObject class. We've got to
* UnRegister() the use of any input classes.
*/
ProcessObject
::~ProcessObject()
{
// Tell each output that we are going away. If other objects have a
// reference to one of these outputs, the data object will not be deleted
// when the process object is deleted. However, the data object's source
// will still point back to the now nonexistent process object if we do not
// clean things up now.
unsigned int idx;
for (idx = 0; idx < m_Outputs.size(); ++idx)
{
if (m_Outputs[idx])
{
// let the output know we no longer want to associate with the object
m_Outputs[idx]->DisconnectSource(this, idx);
// let go of our reference to the data object
m_Outputs[idx] = 0;
}
}
}
//typedef DataObject *DataObjectPointer;
/**
* Called by constructor to set up input array.
*/
void
ProcessObject
::SetNumberOfInputs(unsigned int num)
{
// in case nothing has changed.
if (num == m_Inputs.size())
{
return;
}
m_Inputs.resize(num);
this->Modified();
}
/**
* Get the number of specified inputs
*/
ProcessObject::DataObjectPointerArraySizeType
ProcessObject
::GetNumberOfValidRequiredInputs() const
{
DataObjectPointerArraySizeType num;
if (m_NumberOfRequiredInputs < m_Inputs.size())
{
num = m_NumberOfRequiredInputs;
}
else
{
num = m_Inputs.size();
}
// count the number of non-null inputs
// this used to use std::count_if, but that function object
// did not work correctly with SunPro CC 5.6.
int count = 0;
for(std::vector<DataObjectPointer>::const_iterator i = m_Inputs.begin();
i < (m_Inputs.begin() + num); ++i)
{
if((*i).IsNotNull())
{
count++;
}
}
return count;
}
/**
* Adds an input to the first null position in the input list.
* Expands the list memory if necessary
*/
void
ProcessObject
::AddInput(DataObject *input)
{
DataObjectPointerArraySizeType idx;
this->Modified();
for (idx = 0; idx < m_Inputs.size(); ++idx)
{
if (!m_Inputs[idx])
{
m_Inputs[idx] = input;
return;
}
}
this->SetNumberOfInputs( static_cast<int>( m_Inputs.size() + 1 ) );
m_Inputs[ static_cast<int>( m_Inputs.size() ) - 1] = input;
}
/**
* Remove an input.
*
* Removes the first occurence of the given OutputObject from the
* inputs to this ProcessObject. If it's the last object on the
* list, shortens the list.
*/
void
ProcessObject
::RemoveInput(DataObject *input)
{
if (!input)
{
return;
}
// find the input in the list of inputs
DataObjectPointerArray::iterator pos =
std::find(m_Inputs.begin(), m_Inputs.end(), input);
if(pos == m_Inputs.end())
{
itkDebugMacro("tried to remove an input that was not in the list");
return;
}
// Set the position in the m_Inputs containing input to 0
*pos = 0;
// if that was the last input, then shrink the list
if (pos == m_Inputs.end() - 1 )
{
this->SetNumberOfInputs( static_cast<int>( m_Inputs.size() ) - 1);
}
this->Modified();
}
/**
* Set an Input of this filter. This method
* does Register()/UnRegister() manually to
* deal with the fact that smart pointers aren't
* around to do the reference counting.
*/
void
ProcessObject
::SetNthInput(unsigned int idx, DataObject *input)
{
// does this change anything?
if ( idx < m_Inputs.size() && m_Inputs[idx] == input )
{
return;
}
// Expand array if necessary.
if (idx >= m_Inputs.size())
{
this->SetNumberOfInputs(idx + 1);
}
m_Inputs[idx] = input;
this->Modified();
}
/**
* Model a queue on the input list by providing a push back
*/
void
ProcessObject
::PushBackInput(const DataObject *input)
{
m_Inputs.push_back(const_cast<DataObject*>(input));
this->Modified();
}
/**
* Model a stack on the input list by providing a pop back
*/
void
ProcessObject
::PopBackInput()
{
if (!m_Inputs.empty())
{
m_Inputs.pop_back();
this->Modified();
}
}
/**
*
*/
void
ProcessObject
::PushFrontInput(const DataObject* input)
{
// add an empty element to the end of the vector to make sure that
// we have enough space for the copy
m_Inputs.push_back(0);
// shift the current inputs down by one place
std::copy_backward(m_Inputs.begin(), m_Inputs.end()-1,
m_Inputs.end());
// put in the new input in the front
m_Inputs[0] = const_cast<DataObject*>(input);
this->Modified();
}
/**
*
*/
void
ProcessObject
::PopFrontInput()
{
if (!m_Inputs.empty())
{
std::copy(m_Inputs.begin()+1, m_Inputs.end(),
m_Inputs.begin());
m_Inputs.pop_back();
this->Modified();
}
}
void
ProcessObject
::RemoveOutput(DataObject *output)
{
if (!output)
{
return;
}
// find the input in the list of inputs
DataObjectPointerArray::iterator pos =
std::find(m_Outputs.begin(), m_Outputs.end(), output);
if(pos == m_Outputs.end())
{
itkDebugMacro("tried to remove an output that was not in the list");
return;
}
// let the output know we no longer want to associate with the object
(*pos)->DisconnectSource(this, pos - m_Outputs.begin());
// let go of our reference to the data object
*pos = 0;
// if that was the last output, then shrink the list
if (pos == m_Outputs.end() - 1 )
{
this->SetNumberOfOutputs( static_cast<int>( m_Outputs.size() ) - 1);
}
this->Modified();
}
/**
* Set an output of this filter. This method specifically
* does not do a Register()/UnRegister() because of the
* desire to break the reference counting loop.
*/
void
ProcessObject
::SetNthOutput(unsigned int idx, DataObject *output)
{
// does this change anything?
if ( idx < m_Outputs.size() && output == m_Outputs[idx])
{
return;
}
// Expand array if necessary.
if (idx >= m_Outputs.size())
{
this->SetNumberOfOutputs(idx + 1);
}
// Keep a handle to the original output and disconnect the old output from
// the pipeline
DataObjectPointer oldOutput;
if ( m_Outputs[idx] )
{
oldOutput = m_Outputs[idx];
m_Outputs[idx]->DisconnectSource(this, idx);
}
if (output)
{
output->ConnectSource(this, idx);
}
// save the current reference (which releases the previous reference)
m_Outputs[idx] = output;
// if we are clearing an output, we need to create a new blank output
// so we are prepared for the next Update(). this copies the requested
// region ivar
if (!m_Outputs[idx])
{
itkDebugMacro( " creating new output object." );
DataObjectPointer newOutput = this->MakeOutput(idx);
this->SetNthOutput(idx, newOutput);
// If we had an output object before, copy the requested region
// ivars and release data flag to the the new output
if (oldOutput)
{
newOutput->SetRequestedRegion( oldOutput );
newOutput->SetReleaseDataFlag( oldOutput->GetReleaseDataFlag() );
}
}
this->Modified();
}
/**
* Adds an output to the first null position in the output list.
* Expands the list memory if necessary
*/
void
ProcessObject
::AddOutput(DataObject *output)
{
unsigned int idx;
for (idx = 0; idx < m_Outputs.size(); ++idx)
{
if ( m_Outputs[idx].IsNull() )
{
m_Outputs[idx] = output;
if (output)
{
output->ConnectSource(this, idx);
}
this->Modified();
return;
}
}
this->SetNumberOfOutputs( static_cast<int>( m_Outputs.size() ) + 1);
m_Outputs[ static_cast<int>( m_Outputs.size() ) - 1] = output;
if (output)
{
output->ConnectSource(this, static_cast<int>( m_Outputs.size() ) - 1 );
}
this->Modified();
}
/**
* Called by constructor to set up output array.
*/
void
ProcessObject
::SetNumberOfOutputs(unsigned int num)
{
// in case nothing has changed.
if (num == m_Outputs.size())
{
return;
}
m_Outputs.resize(num);
this->Modified();
}
/**
*
*/
DataObject *
ProcessObject
::GetOutput(unsigned int i)
{
if (m_Outputs.size() < i+1)
{
return NULL;
}
return m_Outputs[i].GetPointer();
}
const DataObject *
ProcessObject
::GetOutput(unsigned int i) const
{
if (m_Outputs.size() < i+1)
{
return NULL;
}
return m_Outputs[i].GetPointer();
}
/**
*
*/
DataObject *
ProcessObject
::GetInput(unsigned int i)
{
if (m_Inputs.size() < i+1)
{
return NULL;
}
return m_Inputs[i].GetPointer();
}
const DataObject *
ProcessObject
::GetInput(unsigned int i) const
{
if (m_Inputs.size() < i+1)
{
return NULL;
}
return m_Inputs[i].GetPointer();
}
/**
* Update the progress of the process object. If a ProgressMethod exists,
* execute it. Then set the Progress ivar to amount. The parameter amount
* should range between (0,1).
*/
void
ProcessObject
::UpdateProgress(float amount)
{
m_Progress = amount;
this->InvokeEvent( ProgressEvent() );
}
/**
*
*/
bool
ProcessObject
::GetReleaseDataFlag() const
{
if (this->GetOutput(0))
{
return this->GetOutput(0)->GetReleaseDataFlag();
}
itkWarningMacro(<<"Output doesn't exist!");
return false;
}
/**
*
*/
void
ProcessObject
::SetReleaseDataFlag(bool val)
{
unsigned int idx;
for (idx = 0; idx < m_Outputs.size(); idx++)
{
if (m_Outputs[idx])
{
m_Outputs[idx]->SetReleaseDataFlag(val);
}
}
}
/**
*
*/
void
ProcessObject
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os,indent);
os << indent << "Number Of Required Inputs: "
<< m_NumberOfRequiredInputs << std::endl;
os << indent << "Number Of Required Outputs: "
<< m_NumberOfRequiredOutputs << std::endl;
os << indent << "Number Of Threads: "
<< m_NumberOfThreads << std::endl;
os << indent << "ReleaseDataFlag: "
<< (this->GetReleaseDataFlag() ? "On" : "Off") << std::endl;
os << indent << "ReleaseDataBeforeUpdateFlag: "
<< (m_ReleaseDataBeforeUpdateFlag ? "On" : "Off") << std::endl;
if ( m_Inputs.size())
{
DataObjectPointerArraySizeType idx;
for (idx = 0; idx < m_Inputs.size(); ++idx)
{
os << indent << "Input " << static_cast<int>( idx );
os << ": (" << m_Inputs[idx].GetPointer() << ")\n";
}
}
else
{
os << indent <<"No Inputs\n";
}
if ( m_Outputs.size())
{
DataObjectPointerArraySizeType idx;
for (idx = 0; idx < m_Outputs.size(); ++idx)
{
os << indent << "Output " << static_cast<int>( idx );
os << ": (" << m_Outputs[idx].GetPointer() << ")\n";
}
}
else
{
os << indent <<"No Output\n";
}
os << indent << "AbortGenerateData: " << (m_AbortGenerateData ? "On\n" : "Off\n");
os << indent << "Progress: " << m_Progress << "\n";
os << indent << "Multithreader: " << std::endl;
m_Threader->PrintSelf(os, indent.GetNextIndent());
}
/**
* The following methods are used to coordinate the execution of the
* data processing pipeline.
*/
/**
*
*/
void
ProcessObject
::Update()
{
if (this->GetOutput(0))
{
this->GetOutput(0)->Update();
}
}
void
ProcessObject
::ResetPipeline()
{
if (this->GetOutput(0))
{
this->GetOutput(0)->ResetPipeline();
}
}
void
ProcessObject
::PropagateResetPipeline()
{
//
// Reset this object.
//
// Clear the updating flag.
m_Updating = 0;
//
// Loop through the inputs
//
unsigned int idx;
DataObject::Pointer input;
for (idx = 0; idx < m_Inputs.size(); ++idx)
{
if (m_Inputs[idx])
{
input = m_Inputs[idx];
/**
* Propagate the ResetPipeline call
*/
input->PropagateResetPipeline();
}
}
}
/**
*
*/
void
ProcessObject
::UpdateOutputInformation()
{
unsigned long t1, t2;
DataObjectPointerArraySizeType idx;
DataObject *input;
DataObject *output;
/**
* Watch out for loops in the pipeline
*/
if ( m_Updating )
{
/**
* Since we are in a loop, we will want to update. But if
* we don't modify this filter, then we will not execute
* because our OutputInformationMTime will be more recent than
* the MTime of our output.
*/
this->Modified();
return;
}
/**
* We now wish to set the PipelineMTime of each output DataObject to
* the largest of this ProcessObject's MTime, all input DataObject's
* PipelineMTime, and all input's MTime. We begin with the MTime of
* this ProcessObject.
*/
t1 = this->GetMTime();
/**
* Loop through the inputs
*/
for (idx = 0; idx < m_Inputs.size(); ++idx)
{
if (m_Inputs[idx])
{
input = m_Inputs[idx];
/**
* Propagate the UpdateOutputInformation call
*/
m_Updating = true;
input->UpdateOutputInformation();
m_Updating = false;
/**
* What is the PipelineMTime of this input? Compare this against
* our current computation to find the largest one.
*/
t2 = input->GetPipelineMTime();
if (t2 > t1)
{
t1 = t2;
}
/**
* Pipeline MTime of the input does not include the MTime of the
* data object itself. Factor these mtimes into the next PipelineMTime
*/
t2 = input->GetMTime();
if (t2 > t1)
{
t1 = t2;
}
}
}
/**
* Call GenerateOutputInformation for subclass specific information.
* Since UpdateOutputInformation propagates all the way up the pipeline,
* we need to be careful here to call GenerateOutputInformation only if
* necessary. Otherwise, we may cause this source to be modified which
* will cause it to execute again on the next update.
*/
if (t1 > m_OutputInformationMTime.GetMTime())
{
for (idx = 0; idx < m_Outputs.size(); ++idx)
{
output = this->GetOutput( static_cast<int>( idx ) );
if (output)
{
output->SetPipelineMTime(t1);
}
}
this->GenerateOutputInformation();
/**
* Keep track of the last time GenerateOutputInformation() was called
*/
m_OutputInformationMTime.Modified();
}
}
/**
*
*/
void
ProcessObject
::PropagateRequestedRegion(DataObject *output)
{
/**
* check flag to avoid executing forever if there is a loop
*/
if (m_Updating)
{
return;
}
/**
* Give the subclass a chance to indicate that it will provide
* more data then required for the output. This can happen, for
* example, when a source can only produce the whole output.
* Although this is being called for a specific output, the source
* may need to enlarge all outputs.
*/
this->EnlargeOutputRequestedRegion( output );
/**
* Give the subclass a chance to define how to set the requested
* regions for each of its outputs, given this output's requested
* region. The default implementation is to make all the output
* requested regions the same. A subclass may need to override this
* method if each output is a different resolution.
*/
this->GenerateOutputRequestedRegion( output );
/**
* Give the subclass a chance to request a larger requested region on
* the inputs. This is necessary when, for example, a filter
* requires more data at the "internal" boundaries to
* produce the boundary values - such as an image filter that
* derives a new pixel value by applying some operation to a
* neighborhood of surrounding original values.
*/
this->GenerateInputRequestedRegion();
/**
* Now that we know the input requested region, propagate this
* through all the inputs.
*/
m_Updating = true;
DataObjectPointerArraySizeType idx;
for (idx = 0; idx < m_Inputs.size(); ++idx)
{
if (m_Inputs[idx])
{
m_Inputs[idx]->PropagateRequestedRegion();
}
}
m_Updating = false;
}
/**
* By default we require all the input to produce the output. This is
* overridden in the subclasses since we can often produce the output with
* just a portion of the input data.
*/
void
ProcessObject
::GenerateInputRequestedRegion()
{
DataObjectPointerArraySizeType idx;
for (idx = 0; idx < m_Inputs.size(); ++idx)
{
if (m_Inputs[idx])
{
m_Inputs[idx]->SetRequestedRegionToLargestPossibleRegion();
}
}
}
/**
* By default we set all the output requested regions to be the same.
*/
void
ProcessObject
::GenerateOutputRequestedRegion(DataObject *output)
{
DataObjectPointerArraySizeType idx;
for (idx = 0; idx < m_Outputs.size(); ++idx)
{
if (m_Outputs[idx] && m_Outputs[idx] != output)
{
m_Outputs[idx]->SetRequestedRegion(output);
}
}
}
/**
*
*/
void
ProcessObject
::PrepareOutputs()
{
unsigned int idx;
if (this->GetReleaseDataBeforeUpdateFlag())
{
for (idx = 0; idx < m_Outputs.size(); idx++)
{
if (m_Outputs[idx])
{
m_Outputs[idx]->PrepareForNewData();
}
}
}
}
/**
*
*/
void
ProcessObject
::ReleaseInputs()
{
unsigned int idx;
for (idx = 0; idx < m_Inputs.size(); ++idx)
{
if (m_Inputs[idx])
{
if ( m_Inputs[idx]->ShouldIReleaseData() )
{
m_Inputs[idx]->ReleaseData();
}
}
}
}
/**
*
*/
void
ProcessObject
::UpdateOutputData(DataObject *itkNotUsed(output))
{
DataObjectPointerArraySizeType idx;
/**
* prevent chasing our tail
*/
if (m_Updating)
{
return;
}
/**
* Prepare all the outputs. This may deallocate previous bulk data.
*/
this->PrepareOutputs();
/**
* Propagate the update call - make sure everything we
* might rely on is up-to-date
* Must call PropagateRequestedRegion before UpdateOutputData if multiple
* inputs since they may lead back to the same data object.
*/
m_Updating = true;
if ( m_Inputs.size() == 1 )
{
if (m_Inputs[0])
{
m_Inputs[0]->UpdateOutputData();
}
}
else
{
for (idx = 0; idx < m_Inputs.size(); ++idx)
{
if (m_Inputs[idx])
{
m_Inputs[idx]->PropagateRequestedRegion();
m_Inputs[idx]->UpdateOutputData();
}
}
}
/**
* Cache the state of any ReleaseDataFlag's on the inputs. While the
* filter is executing, we need to set the ReleaseDataFlag's on the
* inputs to false in case the current filter is implemented using a
* mini-pipeline (which will try to release the inputs). After the
* filter finishes, we restore the state of the ReleaseDataFlag's
* before the call to ReleaseInputs().
*/
this->CacheInputReleaseDataFlags();
/**
* Tell all Observers that the filter is starting
*/
this->InvokeEvent( StartEvent() );
/**
* GenerateData this object - we have not aborted yet, and our progress
* before we start to execute is 0.0.
*/
m_AbortGenerateData = false;
m_Progress = 0.0f;
/**
* Count the number of required inputs which have been assigned
*/
DataObjectPointerArraySizeType ninputs = this->GetNumberOfValidRequiredInputs();
if (ninputs < m_NumberOfRequiredInputs)
{
itkExceptionMacro(<< "At least " << m_NumberOfRequiredInputs
<< " inputs are required but only " << ninputs
<< " are specified.");
}
else
{
try
{
this->GenerateData();
}
catch( ProcessAborted & excp )
{
this->InvokeEvent( AbortEvent() );
this->ResetPipeline();
this->RestoreInputReleaseDataFlags();
throw excp;
}
catch( ExceptionObject& excp )
{
this->ResetPipeline();
this->RestoreInputReleaseDataFlags();
throw excp;
}
}
/**
* If we ended due to aborting, push the progress up to 1.0 (since
* it probably didn't end there)
*
*/
if ( m_AbortGenerateData )
{
this->UpdateProgress(1.0f);
}
/**
* Notify end event observers
*/
this->InvokeEvent( EndEvent() );
/**
* Now we have to mark the data as up to date.
*/
for (idx = 0; idx < m_Outputs.size(); ++idx)
{
if (m_Outputs[idx])
{
m_Outputs[idx]->DataHasBeenGenerated();
}
}
/**
* Restore the state of any input ReleaseDataFlags
*/
this->RestoreInputReleaseDataFlags();
/**
* Release any inputs if marked for release
*/
this->ReleaseInputs();
// Mark that we are no longer updating the data in this filter
m_Updating = false;
}
/**
*
*/
void
ProcessObject
::CacheInputReleaseDataFlags()
{
unsigned int idx;
m_CachedInputReleaseDataFlags.resize( m_Inputs.size() );
for (idx = 0; idx < m_Inputs.size(); ++idx)
{
if (m_Inputs[idx])
{
m_CachedInputReleaseDataFlags[idx]=m_Inputs[idx]->GetReleaseDataFlag();
m_Inputs[idx]->ReleaseDataFlagOff();
}
else
{
m_CachedInputReleaseDataFlags[idx] = false;
}
}
}
/**
*
*/
void
ProcessObject
::RestoreInputReleaseDataFlags()
{
unsigned int idx;
for (idx = 0;
idx < m_Inputs.size() && idx < m_CachedInputReleaseDataFlags.size();
++idx)
{
if (m_Inputs[idx])
{
m_Inputs[idx]->SetReleaseDataFlag(m_CachedInputReleaseDataFlags[idx]);
}
}
}
/**
* Default implementation - copy information from first input to all outputs
*/
void
ProcessObject
::GenerateOutputInformation()
{
DataObjectPointer input, output;
if (m_Inputs.size() && m_Inputs[0])
{
input = m_Inputs[0];
for (unsigned int idx = 0; idx < m_Outputs.size(); ++idx)
{
output = this->GetOutput(idx);
if (output)
{
output->CopyInformation(input);
}
}
}
}
/**
*
*/
void
ProcessObject
::UpdateLargestPossibleRegion()
{
this->UpdateOutputInformation();
if (this->GetOutput(0))
{
this->GetOutput(0)->SetRequestedRegionToLargestPossibleRegion();
this->GetOutput(0)->Update();
}
}
} // end namespace itk
|