]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Base/Algorithm.hxx
...
[FrontAlgorithms.git] / lib / fpa / Base / Algorithm.hxx
index 1b33897407368a33a32597b639d1b1a53650e784..44d2520da23e996f40197f7cf8484077100ee820 100644 (file)
-#ifndef __FPA__BASE__ALGORITHM__HXX__
-#define __FPA__BASE__ALGORITHM__HXX__
+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
 
-#include <queue>
+#ifndef __fpa__Base__Algorithm__hxx__
+#define __fpa__Base__Algorithm__hxx__
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-const _TVertexCompare fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-TNode::VertexCompare = _TVertexCompare( );
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
+TEvent( )
+  : Superclass( )
+{
+}
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-InvokeEvent( const itk::EventObject& e )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
+TEvent( const TVertex& v, unsigned long fid, bool intoq )
+  : Superclass( ),
+    Vertex( v ),
+    FrontId( fid ),
+    IntoQueue( intoq )
 {
-  if( this->m_ThrowEvents )
-    this->Superclass::InvokeEvent( e );
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-InvokeEvent( const itk::EventObject& e ) const
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
+~TEvent( )
 {
-  if( this->m_ThrowEvents )
-    this->Superclass::InvokeEvent( e );
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-unsigned long fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-GetNumberOfSeeds( ) const
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+const char* 
+fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
+GetEventName( ) const
 {
-  return( this->m_Seeds.size( ) );
+  return( "fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent" );
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-AddSeed( const TVertex& s, const TScalar& v )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+bool
+fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
+CheckEvent( const itk::EventObject* e ) const
 {
-  TNode n;
-  n.Vertex = s;
-  n.Parent = s;
-  n.Result = v;
-  n.Label = Self::FrontLabel;
-  this->AddSeed( n );
+  return( dynamic_cast< const Self* >( e ) != NULL );
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-AddSeed( const TNode& n )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+itk::EventObject* 
+fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
+MakeObject( ) const
 {
-  this->m_Seeds.insert( n );
-  this->Modified( );
+  return( new Self );
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-RemoveSeed( const TVertex& s )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
+InvokeEvent( const itk::EventObject& e )
 {
-  TNode n;
-  n.Vertex = s;
-  typename TNodes::iterator i = this->m_Seeds.find( n );
-  if( i != this->m_Seeds.end( ) )
+  TEvent a;
+  if( a.CheckEvent( &e ) )
   {
-    this->m_Seeds.erase( i );
-    this->Modified( );
-
-  } // fi
+    if( this->m_VisualDebug )
+      this->Superclass::InvokeEvent( e );
+  }
+  else
+    this->Superclass::InvokeEvent( e );
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-RemoveAllSeeds( )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
+InvokeEvent( const itk::EventObject& e ) const
 {
-  this->m_Seeds.clear( );
-  this->Modified( );
+  TEvent a;
+  if( a.CheckEvent( &e ) )
+  {
+    if( this->m_VisualDebug )
+      this->Superclass::InvokeEvent( e );
+  }
+  else
+    this->Superclass::InvokeEvent( e );
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
 Algorithm( )
   : Superclass( ),
-    m_StopAtOneFront( false ),
-    m_ThrowEvents( false )
+    _TMarksInterface( this ),
+    _TSeedsInterface( this ),
+    m_VisualDebug( false )
 {
 }
 
-
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
 ~Algorithm( )
 {
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
 GenerateData( )
 {
-  this->InvokeEvent( TStartEvent( ) );
-  this->_BeforeGenerateData( );
+  this->InvokeEvent( itk::StartEvent( ) );
 
-  unsigned int N = this->m_Seeds.size( );
-  if( N > 0 )
+  // Init objects
+  this->_BeforeGenerateData( );
+  this->_ConfigureOutput( this->m_InitValue );
+  this->_InitMarks( this->GetSeeds( ).size( ) );
+  TNodes seeds = this->_UnifySeeds( );
+  this->_PrepareSeeds( seeds );
+
+  // Init queue
+  this->_QueueInit( );
+  typename TNodes::const_iterator sIt = seeds.begin( );
+  for( ; sIt != seeds.end( ); ++sIt )
   {
-    // Enumerate seeds
-    typename TNodes::iterator nIt = this->m_Seeds.begin( );
-    for( unsigned long i = 1; nIt != this->m_Seeds.end( ); ++nIt, ++i )
-      nIt->FrontId = i;
-
-    // Prepare collisions
-    TCollision coll( TVertex( ), false );
-    TCollisionsRow row( N, coll );
-    this->m_Collisions.clear( );
-    this->m_Collisions.resize( N, row );
-
-    // Put seeds on queue
-    this->_QueueClear( );
-    for( nIt = this->m_Seeds.begin( ); nIt != this->m_Seeds.end( ); ++nIt )
-      this->_QueuePush( *nIt );
+    this->_QueuePush( *sIt );
+    this->InvokeEvent( TEvent( sIt->Vertex, sIt->FrontId, true ) );
 
-    // Init marks and results
-    this->_InitMarks( );
-    this->_InitResults( );
+  } // rof
 
-    // Main loop
-    this->_BeforeLoop( );
-    this->_Loop( );
-    this->_AfterLoop( );
-
-    // Deallocate any memory
-    this->_DeallocateAuxiliary( );
-
-  } // fi
-
-  this->_AfterGenerateData( );
-  this->InvokeEvent( TEndEvent( ) );
-}
-
-// -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-_Loop( )
-{
-  this->InvokeEvent( TStartLoopEvent( ) );
-  this->_BeforeLoop( );
-
-  while( !( this->_IsQueueEmpty( ) ) )
+  // Main loop
+  while( this->_QueueSize( ) > 0 )
   {
-    // Extract next candidate
+    // Get next candidate
     TNode node = this->_QueuePop( );
-
-    // Check if it has been visited
-    if( this->_GetMark( node ) > 0 )
-      continue;
-
-    // Mark it as visited
-    this->_Visit( node );
-    this->InvokeEvent( TAliveEvent( node.Vertex, node.FrontId ) );
-
-    // Check if there is an external stop condition
-    if( this->_NeedToStop( ) )
-    {
-      this->_QueueClear( );
-      continue;
-
-    } // fi
-
-    // Get neighborhood
-    TVertices neighborhood = this->_GetNeighborhood( node );
-
-    // Update neighborhood
-    auto neighIt = neighborhood.begin( );
-    bool stop = false;
-    for( ; neighIt != neighborhood.end( ); ++neighIt )
+    this->InvokeEvent( TEvent( node.Vertex, node.FrontId, false ) );
+    if( !( this->_IsMarked( node.Vertex ) ) )
     {
-      if( this->_GetMark( *neighIt ) == 0 )
+      // Mark it
+      if( this->_Mark( node.Vertex, node.FrontId ) )
       {
-        TNode neigh;
-        neigh.Vertex = *neighIt;
-        neigh.Parent = node.Vertex;
-        neigh.FrontId = node.FrontId;
-        if( this->_Result( neigh, node ) )
+        // Update output value
+        this->_UpdateOutputValue( node );
+
+        // Add neighborhood
+        TNeighborhood neighbors = this->_GetNeighbors( node.Vertex );
+        typename TNeighborhood::const_iterator nIt = neighbors.begin( );
+        bool coll = false;
+        while( nIt != neighbors.end( ) && !coll )
         {
-          this->_QueuePush( neigh );
-          this->InvokeEvent( TFrontEvent( *neighIt, node.FrontId ) );
+          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;
+            nnode.Value = this->_ComputeOutputValue( nnode );
+            this->_QueuePush( nnode );
+            this->InvokeEvent( TEvent( nnode.Vertex, nnode.FrontId, true ) );
+
+          } // fi
+          ++nIt;
+
+        } // elihw
+
+      } // fi
 
-        } // fi
-      }
-      else
-        stop |= this->_UpdateCollisions( node.Vertex, *neighIt );
-
-    } // rof
-
-    if( stop )
-      this->_QueueClear( );
+    } // fi
 
   } // elihw
 
-  this->_AfterLoop( );
-  this->InvokeEvent( TEndLoopEvent( ) );
+  // Finish
+  this->_AfterGenerateData( );
+  this->InvokeEvent( itk::EndEvent( ) );
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
 _BeforeGenerateData( )
 {
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
 _AfterGenerateData( )
 {
 }
 
 // -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-_BeforeLoop( )
-{
-}
-
-// -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-void fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-_AfterLoop( )
-{
-}
-
-// -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-typename fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-TFrontId fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-_GetMark( const TNode& v )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
+_QueueInit( )
 {
-  return( this->_GetMark( v.Vertex ) );
-}
-
-// -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-bool fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-_NeedToStop( )
-{
-  return( false );
-}
-
-// -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-typename fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-TVertices fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-_GetNeighborhood( const TNode& n ) const
-{
-  return( this->_GetNeighborhood( n.Vertex ) );
-}
-
-// -------------------------------------------------------------------------
-template< class _TVertex, class _TScalar, class _TFilter, class _TVertexCompare >
-bool fpa::Base::
-Algorithm< _TVertex, _TScalar, _TFilter, _TVertexCompare >::
-_UpdateCollisions( const TVertex& a, const TVertex& b )
-{
-  auto ma = this->_GetMark( a );
-  auto mb = this->_GetMark( b );
-  if( ma == mb || ma == 0 || mb == 0 )
-    return( false );
-  ma--;
-  mb--;
-
-  // Mark collision, if it is new
-  bool ret = false;
-  bool exists = this->m_Collisions[ ma ][ mb ].second;
-  exists     &= this->m_Collisions[ mb ][ ma ].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;
-
-    // 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 );
-
-      } // elihw
-      ret = ( count == N );
-      this->InvokeEvent( TCollisionEvent( a, ma + 1 ) );
-      this->InvokeEvent( TCollisionEvent( b, mb + 1 ) );
-
-    } // fi
-
-  } // fi
-  return( ret );
+  this->_QueueClear( );
 }
 
-#endif // __FPA__BASE__ALGORITHM__HXX__
+#endif // __fpa__Base__Algorithm__hxx__
 
 // eof - $RCSfile$