| 1 |
//########################################################################## |
| 2 |
//# # |
| 3 |
//# CLOUDCOMPARE PLUGIN: qCANUPO # |
| 4 |
//# # |
| 5 |
//# This program is free software; you can redistribute it and/or modify # |
| 6 |
//# it under the terms of the GNU General Public License as published by # |
| 7 |
//# the Free Software Foundation; version 2 or later of the License. # |
| 8 |
//# # |
| 9 |
//# This program is distributed in the hope that it will be useful, # |
| 10 |
//# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 11 |
//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 12 |
//# GNU General Public License for more details. # |
| 13 |
//# # |
| 14 |
//# COPYRIGHT: UEB (UNIVERSITE EUROPEENNE DE BRETAGNE) / CNRS # |
| 15 |
//# # |
| 16 |
//########################################################################## |
| 17 |
|
| 18 |
#include "qCanupoTrainingDialog.h" |
| 19 |
|
| 20 |
//local |
| 21 |
#include "qCanupoTools.h" |
| 22 |
#include "ccPointDescriptor.h" |
| 23 |
|
| 24 |
//qCC_plugins |
| 25 |
#include <ccMainAppInterface.h> |
| 26 |
#include <ccQtHelpers.h> |
| 27 |
|
| 28 |
//qCC_db |
| 29 |
#include <ccPointCloud.h> |
| 30 |
|
| 31 |
//Qt |
| 32 |
#include <QSettings> |
| 33 |
#include <QMainWindow> |
| 34 |
#include <QComboBox> |
| 35 |
#include <QPushButton> |
| 36 |
#include <QApplication> |
| 37 |
#include <QThread> |
| 38 |
|
| 39 |
//system |
| 40 |
#include <limits> |
| 41 |
|
| 42 |
qCanupoTrainingDialog::qCanupoTrainingDialog(ccMainAppInterface* app) |
| 43 |
: QDialog(app ? app->getMainWindow() : nullptr) |
| 44 |
, Ui::CanupoTrainingDialog() |
| 45 |
, m_app(app) |
| 46 |
{ |
| 47 |
setupUi(this); |
| 48 |
|
| 49 |
static const int MaxThreadCount = QThread::idealThreadCount(); |
| 50 |
maxThreadCountSpinBox->setRange(1, MaxThreadCount); |
| 51 |
maxThreadCountSpinBox->setSuffix(QString(" / %1").arg(MaxThreadCount)); |
| 52 |
|
| 53 |
if (m_app) |
| 54 |
{ |
| 55 |
//add list of clouds to the combo-boxes |
| 56 |
ccHObject::Container clouds; |
| 57 |
if (m_app->dbRootObject()) |
| 58 |
m_app->dbRootObject()->filterChildren(clouds,true,CC_TYPES::POINT_CLOUD); |
| 59 |
|
| 60 |
unsigned cloudCount = 0; |
| 61 |
for (size_t i=0; i<clouds.size(); ++i) |
| 62 |
{ |
| 63 |
if (clouds[i]->isA(CC_TYPES::POINT_CLOUD)) //as filterChildren only test 'isKindOf' |
| 64 |
{ |
| 65 |
QString name = qCanupoTools::GetEntityName(clouds[i]); |
| 66 |
QVariant uniqueID(clouds[i]->getUniqueID()); |
| 67 |
originCloudComboBox->addItem(name,uniqueID); |
| 68 |
class1CloudComboBox->addItem(name,uniqueID); |
| 69 |
class2CloudComboBox->addItem(name,uniqueID); |
| 70 |
evaluationCloudComboBox->addItem(name,uniqueID); |
| 71 |
++cloudCount; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
//if 3 clouds are loaded, then there's chances that the first one is the global cloud! |
| 76 |
class1CloudComboBox->setCurrentIndex(cloudCount > 0 ? (cloudCount > 2 ? 1 : 0) : -1); |
| 77 |
class2CloudComboBox->setCurrentIndex(cloudCount > 1 ? (cloudCount > 2 ? 2 : 1) : -1); |
| 78 |
originCloudComboBox->setCurrentIndex(cloudCount > 2 ? 0 : -1); |
| 79 |
|
| 80 |
if (cloudCount < 2 && app) |
| 81 |
app->dispToConsole("You need at least 2 loaded clouds to train a classifier (one per class)",ccMainAppInterface::ERR_CONSOLE_MESSAGE); |
| 82 |
} |
| 83 |
|
| 84 |
//add the list of available descriptors |
| 85 |
{ |
| 86 |
paramComboBox->clear(); |
| 87 |
unsigned count = ScaleParamsComputer::AvailableCount(); |
| 88 |
for (unsigned i=0; i<count; ++i) |
| 89 |
{ |
| 90 |
ScaleParamsComputer* scp = ScaleParamsComputer::GetByIndex(i); |
| 91 |
paramComboBox->addItem(scp->getName(),scp->getID()); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
loadParamsFromPersistentSettings(); |
| 96 |
|
| 97 |
connect(cloud1ClassSpinBox, qOverload<int>(&QSpinBox::valueChanged), this, &qCanupoTrainingDialog::onClassChanged); |
| 98 |
connect(cloud2ClassSpinBox, qOverload<int>(&QSpinBox::valueChanged), this, &qCanupoTrainingDialog::onClassChanged); |
| 99 |
|
| 100 |
connect(class1CloudComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &qCanupoTrainingDialog::onCloudChanged); |
| 101 |
connect(class2CloudComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &qCanupoTrainingDialog::onCloudChanged); |
| 102 |
|
| 103 |
onClassChanged(0); |
| 104 |
onCloudChanged(0); |
| 105 |
} |
| 106 |
|
| 107 |
bool qCanupoTrainingDialog::validParameters() const |
| 108 |
{ |
| 109 |
if (cloud1ClassSpinBox->value() == cloud2ClassSpinBox->value()) |
| 110 |
return false; |
| 111 |
|
| 112 |
int c1 = class1CloudComboBox->currentIndex(); |
| 113 |
int c2 = class2CloudComboBox->currentIndex(); |
| 114 |
if (c1 < 0 || c2 < 0) |
| 115 |
return false; |
| 116 |
if (c1 == c2) |
| 117 |
return false; |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
int qCanupoTrainingDialog::getMaxThreadCount() const |
| 123 |
{ |
| 124 |
return maxThreadCountSpinBox->value(); |
| 125 |
} |
| 126 |
|
| 127 |
bool qCanupoTrainingDialog::getScales(std::vector<float>& scales) const |
| 128 |
{ |
| 129 |
scales.clear(); |
| 130 |
|
| 131 |
try |
| 132 |
{ |
| 133 |
if (scalesRampRadioButton->isChecked()) |
| 134 |
{ |
| 135 |
double maxScale = maxScaleDoubleSpinBox->value(); |
| 136 |
double step = stepScaleDoubleSpinBox->value(); |
| 137 |
double minScale = minScaleDoubleSpinBox->value(); |
| 138 |
if (maxScale < minScale || maxScale < 0 || step < 1.0e-6) |
| 139 |
return false; |
| 140 |
unsigned stepCount = static_cast<unsigned>((maxScale - minScale) / step + 1.0e-6) + 1; //static_cast is equivalent to floor if value >= 0 |
| 141 |
scales.resize(stepCount); |
| 142 |
for (unsigned i=0; i<stepCount; ++i) |
| 143 |
scales[i] = static_cast<float>(maxScale - i*step); |
| 144 |
} |
| 145 |
else if (scalesListRadioButton->isChecked()) |
| 146 |
{ |
| 147 |
QStringList scaleList = scalesListLineEdit->text().split(' ',QString::SkipEmptyParts); |
| 148 |
|
| 149 |
int listSize = scaleList.size(); |
| 150 |
scales.resize(listSize); |
| 151 |
for (int i=0; i<listSize; ++i) |
| 152 |
{ |
| 153 |
bool ok = false; |
| 154 |
float f; |
| 155 |
f = scaleList[i].toFloat(&ok); |
| 156 |
if (!ok) |
| 157 |
return false; |
| 158 |
scales[i] = f; |
| 159 |
} |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
return false; |
| 164 |
} |
| 165 |
} |
| 166 |
catch (const std::bad_alloc&) |
| 167 |
{ |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
unsigned qCanupoTrainingDialog::getDescriptorID() const |
| 175 |
{ |
| 176 |
//paramComboBox |
| 177 |
int currentIndex = paramComboBox->currentIndex(); |
| 178 |
if (currentIndex < 0) |
| 179 |
{ |
| 180 |
assert(false); |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
return paramComboBox->itemData(currentIndex).toUInt(); |
| 185 |
} |
| 186 |
|
| 187 |
void qCanupoTrainingDialog::onClassChanged(int dummy) |
| 188 |
{ |
| 189 |
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(validParameters()); |
| 190 |
} |
| 191 |
|
| 192 |
void qCanupoTrainingDialog::onCloudChanged(int dummy) |
| 193 |
{ |
| 194 |
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(validParameters()); |
| 195 |
} |
| 196 |
|
| 197 |
ccPointCloud* qCanupoTrainingDialog::getClass1Cloud() |
| 198 |
{ |
| 199 |
//return the cloud currently selected in the combox box |
| 200 |
return qCanupoTools::GetCloudFromCombo(class1CloudComboBox, m_app->dbRootObject()); |
| 201 |
} |
| 202 |
|
| 203 |
ccPointCloud* qCanupoTrainingDialog::getClass2Cloud() |
| 204 |
{ |
| 205 |
//return the cloud currently selected in the combox box |
| 206 |
return qCanupoTools::GetCloudFromCombo(class2CloudComboBox, m_app->dbRootObject()); |
| 207 |
} |
| 208 |
|
| 209 |
ccPointCloud* qCanupoTrainingDialog::getOriginPointCloud() |
| 210 |
{ |
| 211 |
//return the cloud currently selected in the combox box |
| 212 |
return useOriginalCloudCheckBox->isChecked() ? qCanupoTools::GetCloudFromCombo(originCloudComboBox, m_app->dbRootObject()) : nullptr; |
| 213 |
} |
| 214 |
|
| 215 |
ccPointCloud* qCanupoTrainingDialog::getEvaluationCloud() |
| 216 |
{ |
| 217 |
//return the cloud currently selected in the combox box |
| 218 |
return evaluateParamsCheckBox->isChecked() ? qCanupoTools::GetCloudFromCombo(evaluationCloudComboBox, m_app->dbRootObject()) : nullptr; |
| 219 |
} |
| 220 |
|
| 221 |
void qCanupoTrainingDialog::loadParamsFromPersistentSettings() |
| 222 |
{ |
| 223 |
QSettings settings("qCanupo"); |
| 224 |
settings.beginGroup("Training"); |
| 225 |
|
| 226 |
//read out parameters |
| 227 |
double minScale = settings.value("MinScale",minScaleDoubleSpinBox->value()).toDouble(); |
| 228 |
double step = settings.value("Step",stepScaleDoubleSpinBox->value()).toDouble(); |
| 229 |
double maxScale = settings.value("MaxScale",maxScaleDoubleSpinBox->value()).toDouble(); |
| 230 |
QString scalesList = settings.value("ScalesList",scalesListLineEdit->text()).toString(); |
| 231 |
bool scalesRampEnabled = settings.value("ScalesRampEnabled",scalesRampRadioButton->isChecked()).toBool(); |
| 232 |
|
| 233 |
unsigned maxPoints = settings.value("MaxPoints",maxPointsSpinBox->value()).toUInt(); |
| 234 |
int classifParam = settings.value("ClassifParam",paramComboBox->currentIndex()).toInt(); |
| 235 |
int maxThreadCount = settings.value("MaxThreadCount", ccQtHelpers::GetMaxThreadCount()).toInt(); |
| 236 |
|
| 237 |
//apply parameters |
| 238 |
|
| 239 |
minScaleDoubleSpinBox->setValue(minScale); |
| 240 |
stepScaleDoubleSpinBox->setValue(step); |
| 241 |
maxScaleDoubleSpinBox->setValue(maxScale); |
| 242 |
scalesListLineEdit->setText(scalesList); |
| 243 |
if (scalesRampEnabled) |
| 244 |
scalesRampRadioButton->setChecked(true); |
| 245 |
else |
| 246 |
scalesListRadioButton->setChecked(true); |
| 247 |
|
| 248 |
maxPointsSpinBox->setValue(maxPoints); |
| 249 |
paramComboBox->setCurrentIndex(classifParam); |
| 250 |
maxThreadCountSpinBox->setValue(maxThreadCount); |
| 251 |
} |
| 252 |
|
| 253 |
void qCanupoTrainingDialog::saveParamsToPersistentSettings() |
| 254 |
{ |
| 255 |
QSettings settings("qCanupo"); |
| 256 |
settings.beginGroup("Training"); |
| 257 |
|
| 258 |
//save parameters |
| 259 |
settings.setValue("MinScale",minScaleDoubleSpinBox->value()); |
| 260 |
settings.setValue("Step",stepScaleDoubleSpinBox->value()); |
| 261 |
settings.setValue("MaxScale",maxScaleDoubleSpinBox->value()); |
| 262 |
settings.setValue("ScalesList",scalesListLineEdit->text()); |
| 263 |
settings.setValue("ScalesRampEnabled",scalesRampRadioButton->isChecked()); |
| 264 |
|
| 265 |
settings.setValue("MaxPoints",maxPointsSpinBox->value()); |
| 266 |
settings.setValue("ClassifParam",paramComboBox->currentIndex()); |
| 267 |
settings.setValue("MaxThreadCount", maxThreadCountSpinBox->value()); |
| 268 |
} |