]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/Algorithm.hxx
...
[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 _TTraits >
11 fpa::Base::Algorithm< _TTraits >::TEvent::
12 TEvent( )
13   : Superclass( )
14 {
15 }
16
17 // -------------------------------------------------------------------------
18 template< class _TTraits >
19 fpa::Base::Algorithm< _TTraits >::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 _TTraits >
30 fpa::Base::Algorithm< _TTraits >::TEvent::
31 ~TEvent( )
32 {
33 }
34
35 // -------------------------------------------------------------------------
36 template< class _TTraits >
37 const char* fpa::Base::Algorithm< _TTraits >::TEvent::
38 GetEventName( ) const
39 {
40   return( "fpa::Base::Algorithm< _TTraits >::TEvent" );
41 }
42
43 // -------------------------------------------------------------------------
44 template< class _TTraits >
45 bool fpa::Base::Algorithm< _TTraits >::TEvent::
46 CheckEvent( const itk::EventObject* e ) const
47 {
48   return( dynamic_cast< const Self* >( e ) != NULL );
49 }
50
51 // -------------------------------------------------------------------------
52 template< class _TTraits >
53 itk::EventObject* fpa::Base::Algorithm< _TTraits >::TEvent::
54 MakeObject( ) const
55 {
56   return( new Self );
57 }
58
59 // -------------------------------------------------------------------------
60 template< class _TTraits >
61 void fpa::Base::Algorithm< _TTraits >::
62 InvokeEvent( const itk::EventObject& e )
63 {
64   TEvent a;
65   if( a.CheckEvent( &e ) )
66   {
67     if( this->m_VisualDebug )
68       this->Superclass::InvokeEvent( e );
69   }
70   else
71     this->Superclass::InvokeEvent( e );
72 }
73
74 // -------------------------------------------------------------------------
75 template< class _TTraits >
76 void fpa::Base::Algorithm< _TTraits >::
77 InvokeEvent( const itk::EventObject& e ) const
78 {
79   TEvent a;
80   if( a.CheckEvent( &e ) )
81   {
82     if( this->m_VisualDebug )
83       this->Superclass::InvokeEvent( e );
84   }
85   else
86     this->Superclass::InvokeEvent( e );
87 }
88
89 // -------------------------------------------------------------------------
90 template< class _TTraits >
91 fpa::Base::Algorithm< _TTraits >::
92 Algorithm( )
93   : Superclass( ),
94     TMarksInterface( this ),
95     TSeedsInterface( this ),
96     m_VisualDebug( false )
97 {
98 }
99
100 // -------------------------------------------------------------------------
101 template< class _TTraits >
102 fpa::Base::Algorithm< _TTraits >::
103 ~Algorithm( )
104 {
105 }
106
107 // -------------------------------------------------------------------------
108 template< class _TTraits >
109 void fpa::Base::Algorithm< _TTraits >::
110 GenerateData( )
111 {
112   this->InvokeEvent( itk::StartEvent( ) );
113
114   // Init objects
115   this->_BeforeGenerateData( );
116   this->_ConfigureOutput( this->m_InitValue );
117   this->_InitMarks( this->GetSeeds( ).size( ) );
118   TNodes seeds = this->_UnifySeeds( );
119   this->_PrepareSeeds( seeds );
120
121   // Init queue
122   this->_QueueInit( );
123   typename TNodes::const_iterator sIt = seeds.begin( );
124   for( ; sIt != seeds.end( ); ++sIt )
125   {
126     this->_QueuePush( *sIt );
127     this->InvokeEvent( TEvent( sIt->Vertex, sIt->FrontId, true ) );
128
129   } // rof
130
131   // Main loop
132   while( this->_QueueSize( ) > 0 )
133   {
134     // Get next candidate
135     TNode node = this->_QueuePop( );
136     this->InvokeEvent( TEvent( node.Vertex, node.FrontId, false ) );
137     if( !( this->_IsMarked( node.Vertex ) ) )
138     {
139       // Update output value and mark vertex
140       this->_UpdateOutputValue( node );
141       this->_Mark( node.Vertex, node.FrontId );
142
143       // The actual node was effectively marked?
144       if( node.FrontId > 0 )
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             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     this->_FinishOneLoop( );
181
182   } // elihw
183
184   // Finish
185   this->_AfterGenerateData( );
186   this->InvokeEvent( itk::EndEvent( ) );
187 }
188
189 // -------------------------------------------------------------------------
190 template< class _TTraits >
191 void fpa::Base::Algorithm< _TTraits >::
192 _BeforeGenerateData( )
193 {
194 }
195
196 // -------------------------------------------------------------------------
197 template< class _TTraits >
198 void fpa::Base::Algorithm< _TTraits >::
199 _AfterGenerateData( )
200 {
201 }
202
203 // -------------------------------------------------------------------------
204 template< class _TTraits >
205 void fpa::Base::Algorithm< _TTraits >::
206 _FinishOneLoop( )
207 {
208 }
209
210 // -------------------------------------------------------------------------
211 template< class _TTraits >
212 void fpa::Base::Algorithm< _TTraits >::
213 _QueueInit( )
214 {
215   this->_QueueClear( );
216 }
217
218 #endif // __fpa__Base__Algorithm__hxx__
219
220 // eof - $RCSfile$