]> 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 <fpa/Functors/BaseVertexFunction.h>
9
10 // -------------------------------------------------------------------------
11 template< class _TTraits >
12 itk::ModifiedTimeType fpa::Filters::Algorithm< _TTraits >::
13 GetMTime( ) const
14 {
15   itk::ModifiedTimeType q = this->Superclass::GetMTime( );
16   itk::ModifiedTimeType t;
17   for( itk::Object* o: this->m_AssociatedObjects )
18   {
19     if( o != NULL )
20     {
21       t = o->GetMTime( );
22       q = ( q < t )? q: t;
23
24     } // fi
25
26   } // rof
27   return( q );
28 }
29
30 // -------------------------------------------------------------------------
31 template< class _TTraits >
32 void fpa::Filters::Algorithm< _TTraits >::
33 InvokeEvent( const itk::EventObject& e )
34 {
35   TEvent a;
36   if( a.CheckEvent( &e ) )
37   {
38     if( this->m_VisualDebug )
39       this->Superclass::InvokeEvent( e );
40   }
41   else
42     this->Superclass::InvokeEvent( e );
43 }
44
45 // -------------------------------------------------------------------------
46 template< class _TTraits >
47 void fpa::Filters::Algorithm< _TTraits >::
48 InvokeEvent( const itk::EventObject& e ) const
49 {
50   TEvent a;
51   if( a.CheckEvent( &e ) )
52   {
53     if( this->m_VisualDebug )
54       this->Superclass::InvokeEvent( e );
55   }
56   else
57     this->Superclass::InvokeEvent( e );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class _TTraits >
62 fpa::Filters::Algorithm< _TTraits >::
63 Algorithm( )
64   : Superclass( ),
65     TTraits::TMarksInterface( this ),
66     TTraits::TSeedsInterface( this ),
67     m_VisualDebug( false ),
68     m_InitValue( TOutputValue( 0 ) )
69 {
70 }
71
72 // -------------------------------------------------------------------------
73 template< class _TTraits >
74 fpa::Filters::Algorithm< _TTraits >::
75 ~Algorithm( )
76 {
77 }
78
79 // -------------------------------------------------------------------------
80 template< class _TTraits >
81 void fpa::Filters::Algorithm< _TTraits >::
82 GenerateData( )
83 {
84   // Init algorithm
85   this->InvokeEvent( itk::StartEvent( ) );
86   this->_BeforeGenerateData( );
87   this->_ConfigureOutputs( );
88   this->_PrepareSeeds( this->_GetReferenceInput( ) );
89   this->_InitCollisions( this->GetSeeds( ).size( ) );
90
91   // Init queue
92   for( TNode seed: this->GetSeeds( ) )
93   {
94     seed.Value = this->m_InitValue;
95     this->_QueuePush( seed );
96     this->InvokeEvent( TEvent( seed.Vertex, seed.FrontId, true ) );
97
98   } // rof
99
100   // Main loop
101   while( this->_QueueSize( ) > 0 )
102   {
103     // Get next candidate
104     TNode node = this->_QueuePop( );
105     this->InvokeEvent( TEvent( node.Vertex, node.FrontId, false ) );
106
107     if( this->_IsNotMarked( node ) )
108     {
109       // Update output value and mark vertex
110       this->_PostComputeOutputValue( node );
111       this->_AssignOutputValue( 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->_PreComputeOutputValue( 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::BaseVertexFunction< TVertex > _TFunction;
199   const itk::DataObject* input = this->GetInput( );
200   for( itk::Object* o: this->m_AssociatedObjects )
201   {
202     _TFunction* f = dynamic_cast< _TFunction* >( 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   // Nothing to do at this level
215 }
216
217 // -------------------------------------------------------------------------
218 template< class _TTraits >
219 typename fpa::Filters::Algorithm< _TTraits >::
220 TInputValue fpa::Filters::Algorithm< _TTraits >::
221 _GetInputValue( const TNode& n ) const
222 {
223   return( this->_GetInputValue( n.Vertex ) );
224 }
225
226 // -------------------------------------------------------------------------
227 template< class _TTraits >
228 typename fpa::Filters::Algorithm< _TTraits >::
229 TOutputValue fpa::Filters::Algorithm< _TTraits >::
230 _GetOutputValue( const TNode& n ) const
231 {
232   return( this->_GetOutputValue( n.Vertex ) );
233 }
234
235 // -------------------------------------------------------------------------
236 template< class _TTraits >
237 typename fpa::Filters::Algorithm< _TTraits >::
238 TNeighborhood fpa::Filters::Algorithm< _TTraits >::
239 _GetNeighbors( const TNode& n ) const
240 {
241   return( this->_GetNeighbors( n.Vertex )  );
242 }
243
244 // -------------------------------------------------------------------------
245 template< class _TTraits >
246 const itk::DataObject* fpa::Filters::Algorithm< _TTraits >::
247 _GetReferenceInput( ) const
248 {
249   return( this->GetInput( ) );
250 }
251
252 // -------------------------------------------------------------------------
253 template< class _TTraits >
254 bool fpa::Filters::Algorithm< _TTraits >::
255 _IsMarked( const TNode& n ) const
256 {
257   return( this->_IsMarked( n.Vertex ) );
258 }
259
260 // -------------------------------------------------------------------------
261 template< class _TTraits >
262 bool fpa::Filters::Algorithm< _TTraits >::
263 _IsNotMarked( const TVertex& v ) const
264 {
265   return( !( this->_IsMarked( v ) ) );
266 }
267
268 // -------------------------------------------------------------------------
269 template< class _TTraits >
270 bool fpa::Filters::Algorithm< _TTraits >::
271 _IsNotMarked( const TNode& n ) const
272 {
273   return( this->_IsNotMarked( n.Vertex ) );
274 }
275
276 // -------------------------------------------------------------------------
277 template< class _TTraits >
278 void fpa::Filters::Algorithm< _TTraits >::
279 _Reinitialize( )
280 {
281   // Nothing to do at this level
282 }
283
284 #endif // __fpa__Filters__Algorithm__hxx__
285 // eof - $RCSfile$