]> Creatis software - FrontAlgorithms.git/blob - lib/fpa/Base/Mori.hxx
548d755983b58756b471339c92f842444e10814a
[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 }
118
119 // -------------------------------------------------------------------------
120 template< class _TAlgorithm >
121 void fpa::Base::Mori< _TAlgorithm >::
122 _FinishOneLoop( )
123 {
124   if( this->m_Queues[ this->m_CurrentQueue ].size( ) == 0 )
125   {
126     this->m_Signal[ this->m_Signal.size( ) + 1 ] =
127       TSignalData( *this->m_CurrentThreshold, this->m_Count );
128     this->m_CurrentThreshold++;
129     this->m_CurrentQueue = ( this->m_CurrentQueue + 1 ) % 2;
130     if( this->m_CurrentThreshold != this->m_Thresholds.end( ) )
131     {
132       this->m_Predicate->SetUpper( *( this->m_CurrentThreshold ) );
133       this->m_Count = 0;
134     }
135     else
136       this->_QueueClear( );
137
138   } // fi
139 }
140
141 // -------------------------------------------------------------------------
142 template< class _TAlgorithm >
143 void fpa::Base::Mori< _TAlgorithm >::
144 _ComputeOutputValue( TNode& n )
145 {
146   // Do nothing!!!
147 }
148
149 // -------------------------------------------------------------------------
150 template< class _TAlgorithm >
151 void fpa::Base::Mori< _TAlgorithm >::
152 _UpdateOutputValue( TNode& n )
153 {
154   TInputValue value = this->_GetInputValue( n.Vertex );
155   bool inside = this->m_Predicate->Evaluate( value );
156   if( !inside )
157   {
158     n.Value = this->m_InitValue;
159     n.FrontId++;
160     this->m_Queues[ ( this->m_CurrentQueue + 1 ) % 2 ].push_back( n );
161     n.FrontId = 0;
162   }
163   else
164   {
165     n.Value = this->m_InsideValue;
166     this->m_Count++;
167
168   } // fi
169   this->Superclass::_UpdateOutputValue( n );
170 }
171
172 // -------------------------------------------------------------------------
173 template< class _TAlgorithm >
174 void fpa::Base::Mori< _TAlgorithm >::
175 _QueueClear( )
176 {
177   this->m_Queues[ 0 ].clear( );
178   this->m_Queues[ 1 ].clear( );
179 }
180
181 // -------------------------------------------------------------------------
182 template< class _TAlgorithm >
183 typename fpa::Base::Mori< _TAlgorithm >::
184 TNode fpa::Base::Mori< _TAlgorithm >::
185 _QueuePop( )
186 {
187   TNode n = this->m_Queues[ this->m_CurrentQueue ].front( );
188   this->m_Queues[ this->m_CurrentQueue ].pop_front( );
189   return( n );
190 }
191
192 // -------------------------------------------------------------------------
193 template< class _TAlgorithm >
194 void fpa::Base::Mori< _TAlgorithm >::
195 _QueuePush( const TNode& node )
196 {
197   this->m_Queues[ this->m_CurrentQueue ].push_back( node );
198 }
199
200 // -------------------------------------------------------------------------
201 template< class _TAlgorithm >
202 unsigned long fpa::Base::Mori< _TAlgorithm >::
203 _QueueSize( ) const
204 {
205   return( this->m_Queues[ this->m_CurrentQueue ].size( ) );
206 }
207
208 // -------------------------------------------------------------------------
209 template< class _TAlgorithm >
210 void fpa::Base::Mori< _TAlgorithm >::
211 _PrepareSeeds( TNodes& nodes )
212 {
213   typename TNodes::iterator nIt = nodes.begin( );
214   for( ; nIt != nodes.end( ); ++nIt )
215     nIt->Value = this->m_InitValue;
216 }
217
218 #endif // __fpa__Base__Mori__hxx__
219
220 // eof - $RCSfile$