]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/Algorithm.hxx
383db743ad3f9ced6eb3c5c5152771978de9c5b0
[FrontAlgorithms.git] / lib / fpa / Base / Algorithm.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __fpa__Base__Algorithm__hxx__
7 #define __fpa__Base__Algorithm__hxx__
8
9 // -------------------------------------------------------------------------
10 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
11 fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
12 TEvent( )
13   : Superclass( )
14 {
15 }
16
17 // -------------------------------------------------------------------------
18 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
19 fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
20 TEvent( const TVertex& v, unsigned long fid, bool intoq )
21   : Superclass( ),
22     Vertex( v ),
23     FrontId( fid ),
24     IntoQueue( intoq )
25 {
26 }
27
28 // -------------------------------------------------------------------------
29 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
30 fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
31 ~TEvent( )
32 {
33 }
34
35 // -------------------------------------------------------------------------
36 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
37 const char* 
38 fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
39 GetEventName( ) const
40 {
41   return( "fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent" );
42 }
43
44 // -------------------------------------------------------------------------
45 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
46 bool
47 fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
48 CheckEvent( const itk::EventObject* e ) const
49 {
50   return( dynamic_cast< const Self* >( e ) != NULL );
51 }
52
53 // -------------------------------------------------------------------------
54 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
55 itk::EventObject* 
56 fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::TEvent::
57 MakeObject( ) const
58 {
59   return( new Self );
60 }
61
62 // -------------------------------------------------------------------------
63 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
64 void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
65 InvokeEvent( const itk::EventObject& e )
66 {
67   TEvent a;
68   if( a.CheckEvent( &e ) )
69   {
70     if( this->m_VisualDebug )
71       this->Superclass::InvokeEvent( e );
72   }
73   else
74     this->Superclass::InvokeEvent( e );
75 }
76
77 // -------------------------------------------------------------------------
78 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
79 void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
80 InvokeEvent( const itk::EventObject& e ) const
81 {
82   TEvent a;
83   if( a.CheckEvent( &e ) )
84   {
85     if( this->m_VisualDebug )
86       this->Superclass::InvokeEvent( e );
87   }
88   else
89     this->Superclass::InvokeEvent( e );
90 }
91
92 // -------------------------------------------------------------------------
93 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
94 fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
95 Algorithm( )
96   : Superclass( ),
97     _TMarksInterface( this ),
98     _TSeedsInterface( this ),
99     m_VisualDebug( false )
100 {
101 }
102
103 // -------------------------------------------------------------------------
104 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
105 fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
106 ~Algorithm( )
107 {
108 }
109
110 // -------------------------------------------------------------------------
111 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
112 void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
113 GenerateData( )
114 {
115   this->InvokeEvent( itk::StartEvent( ) );
116
117   // Init objects
118   this->_BeforeGenerateData( );
119   this->_ConfigureOutput( this->m_InitValue );
120   this->_InitMarks( this->GetNumberOfSeeds( ) );
121
122   // Init queue
123   this->_QueueInit( );
124   typename TSeeds::const_iterator sIt = this->BeginSeeds( );
125   for( ; sIt != this->EndSeeds( ); ++sIt )
126   {
127     this->_QueuePush( *sIt );
128     this->InvokeEvent( TEvent( sIt->Vertex, sIt->FrontId, true ) );
129
130   } // rof
131
132   // Main loop
133   while( this->_QueueSize( ) > 0 )
134   {
135     // Get next candidate
136     TNode node = this->_QueuePop( );
137     this->InvokeEvent( TEvent( node.Vertex, node.FrontId, false ) );
138     if( !( this->_IsMarked( node.Vertex ) ) )
139     {
140       // Mark it
141       if( this->_Mark( node.Vertex, node.FrontId ) )
142       {
143         // Update output value
144         this->_UpdateOutputValue( node );
145
146         // Add neighborhood
147         TNeighborhood neighbors = this->_GetNeighbors( node.Vertex );
148         typename TNeighborhood::const_iterator nIt = neighbors.begin( );
149         bool coll = false;
150         while( nIt != neighbors.end( ) && !coll )
151         {
152           if( this->_IsMarked( *nIt ) )
153           {
154             // Invoke stop at collisions
155             if( this->_Collisions( node.Vertex, *nIt ) )
156             {
157               this->_QueueClear( );
158               coll = true;
159
160             } // fi
161           }
162           else
163           {
164             TNode nnode;
165             nnode.Vertex = *nIt;
166             nnode.Parent = node.Vertex;
167             nnode.FrontId = node.FrontId;
168             nnode.Value = this->_ComputeOutputValue( nnode );
169             this->_QueuePush( nnode );
170             this->InvokeEvent( TEvent( nnode.Vertex, nnode.FrontId, true ) );
171
172           } // fi
173           ++nIt;
174
175         } // elihw
176
177       } // fi
178
179     } // fi
180
181   } // elihw
182
183   // Finish
184   this->_AfterGenerateData( );
185   this->InvokeEvent( itk::EndEvent( ) );
186 }
187
188 // -------------------------------------------------------------------------
189 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
190 void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
191 _BeforeGenerateData( )
192 {
193 }
194
195 // -------------------------------------------------------------------------
196 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
197 void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
198 _AfterGenerateData( )
199 {
200 }
201
202 // -------------------------------------------------------------------------
203 template< class _TFilter, class _TMarksInterface, class _TSeedsInterface >
204 void fpa::Base::Algorithm< _TFilter, _TMarksInterface, _TSeedsInterface >::
205 _QueueInit( )
206 {
207   this->_QueueClear( );
208 }
209
210 #endif // __fpa__Base__Algorithm__hxx__
211
212 // eof - $RCSfile$