]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Filters/Mori.hxx
...
[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 #include <limits>
9
10 // -------------------------------------------------------------------------
11 template< class _TDataInterface >
12 typename fpa::Filters::Mori< _TDataInterface >::
13 TOutputValue fpa::Filters::Mori< _TDataInterface >::
14 GetOutsideValue( ) const
15 {
16   return( this->GetInitValue( ) );
17 }
18
19 // -------------------------------------------------------------------------
20 template< class _TDataInterface >
21 void fpa::Filters::Mori< _TDataInterface >::
22 SetOutsideValue( const TOutputValue& v )
23 {
24   this->SetInitValue( v );
25 }
26
27 // -------------------------------------------------------------------------
28 template< class _TDataInterface >
29 unsigned long fpa::Filters::Mori< _TDataInterface >::
30 GetSignalKernelSize( ) const
31 {
32   return( this->m_PeakDetector.GetKernelSize( ) );
33 }
34
35 // -------------------------------------------------------------------------
36 template< class _TDataInterface >
37 void fpa::Filters::Mori< _TDataInterface >::
38 SetSignalKernelSize( unsigned long k )
39 {
40   this->m_PeakDetector.SetKernelSize( k );
41   this->Modified( );
42 }
43
44 // -------------------------------------------------------------------------
45 template< class _TDataInterface >
46 double fpa::Filters::Mori< _TDataInterface >::
47 GetSignalThreshold( ) const
48 {
49   return( this->m_PeakDetector.GetThreshold( ) );
50 }
51
52 // -------------------------------------------------------------------------
53 template< class _TDataInterface >
54 void fpa::Filters::Mori< _TDataInterface >::
55 SetSignalThreshold( double t )
56 {
57   this->m_PeakDetector.SetThreshold( t );
58   this->Modified( );
59 }
60
61 // -------------------------------------------------------------------------
62 template< class _TDataInterface >
63 double fpa::Filters::Mori< _TDataInterface >::
64 GetSignalInfluence( ) const
65 {
66   return( this->m_PeakDetector.GetInfluence( ) );
67 }
68
69 // -------------------------------------------------------------------------
70 template< class _TDataInterface >
71 void fpa::Filters::Mori< _TDataInterface >::
72 SetSignalInfluence( double i )
73 {
74   this->m_PeakDetector.SetInfluence( i );
75   this->Modified( );
76 }
77
78 // -------------------------------------------------------------------------
79 template< class _TDataInterface >
80 void fpa::Filters::Mori< _TDataInterface >::
81 ClearThresholds( )
82 {
83   this->m_Thresholds.clear( );
84   this->Modified( );
85 }
86
87 // -------------------------------------------------------------------------
88 template< class _TDataInterface >
89 void fpa::Filters::Mori< _TDataInterface >::
90 AddThreshold( const TInputValue& thr )
91 {
92   if( this->m_Thresholds.insert( thr ).second )
93     this->Modified( );
94 }
95
96 // -------------------------------------------------------------------------
97 template< class _TDataInterface >
98 void fpa::Filters::Mori< _TDataInterface >::
99 SetThresholds(
100   const TInputValue& lower,
101   const TInputValue& upper,
102   const TInputValue& delta
103   )
104 {
105   for( TInputValue t = lower; t <= upper; t += delta )
106     this->AddThreshold( t );
107 }
108
109 // -------------------------------------------------------------------------
110 template< class _TDataInterface >
111 const typename fpa::Filters::Mori< _TDataInterface >::
112 TThresholds& fpa::Filters::Mori< _TDataInterface >::
113 GetThresholds( ) const
114 {
115   return( this->m_Thresholds );
116 }
117
118 // -------------------------------------------------------------------------
119 template< class _TDataInterface >
120 unsigned long fpa::Filters::Mori< _TDataInterface >::
121 GetNumberOfEvaluatedThresholds( ) const
122 {
123   return( this->m_PeakDetector.GetNumberOfSamples( ) );
124 }
125
126 // -------------------------------------------------------------------------
127 template< class _TDataInterface >
128 typename fpa::Filters::Mori< _TDataInterface >::
129 TInputValue fpa::Filters::Mori< _TDataInterface >::
130 GetOptimumThreshold( ) const
131 {
132   TInputValue thr = TInputValue( 0 );
133   unsigned long n = this->m_PeakDetector.GetNumberOfSamples( );
134   if( n > 1 )
135     thr = TInputValue( this->m_PeakDetector.GetXValues( )[ n - 2 ] );
136   return( thr );
137 }
138
139 // -------------------------------------------------------------------------
140 template< class _TDataInterface >
141 void fpa::Filters::Mori< _TDataInterface >::
142 GetSignalValues( unsigned long i, double& x, double& y, TPeak& p ) const
143 {
144   if( i < this->m_PeakDetector.GetNumberOfSamples( ) )
145   {
146     x = this->m_PeakDetector.GetXValues( )[ i ];
147     y = this->m_PeakDetector.GetYValues( )[ i ];
148     p = this->m_PeakDetector.GetPeaks( )[ i ];
149
150   } // fi
151 }
152
153 // -------------------------------------------------------------------------
154 template< class _TDataInterface >
155 fpa::Filters::Mori< _TDataInterface >::
156 Mori( )
157   : Superclass( true )
158 {
159   this->SetOutsideValue( TOutputValue( 0 ) );
160   this->m_InsideValue = std::numeric_limits< TOutputValue >::max( );
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 _TDataInterface >
174 fpa::Filters::Mori< _TDataInterface >::
175 ~Mori( )
176 {
177 }
178
179 // -------------------------------------------------------------------------
180 template< class _TDataInterface >
181 void fpa::Filters::Mori< _TDataInterface >::
182 _BeforeGenerateData( )
183 {
184   this->Superclass::_BeforeGenerateData( );
185
186   // Prepare queues
187   this->_QueueClear( );
188
189   // Prepare iteration over all thresholds
190   this->m_CurrThr = this->m_Thresholds.begin( );
191   this->m_Predicate->SetLowerThreshold( *( this->m_CurrThr ) );
192   this->m_CurrThr++;
193   this->m_Predicate->SetUpperThreshold( *( this->m_CurrThr ) );
194
195   // Prepare counting signal
196   this->m_CurrCount = double( 0 );
197   this->m_PeakDetector.Clear( );
198 }
199
200 // -------------------------------------------------------------------------
201 template< class _TDataInterface >
202 void fpa::Filters::Mori< _TDataInterface >::
203 _PostComputeOutputValue( TNode& n )
204 {
205   TInputValue value = this->_GetInputValue( n.Vertex );
206   bool inside = this->m_Predicate->Evaluate( value );
207   if( !inside )
208   {
209     n.Value = this->m_InitValue;
210     n.FrontId++;
211     this->_QueueSwap( );
212     this->_QueuePush( n );
213     this->_QueueSwap( );
214     n.FrontId = 0;
215   }
216   else
217   {
218     n.Value = this->m_InsideValue;
219     this->m_CurrCount += double( 1 );
220
221   } // fi
222 }
223
224 // -------------------------------------------------------------------------
225 template< class _TDataInterface >
226 void fpa::Filters::Mori< _TDataInterface >::
227 _PreComputeOutputValue( TNode& n )
228 {
229   // Nothing to do with this algorithm
230 }
231
232 // -------------------------------------------------------------------------
233 template< class _TDataInterface >
234 void fpa::Filters::Mori< _TDataInterface >::
235 _Reinitialize( )
236 {
237   if( this->_QueueSize( ) == 0 )
238   {
239     // Update peak detector
240     TPeak p = this->m_PeakDetector.AddValue(
241       *this->m_CurrThr, this->m_CurrCount
242       );
243     this->m_CurrThr++;
244     this->_QueueSwap( );
245     if( this->m_CurrThr != this->m_Thresholds.end( ) )
246     {
247       // Update predicate and counting value
248       this->m_Predicate->SetUpperThreshold( *( this->m_CurrThr ) );
249       this->m_CurrCount = double( 0 );
250
251       // Peak detected? -> stop!
252       if(
253         p == TPeakDetector::PosPeak &&
254         this->m_MinimumThreshold < *( this->m_CurrThr )
255         )
256         this->_QueueClear( );
257     }
258     else
259       this->_QueueClear( );
260
261   } // fi
262 }
263
264 #endif // __fpa__Filters__Mori__hxx__
265 // eof - $RCSfile$