]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Image/DijkstraWithSphereBacktracking.hxx
Even more tests
[FrontAlgorithms.git] / lib / fpa / Image / DijkstraWithSphereBacktracking.hxx
index 95f8dcb0ac90901ac9026dba88cc7d37e28d5087..346103ba9df5e722649fdef634e497c8e48908e2 100644 (file)
@@ -5,12 +5,42 @@
 #include <itkImageRegionConstIteratorWithIndex.h>
 #include <itkImageRegionIteratorWithIndex.h>
 
+// -------------------------------------------------------------------------
+template< class I, class C >
+typename fpa::Image::DijkstraWithSphereBacktracking< I, C >::
+TMarkImage* fpa::Image::DijkstraWithSphereBacktracking< I, C >::
+GetOutputMarkImage( )
+{
+  return(
+    dynamic_cast< TMarkImage* >(
+      this->itk::ProcessObject::GetOutput( 1 )
+      )
+    );
+}
+
+// -------------------------------------------------------------------------
+template< class I, class C >
+const typename fpa::Image::DijkstraWithSphereBacktracking< I, C >::
+TMarkImage* fpa::Image::DijkstraWithSphereBacktracking< I, C >::
+GetOutputMarkImage( ) const
+{
+  return(
+    dynamic_cast< const TMarkImage* >(
+      this->itk::ProcessObject::GetOutput( 1 )
+      )
+    );
+}
+
 // -------------------------------------------------------------------------
 template< class I, class C >
 fpa::Image::DijkstraWithSphereBacktracking< I, C >::
 DijkstraWithSphereBacktracking( )
-  : Superclass( )
+  : Superclass( ),
+    m_NumberOfBranches( 0 )
 {
+  this->SetNumberOfRequiredOutputs( 2 );
+  this->SetNthOutput( 0, I::New( ) );
+  this->SetNthOutput( 1, TMarkImage::New( ) );
 }
 
 // -------------------------------------------------------------------------
@@ -67,8 +97,6 @@ template< class I, class C >
 void fpa::Image::DijkstraWithSphereBacktracking< I, C >::
 _AfterMainLoop( )
 {
-  typedef itk::Image< bool, I::ImageDimension > _TMarkImage;
-
   // Finish base algorithm
   this->Superclass::_AfterMainLoop( );
   this->m_FinalTree.clear( );
@@ -88,25 +116,19 @@ _AfterMainLoop( )
   max_spac *= double( 3 );
 
   // Prepare an object to hold marks
-  typename _TMarkImage::Pointer marks = _TMarkImage::New( );
-  marks->SetLargestPossibleRegion( input->GetLargestPossibleRegion( ) );
-  marks->SetRequestedRegion( input->GetRequestedRegion( ) );
-  marks->SetBufferedRegion( input->GetBufferedRegion( ) );
-  marks->SetOrigin( input->GetOrigin( ) );
-  marks->SetSpacing( input->GetSpacing( ) );
-  marks->SetDirection( input->GetDirection( ) );
-  marks->Allocate( );
-  marks->FillBuffer( false );
+  typename TMarkImage::Pointer marks = this->GetOutputMarkImage( );
+  marks->FillBuffer( 0 );
 
   // Iterate over the candidates, starting from the candidates that
   // are near thin branches
   typename _TCandidates::const_reverse_iterator cIt =
     this->m_Candidates.rbegin( );
-  for( int backId = 0; cIt != this->m_Candidates.rend( ); ++cIt )
+  this->m_NumberOfBranches = 0;
+  for( ; cIt != this->m_Candidates.rend( ); ++cIt )
   {
     // If pixel hasn't been visited, continue
     TVertex v = cIt->second;
-    if( marks->GetPixel( v ) )
+    if( marks->GetPixel( v ) != 0 )
       continue;
 
     // Compute nearest start candidate
@@ -128,46 +150,81 @@ _AfterMainLoop( )
     } // rof
 
     // Keep endpoint
-    if( marks->GetPixel( max_vertex ) )
+    if( marks->GetPixel( max_vertex ) != 0 )
       continue;
-    backId++;
+    this->m_NumberOfBranches++;
     this->m_EndPoints.push_back( max_vertex );
 
     // Construct path (at least the part that hasn't been iterated)
-    bool terminate = false;
     do
     {
-      if( this->m_FinalTree.find( max_vertex ) == this->m_FinalTree.end( ) )
+      if(
+        std::find(
+          this->m_BifurcationPoints.begin( ),
+          this->m_BifurcationPoints.end( ),
+          max_vertex
+          ) == this->m_BifurcationPoints.end( )
+        )
       {
         // Mark a sphere around current point as visited
         double dist = std::sqrt( double( input->GetPixel( max_vertex ) ) );
-        region = this->_Region( max_vertex, dist * double( 1.25 ) );
-        itk::ImageRegionIteratorWithIndex< _TMarkImage >
+        region = this->_Region( max_vertex, dist * double( 1.1 ) );
+        itk::ImageRegionIteratorWithIndex< TMarkImage >
           mIt( marks, region );
         for( mIt.GoToBegin( ); !mIt.IsAtEnd( ); ++mIt )
-          mIt.Set( true );
+          mIt.Set( this->m_NumberOfBranches );
 
         // Next vertex in current path
-        this->InvokeEvent( TBacktrackingEvent( max_vertex, backId ) );
+        this->InvokeEvent( TBacktrackingEvent( max_vertex, this->m_NumberOfBranches ) );
         this->m_FinalTree[ max_vertex ] = this->_Parent( max_vertex );
       }
       else
       {
         // A bifurcation point has been reached!
         this->m_BifurcationPoints.push_back( max_vertex );
-        terminate = true;
+        this->m_NumberOfBranches++;
 
       } // fi
       max_vertex = this->_Parent( max_vertex );
 
-    } while( max_vertex != this->_Parent( max_vertex ) && !terminate );
-
-    if( !terminate )
-    {
-      this->m_FinalTree[ max_vertex ] = max_vertex;
-      this->InvokeEvent( TEndBacktrackingEvent( backId ) );
-
-    } // fi
+    } while( max_vertex != this->_Parent( max_vertex ) );
+
+    /* TODO
+       bool terminate = false;
+       do
+       {
+       if( this->m_FinalTree.find( max_vertex ) == this->m_FinalTree.end( ) )
+       {
+       // Mark a sphere around current point as visited
+       double dist = std::sqrt( double( input->GetPixel( max_vertex ) ) );
+       region = this->_Region( max_vertex, dist * double( 1.25 ) );
+       itk::ImageRegionIteratorWithIndex< TMarkImage >
+       mIt( marks, region );
+       for( mIt.GoToBegin( ); !mIt.IsAtEnd( ); ++mIt )
+       mIt.Set( true );
+
+       // Next vertex in current path
+       this->InvokeEvent( TBacktrackingEvent( max_vertex, this->m_NumberOfBranches ) );
+       this->m_FinalTree[ max_vertex ] = this->_Parent( max_vertex );
+       }
+       else
+       {
+       // A bifurcation point has been reached!
+       this->m_BifurcationPoints.push_back( max_vertex );
+       terminate = true;
+
+       } // fi
+       max_vertex = this->_Parent( max_vertex );
+
+       } while( max_vertex != this->_Parent( max_vertex ) && !terminate );
+
+       if( !terminate )
+       {
+       this->m_FinalTree[ max_vertex ] = max_vertex;
+       this->InvokeEvent( TEndBacktrackingEvent( this->m_NumberOfBranches ) );
+
+       } // fi
+    */
 
   } // rof
 }