Subversion Repositories seema-scanner

Rev

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

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