-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.cpp
343 lines (277 loc) · 11.1 KB
/
MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMenu>
#include <QGLFormat>
#include <QDebug>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QUrl>
#include <QVector>
#include <QtCore/qmath.h>
#include <QProgressDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QtCore/qmath.h>
#include <QMimeData>
#include "rply.h"
#include "PointCloud.h"
#include "PLYLoader.h"
#include "PointGenerator.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow), m_viewer(NULL)
{
ui->setupUi(this);
// Get default OpenGL format
QGLFormat format = QGLFormat::defaultFormat();
// Optional sync to VBLANK; enabled by default
#ifndef NO_VBLANK_SYNC
format.setSwapInterval(1);
#endif
// Enable sample buffers
format.setOption(QGL::SampleBuffers);
// Request alpha buffer
format.setAlpha(true);
// Request stereo
format.setStereo(true);
// Create OpenGL context
QGLContext context(format);
// Try to create context with format
if(!context.create())
{
// If context couldn't be created, disable stereo buffers
format.setStereo(false);
}
// Update default OpenGL format
QGLFormat::setDefaultFormat(format);
// Create viewer; don't set as the central widget until later. This
// suppresses an "Invalid Drawable" message for OS X
m_viewer = new Viewer();
m_createOptions = new CreatePointCloudDialog(this);
QMenu *fileMenu = menuBar()->addMenu("File");
fileMenu->addAction("Open File...", this, SLOT(openFile()),
QKeySequence::Open);
fileMenu->addAction("Create Point Cloud...", m_createOptions,
SLOT(show()));
connect(m_createOptions, SIGNAL(accepted(QString,int,bool)),
SLOT(createPointCloud(QString,int,bool)));
// Hide option to export movie from flight path; Not ready for other users
// fileMenu->addAction("Save Path Movie", m_viewer, SLOT(savePathMovie()));
m_infoDialog = new InfoDialog(this);
fileMenu->addAction("Info", this, SLOT(showInfo()),
QKeySequence("Ctrl+I"));
fileMenu->addAction("Quit", qApp, SLOT(quit()));
m_displayOptions = new DisplayOptionsDialog(this);
m_displayOptions->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
// Sync viewer settings to display options dialog
connect(m_viewer, SIGNAL(pointSizeChanged(int)),
m_displayOptions, SLOT(setPointSize(int)));
connect(m_viewer, SIGNAL(pointDensityChanged(int)),
m_displayOptions, SLOT(setPointDensity(int)));
connect(m_viewer, SIGNAL(smoothPointsChanged(bool)),
m_displayOptions, SLOT(setSmoothPoints(bool)));
connect(m_viewer, SIGNAL(pointColorChanged(bool)),
m_displayOptions, SLOT(setPointColor(bool)));
connect(m_viewer, SIGNAL(depthMaskingChanged(bool)),
m_displayOptions, SLOT(setDepthMask(bool)));
connect(m_viewer, SIGNAL(multisampleChanged(bool)),
m_displayOptions, SLOT(setMultisample(bool)));
connect(m_viewer, SIGNAL(fastInteractionChanged(bool)),
m_displayOptions, SLOT(setFastInteraction(bool)));
// Sync display options dialog to viewer
connect(m_displayOptions, SIGNAL(pointSizeChanged(int)),
m_viewer, SLOT(setPointSize(int)));
connect(m_displayOptions, SIGNAL(pointDensityChanged(int)),
m_viewer, SLOT(setPointDensity(int)));
connect(m_displayOptions, SIGNAL(pointSmoothChanged(bool)),
m_viewer, SLOT(setSmoothPoints(bool)));
connect(m_displayOptions, SIGNAL(pointColorChanged(bool)),
m_viewer, SLOT(setColorPoints(bool)));
connect(m_displayOptions, SIGNAL(pointDepthChanged(bool)),
m_viewer, SLOT(setDepthMasking(bool)));
connect(m_displayOptions, SIGNAL(multiSampleChanged(bool)),
m_viewer, SLOT(setMultisample(bool)));
connect(m_displayOptions, SIGNAL(fastInteractionChanged(bool)),
m_viewer, SLOT(setFastInteraction(bool)));
m_displayOptions->setMultisampleAvailable(m_viewer->multisampleAvailable());
QMenu *displayMenu = menuBar()->addMenu("Display");
displayMenu->addAction("Display Options...", m_displayOptions,
SLOT(show()));
// Create stereo options dialog
m_stereoOptions = new StereoOptionsDialog(this);
m_stereoOptions->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
// Sync viewer and dialog
connect(m_stereoOptions, &StereoOptionsDialog::IODistanceChanged,
m_viewer, &Viewer::setIODistance);
connect(m_stereoOptions, &StereoOptionsDialog::screenWidthChanged,
m_viewer, &Viewer::setScreenWidth);
connect(m_stereoOptions, &StereoOptionsDialog::focusDistanceChanged,
m_viewer, &Viewer::setFocusDistance);
connect(m_stereoOptions, &StereoOptionsDialog::swapLeftRightChanged,
m_viewer, &Viewer::setSwapLeftRight);
connect(m_stereoOptions, &StereoOptionsDialog::stereoModeChanged,
m_viewer, &Viewer::setStereoMode);
// Sync viewer values back to dialog
connect(m_viewer, &Viewer::focusDistanceChanged, m_stereoOptions,
&StereoOptionsDialog::setFocusDistance);
connect(m_viewer, &Viewer::physicalScreenWidthChanged, m_stereoOptions,
&StereoOptionsDialog::setScreenWidth);
connect(m_viewer, &Viewer::stereoModeChanged, m_stereoOptions,
&StereoOptionsDialog::setStereoMode);
m_stereoOptions->addStereoMode("Red-Cyan", Viewer::Red_Cyan);
m_stereoOptions->addStereoMode("Red-Blue", Viewer::Red_Blue);
m_stereoOptions->addStereoMode("Side-by-Side", Viewer::Side_by_Side);
m_stereoOptions->addStereoMode("Stacked", Viewer::Stacked);
if(m_viewer->supportsHardwareStereo())
m_stereoOptions->addStereoMode("Hardware", Viewer::Hardware);
m_stereoOptions->setIODistance(m_viewer->camera()->IODistance());
m_stereoOptions->setScreenWidth(m_viewer->camera()->physicalScreenWidth());
m_stereoOptions->setFocusDistance(m_viewer->camera()->focusDistance());
m_stereoOptions->setStereoMode(m_viewer->stereoMode());
// Create menu item
displayMenu->addAction("Stereo Options...", m_stereoOptions, SLOT(show()));
QMenu *helpMenu = menuBar()->addMenu("Help");
helpMenu->addAction("Help...", m_viewer, SLOT(help()));
// Set default viewer values
m_viewer->setPointSize(1);
m_viewer->setSmoothPoints(true);
m_viewer->setColorPoints(true);
m_viewer->setDepthMasking(true);
m_viewer->setMultisample(true);
m_viewer->setFastInteraction(false);
m_viewer->setPointDensity(100);
// Set viewer as central widget
setCentralWidget(m_viewer);
resize(825,550);
setAcceptDrops(true);
}
void MainWindow::openFile()
{
// Get file to open
QString path = QFileDialog::getOpenFileName(this, "Open File");
if(!path.isEmpty())
openFile(path);
}
void MainWindow::openFile(const QString &path)
{
// Try to open PLY file
if(PLYLoader::canRead(path))
{
PLYLoader loader;
if(loader.open(path))
{
QProgressDialog progress(this);
progress.setWindowModality(Qt::WindowModal);
progress.setRange(0, 100);
progress.setMinimumDuration(1000);
progress.setAutoClose(false);
connect(&loader, SIGNAL(progress(int)), &progress, SLOT(setValue(int)));
connect(&progress, SIGNAL(canceled()), &loader, SLOT(cancel()));
PointCloud cloud = loader.load();
cloud.shuffle();
m_viewer->setPointCloud(cloud);
// Load cameras
for(int i = 0; i < loader.cameraPositions().count(); i += 3)
{
Vec position(loader.cameraPositions().at(i + 0),
loader.cameraPositions().at(i + 1),
loader.cameraPositions().at(i + 2));
m_viewer->camera()->setPosition(position);
Vec up(loader.cameraUpVectors().at(i + 0),
loader.cameraUpVectors().at(i + 1),
loader.cameraUpVectors().at(i + 2));
m_viewer->camera()->setUpVector(up);
Vec aim(loader.cameraAimVectors().at(i + 0),
loader.cameraAimVectors().at(i + 1),
loader.cameraAimVectors().at(i + 2));
m_viewer->camera()->setViewDirection(aim);
Vec aspect(loader.cameraAspectRatios().at(i + 0),
loader.cameraAspectRatios().at(i + 1),
loader.cameraAspectRatios().at(i + 2));
m_viewer->camera()->setFieldOfView(2 * qAtan((0.5 * aspect.y)/aspect.z));
m_viewer->m_fov.push_back(2 * qAtan((0.5 * aspect.y)/aspect.z));
m_viewer->camera()->addKeyFrameToPath(1);
}
// Check that keyframes have been added to path 1
if(m_viewer->camera()->keyFrameInterpolator(1) != NULL)
{
connect(m_viewer->camera()->keyFrameInterpolator(1),
SIGNAL(interpolated()), m_viewer, SLOT(updateGL()));
m_viewer->camera()->keyFrameInterpolator(1)->setInterpolationSpeed(4.0);
}
progress.close();
return;
}
}
QMessageBox::critical(this, "Unable to open file",
path + " is not a supported format.");
}
void MainWindow::showInfo()
{
m_infoDialog->setOpenGLInfo(m_viewer->openGLInfo());
m_infoDialog->setPointCloudInfo(m_viewer->pointCloudInfo());
m_infoDialog->show();
}
bool MainWindow::canRead(const QString &path)
{
return PLYLoader::canRead(path);
}
void MainWindow::createPointCloud(QString shape, int count, bool asSurface)
{
PointGenerator generator;
QProgressDialog progress(this);
progress.setWindowModality(Qt::WindowModal);
connect(&generator, SIGNAL(setRange(int,int)), &progress,
SLOT(setRange(int,int)));
connect(&generator, SIGNAL(progress(int)), &progress, SLOT(setValue(int)));
connect(&progress, SIGNAL(canceled()), &generator, SLOT(cancel()));
PointCloud cloud = generator.createPointCloud(shape, count, asSurface);
progress.hide();
m_viewer->setPointCloud(cloud);
}
void MainWindow::closeEvent(QCloseEvent *)
{
qApp->quit();
}
void MainWindow::dragEnterEvent(QDragEnterEvent *e)
{
if(e->mimeData()->hasUrls())
{
if(e->mimeData()->urls().count() == 1)
{
// See if file can be read
QString path = e->mimeData()->urls().at(0).toLocalFile();
if(canRead(path))
{
// Force copy action (for correct mouse pointer icon)
e->setDropAction(Qt::CopyAction);
e->accept();
} else {
e->ignore();
}
}
}
}
void MainWindow::dropEvent(QDropEvent *e)
{
if(e->mimeData()->hasUrls())
{
if(e->mimeData()->urls().count() == 1)
{
// See if file can be read
QString path = e->mimeData()->urls().at(0).toLocalFile();
e->accept();
// The following queues the opening of the file by invoking openFile as
// a queued slot call. This causes the dropEvent to be handled
// immediately, and the file to be opened on return to the event loop.
// Keeps the mouse pointer from becoming a wait pointer and makes the
// gui more responsive.
QMetaObject::invokeMethod(this, "openFile", Qt::QueuedConnection,
Q_ARG(QString, path));
}
}
}
MainWindow::~MainWindow()
{
delete ui;
}