]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Filters/Algorithm.hxx
...
[FrontAlgorithms.git] / lib / fpa / Filters / Algorithm.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #ifndef __fpa__Filters__Algorithm__hxx__
6 #define __fpa__Filters__Algorithm__hxx__
7
8 #include <itkDataObject.h>
9 #include <fpa/Functors/BaseVertexFunction.h>
10
11 // -------------------------------------------------------------------------
12 template< class _TTraits >
13 itk::ModifiedTimeType fpa::Filters::Algorithm< _TTraits >::
14 GetMTime( ) const
15 {
16   itk::ModifiedTimeType q = this->Superclass::GetMTime( );
17   itk::ModifiedTimeType t;
18   for( itk::Object* o: this->m_AssociatedObjects )
19   {
20     if( o != NULL )
21     {
22       t = o->GetMTime( );
23       q = ( q < t )? q: t;
24
25     } // fi
26
27   } // rof
28   return( q );
29 }
30
31 // -------------------------------------------------------------------------
32 template< class _TTraits >
33 void fpa::Filters::Algorithm< _TTraits >::
34 InvokeEvent( const itk::EventObject& e )
35 {
36   TEvent a;
37   if( a.CheckEvent( &e ) )
38   {
39     if( this->m_VisualDebug )
40       this->Superclass::InvokeEvent( e );
41   }
42   else
43     this->Superclass::InvokeEvent( e );
44 }
45
46 // -------------------------------------------------------------------------
47 template< class _TTraits >
48 void fpa::Filters::Algorithm< _TTraits >::
49 InvokeEvent( const itk::EventObject& e ) const
50 {
51   TEvent a;
52   if( a.CheckEvent( &e ) )
53   {
54     if( this->m_VisualDebug )
55       this->Superclass::InvokeEvent( e );
56   }
57   else
58     this->Superclass::InvokeEvent( e );
59 }
60
61 // -------------------------------------------------------------------------
62 template< class _TTraits >
63 fpa::Filters::Algorithm< _TTraits >::
64 Algorithm( )
65   : Superclass( ),
66     TMarksInterface( this ),
67     TSeedsInterface( this ),
68     m_VisualDebug( false ),
69     m_InitValue( TOutputValue( 0 ) )
70 {
71 }
72
73 // -------------------------------------------------------------------------
74 template< class _TTraits >
75 fpa::Filters::Algorithm< _TTraits >::
76 ~Algorithm( )
77 {
78 }
79
80 // -------------------------------------------------------------------------
81 template< class _TTraits >
82 void fpa::Filters::Algorithm< _TTraits >::
83 GenerateData( )
84 {
85   // Init algorithm
86   this->InvokeEvent( itk::StartEvent( ) );
87   this->_BeforeGenerateData( );
88   this->_ConfigureOutputs( );
89   this->_PrepareSeeds( this->_GetReferenceInput( ) );
90   this->_InitCollisions( this->GetSeeds( ).size( ) );
91
92   // Init queue
93   for( TNode seed: this->GetSeeds( ) )
94   {
95     seed.Value = this->m_InitValue;
96     this->_QueuePush( seed );
97     this->InvokeEvent( TEvent( seed.Vertex, seed.FrontId, true ) );
98
99   } // rof
100
101   // Main loop
102   while( this->_QueueSize( ) > 0 )
103   {
104     // Get next candidate
105     TNode node = this->_QueuePop( );
106     this->InvokeEvent( TEvent( node.Vertex, node.FrontId, false ) );
107
108     if( !( this->_IsMarked( node ) ) )
109     {
110       // Update output value and mark vertex
111       this->_UpdateOutputValue( node );
112       this->_Mark( node );
113
114       // The actual node was effectively marked?
115       if( node.FrontId > 0 )
116       {
117         // Add neighborhood
118         TNeighborhood neighbors = this->_GetNeighbors( node.Vertex );
119         typename TNeighborhood::const_iterator nIt = neighbors.begin( );
120         bool coll = false;
121         while( nIt != neighbors.end( ) && !coll )
122         {
123           if( this->_IsMarked( *nIt ) )
124           {
125             // Invoke stop at collisions
126             if( this->_Collisions( node.Vertex, *nIt ) )
127             {
128               this->_QueueClear( );
129               coll = true;
130
131             } // fi
132           }
133           else
134           {
135             TNode nnode;
136             nnode.Vertex = *nIt;
137             nnode.Parent = node.Vertex;
138             nnode.FrontId = node.FrontId;
139             this->_ComputeOutputValue( nnode );
140             this->_QueuePush( nnode );
141             this->InvokeEvent( TEvent( nnode.Vertex, nnode.FrontId, true ) );
142
143           } // fi
144           ++nIt;
145
146         } // elihw
147
148       } // fi
149
150     } // fi
151     this->_Reinitialize( );
152
153   } // elihw
154
155   // Finish algorithm
156   this->_AfterGenerateData( );
157   this->InvokeEvent( itk::EndEvent( ) );
158 }
159
160 // -------------------------------------------------------------------------
161 template< class _TTraits >
162 void fpa::Filters::Algorithm< _TTraits >::
163 _Associate( itk::Object* o )
164 {
165   if( this->m_AssociatedObjects.insert( o ).second )
166     this->Modified( );
167 }
168
169 // -------------------------------------------------------------------------
170 template< class _TTraits >
171 void fpa::Filters::Algorithm< _TTraits >::
172 _Deassociate( itk::Object* o )
173 {
174   std::set< itk::Object* >::iterator i = this->m_AssociatedObjects.find( o );
175   if( i != this->m_AssociatedObjects.end( ) )
176   {
177     this->m_AssociatedObjects.erase( i );
178     this->Modified( );
179
180   } // fi
181 }
182
183 // -------------------------------------------------------------------------
184 template< class _TTraits >
185 void fpa::Filters::Algorithm< _TTraits >::
186 _DeassociateAll( )
187 {
188   if( this->m_AssociatedObjects.size( ) > 0 )
189     this->Modified( );
190   this->m_AssociatedObjects.clear( );
191 }
192
193 // -------------------------------------------------------------------------
194 template< class _TTraits >
195 void fpa::Filters::Algorithm< _TTraits >::
196 _BeforeGenerateData( )
197 {
198   typedef fpa::Functors::LightBaseVertexFunction< TVertex > _TVertexFunc;
199   const itk::DataObject* input = this->GetInput( );
200   for( itk::Object* o: this->m_AssociatedObjects )
201   {
202     _TVertexFunc* f = dynamic_cast< _TVertexFunc* >( o );
203     if( f != NULL )
204       f->SetDataObject( input );
205
206   } // rof
207 }
208
209 // -------------------------------------------------------------------------
210 template< class _TTraits >
211 void fpa::Filters::Algorithm< _TTraits >::
212 _AfterGenerateData( )
213 {
214 }
215
216 // -------------------------------------------------------------------------
217 template< class _TTraits >
218 void fpa::Filters::Algorithm< _TTraits >::
219 _Reinitialize( )
220 {
221 }
222
223 // -------------------------------------------------------------------------
224 template< class _TTraits >
225 const itk::DataObject* fpa::Filters::Algorithm< _TTraits >::
226 _GetReferenceInput( ) const
227 {
228   return( this->GetInput( ) );
229 }
230
231 // -------------------------------------------------------------------------
232 template< class _TTraits >
233 typename fpa::Filters::Algorithm< _TTraits >::
234 TInputValue fpa::Filters::Algorithm< _TTraits >::
235 _GetInputValue( const TNode& n ) const
236 {
237   return( this->_GetInputValue( n.Vertex ) );
238 }
239
240 // -------------------------------------------------------------------------
241 template< class _TTraits >
242 typename fpa::Filters::Algorithm< _TTraits >::
243 TOutputValue fpa::Filters::Algorithm< _TTraits >::
244 _GetOutputValue( const TNode& n ) const
245 {
246   return( this->_GetOutputValue( n.Vertex ) );
247 }
248
249 #endif // __fpa__Filters__Algorithm__hxx__
250 // eof - $RCSfile$