Subversion Repositories seema-scanner

Rev

Rev 249 | Rev 251 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 249 Rev 250
1
#include "SMScanner.h"
1
#include "SMScanner.h"
2
 
2
 
3
#include "ui_SMScanner.h"
3
#include "ui_SMScanner.h"
4
 
4
 
5
#include <QMetaObject>
5
#include <QMetaObject>
6
#include <QFileDialog>
6
#include <QFileDialog>
7
#include <QMessageBox>
7
#include <QMessageBox>
8
#include <QProgressDialog>
8
#include <QProgressDialog>
9
#include <QDesktopServices>
9
#include <QDesktopServices>
10
 
10
 
11
#include <pcl/io/pcd_io.h>
11
#include <pcl/io/pcd_io.h>
12
#include <pcl/io/ascii_io.h>
12
#include <pcl/io/ascii_io.h>
13
#include <pcl/io/ply_io.h>
13
#include <pcl/io/ply_io.h>
14
#include <pcl/io/png_io.h>
14
#include <pcl/io/png_io.h>
15
#include <pcl/io/vtk_io.h>
15
#include <pcl/io/vtk_io.h>
16
#include <vtkPolyDataWriter.h>
16
#include <vtkPolyDataWriter.h>
17
#include <pcl/conversions.h>
17
#include <pcl/conversions.h>
18
 
18
 
19
#include "cvtools.h"
19
#include "cvtools.h"
20
 
20
 
21
SMScanner::SMScanner(QWidget *parent) :QMainWindow(parent), ui(new Ui::SMScanner),
21
SMScanner::SMScanner(QWidget *parent) :QMainWindow(parent), ui(new Ui::SMScanner),
22
                                        calibrationReviewMode(false), captureReviewMode(false), lastCaptureId(-1){
22
                                        calibrationReviewMode(false), captureReviewMode(false), lastCaptureId(-1){
23
 
23
 
24
    // Register metatypes
24
    // Register metatypes
25
    qRegisterMetaType<cv::Mat>("cv::Mat");
25
    qRegisterMetaType<cv::Mat>("cv::Mat");
26
    qRegisterMetaType< std::vector<cv::Mat> >("std::vector<cv::Mat>");
26
    qRegisterMetaType< std::vector<cv::Mat> >("std::vector<cv::Mat>");
27
    qRegisterMetaType< std::vector<float> >("std::vector<float>");
27
    qRegisterMetaType< std::vector<float> >("std::vector<float>");
28
    qRegisterMetaType<SMCalibrationSet>("SMCalibrationSet");
28
    qRegisterMetaType<SMCalibrationSet>("SMCalibrationSet");
29
    qRegisterMetaType< std::vector<SMCalibrationSet> >("std::vector<SMCalibrationSet>");
29
    qRegisterMetaType< std::vector<SMCalibrationSet> >("std::vector<SMCalibrationSet>");
30
    qRegisterMetaType<SMFrameSequence>("SMFrameSequence");
30
    qRegisterMetaType<SMFrameSequence>("SMFrameSequence");
31
    qRegisterMetaType< std::vector<SMFrameSequence> >("std::vector<SMFrameSequence>");
31
    qRegisterMetaType< std::vector<SMFrameSequence> >("std::vector<SMFrameSequence>");
32
    qRegisterMetaType<pcl::PointCloud<pcl::PointXYZRGB>::Ptr>("pcl::PointCloud<pcl::PointXYZRGB>::Ptr");
32
    qRegisterMetaType<pcl::PointCloud<pcl::PointXYZRGB>::Ptr>("pcl::PointCloud<pcl::PointXYZRGB>::Ptr");
33
    qRegisterMetaType<SMPointCloud>("SMPointCloud");
33
    qRegisterMetaType<SMPointCloud>("SMPointCloud");
34
    qRegisterMetaType< std::vector<SMPointCloud> >("std::vector<SMPointCloud>");
34
    qRegisterMetaType< std::vector<SMPointCloud> >("std::vector<SMPointCloud>");
35
 
35
 
36
    // Setup ui (requires stream operators registered)
36
    // Setup ui (requires stream operators registered)
37
    ui->setupUi(this);
37
    ui->setupUi(this);
38
 
38
 
39
    // Restore geometry
39
    // Restore geometry
40
    this->restoreGeometry(settings.value("geometry/mainwindow").toByteArray());
40
    this->restoreGeometry(settings.value("geometry/mainwindow").toByteArray());
41
    this->restoreState(settings.value("state/mainwindow").toByteArray());
41
    this->restoreState(settings.value("state/mainwindow").toByteArray());
42
 
42
 
43
    // Set up capture thread
43
    // Set up capture thread
44
    captureWorker = new SMCaptureWorker;
44
    captureWorker = new SMCaptureWorker;
45
    captureWorkerThread = new QThread(this);
45
    captureWorkerThread = new QThread(this);
46
    captureWorkerThread->setObjectName("captureWorkerThread");
46
    captureWorkerThread->setObjectName("captureWorkerThread");
47
    captureWorker->moveToThread(captureWorkerThread);
47
    captureWorker->moveToThread(captureWorkerThread);
48
    captureWorkerThread->start();
48
    captureWorkerThread->start();
49
 
49
 
50
    // Connections
50
    // Connections
51
    connect(captureWorker, SIGNAL(newFrame(unsigned int, cv::Mat)), this, SLOT(onReceiveFrame(unsigned int, cv::Mat)));
51
    connect(captureWorker, SIGNAL(newFrame(unsigned int, cv::Mat)), this, SLOT(onReceiveFrame(unsigned int, cv::Mat)));
52
    connect(captureWorker, SIGNAL(newCalibrationSet(SMCalibrationSet)), this, SLOT(onReceiveCalibrationSet(SMCalibrationSet)));
52
    connect(captureWorker, SIGNAL(newCalibrationSet(SMCalibrationSet)), this, SLOT(onReceiveCalibrationSet(SMCalibrationSet)));
53
    connect(captureWorker, SIGNAL(newFrameSequence(SMFrameSequence)), this, SLOT(onReceiveFrameSequence(SMFrameSequence)));
53
    connect(captureWorker, SIGNAL(newFrameSequence(SMFrameSequence)), this, SLOT(onReceiveFrameSequence(SMFrameSequence)));
54
    connect(captureWorker, SIGNAL(rotatedTo(float)), this, SLOT(onReceiveRotatedTo(float)));
54
    connect(captureWorker, SIGNAL(rotatedTo(float)), this, SLOT(onReceiveRotatedTo(float)));
55
 
55
 
56
    // Start capturing
56
    // Start capturing
57
    QMetaObject::invokeMethod(captureWorker, "setup");
57
    QMetaObject::invokeMethod(captureWorker, "setup");
58
    QMetaObject::invokeMethod(captureWorker, "doWork");
58
    QMetaObject::invokeMethod(captureWorker, "doWork");
59
 
59
 
60
    // Set up calibration thread
60
    // Set up calibration thread
61
    calibrationWorker = new SMCalibrationWorker;
61
    calibrationWorker = new SMCalibrationWorker;
62
    calibrationWorkerThread = new QThread(this);
62
    calibrationWorkerThread = new QThread(this);
63
    calibrationWorkerThread->setObjectName("calibrationWorkerThread");
63
    calibrationWorkerThread->setObjectName("calibrationWorkerThread");
64
    calibrationWorker->moveToThread(calibrationWorkerThread);
64
    calibrationWorker->moveToThread(calibrationWorkerThread);
65
    calibrationWorkerThread->start();
65
    calibrationWorkerThread->start();
66
 
66
 
67
    // Connections
67
    // Connections
68
    connect(calibrationWorker, SIGNAL(newCheckerboardResult(int, SMCalibrationSet)), this, SLOT(onReceiveCheckerboardResult(int, SMCalibrationSet)));
68
    connect(calibrationWorker, SIGNAL(newCheckerboardResult(int, SMCalibrationSet)), this, SLOT(onReceiveCheckerboardResult(int, SMCalibrationSet)));
69
    connect(calibrationWorker, SIGNAL(done()), this, SLOT(onReceiveCalibrationDone()));
69
    connect(calibrationWorker, SIGNAL(done()), this, SLOT(onReceiveCalibrationDone()));
70
    connect(calibrationWorker, SIGNAL(done()), ui->pointCloudWidget, SLOT(updateCalibrationParameters()));
70
    connect(calibrationWorker, SIGNAL(done()), ui->pointCloudWidget, SLOT(updateCalibrationParameters()));
71
//    connect(calibrationWorker, SIGNAL(done()), calibrationWorkerThread, SLOT(quit()));
71
//    connect(calibrationWorker, SIGNAL(done()), calibrationWorkerThread, SLOT(quit()));
72
//    connect(calibrationWorker, SIGNAL(done()), calibrationWorker, SLOT(deleteLater()));
72
//    connect(calibrationWorker, SIGNAL(done()), calibrationWorker, SLOT(deleteLater()));
73
    connect(calibrationWorker, SIGNAL(logMessage(QString)), &logDialog, SLOT(showLogMessage(QString)));
73
    connect(calibrationWorker, SIGNAL(logMessage(QString)), &logDialog, SLOT(showLogMessage(QString)));
74
 
74
 
75
    // Set up reconstruction thread
75
    // Set up reconstruction thread
76
    reconstructionWorker = new SMReconstructionWorker;
76
    reconstructionWorker = new SMReconstructionWorker;
77
    reconstructionWorkerThread = new QThread(this);
77
    reconstructionWorkerThread = new QThread(this);
78
    reconstructionWorkerThread->setObjectName("reconstructionWorkerThread");
78
    reconstructionWorkerThread->setObjectName("reconstructionWorkerThread");
79
    reconstructionWorker->moveToThread(reconstructionWorkerThread);
79
    reconstructionWorker->moveToThread(reconstructionWorkerThread);
80
    reconstructionWorkerThread->start();
80
    reconstructionWorkerThread->start();
81
 
81
 
82
    // Connections
82
    // Connections
83
    connect(reconstructionWorker, SIGNAL(newPointCloud(SMPointCloud)), this, SLOT(onReceivePointCloud(SMPointCloud)));
83
    connect(reconstructionWorker, SIGNAL(newPointCloud(SMPointCloud)), this, SLOT(onReceivePointCloud(SMPointCloud)));
84
    connect(reconstructionWorker, SIGNAL(done()), reconstructionWorkerThread, SLOT(quit()));
84
    connect(reconstructionWorker, SIGNAL(done()), reconstructionWorkerThread, SLOT(quit()));
85
    connect(reconstructionWorker, SIGNAL(done()), reconstructionWorker, SLOT(deleteLater()));
85
    connect(reconstructionWorker, SIGNAL(done()), reconstructionWorker, SLOT(deleteLater()));
86
 
86
 
87
}
87
}
88
 
88
 
89
void SMScanner::onReceiveFrame(unsigned int camId, cv::Mat frame){
89
void SMScanner::onReceiveFrame(unsigned int camId, cv::Mat frame){
90
 
90
 
91
    if(camId == 0){
91
    if(camId == 0){
92
        if(!calibrationReviewMode)
92
        if(!calibrationReviewMode)
93
            ui->calibrationCamera0Widget->showImageCV(frame);
93
            ui->calibrationCamera0Widget->showImageCV(frame);
94
        if(!captureReviewMode)
94
        if(!captureReviewMode)
95
            ui->captureCamera0Widget->showImageCV(frame);
95
            ui->captureCamera0Widget->showImageCV(frame);
96
    } else if(camId == 1){
96
    } else if(camId == 1){
97
        if(!calibrationReviewMode)
97
        if(!calibrationReviewMode)
98
            ui->calibrationCamera1Widget->showImageCV(frame);
98
            ui->calibrationCamera1Widget->showImageCV(frame);
99
        if(!captureReviewMode)
99
        if(!captureReviewMode)
100
            ui->captureCamera1Widget->showImageCV(frame);
100
            ui->captureCamera1Widget->showImageCV(frame);
101
    }
101
    }
102
}
102
}
103
 
103
 
104
void SMScanner::on_actionPreferences_triggered(){
104
void SMScanner::on_actionPreferences_triggered(){
105
 
105
 
106
    connect(&preferenceDialog, SIGNAL(accepted()), this, SLOT(onPreferencesChanged()));
106
    connect(&preferenceDialog, SIGNAL(accepted()), this, SLOT(onPreferencesChanged()));
107
 
107
 
108
    preferenceDialog.show();
108
    preferenceDialog.show();
109
}
109
}
110
 
110
 
111
void SMScanner::onPreferencesChanged(){
111
void SMScanner::onPreferencesChanged(){
112
 
112
 
113
    // Stop capture worker
113
    // Stop capture worker
114
//    connect(captureWorker, SIGNAL(finished()), captureWorker, SLOT(deleteLater()));
114
//    connect(captureWorker, SIGNAL(finished()), captureWorker, SLOT(deleteLater()));
115
//    connect(captureWorker, SIGNAL(finished()), captureWorkerThread, SLOT(quit()));
115
//    connect(captureWorker, SIGNAL(finished()), captureWorkerThread, SLOT(quit()));
116
    QMetaObject::invokeMethod(captureWorker, "stopWork");
116
    QMetaObject::invokeMethod(captureWorker, "stopWork");
117
//    captureWorkerThread->quit();
117
//    captureWorkerThread->quit();
118
//    captureWorkerThread->wait();
118
//    captureWorkerThread->wait();
119
 
119
 
120
//    // Restart capture worker
120
//    // Restart capture worker
121
//    captureWorker = new SMCaptureWorker;
121
//    captureWorker = new SMCaptureWorker;
122
//    captureWorkerThread = new QThread(this);
122
//    captureWorkerThread = new QThread(this);
123
//    captureWorkerThread->setObjectName("captureWorkerThread");
123
//    captureWorkerThread->setObjectName("captureWorkerThread");
124
//    captureWorker->moveToThread(captureWorkerThread);
124
//    captureWorker->moveToThread(captureWorkerThread);
125
//    captureWorkerThread->start();
125
//    captureWorkerThread->start();
126
 
126
 
127
//    connect(captureWorker, SIGNAL(newFrame(unsigned int, cv::Mat)), this, SLOT(onReceiveFrame(unsigned int, cv::Mat)));
127
//    connect(captureWorker, SIGNAL(newFrame(unsigned int, cv::Mat)), this, SLOT(onReceiveFrame(unsigned int, cv::Mat)));
128
//    connect(captureWorker, SIGNAL(newCalibrationSet(SMCalibrationSet)), this, SLOT(onReceiveCalibrationSet(SMCalibrationSet)));
128
//    connect(captureWorker, SIGNAL(newCalibrationSet(SMCalibrationSet)), this, SLOT(onReceiveCalibrationSet(SMCalibrationSet)));
129
//    connect(captureWorker, SIGNAL(newFrameSequence(SMFrameSequence)), this, SLOT(onReceiveFrameSequence(SMFrameSequence)));
129
//    connect(captureWorker, SIGNAL(newFrameSequence(SMFrameSequence)), this, SLOT(onReceiveFrameSequence(SMFrameSequence)));
130
//    connect(captureWorker, SIGNAL(rotatedTo(float)), this, SLOT(onReceiveRotatedTo(float)));
130
//    connect(captureWorker, SIGNAL(rotatedTo(float)), this, SLOT(onReceiveRotatedTo(float)));
131
 
131
 
132
    QMetaObject::invokeMethod(captureWorker, "setup");
132
    QMetaObject::invokeMethod(captureWorker, "setup");
133
    QMetaObject::invokeMethod(captureWorker, "doWork");
133
    QMetaObject::invokeMethod(captureWorker, "doWork");
134
 
134
 
135
}
135
}
136
 
136
 
137
void SMScanner::closeEvent(QCloseEvent *event){
137
void SMScanner::closeEvent(QCloseEvent *event){
138
 
138
 
139
    // Close dialogs
139
    // Close dialogs
140
    preferenceDialog.close();
140
    preferenceDialog.close();
141
    logDialog.close();
141
    logDialog.close();
142
 
142
 
143
    // Stop capturing thread
143
    // Stop capturing thread
144
    connect(captureWorker, SIGNAL(finished()), captureWorker, SLOT(deleteLater()));
144
    connect(captureWorker, SIGNAL(finished()), captureWorker, SLOT(deleteLater()));
145
    connect(captureWorker, SIGNAL(finished()), captureWorkerThread, SLOT(quit()));
145
    connect(captureWorker, SIGNAL(finished()), captureWorkerThread, SLOT(quit()));
146
    QMetaObject::invokeMethod(captureWorker, "stopWork");
146
    QMetaObject::invokeMethod(captureWorker, "stopWork");
147
    captureWorkerThread->quit();
147
    captureWorkerThread->quit();
148
    captureWorkerThread->wait();
148
    captureWorkerThread->wait();
149
 
149
 
150
    // Save window geometry
150
    // Save window geometry
151
    settings.setValue("geometry/mainwindow", this->saveGeometry());
151
    settings.setValue("geometry/mainwindow", this->saveGeometry());
152
    settings.setValue("state/mainwindow", this->saveState());
152
    settings.setValue("state/mainwindow", this->saveState());
153
 
153
 
154
    event->accept();
154
    event->accept();
155
 
155
 
156
}
156
}
157
 
157
 
158
SMScanner::~SMScanner(){
158
SMScanner::~SMScanner(){
159
    delete ui;
159
    delete ui;
160
}
160
}
161
 
161
 
162
void SMScanner::on_singleCalibrationButton_clicked(){
162
void SMScanner::on_singleCalibrationButton_clicked(){
163
 
163
 
164
    // If in review mode, go back to aquisition mode
164
    // If in review mode, go back to aquisition mode
165
    if(calibrationReviewMode){
165
    if(calibrationReviewMode){
166
        ui->calibrationListWidget->clearSelection();
166
        ui->calibrationListWidget->clearSelection();
167
        ui->singleCalibrationButton->setText("Single Aquisition");
167
        ui->singleCalibrationButton->setText("Single Aquisition");
168
        ui->batchCalibrationButton->setText("Batch Aquisition");
168
        ui->batchCalibrationButton->setText("Batch Aquisition");
169
        calibrationReviewMode = false;
169
        calibrationReviewMode = false;
170
        return;
170
        return;
171
    }
171
    }
172
 
172
 
173
    QMetaObject::invokeMethod(captureWorker, "acquireCalibrationSet", Q_ARG(float, -1.0));
173
    QMetaObject::invokeMethod(captureWorker, "acquireCalibrationSet", Q_ARG(float, -1.0));
174
 
174
 
175
}
175
}
176
 
176
 
177
 
177
 
178
void SMScanner::on_batchCalibrationButton_clicked(){
178
void SMScanner::on_batchCalibrationButton_clicked(){
179
 
179
 
180
    // If in review mode, go back to aquisition mode
180
    // If in review mode, go back to aquisition mode
181
    if(calibrationReviewMode){
181
    if(calibrationReviewMode){
182
        ui->calibrationListWidget->clearSelection();
182
        ui->calibrationListWidget->clearSelection();
183
        ui->singleCalibrationButton->setText("Single Aquisition");
183
        ui->singleCalibrationButton->setText("Single Aquisition");
184
        ui->batchCalibrationButton->setText("Batch Aquisition");
184
        ui->batchCalibrationButton->setText("Batch Aquisition");
185
        calibrationReviewMode = false;
185
        calibrationReviewMode = false;
186
        return;
186
        return;
187
    }
187
    }
188
 
188
 
189
    // Construct vector of angles
189
    // Construct vector of angles
190
    int angleStart = ui->calibrationBatchStartSpinBox->value();
190
    int angleStart = ui->calibrationBatchStartSpinBox->value();
191
    int angleEnd = ui->calibrationBatchEndSpinBox->value();
191
    int angleEnd = ui->calibrationBatchEndSpinBox->value();
192
    int angleStep = ui->calibrationBatchStepSpinBox->value();
192
    int angleStep = ui->calibrationBatchStepSpinBox->value();
193
 
193
 
194
    if(angleStart > angleEnd)
194
    if(angleStart > angleEnd)
195
        angleEnd += 360;
195
        angleEnd += 360;
196
 
196
 
197
    std::vector<float> angles;
197
    std::vector<float> angles;
198
    for(int i=angleStart; i<=angleEnd; i+=angleStep)
198
    for(int i=angleStart; i<=angleEnd; i+=angleStep)
199
        angles.push_back(i % 360);
199
        angles.push_back(i % 360);
200
 
200
 
201
    QMetaObject::invokeMethod(captureWorker, "acquireCalibrationSets", Q_ARG(std::vector<float>, angles));
201
    QMetaObject::invokeMethod(captureWorker, "acquireCalibrationSets", Q_ARG(std::vector<float>, angles));
202
 
202
 
203
    std::cout << "Aquiring sets at ";
203
    std::cout << "Aquiring sets at ";
204
    for(unsigned int i=0; i<angles.size(); i++)
204
    for(unsigned int i=0; i<angles.size(); i++)
205
        std::cout << angles[i] << " ";
205
        std::cout << angles[i] << " ";
206
    std::cout << " degrees" <<std::endl;
206
    std::cout << " degrees" <<std::endl;
207
}
207
}
208
 
208
 
209
 
209
 
210
void SMScanner::on_calibrationRotationDial_sliderReleased(){
210
void SMScanner::on_calibrationRotationDial_sliderReleased(){
211
 
211
 
212
    float angle = ui->calibrationRotationDial->value();
212
    float angle = ui->calibrationRotationDial->value();
213
    std::cout << "Rotation stage target: " << angle << std::endl;
213
    std::cout << "Rotation stage target: " << angle << std::endl;
214
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
214
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
215
 
215
 
216
    ui->calibrationRotationSpinBox->setValue(angle);
216
    ui->calibrationRotationSpinBox->setValue(angle);
217
 
217
 
218
    ui->captureRotationDial->setValue(angle);
218
    ui->captureRotationDial->setValue(angle);
219
    ui->captureRotationSpinBox->setValue(angle);
219
    ui->captureRotationSpinBox->setValue(angle);
220
}
220
}
221
 
221
 
222
 
222
 
223
void SMScanner::on_calibrationRotationSpinBox_editingFinished(){
223
void SMScanner::on_calibrationRotationSpinBox_editingFinished(){
224
 
224
 
225
    float angle = ui->calibrationRotationSpinBox->value();
225
    float angle = ui->calibrationRotationSpinBox->value();
226
    std::cout << "Rotation stage target: " << angle << std::endl;
226
    std::cout << "Rotation stage target: " << angle << std::endl;
227
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
227
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
228
 
228
 
229
    ui->calibrationRotationDial->setValue(angle);
229
    ui->calibrationRotationDial->setValue(angle);
230
 
230
 
231
    ui->captureRotationSpinBox->setValue(angle);
231
    ui->captureRotationSpinBox->setValue(angle);
232
    ui->captureRotationDial->setValue(angle);
232
    ui->captureRotationDial->setValue(angle);
233
}
233
}
234
 
234
 
235
void SMScanner::onReceiveCalibrationSet(SMCalibrationSet calibrationSet){
235
void SMScanner::onReceiveCalibrationSet(SMCalibrationSet calibrationSet){
236
 
236
 
237
    // Send to checkerboard detection on calibration thread
237
    // Send to checkerboard detection on calibration thread
238
 
238
 
239
    if(settings.value("calibration/method").toString() == "Charuco")
239
    if(settings.value("calibration/method").toString() == "Charuco")
240
        onReceiveCheckerboardResult(calibrationSet.id, calibrationSet);
240
        QMetaObject::invokeMethod(calibrationWorker, "checkerboardDetectionCharuco", Q_ARG(SMCalibrationSet, calibrationSet));
241
    else
241
    else
242
        QMetaObject::invokeMethod(calibrationWorker, "checkerboardDetection", Q_ARG(SMCalibrationSet, calibrationSet));
242
        QMetaObject::invokeMethod(calibrationWorker, "checkerboardDetection", Q_ARG(SMCalibrationSet, calibrationSet));
243
 
243
 
244
}
244
}
245
 
245
 
246
void SMScanner::on_calibrateCamerasButton_clicked(){
246
void SMScanner::on_calibrateCamerasButton_clicked(){
247
 
247
 
248
    // disable ui elements
248
    // disable ui elements
249
    ui->calibrateCamerasButton->setEnabled(false);
249
    ui->calibrateCamerasButton->setEnabled(false);
250
    ui->calibrateRotationStageButton->setEnabled(false);
250
    ui->calibrateRotationStageButton->setEnabled(false);
251
    ui->calibrationFrame->setEnabled(false);
251
    ui->calibrationFrame->setEnabled(false);
252
 
252
 
253
    // if none items are selected, we will use all available items
253
    // if none items are selected, we will use all available items
254
    if(ui->calibrationListWidget->selectedItems().empty())
254
    if(ui->calibrationListWidget->selectedItems().empty())
255
        ui->calibrationListWidget->selectAll();
255
        ui->calibrationListWidget->selectAll();
256
 
256
 
257
    // set selected flags
257
    // set selected flags
258
    for(unsigned int i=0; i<calibrationData.size(); i++){
258
    for(unsigned int i=0; i<calibrationData.size(); i++){
259
        if(ui->calibrationListWidget->item(i)->isSelected())
259
        if(ui->calibrationListWidget->item(i)->isSelected())
260
            calibrationData[i].selected = true;
260
            calibrationData[i].selected = true;
261
        else
261
        else
262
            calibrationData[i].selected = false;
262
            calibrationData[i].selected = false;
263
    }
263
    }
264
 
264
 
265
    logDialog.show();
265
    logDialog.show();
266
 
266
 
267
    // start calibration
267
    // start calibration
268
    QSettings settings;
268
    QSettings settings;
269
    if(settings.value("calibration/method").toString() == "Charuco")
269
    if(settings.value("calibration/method").toString() == "Charuco")
270
        QMetaObject::invokeMethod(calibrationWorker, "cameraCalibrationCharuco", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
270
        QMetaObject::invokeMethod(calibrationWorker, "cameraCalibrationCharuco", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
271
    else
271
    else
272
        QMetaObject::invokeMethod(calibrationWorker, "cameraCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
272
        QMetaObject::invokeMethod(calibrationWorker, "cameraCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
273
 
273
 
274
 
274
 
275
}
275
}
276
 
276
 
277
void SMScanner::onReceiveCheckerboardResult(int idx, SMCalibrationSet calibrationSet){
277
void SMScanner::onReceiveCheckerboardResult(int idx, SMCalibrationSet calibrationSet){
278
 
278
 
279
    int id = ui->calibrationListWidget->count();
279
    int id = ui->calibrationListWidget->count();
280
    calibrationSet.id = id;
280
    calibrationSet.id = id;
281
 
281
 
282
    calibrationData.push_back(calibrationSet);
282
    calibrationData.push_back(calibrationSet);
283
 
283
 
284
    // Add identifier to list
284
    // Add identifier to list
285
    QListWidgetItem* item = new QListWidgetItem(QString("Calibration Set %1 -- %2 deg").arg(id).arg(calibrationSet.rotationAngle), ui->calibrationListWidget);
285
    QListWidgetItem* item = new QListWidgetItem(QString("Calibration Set %1 -- %2 deg").arg(id).arg(calibrationSet.rotationAngle), ui->calibrationListWidget);
286
    ui->calibrationListWidget->addItem(item);
286
    ui->calibrationListWidget->addItem(item);
287
    item->setSelected(true);
287
    item->setSelected(true);
288
    ui->calibrationListWidget->setCurrentItem(item);
288
    ui->calibrationListWidget->setCurrentItem(item);
289
 
289
 
290
    // Enable calibration button
290
    // Enable calibration button
291
    if(calibrationData.size() >= 2){
291
    if(calibrationData.size() >= 2){
292
        ui->calibrateCamerasButton->setEnabled(true);
292
        ui->calibrateCamerasButton->setEnabled(true);
293
        ui->calibrateRotationStageButton->setEnabled(true);
293
        ui->calibrateRotationStageButton->setEnabled(true);
294
    }
294
    }
295
 
295
 
296
    //ui->calibrationListWidget->setCurrentRow(idx);
296
    //ui->calibrationListWidget->setCurrentRow(idx);
297
}
297
}
298
 
298
 
299
void SMScanner::onReceiveCalibrationDone(){
299
void SMScanner::onReceiveCalibrationDone(){
300
 
300
 
301
    std::cout << "Calibration done!" << std::endl;
301
    std::cout << "Calibration done!" << std::endl;
302
    ui->calibrateCamerasButton->setEnabled(true);
302
    ui->calibrateCamerasButton->setEnabled(true);
303
    ui->calibrateRotationStageButton->setEnabled(true);
303
    ui->calibrateRotationStageButton->setEnabled(true);
304
    ui->calibrationFrame->setEnabled(true);
304
    ui->calibrationFrame->setEnabled(true);
305
 
305
 
306
}
306
}
307
 
307
 
308
void SMScanner::on_calibrationListWidget_itemSelectionChanged(){
308
void SMScanner::on_calibrationListWidget_itemSelectionChanged(){
309
 
309
 
310
    // if selection is just cleared
310
    // if selection is just cleared
311
    if(ui->calibrationListWidget->selectedItems().empty())
311
    if(ui->calibrationListWidget->selectedItems().empty())
312
        return;
312
        return;
313
 
313
 
314
    calibrationReviewMode = true;
314
    calibrationReviewMode = true;
315
    ui->singleCalibrationButton->setText("Live View");
315
    ui->singleCalibrationButton->setText("Live View");
316
    ui->batchCalibrationButton->setText("Live View");
316
    ui->batchCalibrationButton->setText("Live View");
317
 
317
 
318
    int currentRow = ui->calibrationListWidget->currentRow();
318
    int currentRow = ui->calibrationListWidget->currentRow();
319
    if(currentRow < 0)
319
    if(currentRow < 0)
320
        return;
320
        return;
321
 
321
 
322
    assert(currentRow < (int)calibrationData.size());
322
    assert(currentRow < (int)calibrationData.size());
323
 
323
 
324
    // if checkerboard results are available, show them, otherwise show the frame
324
    // if checkerboard results are available, show them, otherwise show the frame
325
    if(calibrationData[currentRow].frame0Result.empty())
325
    if(calibrationData[currentRow].frame0Result.empty())
326
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0);
326
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0);
327
    else
327
    else
328
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0Result);
328
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0Result);
329
 
329
 
330
    if(calibrationData[currentRow].frame1Result.empty())
330
    if(calibrationData[currentRow].frame1Result.empty())
331
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1);
331
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1);
332
    else
332
    else
333
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1Result);
333
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1Result);
334
 
334
 
335
}
335
}
336
 
336
 
337
void SMScanner::on_captureRotationDial_sliderReleased(){
337
void SMScanner::on_captureRotationDial_sliderReleased(){
338
 
338
 
339
    float angle = ui->captureRotationDial->value();
339
    float angle = ui->captureRotationDial->value();
340
    std::cout << "Rotation stage target: " << angle << std::endl;
340
    std::cout << "Rotation stage target: " << angle << std::endl;
341
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
341
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
342
 
342
 
343
    ui->captureRotationSpinBox->setValue(ui->captureRotationDial->value());
343
    ui->captureRotationSpinBox->setValue(ui->captureRotationDial->value());
344
 
344
 
345
    ui->calibrationRotationDial->setValue(ui->captureRotationDial->value());
345
    ui->calibrationRotationDial->setValue(ui->captureRotationDial->value());
346
    ui->calibrationRotationSpinBox->setValue(ui->captureRotationDial->value());
346
    ui->calibrationRotationSpinBox->setValue(ui->captureRotationDial->value());
347
}
347
}
348
 
348
 
349
void SMScanner::on_captureRotationSpinBox_editingFinished(){
349
void SMScanner::on_captureRotationSpinBox_editingFinished(){
350
 
350
 
351
    float angle = ui->captureRotationSpinBox->value();
351
    float angle = ui->captureRotationSpinBox->value();
352
    std::cout << "Rotation stage target: " << angle << std::endl;
352
    std::cout << "Rotation stage target: " << angle << std::endl;
353
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
353
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
354
 
354
 
355
    ui->captureRotationDial->setValue(angle);
355
    ui->captureRotationDial->setValue(angle);
356
 
356
 
357
    ui->calibrationRotationSpinBox->setValue(angle);
357
    ui->calibrationRotationSpinBox->setValue(angle);
358
    ui->calibrationRotationDial->setValue(angle);
358
    ui->calibrationRotationDial->setValue(angle);
359
}
359
}
360
 
360
 
361
void SMScanner::on_singleCaptureButton_clicked(){
361
void SMScanner::on_singleCaptureButton_clicked(){
362
 
362
 
363
    // If in review mode, go back to aquisition mode
363
    // If in review mode, go back to aquisition mode
364
    if(captureReviewMode){
364
    if(captureReviewMode){
365
        ui->captureTreeWidget->clearSelection();
365
        ui->captureTreeWidget->clearSelection();
366
        ui->singleCaptureButton->setText("Single Aquisition");
366
        ui->singleCaptureButton->setText("Single Aquisition");
367
        ui->batchCaptureButton->setText("Batch Aquisition");
367
        ui->batchCaptureButton->setText("Batch Aquisition");
368
        captureReviewMode = false;
368
        captureReviewMode = false;
369
        return;
369
        return;
370
    }
370
    }
371
 
371
 
372
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequence", Q_ARG(float, -1.0));
372
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequence", Q_ARG(float, -1.0));
373
 
373
 
374
}
374
}
375
 
375
 
376
 
376
 
377
void SMScanner::on_batchCaptureButton_clicked(){
377
void SMScanner::on_batchCaptureButton_clicked(){
378
 
378
 
379
    // If in review mode, go back to aquisition mode
379
    // If in review mode, go back to aquisition mode
380
    if(captureReviewMode){
380
    if(captureReviewMode){
381
        ui->captureTreeWidget->clearSelection();
381
        ui->captureTreeWidget->clearSelection();
382
        ui->singleCaptureButton->setText("Single Aquisition");
382
        ui->singleCaptureButton->setText("Single Aquisition");
383
        ui->batchCaptureButton->setText("Batch Aquisition");
383
        ui->batchCaptureButton->setText("Batch Aquisition");
384
        captureReviewMode = false;
384
        captureReviewMode = false;
385
        return;
385
        return;
386
    }
386
    }
387
 
387
 
388
    // Construct vector of angles
388
    // Construct vector of angles
389
    int angleStart = ui->captureBatchStartSpinBox->value();
389
    int angleStart = ui->captureBatchStartSpinBox->value();
390
    int angleEnd = ui->captureBatchEndSpinBox->value();
390
    int angleEnd = ui->captureBatchEndSpinBox->value();
391
    int angleStep = ui->captureBatchStepSpinBox->value();
391
    int angleStep = ui->captureBatchStepSpinBox->value();
392
 
392
 
393
    if(angleStart > angleEnd)
393
    if(angleStart > angleEnd)
394
        angleEnd += 360;
394
        angleEnd += 360;
395
 
395
 
396
    std::vector<float> angles;
396
    std::vector<float> angles;
397
    for(int i=angleStart; i<=angleEnd; i+=angleStep)
397
    for(int i=angleStart; i<=angleEnd; i+=angleStep)
398
        angles.push_back(i % 360);
398
        angles.push_back(i % 360);
399
 
399
 
400
    std::cout << "Aquiring sequences at ";
400
    std::cout << "Aquiring sequences at ";
401
    for(unsigned int i=0; i<angles.size(); i++)
401
    for(unsigned int i=0; i<angles.size(); i++)
402
        std::cout << angles[i] << " ";
402
        std::cout << angles[i] << " ";
403
    std::cout << " degrees" <<std::endl;
403
    std::cout << " degrees" <<std::endl;
404
 
404
 
405
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequences", Q_ARG(std::vector<float>, angles));
405
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequences", Q_ARG(std::vector<float>, angles));
406
}
406
}
407
 
407
 
408
void SMScanner::onReceiveFrameSequence(SMFrameSequence frameSequence){
408
void SMScanner::onReceiveFrameSequence(SMFrameSequence frameSequence){
409
 
409
 
410
    frameSequence.id = ++lastCaptureId;
410
    frameSequence.id = ++lastCaptureId;
411
 
411
 
412
    // Add identifier to list
412
    // Add identifier to list
413
    QTreeWidgetItem* item = new QTreeWidgetItem(ui->captureTreeWidget);
413
    QTreeWidgetItem* item = new QTreeWidgetItem(ui->captureTreeWidget);
414
    item->setText(0, QString("Frame Sequence %1 -- %2 deg").arg(frameSequence.id).arg(frameSequence.rotationAngle));
414
    item->setText(0, QString("Frame Sequence %1 -- %2 deg").arg(frameSequence.id).arg(frameSequence.rotationAngle));
415
    //item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
415
    //item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
416
    item->setData(0, Qt::UserRole, QPoint(frameSequence.id, -1));
416
    item->setData(0, Qt::UserRole, QPoint(frameSequence.id, -1));
417
    //item->setCheckState(0, Qt::Checked);
417
    //item->setCheckState(0, Qt::Checked);
418
    //ui->captureTreeWidget->addItem(item);
418
    //ui->captureTreeWidget->addItem(item);
419
 
419
 
420
    for(unsigned int i=0; i<frameSequence.frames0.size(); i++){
420
    for(unsigned int i=0; i<frameSequence.frames0.size(); i++){
421
        QTreeWidgetItem* subItem = new QTreeWidgetItem(item);
421
        QTreeWidgetItem* subItem = new QTreeWidgetItem(item);
422
        subItem->setText(0, QString("frames %1").arg(i));
422
        subItem->setText(0, QString("frames %1").arg(i));
423
        subItem->setData(0, Qt::UserRole, QPoint(frameSequence.id, i));
423
        subItem->setData(0, Qt::UserRole, QPoint(frameSequence.id, i));
424
    }
424
    }
425
 
425
 
426
    // Indicate that the current item is currently reconstructing
426
    // Indicate that the current item is currently reconstructing
427
    item->setTextColor(0, QColor(128, 128, 128));
427
    item->setTextColor(0, QColor(128, 128, 128));
428
    item->setIcon(0, QIcon::fromTheme("system-run"));
428
    item->setIcon(0, QIcon::fromTheme("system-run"));
429
 
429
 
430
    // Reconstruct the frame sequence
430
    // Reconstruct the frame sequence
431
    QMetaObject::invokeMethod(reconstructionWorker, "reconstructPointCloud", Q_ARG(SMFrameSequence, frameSequence));
431
    QMetaObject::invokeMethod(reconstructionWorker, "reconstructPointCloud", Q_ARG(SMFrameSequence, frameSequence));
432
    frameSequence.reconstructed = true;
432
    frameSequence.reconstructed = true;
433
 
433
 
434
    captureData.push_back(frameSequence);
434
    captureData.push_back(frameSequence);
435
 
435
 
436
}
436
}
437
 
437
 
438
void SMScanner::on_captureTreeWidget_itemSelectionChanged(){
438
void SMScanner::on_captureTreeWidget_itemSelectionChanged(){
439
 
439
 
440
    // if selection is just cleared
440
    // if selection is just cleared
441
    if(ui->captureTreeWidget->selectedItems().empty())
441
    if(ui->captureTreeWidget->selectedItems().empty())
442
        return;
442
        return;
443
 
443
 
444
    QTreeWidgetItem *item = ui->captureTreeWidget->currentItem();
444
    QTreeWidgetItem *item = ui->captureTreeWidget->currentItem();
445
 
445
 
446
    captureReviewMode = true;
446
    captureReviewMode = true;
447
 
447
 
448
    ui->singleCaptureButton->setText("Live View");
448
    ui->singleCaptureButton->setText("Live View");
449
    ui->batchCaptureButton->setText("Live View");
449
    ui->batchCaptureButton->setText("Live View");
450
 
450
 
451
    QPoint idx = item->data(0, Qt::UserRole).toPoint();
451
    QPoint idx = item->data(0, Qt::UserRole).toPoint();
452
 
452
 
453
    if( idx.y() != -1 && idx.x()>=0 && idx.x()<(int)captureData.size()
453
    if( idx.y() != -1 && idx.x()>=0 && idx.x()<(int)captureData.size()
454
            && idx.y()<(int)captureData[idx.x()].frames0.size()
454
            && idx.y()<(int)captureData[idx.x()].frames0.size()
455
            && idx.y()<(int)captureData[idx.x()].frames1.size() ){
455
            && idx.y()<(int)captureData[idx.x()].frames1.size() ){
456
        // TODO idx.x() can & WILL be out of bounds
456
        // TODO idx.x() can & WILL be out of bounds
457
        ui->captureCamera0Widget->showImageCV(captureData[idx.x()].frames0[idx.y()]);
457
        ui->captureCamera0Widget->showImageCV(captureData[idx.x()].frames0[idx.y()]);
458
        ui->captureCamera1Widget->showImageCV(captureData[idx.x()].frames1[idx.y()]);
458
        ui->captureCamera1Widget->showImageCV(captureData[idx.x()].frames1[idx.y()]);
459
    }
459
    }
460
 
460
 
461
//     std::cout << "on_captureTreeWidget_itemSelectionChanged" << std::endl;
461
//     std::cout << "on_captureTreeWidget_itemSelectionChanged" << std::endl;
462
}
462
}
463
 
463
 
464
 
464
 
465
void SMScanner::onReceiveRotatedTo(float angle){
465
void SMScanner::onReceiveRotatedTo(float angle){
466
 
466
 
467
    // update ui with new position
467
    // update ui with new position
468
    ui->calibrationRotationDial->setValue(angle);
468
    ui->calibrationRotationDial->setValue(angle);
469
    ui->captureRotationDial->setValue(angle);
469
    ui->captureRotationDial->setValue(angle);
470
 
470
 
471
    ui->calibrationRotationSpinBox->setValue(angle);
471
    ui->calibrationRotationSpinBox->setValue(angle);
472
    ui->captureRotationSpinBox->setValue(angle);
472
    ui->captureRotationSpinBox->setValue(angle);
473
}
473
}
474
 
474
 
475
void SMScanner::on_actionExport_Sets_triggered(){
475
void SMScanner::on_actionExport_Sets_triggered(){
476
 
476
 
477
    QString dirName = QFileDialog::getExistingDirectory(this, "Export calibration sets", QString());
477
    QString dirName = QFileDialog::getExistingDirectory(this, "Export calibration sets", QString());
478
 
478
 
479
    QProgressDialog progressDialog("Exporting Sets...", "Cancel", 0, 100, this);
479
    QProgressDialog progressDialog("Exporting Sets...", "Cancel", 0, 100, this);
480
    progressDialog.setWindowModality(Qt::WindowModal);
480
    progressDialog.setWindowModality(Qt::WindowModal);
481
    progressDialog.setMinimumDuration(1000);
481
    progressDialog.setMinimumDuration(1000);
482
 
482
 
483
    progressDialog.show();
483
    progressDialog.show();
484
    cv::Mat frameBGR;
484
    cv::Mat frameBGR;
485
    for(unsigned int i=0; i<calibrationData.size(); i++){
485
    for(unsigned int i=0; i<calibrationData.size(); i++){
486
        QString fileName = QString("%1/frame0_%2.png").arg(dirName).arg(i);
486
        QString fileName = QString("%1/frame0_%2.png").arg(dirName).arg(i);
487
 
487
 
488
        progressDialog.setValue(100.0*i/calibrationData.size());
488
        progressDialog.setValue(100.0*i/calibrationData.size());
489
 
489
 
490
        // Convert to BGR
490
        // Convert to BGR
491
        if(calibrationData[i].frame0.channels() == 1)
491
        if(calibrationData[i].frame0.channels() == 1)
492
            cv::cvtColor(calibrationData[i].frame0, frameBGR, CV_BayerBG2BGR);
492
            cv::cvtColor(calibrationData[i].frame0, frameBGR, CV_BayerBG2BGR);
493
        else
493
        else
494
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
494
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
495
 
495
 
496
        cv::imwrite(fileName.toStdString(), frameBGR);
496
        cv::imwrite(fileName.toStdString(), frameBGR);
497
        fileName = QString("%1/frame1_%2.png").arg(dirName).arg(i);
497
        fileName = QString("%1/frame1_%2.png").arg(dirName).arg(i);
498
 
498
 
499
        // Convert to BGR
499
        // Convert to BGR
500
        if(calibrationData[i].frame1.channels() == 1)
500
        if(calibrationData[i].frame1.channels() == 1)
501
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_BayerBG2BGR);
501
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_BayerBG2BGR);
502
        else
502
        else
503
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
503
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
504
 
504
 
505
        cv::imwrite(fileName.toStdString(), frameBGR);
505
        cv::imwrite(fileName.toStdString(), frameBGR);
506
 
506
 
507
        // Necessary to prevent pileup of video frame signals
507
        // Necessary to prevent pileup of video frame signals
508
        QCoreApplication::processEvents();
508
        QCoreApplication::processEvents();
509
 
509
 
510
        if(progressDialog.wasCanceled())
510
        if(progressDialog.wasCanceled())
511
            return;
511
            return;
512
    }
512
    }
513
 
513
 
514
}
514
}
515
 
515
 
516
void SMScanner::on_actionExport_Sequences_triggered(){
516
void SMScanner::on_actionExport_Sequences_triggered(){
517
 
517
 
518
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
518
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
519
 
519
 
520
    QProgressDialog progressDialog("Exporting Sequences...", "Cancel", 0, 100, this);
520
    QProgressDialog progressDialog("Exporting Sequences...", "Cancel", 0, 100, this);
521
    progressDialog.setWindowModality(Qt::WindowModal);
521
    progressDialog.setWindowModality(Qt::WindowModal);
522
    progressDialog.setMinimumDuration(1000);
522
    progressDialog.setMinimumDuration(1000);
523
 
523
 
524
    progressDialog.show();
524
    progressDialog.show();
525
 
525
 
526
    cv::Mat frameBGR;
526
    cv::Mat frameBGR;
527
    for(unsigned int i=0; i<captureData.size(); i++){
527
    for(unsigned int i=0; i<captureData.size(); i++){
528
 
528
 
529
        QString format;
529
        QString format;
530
        if(captureData[i].frames0[0].depth() == CV_32F)
530
        if(captureData[i].frames0[0].depth() == CV_32F)
531
            format = "hdr";
531
            format = "hdr";
532
        else
532
        else
533
            format = "png";
533
            format = "png";
534
 
534
 
535
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
535
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
536
        if(!QDir().mkdir(seqDirName))
536
        if(!QDir().mkdir(seqDirName))
537
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
537
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
538
        for(unsigned int j=0; j<captureData[i].frames0.size(); j++){
538
        for(unsigned int j=0; j<captureData[i].frames0.size(); j++){
539
 
539
 
540
            progressDialog.setValue(100.0*i/captureData.size() + 100.0/captureData.size()*j/captureData[i].frames0.size());
540
            progressDialog.setValue(100.0*i/captureData.size() + 100.0/captureData.size()*j/captureData[i].frames0.size());
541
 
541
 
542
            QString fileName0 = QString("%1/frames0_%2.%3").arg(seqDirName).arg(j).arg(format);
542
            QString fileName0 = QString("%1/frames0_%2.%3").arg(seqDirName).arg(j).arg(format);
543
 
543
 
544
            // Convert Bayer to rgb (png needs BGR order)
544
            // Convert Bayer to rgb (png needs BGR order)
545
            if(captureData[i].frames0[j].type() == CV_8UC1)
545
            if(captureData[i].frames0[j].type() == CV_8UC1)
546
                cv::cvtColor(captureData[i].frames0[j], frameBGR, CV_BayerBG2BGR);
546
                cv::cvtColor(captureData[i].frames0[j], frameBGR, CV_BayerBG2BGR);
547
            else
547
            else
548
                cv::cvtColor(captureData[i].frames0[j], frameBGR, CV_RGB2BGR);
548
                cv::cvtColor(captureData[i].frames0[j], frameBGR, CV_RGB2BGR);
549
 
549
 
550
            cv::imwrite(fileName0.toStdString(), frameBGR);
550
            cv::imwrite(fileName0.toStdString(), frameBGR);
551
 
551
 
552
            // Necessary to prevent pileup of video frame signals
552
            // Necessary to prevent pileup of video frame signals
553
            QCoreApplication::processEvents();
553
            QCoreApplication::processEvents();
554
 
554
 
555
            QString fileName1 = QString("%1/frames1_%2.%3").arg(seqDirName).arg(j).arg(format);
555
            QString fileName1 = QString("%1/frames1_%2.%3").arg(seqDirName).arg(j).arg(format);
556
 
556
 
557
            // Convert Bayer to rgb (png needs BGR order)
557
            // Convert Bayer to rgb (png needs BGR order)
558
            if(captureData[i].frames1[j].type() == CV_8UC1)
558
            if(captureData[i].frames1[j].type() == CV_8UC1)
559
                cv::cvtColor(captureData[i].frames1[j], frameBGR, CV_BayerBG2BGR);
559
                cv::cvtColor(captureData[i].frames1[j], frameBGR, CV_BayerBG2BGR);
560
            else
560
            else
561
                cv::cvtColor(captureData[i].frames1[j], frameBGR, CV_RGB2BGR);
561
                cv::cvtColor(captureData[i].frames1[j], frameBGR, CV_RGB2BGR);
562
 
562
 
563
            cv::imwrite(fileName1.toStdString(), frameBGR);
563
            cv::imwrite(fileName1.toStdString(), frameBGR);
564
 
564
 
565
            // Necessary to prevent pileup of video frame signals
565
            // Necessary to prevent pileup of video frame signals
566
            QCoreApplication::processEvents();
566
            QCoreApplication::processEvents();
567
            if(progressDialog.wasCanceled())
567
            if(progressDialog.wasCanceled())
568
                return;
568
                return;
569
        }
569
        }
570
    }
570
    }
571
}
571
}
572
 
572
 
573
void SMScanner::on_actionExport_White_Frames_triggered(){
573
void SMScanner::on_actionExport_White_Frames_triggered(){
574
 
574
 
575
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
575
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
576
 
576
 
577
    QProgressDialog progressDialog("Exporting White Frames...", "Cancel", 0, 100, this);
577
    QProgressDialog progressDialog("Exporting White Frames...", "Cancel", 0, 100, this);
578
    progressDialog.setWindowModality(Qt::WindowModal);
578
    progressDialog.setWindowModality(Qt::WindowModal);
579
    progressDialog.setMinimumDuration(1000);
579
    progressDialog.setMinimumDuration(1000);
580
 
580
 
581
    cv::Mat frameBGR;
581
    cv::Mat frameBGR;
582
    for(unsigned int i=0; i<captureData.size(); i++){
582
    for(unsigned int i=0; i<captureData.size(); i++){
583
 
583
 
584
        progressDialog.setValue(100.0*i/captureData.size());
584
        progressDialog.setValue(100.0*i/captureData.size());
585
 
585
 
586
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
586
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
587
        if(!QDir().mkdir(seqDirName))
587
        if(!QDir().mkdir(seqDirName))
588
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
588
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
589
 
589
 
590
        QString fileName = QString("%1/frames0_0.png").arg(seqDirName);
590
        QString fileName = QString("%1/frames0_0.png").arg(seqDirName);
591
        // Convert Bayer to rgb (png needs BGR order)
591
        // Convert Bayer to rgb (png needs BGR order)
592
        cv::cvtColor(captureData[i].frames0[0], frameBGR, CV_BayerBG2BGR);
592
        cv::cvtColor(captureData[i].frames0[0], frameBGR, CV_BayerBG2BGR);
593
        cv::imwrite(fileName.toStdString(), frameBGR);
593
        cv::imwrite(fileName.toStdString(), frameBGR);
594
        // Necessary to prevent memory pileup
594
        // Necessary to prevent memory pileup
595
        QCoreApplication::processEvents();
595
        QCoreApplication::processEvents();
596
 
596
 
597
        fileName = QString("%1/frames1_0.png").arg(seqDirName);
597
        fileName = QString("%1/frames1_0.png").arg(seqDirName);
598
        // Convert Bayer to rgb (png needs BGR order)
598
        // Convert Bayer to rgb (png needs BGR order)
599
        cv::cvtColor(captureData[i].frames1[0], frameBGR, CV_BayerBG2BGR);
599
        cv::cvtColor(captureData[i].frames1[0], frameBGR, CV_BayerBG2BGR);
600
        cv::imwrite(fileName.toStdString(), frameBGR);
600
        cv::imwrite(fileName.toStdString(), frameBGR);
601
        // Necessary to prevent memory pileup
601
        // Necessary to prevent memory pileup
602
        QCoreApplication::processEvents();
602
        QCoreApplication::processEvents();
603
        if(progressDialog.wasCanceled())
603
        if(progressDialog.wasCanceled())
604
            return;
604
            return;
605
    }
605
    }
606
 
606
 
607
}
607
}
608
 
608
 
609
 
609
 
610
void SMScanner::onReceivePointCloud(SMPointCloud smCloud){
610
void SMScanner::onReceivePointCloud(SMPointCloud smCloud){
611
 
611
 
612
    pointCloudData.push_back(smCloud);
612
    pointCloudData.push_back(smCloud);
613
 
613
 
614
    // Add identifier to list
614
    // Add identifier to list
615
    QListWidgetItem* item = new QListWidgetItem(QString("Point Cloud %1 -- %2 deg").arg(smCloud.id).arg(smCloud.rotationAngle));
615
    QListWidgetItem* item = new QListWidgetItem(QString("Point Cloud %1 -- %2 deg").arg(smCloud.id).arg(smCloud.rotationAngle));
616
    item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
616
    item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
617
    item->setData(Qt::UserRole, QVariant(smCloud.id));
617
    item->setData(Qt::UserRole, QVariant(smCloud.id));
618
    item->setCheckState(Qt::Checked);
618
    item->setCheckState(Qt::Checked);
619
 
619
 
620
    ui->pointCloudsListWidget->addItem(item);
620
    ui->pointCloudsListWidget->addItem(item);
621
    ui->pointCloudWidget->addPointCloud(smCloud, smCloud.id);
621
    ui->pointCloudWidget->addPointCloud(smCloud, smCloud.id);
622
 
622
 
623
    // Indicate completed reconstruction in captureTreeWidget
623
    // Indicate completed reconstruction in captureTreeWidget
624
    for(int i=0; i<ui->captureTreeWidget->topLevelItemCount(); i++){
624
    for(int i=0; i<ui->captureTreeWidget->topLevelItemCount(); i++){
625
        QTreeWidgetItem *captureItem = ui->captureTreeWidget->topLevelItem(i);
625
        QTreeWidgetItem *captureItem = ui->captureTreeWidget->topLevelItem(i);
626
        if(captureItem->data(0, Qt::UserRole).toPoint() == QPoint(smCloud.id, -1)){
626
        if(captureItem->data(0, Qt::UserRole).toPoint() == QPoint(smCloud.id, -1)){
627
            captureItem->setTextColor(0, QColor(0, 0, 0));
627
            captureItem->setTextColor(0, QColor(0, 0, 0));
628
            captureItem->setIcon(0, QIcon());
628
            captureItem->setIcon(0, QIcon());
629
        }
629
        }
630
   }
630
   }
631
 
631
 
632
}
632
}
633
 
633
 
634
void SMScanner::on_actionExport_Point_Clouds_triggered(){
634
void SMScanner::on_actionExport_Point_Clouds_triggered(){
635
 
635
 
636
    QString directory = QFileDialog::getExistingDirectory(this, "Export Point Clouds", QString());
636
    QString directory = QFileDialog::getExistingDirectory(this, "Export Point Clouds", QString());
637
 
637
 
638
//    //  Non native file dialog
638
//    //  Non native file dialog
639
//    QFileDialog saveDirectoryDialog(this, "Export Point Clouds", QString(), "*.pcd;;*.ply;;*.vtk;;*.png;;*.txt");
639
//    QFileDialog saveDirectoryDialog(this, "Export Point Clouds", QString(), "*.pcd;;*.ply;;*.vtk;;*.png;;*.txt");
640
//    saveDirectoryDialog.setDefaultSuffix("ply");
640
//    saveDirectoryDialog.setDefaultSuffix("ply");
641
//    saveDirectoryDialog.setFileMode(QFileDialog::Directory);
641
//    saveDirectoryDialog.setFileMode(QFileDialog::Directory);
642
//    saveDirectoryDialog.exec();
642
//    saveDirectoryDialog.exec();
643
//    QString directory = saveDirectoryDialog.directory().path();
643
//    QString directory = saveDirectoryDialog.directory().path();
644
//    QString type = saveDirectoryDialog.selectedNameFilter();
644
//    QString type = saveDirectoryDialog.selectedNameFilter();
645
 
645
 
646
    QProgressDialog progressDialog("Exporting Point Clouds...", "Cancel", 0, 100, this);
646
    QProgressDialog progressDialog("Exporting Point Clouds...", "Cancel", 0, 100, this);
647
    progressDialog.setWindowModality(Qt::WindowModal);
647
    progressDialog.setWindowModality(Qt::WindowModal);
648
    progressDialog.setMinimumDuration(1000);
648
    progressDialog.setMinimumDuration(1000);
649
 
649
 
650
    // save point clouds in ply format
650
    // save point clouds in ply format
651
    for(unsigned int i=0; i<pointCloudData.size(); i++){
651
    for(unsigned int i=0; i<pointCloudData.size(); i++){
652
 
652
 
653
        progressDialog.setValue(100.0*i/pointCloudData.size());
653
        progressDialog.setValue(100.0*i/pointCloudData.size());
654
    if(pointCloudData[i].id != -1){
654
    if(pointCloudData[i].id != -1){
655
        QString fileName = QString("%1/pointcloud_%2.ply").arg(directory).arg(i);
655
        QString fileName = QString("%1/pointcloud_%2.ply").arg(directory).arg(i);
656
        pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCL(pointCloudData[i].pointCloud);
656
        pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCL(pointCloudData[i].pointCloud);
657
        //pcl::io::savePLYFileBinary(fileName.toStdString(), *pointCloudPCL);
657
        //pcl::io::savePLYFileBinary(fileName.toStdString(), *pointCloudPCL);
658
        pcl::PLYWriter w;
658
        pcl::PLYWriter w;
659
        // Write to ply in binary without camera
659
        // Write to ply in binary without camera
660
        w.write<pcl::PointXYZRGBNormal> (fileName.toStdString(), *pointCloudPCL, true, false);
660
        w.write<pcl::PointXYZRGBNormal> (fileName.toStdString(), *pointCloudPCL, true, false);
661
    }
661
    }
662
        QCoreApplication::processEvents();
662
        QCoreApplication::processEvents();
663
        if(progressDialog.wasCanceled())
663
        if(progressDialog.wasCanceled())
664
            return;
664
            return;
665
    }
665
    }
666
 
666
 
667
    // save meshlab aln project file
667
    // save meshlab aln project file
668
    std::ofstream s(QString("%1/alignment.aln").arg(directory).toLocal8Bit());
668
    std::ofstream s(QString("%1/alignment.aln").arg(directory).toLocal8Bit());
669
    s << pointCloudData.size() << std::endl;
669
    s << pointCloudData.size() << std::endl;
670
    for(unsigned int i=0; i<pointCloudData.size(); i++){
670
    for(unsigned int i=0; i<pointCloudData.size(); i++){
671
        if(pointCloudData[i].id != -1){
671
        if(pointCloudData[i].id != -1){
672
        QString fileName = QString("pointcloud_%1.ply").arg(pointCloudData[i].id);
672
        QString fileName = QString("pointcloud_%1.ply").arg(pointCloudData[i].id);
673
        s << fileName.toStdString() << std::endl << "#" << std::endl;
673
        s << fileName.toStdString() << std::endl << "#" << std::endl;
674
        cv::Mat Tr = cv::Mat::eye(4, 4, CV_32F);
674
        cv::Mat Tr = cv::Mat::eye(4, 4, CV_32F);
675
        cv::Mat(pointCloudData[i].R.t()).copyTo(Tr.colRange(0, 3).rowRange(0, 3));
675
        cv::Mat(pointCloudData[i].R.t()).copyTo(Tr.colRange(0, 3).rowRange(0, 3));
676
        cv::Mat(-pointCloudData[i].R.t()*pointCloudData[i].T).copyTo(Tr.col(3).rowRange(0, 3));
676
        cv::Mat(-pointCloudData[i].R.t()*pointCloudData[i].T).copyTo(Tr.col(3).rowRange(0, 3));
677
        for(int j=0; j<Tr.rows; j++){
677
        for(int j=0; j<Tr.rows; j++){
678
            for(int k=0; k<Tr.cols; k++){
678
            for(int k=0; k<Tr.cols; k++){
679
                s << Tr.at<float>(j,k) << " ";
679
                s << Tr.at<float>(j,k) << " ";
680
            }
680
            }
681
            s << std::endl;
681
            s << std::endl;
682
        }
682
        }
683
        }
683
        }
684
    }
684
    }
685
    s << "0" << std::flush;
685
    s << "0" << std::flush;
686
    s.close();
686
    s.close();
687
}
687
}
688
 
688
 
689
void SMScanner::on_pointCloudsListWidget_itemChanged(QListWidgetItem *item){
689
void SMScanner::on_pointCloudsListWidget_itemChanged(QListWidgetItem *item){
690
 
690
 
691
    int id = item->data(Qt::UserRole).toInt();
691
    int id = item->data(Qt::UserRole).toInt();
692
    assert((int)pointCloudData.size()>id);
692
    assert((int)pointCloudData.size()>id);
693
 
693
 
694
    if(item->checkState() == Qt::Checked){
694
    if(item->checkState() == Qt::Checked){
695
        id = std::min(int(pointCloudData.size())-1,std::max(0,id));// clamp range
695
        id = std::min(int(pointCloudData.size())-1,std::max(0,id));// clamp range
696
        ui->pointCloudWidget->addPointCloud(pointCloudData[id], id);
696
        ui->pointCloudWidget->addPointCloud(pointCloudData[id], id);
697
    }
697
    }
698
    else
698
    else
699
        ui->pointCloudWidget->removePointCloud(id);
699
        ui->pointCloudWidget->removePointCloud(id);
700
 
700
 
701
}
701
}
702
 
702
 
703
void SMScanner::on_actionExport_Parameters_triggered(){
703
void SMScanner::on_actionExport_Parameters_triggered(){
704
 
704
 
705
    QString fileName = QFileDialog::getSaveFileName(this, "Export calibration parameters", QString(), "*.xml");
705
    QString fileName = QFileDialog::getSaveFileName(this, "Export calibration parameters", QString(), "*.xml");
706
    QFileInfo info(fileName);
706
    QFileInfo info(fileName);
707
    QString type = info.suffix();
707
    QString type = info.suffix();
708
    if(type == ""){
708
    if(type == ""){
709
        fileName.append(".xml");
709
        fileName.append(".xml");
710
    }
710
    }
711
 
711
 
712
    SMCalibrationParameters calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
712
    SMCalibrationParameters calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
713
    calibration.exportToXML(fileName);
713
    calibration.exportToXML(fileName);
714
}
714
}
715
 
715
 
716
void SMScanner::on_actionClear_Sequences_triggered(){
716
void SMScanner::on_actionClear_Sequences_triggered(){
717
 
717
 
718
    int res = QMessageBox::question(this, "Clear Captured Sequences", "Clear all captured data?", QMessageBox::Ok, QMessageBox::Cancel);
718
    int res = QMessageBox::question(this, "Clear Captured Sequences", "Clear all captured data?", QMessageBox::Ok, QMessageBox::Cancel);
719
 
719
 
720
    if(res == QMessageBox::Ok){
720
    if(res == QMessageBox::Ok){
721
        captureData.clear();
721
        captureData.clear();
722
        ui->captureTreeWidget->clear();
722
        ui->captureTreeWidget->clear();
723
        lastCaptureId=-1;
723
        lastCaptureId=-1;
724
    }
724
    }
725
}
725
}
726
 
726
 
727
 
727
 
728
 
728
 
729
void SMScanner::on_actionImport_Parameters_triggered(){
729
void SMScanner::on_actionImport_Parameters_triggered(){
730
 
730
 
731
    QString fileName = QFileDialog::getOpenFileName(this, "Import calibration parameters", QString(), "*.xml");
731
    QString fileName = QFileDialog::getOpenFileName(this, "Import calibration parameters", QString(), "*.xml");
732
    QFileInfo info(fileName);
732
    QFileInfo info(fileName);
733
    QString type = info.suffix();
733
    QString type = info.suffix();
734
    if(type != "xml"){
734
    if(type != "xml"){
735
        std::cerr << "Error: calibration parameters must be in .xml file." << std::endl;
735
        std::cerr << "Error: calibration parameters must be in .xml file." << std::endl;
736
        return;
736
        return;
737
    }
737
    }
738
 
738
 
739
    SMCalibrationParameters cal;
739
    SMCalibrationParameters cal;
740
    cal.importFromXML(fileName);
740
    cal.importFromXML(fileName);
741
 
741
 
742
    settings.setValue("calibration/parameters",  QVariant::fromValue(cal));
742
    settings.setValue("calibration/parameters",  QVariant::fromValue(cal));
743
    ui->pointCloudWidget->updateCalibrationParameters();
743
    ui->pointCloudWidget->updateCalibrationParameters();
744
 
744
 
745
    std::cout << "Imported calibration parameters " << fileName.toStdString() << std::endl;
745
    std::cout << "Imported calibration parameters " << fileName.toStdString() << std::endl;
746
}
746
}
747
 
747
 
748
void SMScanner::on_actionImport_Sets_triggered(){
748
void SMScanner::on_actionImport_Sets_triggered(){
749
 
749
 
750
    QString dirName = QFileDialog::getExistingDirectory(this, "Import calibration sets", QString());
750
    QString dirName = QFileDialog::getExistingDirectory(this, "Import calibration sets", QString());
751
 
751
 
752
    QDir dir(dirName);
752
    QDir dir(dirName);
753
    QStringList fileNames0 = dir.entryList(QStringList("frame0_*.png"));
753
    QStringList fileNames0 = dir.entryList(QStringList("frame0_*.png"));
754
    QStringList fileNames1 = dir.entryList(QStringList("frame1_*.png"));
754
    QStringList fileNames1 = dir.entryList(QStringList("frame1_*.png"));
755
 
755
 
756
    if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
756
    if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
757
        std::cerr << "Error: could not load calibration sets. Directory must contain .png files for both cameras." << std::endl;
757
        std::cerr << "Error: could not load calibration sets. Directory must contain .png files for both cameras." << std::endl;
758
        return;
758
        return;
759
    }
759
    }
760
 
760
 
761
    calibrationData.clear();
761
    calibrationData.clear();
762
    ui->calibrationListWidget->clear();
762
    ui->calibrationListWidget->clear();
763
 
763
 
764
    QProgressDialog progressDialog("Importing Sets...", "Cancel", 0, 100, this);
764
    QProgressDialog progressDialog("Importing Sets...", "Cancel", 0, 100, this);
765
    progressDialog.setWindowModality(Qt::WindowModal);
765
    progressDialog.setWindowModality(Qt::WindowModal);
766
    progressDialog.setMinimumDuration(1000);
766
    progressDialog.setMinimumDuration(1000);
767
 
767
 
768
    size_t nSets = fileNames0.size();
768
    size_t nSets = fileNames0.size();
769
    for(unsigned int i=0; i<nSets; i++){
769
    for(unsigned int i=0; i<nSets; i++){
770
 
770
 
771
        progressDialog.setValue(100.0*i/nSets);
771
        progressDialog.setValue(100.0*i/nSets);
772
 
772
 
773
        SMCalibrationSet calibrationSet;
773
        SMCalibrationSet calibrationSet;
774
 
774
 
775
        QString fileName0 = QString("%1/frame0_%2.png").arg(dirName).arg(i);
775
        QString fileName0 = QString("%1/frame0_%2.png").arg(dirName).arg(i);
776
        cv::Mat frame0BGR = cv::imread(fileName0.toStdString());
776
        cv::Mat frame0BGR = cv::imread(fileName0.toStdString());
777
        cvtools::cvtColorBGRToBayerBG(frame0BGR, calibrationSet.frame0);
777
        cvtools::cvtColorBGRToBayerBG(frame0BGR, calibrationSet.frame0);
778
 
778
 
779
        QString fileName1 = QString("%1/frame1_%2.png").arg(dirName).arg(i);
779
        QString fileName1 = QString("%1/frame1_%2.png").arg(dirName).arg(i);
780
        cv::Mat frame1BGR = cv::imread(fileName1.toStdString());
780
        cv::Mat frame1BGR = cv::imread(fileName1.toStdString());
781
        cvtools::cvtColorBGRToBayerBG(frame1BGR, calibrationSet.frame1);
781
        cvtools::cvtColorBGRToBayerBG(frame1BGR, calibrationSet.frame1);
782
 
782
 
783
//        int id = ui->calibrationListWidget->count();
783
//        int id = ui->calibrationListWidget->count();
784
//        calibrationSet.id = id;
784
//        calibrationSet.id = id;
785
 
785
 
786
//        calibrationData.push_back(calibrationSet);
786
//        calibrationData.push_back(calibrationSet);
787
 
787
 
788
//        // Add identifier to list
788
//        // Add identifier to list
789
//        QListWidgetItem* item = new QListWidgetItem(QString("Calibration Set %1 -- %2 deg").arg(id).arg(calibrationSet.rotationAngle), ui->calibrationListWidget);
789
//        QListWidgetItem* item = new QListWidgetItem(QString("Calibration Set %1 -- %2 deg").arg(id).arg(calibrationSet.rotationAngle), ui->calibrationListWidget);
790
//        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
790
//        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
791
//        item->setCheckState(Qt::Checked);
791
//        item->setCheckState(Qt::Checked);
792
//        ui->calibrationListWidget->addItem(item);
792
//        ui->calibrationListWidget->addItem(item);
793
 
793
 
794
        this->onReceiveCalibrationSet(calibrationSet);
794
        this->onReceiveCalibrationSet(calibrationSet);
795
 
795
 
796
        QCoreApplication::processEvents();
796
        QCoreApplication::processEvents();
797
        if(progressDialog.wasCanceled())
797
        if(progressDialog.wasCanceled())
798
            return;
798
            return;
799
    }
799
    }
800
 
800
 
801
    // Set enabled checkmark
801
    // Set enabled checkmark
802
    if(calibrationData.size() >= 2){
802
    if(calibrationData.size() >= 2){
803
        ui->calibrateCamerasButton->setEnabled(true);
803
        ui->calibrateCamerasButton->setEnabled(true);
804
        ui->calibrateRotationStageButton->setEnabled(true);
804
        ui->calibrateRotationStageButton->setEnabled(true);
805
    }
805
    }
806
}
806
}
807
 
807
 
808
void SMScanner::on_actionImport_Sequences_triggered(){
808
void SMScanner::on_actionImport_Sequences_triggered(){
809
 
809
 
810
    // NOTE: we do not know which algorithm was used!!!
810
    // NOTE: we do not know which algorithm was used!!!
811
 
811
 
812
    QString dirName = QFileDialog::getExistingDirectory(this, "Import captured sequences", QString());
812
    QString dirName = QFileDialog::getExistingDirectory(this, "Import captured sequences", QString());
813
 
813
 
814
    QDir dir(dirName);
814
    QDir dir(dirName);
815
    QStringList sequenceDirNames = dir.entryList(QStringList("sequence_*"));
815
    QStringList sequenceDirNames = dir.entryList(QStringList("sequence_*"));
816
 
816
 
817
    if(sequenceDirNames.empty()){
817
    if(sequenceDirNames.empty()){
818
        std::cerr << "Error: could not find sequences." << std::endl;
818
        std::cerr << "Error: could not find sequences." << std::endl;
819
        return;
819
        return;
820
    }
820
    }
821
 
821
 
822
    captureData.clear();
822
    captureData.clear();
823
    ui->captureTreeWidget->clear();
823
    ui->captureTreeWidget->clear();
824
    lastCaptureId=-1;
824
    lastCaptureId=-1;
825
 
825
 
826
    QProgressDialog progressDialog("Importing Sequences...", "Cancel", 0, 100, this);
826
    QProgressDialog progressDialog("Importing Sequences...", "Cancel", 0, 100, this);
827
    progressDialog.setWindowModality(Qt::WindowModal);
827
    progressDialog.setWindowModality(Qt::WindowModal);
828
    progressDialog.setMinimumDuration(1000);
828
    progressDialog.setMinimumDuration(1000);
829
 
829
 
830
    for(int s=0; s<sequenceDirNames.count(); s++){
830
    for(int s=0; s<sequenceDirNames.count(); s++){
831
 
831
 
832
        QDir sequenceDir(QString("%1/%2").arg(dirName).arg(sequenceDirNames.at(s)));
832
        QDir sequenceDir(QString("%1/%2").arg(dirName).arg(sequenceDirNames.at(s)));
833
 
833
 
834
        QStringList fileNames0 = sequenceDir.entryList(QStringList("frames0_*.png"));
834
        QStringList fileNames0 = sequenceDir.entryList(QStringList("frames0_*.png"));
835
        QStringList fileNames1 = sequenceDir.entryList(QStringList("frames1_*.png"));
835
        QStringList fileNames1 = sequenceDir.entryList(QStringList("frames1_*.png"));
836
 
836
 
837
        if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
837
        if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
838
            std::cerr << "Error: could not load sequence. Directory must contain .png files for both cameras." << std::endl;
838
            std::cerr << "Error: could not load sequence. Directory must contain .png files for both cameras." << std::endl;
839
            return;
839
            return;
840
        }
840
        }
841
 
841
 
842
        int nFrames = fileNames0.size();
842
        int nFrames = fileNames0.size();
843
 
843
 
844
        SMFrameSequence sequence;
844
        SMFrameSequence sequence;
845
 
845
 
846
        for(int f=0; f<nFrames; f++){
846
        for(int f=0; f<nFrames; f++){
847
 
847
 
848
            progressDialog.setValue(100.0*s/sequenceDirNames.count() + 100.0/sequenceDirNames.count()*f/nFrames);
848
            progressDialog.setValue(100.0*s/sequenceDirNames.count() + 100.0/sequenceDirNames.count()*f/nFrames);
849
 
849
 
850
            cv::Mat frame0BGR, frame0BayerBG, frame1BGR, frame1BayerBG;
850
            cv::Mat frame0BGR, frame0BayerBG, frame1BGR, frame1BayerBG;
851
 
851
 
852
            QString fileName0 = QString("%1/%2/frames0_%3.png").arg(dirName).arg(sequenceDirNames.at(s)).arg(f);
852
            QString fileName0 = QString("%1/%2/frames0_%3.png").arg(dirName).arg(sequenceDirNames.at(s)).arg(f);
853
            frame0BGR = cv::imread(fileName0.toStdString());
853
            frame0BGR = cv::imread(fileName0.toStdString());
854
            cvtools::cvtColorBGRToBayerBG(frame0BGR, frame0BayerBG);
854
            cvtools::cvtColorBGRToBayerBG(frame0BGR, frame0BayerBG);
855
 
855
 
856
            QString fileName1 = QString("%1/%2/frames1_%3.png").arg(dirName).arg(sequenceDirNames.at(s)).arg(f);
856
            QString fileName1 = QString("%1/%2/frames1_%3.png").arg(dirName).arg(sequenceDirNames.at(s)).arg(f);
857
            frame1BGR = cv::imread(fileName1.toStdString());
857
            frame1BGR = cv::imread(fileName1.toStdString());
858
            cvtools::cvtColorBGRToBayerBG(frame1BGR, frame1BayerBG);
858
            cvtools::cvtColorBGRToBayerBG(frame1BGR, frame1BayerBG);
859
 
859
 
860
            sequence.frames0.push_back(frame0BayerBG);
860
            sequence.frames0.push_back(frame0BayerBG);
861
            sequence.frames1.push_back(frame1BayerBG);
861
            sequence.frames1.push_back(frame1BayerBG);
862
 
862
 
863
            QCoreApplication::processEvents();
863
            QCoreApplication::processEvents();
864
            if(progressDialog.wasCanceled())
864
            if(progressDialog.wasCanceled())
865
                return;
865
                return;
866
        }
866
        }
867
 
867
 
868
        // Assign whatever algorithm is configured
868
        // Assign whatever algorithm is configured
869
        sequence.codec = settings.value("algorithm").toString();
869
        sequence.codec = settings.value("algorithm").toString();
870
        sequence.rotationAngle = 0;
870
        sequence.rotationAngle = 0;
871
        sequence.reconstructed = false;
871
        sequence.reconstructed = false;
872
        sequence.shutters.push_back(16.6666);
872
        sequence.shutters.push_back(16.6666);
873
 
873
 
874
        onReceiveFrameSequence(sequence);
874
        onReceiveFrameSequence(sequence);
875
    }
875
    }
876
 
876
 
877
}
877
}
878
 
878
 
879
void SMScanner::on_actionClear_Point_Clouds_triggered(){
879
void SMScanner::on_actionClear_Point_Clouds_triggered(){
880
 
880
 
881
    int res = QMessageBox::question(this, "Clear Reconstructed Point Clouds", "Clear all reconstructed point clouds?", QMessageBox::Ok, QMessageBox::Cancel);
881
    int res = QMessageBox::question(this, "Clear Reconstructed Point Clouds", "Clear all reconstructed point clouds?", QMessageBox::Ok, QMessageBox::Cancel);
882
 
882
 
883
    if(res == QMessageBox::Ok){
883
    if(res == QMessageBox::Ok){
884
 
884
 
885
        pointCloudData.clear();
885
        pointCloudData.clear();
886
        ui->pointCloudsListWidget->clear();
886
        ui->pointCloudsListWidget->clear();
887
        ui->pointCloudWidget->removeAllPointClouds();
887
        ui->pointCloudWidget->removeAllPointClouds();
888
 
888
 
889
        for(unsigned int i=0; i<captureData.size(); i++)
889
        for(unsigned int i=0; i<captureData.size(); i++)
890
            captureData[i].reconstructed = false;
890
            captureData[i].reconstructed = false;
891
    }
891
    }
892
}
892
}
893
 
893
 
894
 
894
 
895
void SMScanner::on_actionProject_Focusing_Pattern_triggered(){
895
void SMScanner::on_actionProject_Focusing_Pattern_triggered(){
896
 
896
 
897
    bool projectFocusingPattern = ui->actionProject_Focusing_Pattern->isChecked();
897
    bool projectFocusingPattern = ui->actionProject_Focusing_Pattern->isChecked();
898
    QMetaObject::invokeMethod(captureWorker, "setProjectFocusingPattern", Q_ARG(bool, projectFocusingPattern));
898
    QMetaObject::invokeMethod(captureWorker, "setProjectFocusingPattern", Q_ARG(bool, projectFocusingPattern));
899
 
899
 
900
}
900
}
901
 
901
 
902
void SMScanner::on_calibrateRotationStageButton_clicked(){
902
void SMScanner::on_calibrateRotationStageButton_clicked(){
903
 
903
 
904
    // disable ui elements
904
    // disable ui elements
905
    ui->calibrateCamerasButton->setEnabled(false);
905
    ui->calibrateCamerasButton->setEnabled(false);
906
    ui->calibrateRotationStageButton->setEnabled(false);
906
    ui->calibrateRotationStageButton->setEnabled(false);
907
    ui->calibrationFrame->setEnabled(false);
907
    ui->calibrationFrame->setEnabled(false);
908
 
908
 
909
    // if none items are selected, we will use all available items
909
    // if none items are selected, we will use all available items
910
    if(ui->calibrationListWidget->selectedItems().empty())
910
    if(ui->calibrationListWidget->selectedItems().empty())
911
        ui->calibrationListWidget->selectAll();
911
        ui->calibrationListWidget->selectAll();
912
 
912
 
913
    // set selected flags
913
    // set selected flags
914
    for(unsigned int i=0; i<calibrationData.size(); i++){
914
    for(unsigned int i=0; i<calibrationData.size(); i++){
915
        if(ui->calibrationListWidget->item(i)->isSelected())
915
        if(ui->calibrationListWidget->item(i)->isSelected())
916
            calibrationData[i].selected = true;
916
            calibrationData[i].selected = true;
917
        else
917
        else
918
            calibrationData[i].selected = false;
918
            calibrationData[i].selected = false;
919
    }
919
    }
920
 
920
 
921
    logDialog.show();
921
    logDialog.show();
922
    logDialog.activateWindow();
922
    logDialog.activateWindow();
923
 
923
 
924
    // start calibration
924
    // start calibration
925
    QMetaObject::invokeMethod(calibrationWorker, "rotationStageCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
925
    QMetaObject::invokeMethod(calibrationWorker, "rotationStageCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
926
 
926
 
927
}
927
}
928
 
928
 
929
 
929
 
930
void SMScanner::on_actionView_Log_Messages_triggered()
930
void SMScanner::on_actionView_Log_Messages_triggered()
931
{
931
{
932
     logDialog.show();
932
     logDialog.show();
933
     logDialog.activateWindow();
933
     logDialog.activateWindow();
934
}
934
}
935
 
935
 
936
void SMScanner::on_tabWidget_currentChanged(int index)
936
void SMScanner::on_tabWidget_currentChanged(int index)
937
{
937
{
938
    if(index==0){
938
    if(index==0){
939
        ui->calibrationCamera0Widget->fitImage();
939
        ui->calibrationCamera0Widget->fitImage();
940
        ui->calibrationCamera1Widget->fitImage();
940
        ui->calibrationCamera1Widget->fitImage();
941
    }
941
    }
942
    if(index==1){
942
    if(index==1){
943
        ui->captureCamera0Widget->fitImage();
943
        ui->captureCamera0Widget->fitImage();
944
        ui->captureCamera1Widget->fitImage();
944
        ui->captureCamera1Widget->fitImage();
945
    }
945
    }
946
}
946
}
947
 
947
 
948
void SMScanner::on_calibrationListWidget_currentRowChanged(int currentRow)
948
void SMScanner::on_calibrationListWidget_currentRowChanged(int currentRow)
949
{
949
{
950
    std::cout << "Current row: " << currentRow << std::endl;
950
    std::cout << "Current row: " << currentRow << std::endl;
951
    on_calibrationListWidget_itemSelectionChanged();
951
    on_calibrationListWidget_itemSelectionChanged();
952
}
952
}
953
 
953
 
954
void SMScanner::on_actionEdit_Configuration_File_triggered()
954
void SMScanner::on_actionEdit_Configuration_File_triggered()
955
{
955
{
956
    QDesktopServices::openUrl(QUrl::fromLocalFile(settings.fileName()));
956
    QDesktopServices::openUrl(QUrl::fromLocalFile(settings.fileName()));
957
}
957
}
958
 
958