]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Base/Dijkstra.hxx
...
[FrontAlgorithms.git] / lib / fpa / Base / Dijkstra.hxx
index df9c75e5d07cbc994b5a4db3966bd879eabdfcc4..b7c10f478a2d6eee59fa04fbc08b0501ca23b4e6 100644 (file)
+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
+
 #ifndef __fpa__Base__Dijkstra__hxx__
 #define __fpa__Base__Dijkstra__hxx__
 
-#include <algorithm>
-#include <limits>
-
 // -------------------------------------------------------------------------
-template< class _TSuperclass, class _TMST >
-_TMST* fpa::Base::Dijkstra< _TSuperclass, _TMST >::
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface, class _TMST >
+typename
+fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+TMST* fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
 GetMinimumSpanningTree( )
 {
   return(
-    dynamic_cast< _TMST* >(
+    dynamic_cast< TMST* >(
       this->itk::ProcessObject::GetOutput( this->m_MSTIndex )
       )
     );
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass, class _TMST >
-const _TMST* fpa::Base::Dijkstra< _TSuperclass, _TMST >::
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface, class _TMST >
+const typename
+fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+TMST* fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
 GetMinimumSpanningTree( ) const
 {
   return(
-    dynamic_cast< const _TMST* >(
+    dynamic_cast< const TMST* >(
       this->itk::ProcessObject::GetOutput( this->m_MSTIndex )
       )
     );
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass, class _TMST >
-fpa::Base::Dijkstra< _TSuperclass, _TMST >::
-Dijkstra( )
-  : Superclass( )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface, class _TMST >
+const typename
+fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+TIntensityFunctor*
+fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+GetIntensityFunctor( ) const
 {
-  this->m_InitResult = TOutput( 0 );
-  this->m_MSTIndex = this->GetNumberOfRequiredOutputs( );
-  this->SetNumberOfRequiredOutputs( this->m_MSTIndex + 1 );
-  this->itk::ProcessObject::SetNthOutput( this->m_MSTIndex, _TMST::New( ) );
+  return( this->m_IntensityFunctor );
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass, class _TMST >
-fpa::Base::Dijkstra< _TSuperclass, _TMST >::
-~Dijkstra( )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface, class _TMST >
+const typename
+fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+TVertexFunctor*
+fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+GetVertexFunctor( ) const
 {
+  return( this->m_VertexFunctor );
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass, class _TMST >
-void fpa::Base::Dijkstra< _TSuperclass, _TMST >::
-_AfterGenerateData( )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface, class _TMST >
+void fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+SetFunctor( TIntensityFunctor* functor )
 {
-  this->Superclass::_AfterGenerateData( );
+  if( this->m_IntensityFunctor.GetPointer( ) != functor )
+  {
+    this->m_IntensityFunctor = functor;
+    this->Modified( );
 
-  auto mst = this->GetMinimumSpanningTree( );
-  mst->ClearSeeds( );
-  for( auto s = this->m_Seeds.begin( ); s != this->m_Seeds.end( ); ++s )
-    mst->AddSeed( s->Vertex );
-  mst->SetCollisions( this->m_Collisions );
+  } // fi
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass, class _TMST >
-void fpa::Base::Dijkstra< _TSuperclass, _TMST >::
-_UpdateResult( const _TQueueNode& n )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface, class _TMST >
+void fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+SetFunctor( TVertexFunctor* functor )
 {
-  this->Superclass::_UpdateResult( n );
-  this->GetMinimumSpanningTree( )->SetParent( n.Vertex, n.Parent );
+  if( this->m_VertexFunctor.GetPointer( ) != functor )
+  {
+    this->m_VertexFunctor = functor;
+    this->Modified( );
+
+  } // fi
 }
 
 // -------------------------------------------------------------------------
-template< class _TSuperclass, class _TMST >
-bool fpa::Base::Dijkstra< _TSuperclass, _TMST >::
-_UpdateValue( _TQueueNode& v, const _TQueueNode& p )
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface, class _TMST >
+fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+Dijkstra( )
+  : Superclass( ),
+    _TMarksInterface( this ),
+    _TSeedsInterface( this )
 {
-  v.Result = this->_GetInputValue( p.Vertex, v.Vertex );
-  if( v.Result >= TOutput( 0 ) )
-  {
-    v.Result += p.Result;
-    return( true );
-  }
-  else
+  this->m_MSTIndex = this->GetNumberOfRequiredOutputs( );
+  this->SetNumberOfRequiredOutputs( this->m_MSTIndex + 1 );
+  this->itk::ProcessObject::SetNthOutput( this->m_MSTIndex, TMST::New( ) );
+}
+
+// -------------------------------------------------------------------------
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface, class _TMST >
+fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+~Dijkstra( )
+{
+}
+
+// -------------------------------------------------------------------------
+template< class _TFilter, class _TMarksInterface, class _TSeedsInterface, class _TMST >
+void fpa::Base::Dijkstra< _TFilter, _TMarksInterface, _TSeedsInterface, _TMST >::
+GenerateData( )
+{
+  // Init objects
+  this->_ConfigureOutputs( std::numeric_limits< TOutputValue >::max( ) );
+  this->_InitMarks( this->GetNumberOfSeeds( ) );
+  TMST* mst = this->GetMinimumSpanningTree( );
+
+  // Init queue
+  std::vector< _TNode > q;
+  unsigned long frontId = 1;
+  typename TSeedsInterface::TSeeds::const_iterator sIt = this->BeginSeeds( );
+  for( ; sIt != this->EndSeeds( ); ++sIt )
+    q.push_back( _TNode( *sIt, *sIt, frontId++ ) );
+
+  // Main loop
+  while( q.size( ) > 0 )
   {
-    v.Result = this->m_InitResult;
-    return( false );
+    // Get next candidate
+    std::pop_heap( q.begin( ), q.end( ) );
+    _TNode node = q.back( );
+    q.pop_back( );
+    if( this->_IsMarked( node.Vertex ) )
+      continue;
+    this->_Mark( node.Vertex, node.FrontId );
 
-  } // fi
+    // Ok, pixel lays inside region
+    this->_SetOutputValue( node.Vertex, node.Cost );
+    mst->SetParent( node.Vertex, node.Parent );
+
+    // Add neighborhood
+    TVertices neighbors = this->_GetNeighbors( node.Vertex );
+    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.Vertex, neigh );
+        if(
+          this->StopAtOneFront( ) &&
+          this->GetNumberOfSeeds( ) > 1 &&
+          nColl == 1
+          )
+        {
+          q.clear( );
+          coll = true;
+
+        } // fi
+      }
+      else
+      {
+        // Compute new cost
+        TOutputValue ncost =
+          this->m_VertexFunctor->Evaluate( neigh, node.Vertex );
+        if( this->m_IntensityFunctor.IsNotNull( ) )
+          ncost = this->m_IntensityFunctor->Evaluate( ncost );
+
+        // This algorithm only supports positive values
+        if( ncost >= TOutputValue( 0 ) )
+        {
+          // Insert new node
+          _TNode nn( neigh, node.Vertex, node.FrontId );
+          nn.Cost = node.Cost + ncost;
+          q.push_back( nn );
+          std::push_heap( q.begin( ), q.end( ) );
+
+        } // fi
+
+      } // fi
+      ++neighIt;
+
+    } // elihw
+
+  } // elihw
+  this->_FreeMarks( );
+
+  // Complete data into minimum spanning tree
+  mst->ClearSeeds( );
+  mst->SetCollisions( this->m_Collisions );
+  for( sIt = this->BeginSeeds( ); sIt != this->EndSeeds( ); ++sIt )
+    mst->AddSeed( *sIt );
 }
 
 #endif // __fpa__Base__Dijkstra__hxx__