v2.0.0
Loading...
Searching...
No Matches
rtsourcedatacontroller.cpp
Go to the documentation of this file.
1//=============================================================================================================
34
35//=============================================================================================================
36// INCLUDES
37//=============================================================================================================
38
40#include "rtsourcedataworker.h"
42
43#include <fs/label.h>
44
45#include <QThread>
46#include <QTimer>
47#include <QDebug>
48
49//=============================================================================================================
50// DEFINE MEMBER METHODS
51//=============================================================================================================
52
54 : QObject(parent)
55{
56 // Create data worker
57 m_pWorker = new DISP3DRHILIB::RtSourceDataWorker();
58
59 // Create and configure data thread
60 m_pWorkerThread = new QThread(this);
61 m_pWorker->moveToThread(m_pWorkerThread);
62
63 // Clean up worker when thread finishes
64 connect(m_pWorkerThread, &QThread::finished, m_pWorker, &QObject::deleteLater);
65
66 // Forward worker signals to controller signals
71
72 // Create timer for driving the streaming cadence
73 m_pTimer = new QTimer(this);
74 m_pTimer->setTimerType(Qt::PreciseTimer);
75 connect(m_pTimer, &QTimer::timeout, m_pWorker, &DISP3DRHILIB::RtSourceDataWorker::streamData);
76
77 // Start the data worker thread
78 m_pWorkerThread->start();
79
80 // Create interpolation matrix worker
82 m_pInterpThread = new QThread(this);
83 m_pInterpWorker->moveToThread(m_pInterpThread);
84
85 connect(m_pInterpThread, &QThread::finished, m_pInterpWorker, &QObject::deleteLater);
86
87 // Forward interpolation results: auto-apply to data worker + re-emit
89 this, &RtSourceDataController::onNewInterpolationMatrixLeft);
91 this, &RtSourceDataController::onNewInterpolationMatrixRight);
92
93 m_pInterpThread->start();
94
95 qDebug() << "RtSourceDataController: Initialized with" << m_iTimeInterval << "ms interval";
96}
97
98//=============================================================================================================
99
101{
102 // Stop timer
103 if (m_pTimer) {
104 m_pTimer->stop();
105 }
106
107 // Stop the data worker thread
108 if (m_pWorkerThread) {
109 m_pWorkerThread->quit();
110 m_pWorkerThread->wait();
111 }
112
113 // Stop the interpolation worker thread
114 if (m_pInterpThread) {
115 m_pInterpThread->quit();
116 m_pInterpThread->wait();
117 }
118}
119
120//=============================================================================================================
121
122void RtSourceDataController::addData(const Eigen::VectorXd &data)
123{
124 if (m_pWorker) {
125 // Direct call is thread-safe because RtSourceDataWorker uses a mutex
126 m_pWorker->addData(data);
127 }
128}
129
130//=============================================================================================================
131
132void RtSourceDataController::setInterpolationMatrixLeft(QSharedPointer<Eigen::SparseMatrix<float>> mat)
133{
134 if (m_pWorker) {
135 m_pWorker->setInterpolationMatrixLeft(mat);
136 }
137}
138
139//=============================================================================================================
140
141void RtSourceDataController::setInterpolationMatrixRight(QSharedPointer<Eigen::SparseMatrix<float>> mat)
142{
143 if (m_pWorker) {
144 m_pWorker->setInterpolationMatrixRight(mat);
145 }
146}
147
148//=============================================================================================================
149
151{
152 m_bIsStreaming = state;
153
154 if (state) {
155 m_pTimer->start(m_iTimeInterval);
156 qDebug() << "RtSourceDataController: Streaming started at" << m_iTimeInterval << "ms interval";
157 } else {
158 m_pTimer->stop();
159 qDebug() << "RtSourceDataController: Streaming stopped";
160 }
161}
162
163//=============================================================================================================
164
166{
167 return m_bIsStreaming;
168}
169
170//=============================================================================================================
171
173{
174 m_iTimeInterval = qMax(1, msec);
175
176 if (m_bIsStreaming) {
177 m_pTimer->setInterval(m_iTimeInterval);
178 }
179
180 qDebug() << "RtSourceDataController: Time interval set to" << m_iTimeInterval << "ms";
181}
182
183//=============================================================================================================
184
186{
187 if (m_pWorker) {
188 m_pWorker->setNumberAverages(numAvr);
189 }
190}
191
192//=============================================================================================================
193
195{
196 if (m_pWorker) {
197 m_pWorker->setColormapType(name);
198 }
199}
200
201//=============================================================================================================
202
203void RtSourceDataController::setThresholds(double min, double mid, double max)
204{
205 if (m_pWorker) {
206 m_pWorker->setThresholds(min, mid, max);
207 }
208}
209
210//=============================================================================================================
211
213{
214 if (m_pWorker) {
215 m_pWorker->setLoopState(enabled);
216 }
217}
218
219//=============================================================================================================
220
222{
223 if (m_pWorker) {
224 m_pWorker->setSFreq(sFreq);
225 }
226}
227
228//=============================================================================================================
229
231{
232 if (m_pWorker) {
233 m_pWorker->clear();
234 }
235}
236
237//=============================================================================================================
238
239void RtSourceDataController::setSurfaceColor(const QVector<uint32_t> &baseColorsLh,
240 const QVector<uint32_t> &baseColorsRh)
241{
242 if (m_pWorker) {
243 m_pWorker->setSurfaceColor(baseColorsLh, baseColorsRh);
244 }
245}
246
247//=============================================================================================================
248
250{
251 if (m_pWorker) {
252 m_pWorker->setStreamSmoothedData(bStreamSmoothedData);
253 }
254}
255
256//=============================================================================================================
257
258void RtSourceDataController::setInterpolationFunction(const QString &sInterpolationFunction)
259{
260 if (m_pInterpWorker) {
261 m_pInterpWorker->setInterpolationFunction(sInterpolationFunction);
262 }
263}
264
265//=============================================================================================================
266
268{
269 if (m_pInterpWorker) {
270 m_pInterpWorker->setCancelDistance(dCancelDist);
271 }
272}
273
274//=============================================================================================================
275
276void RtSourceDataController::setInterpolationInfoLeft(const Eigen::MatrixX3f &matVertices,
277 const std::vector<Eigen::VectorXi> &vecNeighborVertices,
278 const Eigen::VectorXi &vecSourceVertices)
279{
280 if (m_pInterpWorker) {
281 m_pInterpWorker->setInterpolationInfoLeft(matVertices, vecNeighborVertices, vecSourceVertices);
282 }
283}
284
285//=============================================================================================================
286
287void RtSourceDataController::setInterpolationInfoRight(const Eigen::MatrixX3f &matVertices,
288 const std::vector<Eigen::VectorXi> &vecNeighborVertices,
289 const Eigen::VectorXi &vecSourceVertices)
290{
291 if (m_pInterpWorker) {
292 m_pInterpWorker->setInterpolationInfoRight(matVertices, vecNeighborVertices, vecSourceVertices);
293 }
294}
295
296//=============================================================================================================
297
299{
300 if (m_pInterpWorker) {
301 QMetaObject::invokeMethod(m_pInterpWorker, "computeInterpolationMatrix", Qt::QueuedConnection);
302 }
303}
304
305//=============================================================================================================
306
307void RtSourceDataController::onNewInterpolationMatrixLeft(QSharedPointer<Eigen::SparseMatrix<float>> interpMat)
308{
309 // Auto-forward to data worker so it immediately uses the new matrix
310 if (m_pWorker) {
311 m_pWorker->setInterpolationMatrixLeft(interpMat);
312 }
313 // Re-emit so external listeners can react
315}
316
317//=============================================================================================================
318
319void RtSourceDataController::onNewInterpolationMatrixRight(QSharedPointer<Eigen::SparseMatrix<float>> interpMat)
320{
321 // Auto-forward to data worker so it immediately uses the new matrix
322 if (m_pWorker) {
323 m_pWorker->setInterpolationMatrixRight(interpMat);
324 }
325 // Re-emit so external listeners can react
327}
328
329//=============================================================================================================
330
332{
333 if (m_pInterpWorker) {
334 m_pInterpWorker->setVisualizationType(iVisType);
335 }
336}
337
338//=============================================================================================================
339
340void RtSourceDataController::setAnnotationInfoLeft(const Eigen::VectorXi &vecLabelIds,
341 const QList<FSLIB::Label> &lLabels,
342 const Eigen::VectorXi &vecVertNo)
343{
344 if (m_pInterpWorker) {
345 m_pInterpWorker->setAnnotationInfoLeft(vecLabelIds, lLabels, vecVertNo);
346 }
347}
348
349//=============================================================================================================
350
351void RtSourceDataController::setAnnotationInfoRight(const Eigen::VectorXi &vecLabelIds,
352 const QList<FSLIB::Label> &lLabels,
353 const Eigen::VectorXi &vecVertNo)
354{
355 if (m_pInterpWorker) {
356 m_pInterpWorker->setAnnotationInfoRight(vecLabelIds, lLabels, vecVertNo);
357 }
358}
RtSourceDataController class declaration.
RtSourceDataWorker class declaration.
RtSourceInterpolationMatWorker class declaration.
Label class declaration.
void setAnnotationInfoRight(const Eigen::VectorXi &vecLabelIds, const QList< FSLIB::Label > &lLabels, const Eigen::VectorXi &vecVertNo)
void setThresholds(double min, double mid, double max)
void setInterpolationMatrixLeft(QSharedPointer< Eigen::SparseMatrix< float > > mat)
void addData(const Eigen::VectorXd &data)
void setStreamSmoothedData(bool bStreamSmoothedData)
void setAnnotationInfoLeft(const Eigen::VectorXi &vecLabelIds, const QList< FSLIB::Label > &lLabels, const Eigen::VectorXi &vecVertNo)
void newInterpolationMatrixRightAvailable(QSharedPointer< Eigen::SparseMatrix< float > > interpMat)
void setSurfaceColor(const QVector< uint32_t > &baseColorsLh, const QVector< uint32_t > &baseColorsRh)
void setCancelDistance(double dCancelDist)
void setInterpolationInfoRight(const Eigen::MatrixX3f &matVertices, const std::vector< Eigen::VectorXi > &vecNeighborVertices, const Eigen::VectorXi &vecSourceVertices)
void newRawDataAvailable(const Eigen::VectorXd &dataLh, const Eigen::VectorXd &dataRh)
void newSmoothedDataAvailable(const QVector< uint32_t > &colorsLh, const QVector< uint32_t > &colorsRh)
void setVisualizationType(int iVisType)
void setInterpolationFunction(const QString &sInterpolationFunction)
void setColormapType(const QString &name)
void newInterpolationMatrixLeftAvailable(QSharedPointer< Eigen::SparseMatrix< float > > interpMat)
RtSourceDataController(QObject *parent=nullptr)
void setInterpolationMatrixRight(QSharedPointer< Eigen::SparseMatrix< float > > mat)
void setInterpolationInfoLeft(const Eigen::MatrixX3f &matVertices, const std::vector< Eigen::VectorXi > &vecNeighborVertices, const Eigen::VectorXi &vecSourceVertices)
Background worker for real-time source estimate streaming.
void newRtSmoothedData(const QVector< uint32_t > &colorsLh, const QVector< uint32_t > &colorsRh)
void setInterpolationMatrixLeft(QSharedPointer< Eigen::SparseMatrix< float > > mat)
void newRtRawData(const Eigen::VectorXd &dataLh, const Eigen::VectorXd &dataRh)
Background worker for computing source interpolation matrices.
void newInterpolationMatrixRightAvailable(QSharedPointer< Eigen::SparseMatrix< float > > interpMat)
void newInterpolationMatrixLeftAvailable(QSharedPointer< Eigen::SparseMatrix< float > > interpMat)