]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/Mori.hxx
f0141448ef11996a484e35fe68201d0c6b8dcbb2
[FrontAlgorithms.git] / lib / fpa / Base / Mori.hxx
1 // =========================================================================
2 // @author Leonardo Florez Valencia
3 // @email florez-l@javeriana.edu.co
4 // =========================================================================
5
6 #ifndef __fpa__Base__Mori__hxx__
7 #define __fpa__Base__Mori__hxx__
8
9 // -------------------------------------------------------------------------
10 template< class _TAlgorithm >
11 itk::ModifiedTimeType fpa::Base::Mori< _TAlgorithm >::
12 GetMTime( ) const
13 {
14   itk::ModifiedTimeType t = this->Superclass::GetMTime( );
15   itk::ModifiedTimeType q = this->m_Predicate->GetMTime( );
16   return( ( q < t )? q: t );
17 }
18
19 // -------------------------------------------------------------------------
20 template< class _TAlgorithm >
21 typename fpa::Base::Mori< _TAlgorithm >::
22 TOutputValue fpa::Base::Mori< _TAlgorithm >::
23 GetOutsideValue( ) const
24 {
25   return( this->GetInitValue( ) );
26 }
27
28 // -------------------------------------------------------------------------
29 template< class _TAlgorithm >
30 void fpa::Base::Mori< _TAlgorithm >::
31 SetOutsideValue( const TOutputValue& v )
32 {
33   this->SetInitValue( v );
34 }
35
36 // -------------------------------------------------------------------------
37 template< class _TAlgorithm >
38 void fpa::Base::Mori< _TAlgorithm >::
39 ClearThresholds( )
40 {
41   this->m_Thresholds.clear( );
42   this->Modified( );
43 }
44
45 // -------------------------------------------------------------------------
46 template< class _TAlgorithm >
47 void fpa::Base::Mori< _TAlgorithm >::
48 AddThreshold( const TInputValue& thr )
49 {
50   if( this->m_Thresholds.insert( thr ).second )
51     this->Modified( );
52 }
53
54 // -------------------------------------------------------------------------
55 template< class _TAlgorithm >
56 void fpa::Base::Mori< _TAlgorithm >::
57 SetThresholds(
58   const TInputValue& init,
59   const TInputValue& end,
60   const TInputValue& delta
61   )
62 {
63   for( TInputValue thr = init; thr <= end; thr += delta )
64     this->AddThreshold( thr );
65 }
66
67 // -------------------------------------------------------------------------
68 template< class _TAlgorithm >
69 fpa::Base::Mori< _TAlgorithm >::
70 Mori( )
71   : Superclass( ),
72     m_InsideValue( TOutputValue( 1 ) )
73 {
74   this->SetInitValue( TOutputValue( 0 ) );
75   this->m_Predicate = TPredicate::New( );
76   this->m_Predicate->StrictOff( );
77 }
78
79 // -------------------------------------------------------------------------
80 template< class _TAlgorithm >
81 fpa::Base::Mori< _TAlgorithm >::
82 ~Mori( )
83 {
84 }
85
86 // -------------------------------------------------------------------------
87 template< class _TAlgorithm >
88 void fpa::Base::Mori< _TAlgorithm >::
89 _BeforeGenerateData( )
90 {
91   this->Superclass::_BeforeGenerateData( );
92
93   this->_QueueClear( );
94   this->m_CurrentQueue = 0;
95   this->m_CurrentThreshold = this->m_Thresholds.begin( );
96   this->m_Predicate->SetLower( *( this->m_CurrentThreshold ) );
97   this->m_CurrentThreshold++;
98   this->m_Predicate->SetUpper( *( this->m_CurrentThreshold ) );
99   this->m_Count = 0;
100   this->m_Signal.clear( );
101 }
102
103 // -------------------------------------------------------------------------
104 template< class _TAlgorithm >
105 void fpa::Base::Mori< _TAlgorithm >::
106 _AfterGenerateData( )
107 {
108   // https://stackoverflow.com/questions/22583391/peak-signal-detection-in-realtime-timeseries-data
109   this->Superclass::_AfterGenerateData( );
110
111   typename TSignal::const_iterator sIt = this->m_Signal.begin( );
112   for( ; sIt != this->m_Signal.end( ); ++sIt )
113   {
114     std::cout << int( sIt->first ) << " " << int( sIt->second.first ) << " " << sIt->second.second << std::endl;
115
116   } // rof
117   std::cerr << ( this->m_CurrentThreshold != this->m_Thresholds.end( ) ) << std::endl;
118 }
119
120 // -------------------------------------------------------------------------
121 template< class _TAlgorithm >
122 void fpa::Base::Mori< _TAlgorithm >::
123 _FinishOneLoop( )
124 {
125   if( this->m_Queues[ this->m_CurrentQueue ].size( ) == 0 )
126   {
127     this->m_Signal[ this->m_Signal.size( ) + 1 ] =
128       TSignalData( *this->m_CurrentThreshold, this->m_Count );
129     std::cerr << *( this->m_CurrentThreshold ) << std::endl;
130     this->m_CurrentThreshold++;
131     this->m_CurrentQueue = ( this->m_CurrentQueue + 1 ) % 2;
132     if( this->m_CurrentThreshold != this->m_Thresholds.end( ) )
133     {
134       this->m_Predicate->SetUpper( *( this->m_CurrentThreshold ) );
135       this->m_Count = 0;
136     }
137     else
138       this->_QueueClear( );
139
140   } // fi
141 }
142
143 // -------------------------------------------------------------------------
144 template< class _TAlgorithm >
145 bool fpa::Base::Mori< _TAlgorithm >::
146 _ComputeOutputValue( TNode& n )
147 {
148   TInputValue value = this->_GetInputValue( n.Vertex );
149   bool inside = this->m_Predicate->Evaluate( value );
150   n.Value = this->m_InsideValue;
151   if( !inside )
152   {
153     n.FrontId++;
154     this->m_Queues[ ( this->m_CurrentQueue + 1 ) % 2 ].push_back( n );
155   }
156   else
157     this->m_Count++;
158   return( inside );
159 }
160   
161 // -------------------------------------------------------------------------
162 template< class _TAlgorithm >
163 void fpa::Base::Mori< _TAlgorithm >::
164 _QueueClear( )
165 {
166   this->m_Queues[ 0 ].clear( );
167   this->m_Queues[ 1 ].clear( );
168 }
169
170 // -------------------------------------------------------------------------
171 template< class _TAlgorithm >
172 typename fpa::Base::Mori< _TAlgorithm >::
173 TNode fpa::Base::Mori< _TAlgorithm >::
174 _QueuePop( )
175 {
176   TNode n = this->m_Queues[ this->m_CurrentQueue ].front( );
177   this->m_Queues[ this->m_CurrentQueue ].pop_front( );
178   return( n );
179 }
180
181 // -------------------------------------------------------------------------
182 template< class _TAlgorithm >
183 void fpa::Base::Mori< _TAlgorithm >::
184 _QueuePush( const TNode& node )
185 {
186   this->m_Queues[ this->m_CurrentQueue ].push_back( node );
187 }
188
189 // -------------------------------------------------------------------------
190 template< class _TAlgorithm >
191 unsigned long fpa::Base::Mori< _TAlgorithm >::
192 _QueueSize( ) const
193 {
194   return( this->m_Queues[ this->m_CurrentQueue ].size( ) );
195 }
196
197 // -------------------------------------------------------------------------
198 template< class _TAlgorithm >
199 void fpa::Base::Mori< _TAlgorithm >::
200 _PrepareSeeds( TNodes& nodes )
201 {
202   typename TNodes::iterator nIt = nodes.begin( );
203   for( ; nIt != nodes.end( ); ++nIt )
204     nIt->Value = this->m_InsideValue;
205 }
206
207 #endif // __fpa__Base__Mori__hxx__
208
209 // eof - $RCSfile$