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