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 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
//##########################################################################
//# #
//# 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) #
//# #
//##########################################################################
#include "ccShiftAndScaleCloudDlg.h"
//GUIs generated by Qt Designer
#include <ui_globalShiftAndScaleDlg.h>
#include <ui_globalShiftAndScaleAboutDlg.h>
//Local
#include "ccGlobalShiftManager.h"
//Qt
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QPushButton>
#include <QTextStream>
#include <QStringList>
//qCC_db
#include <ccLog.h>
//system
#include <float.h>
#include <assert.h>
//default name for the Global Shift List file
static QString s_defaultGlobalShiftListFilename("global_shift_list.txt");
ccShiftAndScaleCloudDlg::ccShiftAndScaleCloudDlg(const CCVector3d& Pg,
double Dg/*=0*/,
QWidget* parent/*=0*/)
: QDialog(parent)
, m_ui(0)
, m_applyAll(false)
, m_cancel(false)
, m_activeInfoIndex(-1)
, m_originalPoint(Pg)
, m_originalDiagonal(Dg)
, m_localPoint(0, 0, 0)
, m_localDiagonal(-1.0)
, m_reversedMode(false)
{
init();
showWarning(false);
showKeepGlobalPosCheckbox(false);
showPreserveShiftOnSave(true);
showScaleItems(m_originalDiagonal > 0.0);
showCancelButton(false);
}
ccShiftAndScaleCloudDlg::ccShiftAndScaleCloudDlg( const CCVector3d& Pl,
double Dl,
const CCVector3d& Pg,
double Dg,
QWidget* parent/*=0*/)
: QDialog(parent)
, m_ui(0)
, m_applyAll(false)
, m_cancel(false)
, m_activeInfoIndex(-1)
, m_originalPoint(Pg)
, m_originalDiagonal(Dg)
, m_localPoint(Pl)
, m_localDiagonal(Dl)
, m_reversedMode(true)
{
init();
showWarning(false);
showTitle(false);
showKeepGlobalPosCheckbox(true);
showPreserveShiftOnSave(false);
showScaleItems(m_originalDiagonal > 0.0 && m_localDiagonal > 0.0);
showCancelButton(true);
//to update the GUI accordingly
onGlobalPosCheckBoxToggled(m_ui->keepGlobalPosCheckBox->isChecked());
}
ccShiftAndScaleCloudDlg::~ccShiftAndScaleCloudDlg()
{
if (m_ui)
{
delete m_ui;
m_ui = 0;
}
}
void ccShiftAndScaleCloudDlg::setShiftFieldsPrecision(int precision)
{
m_ui->shiftX->setDecimals(precision);
m_ui->shiftY->setDecimals(precision);
m_ui->shiftZ->setDecimals(precision);
}
void ccShiftAndScaleCloudDlg::init()
{
//should be called once and only once!
if (m_ui)
{
assert(false);
return;
}
m_ui = new Ui_GlobalShiftAndScaleDlg;
m_ui->setupUi(this);
//DGM: we sometimes need to input values > 1.0e9 (for georeferenced clouds expressed in mm!)
m_ui->shiftX->setRange(-1.0e12, 1.0e12);
m_ui->shiftY->setRange(-1.0e12, 1.0e12);
m_ui->shiftZ->setRange(-1.0e12, 1.0e12);
updateGlobalAndLocalSystems();
connect(m_ui->loadComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onLoadIndexChanged(int)));
connect(m_ui->moreInfoToolButton, SIGNAL(clicked()), this, SLOT(displayMoreInfo()));
connect(m_ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(onClick(QAbstractButton*)));
connect(m_ui->shiftX, SIGNAL(valueChanged(double)), this, SLOT(updateGlobalAndLocalSystems()));
connect(m_ui->shiftY, SIGNAL(valueChanged(double)), this, SLOT(updateGlobalAndLocalSystems()));
connect(m_ui->shiftZ, SIGNAL(valueChanged(double)), this, SLOT(updateGlobalAndLocalSystems()));
connect(m_ui->scaleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(updateGlobalAndLocalSystems()));
connect(m_ui->keepGlobalPosCheckBox, SIGNAL(toggled(bool)), this, SLOT(onGlobalPosCheckBoxToggled(bool)));
}
void ccShiftAndScaleCloudDlg::displayMoreInfo()
{
QDialog dlg(this);
Ui_GlobalShiftAndScaleAboutDlg uiDlg;
uiDlg.setupUi(&dlg);
dlg.exec();
}
bool ccShiftAndScaleCloudDlg::addFileInfo()
{
//try to load the 'global_shift_list.txt" file
return loadInfoFromFile(QDir::currentPath() + QString("/")+ s_defaultGlobalShiftListFilename);
}
bool ccShiftAndScaleCloudDlg::loadInfoFromFile(QString filename)
{
QFile file(filename);
if (!file.open(QFile::Text | QFile::ReadOnly))
return false;
size_t originalSize = m_defaultInfos.size();
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("[ccShiftAndScaleCloudDlg::loadInfoFromFile] File '%1' is malformed (5 items expected per line)").arg(filename));
m_defaultInfos.resize(originalSize);
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("[ccShiftAndScaleCloudDlg::loadInfoFromFile] File '%1' is malformed (wrong item type encountered on line %2)").arg(filename).arg(lineNumber));
m_defaultInfos.resize(originalSize);
return false;
}
try
{
m_defaultInfos.push_back(info);
}
catch (const std::bad_alloc&)
{
//not enough memory
ccLog::Warning(QString("[ccShiftAndScaleCloudDlg::loadInfoFromFile] Not enough memory to read file '%1'").arg(filename));
m_defaultInfos.resize(originalSize);
return false;
}
}
//now add the new entries in the combo-box
for (size_t i = originalSize; i < m_defaultInfos.size(); ++i)
{
m_ui->loadComboBox->addItem(m_defaultInfos[i].name);
}
m_ui->loadComboBox->setEnabled(m_defaultInfos.size() >= 2);
return true;
}
void ccShiftAndScaleCloudDlg::updateGlobalAndLocalSystems()
{
updateGlobalSystem();
updateLocalSystem();
}
bool AlmostEq(double a, double b)
{
qint64 ai = static_cast<qint64>(a * 100.0);
qint64 bi = static_cast<qint64>(b * 100.0);
return ai == bi;
}
void ccShiftAndScaleCloudDlg::updateGlobalSystem()
{
CCVector3d P = m_originalPoint;
double diag = m_originalDiagonal;
if (m_reversedMode && !keepGlobalPos())
{
P = (m_localPoint - getShift()) / getScale();
diag = m_localDiagonal / getScale();
}
m_ui->xOriginLabel->setText(QString("x = %1").arg(P.x, 0, 'f'));
m_ui->xOriginLabel->setStyleSheet(AlmostEq(P.x, m_originalPoint.x) ? QString() : QString("color: purple;"));
m_ui->yOriginLabel->setText(QString("y = %1").arg(P.y, 0, 'f'));
m_ui->yOriginLabel->setStyleSheet(AlmostEq(P.y, m_originalPoint.y) ? QString() : QString("color: purple;"));
m_ui->zOriginLabel->setText(QString("z = %1").arg(P.z, 0, 'f'));
m_ui->zOriginLabel->setStyleSheet(AlmostEq(P.z, m_originalPoint.z) ? QString() : QString("color: purple;"));
m_ui->diagOriginLabel->setText(QString("diagonal = %1").arg(diag, 0, 'f'));
m_ui->diagOriginLabel->setStyleSheet(AlmostEq(diag, m_originalDiagonal) ? QString() : QString("color: purple;"));
}
void ccShiftAndScaleCloudDlg::updateLocalSystem()
{
CCVector3d localPoint = m_localPoint;
double localDiagonal = m_localDiagonal;
if (!m_reversedMode || keepGlobalPos())
{
localPoint = (m_originalPoint + getShift()) * getScale();
localDiagonal = m_originalDiagonal * getScale();
}
//adaptive precision
double maxCoord = std::max(fabs(localPoint.x), fabs(localPoint.y));
maxCoord = std::max(fabs(localPoint.z), maxCoord);
int digitsBeforeDec = static_cast<int>(floor(log10(maxCoord))) + 1;
int prec = std::max(0, 8 - digitsBeforeDec);
m_ui->xDestLabel->setText(QString("x = %1").arg(localPoint.x, 0, 'f', prec));
m_ui->xDestLabel->setStyleSheet(ccGlobalShiftManager::NeedShift(localPoint.x) ? QString("color: red;") : QString());
m_ui->yDestLabel->setText(QString("y = %1").arg(localPoint.y, 0, 'f', prec));
m_ui->yDestLabel->setStyleSheet(ccGlobalShiftManager::NeedShift(localPoint.y) ? QString("color: red;") : QString());
m_ui->zDestLabel->setText(QString("z = %1").arg(localPoint.z, 0, 'f', prec));
m_ui->zDestLabel->setStyleSheet(ccGlobalShiftManager::NeedShift(localPoint.z) ? QString("color: red;") : QString());
m_ui->diagDestLabel->setText(QString("diagonal = %1").arg(localDiagonal, 0, 'f', prec));
m_ui->diagDestLabel->setStyleSheet(ccGlobalShiftManager::NeedRescale(localDiagonal) ? QString("color: red;") : QString());
}
void ccShiftAndScaleCloudDlg::setShift(const CCVector3d& shift)
{
m_ui->shiftX->setValue(shift.x);
m_ui->shiftY->setValue(shift.y);
m_ui->shiftZ->setValue(shift.z);
}
CCVector3d ccShiftAndScaleCloudDlg::getShift() const
{
return CCVector3d( m_ui->shiftX->value(), m_ui->shiftY->value(), m_ui->shiftZ->value() );
}
void ccShiftAndScaleCloudDlg::setScale(double scale)
{
m_ui->scaleSpinBox->setValue(scale);
}
double ccShiftAndScaleCloudDlg::getScale() const
{
return m_ui->scaleSpinBox->value();
}
void ccShiftAndScaleCloudDlg::showScaleItems(bool state)
{
m_ui->diagOriginLabel->setVisible(state);
m_ui->diagDestLabel->setVisible(state);
//m_ui->scaleFrame->setVisible(state);
}
void ccShiftAndScaleCloudDlg::showApplyAllButton(bool state)
{
m_ui->buttonBox->button(QDialogButtonBox::YesToAll)->setVisible(state);
}
void ccShiftAndScaleCloudDlg::showApplyButton(bool state)
{
m_ui->buttonBox->button(QDialogButtonBox::Yes)->setVisible(state);
}
void ccShiftAndScaleCloudDlg::showNoButton(bool state)
{
m_ui->buttonBox->button(QDialogButtonBox::No)->setVisible(state);
}
void ccShiftAndScaleCloudDlg::showCancelButton(bool state)
{
m_ui->buttonBox->button(QDialogButtonBox::Cancel)->setVisible(state);
}
void ccShiftAndScaleCloudDlg::showWarning(bool state)
{
m_ui->warningLabel->setVisible(state);
}
void ccShiftAndScaleCloudDlg::showTitle(bool state)
{
m_ui->titleFrame->setVisible(state);
}
void ccShiftAndScaleCloudDlg::showKeepGlobalPosCheckbox(bool state)
{
m_ui->keepGlobalPosCheckBox->setVisible(state);
}
void ccShiftAndScaleCloudDlg::showPreserveShiftOnSave(bool state)
{
m_ui->preserveShiftOnSaveCheckBox->setVisible(state);
}
bool ccShiftAndScaleCloudDlg::preserveShiftOnSave() const
{
return m_ui->preserveShiftOnSaveCheckBox->isChecked();
}
void ccShiftAndScaleCloudDlg::setPreserveShiftOnSave(bool state)
{
m_ui->preserveShiftOnSaveCheckBox->setChecked(state);
}
bool ccShiftAndScaleCloudDlg::keepGlobalPos() const
{
return m_ui->keepGlobalPosCheckBox->isChecked();
}
void ccShiftAndScaleCloudDlg::setKeepGlobalPos(bool state)
{
m_ui->keepGlobalPosCheckBox->setChecked(state);
}
void ccShiftAndScaleCloudDlg::onGlobalPosCheckBoxToggled(bool state)
{
//set the thickest border to the point that will be modified
m_ui->smallCubeFrame->setLineWidth(state ? 2 : 1);
m_ui->bigCubeFrame->setLineWidth(state ? 1 : 2);
updateGlobalSystem();
updateLocalSystem();
}
void ccShiftAndScaleCloudDlg::onClick(QAbstractButton* button)
{
m_applyAll = (button == m_ui->buttonBox->button(QDialogButtonBox::YesToAll));
m_cancel = (button == m_ui->buttonBox->button(QDialogButtonBox::Cancel));
}
void ccShiftAndScaleCloudDlg::onLoadIndexChanged(int index)
{
if (index < 0 || index >= static_cast<int>(m_defaultInfos.size()))
return;
setShift(m_defaultInfos[index].shift);
if (m_ui->scaleSpinBox->isVisible())
setScale(m_defaultInfos[index].scale);
}
bool ccShiftAndScaleCloudDlg::getInfo(size_t index, ccGlobalShiftManager::ShiftInfo& info) const
{
if (index >= m_defaultInfos.size())
return false;
info = m_defaultInfos[index];
return true;
}
void ccShiftAndScaleCloudDlg::setCurrentProfile(int index)
{
if (index >= 0 && index < static_cast<int>(m_defaultInfos.size()))
{
m_ui->loadComboBox->setCurrentIndex(index);
}
}
int ccShiftAndScaleCloudDlg::addShiftInfo(const ccGlobalShiftManager::ShiftInfo& info)
{
try
{
m_defaultInfos.push_back(info);
}
catch (const std::bad_alloc&)
{
//not enough memory
return -1;
}
m_ui->loadComboBox->addItem(m_defaultInfos.back().name);
m_ui->loadComboBox->setEnabled(m_defaultInfos.size() >= 2);
return static_cast<int>(m_defaultInfos.size()) - 1;
}
int ccShiftAndScaleCloudDlg::addShiftInfo(const std::vector<ccGlobalShiftManager::ShiftInfo>& infos)
{
for (const ccGlobalShiftManager::ShiftInfo& info : infos)
{
if (addShiftInfo(info) < 0)
break;
}
return static_cast<int>(m_defaultInfos.size()) - 1;
}
|