]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Base/RegionGrow.hxx
...
[FrontAlgorithms.git] / lib / fpa / Base / RegionGrow.hxx
index b0beaf29db7e2fc38477df7a13f95474ec5b5bcb..09fe59a726de35fa761af02f636317ee09db42f6 100644 (file)
+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
+
 #ifndef __fpa__Base__RegionGrow__hxx__
 #define __fpa__Base__RegionGrow__hxx__
 
+#include <queue>
+
 // -------------------------------------------------------------------------
-template< class _TSuperclass >
-fpa::Base::RegionGrow< _TSuperclass >::
-RegionGrow( )
-  : Superclass( ),
-    m_InsideValue( TOutput( 1 ) ),
-    m_OutsideValue( TOutput( 0 ) )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+const typename
+fpa::Base::RegionGrow< _TFilter, _TMarksInterface, _TSeedsInterface >::
+TIntensityFunctor*
+fpa::Base::RegionGrow< _TFilter, _TMarksInterface, _TSeedsInterface >::
+GetIntensityPredicate( ) const
 {
-  this->m_InitResult = TOutput( 0 );
+  return( this->m_IntensityFunctor );
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass >
-fpa::Base::RegionGrow< _TSuperclass >::
-~RegionGrow( )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+const typename
+fpa::Base::RegionGrow< _TFilter, _TMarksInterface, _TSeedsInterface >::
+TVertexFunctor*
+fpa::Base::RegionGrow< _TFilter, _TMarksInterface, _TSeedsInterface >::
+GetVertexPredicate( ) const
 {
+  return( this->m_VertexFunctor );
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass >
-bool fpa::Base::RegionGrow< _TSuperclass >::
-_UpdateValue( _TQueueNode& v, const _TQueueNode& p )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+void
+fpa::Base::RegionGrow< _TFilter, _TMarksInterface, _TSeedsInterface >::
+SetPredicate( TIntensityFunctor* functor )
 {
-  if( this->m_GrowFunction.IsNotNull( ) )
-  {
-    bool in = this->m_GrowFunction->Evaluate( p.Vertex, v.Vertex );
-    v.Result = ( in )? this->m_InsideValue: this->m_OutsideValue;
-    return( in );
-  }
-  else
+  if( this->m_IntensityFunctor.GetPointer( ) != functor )
   {
-    v.Result = this->m_InitResult;
-    return( false );
+    this->m_IntensityFunctor = functor;
+    this->Modified( );
 
   } // fi
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass >
-unsigned long fpa::Base::RegionGrow< _TSuperclass >::
-_QueueSize( ) const
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+void
+fpa::Base::RegionGrow< _TFilter, _TMarksInterface, _TSeedsInterface >::
+SetPredicate( TVertexFunctor* functor )
 {
-  return( this->m_Queue.size( ) );
+  if( this->m_VertexFunctor.GetPointer( ) != functor )
+  {
+    this->m_VertexFunctor = functor;
+    this->Modified( );
+
+  } // fi
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass >
-void fpa::Base::RegionGrow< _TSuperclass >::
-_QueueClear( )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+fpa::Base::RegionGrow< _TFilter, _TMarksInterface, _TSeedsInterface >::
+RegionGrow( )
+  : Superclass( ),
+    _TMarksInterface( this ),
+    _TSeedsInterface( this ),
+    m_InsideValue( TOutputValue( 1 ) ),
+    m_OutsideValue( TOutputValue( 0 ) )
 {
-  while( this->m_Queue.size( ) > 0 )
-    this->m_Queue.pop( );
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass >
-void fpa::Base::RegionGrow< _TSuperclass >::
-_QueuePush( const _TQueueNode& node )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+fpa::Base::RegionGrow< _TFilter, _TMarksInterface, _TSeedsInterface >::
+~RegionGrow( )
 {
-  this->m_Queue.push( node );
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass >
-typename fpa::Base::RegionGrow< _TSuperclass >::
-_TQueueNode fpa::Base::RegionGrow< _TSuperclass >::
-_QueuePop( )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
+void
+fpa::Base::RegionGrow< _TFilter, _TMarksInterface, _TSeedsInterface >::
+GenerateData( )
 {
-  _TQueueNode f = this->m_Queue.front( );
-  this->m_Queue.pop( );
-  return( f );
+  // Init objects
+  this->_ConfigureOutputs( this->m_OutsideValue );
+  this->_InitMarks( this->GetNumberOfSeeds( ) );
+
+  // Init queue
+  typedef std::pair< TVertex, unsigned long > _TNode;
+  std::queue< _TNode > q;
+  unsigned long frontId = 1;
+  typename TSeedsInterface::TSeeds::const_iterator sIt = this->BeginSeeds( );
+  for( ; sIt != this->EndSeeds( ); ++sIt )
+    q.push( _TNode( *sIt, frontId++ ) );
+
+  // Main loop
+  while( q.size( ) > 0 )
+  {
+    // Get next candidate
+    _TNode node = q.front( );
+    q.pop( );
+    if( this->_IsMarked( node.first ) )
+      continue;
+    this->_Mark( node.first, node.second );
+
+    // Apply inclusion predicate
+    TInputValue value = this->_GetInputValue( node.first );
+    bool inside = false;
+    if( this->m_IntensityFunctor.IsNotNull( ) )
+      inside = this->m_IntensityFunctor->Evaluate( value );
+    if( this->m_VertexFunctor.IsNotNull( ) )
+      inside &= this->m_VertexFunctor->Evaluate( node.first );
+    if( !inside )
+      continue;
+
+    // Ok, pixel lays inside region
+    this->_SetOutputValue( node.first, this->m_InsideValue );
+
+    // Add neighborhood
+    TVertices neighbors = this->_GetNeighbors( node.first );
+    typename TVertices::const_iterator neighIt = neighbors.begin( );
+    bool coll = false;
+    while( neighIt != neighbors.end( ) && !coll )
+    {
+      TVertex neigh = *neighIt;
+      if( this->_IsMarked( neigh ) )
+      {
+        // Invoke stop at collisions
+        unsigned long nColl = this->_Collisions( node.first, neigh );
+        if(
+          this->StopAtOneFront( ) &&
+          this->GetNumberOfSeeds( ) > 1 &&
+          nColl == 1
+          )
+        {
+          while( q.size( ) > 0 )
+            q.pop( );
+          coll = true;
+
+        } // fi
+      }
+      else
+        q.push( _TNode( neigh, node.second ) );
+      ++neighIt;
+
+    } // elihw
+
+  } // elihw
+  this->_FreeMarks( );
 }
 
 #endif // __fpa__Base__RegionGrow__hxx__