/*========================================================================= Program: Insight Segmentation & Registration Toolkit Module: $RCSfile: itkLabelMap.txx,v $ Language: C++ Date: $Date: 2006/05/10 20:27:16 $ Version: $Revision: 1.97 $ 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 _itkLabelMap_txx #define _itkLabelMap_txx #include "itkLabelMap.h" #include "itkProcessObject.h" namespace itk { /** * */ template LabelMap ::LabelMap() { m_BackgroundValue = NumericTraits< LabelType >::Zero; this->Initialize(); } /** * */ template void LabelMap ::PrintSelf(std::ostream& os, Indent indent) const { Superclass::PrintSelf(os,indent); os << indent << "BackgroundValue: " << static_cast::PrintType>(m_BackgroundValue) << std::endl; os << indent << "LabelObjectContainer: " << & m_LabelObjectContainer << std::endl; } /** * */ template void LabelMap ::Initialize() { m_LabelObjectContainer.clear(); } /** * */ template void LabelMap ::Allocate() { this->Initialize(); } template void LabelMap ::Graft(const DataObject *data) { // call the superclass' implementation Superclass::Graft( data ); if ( data ) { // Attempt to cast data to an Image const Self * imgData; try { imgData = dynamic_cast( data ); } catch( ... ) { return; } if ( imgData ) { // Now copy anything remaining that is needed m_LabelObjectContainer = imgData->m_LabelObjectContainer; m_BackgroundValue = imgData->m_BackgroundValue; } else { // pointer could not be cast back down itkExceptionMacro( << "itk::Image::Graft() cannot cast " << typeid(data).name() << " to " << typeid(const Self *).name() ); } } } template typename LabelMap::LabelObjectType * LabelMap ::GetLabelObject( const LabelType & label ) { if( ! this->HasLabel( label ) ) { itkExceptionMacro( << "No label object with label " << static_cast::PrintType>(label) << "." ); } if( m_BackgroundValue == label ) { itkExceptionMacro( << "Label " << static_cast::PrintType>(label) << " is the background label." ); } return m_LabelObjectContainer[label].GetPointer(); } template const typename LabelMap::LabelObjectType * LabelMap ::GetLabelObject( const LabelType & label ) const { if( ! this->HasLabel( label ) ) { itkExceptionMacro( << "No label object with label " << static_cast::PrintType>(label) << "." ); } if( m_BackgroundValue == label ) { itkExceptionMacro( << "Label " << static_cast::PrintType>(label) << " is the background label." ); } return m_LabelObjectContainer.find( label )->second.GetPointer(); } template bool LabelMap ::HasLabel( const LabelType label ) const { if( label == m_BackgroundValue ) { return true; } return m_LabelObjectContainer.find( label ) != m_LabelObjectContainer.end(); } template const typename LabelMap::LabelType & LabelMap ::GetPixel( const IndexType & idx ) const { for( typename LabelObjectContainerType::const_iterator it = m_LabelObjectContainer.begin(); it != m_LabelObjectContainer.end(); it++ ) { if( it->second->HasIndex( idx ) ) { return it->second->GetLabel(); } } return m_BackgroundValue; } template void LabelMap ::SetPixel( const IndexType & idx, const LabelType & label ) { if( label == m_BackgroundValue ) { // just do nothing return; } typename LabelObjectContainerType::iterator it = m_LabelObjectContainer.find( label ); if( it != m_LabelObjectContainer.end() ) { // the label already exist - add the pixel to it (*it).second->AddIndex( idx ); } else { // the label does not exist yet - create a new one LabelObjectPointerType labelObject = LabelObjectType::New(); labelObject->SetLabel( label ); labelObject->AddIndex( idx ); this->AddLabelObject( labelObject ); // std::cout<< m_LabelObjectContainer.size() << std::endl; } } template void LabelMap ::SetLine( const IndexType & idx, const unsigned long & length, const LabelType & label ) { if( label == m_BackgroundValue ) { // just do nothing return; } typename LabelObjectContainerType::iterator it = m_LabelObjectContainer.find( label ); if( it != m_LabelObjectContainer.end() ) { // the label already exist - add the pixel to it (*it).second->AddLine( idx, length ); } else { // the label does not exist yet - create a new one LabelObjectPointerType labelObject = LabelObjectType::New(); labelObject->SetLabel( label ); labelObject->AddLine( idx, length ); this->AddLabelObject( labelObject ); // std::cout<< m_LabelObjectContainer.size() << std::endl; } } template typename LabelMap::LabelObjectType * LabelMap ::GetLabelObject( const IndexType & idx ) const { for( typename LabelObjectContainerType::const_iterator it = m_LabelObjectContainer.begin(); it != m_LabelObjectContainer.end(); it++ ) { if( it->second->HasIndex( idx ) ) { return it->second.GetPointer(); } } itkExceptionMacro( << "No label object at index " << idx << "." ); // return NULL; } template void LabelMap ::AddLabelObject( LabelObjectType * labelObject ) { assert( labelObject != NULL ); assert( !this->HasLabel( labelObject->GetLabel() ) ); m_LabelObjectContainer[ labelObject->GetLabel() ] = labelObject; } template void LabelMap ::RemoveLabelObject( LabelObjectType * labelObject ) { assert( labelObject != NULL ); this->RemoveLabel( labelObject->GetLabel() ); } template void LabelMap ::RemoveLabel( const LabelType & label ) { if( label == m_BackgroundValue ) { // just do nothing return; } m_LabelObjectContainer.erase( label ); } template void LabelMap ::ClearLabels() { m_LabelObjectContainer.clear(); } template const typename LabelMap::LabelObjectContainerType & LabelMap ::GetLabelObjectContainer() const { return m_LabelObjectContainer; } template unsigned long LabelMap ::GetNumberOfLabelObjects() const { return m_LabelObjectContainer.size(); } template void LabelMap ::PrintLabelObjects(std::ostream& os) const { for( typename LabelObjectContainerType::const_iterator it = m_LabelObjectContainer.begin(); it != m_LabelObjectContainer.end(); it++ ) { assert( it->second.IsNotNull() ); it->second->Print( os ); os << std::endl; } } } // end namespace itk #endif