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