]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/Mori.hxx
3d1a94dfa63e996cbf36aa580a8ae15bdd964660
[FrontAlgorithms.git] / lib / fpa / Base / Mori.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __fpa__Base__Mori__hxx__
7 #define __fpa__Base__Mori__hxx__
8
9 // -------------------------------------------------------------------------
10 template< class _TAlgorithm >
11 itk::ModifiedTimeType fpa::Base::Mori< _TAlgorithm >::
12 GetMTime( ) const
13 {
14   itk::ModifiedTimeType t = this->Superclass::GetMTime( );
15   itk::ModifiedTimeType q = this->m_Predicate->GetMTime( );
16   return( ( q < t )? q: t );
17 }
18
19 // -------------------------------------------------------------------------
20 template< class _TAlgorithm >
21 typename fpa::Base::Mori< _TAlgorithm >::
22 TOutputValue fpa::Base::Mori< _TAlgorithm >::
23 GetOutsideValue( ) const
24 {
25   return( this->GetInitValue( ) );
26 }
27
28 // -------------------------------------------------------------------------
29 template< class _TAlgorithm >
30 void fpa::Base::Mori< _TAlgorithm >::
31 SetOutsideValue( const TOutputValue& v )
32 {
33   this->SetInitValue( v );
34 }
35
36 // -------------------------------------------------------------------------
37 template< class _TAlgorithm >
38 unsigned long fpa::Base::Mori< _TAlgorithm >::
39 GetSignalKernelSize( ) const
40 {
41   return( this->m_PeakDetector.GetKernelSize( ) );
42 }
43
44 // -------------------------------------------------------------------------
45 template< class _TAlgorithm >
46 double fpa::Base::Mori< _TAlgorithm >::
47 GetSignalThreshold( ) const
48 {
49   return( this->m_PeakDetector.GetThreshold( ) );
50 }
51
52 // -------------------------------------------------------------------------
53 template< class _TAlgorithm >
54 double fpa::Base::Mori< _TAlgorithm >::
55 GetSignalInfluence( ) const
56 {
57   return( this->m_PeakDetector.GetInfluence( ) );
58 }
59
60 // -------------------------------------------------------------------------
61 template< class _TAlgorithm >
62 void fpa::Base::Mori< _TAlgorithm >::
63 SetSignalKernelSize( unsigned long k )
64 {
65   this->m_PeakDetector.SetKernelSize( k );
66   this->Modified( );
67 }
68
69 // -------------------------------------------------------------------------
70 template< class _TAlgorithm >
71 void fpa::Base::Mori< _TAlgorithm >::
72 SetSignalThreshold( double t )
73 {
74   this->m_PeakDetector.SetThreshold( t );
75   this->Modified( );
76 }
77
78 // -------------------------------------------------------------------------
79 template< class _TAlgorithm >
80 void fpa::Base::Mori< _TAlgorithm >::
81 SetSignalInfluence( double i )
82 {
83   this->m_PeakDetector.SetInfluence( i );
84   this->Modified( );
85 }
86
87 // -------------------------------------------------------------------------
88 template< class _TAlgorithm >
89 void fpa::Base::Mori< _TAlgorithm >::
90 ClearThresholds( )
91 {
92   this->m_Thresholds.clear( );
93   this->Modified( );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class _TAlgorithm >
98 void fpa::Base::Mori< _TAlgorithm >::
99 AddThreshold( const TInputValue& thr )
100 {
101   if( this->m_Thresholds.insert( thr ).second )
102     this->Modified( );
103 }
104
105 // -------------------------------------------------------------------------
106 template< class _TAlgorithm >
107 void fpa::Base::Mori< _TAlgorithm >::
108 SetThresholds(
109   const TInputValue& init,
110   const TInputValue& end,
111   const TInputValue& delta
112   )
113 {
114   for( TInputValue thr = init; thr <= end; thr += delta )
115     this->AddThreshold( thr );
116 }
117
118 // -------------------------------------------------------------------------
119 template< class _TAlgorithm >
120 const typename fpa::Base::Mori< _TAlgorithm >::
121 TThresholds& fpa::Base::Mori< _TAlgorithm >::
122 GetThresholds( ) const
123 {
124   return( this->m_Thresholds );
125 }
126
127 // -------------------------------------------------------------------------
128 template< class _TAlgorithm >
129 unsigned long fpa::Base::Mori< _TAlgorithm >::
130 GetNumberOfEvaluatedThresholds( ) const
131 {
132   return( this->m_PeakDetector.GetNumberOfSamples( ) );
133 }
134
135 // -------------------------------------------------------------------------
136 template< class _TAlgorithm >
137 typename fpa::Base::Mori< _TAlgorithm >::
138 TInputValue fpa::Base::Mori< _TAlgorithm >::
139 GetOptimumThreshold( ) const
140 {
141   TInputValue thr = TInputValue( 0 );
142   unsigned long n = this->m_PeakDetector.GetNumberOfSamples( );
143   if( n > 1 )
144     thr = TInputValue( this->m_PeakDetector.GetXValues( )[ n - 2 ] );
145   return( thr );
146 }
147
148 // -------------------------------------------------------------------------
149 template< class _TAlgorithm >
150 void fpa::Base::Mori< _TAlgorithm >::
151 GetSignalValues( unsigned long i, double& x, double& y, TPeak& p ) const
152 {
153   if( i < this->m_PeakDetector.GetNumberOfSamples( ) )
154   {
155     x = this->m_PeakDetector.GetXValues( )[ i ];
156     y = this->m_PeakDetector.GetYValues( )[ i ];
157     p = this->m_PeakDetector.GetPeaks( )[ i ];
158
159   } // fi
160 }
161
162 // -------------------------------------------------------------------------
163 template< class _TAlgorithm >
164 fpa::Base::Mori< _TAlgorithm >::
165 Mori( )
166   : Superclass( ),
167      m_InsideValue( TOutputValue( 1 ) )
168 {
169   this->SetInitValue( TOutputValue( 0 ) );
170   this->m_Predicate = TPredicate::New( );
171   this->m_Predicate->StrictOff( );
172   if( std::numeric_limits< TInputValue >::is_integer )
173     this->m_MinimumThreshold = std::numeric_limits< TInputValue >::min( );
174   else
175     this->m_MinimumThreshold = -std::numeric_limits< TInputValue >::max( );
176   this->m_PeakDetector.SetKernelSize( 20 );
177   this->m_PeakDetector.SetThreshold( 500 );
178   this->m_PeakDetector.SetInfluence( 0.5 );
179 }
180
181 // -------------------------------------------------------------------------
182 template< class _TAlgorithm >
183 fpa::Base::Mori< _TAlgorithm >::
184 ~Mori( )
185 {
186 }
187
188 // -------------------------------------------------------------------------
189 template< class _TAlgorithm >
190 void fpa::Base::Mori< _TAlgorithm >::
191 _BeforeGenerateData( )
192 {
193   this->Superclass::_BeforeGenerateData( );
194
195   // Prepare queues
196   this->_QueueClear( );
197   this->m_CurrQueue = 0;
198
199   // Prepare iteration over all thresholds
200   this->m_CurrThr = this->m_Thresholds.begin( );
201   this->m_Predicate->SetLower( *( this->m_CurrThr ) );
202   this->m_CurrThr++;
203   this->m_Predicate->SetUpper( *( this->m_CurrThr ) );
204
205   // Prepare counting signal
206   this->m_CurrCount = double( 0 );
207   this->m_PeakDetector.Clear( );
208 }
209
210 // -------------------------------------------------------------------------
211 template< class _TAlgorithm >
212 void fpa::Base::Mori< _TAlgorithm >::
213 _FinishOneLoop( )
214 {
215   if( this->m_Queues[ this->m_CurrQueue ].size( ) == 0 )
216   {
217     // Update peak detector
218     TPeak p = this->m_PeakDetector.AddValue(
219       *this->m_CurrThr, this->m_CurrCount
220       );
221     std::cout << *( this->m_CurrThr ) << " " << this->m_CurrCount << std::endl;
222     this->m_CurrThr++;
223     if( this->m_CurrThr != this->m_Thresholds.end( ) )
224     {
225       // Update predicate and counting value
226       this->m_Predicate->SetUpper( *( this->m_CurrThr ) );
227       this->m_CurrCount = double( 0 );
228
229       // Peak detected? -> stop!
230       if(
231         p == TPeakDetector::PosPeak &&
232         this->m_MinimumThreshold < *( this->m_CurrThr )
233         )
234         this->_QueueClear( );
235     }
236     else
237       this->_QueueClear( );
238
239   } // fi
240 }
241
242 // -------------------------------------------------------------------------
243 template< class _TAlgorithm >
244 void fpa::Base::Mori< _TAlgorithm >::
245 _ComputeOutputValue( TNode& n )
246 {
247   // Do nothing!!!
248 }
249
250 // -------------------------------------------------------------------------
251 template< class _TAlgorithm >
252 void fpa::Base::Mori< _TAlgorithm >::
253 _UpdateOutputValue( TNode& n )
254 {
255   TInputValue value = this->_GetInputValue( n.Vertex );
256   bool inside = this->m_Predicate->Evaluate( value );
257   if( !inside )
258   {
259     n.Value = this->m_InitValue;
260     n.FrontId++;
261     this->m_Queues[ ( this->m_CurrQueue + 1 ) % 2 ].push_back( n );
262     n.FrontId = 0;
263   }
264   else
265   {
266     n.Value = this->m_InsideValue;
267     this->m_CurrCount += double( 1 );
268
269   } // fi
270   this->Superclass::_UpdateOutputValue( n );
271 }
272
273 // -------------------------------------------------------------------------
274 template< class _TAlgorithm >
275 void fpa::Base::Mori< _TAlgorithm >::
276 _QueueClear( )
277 {
278   this->m_Queues[ 0 ].clear( );
279   this->m_Queues[ 1 ].clear( );
280 }
281
282 // -------------------------------------------------------------------------
283 template< class _TAlgorithm >
284 typename fpa::Base::Mori< _TAlgorithm >::
285 TNode fpa::Base::Mori< _TAlgorithm >::
286 _QueuePop( )
287 {
288   TNode n = this->m_Queues[ this->m_CurrQueue ].front( );
289   this->m_Queues[ this->m_CurrQueue ].pop_front( );
290   return( n );
291 }
292
293 // -------------------------------------------------------------------------
294 template< class _TAlgorithm >
295 void fpa::Base::Mori< _TAlgorithm >::
296 _QueuePush( const TNode& node )
297 {
298   this->m_Queues[ this->m_CurrQueue ].push_back( node );
299 }
300
301 // -------------------------------------------------------------------------
302 template< class _TAlgorithm >
303 unsigned long fpa::Base::Mori< _TAlgorithm >::
304 _QueueSize( ) const
305 {
306   return( this->m_Queues[ this->m_CurrQueue ].size( ) );
307 }
308
309 // -------------------------------------------------------------------------
310 template< class _TAlgorithm >
311 void fpa::Base::Mori< _TAlgorithm >::
312 _PrepareSeeds( TNodes& nodes )
313 {
314   typename TNodes::iterator nIt = nodes.begin( );
315   for( ; nIt != nodes.end( ); ++nIt )
316     nIt->Value = this->m_InitValue;
317 }
318
319 #endif // __fpa__Base__Mori__hxx__
320
321 // eof - $RCSfile$