Subversion Repositories seema-scanner

Rev

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

Rev 247 Rev 249
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
 
-
 
239
    if(settings.value("calibration/method").toString() == "Charuco")
-
 
240
        onReceiveCheckerboardResult(calibrationSet.id, calibrationSet);
-
 
241
    else
238
    QMetaObject::invokeMethod(calibrationWorker, "checkerboardDetection", Q_ARG(SMCalibrationSet, calibrationSet));
242
        QMetaObject::invokeMethod(calibrationWorker, "checkerboardDetection", Q_ARG(SMCalibrationSet, calibrationSet));
239
 
243
 
240
}
244
}
241
 
245
 
242
void SMScanner::on_calibrateCamerasButton_clicked(){
246
void SMScanner::on_calibrateCamerasButton_clicked(){
243
 
247
 
244
    // disable ui elements
248
    // disable ui elements
245
    ui->calibrateCamerasButton->setEnabled(false);
249
    ui->calibrateCamerasButton->setEnabled(false);
246
    ui->calibrateRotationStageButton->setEnabled(false);
250
    ui->calibrateRotationStageButton->setEnabled(false);
247
    ui->calibrationFrame->setEnabled(false);
251
    ui->calibrationFrame->setEnabled(false);
248
 
252
 
249
    // if none items are selected, we will use all available items
253
    // if none items are selected, we will use all available items
250
    if(ui->calibrationListWidget->selectedItems().empty())
254
    if(ui->calibrationListWidget->selectedItems().empty())
251
        ui->calibrationListWidget->selectAll();
255
        ui->calibrationListWidget->selectAll();
252
 
256
 
253
    // set selected flags
257
    // set selected flags
254
    for(unsigned int i=0; i<calibrationData.size(); i++){
258
    for(unsigned int i=0; i<calibrationData.size(); i++){
255
        if(ui->calibrationListWidget->item(i)->isSelected())
259
        if(ui->calibrationListWidget->item(i)->isSelected())
256
            calibrationData[i].selected = true;
260
            calibrationData[i].selected = true;
257
        else
261
        else
258
            calibrationData[i].selected = false;
262
            calibrationData[i].selected = false;
259
    }
263
    }
260
 
264
 
261
    logDialog.show();
265
    logDialog.show();
262
 
266
 
263
    // start calibration
267
    // start calibration
-
 
268
    QSettings settings;
-
 
269
    if(settings.value("calibration/method").toString() == "Charuco")
-
 
270
        QMetaObject::invokeMethod(calibrationWorker, "cameraCalibrationCharuco", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
-
 
271
    else
264
    QMetaObject::invokeMethod(calibrationWorker, "cameraCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
272
        QMetaObject::invokeMethod(calibrationWorker, "cameraCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
-
 
273
 
265
 
274
 
266
}
275
}
267
 
276
 
268
void SMScanner::onReceiveCheckerboardResult(int idx, SMCalibrationSet calibrationSet){
277
void SMScanner::onReceiveCheckerboardResult(int idx, SMCalibrationSet calibrationSet){
269
 
278
 
270
    int id = ui->calibrationListWidget->count();
279
    int id = ui->calibrationListWidget->count();
271
    calibrationSet.id = id;
280
    calibrationSet.id = id;
272
 
281
 
273
    calibrationData.push_back(calibrationSet);
282
    calibrationData.push_back(calibrationSet);
274
 
283
 
275
    // Add identifier to list
284
    // Add identifier to list
276
    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);
277
    ui->calibrationListWidget->addItem(item);
286
    ui->calibrationListWidget->addItem(item);
278
    item->setSelected(true);
287
    item->setSelected(true);
279
    ui->calibrationListWidget->setCurrentItem(item);
288
    ui->calibrationListWidget->setCurrentItem(item);
280
 
289
 
281
    // Enable calibration button
290
    // Enable calibration button
282
    if(calibrationData.size() >= 2){
291
    if(calibrationData.size() >= 2){
283
        ui->calibrateCamerasButton->setEnabled(true);
292
        ui->calibrateCamerasButton->setEnabled(true);
284
        ui->calibrateRotationStageButton->setEnabled(true);
293
        ui->calibrateRotationStageButton->setEnabled(true);
285
    }
294
    }
286
 
295
 
287
    //ui->calibrationListWidget->setCurrentRow(idx);
296
    //ui->calibrationListWidget->setCurrentRow(idx);
288
}
297
}
289
 
298
 
290
void SMScanner::onReceiveCalibrationDone(){
299
void SMScanner::onReceiveCalibrationDone(){
291
 
300
 
292
    std::cout << "Calibration done!" << std::endl;
301
    std::cout << "Calibration done!" << std::endl;
293
    ui->calibrateCamerasButton->setEnabled(true);
302
    ui->calibrateCamerasButton->setEnabled(true);
294
    ui->calibrateRotationStageButton->setEnabled(true);
303
    ui->calibrateRotationStageButton->setEnabled(true);
295
    ui->calibrationFrame->setEnabled(true);
304
    ui->calibrationFrame->setEnabled(true);
296
 
305
 
297
}
306
}
298
 
307
 
299
void SMScanner::on_calibrationListWidget_itemSelectionChanged(){
308
void SMScanner::on_calibrationListWidget_itemSelectionChanged(){
300
 
309
 
301
    // if selection is just cleared
310
    // if selection is just cleared
302
    if(ui->calibrationListWidget->selectedItems().empty())
311
    if(ui->calibrationListWidget->selectedItems().empty())
303
        return;
312
        return;
304
 
313
 
305
    calibrationReviewMode = true;
314
    calibrationReviewMode = true;
306
    ui->singleCalibrationButton->setText("Live View");
315
    ui->singleCalibrationButton->setText("Live View");
307
    ui->batchCalibrationButton->setText("Live View");
316
    ui->batchCalibrationButton->setText("Live View");
308
 
317
 
309
    int currentRow = ui->calibrationListWidget->currentRow();
318
    int currentRow = ui->calibrationListWidget->currentRow();
310
    if(currentRow < 0)
319
    if(currentRow < 0)
311
        return;
320
        return;
312
 
321
 
313
    assert(currentRow < (int)calibrationData.size());
322
    assert(currentRow < (int)calibrationData.size());
314
 
323
 
315
    // if checkerboard results are available, show them, otherwise show the frame
324
    // if checkerboard results are available, show them, otherwise show the frame
316
    if(calibrationData[currentRow].frame0Result.empty())
325
    if(calibrationData[currentRow].frame0Result.empty())
317
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0);
326
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0);
318
    else
327
    else
319
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0Result);
328
        ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0Result);
320
 
329
 
321
    if(calibrationData[currentRow].frame1Result.empty())
330
    if(calibrationData[currentRow].frame1Result.empty())
322
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1);
331
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1);
323
    else
332
    else
324
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1Result);
333
        ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1Result);
325
 
334
 
326
}
335
}
327
 
336
 
328
void SMScanner::on_captureRotationDial_sliderReleased(){
337
void SMScanner::on_captureRotationDial_sliderReleased(){
329
 
338
 
330
    float angle = ui->captureRotationDial->value();
339
    float angle = ui->captureRotationDial->value();
331
    std::cout << "Rotation stage target: " << angle << std::endl;
340
    std::cout << "Rotation stage target: " << angle << std::endl;
332
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
341
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
333
 
342
 
334
    ui->captureRotationSpinBox->setValue(ui->captureRotationDial->value());
343
    ui->captureRotationSpinBox->setValue(ui->captureRotationDial->value());
335
 
344
 
336
    ui->calibrationRotationDial->setValue(ui->captureRotationDial->value());
345
    ui->calibrationRotationDial->setValue(ui->captureRotationDial->value());
337
    ui->calibrationRotationSpinBox->setValue(ui->captureRotationDial->value());
346
    ui->calibrationRotationSpinBox->setValue(ui->captureRotationDial->value());
338
}
347
}
339
 
348
 
340
void SMScanner::on_captureRotationSpinBox_editingFinished(){
349
void SMScanner::on_captureRotationSpinBox_editingFinished(){
341
 
350
 
342
    float angle = ui->captureRotationSpinBox->value();
351
    float angle = ui->captureRotationSpinBox->value();
343
    std::cout << "Rotation stage target: " << angle << std::endl;
352
    std::cout << "Rotation stage target: " << angle << std::endl;
344
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
353
    QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
345
 
354
 
346
    ui->captureRotationDial->setValue(angle);
355
    ui->captureRotationDial->setValue(angle);
347
 
356
 
348
    ui->calibrationRotationSpinBox->setValue(angle);
357
    ui->calibrationRotationSpinBox->setValue(angle);
349
    ui->calibrationRotationDial->setValue(angle);
358
    ui->calibrationRotationDial->setValue(angle);
350
}
359
}
351
 
360
 
352
void SMScanner::on_singleCaptureButton_clicked(){
361
void SMScanner::on_singleCaptureButton_clicked(){
353
 
362
 
354
    // If in review mode, go back to aquisition mode
363
    // If in review mode, go back to aquisition mode
355
    if(captureReviewMode){
364
    if(captureReviewMode){
356
        ui->captureTreeWidget->clearSelection();
365
        ui->captureTreeWidget->clearSelection();
357
        ui->singleCaptureButton->setText("Single Aquisition");
366
        ui->singleCaptureButton->setText("Single Aquisition");
358
        ui->batchCaptureButton->setText("Batch Aquisition");
367
        ui->batchCaptureButton->setText("Batch Aquisition");
359
        captureReviewMode = false;
368
        captureReviewMode = false;
360
        return;
369
        return;
361
    }
370
    }
362
 
371
 
363
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequence", Q_ARG(float, -1.0));
372
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequence", Q_ARG(float, -1.0));
364
 
373
 
365
}
374
}
366
 
375
 
367
 
376
 
368
void SMScanner::on_batchCaptureButton_clicked(){
377
void SMScanner::on_batchCaptureButton_clicked(){
369
 
378
 
370
    // If in review mode, go back to aquisition mode
379
    // If in review mode, go back to aquisition mode
371
    if(captureReviewMode){
380
    if(captureReviewMode){
372
        ui->captureTreeWidget->clearSelection();
381
        ui->captureTreeWidget->clearSelection();
373
        ui->singleCaptureButton->setText("Single Aquisition");
382
        ui->singleCaptureButton->setText("Single Aquisition");
374
        ui->batchCaptureButton->setText("Batch Aquisition");
383
        ui->batchCaptureButton->setText("Batch Aquisition");
375
        captureReviewMode = false;
384
        captureReviewMode = false;
376
        return;
385
        return;
377
    }
386
    }
378
 
387
 
379
    // Construct vector of angles
388
    // Construct vector of angles
380
    int angleStart = ui->captureBatchStartSpinBox->value();
389
    int angleStart = ui->captureBatchStartSpinBox->value();
381
    int angleEnd = ui->captureBatchEndSpinBox->value();
390
    int angleEnd = ui->captureBatchEndSpinBox->value();
382
    int angleStep = ui->captureBatchStepSpinBox->value();
391
    int angleStep = ui->captureBatchStepSpinBox->value();
383
 
392
 
384
    if(angleStart > angleEnd)
393
    if(angleStart > angleEnd)
385
        angleEnd += 360;
394
        angleEnd += 360;
386
 
395
 
387
    std::vector<float> angles;
396
    std::vector<float> angles;
388
    for(int i=angleStart; i<=angleEnd; i+=angleStep)
397
    for(int i=angleStart; i<=angleEnd; i+=angleStep)
389
        angles.push_back(i % 360);
398
        angles.push_back(i % 360);
390
 
399
 
391
    std::cout << "Aquiring sequences at ";
400
    std::cout << "Aquiring sequences at ";
392
    for(unsigned int i=0; i<angles.size(); i++)
401
    for(unsigned int i=0; i<angles.size(); i++)
393
        std::cout << angles[i] << " ";
402
        std::cout << angles[i] << " ";
394
    std::cout << " degrees" <<std::endl;
403
    std::cout << " degrees" <<std::endl;
395
 
404
 
396
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequences", Q_ARG(std::vector<float>, angles));
405
    QMetaObject::invokeMethod(captureWorker, "acquireFrameSequences", Q_ARG(std::vector<float>, angles));
397
}
406
}
398
 
407
 
399
void SMScanner::onReceiveFrameSequence(SMFrameSequence frameSequence){
408
void SMScanner::onReceiveFrameSequence(SMFrameSequence frameSequence){
400
 
409
 
401
    frameSequence.id = ++lastCaptureId;
410
    frameSequence.id = ++lastCaptureId;
402
 
411
 
403
    // Add identifier to list
412
    // Add identifier to list
404
    QTreeWidgetItem* item = new QTreeWidgetItem(ui->captureTreeWidget);
413
    QTreeWidgetItem* item = new QTreeWidgetItem(ui->captureTreeWidget);
405
    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));
406
    //item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
415
    //item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
407
    item->setData(0, Qt::UserRole, QPoint(frameSequence.id, -1));
416
    item->setData(0, Qt::UserRole, QPoint(frameSequence.id, -1));
408
    //item->setCheckState(0, Qt::Checked);
417
    //item->setCheckState(0, Qt::Checked);
409
    //ui->captureTreeWidget->addItem(item);
418
    //ui->captureTreeWidget->addItem(item);
410
 
419
 
411
    for(unsigned int i=0; i<frameSequence.frames0.size(); i++){
420
    for(unsigned int i=0; i<frameSequence.frames0.size(); i++){
412
        QTreeWidgetItem* subItem = new QTreeWidgetItem(item);
421
        QTreeWidgetItem* subItem = new QTreeWidgetItem(item);
413
        subItem->setText(0, QString("frames %1").arg(i));
422
        subItem->setText(0, QString("frames %1").arg(i));
414
        subItem->setData(0, Qt::UserRole, QPoint(frameSequence.id, i));
423
        subItem->setData(0, Qt::UserRole, QPoint(frameSequence.id, i));
415
    }
424
    }
416
 
425
 
417
    // Indicate that the current item is currently reconstructing
426
    // Indicate that the current item is currently reconstructing
418
    item->setTextColor(0, QColor(128, 128, 128));
427
    item->setTextColor(0, QColor(128, 128, 128));
419
    item->setIcon(0, QIcon::fromTheme("system-run"));
428
    item->setIcon(0, QIcon::fromTheme("system-run"));
420
 
429
 
421
    // Reconstruct the frame sequence
430
    // Reconstruct the frame sequence
422
    QMetaObject::invokeMethod(reconstructionWorker, "reconstructPointCloud", Q_ARG(SMFrameSequence, frameSequence));
431
    QMetaObject::invokeMethod(reconstructionWorker, "reconstructPointCloud", Q_ARG(SMFrameSequence, frameSequence));
423
    frameSequence.reconstructed = true;
432
    frameSequence.reconstructed = true;
424
 
433
 
425
    captureData.push_back(frameSequence);
434
    captureData.push_back(frameSequence);
426
 
435
 
427
}
436
}
428
 
437
 
429
void SMScanner::on_captureTreeWidget_itemSelectionChanged(){
438
void SMScanner::on_captureTreeWidget_itemSelectionChanged(){
430
 
439
 
431
    // if selection is just cleared
440
    // if selection is just cleared
432
    if(ui->captureTreeWidget->selectedItems().empty())
441
    if(ui->captureTreeWidget->selectedItems().empty())
433
        return;
442
        return;
434
 
443
 
435
    QTreeWidgetItem *item = ui->captureTreeWidget->currentItem();
444
    QTreeWidgetItem *item = ui->captureTreeWidget->currentItem();
436
 
445
 
437
    captureReviewMode = true;
446
    captureReviewMode = true;
438
 
447
 
439
    ui->singleCaptureButton->setText("Live View");
448
    ui->singleCaptureButton->setText("Live View");
440
    ui->batchCaptureButton->setText("Live View");
449
    ui->batchCaptureButton->setText("Live View");
441
 
450
 
442
    QPoint idx = item->data(0, Qt::UserRole).toPoint();
451
    QPoint idx = item->data(0, Qt::UserRole).toPoint();
443
 
452
 
444
    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()
445
            && idx.y()<(int)captureData[idx.x()].frames0.size()
454
            && idx.y()<(int)captureData[idx.x()].frames0.size()
446
            && idx.y()<(int)captureData[idx.x()].frames1.size() ){
455
            && idx.y()<(int)captureData[idx.x()].frames1.size() ){
447
        // TODO idx.x() can & WILL be out of bounds
456
        // TODO idx.x() can & WILL be out of bounds
448
        ui->captureCamera0Widget->showImageCV(captureData[idx.x()].frames0[idx.y()]);
457
        ui->captureCamera0Widget->showImageCV(captureData[idx.x()].frames0[idx.y()]);
449
        ui->captureCamera1Widget->showImageCV(captureData[idx.x()].frames1[idx.y()]);
458
        ui->captureCamera1Widget->showImageCV(captureData[idx.x()].frames1[idx.y()]);
450
    }
459
    }
451
 
460
 
452
//     std::cout << "on_captureTreeWidget_itemSelectionChanged" << std::endl;
461
//     std::cout << "on_captureTreeWidget_itemSelectionChanged" << std::endl;
453
}
462
}
454
 
463
 
455
 
464
 
456
void SMScanner::onReceiveRotatedTo(float angle){
465
void SMScanner::onReceiveRotatedTo(float angle){
457
 
466
 
458
    // update ui with new position
467
    // update ui with new position
459
    ui->calibrationRotationDial->setValue(angle);
468
    ui->calibrationRotationDial->setValue(angle);
460
    ui->captureRotationDial->setValue(angle);
469
    ui->captureRotationDial->setValue(angle);
461
 
470
 
462
    ui->calibrationRotationSpinBox->setValue(angle);
471
    ui->calibrationRotationSpinBox->setValue(angle);
463
    ui->captureRotationSpinBox->setValue(angle);
472
    ui->captureRotationSpinBox->setValue(angle);
464
}
473
}
465
 
474
 
466
void SMScanner::on_actionExport_Sets_triggered(){
475
void SMScanner::on_actionExport_Sets_triggered(){
467
 
476
 
468
    QString dirName = QFileDialog::getExistingDirectory(this, "Export calibration sets", QString());
477
    QString dirName = QFileDialog::getExistingDirectory(this, "Export calibration sets", QString());
469
 
478
 
470
    QProgressDialog progressDialog("Exporting Sets...", "Cancel", 0, 100, this);
479
    QProgressDialog progressDialog("Exporting Sets...", "Cancel", 0, 100, this);
471
    progressDialog.setWindowModality(Qt::WindowModal);
480
    progressDialog.setWindowModality(Qt::WindowModal);
472
    progressDialog.setMinimumDuration(1000);
481
    progressDialog.setMinimumDuration(1000);
473
 
482
 
474
    progressDialog.show();
483
    progressDialog.show();
475
    cv::Mat frameBGR;
484
    cv::Mat frameBGR;
476
    for(unsigned int i=0; i<calibrationData.size(); i++){
485
    for(unsigned int i=0; i<calibrationData.size(); i++){
477
        QString fileName = QString("%1/frame0_%2.png").arg(dirName).arg(i);
486
        QString fileName = QString("%1/frame0_%2.png").arg(dirName).arg(i);
478
 
487
 
479
        progressDialog.setValue(100.0*i/calibrationData.size());
488
        progressDialog.setValue(100.0*i/calibrationData.size());
480
 
489
 
481
        // Convert to BGR
490
        // Convert to BGR
482
        if(calibrationData[i].frame0.channels() == 1)
491
        if(calibrationData[i].frame0.channels() == 1)
483
            cv::cvtColor(calibrationData[i].frame0, frameBGR, CV_BayerBG2BGR);
492
            cv::cvtColor(calibrationData[i].frame0, frameBGR, CV_BayerBG2BGR);
484
        else
493
        else
485
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
494
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
486
 
495
 
487
        cv::imwrite(fileName.toStdString(), frameBGR);
496
        cv::imwrite(fileName.toStdString(), frameBGR);
488
        fileName = QString("%1/frame1_%2.png").arg(dirName).arg(i);
497
        fileName = QString("%1/frame1_%2.png").arg(dirName).arg(i);
489
 
498
 
490
        // Convert to BGR
499
        // Convert to BGR
491
        if(calibrationData[i].frame1.channels() == 1)
500
        if(calibrationData[i].frame1.channels() == 1)
492
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_BayerBG2BGR);
501
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_BayerBG2BGR);
493
        else
502
        else
494
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
503
            cv::cvtColor(calibrationData[i].frame1, frameBGR, CV_RGB2BGR);
495
 
504
 
496
        cv::imwrite(fileName.toStdString(), frameBGR);
505
        cv::imwrite(fileName.toStdString(), frameBGR);
497
 
506
 
498
        // Necessary to prevent pileup of video frame signals
507
        // Necessary to prevent pileup of video frame signals
499
        QCoreApplication::processEvents();
508
        QCoreApplication::processEvents();
500
 
509
 
501
        if(progressDialog.wasCanceled())
510
        if(progressDialog.wasCanceled())
502
            return;
511
            return;
503
    }
512
    }
504
 
513
 
505
}
514
}
506
 
515
 
507
void SMScanner::on_actionExport_Sequences_triggered(){
516
void SMScanner::on_actionExport_Sequences_triggered(){
508
 
517
 
509
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
518
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
510
 
519
 
511
    QProgressDialog progressDialog("Exporting Sequences...", "Cancel", 0, 100, this);
520
    QProgressDialog progressDialog("Exporting Sequences...", "Cancel", 0, 100, this);
512
    progressDialog.setWindowModality(Qt::WindowModal);
521
    progressDialog.setWindowModality(Qt::WindowModal);
513
    progressDialog.setMinimumDuration(1000);
522
    progressDialog.setMinimumDuration(1000);
514
 
523
 
515
    progressDialog.show();
524
    progressDialog.show();
516
 
525
 
517
    cv::Mat frameBGR;
526
    cv::Mat frameBGR;
518
    for(unsigned int i=0; i<captureData.size(); i++){
527
    for(unsigned int i=0; i<captureData.size(); i++){
519
 
528
 
520
        QString format;
529
        QString format;
521
        if(captureData[i].frames0[0].depth() == CV_32F)
530
        if(captureData[i].frames0[0].depth() == CV_32F)
522
            format = "hdr";
531
            format = "hdr";
523
        else
532
        else
524
            format = "png";
533
            format = "png";
525
 
534
 
526
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
535
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
527
        if(!QDir().mkdir(seqDirName))
536
        if(!QDir().mkdir(seqDirName))
528
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
537
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
529
        for(unsigned int j=0; j<captureData[i].frames0.size(); j++){
538
        for(unsigned int j=0; j<captureData[i].frames0.size(); j++){
530
 
539
 
531
            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());
532
 
541
 
533
            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);
534
 
543
 
535
            // Convert Bayer to rgb (png needs BGR order)
544
            // Convert Bayer to rgb (png needs BGR order)
536
            if(captureData[i].frames0[j].type() == CV_8UC1)
545
            if(captureData[i].frames0[j].type() == CV_8UC1)
537
                cv::cvtColor(captureData[i].frames0[j], frameBGR, CV_BayerBG2BGR);
546
                cv::cvtColor(captureData[i].frames0[j], frameBGR, CV_BayerBG2BGR);
538
            else
547
            else
539
                cv::cvtColor(captureData[i].frames0[j], frameBGR, CV_RGB2BGR);
548
                cv::cvtColor(captureData[i].frames0[j], frameBGR, CV_RGB2BGR);
540
 
549
 
541
            cv::imwrite(fileName0.toStdString(), frameBGR);
550
            cv::imwrite(fileName0.toStdString(), frameBGR);
542
 
551
 
543
            // Necessary to prevent pileup of video frame signals
552
            // Necessary to prevent pileup of video frame signals
544
            QCoreApplication::processEvents();
553
            QCoreApplication::processEvents();
545
 
554
 
546
            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);
547
 
556
 
548
            // Convert Bayer to rgb (png needs BGR order)
557
            // Convert Bayer to rgb (png needs BGR order)
549
            if(captureData[i].frames1[j].type() == CV_8UC1)
558
            if(captureData[i].frames1[j].type() == CV_8UC1)
550
                cv::cvtColor(captureData[i].frames1[j], frameBGR, CV_BayerBG2BGR);
559
                cv::cvtColor(captureData[i].frames1[j], frameBGR, CV_BayerBG2BGR);
551
            else
560
            else
552
                cv::cvtColor(captureData[i].frames1[j], frameBGR, CV_RGB2BGR);
561
                cv::cvtColor(captureData[i].frames1[j], frameBGR, CV_RGB2BGR);
553
 
562
 
554
            cv::imwrite(fileName1.toStdString(), frameBGR);
563
            cv::imwrite(fileName1.toStdString(), frameBGR);
555
 
564
 
556
            // Necessary to prevent pileup of video frame signals
565
            // Necessary to prevent pileup of video frame signals
557
            QCoreApplication::processEvents();
566
            QCoreApplication::processEvents();
558
            if(progressDialog.wasCanceled())
567
            if(progressDialog.wasCanceled())
559
                return;
568
                return;
560
        }
569
        }
561
    }
570
    }
562
}
571
}
563
 
572
 
564
void SMScanner::on_actionExport_White_Frames_triggered(){
573
void SMScanner::on_actionExport_White_Frames_triggered(){
565
 
574
 
566
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
575
    QString dirName = QFileDialog::getExistingDirectory(this, "Export frame sequences", QString());
567
 
576
 
568
    QProgressDialog progressDialog("Exporting White Frames...", "Cancel", 0, 100, this);
577
    QProgressDialog progressDialog("Exporting White Frames...", "Cancel", 0, 100, this);
569
    progressDialog.setWindowModality(Qt::WindowModal);
578
    progressDialog.setWindowModality(Qt::WindowModal);
570
    progressDialog.setMinimumDuration(1000);
579
    progressDialog.setMinimumDuration(1000);
571
 
580
 
572
    cv::Mat frameBGR;
581
    cv::Mat frameBGR;
573
    for(unsigned int i=0; i<captureData.size(); i++){
582
    for(unsigned int i=0; i<captureData.size(); i++){
574
 
583
 
575
        progressDialog.setValue(100.0*i/captureData.size());
584
        progressDialog.setValue(100.0*i/captureData.size());
576
 
585
 
577
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
586
        QString seqDirName = QString("%1/sequence_%2").arg(dirName).arg(i);
578
        if(!QDir().mkdir(seqDirName))
587
        if(!QDir().mkdir(seqDirName))
579
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
588
            std::cerr << "Could not create directory " << seqDirName.toStdString() << std::endl;
580
 
589
 
581
        QString fileName = QString("%1/frames0_0.png").arg(seqDirName);
590
        QString fileName = QString("%1/frames0_0.png").arg(seqDirName);
582
        // Convert Bayer to rgb (png needs BGR order)
591
        // Convert Bayer to rgb (png needs BGR order)
583
        cv::cvtColor(captureData[i].frames0[0], frameBGR, CV_BayerBG2BGR);
592
        cv::cvtColor(captureData[i].frames0[0], frameBGR, CV_BayerBG2BGR);
584
        cv::imwrite(fileName.toStdString(), frameBGR);
593
        cv::imwrite(fileName.toStdString(), frameBGR);
585
        // Necessary to prevent memory pileup
594
        // Necessary to prevent memory pileup
586
        QCoreApplication::processEvents();
595
        QCoreApplication::processEvents();
587
 
596
 
588
        fileName = QString("%1/frames1_0.png").arg(seqDirName);
597
        fileName = QString("%1/frames1_0.png").arg(seqDirName);
589
        // Convert Bayer to rgb (png needs BGR order)
598
        // Convert Bayer to rgb (png needs BGR order)
590
        cv::cvtColor(captureData[i].frames1[0], frameBGR, CV_BayerBG2BGR);
599
        cv::cvtColor(captureData[i].frames1[0], frameBGR, CV_BayerBG2BGR);
591
        cv::imwrite(fileName.toStdString(), frameBGR);
600
        cv::imwrite(fileName.toStdString(), frameBGR);
592
        // Necessary to prevent memory pileup
601
        // Necessary to prevent memory pileup
593
        QCoreApplication::processEvents();
602
        QCoreApplication::processEvents();
594
        if(progressDialog.wasCanceled())
603
        if(progressDialog.wasCanceled())
595
            return;
604
            return;
596
    }
605
    }
597
 
606
 
598
}
607
}
599
 
608
 
600
 
609
 
601
void SMScanner::onReceivePointCloud(SMPointCloud smCloud){
610
void SMScanner::onReceivePointCloud(SMPointCloud smCloud){
602
 
611
 
603
    pointCloudData.push_back(smCloud);
612
    pointCloudData.push_back(smCloud);
604
 
613
 
605
    // Add identifier to list
614
    // Add identifier to list
606
    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));
607
    item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
616
    item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
608
    item->setData(Qt::UserRole, QVariant(smCloud.id));
617
    item->setData(Qt::UserRole, QVariant(smCloud.id));
609
    item->setCheckState(Qt::Checked);
618
    item->setCheckState(Qt::Checked);
610
 
619
 
611
    ui->pointCloudsListWidget->addItem(item);
620
    ui->pointCloudsListWidget->addItem(item);
612
    ui->pointCloudWidget->addPointCloud(smCloud, smCloud.id);
621
    ui->pointCloudWidget->addPointCloud(smCloud, smCloud.id);
613
 
622
 
614
    // Indicate completed reconstruction in captureTreeWidget
623
    // Indicate completed reconstruction in captureTreeWidget
615
    for(int i=0; i<ui->captureTreeWidget->topLevelItemCount(); i++){
624
    for(int i=0; i<ui->captureTreeWidget->topLevelItemCount(); i++){
616
        QTreeWidgetItem *captureItem = ui->captureTreeWidget->topLevelItem(i);
625
        QTreeWidgetItem *captureItem = ui->captureTreeWidget->topLevelItem(i);
617
        if(captureItem->data(0, Qt::UserRole).toPoint() == QPoint(smCloud.id, -1)){
626
        if(captureItem->data(0, Qt::UserRole).toPoint() == QPoint(smCloud.id, -1)){
618
            captureItem->setTextColor(0, QColor(0, 0, 0));
627
            captureItem->setTextColor(0, QColor(0, 0, 0));
619
            captureItem->setIcon(0, QIcon());
628
            captureItem->setIcon(0, QIcon());
620
        }
629
        }
621
   }
630
   }
622
 
631
 
623
}
632
}
624
 
633
 
625
void SMScanner::on_actionExport_Point_Clouds_triggered(){
634
void SMScanner::on_actionExport_Point_Clouds_triggered(){
626
 
635
 
627
    QString directory = QFileDialog::getExistingDirectory(this, "Export Point Clouds", QString());
636
    QString directory = QFileDialog::getExistingDirectory(this, "Export Point Clouds", QString());
628
 
637
 
629
//    //  Non native file dialog
638
//    //  Non native file dialog
630
//    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");
631
//    saveDirectoryDialog.setDefaultSuffix("ply");
640
//    saveDirectoryDialog.setDefaultSuffix("ply");
632
//    saveDirectoryDialog.setFileMode(QFileDialog::Directory);
641
//    saveDirectoryDialog.setFileMode(QFileDialog::Directory);
633
//    saveDirectoryDialog.exec();
642
//    saveDirectoryDialog.exec();
634
//    QString directory = saveDirectoryDialog.directory().path();
643
//    QString directory = saveDirectoryDialog.directory().path();
635
//    QString type = saveDirectoryDialog.selectedNameFilter();
644
//    QString type = saveDirectoryDialog.selectedNameFilter();
636
 
645
 
637
    QProgressDialog progressDialog("Exporting Point Clouds...", "Cancel", 0, 100, this);
646
    QProgressDialog progressDialog("Exporting Point Clouds...", "Cancel", 0, 100, this);
638
    progressDialog.setWindowModality(Qt::WindowModal);
647
    progressDialog.setWindowModality(Qt::WindowModal);
639
    progressDialog.setMinimumDuration(1000);
648
    progressDialog.setMinimumDuration(1000);
640
 
649
 
641
    // save point clouds in ply format
650
    // save point clouds in ply format
642
    for(unsigned int i=0; i<pointCloudData.size(); i++){
651
    for(unsigned int i=0; i<pointCloudData.size(); i++){
643
 
652
 
644
        progressDialog.setValue(100.0*i/pointCloudData.size());
653
        progressDialog.setValue(100.0*i/pointCloudData.size());
645
    if(pointCloudData[i].id != -1){
654
    if(pointCloudData[i].id != -1){
646
        QString fileName = QString("%1/pointcloud_%2.ply").arg(directory).arg(i);
655
        QString fileName = QString("%1/pointcloud_%2.ply").arg(directory).arg(i);
647
        pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCL(pointCloudData[i].pointCloud);
656
        pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCL(pointCloudData[i].pointCloud);
648
        //pcl::io::savePLYFileBinary(fileName.toStdString(), *pointCloudPCL);
657
        //pcl::io::savePLYFileBinary(fileName.toStdString(), *pointCloudPCL);
649
        pcl::PLYWriter w;
658
        pcl::PLYWriter w;
650
        // Write to ply in binary without camera
659
        // Write to ply in binary without camera
651
        w.write<pcl::PointXYZRGBNormal> (fileName.toStdString(), *pointCloudPCL, true, false);
660
        w.write<pcl::PointXYZRGBNormal> (fileName.toStdString(), *pointCloudPCL, true, false);
652
    }
661
    }
653
        QCoreApplication::processEvents();
662
        QCoreApplication::processEvents();
654
        if(progressDialog.wasCanceled())
663
        if(progressDialog.wasCanceled())
655
            return;
664
            return;
656
    }
665
    }
657
 
666
 
658
    // save meshlab aln project file
667
    // save meshlab aln project file
659
    std::ofstream s(QString("%1/alignment.aln").arg(directory).toLocal8Bit());
668
    std::ofstream s(QString("%1/alignment.aln").arg(directory).toLocal8Bit());
660
    s << pointCloudData.size() << std::endl;
669
    s << pointCloudData.size() << std::endl;
661
    for(unsigned int i=0; i<pointCloudData.size(); i++){
670
    for(unsigned int i=0; i<pointCloudData.size(); i++){
662
        if(pointCloudData[i].id != -1){
671
        if(pointCloudData[i].id != -1){
663
        QString fileName = QString("pointcloud_%1.ply").arg(pointCloudData[i].id);
672
        QString fileName = QString("pointcloud_%1.ply").arg(pointCloudData[i].id);
664
        s << fileName.toStdString() << std::endl << "#" << std::endl;
673
        s << fileName.toStdString() << std::endl << "#" << std::endl;
665
        cv::Mat Tr = cv::Mat::eye(4, 4, CV_32F);
674
        cv::Mat Tr = cv::Mat::eye(4, 4, CV_32F);
666
        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));
667
        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));
668
        for(int j=0; j<Tr.rows; j++){
677
        for(int j=0; j<Tr.rows; j++){
669
            for(int k=0; k<Tr.cols; k++){
678
            for(int k=0; k<Tr.cols; k++){
670
                s << Tr.at<float>(j,k) << " ";
679
                s << Tr.at<float>(j,k) << " ";
671
            }
680
            }
672
            s << std::endl;
681
            s << std::endl;
673
        }
682
        }
674
        }
683
        }
675
    }
684
    }
676
    s << "0" << std::flush;
685
    s << "0" << std::flush;
677
    s.close();
686
    s.close();
678
}
687
}
679
 
688
 
680
void SMScanner::on_pointCloudsListWidget_itemChanged(QListWidgetItem *item){
689
void SMScanner::on_pointCloudsListWidget_itemChanged(QListWidgetItem *item){
681
 
690
 
682
    int id = item->data(Qt::UserRole).toInt();
691
    int id = item->data(Qt::UserRole).toInt();
683
    assert((int)pointCloudData.size()>id);
692
    assert((int)pointCloudData.size()>id);
684
 
693
 
685
    if(item->checkState() == Qt::Checked){
694
    if(item->checkState() == Qt::Checked){
686
        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
687
        ui->pointCloudWidget->addPointCloud(pointCloudData[id], id);
696
        ui->pointCloudWidget->addPointCloud(pointCloudData[id], id);
688
    }
697
    }
689
    else
698
    else
690
        ui->pointCloudWidget->removePointCloud(id);
699
        ui->pointCloudWidget->removePointCloud(id);
691
 
700
 
692
}
701
}
693
 
702
 
694
void SMScanner::on_actionExport_Parameters_triggered(){
703
void SMScanner::on_actionExport_Parameters_triggered(){
695
 
704
 
696
    QString fileName = QFileDialog::getSaveFileName(this, "Export calibration parameters", QString(), "*.xml");
705
    QString fileName = QFileDialog::getSaveFileName(this, "Export calibration parameters", QString(), "*.xml");
697
    QFileInfo info(fileName);
706
    QFileInfo info(fileName);
698
    QString type = info.suffix();
707
    QString type = info.suffix();
699
    if(type == ""){
708
    if(type == ""){
700
        fileName.append(".xml");
709
        fileName.append(".xml");
701
    }
710
    }
702
 
711
 
703
    SMCalibrationParameters calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
712
    SMCalibrationParameters calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
704
    calibration.exportToXML(fileName);
713
    calibration.exportToXML(fileName);
705
}
714
}
706
 
715
 
707
void SMScanner::on_actionClear_Sequences_triggered(){
716
void SMScanner::on_actionClear_Sequences_triggered(){
708
 
717
 
709
    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);
710
 
719
 
711
    if(res == QMessageBox::Ok){
720
    if(res == QMessageBox::Ok){
712
        captureData.clear();
721
        captureData.clear();
713
        ui->captureTreeWidget->clear();
722
        ui->captureTreeWidget->clear();
714
        lastCaptureId=-1;
723
        lastCaptureId=-1;
715
    }
724
    }
716
}
725
}
717
 
726
 
718
 
727
 
719
 
728
 
720
void SMScanner::on_actionImport_Parameters_triggered(){
729
void SMScanner::on_actionImport_Parameters_triggered(){
721
 
730
 
722
    QString fileName = QFileDialog::getOpenFileName(this, "Import calibration parameters", QString(), "*.xml");
731
    QString fileName = QFileDialog::getOpenFileName(this, "Import calibration parameters", QString(), "*.xml");
723
    QFileInfo info(fileName);
732
    QFileInfo info(fileName);
724
    QString type = info.suffix();
733
    QString type = info.suffix();
725
    if(type != "xml"){
734
    if(type != "xml"){
726
        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;
727
        return;
736
        return;
728
    }
737
    }
729
 
738
 
730
    SMCalibrationParameters cal;
739
    SMCalibrationParameters cal;
731
    cal.importFromXML(fileName);
740
    cal.importFromXML(fileName);
732
 
741
 
733
    settings.setValue("calibration/parameters",  QVariant::fromValue(cal));
742
    settings.setValue("calibration/parameters",  QVariant::fromValue(cal));
734
    ui->pointCloudWidget->updateCalibrationParameters();
743
    ui->pointCloudWidget->updateCalibrationParameters();
735
 
744
 
736
    std::cout << "Imported calibration parameters " << fileName.toStdString() << std::endl;
745
    std::cout << "Imported calibration parameters " << fileName.toStdString() << std::endl;
737
}
746
}
738
 
747
 
739
void SMScanner::on_actionImport_Sets_triggered(){
748
void SMScanner::on_actionImport_Sets_triggered(){
740
 
749
 
741
    QString dirName = QFileDialog::getExistingDirectory(this, "Import calibration sets", QString());
750
    QString dirName = QFileDialog::getExistingDirectory(this, "Import calibration sets", QString());
742
 
751
 
743
    QDir dir(dirName);
752
    QDir dir(dirName);
744
    QStringList fileNames0 = dir.entryList(QStringList("frame0_*.png"));
753
    QStringList fileNames0 = dir.entryList(QStringList("frame0_*.png"));
745
    QStringList fileNames1 = dir.entryList(QStringList("frame1_*.png"));
754
    QStringList fileNames1 = dir.entryList(QStringList("frame1_*.png"));
746
 
755
 
747
    if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
756
    if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
748
        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;
749
        return;
758
        return;
750
    }
759
    }
751
 
760
 
752
    calibrationData.clear();
761
    calibrationData.clear();
753
    ui->calibrationListWidget->clear();
762
    ui->calibrationListWidget->clear();
754
 
763
 
755
    QProgressDialog progressDialog("Importing Sets...", "Cancel", 0, 100, this);
764
    QProgressDialog progressDialog("Importing Sets...", "Cancel", 0, 100, this);
756
    progressDialog.setWindowModality(Qt::WindowModal);
765
    progressDialog.setWindowModality(Qt::WindowModal);
757
    progressDialog.setMinimumDuration(1000);
766
    progressDialog.setMinimumDuration(1000);
758
 
767
 
759
    size_t nSets = fileNames0.size();
768
    size_t nSets = fileNames0.size();
760
    for(unsigned int i=0; i<nSets; i++){
769
    for(unsigned int i=0; i<nSets; i++){
761
 
770
 
762
        progressDialog.setValue(100.0*i/nSets);
771
        progressDialog.setValue(100.0*i/nSets);
763
 
772
 
764
        SMCalibrationSet calibrationSet;
773
        SMCalibrationSet calibrationSet;
765
 
774
 
766
        QString fileName0 = QString("%1/frame0_%2.png").arg(dirName).arg(i);
775
        QString fileName0 = QString("%1/frame0_%2.png").arg(dirName).arg(i);
767
        cv::Mat frame0BGR = cv::imread(fileName0.toStdString());
776
        cv::Mat frame0BGR = cv::imread(fileName0.toStdString());
768
        cvtools::cvtColorBGRToBayerBG(frame0BGR, calibrationSet.frame0);
777
        cvtools::cvtColorBGRToBayerBG(frame0BGR, calibrationSet.frame0);
769
 
778
 
770
        QString fileName1 = QString("%1/frame1_%2.png").arg(dirName).arg(i);
779
        QString fileName1 = QString("%1/frame1_%2.png").arg(dirName).arg(i);
771
        cv::Mat frame1BGR = cv::imread(fileName1.toStdString());
780
        cv::Mat frame1BGR = cv::imread(fileName1.toStdString());
772
        cvtools::cvtColorBGRToBayerBG(frame1BGR, calibrationSet.frame1);
781
        cvtools::cvtColorBGRToBayerBG(frame1BGR, calibrationSet.frame1);
773
 
782
 
774
//        int id = ui->calibrationListWidget->count();
783
//        int id = ui->calibrationListWidget->count();
775
//        calibrationSet.id = id;
784
//        calibrationSet.id = id;
776
 
785
 
777
//        calibrationData.push_back(calibrationSet);
786
//        calibrationData.push_back(calibrationSet);
778
 
787
 
779
//        // Add identifier to list
788
//        // Add identifier to list
780
//        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);
781
//        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
790
//        item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
782
//        item->setCheckState(Qt::Checked);
791
//        item->setCheckState(Qt::Checked);
783
//        ui->calibrationListWidget->addItem(item);
792
//        ui->calibrationListWidget->addItem(item);
784
 
793
 
785
        this->onReceiveCalibrationSet(calibrationSet);
794
        this->onReceiveCalibrationSet(calibrationSet);
786
 
795
 
787
        QCoreApplication::processEvents();
796
        QCoreApplication::processEvents();
788
        if(progressDialog.wasCanceled())
797
        if(progressDialog.wasCanceled())
789
            return;
798
            return;
790
    }
799
    }
791
 
800
 
792
    // Set enabled checkmark
801
    // Set enabled checkmark
793
    if(calibrationData.size() >= 2){
802
    if(calibrationData.size() >= 2){
794
        ui->calibrateCamerasButton->setEnabled(true);
803
        ui->calibrateCamerasButton->setEnabled(true);
795
        ui->calibrateRotationStageButton->setEnabled(true);
804
        ui->calibrateRotationStageButton->setEnabled(true);
796
    }
805
    }
797
}
806
}
798
 
807
 
799
void SMScanner::on_actionImport_Sequences_triggered(){
808
void SMScanner::on_actionImport_Sequences_triggered(){
800
 
809
 
801
    // NOTE: we do not know which algorithm was used!!!
810
    // NOTE: we do not know which algorithm was used!!!
802
 
811
 
803
    QString dirName = QFileDialog::getExistingDirectory(this, "Import captured sequences", QString());
812
    QString dirName = QFileDialog::getExistingDirectory(this, "Import captured sequences", QString());
804
 
813
 
805
    QDir dir(dirName);
814
    QDir dir(dirName);
806
    QStringList sequenceDirNames = dir.entryList(QStringList("sequence_*"));
815
    QStringList sequenceDirNames = dir.entryList(QStringList("sequence_*"));
807
 
816
 
808
    if(sequenceDirNames.empty()){
817
    if(sequenceDirNames.empty()){
809
        std::cerr << "Error: could not find sequences." << std::endl;
818
        std::cerr << "Error: could not find sequences." << std::endl;
810
        return;
819
        return;
811
    }
820
    }
812
 
821
 
813
    captureData.clear();
822
    captureData.clear();
814
    ui->captureTreeWidget->clear();
823
    ui->captureTreeWidget->clear();
815
    lastCaptureId=-1;
824
    lastCaptureId=-1;
816
 
825
 
817
    QProgressDialog progressDialog("Importing Sequences...", "Cancel", 0, 100, this);
826
    QProgressDialog progressDialog("Importing Sequences...", "Cancel", 0, 100, this);
818
    progressDialog.setWindowModality(Qt::WindowModal);
827
    progressDialog.setWindowModality(Qt::WindowModal);
819
    progressDialog.setMinimumDuration(1000);
828
    progressDialog.setMinimumDuration(1000);
820
 
829
 
821
    for(int s=0; s<sequenceDirNames.count(); s++){
830
    for(int s=0; s<sequenceDirNames.count(); s++){
822
 
831
 
823
        QDir sequenceDir(QString("%1/%2").arg(dirName).arg(sequenceDirNames.at(s)));
832
        QDir sequenceDir(QString("%1/%2").arg(dirName).arg(sequenceDirNames.at(s)));
824
 
833
 
825
        QStringList fileNames0 = sequenceDir.entryList(QStringList("frames0_*.png"));
834
        QStringList fileNames0 = sequenceDir.entryList(QStringList("frames0_*.png"));
826
        QStringList fileNames1 = sequenceDir.entryList(QStringList("frames1_*.png"));
835
        QStringList fileNames1 = sequenceDir.entryList(QStringList("frames1_*.png"));
827
 
836
 
828
        if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
837
        if(fileNames0.empty() || fileNames1.empty() || fileNames0.count() != fileNames1.count()){
829
            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;
830
            return;
839
            return;
831
        }
840
        }
832
 
841
 
833
        int nFrames = fileNames0.size();
842
        int nFrames = fileNames0.size();
834
 
843
 
835
        SMFrameSequence sequence;
844
        SMFrameSequence sequence;
836
 
845
 
837
        for(int f=0; f<nFrames; f++){
846
        for(int f=0; f<nFrames; f++){
838
 
847
 
839
            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);
840
 
849
 
841
            cv::Mat frame0BGR, frame0BayerBG, frame1BGR, frame1BayerBG;
850
            cv::Mat frame0BGR, frame0BayerBG, frame1BGR, frame1BayerBG;
842
 
851
 
843
            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);
844
            frame0BGR = cv::imread(fileName0.toStdString());
853
            frame0BGR = cv::imread(fileName0.toStdString());
845
            cvtools::cvtColorBGRToBayerBG(frame0BGR, frame0BayerBG);
854
            cvtools::cvtColorBGRToBayerBG(frame0BGR, frame0BayerBG);
846
 
855
 
847
            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);
848
            frame1BGR = cv::imread(fileName1.toStdString());
857
            frame1BGR = cv::imread(fileName1.toStdString());
849
            cvtools::cvtColorBGRToBayerBG(frame1BGR, frame1BayerBG);
858
            cvtools::cvtColorBGRToBayerBG(frame1BGR, frame1BayerBG);
850
 
859
 
851
            sequence.frames0.push_back(frame0BayerBG);
860
            sequence.frames0.push_back(frame0BayerBG);
852
            sequence.frames1.push_back(frame1BayerBG);
861
            sequence.frames1.push_back(frame1BayerBG);
853
 
862
 
854
            QCoreApplication::processEvents();
863
            QCoreApplication::processEvents();
855
            if(progressDialog.wasCanceled())
864
            if(progressDialog.wasCanceled())
856
                return;
865
                return;
857
        }
866
        }
858
 
867
 
859
        // Assign whatever algorithm is configured
868
        // Assign whatever algorithm is configured
860
        sequence.codec = settings.value("algorithm").toString();
869
        sequence.codec = settings.value("algorithm").toString();
861
        sequence.rotationAngle = 0;
870
        sequence.rotationAngle = 0;
862
        sequence.reconstructed = false;
871
        sequence.reconstructed = false;
863
        sequence.shutters.push_back(16.6666);
872
        sequence.shutters.push_back(16.6666);
864
 
873
 
865
        onReceiveFrameSequence(sequence);
874
        onReceiveFrameSequence(sequence);
866
    }
875
    }
867
 
876
 
868
}
877
}
869
 
878
 
870
void SMScanner::on_actionClear_Point_Clouds_triggered(){
879
void SMScanner::on_actionClear_Point_Clouds_triggered(){
871
 
880
 
872
    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);
873
 
882
 
874
    if(res == QMessageBox::Ok){
883
    if(res == QMessageBox::Ok){
875
 
884
 
876
        pointCloudData.clear();
885
        pointCloudData.clear();
877
        ui->pointCloudsListWidget->clear();
886
        ui->pointCloudsListWidget->clear();
878
        ui->pointCloudWidget->removeAllPointClouds();
887
        ui->pointCloudWidget->removeAllPointClouds();
879
 
888
 
880
        for(unsigned int i=0; i<captureData.size(); i++)
889
        for(unsigned int i=0; i<captureData.size(); i++)
881
            captureData[i].reconstructed = false;
890
            captureData[i].reconstructed = false;
882
    }
891
    }
883
}
892
}
884
 
893
 
885
 
894
 
886
void SMScanner::on_actionProject_Focusing_Pattern_triggered(){
895
void SMScanner::on_actionProject_Focusing_Pattern_triggered(){
887
 
896
 
888
    bool projectFocusingPattern = ui->actionProject_Focusing_Pattern->isChecked();
897
    bool projectFocusingPattern = ui->actionProject_Focusing_Pattern->isChecked();
889
    QMetaObject::invokeMethod(captureWorker, "setProjectFocusingPattern", Q_ARG(bool, projectFocusingPattern));
898
    QMetaObject::invokeMethod(captureWorker, "setProjectFocusingPattern", Q_ARG(bool, projectFocusingPattern));
890
 
899
 
891
}
900
}
892
 
901
 
893
void SMScanner::on_calibrateRotationStageButton_clicked(){
902
void SMScanner::on_calibrateRotationStageButton_clicked(){
894
 
903
 
895
    // disable ui elements
904
    // disable ui elements
896
    ui->calibrateCamerasButton->setEnabled(false);
905
    ui->calibrateCamerasButton->setEnabled(false);
897
    ui->calibrateRotationStageButton->setEnabled(false);
906
    ui->calibrateRotationStageButton->setEnabled(false);
898
    ui->calibrationFrame->setEnabled(false);
907
    ui->calibrationFrame->setEnabled(false);
899
 
908
 
900
    // if none items are selected, we will use all available items
909
    // if none items are selected, we will use all available items
901
    if(ui->calibrationListWidget->selectedItems().empty())
910
    if(ui->calibrationListWidget->selectedItems().empty())
902
        ui->calibrationListWidget->selectAll();
911
        ui->calibrationListWidget->selectAll();
903
 
912
 
904
    // set selected flags
913
    // set selected flags
905
    for(unsigned int i=0; i<calibrationData.size(); i++){
914
    for(unsigned int i=0; i<calibrationData.size(); i++){
906
        if(ui->calibrationListWidget->item(i)->isSelected())
915
        if(ui->calibrationListWidget->item(i)->isSelected())
907
            calibrationData[i].selected = true;
916
            calibrationData[i].selected = true;
908
        else
917
        else
909
            calibrationData[i].selected = false;
918
            calibrationData[i].selected = false;
910
    }
919
    }
911
 
920
 
912
    logDialog.show();
921
    logDialog.show();
913
    logDialog.activateWindow();
922
    logDialog.activateWindow();
914
 
923
 
915
    // start calibration
924
    // start calibration
916
    QMetaObject::invokeMethod(calibrationWorker, "rotationStageCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
925
    QMetaObject::invokeMethod(calibrationWorker, "rotationStageCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
917
 
926
 
918
}
927
}
919
 
928
 
920
 
929
 
921
void SMScanner::on_actionView_Log_Messages_triggered()
930
void SMScanner::on_actionView_Log_Messages_triggered()
922
{
931
{
923
     logDialog.show();
932
     logDialog.show();
924
     logDialog.activateWindow();
933
     logDialog.activateWindow();
925
}
934
}
926
 
935
 
927
void SMScanner::on_tabWidget_currentChanged(int index)
936
void SMScanner::on_tabWidget_currentChanged(int index)
928
{
937
{
929
    if(index==0){
938
    if(index==0){
930
        ui->calibrationCamera0Widget->fitImage();
939
        ui->calibrationCamera0Widget->fitImage();
931
        ui->calibrationCamera1Widget->fitImage();
940
        ui->calibrationCamera1Widget->fitImage();
932
    }
941
    }
933
    if(index==1){
942
    if(index==1){
934
        ui->captureCamera0Widget->fitImage();
943
        ui->captureCamera0Widget->fitImage();
935
        ui->captureCamera1Widget->fitImage();
944
        ui->captureCamera1Widget->fitImage();
936
    }
945
    }
937
}
946
}
938
 
947
 
939
void SMScanner::on_calibrationListWidget_currentRowChanged(int currentRow)
948
void SMScanner::on_calibrationListWidget_currentRowChanged(int currentRow)
940
{
949
{
941
    std::cout << "Current row: " << currentRow << std::endl;
950
    std::cout << "Current row: " << currentRow << std::endl;
942
    on_calibrationListWidget_itemSelectionChanged();
951
    on_calibrationListWidget_itemSelectionChanged();
943
}
952
}
944
 
953
 
945
void SMScanner::on_actionEdit_Configuration_File_triggered()
954
void SMScanner::on_actionEdit_Configuration_File_triggered()
946
{
955
{
947
    QDesktopServices::openUrl(QUrl::fromLocalFile(settings.fileName()));
956
    QDesktopServices::openUrl(QUrl::fromLocalFile(settings.fileName()));
948
}
957
}
949
 
958