/*========================================================================= Program: Insight Segmentation & Registration Toolkit Module: $RCSfile: itkInPlaceComponentTreeFilter.txx,v $ Language: C++ Date: $Date: 2004/07/11 14:56:39 $ Version: $Revision: 1.7 $ 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. =========================================================================*/ #ifndef _itkInPlaceComponentTreeFilter_txx #define _itkInPlaceComponentTreeFilter_txx #include "itkInPlaceComponentTreeFilter.h" namespace itk { /** * */ template InPlaceComponentTreeFilter ::InPlaceComponentTreeFilter() : m_InPlace(true) { } /** * */ template InPlaceComponentTreeFilter ::~InPlaceComponentTreeFilter() { } template void InPlaceComponentTreeFilter ::PrintSelf(std::ostream& os, Indent indent) const { Superclass::PrintSelf(os, indent); os << indent << "InPlace: " << (m_InPlace ? "On" : "Off") << std::endl; if ( this->CanRunInPlace()) { os << indent << "The input and output to this filter are the same type. The filter can be run in place." << std::endl; } else { os << indent << "The input and output to this filter are different types. The filter cannot be run in place." << std::endl; } } template void InPlaceComponentTreeFilter ::AllocateOutputs() { // if told to run in place and the types support it, if (m_InPlace && this->CanRunInPlace() ) { // Graft this first input to the output. Later, we'll need to // remove the input's hold on the bulk data. // OutputImagePointer inputAsOutput = dynamic_cast(const_cast(this->GetInput())); if (inputAsOutput) { this->GraftOutput( inputAsOutput ); } } else { Superclass::AllocateOutputs(); // copy the content of the input image to the output image this->GetOutput()->SetRoot( this->GetInput()->GetRoot()->Clone() ); this->GetOutput()->GetLinkedListArray() = this->GetInput()->GetLinkedListArray(); } } template void InPlaceComponentTreeFilter ::ReleaseInputs() { // if told to run in place and the types support it, if (m_InPlace && (typeid(TInputImage) == typeid(TOutputImage))) { // Release any input where the ReleaseData flag has been set ProcessObject::ReleaseInputs(); // Release input 0 by default since we overwrote it TInputImage * ptr = const_cast( this->GetInput() ); if( ptr ) { ptr->ReleaseData(); } } else { Superclass::ReleaseInputs(); } } template void InPlaceComponentTreeFilter ::GenerateInputRequestedRegion() { // call the superclass' implementation of this method Superclass::GenerateInputRequestedRegion(); // We need all the input. InputImagePointer input = const_cast(this->GetInput()); if ( !input ) { return; } input->SetRequestedRegion( input->GetLargestPossibleRegion() ); } template void InPlaceComponentTreeFilter ::EnlargeOutputRequestedRegion(DataObject *) { this->GetOutput() ->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() ); } template bool InPlaceComponentTreeFilter ::IsMonotone( bool inc, bool strict ) { return this->IsMonotone( this->GetOutput()->GetRoot(), inc, strict ); } template bool InPlaceComponentTreeFilter ::IsMonotone( const NodeType * node, bool inc, bool strict ) { const typename NodeType::ChildrenListType * childrenList = & node->GetChildren(); for( typename NodeType::ChildrenListType::const_iterator it=childrenList->begin(); it!=childrenList->end(); it++ ) { if( inc ) { if( node->GetAttribute() > (*it)->GetAttribute() ) { return false; } } else { if( node->GetAttribute() < (*it)->GetAttribute() ) { return false; } } if( strict && node->GetAttribute() == (*it)->GetAttribute() ) { return false; } if( ! this->IsMonotone( *it, inc, strict ) ) { return false; } } return true; } } // end namespace itk #endif