]> 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 <limits>
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     TTraits::TMarksInterface( this ),
67     TTraits::TSeedsInterface( this ),
68     m_VisualDebug( false ),
69     m_InitValue( TOutputValue( 0 ) ),
70     m_FillValue( std::numeric_limits< TOutputValue >::max( ) )
71 {
72 }
73
74 // -------------------------------------------------------------------------
75 template< class _TTraits >
76 fpa::Filters::Algorithm< _TTraits >::
77 ~Algorithm( )
78 {
79 }
80
81 // -------------------------------------------------------------------------
82 template< class _TTraits >
83 void fpa::Filters::Algorithm< _TTraits >::
84 GenerateData( )
85 {
86   // Init algorithm
87   this->InvokeEvent( itk::StartEvent( ) );
88   this->_BeforeGenerateData( );
89   this->_ConfigureOutputs( );
90   this->_PrepareSeeds( this->_GetReferenceInput( ) );
91   this->_InitCollisions( this->GetSeeds( ).size( ) );
92
93   // Init queue
94   for( TNode seed: this->GetSeeds( ) )
95   {
96     seed.Value = this->m_InitValue;
97     this->_QueuePush( seed );
98     this->InvokeEvent( TEvent( seed.Vertex, seed.FrontId, true ) );
99
100   } // rof
101
102   // Main loop
103   while( this->_QueueSize( ) > 0 )
104   {
105     // Get next candidate
106     TNode node = this->_QueuePop( );
107     this->InvokeEvent( TEvent( node.Vertex, node.FrontId, false ) );
108
109     if( this->_IsNotMarked( node ) )
110     {
111       // Update output value and mark vertex
112       this->_PostComputeOutputValue( node );
113       this->_AssignOutputValue( node );
114       this->_Mark( node );
115
116       // The actual node was effectively marked?
117       if( node.FrontId > 0 )
118       {
119         // Add neighborhood
120         TNeighborhood neighbors = this->_GetNeighbors( node.Vertex );
121         typename TNeighborhood::const_iterator nIt = neighbors.begin( );
122         bool coll = false;
123         while( nIt != neighbors.end( ) && !coll )
124         {
125           if( this->_IsMarked( *nIt ) )
126           {
127             // Invoke stop at collisions
128             if( this->_Collisions( node.Vertex, *nIt ) )
129             {
130               this->_QueueClear( );
131               coll = true;
132
133             } // fi
134           }
135           else
136           {
137             TNode nnode;
138             nnode.Vertex = *nIt;
139             nnode.Parent = node.Vertex;
140             nnode.FrontId = node.FrontId;
141             this->_PreComputeOutputValue( nnode );
142             this->_QueuePush( nnode );
143             this->InvokeEvent( TEvent( nnode.Vertex, nnode.FrontId, true ) );
144
145           } // fi
146           ++nIt;
147
148         } // elihw
149
150       } // fi
151
152     } // fi
153     this->_Reinitialize( );
154
155   } // elihw
156
157   // Finish algorithm
158   this->_AfterGenerateData( );
159   this->InvokeEvent( itk::EndEvent( ) );
160 }
161
162 // -------------------------------------------------------------------------
163 template< class _TTraits >
164 void fpa::Filters::Algorithm< _TTraits >::
165 _Associate( itk::Object* o )
166 {
167   if( this->m_AssociatedObjects.insert( o ).second )
168     this->Modified( );
169 }
170
171 // -------------------------------------------------------------------------
172 template< class _TTraits >
173 void fpa::Filters::Algorithm< _TTraits >::
174 _Deassociate( itk::Object* o )
175 {
176   std::set< itk::Object* >::iterator i = this->m_AssociatedObjects.find( o );
177   if( i != this->m_AssociatedObjects.end( ) )
178   {
179     this->m_AssociatedObjects.erase( i );
180     this->Modified( );
181
182   } // fi
183 }
184
185 // -------------------------------------------------------------------------
186 template< class _TTraits >
187 void fpa::Filters::Algorithm< _TTraits >::
188 _DeassociateAll( )
189 {
190   if( this->m_AssociatedObjects.size( ) > 0 )
191     this->Modified( );
192   this->m_AssociatedObjects.clear( );
193 }
194
195 // -------------------------------------------------------------------------
196 template< class _TTraits >
197 void fpa::Filters::Algorithm< _TTraits >::
198 _BeforeGenerateData( )
199 {
200   typedef fpa::Functors::BaseVertexFunction< TVertex > _TFunction;
201   const itk::DataObject* input = this->GetInput( );
202   for( itk::Object* o: this->m_AssociatedObjects )
203   {
204     _TFunction* f = dynamic_cast< _TFunction* >( o );
205     if( f != NULL )
206       f->SetDataObject( input );
207
208   } // rof
209 }
210
211 // -------------------------------------------------------------------------
212 template< class _TTraits >
213 void fpa::Filters::Algorithm< _TTraits >::
214 _AfterGenerateData( )
215 {
216   // Nothing to do at this level
217 }
218
219 // -------------------------------------------------------------------------
220 template< class _TTraits >
221 typename fpa::Filters::Algorithm< _TTraits >::
222 TInputValue fpa::Filters::Algorithm< _TTraits >::
223 _GetInputValue( const TNode& n ) const
224 {
225   return( this->_GetInputValue( n.Vertex ) );
226 }
227
228 // -------------------------------------------------------------------------
229 template< class _TTraits >
230 typename fpa::Filters::Algorithm< _TTraits >::
231 TOutputValue fpa::Filters::Algorithm< _TTraits >::
232 _GetOutputValue( const TNode& n ) const
233 {
234   return( this->_GetOutputValue( n.Vertex ) );
235 }
236
237 // -------------------------------------------------------------------------
238 template< class _TTraits >
239 typename fpa::Filters::Algorithm< _TTraits >::
240 TNeighborhood fpa::Filters::Algorithm< _TTraits >::
241 _GetNeighbors( const TNode& n ) const
242 {
243   return( this->_GetNeighbors( n.Vertex )  );
244 }
245
246 // -------------------------------------------------------------------------
247 template< class _TTraits >
248 const itk::DataObject* fpa::Filters::Algorithm< _TTraits >::
249 _GetReferenceInput( ) const
250 {
251   return( this->GetInput( ) );
252 }
253
254 // -------------------------------------------------------------------------
255 template< class _TTraits >
256 bool fpa::Filters::Algorithm< _TTraits >::
257 _IsMarked( const TNode& n ) const
258 {
259   return( this->_IsMarked( n.Vertex ) );
260 }
261
262 // -------------------------------------------------------------------------
263 template< class _TTraits >
264 bool fpa::Filters::Algorithm< _TTraits >::
265 _IsNotMarked( const TVertex& v ) const
266 {
267   return( !( this->_IsMarked( v ) ) );
268 }
269
270 // -------------------------------------------------------------------------
271 template< class _TTraits >
272 bool fpa::Filters::Algorithm< _TTraits >::
273 _IsNotMarked( const TNode& n ) const
274 {
275   return( this->_IsNotMarked( n.Vertex ) );
276 }
277
278 // -------------------------------------------------------------------------
279 template< class _TTraits >
280 void fpa::Filters::Algorithm< _TTraits >::
281 _Reinitialize( )
282 {
283   // Nothing to do at this level
284 }
285
286 #endif // __fpa__Filters__Algorithm__hxx__
287 // eof - $RCSfile$