]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Algorithm.hxx
07c09e6a1a628e92739db2455a575212c4db7b31
[FrontAlgorithms.git] / lib / fpa / Image / Algorithm.hxx
1 #ifndef __FPA__IMAGE__ALGORITHM__HXX__
2 #define __FPA__IMAGE__ALGORITHM__HXX__
3
4 #include <itkConstNeighborhoodIterator.h>
5
6 // -------------------------------------------------------------------------
7 template< class _TInputImage, class _TOutputImage >
8 fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
9 Algorithm( )
10   : Superclass( ),
11     m_NeighborhoodOrder( 1 )
12 {
13 }
14
15 // -------------------------------------------------------------------------
16 template< class _TInputImage, class _TOutputImage >
17 fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
18 ~Algorithm( )
19 {
20 }
21
22 // -------------------------------------------------------------------------
23 template< class _TInputImage, class _TOutputImage >
24 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
25 _BeforeGenerateData( )
26 {
27   this->Superclass::_BeforeGenerateData( );
28   this->AllocateOutputs( );
29 }
30
31 // -------------------------------------------------------------------------
32 template< class _TInputImage, class _TOutputImage >
33 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
34 _InitMarks( )
35 {
36   TOutputImage* out = this->GetOutput( );
37   this->m_Marks = TMarkImage::New( );
38   this->m_Marks->CopyInformation( out );
39   this->m_Marks->SetRequestedRegion( out->GetRequestedRegion( ) );
40   this->m_Marks->SetBufferedRegion( out->GetBufferedRegion( ) );
41   this->m_Marks->Allocate( );
42   this->m_Marks->FillBuffer( 0 );
43 }
44
45 // -------------------------------------------------------------------------
46 template< class _TInputImage, class _TOutputImage >
47 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
48 _InitResults( )
49 {
50   this->GetOutput( )->FillBuffer( TScalar( 0 ) );
51 }
52
53 // -------------------------------------------------------------------------
54 template< class _TInputImage, class _TOutputImage >
55 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
56 _DeallocateAuxiliary( )
57 {
58   this->m_Marks = NULL;
59 }
60
61 // -------------------------------------------------------------------------
62 template< class _TInputImage, class _TOutputImage >
63 typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
64 TFrontId fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
65 _GetMark( const TVertex& v )
66 {
67   if( this->m_Marks->GetRequestedRegion( ).IsInside( v ) )
68     return( this->m_Marks->GetPixel( v ) );
69   else
70     return( 0 );
71 }
72
73 // -------------------------------------------------------------------------
74 template< class _TInputImage, class _TOutputImage >
75 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
76 _Visit( const TNode& n )
77 {
78   if( this->m_Marks->GetRequestedRegion( ).IsInside( n.Vertex ) )
79   {
80     this->m_Marks->SetPixel( n.Vertex, n.FrontId );
81     this->GetOutput( )->SetPixel( n.Vertex, n.Result );
82
83   } // fi
84 }
85
86 // -------------------------------------------------------------------------
87 template< class _TInputImage, class _TOutputImage >
88 typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
89 TVertices fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
90 _GetNeighborhood( const TVertex& v ) const
91 {
92   TVertices neighs;
93   auto reg = this->GetInput( )->GetRequestedRegion( );
94   if( this->m_NeighborhoodOrder == 1 )
95   {
96     for( unsigned int d = 0; d < _TInputImage::ImageDimension; d++ )
97     {
98       for( int i = -1; i <= 1; i += 2 )
99       {
100         TVertex n = v;
101         n[ d ] += i;
102         if( reg.IsInside( n ) )
103           neighs.push_back( n );
104
105       } // rof
106
107     } // rof
108   }
109   else
110   {
111     typedef itk::ConstNeighborhoodIterator< _TInputImage > _TNeighIt;
112     typename _TInputImage::SizeType nSize;
113     nSize.Fill( 1 );
114
115     _TNeighIt nIt( nSize, this->GetInput( ), reg );
116     nIt.SetLocation( v );
117     for( unsigned int i = 0; i < nIt.Size( ); i++ )
118     {
119       TVertex n = nIt.GetIndex( i );
120       if( n == v )
121         continue;
122       if( reg.IsInside( n ) )
123         neighs.push_back( n );
124
125     } // rof
126
127   } // fi
128   return( neighs );
129 }
130
131 #endif // __FPA__IMAGE__ALGORITHM__HXX__
132
133 // eof - $RCSfile$