-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlasifywindow.cpp
435 lines (411 loc) · 13.3 KB
/
lasifywindow.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/******************************************************/
/* */
/* lasifywindow.cpp - main window of lasify */
/* */
/******************************************************/
/* Copyright 2020-2022 Pierre Abbat.
* This file is part of Wolkenbase.
*
* Wolkenbase is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wolkenbase is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wolkenbase. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "lasifywindow.h"
#include "ldecimal.h"
#include "angle.h"
#include "ply.h"
#include "cloud.h"
#include "fileio.h"
#include "brevno.h"
#include "octree.h"
#include "freeram.h"
#include "scan.h"
using namespace std;
const char unitIconNames[4][28]=
{
":/meter.png",":/international-foot.png",":/us-foot.png",":/indian-foot.png"
};
const char prefixes[]="yzafpnum kMGTPEZY"; // u should be µ but that's two bytes
string threePrefix(double x)
// Converts x to three digits with a prefix.
{
int e=0;
int sign=1;
char buf[8];
string ret;
if (x<0)
{
x=-x;
sign=-1;
}
while (x>0 && x<1)
{
x*=1000;
e--;
}
while (x>=1000 && x<INFINITY)
{
x/=1000;
e++;
}
if (x>=100)
sprintf(buf,"%3.0f",x);
else if (x>=10)
sprintf(buf,"%3.1f",x);
else
sprintf(buf,"%3.2f",x);
ret=buf;
if (sign<0)
ret='-'+ret;
switch (prefixes[e+8])
{
case 'u':
ret+=" µ";
break;
case ' ':
ret+=' ';
break;
default:
ret=ret+' '+prefixes[e+8];
}
return ret;
}
LasifyWindow::LasifyWindow(QWidget *parent):QMainWindow(parent)
{
setWindowTitle(QApplication::translate("main", "LASify"));
setWindowIcon(QIcon(":/wolkenbase.png"));
fileMsg=new QLabel(this);
dotTriangleMsg=new QLabel(this);
memoryMsg=new QLabel(this);
densityMsg=new QLabel(this);
canvas=new WolkenCanvas(this);
configDialog=new ConfigurationDialog(this);
msgBox=new QMessageBox(this);
connect(configDialog,SIGNAL(settingsChanged(double,int,int,bool,double,double,double,double)),
this,SLOT(setSettings(double,int,int,bool,double,double,double,double)));
connect(this,SIGNAL(tinSizeChanged()),canvas,SLOT(setSize()));
connect(this,SIGNAL(lengthUnitChanged(double)),canvas,SLOT(setLengthUnit(double)));
connect(this,SIGNAL(fileOpened(std::string)),canvas,SLOT(readFileHeader(std::string)));
connect(this,SIGNAL(allPointsCounted()),canvas,SLOT(writeFile()));
connect(canvas,SIGNAL(readFileProgress(size_t,size_t)),this,SLOT(readFileProgress(size_t,size_t)));
connect(this,SIGNAL(gotResult(ThreadAction)),this,SLOT(handleResult(ThreadAction)));
doneBar=new QProgressBar(this);
busyBar=new QProgressBar(this);
doneBar->setRange(0,16777216);
busyBar->setRange(0,16777216);
lpfBusyFraction=0;
density=0;
threadsCountedPoints=0;
conversionStopped=false;
showingResult=false;
toolbar=new QToolBar(this);
addToolBar(Qt::TopToolBarArea,toolbar);
toolbar->setIconSize(QSize(32,32));
makeActions();
makeStatusBar();
setCentralWidget(canvas);
readSettings();
canvas->show();
show();
timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(tick()));
connect(timer,SIGNAL(timeout()),canvas,SLOT(tick()));
timer->start(50);
}
LasifyWindow::~LasifyWindow()
{
}
/* Rules for enabling actions:
* You cannot load or open a file while a conversion is in progress.
* You can load a file while loading another file (it will be queued).
* You can load a file while a conversion is stopped, but then you cannot resume.
* You can open a file while a conversion is stopped. If it is a final file,
* you cannot resume; if it is a checkpoint file, you can resume.
* You cannot clear or export while loading a file or converting.
* You cannot start a conversion while loading a file or converting.
* You can start a conversion after loading a file, but not after opening one.
* You cannot stop a conversion unless one is in progress and has
* passed the octagon stage.
* You cannot resume a conversion unless one has been stopped or you have
* opened a checkpoint file and no file has been loaded and the TIN
* has not been cleared.
*/
void LasifyWindow::tick()
{
double toleranceRatio;
int tstatus=getThreadStatus();
ThreadAction ta;
if (!showingResult && !resultQueueEmpty())
{
ta=dequeueResult();
gotResult(ta);
}
dotTriangleMsg->setText(tr("%n points","",cloud.size()));
memoryMsg->setText(QString::fromStdString(threePrefix(freeRam())+'B'));
lpfBusyFraction=(16*lpfBusyFraction+busyFraction())/17;
busyBar->setValue(lrint(lpfBusyFraction*16777216));
if ((tstatus&0x3ffbfeff)==1048577*TH_READ && readFileTotal>0)
doneBar->setValue(lrint((double)readFileSoFar/readFileTotal*16777216));
if ((tstatus&0x3ffbfeff)==1048577*TH_SCAN || (tstatus&0x3ffbfeff)==1048577*TH_SPLIT)
doneBar->setValue(lrint(snake.progress()*16777216));
if (tstatus==1048577*TH_PAUSE+TH_ASLEEP && actionQueueEmpty())
currentAction=0;
writeBufLog();
}
void LasifyWindow::openFile()
{
int dialogResult;
QStringList files;
int i;
string fileName;
ThreadAction ta;
fileDialog=new QFileDialog(this);
fileDialog->setWindowTitle(tr("Open Point Cloud"));
fileDialog->setFileMode(QFileDialog::ExistingFile);
fileDialog->setAcceptMode(QFileDialog::AcceptOpen);
fileDialog->selectFile("");
#ifdef Plytapus_FOUND
fileDialog->setNameFilter(tr("(*.ply);;(*.xyz);;(*)"));
#else
fileDialog->setNameFilter(tr("(*.xyz);;(*)"));
#endif
dialogResult=fileDialog->exec();
if (dialogResult)
{
files=fileDialog->selectedFiles();
for (i=0;i<files.size();i++)
{
fileName=files[i].toStdString();
if (fileNames.length())
fileNames+=';';
fileNames+=baseName(fileName);
lastFileName=fileName;
ta.filename=fileName;
canvas->inFileHeaders.resize(1);
canvas->inFileHeaders[0].openFake(fileName);
ta.hdr=&canvas->inFileHeaders[0];
ta.opcode=ACT_LOAD;
enqueueAction(ta);
}
}
delete fileDialog;
fileDialog=nullptr;
}
void LasifyWindow::disableMenuSplash()
/* Disable menu during splash screen, so that the user can't mess up
* the TIN that the splash screen shows.
*/
{
openAction->setEnabled(false);
loadAction->setEnabled(false);
clearAction->setEnabled(false);
stopAction->setEnabled(false);
}
void LasifyWindow::enableMenuSplash()
{
openAction->setEnabled(true);
loadAction->setEnabled(true);
clearAction->setEnabled(false);
stopAction->setEnabled(false);
}
void LasifyWindow::stopConversion()
{
setThreadCommand(TH_WAIT);
conversionStopped=true;
}
void LasifyWindow::resumeConversion()
{
if (conversionStopped)
{
setThreadCommand(TH_SPLIT);
conversionStopped=false;
}
}
void LasifyWindow::readFileProgress(size_t sofar,size_t total)
{
readFileSoFar=sofar;
readFileTotal=total;
}
void LasifyWindow::configure()
{
configDialog->set(lengthUnit,numberThreads,cloudOutput.pointsPerFile,cloudOutput.separateClasses,canvas->tileSize,maxSlope,thickness,minHyperboloidSize);
configDialog->open();
}
void LasifyWindow::handleResult(ThreadAction ta)
/* Receives the result of counting classified points.
*/
{
QString message;
showingResult=true;
switch (ta.opcode)
{
case ACT_COUNT:
if (++threadsCountedPoints==nThreads())
{
allPointsCounted();
threadsCountedPoints=0;
}
break;
case ACT_LOAD:
cout<<"Finished loading cloud\n";
currentAction=0;
tinSizeChanged();
break;
}
showingResult=false;
}
void LasifyWindow::aboutProgram()
{
QString progName=tr("Wolkenbase");
#ifdef Plytapus_FOUND
QString rajotte=tr("\nPlytapus library version %1\nCopyright %2\nSimon Rajotte and Pierre Abbat\nMIT license")
.arg(QString::fromStdString(plytapusVersion())).arg(plytapusYear());
#else
QString rajotte("");
#endif
QMessageBox::about(this,tr("Wolkenbase"),
tr("%1\nVersion %2\nCopyright %3 Pierre Abbat\nLicense GPL 3 or later%4")
.arg(progName).arg(QString(VERSION)).arg(COPY_YEAR).arg(rajotte));
}
void LasifyWindow::aboutQt()
{
QMessageBox::aboutQt(this,tr("Wolkenbase"));
}
void LasifyWindow::closeEvent(QCloseEvent *event)
{
int result;
writeSettings();
event->accept();
}
void LasifyWindow::makeActions()
{
int i;
fileMenu=menuBar()->addMenu(tr("&File"));
//viewMenu=menuBar()->addMenu(tr("&View"));
settingsMenu=menuBar()->addMenu(tr("&Settings"));
helpMenu=menuBar()->addMenu(tr("&Help"));
// File menu
openAction=new QAction(this);
openAction->setIcon(QIcon::fromTheme("document-open"));
openAction->setText(tr("Open"));
fileMenu->addAction(openAction);
connect(openAction,SIGNAL(triggered(bool)),this,SLOT(openFile()));
clearAction=new QAction(this);
clearAction->setIcon(QIcon::fromTheme("edit-clear"));
clearAction->setText(tr("Clear"));
fileMenu->addAction(clearAction);
connect(clearAction,SIGNAL(triggered(bool)),canvas,SLOT(clearCloud()));
processAction=new QAction(this);
//procesAction->setIcon(QIcon::fromTheme("edit-clear"));
processAction->setText(tr("Save as LAS"));
fileMenu->addAction(processAction);
connect(processAction,SIGNAL(triggered(bool)),canvas,SLOT(saveFile()));
stopAction=new QAction(this);
stopAction->setIcon(QIcon::fromTheme("process-stop"));
stopAction->setText(tr("Stop"));
stopAction->setEnabled(false);
fileMenu->addAction(stopAction);
connect(stopAction,SIGNAL(triggered(bool)),this,SLOT(stopConversion()));
resumeAction=new QAction(this);
//resumeAction->setIcon(QIcon::fromTheme("edit-clear"));
resumeAction->setText(tr("Resume"));
resumeAction->setEnabled(false);
fileMenu->addAction(resumeAction);
connect(resumeAction,SIGNAL(triggered(bool)),this,SLOT(resumeConversion()));
exitAction=new QAction(this);
exitAction->setIcon(QIcon::fromTheme("application-exit"));
exitAction->setText(tr("Exit"));
fileMenu->addAction(exitAction);
connect(exitAction,SIGNAL(triggered(bool)),this,SLOT(close()));
// View menu
// Settings menu
configureAction=new QAction(this);
configureAction->setIcon(QIcon::fromTheme("configure"));
configureAction->setText(tr("Configure"));
settingsMenu->addAction(configureAction);
connect(configureAction,SIGNAL(triggered(bool)),this,SLOT(configure()));
// Help menu
aboutProgramAction=new QAction(this);
aboutProgramAction->setText(tr("About Wolkenbase"));
helpMenu->addAction(aboutProgramAction);
connect(aboutProgramAction,SIGNAL(triggered(bool)),this,SLOT(aboutProgram()));
aboutQtAction=new QAction(this);
aboutQtAction->setText(tr("About Qt"));
helpMenu->addAction(aboutQtAction);
connect(aboutQtAction,SIGNAL(triggered(bool)),this,SLOT(aboutQt()));
// Toolbar
for (i=0;i<4;i++)
{
unitButtons[i]=new UnitButton(this,conversionFactors[i]);
unitButtons[i]->setText(configDialog->tr(unitNames[i])); // lupdate warns but it works
unitButtons[i]->setIcon(QIcon(unitIconNames[i]));
connect(this,SIGNAL(lengthUnitChanged(double)),unitButtons[i],SLOT(setUnit(double)));
connect(unitButtons[i],SIGNAL(triggered(bool)),unitButtons[i],SLOT(selfTriggered(bool)));
connect(unitButtons[i],SIGNAL(unitChanged(double)),this,SLOT(setUnit(double)));
toolbar->addAction(unitButtons[i]);
}
}
void LasifyWindow::makeStatusBar()
{
statusBar()->addWidget(fileMsg);
statusBar()->addWidget(dotTriangleMsg);
statusBar()->addWidget(memoryMsg);
statusBar()->addWidget(densityMsg);
statusBar()->addWidget(doneBar);
statusBar()->addWidget(busyBar);
statusBar()->show();
}
void LasifyWindow::readSettings()
{
QSettings settings("Bezitopo","Wolkenbase");
resize(settings.value("size",QSize(707,500)).toSize());
move(settings.value("pos",QPoint(0,0)).toPoint());
numberThreads=settings.value("threads",0).toInt();
lengthUnit=1; // convert the file format, not the unit
cloudOutput.pointsPerFile=settings.value("pointsPerFile",0).toInt();
cloudOutput.separateClasses=false; // all points are raw
canvas->tileSize=1; // irrelevant
maxSlope=settings.value("maxSlope",1).toDouble();
thickness=settings.value("thickness",0).toDouble();
minHyperboloidSize=settings.value("minimumHyperboloidSize",0.1).toDouble();
lengthUnitChanged(lengthUnit);
}
void LasifyWindow::writeSettings()
{
QSettings settings("Bezitopo","Wolkenbase");
settings.setValue("size",size());
settings.setValue("pos",pos());
settings.setValue("threads",numberThreads);
settings.setValue("pointsPerFile",cloudOutput.pointsPerFile);
settings.setValue("maxSlope",maxSlope);
settings.setValue("thickness",thickness);
settings.setValue("minimumHyperboloidSize",minHyperboloidSize);
}
void LasifyWindow::setSettings(double lu,int thr,int ppf,bool sc,double ts,double maxsl,double thick,double minhs)
{
lengthUnit=lu;
numberThreads=thr;
cloudOutput.pointsPerFile=ppf;
cloudOutput.separateClasses=sc;
canvas->tileSize=ts;
maxSlope=maxsl;
thickness=thick;
minHyperboloidSize=minhs;
writeSettings();
lengthUnitChanged(lengthUnit);
}
void LasifyWindow::setUnit(double lu)
{
lengthUnit=lu;
lengthUnitChanged(lengthUnit);
}