]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Algorithm.hxx
6e4eef139929c21ebb5fc22b182576067fde4b04
[FrontAlgorithms.git] / lib / fpa / Image / Algorithm.hxx
1 #ifndef __fpa__Image__Algorithm__hxx__
2 #define __fpa__Image__Algorithm__hxx__
3
4 // Send Piotr's code to Anna
5
6 #include <itkImage.h>
7 #include <fpa/Image/Functors/SimpleNeighborhood.h>
8
9 // -------------------------------------------------------------------------
10 template< class _TInputImage, class _TOutputImage >
11 fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
12 Algorithm( )
13   : Superclass( )
14 {
15   typedef fpa::Image::Functors::SimpleNeighborhood< _TInputImage::ImageDimension > _TNeigh;
16   typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
17
18   this->m_MarksIdx = this->GetNumberOfRequiredOutputs( );
19   this->itk::ProcessObject::SetNumberOfRequiredOutputs( this->m_MarksIdx + 1 );
20   typename _TMarks::Pointer marks = _TMarks::New( );
21   this->SetNthOutput( this->m_MarksIdx, marks );
22
23   typename _TNeigh::Pointer neigh = _TNeigh::New( );
24   this->SetNeighborhoodFunction( neigh );
25 }
26
27 // -------------------------------------------------------------------------
28 template< class _TInputImage, class _TOutputImage >
29 fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
30 ~Algorithm( )
31 {
32 }
33
34 // -------------------------------------------------------------------------
35 template< class _TInputImage, class _TOutputImage >
36 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
37 _BeforeGenerateData( )
38 {
39   this->Superclass::_BeforeGenerateData( );
40   this->AllocateOutputs( );
41
42   TNeighborhoodFunction* neighFunc =
43     dynamic_cast< TNeighborhoodFunction* >(
44       this->GetNeighborhoodFunction( )
45       );
46   if( neighFunc == NULL )
47     itkExceptionMacro( << "NeighborhoodFunction not well defined." );
48   neighFunc->SetImage( this->GetInput( ) );
49
50   TVertexFunction* vertexFunc =
51     dynamic_cast< TVertexFunction* >(
52       this->GetVertexFunction( )
53       );
54   if( vertexFunc == NULL )
55     itkExceptionMacro( << "VertexFunction not well defined." );
56   vertexFunc->SetImage( this->GetInput( ) );
57 }
58
59 // -------------------------------------------------------------------------
60 template< class _TInputImage, class _TOutputImage >
61 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
62 _InitMarks( )
63 {
64   typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
65   _TMarks* marks =
66     dynamic_cast< _TMarks* >(
67       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
68       );
69   marks->FillBuffer( 0 );
70 }
71
72 // -------------------------------------------------------------------------
73 template< class _TInputImage, class _TOutputImage >
74 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
75 _InitResults( const TOutput& init_value )
76 {
77   this->GetOutput( )->FillBuffer( init_value );
78 }
79
80 // -------------------------------------------------------------------------
81 template< class _TInputImage, class _TOutputImage >
82 bool fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
83 _IsMarked( const TVertex& v ) const
84 {
85   typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
86   const _TMarks* marks =
87     dynamic_cast< const _TMarks* >(
88       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
89       );
90   return( marks->GetPixel( v ) != 0 );
91 }
92
93 // -------------------------------------------------------------------------
94 template< class _TInputImage, class _TOutputImage >
95 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
96 _Mark( const _TQueueNode& n )
97 {
98   typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
99   _TMarks* marks =
100     dynamic_cast< _TMarks* >(
101       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
102       );
103   marks->SetPixel( n.Vertex, n.FrontId );
104 }
105
106 // -------------------------------------------------------------------------
107 template< class _TInputImage, class _TOutputImage >
108 typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
109 TFrontId fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
110 _GetMark( const TVertex& v ) const
111 {
112   typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
113   const _TMarks* marks =
114     dynamic_cast< const _TMarks* >(
115       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
116       );
117   return( marks->GetPixel( v ) );
118 }
119
120 // -------------------------------------------------------------------------
121 template< class _TInputImage, class _TOutputImage >
122 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
123 _UpdateResult( const _TQueueNode& n )
124 {
125   this->GetOutput( )->SetPixel( n.Vertex, n.Result );
126 }
127
128 // -------------------------------------------------------------------------
129 template< class _TInputImage, class _TOutputImage >
130 typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::TOutput
131 fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
132 _GetResult( const TVertex& v ) const
133 {
134   if( this->GetOutput( )->GetLargestPossibleRegion( ).IsInside( v ) )
135     return( this->GetOutput( )->GetPixel( v ) );
136   else
137     return( this->m_InitResult );
138 }
139
140 // -------------------------------------------------------------------------
141 template< class _TInputImage, class _TOutputImage >
142 unsigned int fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
143 _GetNumberOfDimensions( ) const
144 {
145   return( _TInputImage::ImageDimension );
146 }
147
148 #endif // __fpa__Image__Algorithm__hxx__
149
150 // eof - $RCSfile$