/
ccGlobalShiftManager.cpp
418 lines (363 loc) · 11.5 KB
/
ccGlobalShiftManager.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
//##########################################################################
//# #
//# CLOUDCOMPARE #
//# #
//# This program 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; version 2 or later of the License. #
//# #
//# This program 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. #
//# #
//# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
//# #
//##########################################################################
//Local
#include "ccGlobalShiftManager.h"
#include "ccShiftAndScaleCloudDlg.h"
//Qt
#include <QCoreApplication>
#include <QFile>
//qCC_db
#include <ccHObject.h>
//System
#include <string.h>
#include <assert.h>
double ccGlobalShiftManager::MAX_COORDINATE_ABS_VALUE = 1.0e4;
double ccGlobalShiftManager::MAX_DIAGONAL_LENGTH = 1.0e6;
// default name for the Global Shift 'bookmarks' file
static QString s_defaultGlobalShiftListFilename("global_shift_list.txt");
// default and last input shift/scale entires (don't use it directly, use GetLast() instead)
static std::vector<ccGlobalShiftManager::ShiftInfo> s_lastInfoBuffer;
const std::vector<ccGlobalShiftManager::ShiftInfo>& ccGlobalShiftManager::GetLast()
{
// the first time this method is called, load the default values from the 'bookmark' files
static bool s_firstTime = true;
if (s_firstTime)
{
assert(s_lastInfoBuffer.empty());
LoadInfoFromFile(QCoreApplication::applicationDirPath() + QString("/") + s_defaultGlobalShiftListFilename, s_lastInfoBuffer);
s_firstTime = false;
}
return s_lastInfoBuffer;
}
static bool IsDefaultShift(const CCVector3d& shift, double scale)
{
return (scale == 1.0 && shift.norm2d() == 0);
}
static bool SameShift(const ccGlobalShiftManager::ShiftInfo& shiftInfo, const CCVector3d& shift, double scale)
{
return ( (shiftInfo.shift - shift).norm() <= CCCoreLib::ZERO_TOLERANCE_D
&& std::abs(shiftInfo.scale - scale) <= CCCoreLib::ZERO_TOLERANCE_D );
}
void ccGlobalShiftManager::StoreShift(const CCVector3d& shift, double scale, bool preserve/*=true*/)
{
if (IsDefaultShift(shift, scale))
{
// default shift and scale are ignored
return;
}
// check if it's already stored
for (const ShiftInfo& shiftInfo : s_lastInfoBuffer)
{
if (SameShift(shiftInfo, shift, scale))
{
//we already know this one
return;
}
}
static unsigned lastInputIndex = 0;
ShiftInfo info("Previous input");
if (lastInputIndex != 0)
{
info.name += QString(" (%1)").arg(lastInputIndex);
}
++lastInputIndex;
info.scale = scale;
info.shift = shift;
info.preserve = preserve;
s_lastInfoBuffer.emplace_back(info);
}
bool ccGlobalShiftManager::NeedShift(const CCVector3d& P)
{
return NeedShift(P.x) || NeedShift(P.y) || NeedShift(P.z);
}
bool ccGlobalShiftManager::NeedShift(double d)
{
return std::abs(d) >= MAX_COORDINATE_ABS_VALUE;
}
bool ccGlobalShiftManager::NeedRescale(double d)
{
return std::abs(d) >= MAX_DIAGONAL_LENGTH;
}
bool ccGlobalShiftManager::Handle( const CCVector3d& P,
double diagonal,
Mode mode,
bool useInputCoordinatesShiftIfPossible,
CCVector3d& coordinatesShift,
bool* _preserveCoordinateShift/*=nullptr*/,
double* _coordinatesScale/*=nullptr*/,
bool* _applyAll/*=nullptr*/)
{
assert(diagonal >= 0.0);
bool preserveCoordinateShift = true;
if (useInputCoordinatesShiftIfPossible && _preserveCoordinateShift)
{
// if shift info was provided as input
preserveCoordinateShift = *_preserveCoordinateShift;
}
if (_applyAll)
{
*_applyAll = false;
}
//default scale
double scale = 1.0;
bool needShift = false;
bool needRescale = false;
// if shift info was provided as input (typically from a previous entity)
if (useInputCoordinatesShiftIfPossible)
{
if (nullptr != _coordinatesScale)
{
// use the input scale if specified
*_coordinatesScale = std::max(*_coordinatesScale, CCCoreLib::ZERO_TOLERANCE_D);
scale = *_coordinatesScale;
}
needShift = NeedShift(P*scale + coordinatesShift);
needRescale = NeedRescale(diagonal*scale);
if (mode == NO_DIALOG)
{
// we can apply the input shift only if it 'works'
if (!needShift && !needRescale)
{
return !IsDefaultShift(coordinatesShift, scale); // if it's the default shift, we don't need to apply anything (= false)
}
else
{
return false; // we can't apply the input shift
}
}
}
else
{
// no shift by default
coordinatesShift = CCVector3d(0, 0, 0);
if (nullptr != _coordinatesScale)
{
*_coordinatesScale = 1.0;
}
if (mode == NO_DIALOG)
{
// it would be a bit strange to call this method with NO_DIALOG and no input shift
// but that's theoretically possible ;)
return false;
}
needShift = NeedShift(P);
needRescale = NeedRescale(diagonal);
if ( !needShift
&& !needRescale
&& mode != ALWAYS_DISPLAY_DIALOG)
{
return false; //no need to apply any shift
}
}
// after this point, we should either determine a new global shift
// or ask the user to review/provide one with a dialog
assert(mode != NO_DIALOG);
// if necessary, we can try with if a previously used shift works
int bestPreviousShiftIndex = -1;
if (needShift || needRescale)
{
preserveCoordinateShift = true; // if we fall here, this means that even the provided input doesn't work
if (mode != NO_DIALOG_AUTO_SHIFT)
{
// at this point, we will display the dialog since the input info doesn't work
mode = ALWAYS_DISPLAY_DIALOG;
}
const std::vector<ShiftInfo>& lastInfoBuffer = ccGlobalShiftManager::GetLast();
// try to find an already used Global Shift that would work
for (size_t i = 0; i < lastInfoBuffer.size(); ++i)
{
const ShiftInfo& shiftInfo = lastInfoBuffer[i];
needShift = NeedShift(P*shiftInfo.scale + shiftInfo.shift);
needRescale = NeedRescale(diagonal*shiftInfo.scale);
if (!needShift && !needRescale)
{
// we found a valid candidate
coordinatesShift = shiftInfo.shift;
scale = shiftInfo.scale;
bestPreviousShiftIndex = static_cast<int>(i);
break;
}
}
}
if (mode == NO_DIALOG_AUTO_SHIFT)
{
// since we can't display a dialog, we'll just use the best shift found so far
if (needShift)
{
coordinatesShift = BestShift(P);
}
if (needRescale)
{
scale = BestScale(diagonal);
}
}
// should we still display the dialog?
else if ( needShift
|| needRescale
|| mode == ALWAYS_DISPLAY_DIALOG)
{
ccShiftAndScaleCloudDlg sasDlg(P, diagonal);
sasDlg.showApplyAllButton(_applyAll != nullptr);
sasDlg.showScaleItems(_coordinatesScale != nullptr);
sasDlg.showWarning(needShift || needRescale);
sasDlg.setPreserveShiftOnSave(preserveCoordinateShift);
sasDlg.showPreserveShiftOnSave(_preserveCoordinateShift != nullptr);
sasDlg.showTitle(needShift || needRescale);
// always add the "suggested" entry
CCVector3d suggestedShift = BestShift(P);
double suggestedScale = BestScale(diagonal);
int index = sasDlg.addShiftInfo(ShiftInfo("Suggested", suggestedShift, suggestedScale));
const std::vector<ShiftInfo>& lastInfoBuffer = ccGlobalShiftManager::GetLast();
// potentially add the input shift if it's different from the others
if ( useInputCoordinatesShiftIfPossible
&& (!needShift && !needRescale)
&& bestPreviousShiftIndex < 0 )
{
// let's check if the solution we found was part of the previous shifts anyway
for (size_t i = 0; i < lastInfoBuffer.size(); ++i)
{
const ShiftInfo& shiftInfo = lastInfoBuffer[i];
if (( shiftInfo.shift - coordinatesShift).norm() <= CCCoreLib::ZERO_TOLERANCE_D
&& std::abs(shiftInfo.scale - scale) <= CCCoreLib::ZERO_TOLERANCE_D )
{
// found the same shift
bestPreviousShiftIndex = static_cast<int>(i);
break;
}
}
if (bestPreviousShiftIndex < 0)
{
index = sasDlg.addShiftInfo(ShiftInfo("Input", coordinatesShift, scale));
}
}
// add the previous entries (if any)
if (!lastInfoBuffer.empty())
{
sasDlg.addShiftInfo(lastInfoBuffer);
if (bestPreviousShiftIndex >= 0)
{
index = bestPreviousShiftIndex + 1; // + 1 because of the 'suggested' shift
}
}
sasDlg.setCurrentProfile(index);
if (!sasDlg.exec())
{
// process cancelled by the user
return false;
}
coordinatesShift = sasDlg.getShift();
if (_coordinatesScale)
{
scale = sasDlg.getScale();
}
if (_preserveCoordinateShift)
{
preserveCoordinateShift = sasDlg.preserveShiftOnSave();
}
if (_applyAll)
{
*_applyAll = sasDlg.applyAll();
}
}
// save info for next time
StoreShift(coordinatesShift, scale, preserveCoordinateShift);
if (_coordinatesScale)
{
*_coordinatesScale = scale;
}
if (_preserveCoordinateShift)
{
*_preserveCoordinateShift = preserveCoordinateShift;
}
return true;
}
CCVector3d ccGlobalShiftManager::BestShift(const CCVector3d& P)
{
if (!NeedShift(P))
{
return CCVector3d(0, 0, 0);
}
CCVector3d shift( std::abs(P[0]) >= MAX_COORDINATE_ABS_VALUE ? -P[0] : 0,
std::abs(P[1]) >= MAX_COORDINATE_ABS_VALUE ? -P[1] : 0,
std::abs(P[2]) >= MAX_COORDINATE_ABS_VALUE ? -P[2] : 0 );
//round-off to the nearest hundred
shift.x = static_cast<int>(shift.x / 100) * 100.0;
shift.y = static_cast<int>(shift.y / 100) * 100.0;
shift.z = static_cast<int>(shift.z / 100) * 100.0;
return shift;
}
double ccGlobalShiftManager::BestScale(double d)
{
return d < MAX_DIAGONAL_LENGTH ? 1.0 : pow(10.0, -static_cast<double>(ceil(log(d / MAX_DIAGONAL_LENGTH))));
}
bool ccGlobalShiftManager::LoadInfoFromFile(QString filename, std::vector<ShiftInfo>& infos)
{
QFile file(filename);
if (!file.open(QFile::Text | QFile::ReadOnly))
return false;
QTextStream stream(&file);
unsigned lineNumber = 0;
while (true)
{
//read next line
QString line = stream.readLine();
if (line.isEmpty())
break;
++lineNumber;
if (line.startsWith("//"))
continue;
//split line in 5 items
QStringList tokens = line.split(";", QString::SkipEmptyParts);
if (tokens.size() != 5)
{
//invalid file
ccLog::Warning(QString("[ccGlobalShiftManager::LoadInfoFromFile] File '%1' is malformed (5 items expected per line)").arg(filename));
return false;
}
//decode items
bool ok = true;
unsigned errors = 0;
ccGlobalShiftManager::ShiftInfo info;
info.name = tokens[0].trimmed();
info.shift.x = tokens[1].toDouble(&ok);
if (!ok) ++errors;
info.shift.y = tokens[2].toDouble(&ok);
if (!ok) ++errors;
info.shift.z = tokens[3].toDouble(&ok);
if (!ok) ++errors;
info.scale = tokens[4].toDouble(&ok);
if (!ok) ++errors;
//process errors
if (errors)
{
//invalid file
ccLog::Warning(QString("[ccGlobalShiftManager::LoadInfoFromFile] File '%1' is malformed (wrong item type encountered on line %2)").arg(filename).arg(lineNumber));
return false;
}
try
{
infos.push_back(info);
}
catch (const std::bad_alloc&)
{
//not enough memory
ccLog::Warning(QString("[ccGlobalShiftManager::LoadInfoFromFile] Not enough memory to read file '%1'").arg(filename));
return false;
}
}
return true;
}