1 #ifndef __FPA__BASE__ALGORITHM__HXX__
2 #define __FPA__BASE__ALGORITHM__HXX__
6 // -------------------------------------------------------------------------
7 template< class V, class C, class R, class B >
8 fpa::Base::Algorithm< V, C, R, B >::_TNode::
10 : Result( TResult( 0 ) ),
12 Label( Self::FarLabel )
16 // -------------------------------------------------------------------------
17 template< class V, class C, class R, class B >
18 fpa::Base::Algorithm< V, C, R, B >::_TNode::
23 // -------------------------------------------------------------------------
24 template< class V, class C, class R, class B >
25 void fpa::Base::Algorithm< V, C, R, B >::
26 InvokeEvent( const itk::EventObject& e )
28 if( this->m_ThrowEvents )
29 this->Superclass::InvokeEvent( e );
32 // -------------------------------------------------------------------------
33 template< class V, class C, class R, class B >
34 void fpa::Base::Algorithm< V, C, R, B >::
35 InvokeEvent( const itk::EventObject& e ) const
37 if( this->m_ThrowEvents )
38 this->Superclass::InvokeEvent( e );
41 // -------------------------------------------------------------------------
42 template< class V, class C, class R, class B >
43 void fpa::Base::Algorithm< V, C, R, B >::
44 AddSeed( const TVertex& s, const TResult& r )
50 ns.FrontId = this->m_Seeds.size( );
51 ns.Label = Self::FrontLabel;
52 this->m_Seeds.push_back( ns );
56 // -------------------------------------------------------------------------
57 template< class V, class C, class R, class B >
58 const typename fpa::Base::Algorithm< V, C, R, B >::
59 TVertex& fpa::Base::Algorithm< V, C, R, B >::
60 GetSeed( const unsigned int& id ) const
62 return( this->m_Seeds[ id ].Vertex );
65 // -------------------------------------------------------------------------
66 template< class V, class C, class R, class B >
67 void fpa::Base::Algorithm< V, C, R, B >::
70 this->m_Seeds.clear( );
74 // -------------------------------------------------------------------------
75 template< class V, class C, class R, class B >
76 unsigned long fpa::Base::Algorithm< V, C, R, B >::
77 GetNumberOfSeeds( ) const
79 return( this->m_Seeds.size( ) );
82 // -------------------------------------------------------------------------
83 template< class V, class C, class R, class B >
84 fpa::Base::Algorithm< V, C, R, B >::
87 m_ThrowEvents( false ),
88 m_StopAtOneFront( false )
92 // -------------------------------------------------------------------------
93 template< class V, class C, class R, class B >
94 fpa::Base::Algorithm< V, C, R, B >::
99 // -------------------------------------------------------------------------
100 template< class V, class C, class R, class B >
101 void fpa::Base::Algorithm< V, C, R, B >::
104 unsigned long N = this->m_Seeds.size( );
108 this->InvokeEvent( TStartEvent( ) );
109 this->_BeforeGenerateData( );
111 this->m_Collisions.clear( );
113 resize( N, _TCollisionsRow( N, _TCollision( TVertex( ), false ) ) );
114 this->_InitResults( );
118 this->_AfterGenerateData( );
119 this->InvokeEvent( TEndEvent( ) );
122 // -------------------------------------------------------------------------
123 template< class V, class C, class R, class B >
124 void fpa::Base::Algorithm< V, C, R, B >::
127 this->InvokeEvent( TStartLoopEvent( ) );
128 this->_BeforeLoop( );
129 while( !( this->_IsQueueEmpty( ) ) )
131 // Get next candidate
132 _TNode candidate = this->_QueuePop( );
133 if( this->_Node( candidate.Vertex ).Label == Self::AliveLabel )
136 // Mark it as "Alive" and update final result
137 candidate.Label = Self::AliveLabel;
138 this->_Mark( candidate );
139 this->_SetResult( candidate.Vertex, candidate.Result );
140 this->InvokeEvent( TAliveEvent( candidate.Vertex, candidate.FrontId ) );
142 // Check if a forced stop condition arises
143 if( !( this->_NeedToStop( ) ) )
145 // Compute neighborhood
146 _TVertices neighborhood;
147 this->_Neighborhood( neighborhood, candidate.Vertex );
149 // Iterate over neighbors
150 typename _TVertices::iterator nIt = neighborhood.begin( );
151 while( nIt != neighborhood.end( ) )
153 _TNode neighbor = this->_Node( *nIt );
154 neighbor.Vertex = *nIt;
155 if( neighbor.Label == Self::AliveLabel )
158 if( this->_UpdateCollisions( candidate.Vertex, *nIt ) )
160 this->_QueueClear( );
161 nIt = neighborhood.end( );
168 // Add new candidate to queue
170 this->_ComputeNeighborResult(
171 neighbor.Result, *nIt, candidate.Vertex
175 neighbor.FrontId = candidate.FrontId;
176 neighbor.Parent = candidate.Vertex;
177 neighbor.Label = Self::FrontLabel;
178 this->_QueuePush( neighbor );
179 this->_Mark( neighbor );
180 this->InvokeEvent( TFrontEvent( *nIt, candidate.FrontId ) );
183 this->InvokeEvent( TFreezeEvent( *nIt, candidate.FrontId ) );
191 this->_QueueClear( );
195 this->InvokeEvent( TEndLoopEvent( ) );
198 // -------------------------------------------------------------------------
199 template< class V, class C, class R, class B >
200 void fpa::Base::Algorithm< V, C, R, B >::
201 _BeforeGenerateData( )
205 // -------------------------------------------------------------------------
206 template< class V, class C, class R, class B >
207 void fpa::Base::Algorithm< V, C, R, B >::
208 _AfterGenerateData( )
212 // -------------------------------------------------------------------------
213 template< class V, class C, class R, class B >
214 void fpa::Base::Algorithm< V, C, R, B >::
219 // -------------------------------------------------------------------------
220 template< class V, class C, class R, class B >
221 void fpa::Base::Algorithm< V, C, R, B >::
226 // -------------------------------------------------------------------------
227 template< class V, class C, class R, class B >
228 bool fpa::Base::Algorithm< V, C, R, B >::
229 _UpdateCollisions( const TVertex& a, const TVertex& b )
232 _TNode na = this->_Node( a );
233 _TNode nb = this->_Node( b );
234 long fa = na.FrontId;
235 long fb = nb.FrontId;
239 // Mark collision, if it is new
240 bool exists = this->m_Collisions[ fa ][ fb ].second;
241 exists &= this->m_Collisions[ fb ][ fa ].second;
245 this->m_Collisions[ fa ][ fb ].first = na.Vertex;
246 this->m_Collisions[ fa ][ fb ].second = true;
247 this->m_Collisions[ fb ][ fa ].first = nb.Vertex;
248 this->m_Collisions[ fb ][ fa ].second = true;
250 // Stop if one front is desired
251 if( this->m_StopAtOneFront )
253 // Perform a depth-first iteration on front graph
254 unsigned long N = this->GetNumberOfSeeds( );
255 unsigned long count = 0;
256 std::vector< bool > m( N, false );
257 std::queue< unsigned long > q;
261 unsigned long f = q.front( );
269 for( unsigned int n = 0; n < N; ++n )
270 if( this->m_Collisions[ f ][ n ].second && !m[ n ] )
274 ret = ( count == N );
284 // -------------------------------------------------------------------------
285 template< class V, class C, class R, class B >
286 bool fpa::Base::Algorithm< V, C, R, B >::
292 // -------------------------------------------------------------------------
293 template< class V, class C, class R, class B >
294 void fpa::Base::Algorithm< V, C, R, B >::
297 this->_QueueClear( );
299 typename _TNodes::const_iterator sIt = this->m_Seeds.begin( );
300 sIt != this->m_Seeds.end( );
304 this->_QueuePush( *sIt );
310 #endif // __FPA__BASE__ALGORITHM__HXX__