1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
6 #ifndef __fpa__Base__Mori__hxx__
7 #define __fpa__Base__Mori__hxx__
9 // -------------------------------------------------------------------------
10 template< class _TAlgorithm >
11 itk::ModifiedTimeType fpa::Base::Mori< _TAlgorithm >::
14 itk::ModifiedTimeType t = this->Superclass::GetMTime( );
15 itk::ModifiedTimeType q = this->m_Predicate->GetMTime( );
16 return( ( q < t )? q: t );
19 // -------------------------------------------------------------------------
20 template< class _TAlgorithm >
21 typename fpa::Base::Mori< _TAlgorithm >::
22 TOutputValue fpa::Base::Mori< _TAlgorithm >::
23 GetOutsideValue( ) const
25 return( this->GetInitValue( ) );
28 // -------------------------------------------------------------------------
29 template< class _TAlgorithm >
30 void fpa::Base::Mori< _TAlgorithm >::
31 SetOutsideValue( const TOutputValue& v )
33 this->SetInitValue( v );
36 // -------------------------------------------------------------------------
37 template< class _TAlgorithm >
38 void fpa::Base::Mori< _TAlgorithm >::
41 this->m_Thresholds.clear( );
45 // -------------------------------------------------------------------------
46 template< class _TAlgorithm >
47 void fpa::Base::Mori< _TAlgorithm >::
48 AddThreshold( const TInputValue& thr )
50 if( this->m_Thresholds.insert( thr ).second )
54 // -------------------------------------------------------------------------
55 template< class _TAlgorithm >
56 void fpa::Base::Mori< _TAlgorithm >::
58 const TInputValue& init,
59 const TInputValue& end,
60 const TInputValue& delta
63 for( TInputValue thr = init; thr <= end; thr += delta )
64 this->AddThreshold( thr );
67 // -------------------------------------------------------------------------
68 template< class _TAlgorithm >
69 unsigned long fpa::Base::Mori< _TAlgorithm >::
70 GetNumberOfEvaluatedThresholds( ) const
72 return( this->m_Signal.size( ) );
75 // -------------------------------------------------------------------------
76 template< class _TAlgorithm >
77 typename fpa::Base::Mori< _TAlgorithm >::
78 TInputValue fpa::Base::Mori< _TAlgorithm >::
79 GetOptimumThreshold( ) const
81 TInputValue thr = TInputValue( 0 );
82 if( this->m_Signal.size( ) > 1 )
83 thr = this->m_Signal[ this->m_Signal.size( ) - 2 ].first;
87 // -------------------------------------------------------------------------
88 template< class _TAlgorithm >
89 fpa::Base::Mori< _TAlgorithm >::
93 m_SignalThreshold( 500 ),
94 m_SignalInfluence( 0.5 ),
95 m_InsideValue( TOutputValue( 1 ) )
97 this->SetInitValue( TOutputValue( 0 ) );
98 this->m_Predicate = TPredicate::New( );
99 this->m_Predicate->StrictOff( );
102 // -------------------------------------------------------------------------
103 template< class _TAlgorithm >
104 fpa::Base::Mori< _TAlgorithm >::
109 // -------------------------------------------------------------------------
110 template< class _TAlgorithm >
111 void fpa::Base::Mori< _TAlgorithm >::
112 _BeforeGenerateData( )
114 this->Superclass::_BeforeGenerateData( );
116 this->_QueueClear( );
117 this->m_CurrentQueue = 0;
118 this->m_CurrentThreshold = this->m_Thresholds.begin( );
119 this->m_Predicate->SetLower( *( this->m_CurrentThreshold ) );
120 this->m_CurrentThreshold++;
121 this->m_Predicate->SetUpper( *( this->m_CurrentThreshold ) );
123 this->m_Signal.clear( );
124 this->m_FilteredSignal.clear( );
125 this->m_SignalAverages.clear( );
126 this->m_SignalDeviations.clear( );
127 this->m_SignalPeaks.clear( );
128 this->m_CurrentAverage = double( 0 );
129 this->m_CurrentVariance = double( 0 );
132 // -------------------------------------------------------------------------
133 template< class _TAlgorithm >
134 void fpa::Base::Mori< _TAlgorithm >::
137 if( this->m_Queues[ this->m_CurrentQueue ].size( ) == 0 )
139 this->m_Signal.push_back(
140 TSignalData( *this->m_CurrentThreshold, this->m_Count )
142 this->m_CurrentThreshold++;
143 this->m_CurrentQueue = ( this->m_CurrentQueue + 1 ) % 2;
144 if( this->m_CurrentThreshold != this->m_Thresholds.end( ) )
146 if( this->m_FilteredSignal.size( ) < this->m_SignalLag )
148 double v = double( this->m_Count );
149 double n = double( this->m_FilteredSignal.size( ) + 1 );
150 this->m_FilteredSignal.push_back( v );
151 this->m_SignalAverages.push_back( double( 0 ) );
152 this->m_SignalDeviations.push_back( double( 0 ) );
153 this->m_SignalPeaks.push_back( 0 );
154 if( n > double( 1 ) )
155 this->m_CurrentVariance =
156 ( ( ( n - 2.0 ) / ( n - 1.0 ) ) * this->m_CurrentVariance ) +
158 ( v - this->m_CurrentAverage ) *
159 ( v - this->m_CurrentAverage )
161 this->m_CurrentAverage += ( v - this->m_CurrentAverage ) / n;
162 if( this->m_FilteredSignal.size( ) == this->m_SignalLag )
164 this->m_SignalAverages.push_back( this->m_CurrentAverage );
165 this->m_SignalDeviations.push_back(
166 std::sqrt( this->m_CurrentVariance )
173 unsigned long i = this->m_Signal.size( ) - 1;
174 double v = double( this->m_Count );
176 ( std::fabs( v - this->m_SignalAverages[ i - 1 ] ) ) >
177 ( this->m_SignalThreshold * this->m_SignalDeviations[ i - 1 ] )
180 if( v > this->m_SignalAverages[ i - 1 ] )
181 this->m_SignalPeaks.push_back( 1 );
183 this->m_SignalPeaks.push_back( -1 );
184 this->m_FilteredSignal.push_back(
185 ( this->m_SignalInfluence * v ) +
187 ( 1.0 - this->m_SignalInfluence ) *
188 this->m_FilteredSignal[ i - 1 ]
194 this->m_SignalPeaks.push_back( 0 );
195 this->m_FilteredSignal.push_back( v );
199 double avg = double( 0 );
200 double var = double( 0 );
202 for( unsigned long j = i - this->m_SignalLag; j <= i; ++j, ++k )
204 double v = this->m_FilteredSignal[ j ];
205 double n = double( k + 1 );
208 ( ( ( n - 2.0 ) / ( n - 1.0 ) ) * var ) +
209 ( ( ( v - avg ) * ( v - avg ) ) / n );
210 avg += ( v - avg ) / n;
213 this->m_SignalAverages.push_back( avg );
214 this->m_SignalDeviations.push_back( std::sqrt( var ) );
217 this->m_Predicate->SetUpper( *( this->m_CurrentThreshold ) );
220 // Peak detected? -> stop!
221 if( this->m_SignalPeaks.back( ) == 1 )
222 this->_QueueClear( );
225 this->_QueueClear( );
230 // -------------------------------------------------------------------------
231 template< class _TAlgorithm >
232 void fpa::Base::Mori< _TAlgorithm >::
233 _ComputeOutputValue( TNode& n )
238 // -------------------------------------------------------------------------
239 template< class _TAlgorithm >
240 void fpa::Base::Mori< _TAlgorithm >::
241 _UpdateOutputValue( TNode& n )
243 TInputValue value = this->_GetInputValue( n.Vertex );
244 bool inside = this->m_Predicate->Evaluate( value );
247 n.Value = this->m_InitValue;
249 this->m_Queues[ ( this->m_CurrentQueue + 1 ) % 2 ].push_back( n );
254 n.Value = this->m_InsideValue;
258 this->Superclass::_UpdateOutputValue( n );
261 // -------------------------------------------------------------------------
262 template< class _TAlgorithm >
263 void fpa::Base::Mori< _TAlgorithm >::
266 this->m_Queues[ 0 ].clear( );
267 this->m_Queues[ 1 ].clear( );
270 // -------------------------------------------------------------------------
271 template< class _TAlgorithm >
272 typename fpa::Base::Mori< _TAlgorithm >::
273 TNode fpa::Base::Mori< _TAlgorithm >::
276 TNode n = this->m_Queues[ this->m_CurrentQueue ].front( );
277 this->m_Queues[ this->m_CurrentQueue ].pop_front( );
281 // -------------------------------------------------------------------------
282 template< class _TAlgorithm >
283 void fpa::Base::Mori< _TAlgorithm >::
284 _QueuePush( const TNode& node )
286 this->m_Queues[ this->m_CurrentQueue ].push_back( node );
289 // -------------------------------------------------------------------------
290 template< class _TAlgorithm >
291 unsigned long fpa::Base::Mori< _TAlgorithm >::
294 return( this->m_Queues[ this->m_CurrentQueue ].size( ) );
297 // -------------------------------------------------------------------------
298 template< class _TAlgorithm >
299 void fpa::Base::Mori< _TAlgorithm >::
300 _PrepareSeeds( TNodes& nodes )
302 typename TNodes::iterator nIt = nodes.begin( );
303 for( ; nIt != nodes.end( ); ++nIt )
304 nIt->Value = this->m_InitValue;
307 #endif // __fpa__Base__Mori__hxx__