]> 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->SetFillValue( TOutputValue( 0 ) );
161   this->m_InsideValue = std::numeric_limits< TOutputValue >::max( );
162   this->m_Predicate = TPredicate::New( );
163   this->m_Predicate->StrictOff( );
164   if( std::numeric_limits< TInputValue >::is_integer )
165     this->m_MinimumThreshold = std::numeric_limits< TInputValue >::min( );
166   else
167     this->m_MinimumThreshold = -std::numeric_limits< TInputValue >::max( );
168   this->m_PeakDetector.SetKernelSize( 20 );
169   this->m_PeakDetector.SetThreshold( 500 );
170   this->m_PeakDetector.SetInfluence( 0.5 );
171 }
172
173 // -------------------------------------------------------------------------
174 template< class _TDataInterface >
175 fpa::Filters::Mori< _TDataInterface >::
176 ~Mori( )
177 {
178 }
179
180 // -------------------------------------------------------------------------
181 template< class _TDataInterface >
182 void fpa::Filters::Mori< _TDataInterface >::
183 _BeforeGenerateData( )
184 {
185   this->Superclass::_BeforeGenerateData( );
186
187   // Prepare queues
188   this->_QueueClear( );
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 _TDataInterface >
203 void fpa::Filters::Mori< _TDataInterface >::
204 _PostComputeOutputValue( TNode& n )
205 {
206   TInputValue value = this->_GetInputValue( n.Vertex );
207   bool inside = this->m_Predicate->Evaluate( value );
208   if( !inside )
209   {
210     n.Value = this->m_InitValue;
211     n.FrontId++;
212     this->_QueueSwap( );
213     this->_QueuePush( n );
214     this->_QueueSwap( );
215     n.FrontId = 0;
216   }
217   else
218   {
219     n.Value = this->m_InsideValue;
220     this->m_CurrCount += double( 1 );
221
222   } // fi
223 }
224
225 // -------------------------------------------------------------------------
226 template< class _TDataInterface >
227 void fpa::Filters::Mori< _TDataInterface >::
228 _PreComputeOutputValue( TNode& n )
229 {
230   // Nothing to do with this algorithm
231 }
232
233 // -------------------------------------------------------------------------
234 template< class _TDataInterface >
235 void fpa::Filters::Mori< _TDataInterface >::
236 _Reinitialize( )
237 {
238   if( this->_QueueSize( ) == 0 )
239   {
240     // Update peak detector
241     TPeak p = this->m_PeakDetector.AddValue(
242       *this->m_CurrThr, this->m_CurrCount
243       );
244     this->m_CurrThr++;
245     this->_QueueSwap( );
246     if( this->m_CurrThr != this->m_Thresholds.end( ) )
247     {
248       // Update predicate and counting value
249       this->m_Predicate->SetUpperThreshold( *( this->m_CurrThr ) );
250       this->m_CurrCount = double( 0 );
251
252       // Peak detected? -> stop!
253       if(
254         p == TPeakDetector::PosPeak &&
255         this->m_MinimumThreshold < *( this->m_CurrThr )
256         )
257         this->_QueueClear( );
258     }
259     else
260       this->_QueueClear( );
261
262   } // fi
263 }
264
265 #endif // __fpa__Filters__Mori__hxx__
266 // eof - $RCSfile$