Debian Codegraph @

plugins/core/Standard/qCanupo/src/qCanupoClassifDialog.cpp

raw
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 "qCanupoClassifDialog.h"
19
20 //local
21 #include "classifier.h"
22 #include "qCanupoTools.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 <QFileInfo>
36 #include <QFileDialog>
37 #include <QPushButton>
38 #include <QApplication>
39 #include <QThread>
40
41 qCanupoClassifDialog::qCanupoClassifDialog(ccPointCloud* cloud, ccMainAppInterface* app)
42 : QDialog(app ? app->getMainWindow() : nullptr)
43 , Ui::CanupoClassifDialog()
44 , m_app(app)
45 , m_cloud(cloud)
46 {
47 setupUi(this);
48
49 #ifndef COMPILE_PRIVATE_CANUPO
50 generateRoughnessSFsCheckBox->setVisible(false);
51 #endif
52
53 static const int MaxThreadCount = QThread::idealThreadCount();
54 maxThreadCountSpinBox->setRange(1, MaxThreadCount);
55 maxThreadCountSpinBox->setSuffix(QString(" / %1").arg(MaxThreadCount));
56
57 loadParamsFromPersistentSettings();
58
59 if (cloud)
60 {
61 //check if a the cloud has an active SF!
62 useSFCheckBox->setEnabled(cloud->getCurrentDisplayedScalarField() != nullptr);
63 }
64
65 if (m_app)
66 {
67 //add list of clouds to the combo-boxes
68 ccHObject::Container clouds;
69 if (m_app->dbRootObject())
70 m_app->dbRootObject()->filterChildren(clouds, true, CC_TYPES::POINT_CLOUD);
71
72 for (size_t i = 0; i < clouds.size(); ++i)
73 {
74 if (clouds[i]->isA(CC_TYPES::POINT_CLOUD)) //as filterChildren only test 'isKindOf'
75 cpOtherCloudComboBox->addItem(qCanupoTools::GetEntityName(clouds[i]), QVariant(clouds[i]->getUniqueID()));
76 }
77 }
78
79 connect(browseToolButton, &QAbstractButton::clicked, this, &qCanupoClassifDialog::browseClassifierFile);
80 connect(mscBrowseToolButton, &QAbstractButton::clicked, this, &qCanupoClassifDialog::browseMscFile);
81
82 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
83 }
84
85 double qCanupoClassifDialog::getConfidenceTrehshold() const
86 {
87 if (!useConfThresholdGroupBox->isChecked())
88 return 0.0;
89
90 //confidence threshold between 0 and 1
91 assert(pokDoubleSpinBox->value() >= 0 && pokDoubleSpinBox->value() <= 1.0);
92 return pokDoubleSpinBox->value();
93 }
94
95 bool qCanupoClassifDialog::useSF() const
96 {
97 return useConfThresholdGroupBox->isChecked() && useSFCheckBox->isChecked();
98 }
99
100 int qCanupoClassifDialog::getMaxThreadCount() const
101 {
102 return maxThreadCountSpinBox->value();
103 }
104
105 qCanupoClassifDialog::CORE_CLOUD_SOURCES qCanupoClassifDialog::getCorePointsCloudSource() const
106 {
107 if (cpUseCloudRadioButton->isChecked())
108 {
109 return ORIGINAL;
110 }
111 else if (cpSubsampleRadioButton->isChecked())
112 {
113 return SUBSAMPLED;
114 }
115 else if (cpUseOtherCloudRadioButton->isChecked())
116 {
117 return OTHER;
118 }
119 else if (cpMscFileRadioButton->isChecked())
120 {
121 return MSC_FILE;
122 }
123
124 assert(false);
125 return ORIGINAL;
126 }
127
128 QString qCanupoClassifDialog::getMscFilename() const
129 {
130 return mscFileLineEdit->text();
131 }
132
133 ccPointCloud* qCanupoClassifDialog::getCorePointsCloud()
134 {
135 if (cpUseCloudRadioButton->isChecked())
136 {
137 return m_cloud;
138 }
139 else if (cpUseOtherCloudRadioButton->isChecked())
140 {
141 //return the cloud currently selected in the combox box
142 return qCanupoTools::GetCloudFromCombo(cpOtherCloudComboBox, m_app->dbRootObject());
143 }
144 else
145 {
146 return nullptr;
147 }
148 }
149
150 void qCanupoClassifDialog::loadParamsFromPersistentSettings()
151 {
152 QSettings settings("qCanupo");
153 settings.beginGroup("Classif");
154
155 //read out parameters
156 //double minScale = settings.value("MinScale",minScaleDoubleSpinBox->value()).toDouble();
157 //double step = settings.value("Step",stepScaleDoubleSpinBox->value()).toDouble();
158 //double maxScale = settings.value("MaxScale",maxScaleDoubleSpinBox->value()).toDouble();
159
160 double subsampleRadius = settings.value("SubsampleRadius", cpSubsamplingDoubleSpinBox->value()).toDouble();
161 bool subsampleEnabled = settings.value("SubsampleEnabled", cpSubsampleRadioButton->isChecked()).toBool();
162
163 QString currentPath = settings.value("CurrentPath", QApplication::applicationDirPath()).toString();
164 QString mscCurrentPath = settings.value("MscCurrentPath", QApplication::applicationDirPath()).toString();
165
166 bool useConfThreshold = settings.value("UseConfThreshold", useConfThresholdGroupBox->isChecked()).toBool();
167 double pok = settings.value("Pok", pokDoubleSpinBox->value()).toDouble();
168 bool useSF = settings.value("UseSF", useSFCheckBox->isChecked()).toBool();
169 bool additionalSF = settings.value("AdditionalSF", generateAdditionalSFsCheckBox->isChecked()).toBool();
170 bool roughnessSF = settings.value("RoughnessSF", generateRoughnessSFsCheckBox->isChecked()).toBool();
171 int maxThreadCount = settings.value("MaxThreadCount", ccQtHelpers::GetMaxThreadCount()).toInt(); // always leave one thread/core to let the application breath
172
173 //apply parameters
174
175 //minScaleDoubleSpinBox->setValue(minScale);
176 //stepScaleDoubleSpinBox->setValue(step);
177 //maxScaleDoubleSpinBox->setValue(maxScale);
178
179 cpSubsamplingDoubleSpinBox->setValue(subsampleRadius);
180 if (subsampleEnabled)
181 cpSubsampleRadioButton->setChecked(true);
182
183 classifFileLineEdit->setText(currentPath);
184 mscFileLineEdit->setText(mscCurrentPath);
185
186 useConfThresholdGroupBox->setChecked(useConfThreshold);
187 pokDoubleSpinBox->setValue(pok);
188 useSFCheckBox->setChecked(useSF);
189 generateAdditionalSFsCheckBox->setChecked(additionalSF);
190 generateRoughnessSFsCheckBox->setChecked(roughnessSF);
191 maxThreadCountSpinBox->setValue(maxThreadCount);
192 }
193
194 void qCanupoClassifDialog::saveParamsToPersistentSettings()
195 {
196 QSettings settings("qCanupo");
197 settings.beginGroup("Classif");
198
199 //save parameters
200 //settings.setValue("MinScale",minScaleDoubleSpinBox->value());
201 //settings.setValue("Step",stepScaleDoubleSpinBox->value());
202 //settings.setValue("MaxScale",maxScaleDoubleSpinBox->value());
203
204 settings.setValue("SubsampleRadius", cpSubsamplingDoubleSpinBox->value());
205 settings.setValue("SubsampleEnabled", cpSubsampleRadioButton->isChecked());
206
207 QString currentPath = QFileInfo(classifFileLineEdit->text()).absolutePath();
208 settings.setValue("CurrentPath", currentPath);
209
210 settings.setValue("UseConfThreshold", useConfThresholdGroupBox->isChecked());
211 settings.setValue("Pok", pokDoubleSpinBox->value());
212 settings.setValue("UseSF", useSFCheckBox->isChecked());
213 settings.setValue("AdditionalSF", generateAdditionalSFsCheckBox->isChecked());
214 settings.setValue("RoughnessSF", generateRoughnessSFsCheckBox->isChecked());
215
216 settings.setValue("MaxThreadCount", maxThreadCountSpinBox->value());
217 }
218
219 void qCanupoClassifDialog::browseMscFile()
220 {
221 //select file to open
222 QSettings settings("qCanupo");
223 settings.beginGroup("Classif");
224 QString currentPath = settings.value("MscCurrentPath", mscFileLineEdit->text()).toString();
225
226 QString filename = QFileDialog::getOpenFileName(this, "Load MSC file", currentPath, "*.msc");
227 if (filename.isEmpty())
228 return;
229
230 //we update current file path
231 mscFileLineEdit->setText(filename);
232 currentPath = QFileInfo(filename).absolutePath();
233 settings.setValue("MscCurrentPath", currentPath);
234 }
235
236 void qCanupoClassifDialog::browseClassifierFile()
237 {
238 //select file to open
239 QString filename;
240 QSettings settings("qCanupo");
241 settings.beginGroup("Classif");
242 QString currentPath = settings.value("CurrentPath", classifFileLineEdit->text()).toString();
243
244 filename = QFileDialog::getOpenFileName(this, "Load classifier file", currentPath, "*.prm");
245 if (filename.isEmpty())
246 return;
247
248 //we update current file path
249 classifFileLineEdit->setText(filename);
250 currentPath = QFileInfo(filename).absolutePath();
251 settings.setValue("CurrentPath", currentPath);
252
253 //and we try to load the file header (to display some info)
254 std::vector<Classifier> classifiers;
255 std::vector<PointCoordinateType> scales;
256 QString error;
257 Classifier::FileHeader header;
258 if (Classifier::Load(filename, classifiers, scales, error, &header, true))
259 {
260 //display classifier file description
261 QStringList fileDesc;
262 fileDesc << QString("File: %1").arg(QFileInfo(filename).fileName());
263 fileDesc << QString("Classifier(s) in file: %1").arg(header.classifierCount);
264
265 assert(header.descID != 0);
266 ScaleParamsComputer* computer = ScaleParamsComputer::GetByID(header.descID);
267 QString descriptorName = computer ? computer->getName() : QString("INVALID");
268 fileDesc << QString("Descriptor ID: %1 (%2)").arg(header.descID).arg(descriptorName);
269
270 fileDesc << QString("Dimensions per scale: %1").arg(header.dimPerScale);
271 fileDesc << QString("Scales: %1").arg(scales.size());
272 QString scalesStr(" [ ");
273 for (size_t i = 0; i < scales.size(); ++i)
274 scalesStr.append(QString("%1 ").arg(scales[scales.size() - 1 - i])); //reverse order as scales are sorted from biggest to smallest
275 scalesStr.append("]");
276 fileDesc << scalesStr;
277
278 classifInfoTextEdit->setText(fileDesc.join("\n"));
279
280 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
281 }
282 else
283 {
284 classifInfoTextEdit->setText("Error: failed to open/read the input file");
285 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
286 }
287 }