Subversion Repositories seema-scanner

Rev

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

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