]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Image/Algorithm.hxx
3c33e4e3ca90019e7ac77b58c1c8ae9b541948cc
[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     vertexFunc->SetImage( this->GetInput( ) );
56 }
57
58 // -------------------------------------------------------------------------
59 template< class _TInputImage, class _TOutputImage >
60 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
61 _InitMarks( )
62 {
63   typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
64   _TMarks* marks =
65     dynamic_cast< _TMarks* >(
66       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
67       );
68   marks->FillBuffer( 0 );
69 }
70
71 // -------------------------------------------------------------------------
72 template< class _TInputImage, class _TOutputImage >
73 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
74 _InitResults( const TOutput& init_value )
75 {
76   this->GetOutput( )->FillBuffer( init_value );
77 }
78
79 // -------------------------------------------------------------------------
80 template< class _TInputImage, class _TOutputImage >
81 bool fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
82 _IsMarked( const TVertex& v ) const
83 {
84   typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
85   const _TMarks* marks =
86     dynamic_cast< const _TMarks* >(
87       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
88       );
89   return( marks->GetPixel( v ) != 0 );
90 }
91
92 // -------------------------------------------------------------------------
93 template< class _TInputImage, class _TOutputImage >
94 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
95 _Mark( const _TQueueNode& n )
96 {
97   typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
98   _TMarks* marks =
99     dynamic_cast< _TMarks* >(
100       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
101       );
102   marks->SetPixel( n.Vertex, n.FrontId );
103 }
104
105 // -------------------------------------------------------------------------
106 template< class _TInputImage, class _TOutputImage >
107 typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
108 TFrontId fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
109 _GetMark( const TVertex& v ) const
110 {
111   typedef itk::Image< TFrontId, _TInputImage::ImageDimension > _TMarks;
112   const _TMarks* marks =
113     dynamic_cast< const _TMarks* >(
114       this->itk::ProcessObject::GetOutput( this->m_MarksIdx )
115       );
116   return( marks->GetPixel( v ) );
117 }
118
119 // -------------------------------------------------------------------------
120 template< class _TInputImage, class _TOutputImage >
121 void fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
122 _UpdateResult( const _TQueueNode& n )
123 {
124   this->GetOutput( )->SetPixel( n.Vertex, n.Result );
125 }
126
127 // -------------------------------------------------------------------------
128 template< class _TInputImage, class _TOutputImage >
129 typename fpa::Image::Algorithm< _TInputImage, _TOutputImage >::TOutput
130 fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
131 _GetResult( const TVertex& v ) const
132 {
133   if( this->GetOutput( )->GetLargestPossibleRegion( ).IsInside( v ) )
134     return( this->GetOutput( )->GetPixel( v ) );
135   else
136     return( this->m_InitResult );
137 }
138
139 // -------------------------------------------------------------------------
140 template< class _TInputImage, class _TOutputImage >
141 unsigned int fpa::Image::Algorithm< _TInputImage, _TOutputImage >::
142 _GetNumberOfDimensions( ) const
143 {
144   return( _TInputImage::ImageDimension );
145 }
146
147 #endif // __fpa__Image__Algorithm__hxx__
148
149 // eof - $RCSfile$