-#ifndef __FPA__BASE__ALGORITHM__HXX__
-#define __FPA__BASE__ALGORITHM__HXX__
+#ifndef __fpa__Base__Algorithm__hxx__
+#define __fpa__Base__Algorithm__hxx__
#include <queue>
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-fpa::Base::Algorithm< V, C, R, B >::_TNode::
-_TNode( )
- : Result( TResult( 0 ) ),
- FrontId( -1 ),
- Label( Self::FarLabel )
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::_TQueueNode::
+_TQueueNode( )
{
+ this->Vertex.Fill( 0 );
+ this->Parent.Fill( 0 );
+ this->Result = _TOutput( 0 );
+ this->FrontId = 0;
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-fpa::Base::Algorithm< V, C, R, B >::_TNode::
-~_TNode( )
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::_TQueueNode::
+_TQueueNode( const _TVertex& v )
{
+ this->Vertex = v;
+ this->Parent = v;
+ this->Result = _TOutput( 0 );
+ this->FrontId = 0;
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
-InvokeEvent( const itk::EventObject& e )
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::_TQueueNode::
+_TQueueNode( const _TVertex& v, const _TQueueNode& n )
{
- if( this->m_ThrowEvents )
- this->Superclass::InvokeEvent( e );
+ this->Vertex = v;
+ this->Parent = n.Vertex;
+ this->Result = n.Result;
+ this->FrontId = n.FrontId;
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
-InvokeEvent( const itk::EventObject& e ) const
-{
- if( this->m_ThrowEvents )
- this->Superclass::InvokeEvent( e );
-}
-
-// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
-AddSeed( const TVertex& s, const TResult& r )
-{
- _TNode ns;
- ns.Vertex = s;
- ns.Parent = s;
- ns.Result = r;
- ns.FrontId = this->m_Seeds.size( );
- ns.Label = Self::FrontLabel;
- this->m_Seeds.push_back( ns );
- this->Modified( );
-}
-
-// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-const typename fpa::Base::Algorithm< V, C, R, B >::
-TVertex& fpa::Base::Algorithm< V, C, R, B >::
-GetSeed( const unsigned int& id ) const
-{
- return( this->m_Seeds[ id ].Vertex );
-}
-
-// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
ClearSeeds( )
{
this->m_Seeds.clear( );
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-unsigned long fpa::Base::Algorithm< V, C, R, B >::
-GetNumberOfSeeds( ) 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.size( ) );
+ _TQueueNode node( seed );
+ node.FrontId = this->m_Seeds.size( ) + 1;
+ node.Result = value;
+ this->m_Seeds.push_back( node );
+ this->Modified( );
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
Algorithm( )
: Superclass( ),
- m_ThrowEvents( false ),
+ m_InitResult( _TOutput( 0 ) ),
m_StopAtOneFront( false )
{
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
~Algorithm( )
{
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
GenerateData( )
{
- unsigned long N = this->m_Seeds.size( );
- if( N == 0 )
- return;
-
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( ) );
- this->m_Collisions.clear( );
- this->m_Collisions.
- resize( N, _TCollisionsRow( N, _TCollision( TVertex( ), false ) ) );
- this->_InitResults( );
- this->_InitMarks( );
- this->_InitQueue( );
- this->_Loop( );
+ } // fi
this->_AfterGenerateData( );
this->InvokeEvent( TEndEvent( ) );
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+bool fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_ContinueGenerateData( )
+{
+ return( false );
+}
+
+// -------------------------------------------------------------------------
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
_Loop( )
{
this->InvokeEvent( TStartLoopEvent( ) );
this->_BeforeLoop( );
- while( !( this->_IsQueueEmpty( ) ) )
+ while( this->_ValidLoop( ) )
{
- // Get next candidate
- _TNode candidate = this->_QueuePop( );
- if( this->_Node( candidate.Vertex ).Label == Self::AliveLabel )
- continue;
+ _TQueueNode node = this->_QueuePop( );
+ this->InvokeEvent( TPopEvent( node.Vertex, node.FrontId ) );
- // Mark it as "Alive" and update final result
- candidate.Label = Self::AliveLabel;
- this->_Mark( candidate );
- this->_SetResult( candidate.Vertex, candidate.Result );
- this->InvokeEvent( TAliveEvent( candidate.Vertex, candidate.FrontId ) );
+ // Process actual vertex
+ if( this->_IsMarked( node.Vertex ) )
+ continue;
+ this->_Mark( node );
+ this->_UpdateResult( node );
+ this->InvokeEvent( TMarkEvent( node.Vertex, node.FrontId ) );
- // Check if a forced stop condition arises
- if( !( this->_NeedToStop( ) ) )
+ // Add neighbors to queue
+ TNeighborhood neighs = this->m_NeighborhoodFunction->Evaluate( node.Vertex );
+ for( auto it = neighs.begin( ); it != neighs.end( ); ++it )
{
- // Compute neighborhood
- _TVertices neighborhood;
- this->_Neighborhood( neighborhood, candidate.Vertex );
-
- // Iterate over neighbors
- typename _TVertices::iterator nIt = neighborhood.begin( );
- while( nIt != neighborhood.end( ) )
+ if( !( this->_IsMarked( *it ) ) )
{
- _TNode neighbor = this->_Node( *nIt );
- neighbor.Vertex = *nIt;
- if( neighbor.Label == Self::AliveLabel )
+ _TQueueNode neigh( *it, node );
+ if( this->_UpdateValue( neigh, node ) )
{
- // Update collisions
- if( this->_UpdateCollisions( candidate.Vertex, *nIt ) )
- {
- this->_QueueClear( );
- nIt = neighborhood.end( );
- continue;
-
- } // fi
- }
- else
- {
- // Add new candidate to queue
- if(
- this->_ComputeNeighborResult(
- neighbor.Result, *nIt, candidate.Vertex
- )
- )
- {
- neighbor.FrontId = candidate.FrontId;
- neighbor.Parent = candidate.Vertex;
- neighbor.Label = Self::FrontLabel;
- this->_QueuePush( neighbor );
- this->_Mark( neighbor );
- this->InvokeEvent( TFrontEvent( *nIt, candidate.FrontId ) );
- }
- else
- this->InvokeEvent( TFreezeEvent( *nIt, candidate.FrontId ) );
+ this->_QueuePush( neigh );
+ this->InvokeEvent( TPushEvent( node.Vertex, node.FrontId ) );
} // fi
- ++nIt;
+ }
+ else
+ this->_UpdateCollisions( node.Vertex, *it );
- } // elihw
- }
- else
- this->_QueueClear( );
+ } // rof
} // elihw
this->_AfterLoop( );
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
_BeforeGenerateData( )
{
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
_AfterGenerateData( )
{
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
_BeforeLoop( )
{
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
_AfterLoop( )
{
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-bool fpa::Base::Algorithm< V, C, R, B >::
+template < class _TFilter, class _TVertex, class _TOutput >
+bool fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_ValidLoop( ) const
+{
+ bool r = ( this->_QueueSize( ) > 0 );
+ if( this->m_StopAtOneFront )
+ r &= ( this->m_NumberOfFronts > 0 );
+ return( r );
+}
+
+// -------------------------------------------------------------------------
+template < class _TFilter, class _TVertex, class _TOutput >
+void fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
_UpdateCollisions( const TVertex& a, const TVertex& b )
{
- bool ret = false;
- _TNode na = this->_Node( a );
- _TNode nb = this->_Node( b );
- long fa = na.FrontId;
- long fb = nb.FrontId;
+ auto ma = this->_GetMark( a );
+ auto mb = this->_GetMark( b );
+ if( ma == mb || ma == 0 || mb == 0 )
+ return;
- if( fa != fb )
+ // 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 )
{
- // Mark collision, if it is new
- bool exists = this->m_Collisions[ fa ][ fb ].second;
- exists &= this->m_Collisions[ fb ][ fa ].second;
-
- if( !exists )
+ 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( ) )
{
- this->m_Collisions[ fa ][ fb ].first = na.Vertex;
- this->m_Collisions[ fa ][ fb ].second = true;
- this->m_Collisions[ fb ][ fa ].first = nb.Vertex;
- this->m_Collisions[ fb ][ fa ].second = true;
+ unsigned long f = q.front( );
+ q.pop( );
- // 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 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( );
-
- 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 );
+ if( m[ f ] )
+ continue;
+ m[ f ] = true;
+ count++;
- } // elihw
- ret = ( count == N );
+ for( unsigned int n = 0; n < N; ++n )
+ if( this->m_Collisions[ f ][ n ].second && !m[ n ] )
+ q.push( n );
- } // fi
-
- } // fi
+ } // elihw
+ this->m_NumberOfFronts = N - count;
} // fi
- return( ret );
}
// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-bool fpa::Base::Algorithm< V, C, R, B >::
-_NeedToStop( ) const
+template < class _TFilter, class _TVertex, class _TOutput >
+_TOutput fpa::Base::Algorithm< _TFilter, _TVertex, _TOutput >::
+_GetInputValue( const _TQueueNode& v, const _TQueueNode& p )
{
- return( false );
-}
-
-// -------------------------------------------------------------------------
-template< class V, class C, class R, class B >
-void fpa::Base::Algorithm< V, C, R, B >::
-_InitQueue( )
-{
- this->_QueueClear( );
- for(
- typename _TNodes::const_iterator sIt = this->m_Seeds.begin( );
- sIt != this->m_Seeds.end( );
- sIt++
- )
- {
- this->_QueuePush( *sIt );
- this->_Mark( *sIt );
-
- } // rof
+ _TOutput res = this->m_InitResult;
+ if( this->m_VertexFunction.IsNotNull( ) )
+ res = this->m_VertexFunction->Evaluate( v.Vertex, p.Vertex );
+ if( this->m_ConversionFunction.IsNotNull( ) )
+ res = this->m_ConversionFunction->Evaluate( res );
+ return( res );
}
-#endif // __FPA__BASE__ALGORITHM__HXX__
+#endif // __fpa__Base__Algorithm__hxx__
// eof - $RCSfile$