]> Creatis software - FrontAlgorithms.git/blobdiff - lib/fpa/Image/Algorithm.hxx
...
[FrontAlgorithms.git] / lib / fpa / Image / Algorithm.hxx
index a5b1f8548e5e4dd481a93eb1fbb3e3f6561d4ff4..3c33e4e3ca90019e7ac77b58c1c8ae9b541948cc 100644 (file)
-#ifndef __FPA__IMAGE__ALGORITHM__HXX__
-#define __FPA__IMAGE__ALGORITHM__HXX__
+#ifndef __fpa__Image__Algorithm__hxx__
+#define __fpa__Image__Algorithm__hxx__
 
-#include <cmath>
-#include <itkConstNeighborhoodIterator.h>
+// Send Piotr's code to Anna
 
-// -------------------------------------------------------------------------
-template< class I, class O, class A >
-typename fpa::Image::Algorithm< I, O, A >::
-TMinimumSpanningTree* fpa::Image::Algorithm< I, O, A >::
-GetMinimumSpanningTree( )
-{
-  return(
-    dynamic_cast< TMinimumSpanningTree* >(
-      this->itk::ProcessObject::GetOutput( 1 )
-      )
-    );
-}
+#include <itkImage.h>
+#include <fpa/Image/Functors/SimpleNeighborhood.h>
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-const typename fpa::Image::Algorithm< I, O, A >::
-TMinimumSpanningTree* fpa::Image::Algorithm< I, O, A >::
-GetMinimumSpanningTree( ) const
+template< class _TInputImage, class _TOutputImage >
+fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+Algorithm( )
+  : Superclass( )
 {
-  return(
-    dynamic_cast< const TMinimumSpanningTree* >(
-      this->itk::ProcessObject::GetOutput( 1 )
-      )
-    );
-}
+  typedef fpa::Image::Functors::SimpleNeighborhood< _TInputImage::ImageDimension > _TNeigh;
+  typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
 
-// -------------------------------------------------------------------------
-template< class I, class O, class A >
-void fpa::Image::Algorithm< I, O, A >::
-GraftMinimumSpanningTree( itk::DataObject* obj )
-{
-  TMinimumSpanningTree* mst = dynamic_cast< TMinimumSpanningTree* >( obj );
-  if( mst != NULL )
-    this->GraftNthOutput( 1, mst );
-}
+  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 );
 
-// -------------------------------------------------------------------------
-template< class I, class O, class A >
-fpa::Image::Algorithm< I, O, A >::
-Algorithm( )
-  : Superclass( ),
-    m_NeighborhoodOrder( 1 )
-{
-  this->itk::ProcessObject::SetNumberOfRequiredOutputs( 2 );
-  this->itk::ProcessObject::SetNthOutput( 0, O::New( ) );
-  this->itk::ProcessObject::SetNthOutput( 1, TMinimumSpanningTree::New( ) );
+  typename _TNeigh::Pointer neigh = _TNeigh::New( );
+  this->SetNeighborhoodFunction( neigh );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-fpa::Image::Algorithm< I, O, A >::
+template< class _TInputImage, class _TOutputImage >
+fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
 ~Algorithm( )
 {
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-void fpa::Image::Algorithm< I, O, A >::
+template< class _TInputImage, class _TOutputImage >
+void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
 _BeforeGenerateData( )
 {
   this->Superclass::_BeforeGenerateData( );
   this->AllocateOutputs( );
-}
-
-// -------------------------------------------------------------------------
-template< class I, class O, class A >
-void fpa::Image::Algorithm< I, O, A >::
-_AfterGenerateData( )
-{
-  this->Superclass::_AfterGenerateData( );
-  this->GetMinimumSpanningTree( )->SetCollisions( this->m_Collisions );
-}
 
-// -------------------------------------------------------------------------
-template< class I, class O, class A >
-unsigned long fpa::Image::Algorithm< I, O, A >::
-_NumberOfVertices( ) const
-{
-  return( this->GetInput( )->GetRequestedRegion( ).GetNumberOfPixels( ) );
-}
+  TNeighborhoodFunction* neighFunc =
+    dynamic_cast< TNeighborhoodFunction* >(
+      this->GetNeighborhoodFunction( )
+      );
+  if( neighFunc == NULL )
+    itkExceptionMacro( << "NeighborhoodFunction not well defined." );
+  neighFunc->SetImage( this->GetInput( ) );
 
-// -------------------------------------------------------------------------
-template< class I, class O, class A >
-const typename fpa::Image::Algorithm< I, O, A >::
-TValue& fpa::Image::Algorithm< I, O, A >::
-_VertexValue( const TVertex& v ) const
-{
-  return( this->GetInput( )->GetPixel( v ) );
+  TVertexFunction* vertexFunc =
+    dynamic_cast< TVertexFunction* >(
+      this->GetVertexFunction( )
+      );
+  if( vertexFunc != NULL )
+    vertexFunc->SetImage( this->GetInput( ) );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-double fpa::Image::Algorithm< I, O, A >::
-_Distance( const TVertex& a, const TVertex& b ) const
-{
-  typename I::PointType pa, pb;
-  this->GetInput( )->TransformIndexToPhysicalPoint( a, pa );
-  this->GetInput( )->TransformIndexToPhysicalPoint( b, pb );
-  return( double( pa.EuclideanDistanceTo( pb ) ) );
-}
-
-// -------------------------------------------------------------------------
-template< class I, class O, class A >
-bool fpa::Image::Algorithm< I, O, A >::
-_HasEdge( const TVertex& a, const TVertex& b ) const
+template< class _TInputImage, class _TOutputImage >
+void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+_InitMarks( )
 {
-  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 <= 1 );
-  else
-    return( dist <= I::ImageDimension );
+  typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
+  _TMarks* marks =
+    dynamic_cast< _TMarks* >(
+      this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
+      );
+  marks->FillBuffer( 0 );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-void fpa::Image::Algorithm< I, O, A >::
-_Neighborhood( _TVertices& neighborhood, const TVertex& v ) const
+template< class _TInputImage, class _TOutputImage >
+void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+_InitResults( const TOutput& init_value )
 {
-  typename I::RegionType reg = this->GetInput( )->GetRequestedRegion( );
-
-  neighborhood.clear( );
-  if( this->m_NeighborhoodOrder == 1 )
-  {
-    for( unsigned int d = 0; d < I::ImageDimension; d++ )
-    {
-      for( int i = -1; i <= 1; i += 2 )
-      {
-        TVertex n = v;
-        n[ d ] += i;
-        if( reg.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( v );
-    for( unsigned int i = 0; i < nIt.Size( ); i++ )
-    {
-      TVertex n = nIt.GetIndex( i );
-      if( n == v )
-        continue;
-      if( reg.IsInside( n ) )
-        neighborhood.push_back( n );
-
-    } // rof
-
-  } // fi
+  this->GetOutput( )->FillBuffer( init_value );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-void fpa::Image::Algorithm< I, O, A >::
-_InitResults( )
+template< class _TInputImage, class _TOutputImage >
+bool fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+_IsMarked( 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 );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-const typename fpa::Image::Algorithm< I, O, A >::
-TResult& fpa::Image::Algorithm< I, O, A >::
-_Result( const TVertex& v ) const
+template< class _TInputImage, class _TOutputImage >
+void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+_Mark( const _TQueueNode& n )
 {
-  return( this->GetOutput( )->GetPixel( v ) );
+  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 );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-void fpa::Image::Algorithm< I, O, A >::
-_SetResult( const TVertex& v, const TResult& r )
+template< class _TInputImage, class _TOutputImage >
+typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+TFrontId fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+_GetMark( const TVertex& v ) const
 {
-  this->GetOutput( )->SetPixel( v, r );
+  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 ) );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-const typename fpa::Image::Algorithm< I, O, A >::
-_TNode& fpa::Image::Algorithm< I, O, A >::
-_Node( const TVertex& v ) const
+template< class _TInputImage, class _TOutputImage >
+void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+_UpdateResult( const _TQueueNode& n )
 {
-  return( this->GetMinimumSpanningTree( )->GetPixel( v ) );
+  this->GetOutput( )->SetPixel( n.Vertex, n.Result );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-void fpa::Image::Algorithm< I, O, A >::
-_InitMarks( )
+template< class _TInputImage, class _TOutputImage >
+typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::TOutput
+fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+_GetResult( const TVertex& v ) const
 {
-  _TNode far_node;
-  far_node.Label = Self::FarLabel;
-  this->GetMinimumSpanningTree( )->FillBuffer( far_node );
+  if( this->GetOutput( )->GetLargestPossibleRegion( ).IsInside( v ) )
+    return( this->GetOutput( )->GetPixel( v ) );
+  else
+    return( this->m_InitResult );
 }
 
 // -------------------------------------------------------------------------
-template< class I, class O, class A >
-void fpa::Image::Algorithm< I, O, A >::
-_Mark( const _TNode& node )
+template< class _TInputImage, class _TOutputImage >
+unsigned int fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
+_GetNumberOfDimensions( ) const
 {
-  this->GetMinimumSpanningTree( )->SetPixel( node.Vertex, node );
+  return( _TInputImage::ImageDimension );
 }
 
-#endif // __FPA__IMAGE__ALGORITHM__HXX__
+#endif // __fpa__Image__Algorithm__hxx__
 
 // eof - $RCSfile$