// ========================================================================= // @author Leonardo Florez Valencia // @email florez-l@javeriana.edu.co // ========================================================================= #ifndef __fpa__Base__Mori__hxx__ #define __fpa__Base__Mori__hxx__ // ------------------------------------------------------------------------- template< class _TAlgorithm > itk::ModifiedTimeType fpa::Base::Mori< _TAlgorithm >:: GetMTime( ) const { itk::ModifiedTimeType t = this->Superclass::GetMTime( ); itk::ModifiedTimeType q = this->m_Predicate->GetMTime( ); return( ( q < t )? q: t ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > typename fpa::Base::Mori< _TAlgorithm >:: TOutputValue fpa::Base::Mori< _TAlgorithm >:: GetOutsideValue( ) const { return( this->GetInitValue( ) ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: SetOutsideValue( const TOutputValue& v ) { this->SetInitValue( v ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: ClearThresholds( ) { this->m_Thresholds.clear( ); this->Modified( ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: AddThreshold( const TInputValue& thr ) { if( this->m_Thresholds.insert( thr ).second ) this->Modified( ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: SetThresholds( const TInputValue& init, const TInputValue& end, const TInputValue& delta ) { for( TInputValue thr = init; thr <= end; thr += delta ) this->AddThreshold( thr ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > fpa::Base::Mori< _TAlgorithm >:: Mori( ) : Superclass( ), m_InsideValue( TOutputValue( 1 ) ) { this->SetInitValue( TOutputValue( 0 ) ); this->m_Predicate = TPredicate::New( ); this->m_Predicate->StrictOff( ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > fpa::Base::Mori< _TAlgorithm >:: ~Mori( ) { } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: _BeforeGenerateData( ) { this->Superclass::_BeforeGenerateData( ); this->_QueueClear( ); this->m_CurrentQueue = 0; this->m_CurrentThreshold = this->m_Thresholds.begin( ); this->m_Predicate->SetLower( *( this->m_CurrentThreshold ) ); this->m_CurrentThreshold++; this->m_Predicate->SetUpper( *( this->m_CurrentThreshold ) ); this->m_Count = 0; this->m_Signal.clear( ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: _AfterGenerateData( ) { // https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data this->Superclass::_AfterGenerateData( ); typename TSignal::const_iterator sIt = this->m_Signal.begin( ); for( ; sIt != this->m_Signal.end( ); ++sIt ) { std::cout << int( sIt->first ) << " " << int( sIt->second.first ) << " " << sIt->second.second << std::endl; } // rof } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: _FinishOneLoop( ) { if( this->m_Queues[ this->m_CurrentQueue ].size( ) == 0 ) { this->m_Signal[ this->m_Signal.size( ) + 1 ] = TSignalData( *this->m_CurrentThreshold, this->m_Count ); this->m_CurrentThreshold++; this->m_CurrentQueue = ( this->m_CurrentQueue + 1 ) % 2; if( this->m_CurrentThreshold != this->m_Thresholds.end( ) ) { this->m_Predicate->SetUpper( *( this->m_CurrentThreshold ) ); this->m_Count = 0; } else this->_QueueClear( ); } // fi } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: _ComputeOutputValue( TNode& n ) { // Do nothing!!! } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: _UpdateOutputValue( TNode& n ) { TInputValue value = this->_GetInputValue( n.Vertex ); bool inside = this->m_Predicate->Evaluate( value ); if( !inside ) { n.Value = this->m_InitValue; n.FrontId++; this->m_Queues[ ( this->m_CurrentQueue + 1 ) % 2 ].push_back( n ); n.FrontId = 0; } else { n.Value = this->m_InsideValue; this->m_Count++; } // fi this->Superclass::_UpdateOutputValue( n ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: _QueueClear( ) { this->m_Queues[ 0 ].clear( ); this->m_Queues[ 1 ].clear( ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > typename fpa::Base::Mori< _TAlgorithm >:: TNode fpa::Base::Mori< _TAlgorithm >:: _QueuePop( ) { TNode n = this->m_Queues[ this->m_CurrentQueue ].front( ); this->m_Queues[ this->m_CurrentQueue ].pop_front( ); return( n ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: _QueuePush( const TNode& node ) { this->m_Queues[ this->m_CurrentQueue ].push_back( node ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > unsigned long fpa::Base::Mori< _TAlgorithm >:: _QueueSize( ) const { return( this->m_Queues[ this->m_CurrentQueue ].size( ) ); } // ------------------------------------------------------------------------- template< class _TAlgorithm > void fpa::Base::Mori< _TAlgorithm >:: _PrepareSeeds( TNodes& nodes ) { typename TNodes::iterator nIt = nodes.begin( ); for( ; nIt != nodes.end( ); ++nIt ) nIt->Value = this->m_InitValue; } #endif // __fpa__Base__Mori__hxx__ // eof - $RCSfile$