]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/Algorithm.hxx
888dc3801711dbc290299fedea9bc45cf10b5a79
[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 _TMarks, class _TSeeds >
11 void fpa::Base::Algorithm< _TFilter, _TMarks, _TSeeds >::
12 InvokeEvent( const itk::EventObject& e )
13 {
14   TEvent a;
15   if( a.CheckEvent( &e ) )
16   {
17     if( this->m_VisualDebug )
18       this->Superclass::InvokeEvent( e );
19   }
20   else
21     this->Superclass::InvokeEvent( e );
22 }
23
24 // -------------------------------------------------------------------------
25 template< class _TFilter, class _TMarks, class _TSeeds >
26 void fpa::Base::Algorithm< _TFilter, _TMarks, _TSeeds >::
27 InvokeEvent( const itk::EventObject& e ) const
28 {
29   TEvent a;
30   if( a.CheckEvent( &e ) )
31   {
32     if( this->m_VisualDebug )
33       this->Superclass::InvokeEvent( e );
34   }
35   else
36     this->Superclass::InvokeEvent( e );
37 }
38
39 // -------------------------------------------------------------------------
40 template< class _TFilter, class _TMarks, class _TSeeds >
41 fpa::Base::Algorithm< _TFilter, _TMarks, _TSeeds >::
42 Algorithm( )
43   : _TFilter( ),
44     _TMarks( this ),
45     _TSeeds( this ),
46     m_VisualDebug( false )
47 {
48 }
49
50 // -------------------------------------------------------------------------
51 template< class _TFilter, class _TMarks, class _TSeeds >
52 fpa::Base::Algorithm< _TFilter, _TMarks, _TSeeds >::
53 ~Algorithm( )
54 {
55 }
56
57 // -------------------------------------------------------------------------
58 template< class _TFilter, class _TMarks, class _TSeeds >
59 void fpa::Base::Algorithm< _TFilter, _TMarks, _TSeeds >::
60 GenerateData( )
61 {
62   this->InvokeEvent( itk::StartEvent( ) );
63
64   // Init objects
65   this->_BeforeGenerateData( );
66   this->_ConfigureOutput( this->m_InitValue );
67   this->_InitMarks( this->GetSeeds( ).size( ) );
68   TNodes seeds = this->_UnifySeeds( );
69   this->_PrepareSeeds( seeds );
70
71   // Init queue
72   this->_QueueInit( );
73   typename TNodes::const_iterator sIt = seeds.begin( );
74   for( ; sIt != seeds.end( ); ++sIt )
75   {
76     this->_QueuePush( *sIt );
77     this->InvokeEvent( TEvent( sIt->Vertex, sIt->FrontId, true ) );
78
79   } // rof
80
81   // Main loop
82   while( this->_QueueSize( ) > 0 )
83   {
84     // Get next candidate
85     TNode node = this->_QueuePop( );
86     this->InvokeEvent( TEvent( node.Vertex, node.FrontId, false ) );
87     if( !( this->_IsMarked( node.Vertex ) ) )
88     {
89       // Update output value and mark vertex
90       this->_UpdateOutputValue( node );
91       this->_Mark( node.Vertex, node.FrontId );
92
93       // The actual node was effectively marked?
94       if( node.FrontId > 0 )
95       {
96         // Add neighborhood
97         TNeighborhood neighbors = this->_GetNeighbors( node.Vertex );
98         typename TNeighborhood::const_iterator nIt = neighbors.begin( );
99         bool coll = false;
100         while( nIt != neighbors.end( ) && !coll )
101         {
102           if( this->_IsMarked( *nIt ) )
103           {
104             // Invoke stop at collisions
105             if( this->_Collisions( node.Vertex, *nIt ) )
106             {
107               this->_QueueClear( );
108               coll = true;
109
110             } // fi
111           }
112           else
113           {
114             TNode nnode;
115             nnode.Vertex = *nIt;
116             nnode.Parent = node.Vertex;
117             nnode.FrontId = node.FrontId;
118             this->_ComputeOutputValue( nnode );
119             this->_QueuePush( nnode );
120             this->InvokeEvent( TEvent( nnode.Vertex, nnode.FrontId, true ) );
121
122           } // fi
123           ++nIt;
124
125         } // elihw
126
127       } // fi
128
129     } // fi
130     this->_FinishOneLoop( );
131
132   } // elihw
133
134   // Finish
135   this->_AfterGenerateData( );
136   this->InvokeEvent( itk::EndEvent( ) );
137 }
138
139 // -------------------------------------------------------------------------
140 template< class _TFilter, class _TMarks, class _TSeeds >
141 void fpa::Base::Algorithm< _TFilter, _TMarks, _TSeeds >::
142 _BeforeGenerateData( )
143 {
144 }
145
146 // -------------------------------------------------------------------------
147 template< class _TFilter, class _TMarks, class _TSeeds >
148 void fpa::Base::Algorithm< _TFilter, _TMarks, _TSeeds >::
149 _AfterGenerateData( )
150 {
151 }
152
153 // -------------------------------------------------------------------------
154 template< class _TFilter, class _TMarks, class _TSeeds >
155 void fpa::Base::Algorithm< _TFilter, _TMarks, _TSeeds >::
156 _FinishOneLoop( )
157 {
158 }
159
160 // -------------------------------------------------------------------------
161 template< class _TFilter, class _TMarks, class _TSeeds >
162 void fpa::Base::Algorithm< _TFilter, _TMarks, _TSeeds >::
163 _QueueInit( )
164 {
165   this->_QueueClear( );
166 }
167
168 #endif // __fpa__Base__Algorithm__hxx__
169
170 // eof - $RCSfile$