-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSLStudio.cpp
327 lines (259 loc) · 11.9 KB
/
SLStudio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "SLStudio.h"
#include "ui_SLStudio.h"
#include <stdio.h>
#include <string.h>
#include <QThread>
#include <QFileDialog>
#include "SLCalibrationDialog.h"
#include "SLPreferenceDialog.h"
#include "SLAboutDialog.h"
#include "SLVideoWidget.h"
#include <QtGui>
#include "cvtools.h"
using namespace std;
SLStudio::SLStudio(QWidget *parent) : QMainWindow(parent),
ui(new Ui::SLStudio),
scanWorkerThread(NULL),
settings(NULL),
histogramDialog(NULL),
shadingDialog(NULL),
decoderUpDialog(NULL),
decoderVpDialog(NULL),
depthDialog(NULL),
trackerDialog(NULL)
{
ui->setupUi(this);
time = new QTime;
// Restore main window geometry
settings = new QSettings("SLStudio");
restoreGeometry(settings->value("geometry/mainwindow").toByteArray());
restoreState(settings->value("state/mainwindow").toByteArray());
// Ui connections
connect(ui->pointCloudWidget, SIGNAL(newPointCloudDisplayed()), this, SLOT(updateDisplayRate()));
// Create video dialogs
histogramDialog = new SLVideoDialog("Histogram", this);
shadingDialog = new SLVideoDialog("Shading", this);
cameraFramesDialog = new SLVideoDialog("Camera Frames", this);
decoderUpDialog = new SLVideoDialog("Decoder Up", this);
decoderVpDialog = new SLVideoDialog("Decoder Vp", this);
depthDialog = new SLVideoDialog("Depth", this);
// Add view menu actions
ui->menuView->addAction(histogramDialog->toggleViewAction());
ui->menuView->addAction(shadingDialog->toggleViewAction());
ui->menuView->addAction(cameraFramesDialog->toggleViewAction());
ui->menuView->addAction(decoderUpDialog->toggleViewAction());
ui->menuView->addAction(decoderVpDialog->toggleViewAction());
ui->menuView->addAction(depthDialog->toggleViewAction());
// Restore Geometry
histogramDialog->restoreGeometry(settings->value("geometry/histogram").toByteArray());
shadingDialog->restoreGeometry(settings->value("geometry/shading").toByteArray());
cameraFramesDialog->restoreGeometry(settings->value("geometry/cameraFrames").toByteArray());
decoderUpDialog->restoreGeometry(settings->value("geometry/decoderUp").toByteArray());
decoderVpDialog->restoreGeometry(settings->value("geometry/decoderVp").toByteArray());
depthDialog->restoreGeometry(settings->value("geometry/depth").toByteArray());
// Restore Visibility
histogramDialog->setVisible(settings->value("visible/histogram", false).toBool());
shadingDialog->setVisible(settings->value("visible/shading", false).toBool());
cameraFramesDialog->setVisible(settings->value("visible/cameraFrames", false).toBool());
decoderUpDialog->setVisible(settings->value("visible/decoderUp", false).toBool());
decoderVpDialog->setVisible(settings->value("visible/decoderVp", false).toBool());
depthDialog->setVisible(settings->value("visible/depth", false).toBool());
// Tracker Dialog
trackerDialog = new SLTrackerDialog(this);
ui->menuView->addAction(trackerDialog->toggleViewAction());
trackerDialog->setVisible(settings->value("visible/trackerDialog", false).toBool());
}
void SLStudio::onShowHistogram(cv::Mat im){
if(histogramDialog->isVisible())
histogramDialog->showImageCV(im);
}
void SLStudio::onShowShading(cv::Mat im){
if(shadingDialog->isVisible())
shadingDialog->showImageCV(im);
}
void SLStudio::onShowCameraFrames(std::vector<cv::Mat> frameSeq){
if(cameraFramesDialog->isVisible())
cameraFramesDialog->showImageSeqCV(frameSeq);
}
void SLStudio::onShowDecoderUp(cv::Mat im){
if(decoderUpDialog->isVisible())
decoderUpDialog->showImageCV(im);
}
void SLStudio::onShowDecoderVp(cv::Mat im){
if(decoderVpDialog->isVisible()){
decoderVpDialog->showImageCV(im);
//std::cout << "Showing now!" << std::endl;
}
}
void SLStudio::onShowDepth(cv::Mat im){
if(depthDialog->isVisible())
depthDialog->showImageCV(im);
}
void SLStudio::onActionStart(){
// Prepare scanWorker on separate thread
scanWorker = new SLScanWorker(this);
scanWorkerThread = new QThread(this);
scanWorkerThread->setObjectName("scanWorkerThread");
scanWorker->moveToThread(scanWorkerThread);
connect(scanWorker, SIGNAL(finished()), this, SLOT(onScanWorkerFinished()));
// Prepare decoderWorker on separate thread
decoderWorker = new SLDecoderWorker();
decoderThread = new QThread(this);
decoderThread->setObjectName("decoderThread");
decoderWorker->moveToThread(decoderThread);
connect(decoderThread, SIGNAL(started()), decoderWorker, SLOT(setup()));
connect(decoderThread, SIGNAL(finished()), decoderWorker, SLOT(deleteLater()));
connect(decoderThread, SIGNAL(finished()), decoderThread, SLOT(deleteLater()));
// Prepare triangulatorWorker on separate thread
triangulatorWorker = new SLTriangulatorWorker();
triangulatorThread = new QThread(this);
triangulatorThread->setObjectName("triangulatorThread");
triangulatorWorker->moveToThread(triangulatorThread);
connect(triangulatorThread, SIGNAL(started()), triangulatorWorker, SLOT(setup()));
connect(triangulatorThread, SIGNAL(finished()), triangulatorWorker, SLOT(deleteLater()));
connect(triangulatorThread, SIGNAL(finished()), triangulatorThread, SLOT(deleteLater()));
// Register metatypes
qRegisterMetaType<cv::Mat>("cv::Mat");
qRegisterMetaType< std::vector<cv::Mat> >("std::vector<cv::Mat>");
qRegisterMetaType< PointCloudConstPtr >("PointCloudConstPtr");
// Inter thread connections
connect(scanWorker, SIGNAL(showHistogram(cv::Mat)), this, SLOT(onShowHistogram(cv::Mat)));
connect(scanWorker, SIGNAL(newFrameSeq(std::vector<cv::Mat>)), decoderWorker, SLOT(decodeSequence(std::vector<cv::Mat>)));
connect(scanWorker, SIGNAL(newFrameSeq(std::vector<cv::Mat>)), this, SLOT(onShowCameraFrames(std::vector<cv::Mat>)));
connect(decoderWorker, SIGNAL(showShading(cv::Mat)), this, SLOT(onShowShading(cv::Mat)));
connect(decoderWorker, SIGNAL(showDecoderUp(cv::Mat)), this, SLOT(onShowDecoderUp(cv::Mat)));
connect(decoderWorker, SIGNAL(showDecoderVp(cv::Mat)), this, SLOT(onShowDecoderVp(cv::Mat)));
connect(decoderWorker, SIGNAL(newUpVp(cv::Mat,cv::Mat,cv::Mat,cv::Mat)), triangulatorWorker, SLOT(triangulatePointCloud(cv::Mat,cv::Mat,cv::Mat,cv::Mat)));
connect(triangulatorWorker, SIGNAL(newPointCloud(PointCloudConstPtr)), this, SLOT(receiveNewPointCloud(PointCloudConstPtr)));
connect(triangulatorWorker, SIGNAL(newDepthImage(cv::Mat)), this, SLOT(onShowDepth(cv::Mat)));
connect(triangulatorWorker, SIGNAL(imshow(const char*,cv::Mat,uint,uint)), this, SLOT(imshow(const char*,cv::Mat,uint,uint)));
// Start threads
decoderThread->start(QThread::LowPriority);
triangulatorThread->start(QThread::LowPriority);
scanWorkerThread->start(QThread::TimeCriticalPriority);
// Setup and start processing
QMetaObject::invokeMethod(scanWorker, "setup");
QMetaObject::invokeMethod(scanWorker, "doWork");
time->start();
// Change ui elements
ui->actionStart->setEnabled(false);
ui->actionStop->setEnabled(true);
ui->actionTracking->setEnabled(true);
ui->actionSavePointCloud->setEnabled(true);
ui->actionSaveScreenshot->setEnabled(true);
ui->actionCalibration->setEnabled(false);
}
void SLStudio::onActionStop(){
// Stop processing on scan worker thread
QMetaObject::invokeMethod(scanWorker, "stopWorking");
//cv::destroyAllWindows();
decoderThread->quit();
decoderThread->wait();
std::cout<<"decoderThread deleted\n"<<std::flush;
triangulatorThread->quit();
triangulatorThread->wait();
std::cout<<"triangulatorThread deleted\n"<<std::flush;
}
void SLStudio::onScanWorkerFinished(){
QMetaObject::invokeMethod(scanWorker, "deleteLater");
// Terminate scan worker thread
scanWorkerThread->quit();
scanWorkerThread->wait();
//scanWorkerThread->deleteLater();
//delete scanWorkerThread;
// Change ui elements
ui->actionStart->setEnabled(true);
ui->actionStop->setEnabled(false);
ui->actionTracking->setEnabled(false);
ui->actionCalibration->setEnabled(true);
}
void SLStudio::onActionCalibration(){
SLCalibrationDialog *calibrationDialog = new SLCalibrationDialog(this);
calibrationDialog->exec();
}
void SLStudio::onActionPreferences(){
SLPreferenceDialog *preferenceDialog = new SLPreferenceDialog(this);
preferenceDialog->exec();
}
void SLStudio::updateDisplayRate(){
int mSecElapsed = time->restart();
displayIntervals.push_back(mSecElapsed);
if(displayIntervals.size() > 10)
displayIntervals.erase(displayIntervals.begin(), displayIntervals.end()-10);
float meanMSecElapsed = 0;
for(unsigned int i=0; i<displayIntervals.size(); i++)
meanMSecElapsed += displayIntervals[i];
meanMSecElapsed /= displayIntervals.size();
QString fpsString = QString("PCPS: %1").arg(1000.0/meanMSecElapsed, 0, 'f', 2);
ui->statusBar->showMessage(fpsString);
}
void SLStudio::receiveNewPointCloud(PointCloudConstPtr pointCloud){
// Display point cloud in widget
if(ui->actionUpdatePointClouds->isChecked())
ui->pointCloudWidget->updatePointCloud(pointCloud);
if(trackerDialog->isVisible())
trackerDialog->receiveNewPointCloud(pointCloud);
}
void SLStudio::closeEvent(QCloseEvent *event){
// Save main window geometry
settings->setValue("geometry/mainwindow", saveGeometry());
settings->setValue("state/mainwindow", saveState());
// Store Geometry
settings->setValue("geometry/histogram", histogramDialog->saveGeometry());
settings->setValue("geometry/shading", shadingDialog->saveGeometry());
settings->setValue("geometry/decoderUp", decoderUpDialog->saveGeometry());
settings->setValue("geometry/decoderVp", decoderVpDialog->saveGeometry());
settings->setValue("geometry/depth", depthDialog->saveGeometry());
// Store Visibility
settings->setValue("visible/histogram", histogramDialog->isVisible());
settings->setValue("visible/shading", shadingDialog->isVisible());
settings->setValue("visible/decoderUp", decoderUpDialog->isVisible());
settings->setValue("visible/decoderVp", decoderVpDialog->isVisible());
settings->setValue("visible/depth", depthDialog->isVisible());
// Store data for trackerDialog (temp)
settings->setValue("geometry/trackerDialog", trackerDialog->saveGeometry());
settings->setValue("visible/trackerDialog", trackerDialog->isVisible());
event->accept();
}
SLStudio::~SLStudio(){
delete ui;
delete settings;
}
void SLStudio::onActionLoadCalibration(){
QString fileName = QFileDialog::getOpenFileName(this, "Choose calibration file", QString(), "*.xml");
if(!(fileName.length() == 0)){
CalibrationData calibration;
calibration.load(fileName);
calibration.save("calibration.xml");
}
}
void SLStudio::onActionExportCalibration(){
CalibrationData calibration;
calibration.load("calibration.xml");
// Non native file dialog
// QFileDialog saveFileDialog(this, "Export Calibration", QString(), "*.xml;;*.slcalib;;*.m");
// saveFileDialog.setDefaultSuffix("xml");
// saveFileDialog.exec();
// QString fileName = saveFileDialog.selectedFiles().first();
// Native file dialog
QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this, "Export Calibration", QString(), "*.xml;;*.slcalib;;*.m", &selectedFilter);
if(!(fileName.length() == 0)){
QFileInfo info(fileName);
QString type = info.suffix();
if(type == "")
fileName.append(selectedFilter.remove(0,1));
calibration.save(fileName);
}
}
void SLStudio::onActionAbout(){
SLAboutDialog *aboutDialog = new SLAboutDialog(this);
aboutDialog->exec();
}
// Debuggings slots for plotting on the main thread
void SLStudio::hist(const char* windowName, cv::Mat im, unsigned int x, unsigned int y){
cvtools::hist(windowName, im, x, y);
}
void SLStudio::imshow(const char* windowName, cv::Mat im, unsigned int x, unsigned int y){
cvtools::imshow(windowName, im, x, y);
}