X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=lib%2Ffpa%2FFilters%2FAlgorithm.hxx;fp=lib%2Ffpa%2FFilters%2FAlgorithm.hxx;h=be4eacc9171ceea4fe4717fcd521838d2f90a3a6;hb=bd89a1af0c14ed2ac0afeca923103de54283cbaf;hp=0000000000000000000000000000000000000000;hpb=a8ac405fe1422bc0792a810f7f0693096a22c20e;p=FrontAlgorithms.git diff --git a/lib/fpa/Filters/Algorithm.hxx b/lib/fpa/Filters/Algorithm.hxx new file mode 100644 index 0000000..be4eacc --- /dev/null +++ b/lib/fpa/Filters/Algorithm.hxx @@ -0,0 +1,285 @@ +// ========================================================================= +// @author Leonardo Florez Valencia +// @email florez-l@javeriana.edu.co +// ========================================================================= +#ifndef __fpa__Filters__Algorithm__hxx__ +#define __fpa__Filters__Algorithm__hxx__ + +#include + +// ------------------------------------------------------------------------- +template< class _TTraits > +itk::ModifiedTimeType fpa::Filters::Algorithm< _TTraits >:: +GetMTime( ) const +{ + itk::ModifiedTimeType q = this->Superclass::GetMTime( ); + itk::ModifiedTimeType t; + for( itk::Object* o: this->m_AssociatedObjects ) + { + if( o != NULL ) + { + t = o->GetMTime( ); + q = ( q < t )? q: t; + + } // fi + + } // rof + return( q ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +void fpa::Filters::Algorithm< _TTraits >:: +InvokeEvent( const itk::EventObject& e ) +{ + TEvent a; + if( a.CheckEvent( &e ) ) + { + if( this->m_VisualDebug ) + this->Superclass::InvokeEvent( e ); + } + else + this->Superclass::InvokeEvent( e ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +void fpa::Filters::Algorithm< _TTraits >:: +InvokeEvent( const itk::EventObject& e ) const +{ + TEvent a; + if( a.CheckEvent( &e ) ) + { + if( this->m_VisualDebug ) + this->Superclass::InvokeEvent( e ); + } + else + this->Superclass::InvokeEvent( e ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +fpa::Filters::Algorithm< _TTraits >:: +Algorithm( ) + : Superclass( ), + TTraits::TMarksInterface( this ), + TTraits::TSeedsInterface( this ), + m_VisualDebug( false ), + m_InitValue( TOutputValue( 0 ) ) +{ +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +fpa::Filters::Algorithm< _TTraits >:: +~Algorithm( ) +{ +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +void fpa::Filters::Algorithm< _TTraits >:: +GenerateData( ) +{ + // Init algorithm + this->InvokeEvent( itk::StartEvent( ) ); + this->_BeforeGenerateData( ); + this->_ConfigureOutputs( ); + this->_PrepareSeeds( this->_GetReferenceInput( ) ); + this->_InitCollisions( this->GetSeeds( ).size( ) ); + + // Init queue + for( TNode seed: this->GetSeeds( ) ) + { + seed.Value = this->m_InitValue; + this->_QueuePush( seed ); + this->InvokeEvent( TEvent( seed.Vertex, seed.FrontId, true ) ); + + } // rof + + // Main loop + while( this->_QueueSize( ) > 0 ) + { + // Get next candidate + TNode node = this->_QueuePop( ); + this->InvokeEvent( TEvent( node.Vertex, node.FrontId, false ) ); + + if( this->_IsNotMarked( node ) ) + { + // Update output value and mark vertex + this->_PostComputeOutputValue( node ); + this->_AssignOutputValue( node ); + this->_Mark( node ); + + // The actual node was effectively marked? + if( node.FrontId > 0 ) + { + // Add neighborhood + TNeighborhood neighbors = this->_GetNeighbors( node.Vertex ); + typename TNeighborhood::const_iterator nIt = neighbors.begin( ); + bool coll = false; + while( nIt != neighbors.end( ) && !coll ) + { + if( this->_IsMarked( *nIt ) ) + { + // Invoke stop at collisions + if( this->_Collisions( node.Vertex, *nIt ) ) + { + this->_QueueClear( ); + coll = true; + + } // fi + } + else + { + TNode nnode; + nnode.Vertex = *nIt; + nnode.Parent = node.Vertex; + nnode.FrontId = node.FrontId; + this->_PreComputeOutputValue( nnode ); + this->_QueuePush( nnode ); + this->InvokeEvent( TEvent( nnode.Vertex, nnode.FrontId, true ) ); + + } // fi + ++nIt; + + } // elihw + + } // fi + + } // fi + this->_Reinitialize( ); + + } // elihw + + // Finish algorithm + this->_AfterGenerateData( ); + this->InvokeEvent( itk::EndEvent( ) ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +void fpa::Filters::Algorithm< _TTraits >:: +_Associate( itk::Object* o ) +{ + if( this->m_AssociatedObjects.insert( o ).second ) + this->Modified( ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +void fpa::Filters::Algorithm< _TTraits >:: +_Deassociate( itk::Object* o ) +{ + std::set< itk::Object* >::iterator i = this->m_AssociatedObjects.find( o ); + if( i != this->m_AssociatedObjects.end( ) ) + { + this->m_AssociatedObjects.erase( i ); + this->Modified( ); + + } // fi +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +void fpa::Filters::Algorithm< _TTraits >:: +_DeassociateAll( ) +{ + if( this->m_AssociatedObjects.size( ) > 0 ) + this->Modified( ); + this->m_AssociatedObjects.clear( ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +void fpa::Filters::Algorithm< _TTraits >:: +_BeforeGenerateData( ) +{ + typedef fpa::Functors::BaseVertexFunction< TVertex > _TFunction; + const itk::DataObject* input = this->GetInput( ); + for( itk::Object* o: this->m_AssociatedObjects ) + { + _TFunction* f = dynamic_cast< _TFunction* >( o ); + if( f != NULL ) + f->SetDataObject( input ); + + } // rof +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +void fpa::Filters::Algorithm< _TTraits >:: +_AfterGenerateData( ) +{ + // Nothing to do at this level +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +typename fpa::Filters::Algorithm< _TTraits >:: +TInputValue fpa::Filters::Algorithm< _TTraits >:: +_GetInputValue( const TNode& n ) const +{ + return( this->_GetInputValue( n.Vertex ) ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +typename fpa::Filters::Algorithm< _TTraits >:: +TOutputValue fpa::Filters::Algorithm< _TTraits >:: +_GetOutputValue( const TNode& n ) const +{ + return( this->_GetOutputValue( n.Vertex ) ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +typename fpa::Filters::Algorithm< _TTraits >:: +TNeighborhood fpa::Filters::Algorithm< _TTraits >:: +_GetNeighbors( const TNode& n ) const +{ + return( this->_GetNeighbors( n.Vertex ) ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +const itk::DataObject* fpa::Filters::Algorithm< _TTraits >:: +_GetReferenceInput( ) const +{ + return( this->GetInput( ) ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +bool fpa::Filters::Algorithm< _TTraits >:: +_IsMarked( const TNode& n ) const +{ + return( this->_IsMarked( n.Vertex ) ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +bool fpa::Filters::Algorithm< _TTraits >:: +_IsNotMarked( const TVertex& v ) const +{ + return( !( this->_IsMarked( v ) ) ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +bool fpa::Filters::Algorithm< _TTraits >:: +_IsNotMarked( const TNode& n ) const +{ + return( this->_IsNotMarked( n.Vertex ) ); +} + +// ------------------------------------------------------------------------- +template< class _TTraits > +void fpa::Filters::Algorithm< _TTraits >:: +_Reinitialize( ) +{ + // Nothing to do at this level +} + +#endif // __fpa__Filters__Algorithm__hxx__ +// eof - $RCSfile$