]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Filters/MarksWithCollisionsInterface.hxx
9a9852c34cd2a0b9200ca65158f1228a672390af
[FrontAlgorithms.git] / lib / fpa / Filters / MarksWithCollisionsInterface.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5 #ifndef __fpa__Filters__MarksWithCollisionsInterface__hxx__
6 #define __fpa__Filters__MarksWithCollisionsInterface__hxx__
7
8 #include <queue>
9
10 // -------------------------------------------------------------------------
11 template< class _TTraits >
12 fpa::Filters::MarksWithCollisionsInterface< _TTraits >::
13 MarksWithCollisionsInterface( itk::ProcessObject* f )
14   : Superclass( f ),
15     m_StopAtOneFront( false ),
16     m_NumberOfFronts( 0 ),
17     m_NumberOfSeeds( 0 )
18 {
19 }
20
21 // -------------------------------------------------------------------------
22 template< class _TTraits >
23 fpa::Filters::MarksWithCollisionsInterface< _TTraits >::
24 ~MarksWithCollisionsInterface( )
25 {
26 }
27
28 // -------------------------------------------------------------------------
29 template< class _TTraits >
30 bool fpa::Filters::MarksWithCollisionsInterface< _TTraits >::
31 StopAtOneFront( ) const
32 {
33   return( this->m_StopAtOneFront );
34 }
35
36 // -------------------------------------------------------------------------
37 template< class _TTraits >
38 void fpa::Filters::MarksWithCollisionsInterface< _TTraits >::
39 StopAtOneFrontOn( )
40 {
41   this->SetStopAtOneFront( true );
42 }
43
44 // -------------------------------------------------------------------------
45 template< class _TTraits >
46 void fpa::Filters::MarksWithCollisionsInterface< _TTraits >::
47 StopAtOneFrontOff( )
48 {
49   this->SetStopAtOneFront( false );
50 }
51
52 // -------------------------------------------------------------------------
53 template< class _TTraits >
54 void fpa::Filters::MarksWithCollisionsInterface< _TTraits >::
55 SetStopAtOneFront( bool v )
56 {
57   if( this->m_StopAtOneFront != v )
58   {
59     this->m_StopAtOneFront = v;
60     if( this->m_Filter != NULL )
61       this->m_Filter->Modified( );
62
63   } // fi
64 }
65
66 // -------------------------------------------------------------------------
67 template< class _TTraits >
68 void fpa::Filters::MarksWithCollisionsInterface< _TTraits >::
69 _InitCollisions( unsigned long nSeeds )
70 {
71   this->m_NumberOfFronts = this->m_NumberOfSeeds = nSeeds;
72   TCollision coll( TVertex( ), false );
73   TCollisionsRow row( this->m_NumberOfFronts, coll );
74   this->m_Collisions.clear( );
75   this->m_Collisions.resize( this->m_NumberOfFronts, row );
76 }
77
78 // -------------------------------------------------------------------------
79 template< class _TTraits >
80 bool fpa::Filters::MarksWithCollisionsInterface< _TTraits >::
81 _Collisions( const TVertex& a, const TVertex& b )
82 {
83   unsigned long ma = this->_GetMark( a );
84   unsigned long mb = this->_GetMark( b );
85   if( ma == mb || ma == 0 || mb == 0 )
86     return( false );
87
88   // Mark collision, if it is new
89   ma--; mb--;
90   bool ret = false;
91   bool exists = this->m_Collisions[ ma ][ mb ].second;
92   exists     &= this->m_Collisions[ mb ][ ma ].second;
93   if( !exists )
94   {
95     this->m_Collisions[ ma ][ mb ].first = a;
96     this->m_Collisions[ ma ][ mb ].second = true;
97     this->m_Collisions[ mb ][ ma ].first = b;
98     this->m_Collisions[ mb ][ ma ].second = true;
99
100     // Update number of fronts
101     unsigned long count = 0;
102     std::vector< bool > m( this->m_NumberOfSeeds, false );
103     std::queue< unsigned long > q;
104     q.push( 0 );
105     while( !q.empty( ) )
106     {
107       unsigned long f = q.front( );
108       q.pop( );
109
110       if( m[ f ] )
111         continue;
112       m[ f ] = true;
113       count++;
114
115       for( unsigned int n = 0; n < this->m_NumberOfSeeds; ++n )
116         if( this->m_Collisions[ f ][ n ].second && !m[ n ] )
117           q.push( n );
118
119     } // elihw
120     this->m_NumberOfFronts = this->m_NumberOfSeeds - count + 1;
121
122   } // fi
123   return(
124     this->m_StopAtOneFront &&
125     this->m_NumberOfSeeds > 1 &&
126     this->m_NumberOfFronts == 1
127     );
128 }
129
130 #endif // __fpa__Filters__MarksWithCollisionsInterface__hxx__
131 // eof - $RCSfile$