]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Image/Algorithm.hxx
...
[FrontAlgorithms.git] / lib / fpa / Image / Algorithm.hxx
index 3c33e4e3ca90019e7ac77b58c1c8ae9b541948cc..9ac7548b55c2fbe729b2544f85795de31d541bb2 100644 (file)
+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
+
 #ifndef __fpa__Image__Algorithm__hxx__
 #define __fpa__Image__Algorithm__hxx__
 
-// Send Piotr's code to Anna
+// -------------------------------------------------------------------------
+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 )
+      )
+    );
+}
 
-#include <itkImage.h>
-#include <fpa/Image/Functors/SimpleNeighborhood.h>
+// -------------------------------------------------------------------------
+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 >
-fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
 Algorithm( )
-  : Superclass( )
+  : Superclass( ),
+    m_NeigborhoodOrder( 1 )
 {
-  typedef fpa::Image::Functors::SimpleNeighborhood< _TInputImage::ImageDimension > _TNeigh;
-  typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
-
   this->m_MarksIdx = this->GetNumberOfRequiredOutputs( );
   this->itk::ProcessObject::SetNumberOfRequiredOutputs( this->m_MarksIdx + 1 );
-  typename _TMarks::Pointer marks = _TMarks::New( );
-  this->SetNthOutput( this->m_MarksIdx, marks );
-
-  typename _TNeigh::Pointer neigh = _TNeigh::New( );
-  this->SetNeighborhoodFunction( neigh );
+  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( );
-
-  TNeighborhoodFunction* neighFunc =
-    dynamic_cast< TNeighborhoodFunction* >(
-      this->GetNeighborhoodFunction( )
-      );
-  if( neighFunc == NULL )
-    itkExceptionMacro( << "NeighborhoodFunction not well defined." );
-  neighFunc->SetImage( this->GetInput( ) );
-
-  TVertexFunction* vertexFunc =
-    dynamic_cast< TVertexFunction* >(
-      this->GetVertexFunction( )
-      );
-  if( vertexFunc != NULL )
-    vertexFunc->SetImage( this->GetInput( ) );
+  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 )
 {
-  typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
-  _TMarks* marks =
-    dynamic_cast< _TMarks* >(
-      this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
-      );
-  marks->FillBuffer( 0 );
+  const TInputImage* in = this->GetInput( );
+
+  TOutputImage* out = this->GetOutput( );
+  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( const TOutput& init_value )
+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( init_value );
+  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 >
-bool fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_IsMarked( const TVertex& v ) const
+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
 {
-  typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
-  const _TMarks* marks =
-    dynamic_cast< const _TMarks* >(
-      this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
-      );
-  return( marks->GetPixel( v ) != 0 );
+  return( this->GetInput( )->GetPixel( v ) );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_Mark( const _TQueueNode& n )
+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
 {
-  typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
-  _TMarks* marks =
-    dynamic_cast< _TMarks* >(
-      this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
-      );
-  marks->SetPixel( n.Vertex, n.FrontId );
+  return( this->GetOutput( )->GetPixel( v ) );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-TFrontId fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_GetMark( const TVertex& v ) const
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+void
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_UpdateOutputValue( const TNode& n )
 {
-  typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
-  const _TMarks* marks =
-    dynamic_cast< const _TMarks* >(
-      this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
-      );
-  return( marks->GetPixel( v ) );
+  this->GetOutput( )->SetPixel( n.Vertex, n.Value );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_UpdateResult( const _TQueueNode& n )
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+bool
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_IsMarked( const TVertex& v ) const
 {
-  this->GetOutput( )->SetPixel( n.Vertex, n.Result );
+  return( this->GetMarks( )->GetPixel( v ) > 0 );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::TOutput
-fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_GetResult( const TVertex& v ) const
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+unsigned long
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_GetMark( const TVertex& v ) const
 {
-  if( this->GetOutput( )->GetLargestPossibleRegion( ).IsInside( v ) )
-    return( this->GetOutput( )->GetPixel( v ) );
-  else
-    return( this->m_InitResult );
+  return( ( unsigned long )( this->GetMarks( )->GetPixel( v ) ) );
 }
 
 // -------------------------------------------------------------------------
-template< class _TInputImage, class _TOutputImage >
-unsigned int fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
-_GetNumberOfDimensions( ) const
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+bool
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_Mark( const TVertex& v, unsigned long frontId )
 {
-  return( _TInputImage::ImageDimension );
+  this->GetMarks( )->SetPixel( v, TFrontId( frontId ) );
+  return( true );
 }
 
 #endif // __fpa__Image__Algorithm__hxx__