MNE-CPP  beta 1.0
cluststcworker.cpp
Go to the documentation of this file.
1 //=============================================================================================================
36 //*************************************************************************************************************
37 //=============================================================================================================
38 // INCLUDES
39 //=============================================================================================================
40 
41 #include "cluststcworker.h"
42 
43 
44 //*************************************************************************************************************
45 //=============================================================================================================
46 // Qt INCLUDES
47 //=============================================================================================================
48 
49 #include <QDebug>
50 
51 
52 //*************************************************************************************************************
53 //=============================================================================================================
54 // USED NAMESPACES
55 //=============================================================================================================
56 
57 using namespace DISP3DLIB;
58 
59 
60 //*************************************************************************************************************
61 //=============================================================================================================
62 // DEFINE MEMBER METHODS
63 //=============================================================================================================
64 
65 ClustStcWorker::ClustStcWorker(QObject *parent)
66 : QThread(parent)
67 , m_bIsRunning(false)
68 , m_bIsLooping(false)
69 , m_iAverageSamples(10)
70 , m_iCurrentSample(0)
71 , m_iUSecIntervall(100)
72 {
73  m_data.clear();
74 }
75 
76 
77 //*************************************************************************************************************
78 
79 ClustStcWorker::~ClustStcWorker()
80 {
81  if(this->isRunning())
82  stop();
83 }
84 
85 
86 //*************************************************************************************************************
87 
88 void ClustStcWorker::addData(QList<VectorXd> &data)
89 {
90  QMutexLocker locker(&m_qMutex);
91  if(data.size() == 0)
92  return;
93 
94  m_data.append(data);
95 }
96 
97 
98 //*************************************************************************************************************
99 
100 void ClustStcWorker::clear()
101 {
102  QMutexLocker locker(&m_qMutex);
103  m_data.clear();
104 }
105 
106 
107 //*************************************************************************************************************
108 
109 void ClustStcWorker::run()
110 {
111  VectorXd m_vecAverage(0,0);
112 
113  m_bIsRunning = true;
114 
115  while(true)
116  {
117  {
118  QMutexLocker locker(&m_qMutex);
119  if(!m_bIsRunning)
120  break;
121  }
122 
123  bool doProcessing = false;
124  {
125  QMutexLocker locker(&m_qMutex);
126  if(!m_data.isEmpty() && m_data.size() > 0)
127  doProcessing = true;
128  }
129 
130  if(doProcessing)
131  {
132  if(m_bIsLooping)
133  {
134  m_qMutex.lock();
135  //Down sampling in loop mode
136  if(m_vecAverage.rows() != m_data[0].rows())
137  m_vecAverage = m_data[m_iCurrentSample%m_data.size()];
138  else
139  m_vecAverage += m_data[m_iCurrentSample%m_data.size()];
140  m_qMutex.unlock();
141  }
142  else
143  {
144  m_qMutex.lock();
145  //Down sampling in stream mode
146  if(m_vecAverage.rows() != m_data[0].rows())
147  m_vecAverage = m_data.front();
148  else
149  m_vecAverage += m_data.front();
150 
151  m_data.pop_front();
152  m_qMutex.unlock();
153  }
154 
155  m_qMutex.lock();
156  ++m_iCurrentSample;
157 
158  if(m_iCurrentSample%m_iAverageSamples == 0)
159  {
160  m_vecAverage /= (double)m_iAverageSamples;
161 
162  emit stcSample(m_vecAverage);
163  m_vecAverage = VectorXd::Zero(m_vecAverage.rows());
164  }
165  m_qMutex.unlock();
166  }
167 
168  QThread::usleep(m_iUSecIntervall);
169  }
170 }
171 
172 
173 //*************************************************************************************************************
174 
175 void ClustStcWorker::setAverage(qint32 samples)
176 {
177  QMutexLocker locker(&m_qMutex);
178  m_iAverageSamples = samples;
179 }
180 
181 
182 //*************************************************************************************************************
183 
184 void ClustStcWorker::setInterval(int usec)
185 {
186  QMutexLocker locker(&m_qMutex);
187  m_iUSecIntervall = usec;
188 }
189 
190 
191 //*************************************************************************************************************
192 
193 void ClustStcWorker::setLoop(bool looping)
194 {
195  QMutexLocker locker(&m_qMutex);
196  m_bIsLooping = looping;
197 }
198 
199 
200 //*************************************************************************************************************
201 
202 void ClustStcWorker::stop()
203 {
204  m_qMutex.lock();
205  m_bIsRunning = false;
206  m_qMutex.unlock();
207 
208  QThread::wait();
209 }
ClustStcWorker class declaration.