]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Image/Algorithm.hxx
...
[FrontAlgorithms.git] / lib / fpa / Image / Algorithm.hxx
index 7800a549576e7f378b1454ed39013d5996c72cff..98592f63d21c375268ff7d1e5fa68ae56f2dd550 100644 (file)
-#ifndef __FPA__IMAGE__ALGORITHM__HXX__
-#define __FPA__IMAGE__ALGORITHM__HXX__
+// =========================================================================
+// @author Leonardo Florez Valencia
+// @email florez-l@javeriana.edu.co
+// =========================================================================
 
-#include <cmath>
-#include <limits>
-#include <itkConstNeighborhoodIterator.h>
+#ifndef __fpa__Image__Algorithm__hxx__
+#define __fpa__Image__Algorithm__hxx__
 
 // -------------------------------------------------------------------------
-template< class I, class A >
-fpa::Image::Algorithm< I, A >::
-Algorithm( )
-  : Superclass( ),
-    m_NeighborhoodOrder( 1 )
-{
-}
-
-// -------------------------------------------------------------------------
-template< class I, class A >
-fpa::Image::Algorithm< I, A >::
-~Algorithm( )
+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 I, class A >
-bool fpa::Image::Algorithm< I, A >::
-_UpdateResult( _TNode& n )
-{
-  bool ret = this->Superclass::_UpdateResult( n );
-  this->GetOutput( )->SetPixel( n.Vertex, n.Result );
-  return( ret );
-}
-
-// -------------------------------------------------------------------------
-template< class I, class A >
-unsigned long fpa::Image::Algorithm< I, A >::
-_NumberOfVertices( ) const
+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(
-    this->GetInput( )->GetLargestPossibleRegion( ).GetNumberOfPixels( )
+    dynamic_cast< const TMarks* >(
+      this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
+      )
     );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class A >
-typename fpa::Image::Algorithm< I, A >::
-TVertexValue fpa::Image::Algorithm< I, A >::
-_Value( const TVertex& v ) const
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+Algorithm( )
+  : Superclass( ),
+    m_NeigborhoodOrder( 1 )
 {
-  return( this->GetInput( )->GetPixel( v ) );
+  this->m_MarksIdx = this->GetNumberOfRequiredOutputs( );
+  this->itk::ProcessObject::SetNumberOfRequiredOutputs( this->m_MarksIdx + 1 );
+  this->SetNthOutput( this->m_MarksIdx, TMarks::New( ) );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class A >
-typename fpa::Image::Algorithm< I, A >::
-TResult fpa::Image::Algorithm< I, A >::
-_Result( const TVertex& v ) const
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+~Algorithm( )
 {
-  return( this->GetOutput( )->GetPixel( v ) );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class A >
-double fpa::Image::Algorithm< I, A >::
-_Norm( const TVertex& a, const TVertex& b ) const
+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( )
 {
-  typename I::PointType pa, pb;
-  this->GetInput( )->TransformIndexToPhysicalPoint( a, pa );
-  this->GetInput( )->TransformIndexToPhysicalPoint( b, pb );
-  return( double( pa.EuclideanDistanceTo( pb ) ) );
+  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;
+      node.FrontId = nodes.size( ) + 1;
+      nodes.insert( node );
+    }
+    else
+      sIt->IsUnified = false;
+      
+  } // rof
+
+  return( nodes );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class A >
-bool fpa::Image::Algorithm< I, A >::
-_Edge( const TVertex& a, const TVertex& b ) const
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+void
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_ConfigureOutput( const TOutputValue& v )
 {
-  unsigned long dist = 0;
-  for( unsigned int d = 0; d < I::ImageDimension; d++ )
-    dist += std::abs( long( a[ d ] ) - long( b[ d ] ) );
-  if( this->m_NeighborhoodOrder == 1 )
-    return( dist == 0 || dist == 1 );
-  else
-    return( dist == 0 || dist == 1 || dist == 2 );
+  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 I, class A >
-typename fpa::Image::Algorithm< I, A >::
-TCost fpa::Image::Algorithm< I, A >::
-_Cost( const TVertex& a, const TVertex& b ) const
+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
 {
-  static const TCost INF_COST = std::numeric_limits< TCost >::max( );
-  if( this->_Edge( a, b ) )
+  typename TInputImage::RegionType region =
+    this->GetInput( )->GetRequestedRegion( );
+  TNeighborhood neighborhood;
+  if( this->m_NeigborhoodOrder != 1 )
   {
-    TCost c = TCost( this->GetInput( )->GetPixel( b ) );
-    if( this->m_CostConversion.IsNotNull( ) )
-      return( this->m_CostConversion->Evaluate( c ) );
-    else
-      return( c );
+    // TODO
   }
   else
-    return( INF_COST );
-}
-
-// -------------------------------------------------------------------------
-template< class I, class A >
-void fpa::Image::Algorithm< I, A >::
-_Neighs( const _TNode& n, _TNodes& N ) const
-{
-  typename I::RegionType reg = this->GetInput( )->GetRequestedRegion( );
-
-  N.clear( );
-  if( this->m_NeighborhoodOrder == 1 )
   {
-    for( unsigned int d = 0; d < I::ImageDimension; d++ )
+    for( unsigned int d = 0; d < TInputImage::ImageDimension; ++d )
     {
-      for( int i = -1; i <= 1; i += 2 )
+      for( int s = -1; s <= 1; s += 2 )
       {
-        TVertex v = n.Vertex;
-        v[ d ] += i;
-        if( reg.IsInside( v ) )
-          N.push_back( _TNode( v, n.FrontId ) );
-        else
-          N.push_back( n );
+        TVertex n = v;
+        n[ d ] += s;
+        if( region.IsInside( n ) )
+          neighborhood.push_back( n );
 
       } // rof
 
     } // rof
-  }
-  else
-  {
-    typedef itk::ConstNeighborhoodIterator< I > TNeighIt;
-    typename I::SizeType nSize;
-    nSize.Fill( 1 );
 
-    TNeighIt nIt( nSize, this->GetInput( ), reg );
-    nIt.SetLocation( n.Vertex );
-    for( unsigned int i = 0; i < nIt.Size( ); i++ )
-    {
-      TVertex idxN = nIt.GetIndex( i );
-      if( idxN == n.Vertex )
-        continue;
-      if( !reg.IsInside( idxN ) )
-        continue;
-      N.push_back( _TNode( idxN, n.FrontId ) );
+  } // fi
+  return( neighborhood );
+}
 
-    } // rof
+// -------------------------------------------------------------------------
+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
+{
+  return( this->GetInput( )->GetPixel( v ) );
+}
 
-  } // fi
+// -------------------------------------------------------------------------
+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
+{
+  return( this->GetOutput( )->GetPixel( v ) );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class A >
-void fpa::Image::Algorithm< I, A >::
-_NeighsInDim( const _TNode& n, const unsigned int& d, _TNodes& N )
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+void
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_UpdateOutputValue( const TNode& n )
 {
-  typename I::RegionType reg = this->GetInput( )->GetRequestedRegion( );
+  this->GetOutput( )->SetPixel( n.Vertex, n.Value );
+}
 
-  N.clear( );
-  for( int i = -1; i <= 1; i += 2 )
-  {
-    TVertex v = n.Vertex;
-    v[ d ] += i;
-    if( reg.IsInside( v ) )
-      N.push_back( _TNode( v, n.FrontId ) );
+// -------------------------------------------------------------------------
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+bool
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_IsMarked( const TVertex& v ) const
+{
+  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 ) ) );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class A >
-void fpa::Image::Algorithm< I, A >::
-_InitializeResults( )
+template< class _TInputImage, class _TOutputImage, class _TMarksInterface, class _TSeedsInterface >
+bool
+fpa::Image::Algorithm< _TInputImage, _TOutputImage, _TMarksInterface, _TSeedsInterface >::
+_Mark( const TVertex& v, unsigned long frontId )
 {
+  this->GetMarks( )->SetPixel( v, TFrontId( frontId ) );
+  return( true );
 }
 
-#endif // __FPA__IMAGE__ALGORITHM__HXX__
+#endif // __fpa__Image__Algorithm__hxx__
 
 // eof - $RCSfile$