-#ifndef __FPA__BASE__ALGORITHM__HXX__
-#define __FPA__BASE__ALGORITHM__HXX__
+#ifndef __fpa__Base__Algorithm__hxx__
+#define __fpa__Base__Algorithm__hxx__
-#include <algorithm>
-#include <limits>
#include <queue>
// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
-InvokeEvent( const itk::EventObject& e )
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::_TQueueNode::
+_TQueueNode( )
{
- if( this->m_ThrowEvents )
- this->Superclass::InvokeEvent( e );
+ this->Vertex.Fill( 0 );
+ this->Parent.Fill( 0 );
+ this->Result = _TOutput( 0 );
+ this->FrontId = 0;
}
// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
-InvokeEvent( const itk::EventObject& e ) const
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::_TQueueNode::
+_TQueueNode( const _TVertex& v )
{
- if( this->m_ThrowEvents )
- this->Superclass::InvokeEvent( e );
+ this->Vertex = v;
+ this->Parent = v;
+ this->Result = _TOutput( 0 );
+ this->FrontId = 0;
}
// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
-AddSeed( const TVertex& s, const TResult& v )
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::_TQueueNode::
+_TQueueNode( const _TVertex& v, const _TQueueNode& n )
{
- this->m_Seeds.push_back( _TNode( s, v, this->m_Seeds.size( ) ) );
- this->Modified( );
+ this->Vertex = v;
+ this->Parent = n.Vertex;
+ this->Result = n.Result;
+ this->FrontId = n.FrontId;
}
// -------------------------------------------------------------------------
-template< class T, class B >
-const typename fpa::Base::Algorithm< T, B >::
-TVertex& fpa::Base::Algorithm< T, B >::
-GetSeed( const unsigned long& i ) const
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+AddSeed( const _TVertex& seed, const TOutput& value )
{
- return( this->m_Seeds[ i ].Vertex );
-}
-
-// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
-ClearSeeds( )
-{
- this->m_Seeds.clear( );
+ _TQueueNode node( seed );
+ node.FrontId = this->m_Seeds.size( ) + 1;
+ node.Result = value;
+ this->m_Seeds.push_back( node );
this->Modified( );
}
// -------------------------------------------------------------------------
-template< class T, class B >
-unsigned long fpa::Base::Algorithm< T, B >::
-GetNumberOfSeeds( ) const
-{
- return( this->m_Seeds.size( ) );
-}
-
-// -------------------------------------------------------------------------
-template< class T, class B >
-fpa::Base::Algorithm< T, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
Algorithm( )
: Superclass( ),
- m_StopAtOneFront( false ),
- m_ThrowEvents( false )
+ m_InitResult( _TOutput( 0 ) ),
+ m_StopAtOneFront( false )
{
}
// -------------------------------------------------------------------------
-template< class T, class B >
-fpa::Base::Algorithm< T, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
~Algorithm( )
{
}
// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
GenerateData( )
{
- this->AllocateOutputs( );
-
- unsigned long N = this->m_Seeds.size( );
- if( N == 0 )
- return;
- this->m_CollisionSites.clear( );
- this->m_CollisionSites.
- resize( N, _TCollisionSitesRow( N, _TCollision( TVertex( ), false ) ) );
- this->_BeforeLoop( );
- this->_Loop( );
- this->_AfterLoop( );
-}
+ this->InvokeEvent( TStartEvent( ) );
+ this->_BeforeGenerateData( );
+ this->m_NumberOfFronts = this->m_Seeds.size( );
+ if( this->m_NumberOfFronts > 0 )
+ {
+ // Prepare collisions
+ TCollision coll( TVertex( ), false );
+ TCollisionsRow row( this->m_NumberOfFronts, coll );
+ this->m_Collisions.clear( );
+ this->m_Collisions.resize( this->m_NumberOfFronts, row );
+
+ // Put seeds on queue
+ this->_QueueClear( );
+ for(
+ auto nIt = this->m_Seeds.begin( );
+ nIt != this->m_Seeds.end( );
+ ++nIt
+ )
+ this->_QueuePush( *nIt );
+
+ // Init marks and results
+ this->_InitMarks( );
+ this->_InitResults( this->m_InitResult );
+
+ // Main loop
+ do
+ this->_Loop( );
+ while( this->_ContinueGenerateData( ) );
-// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
-_BeforeLoop( )
-{
+ } // fi
+ this->_AfterGenerateData( );
+ this->InvokeEvent( TEndEvent( ) );
}
// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
-_AfterLoop( )
+template < class _TFilter, class _TVertex, class _TOutput >
+bool fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_ContinueGenerateData( )
{
+ return( false );
}
// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
_Loop( )
{
- this->_InitializeMarks( );
- this->_InitializeResults( );
- this->_InitializeQueue( );
- while( !( this->_IsQueueEmpty( ) ) )
+ this->InvokeEvent( TStartLoopEvent( ) );
+ this->_BeforeLoop( );
+ while( this->_ValidLoop( ) )
{
- _TNode n = this->_QueuePop( );
- if( this->_IsMarked( n.Vertex ) )
+ _TQueueNode node = this->_QueuePop( );
+ this->InvokeEvent( TPopEvent( node.Vertex, node.FrontId ) );
+
+ // Process actual vertex
+ if( this->_IsMarked( node.Vertex ) )
continue;
+ this->_Mark( node );
+ this->_UpdateResult( node );
+ this->InvokeEvent( TMarkEvent( node.Vertex, node.FrontId ) );
- this->_Mark( n );
- this->InvokeEvent( TMarkEvent( n ) );
- if( this->_UpdateResult( n ) )
+ // Add neighbors to queue
+ TNeighborhood neighs = this->m_NeighborhoodFunction->Evaluate( node.Vertex );
+ for( auto it = neighs.begin( ); it != neighs.end( ); ++it )
{
- if( !( this->_CheckStopCondition( ) ) )
+ if( !( this->_IsMarked( *it ) ) )
{
- _TNodes N;
- this->_Neighs( n, N );
- typename _TNodes::iterator nnIt = N.begin( );
- while( nnIt != N.end( ) )
+ _TQueueNode neigh( *it, node );
+ if( this->_UpdateValue( neigh, node ) )
{
- if( this->_IsMarked( nnIt->Vertex ) )
- {
- // Update real front identifier
- nnIt->FrontId = this->_FrontId( nnIt->Vertex );
-
- // Update collisions
- if( this->_CheckCollisions( n, *nnIt ) )
- {
- if( this->m_StopAtOneFront )
- {
- this->_QueueClear( );
- nnIt = N.end( );
-
- } // fi
-
- } // fi
- }
- else
- {
- if( this->_UpdateNeigh( *nnIt, n ) )
- {
- nnIt->Parent = n.Vertex;
- this->_QueuePush( *nnIt );
- this->InvokeEvent( TFrontEvent( *nnIt ) );
-
- } // fi
-
- } // fi
- if( nnIt != N.end( ) )
- nnIt++;
-
- } // elihw
+ this->_QueuePush( neigh );
+ this->InvokeEvent( TPushEvent( node.Vertex, node.FrontId ) );
+
+ } // fi
}
else
- this->_QueueClear( );
+ this->_UpdateCollisions( node.Vertex, *it );
- } // fi
+ } // rof
} // elihw
- this->InvokeEvent( TEndEvent( ) );
+ this->_AfterLoop( );
+ this->InvokeEvent( TEndLoopEvent( ) );
}
// -------------------------------------------------------------------------
-template< class T, class B >
-bool fpa::Base::Algorithm< T, B >::
-_CheckCollisions( const _TNode& a, const _TNode& b )
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_BeforeGenerateData( )
{
- bool ret = false;
- if( a.FrontId != b.FrontId )
- {
- // Mark collision, if it is new
- bool exists = this->m_CollisionSites[ a.FrontId ][ b.FrontId ].second;
- exists &= this->m_CollisionSites[ b.FrontId ][ a.FrontId ].second;
- if( !exists )
- {
- this->m_CollisionSites[ a.FrontId ][ b.FrontId ].first = a.Vertex;
- this->m_CollisionSites[ a.FrontId ][ b.FrontId ].second = true;
- this->m_CollisionSites[ b.FrontId ][ a.FrontId ].first = b.Vertex;
- this->m_CollisionSites[ b.FrontId ][ a.FrontId ].second = true;
-
- // Stop if one front is desired
- if( this->m_StopAtOneFront )
- {
- // Perform a depth-first iteration on front graph
- unsigned long N = this->GetNumberOfSeeds( );
- unsigned long C = 0;
- std::vector< bool > m( N, false );
- std::queue< unsigned long > q;
- q.push( 0 );
- while( !q.empty( ) )
- {
- unsigned long f = q.front( );
- q.pop( );
-
- if( m[ f ] )
- continue;
- m[ f ] = true;
- C++;
-
- for( unsigned int n = 0; n < N; ++n )
- if( this->m_CollisionSites[ f ][ n ].second && !m[ n ] )
- q.push( n );
-
- } // elihw
- ret = ( C == N );
-
- } // fi
-
- } // fi
-
- } // fi
- return( ret );
}
// -------------------------------------------------------------------------
-template< class T, class B >
-bool fpa::Base::Algorithm< T, B >::
-_CheckStopCondition( )
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_AfterGenerateData( )
{
- return( false );
}
// -------------------------------------------------------------------------
-template< class T, class B >
-bool fpa::Base::Algorithm< T, B >::
-_UpdateResult( _TNode& n )
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_BeforeLoop( )
{
- return( true );
}
// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
-_InitializeMarks( )
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_AfterLoop( )
{
- this->m_Marks.clear( );
}
// -------------------------------------------------------------------------
-template< class T, class B >
-bool fpa::Base::Algorithm< T, B >::
-_IsMarked( const TVertex& v ) const
+template < class _TFilter, class _TVertex, class _TOutput >
+bool fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_ValidLoop( ) const
{
- return( this->m_Marks.find( v ) != this->m_Marks.end( ) );
+ bool r = ( this->_QueueSize( ) > 0 );
+ if( this->m_StopAtOneFront )
+ r &= ( this->m_NumberOfFronts > 0 );
+ return( r );
}
// -------------------------------------------------------------------------
-template< class T, class B >
-typename fpa::Base::Algorithm< T, B >::
-_TFrontId fpa::Base::Algorithm< T, B >::
-_FrontId( const TVertex& v ) const
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_UpdateCollisions( const TVertex& a, const TVertex& b )
{
- typename _TMarks::const_iterator mIt = this->m_Marks.find( v );
- if( mIt != this->m_Marks.end( ) )
- return( mIt->second.FrontId );
- else
- return( std::numeric_limits< _TFrontId >::max( ) );
-}
+ auto ma = this->_GetMark( a );
+ auto mb = this->_GetMark( b );
+ if( ma == mb || ma == 0 || mb == 0 )
+ return;
-// -------------------------------------------------------------------------
-template< class T, class B >
-typename fpa::Base::Algorithm< T, B >::
-TVertex fpa::Base::Algorithm< T, B >::
-_Parent( const TVertex& v ) const
-{
- typename _TMarks::const_iterator mIt = this->m_Marks.find( v );
- if( mIt == this->m_Marks.end( ) )
+ // Mark collision, if it is new
+ ma--; mb--;
+ bool ret = false;
+ bool exists = this->m_Collisions[ ma ][ mb ].second;
+ exists &= this->m_Collisions[ mb ][ ma ].second;
+ if( !exists )
{
- TVertex v;
- return( v );
- }
- else
- return( mIt->second.Parent );
-}
+ this->m_Collisions[ ma ][ mb ].first = a;
+ this->m_Collisions[ ma ][ mb ].second = true;
+ this->m_Collisions[ mb ][ ma ].first = b;
+ this->m_Collisions[ mb ][ ma ].second = true;
+
+ // Update number of fronts
+ unsigned long N = this->m_Seeds.size( );
+ unsigned long count = 0;
+ std::vector< bool > m( N, false );
+ std::queue< unsigned long > q;
+ q.push( 0 );
+ while( !q.empty( ) )
+ {
+ unsigned long f = q.front( );
+ q.pop( );
-// -------------------------------------------------------------------------
-template< class T, class B >
-void fpa::Base::Algorithm< T, B >::
-_Mark( const _TNode& n )
-{
- this->m_Marks[ n.Vertex ] = n;
+ if( m[ f ] )
+ continue;
+ m[ f ] = true;
+ count++;
+
+ for( unsigned int n = 0; n < N; ++n )
+ if( this->m_Collisions[ f ][ n ].second && !m[ n ] )
+ q.push( n );
+
+ } // elihw
+ this->m_NumberOfFronts = N - count;
+
+ } // fi
}
-#endif // __FPA__BASE__ALGORITHM__HXX__
+#endif // __fpa__Base__Algorithm__hxx__
// eof - $RCSfile$