]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Image/Algorithm.hxx
...
[FrontAlgorithms.git] / lib / fpa / Image / Algorithm.hxx
index 07c09e6a1a628e92739db2455a575212c4db7b31..ba3497f738629f57f09e381616353650b44eb9ac 100644 (file)
-#ifndef __FPA__IMAGE__ALGORITHM__HXX__
-#define __FPA__IMAGE__ALGORITHM__HXX__
+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
 
-#include <itkConstNeighborhoodIterator.h>
+#ifndef __fpa__Image__Algorithm__hxx__
+#define __fpa__Image__Algorithm__hxx__
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+typename
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+TMarks*
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+GetMarks( )
+{
+  return(
+    dynamic_cast< TMarks* >(
+      this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
+      )
+    );
+}
+
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+const typename
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+TMarks*
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+GetMarks( ) const
+{
+  return(
+    dynamic_cast< const TMarks* >(
+      this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
+      )
+    );
+}
+
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
 Algorithm( )
   : Superclass( ),
-    m_NeighborhoodOrder( 1 )
+    m_NeigborhoodOrder( 1 )
 {
+  this->m_MarksIdx = this->GetNumberOfRequiredOutputs( );
+  this->itk::ProcessObject::SetNumberOfRequiredOutputs( this->m_MarksIdx + 1 );
+  this->SetNthOutput( this->m_MarksIdx, TMarks::New( ) );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
 ~Algorithm( )
 {
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_BeforeGenerateData( )
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+typename
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+TNodes fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_UnifySeeds( )
 {
-  this->Superclass::_BeforeGenerateData( );
-  this->AllocateOutputs( );
+  const TInputImage* input = this->GetInput( );
+  typename TInputImage::RegionType region = input->GetRequestedRegion( );
+  TSeeds seeds = this->GetSeeds( );
+  TNodes nodes;
+
+  typename TSeeds::iterator sIt = seeds.begin( );
+  for( ; sIt != seeds.end( ); ++sIt )
+  {
+    TNode node;
+    if( sIt->IsPoint )
+      input->TransformPhysicalPointToIndex( sIt->Point, sIt->Vertex );
+    else
+      input->TransformIndexToPhysicalPoint( sIt->Vertex, sIt->Point );
+    if( region.IsInside( sIt->Vertex ) )
+    {
+      sIt->IsUnified = true;
+      node.Vertex = sIt->Vertex;
+      node.Parent = node.Vertex;
+      if( sIt->FrontId == 0 )
+        node.FrontId = nodes.size( ) + 1;
+      else
+        node.FrontId = sIt->FrontId;
+      nodes.insert( node );
+    }
+    else
+      sIt->IsUnified = false;
+
+  } // rof
+
+  return( nodes );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_InitMarks( )
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+void
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_ConfigureOutput( const TOutputValue& v )
 {
+  const TInputImage* in = this->GetInput( );
+
   TOutputImage* out = this->GetOutput( );
-  this->m_Marks = TMarkImage::New( );
-  this->m_Marks->CopyInformation( out );
-  this->m_Marks->SetRequestedRegion( out->GetRequestedRegion( ) );
-  this->m_Marks->SetBufferedRegion( out->GetBufferedRegion( ) );
-  this->m_Marks->Allocate( );
-  this->m_Marks->FillBuffer( 0 );
+  out->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
+  out->SetRequestedRegion( in->GetRequestedRegion( ) );
+  out->SetBufferedRegion( in->GetBufferedRegion( ) );
+  out->SetSpacing( in->GetSpacing( ) );
+  out->SetOrigin( in->GetOrigin( ) );
+  out->SetDirection( in->GetDirection( ) );
+  out->Allocate( );
+  out->FillBuffer( v );
+
+  TMarks* marks = this->GetMarks( );
+  marks->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
+  marks->SetRequestedRegion( in->GetRequestedRegion( ) );
+  marks->SetBufferedRegion( in->GetBufferedRegion( ) );
+  marks->SetSpacing( in->GetSpacing( ) );
+  marks->SetOrigin( in->GetOrigin( ) );
+  marks->SetDirection( in->GetDirection( ) );
+  marks->Allocate( );
+  marks->FillBuffer( TFrontId( 0 ) );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_InitResults( )
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+typename
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+TNeighborhood fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_GetNeighbors( const TVertex& v ) const
 {
-  this->GetOutput( )->FillBuffer( TScalar( 0 ) );
+  typename TInputImage::RegionType region =
+    this->GetInput( )->GetRequestedRegion( );
+  TNeighborhood neighborhood;
+  if( this->m_NeigborhoodOrder != 1 )
+  {
+    // TODO
+  }
+  else
+  {
+    for( unsigned int d = 0; d < TInputImage::ImageDimension; ++d )
+    {
+      for( int s = -1; s <= 1; s += 2 )
+      {
+        TVertex n = v;
+        n[ d ] += s;
+        if( region.IsInside( n ) )
+          neighborhood.push_back( n );
+
+      } // rof
+
+    } // rof
+
+  } // fi
+  return( neighborhood );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_DeallocateAuxiliary( )
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+typename
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+TInputValue
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_GetInputValue( const TVertex& v ) const
 {
-  this->m_Marks = NULL;
+  return( this->GetInput( )->GetPixel( v ) );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-TFrontId fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_GetMark( const TVertex& v )
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+typename
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+TOutputValue fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_GetOutputValue( const TVertex& v ) const
 {
-  if( this->m_Marks->GetRequestedRegion( ).IsInside( v ) )
-    return( this->m_Marks->GetPixel( v ) );
-  else
-    return( 0 );
+  return( this->GetOutput( )->GetPixel( v ) );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_Visit( const TNode& n )
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+void
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_UpdateOutputValue( TNode& n )
 {
-  if( this->m_Marks->GetRequestedRegion( ).IsInside( n.Vertex ) )
-  {
-    this->m_Marks->SetPixel( n.Vertex, n.FrontId );
-    this->GetOutput( )->SetPixel( n.Vertex, n.Result );
-
-  } // fi
+  this->GetOutput( )->SetPixel( n.Vertex, n.Value );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-TVertices fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_GetNeighborhood( const TVertex& v ) const
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+bool
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_IsMarked( const TVertex& v ) const
 {
-  TVertices neighs;
-  auto reg = this->GetInput( )->GetRequestedRegion( );
-  if( this->m_NeighborhoodOrder == 1 )
-  {
-    for( unsigned int d = 0; d < _TInputImage::ImageDimension; d++ )
-    {
-      for( int i = -1; i <= 1; i += 2 )
-      {
-        TVertex n = v;
-        n[ d ] += i;
-        if( reg.IsInside( n ) )
-          neighs.push_back( n );
-
-      } // rof
-
-    } // rof
-  }
-  else
-  {
-    typedef itk::ConstNeighborhoodIterator< _TInputImage > _TNeighIt;
-    typename _TInputImage::SizeType nSize;
-    nSize.Fill( 1 );
-
-    _TNeighIt nIt( nSize, this->GetInput( ), reg );
-    nIt.SetLocation( v );
-    for( unsigned int i = 0; i < nIt.Size( ); i++ )
-    {
-      TVertex n = nIt.GetIndex( i );
-      if( n == v )
-        continue;
-      if( reg.IsInside( n ) )
-        neighs.push_back( n );
+  return( this->GetMarks( )->GetPixel( v ) > 0 );
+}
 
-    } // rof
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+unsigned long
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_GetMark( const TVertex& v ) const
+{
+  return( ( unsigned long )( this->GetMarks( )->GetPixel( v ) ) );
+}
 
-  } // fi
-  return( neighs );
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+void
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_Mark( const TVertex& v, unsigned long frontId )
+{
+  this->GetMarks( )->SetPixel( v, TFrontId( frontId ) );
 }
 
-#endif // __FPA__IMAGE__ALGORITHM__HXX__
+#endif // __fpa__Image__Algorithm__hxx__
 
 // eof - $RCSfile$