File: ccSubsamplingDlg.cpp

package info (click to toggle)
cloudcompare 2.11.3-7.1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 58,224 kB
  • sloc: cpp: 229,982; ansic: 30,723; makefile: 84; sh: 20
file content (274 lines) | stat: -rw-r--r-- 8,756 bytes parent folder | download
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
//##########################################################################
//#                                                                        #
//#                              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 "ccSubsamplingDlg.h"

//CCLib
#include <DgmOctree.h>
#include <ReferenceCloud.h>
#include <CloudSamplingTools.h>
#include <GeometricalAnalysisTools.h>
#include <ScalarField.h>

//qCC_db
#include <ccLog.h>
#include <ccGenericPointCloud.h>
#include <ccOctree.h>

//Exponent of the 'log' scale used for 'SPACE' interval
static const double SPACE_RANGE_EXPONENT = 0.05;

ccSubsamplingDlg::ccSubsamplingDlg(unsigned maxPointCount, double maxCloudRadius, QWidget* parent/*=0*/)
	: QDialog(parent, Qt::Tool)
	, Ui::SubsamplingDialog()
	, m_maxPointCount(maxPointCount)
	, m_maxRadius(maxCloudRadius)
	, m_sfModEnabled(false)
	, m_sfMin(0)
	, m_sfMax(0)
{
	setupUi(this);

	samplingMethod->addItem("Random");
	samplingMethod->addItem("Space");
	samplingMethod->addItem("Octree");

	connect(slider, &QSlider::sliderMoved, this, &ccSubsamplingDlg::sliderMoved);
	connect(samplingValue,  static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), this, &ccSubsamplingDlg::samplingRateChanged);
	connect(samplingMethod, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),		  this, &ccSubsamplingDlg::changeSamplingMethod);

	samplingMethod->setCurrentIndex(1);
	sliderMoved(slider->sliderPosition());
}

CCLib::ReferenceCloud* ccSubsamplingDlg::getSampledCloud(ccGenericPointCloud* cloud, CCLib::GenericProgressCallback* progressCb/*=0*/)
{
	if (!cloud || cloud->size() == 0)
	{
		ccLog::Warning("[ccSubsamplingDlg::getSampledCloud] Invalid input cloud!");
		return nullptr;
	}

	switch (samplingMethod->currentIndex())
	{
	case RANDOM:
		{
			assert(samplingValue->value() >= 0);
			unsigned count = static_cast<unsigned>(samplingValue->value());
			return CCLib::CloudSamplingTools::subsampleCloudRandomly(	cloud,
																		count,
																		progressCb);
		}
		break;

	case SPACE:
		{
			ccOctree::Shared octree = cloud->getOctree();
			if (!octree)
				octree = cloud->computeOctree(progressCb);
			if (octree)
			{
				PointCoordinateType minDist = static_cast<PointCoordinateType>(samplingValue->value());
				CCLib::CloudSamplingTools::SFModulationParams modParams;
				modParams.enabled = sfGroupBox->isEnabled() && sfGroupBox->isChecked();
				if (modParams.enabled)
				{
					double deltaSF = static_cast<double>(m_sfMax) - m_sfMin;
					assert(deltaSF >= 0);
					if (deltaSF > ZERO_TOLERANCE)
					{
						double sfMinSpacing = minSFSpacingDoubleSpinBox->value();
						double sfMaxSpacing = maxSFSpacingDoubleSpinBox->value();
						modParams.a = (sfMaxSpacing - sfMinSpacing) / deltaSF;
						modParams.b = sfMinSpacing - modParams.a * m_sfMin;
					}
					else
					{
						modParams.a = 0.0;
						modParams.b = m_sfMin;
					}
				}
				return CCLib::CloudSamplingTools::resampleCloudSpatially(	cloud, 
																			minDist,
																			modParams,
																			octree.data(),
																			progressCb);
			}
			else
			{
				ccLog::Warning(QString("[ccSubsamplingDlg::getSampledCloud] Failed to compute octree for cloud '%1'").arg(cloud->getName()));
			}
		}
		break;

	case OCTREE:
		{
			ccOctree::Shared octree = cloud->getOctree();
			if (!octree)
				octree = cloud->computeOctree(progressCb);
			if (octree)
			{
				assert(samplingValue->value() >= 0);
				unsigned char level = static_cast<unsigned char>(samplingValue->value());
				return CCLib::CloudSamplingTools::subsampleCloudWithOctreeAtLevel(	cloud,
																					level,
																					CCLib::CloudSamplingTools::NEAREST_POINT_TO_CELL_CENTER,
																					progressCb,
																					octree.data());
			}
			else
			{
				ccLog::Warning(QString("[ccSubsamplingDlg::getSampledCloud] Failed to compute octree for cloud '%1'").arg(cloud->getName()));
			}
		}
		break;
	}

	//something went wrong!
	return nullptr;
}

void ccSubsamplingDlg::updateLabels()
{
	switch(samplingMethod->currentIndex())
	{
	case RANDOM:
		labelSliderMin->setText("none");
		labelSliderMax->setText("all");
		valueLabel->setText("remaining points");
		break;
	case SPACE:
		labelSliderMin->setText("large");
		labelSliderMax->setText("small");
		valueLabel->setText("min. space between points");
		break;
	case OCTREE:
		labelSliderMin->setText("min");
		labelSliderMax->setText("max");
		valueLabel->setText("subdivision level");
		break;
	default:
		break;
	}
}

void ccSubsamplingDlg::sliderMoved(int sliderPos)
{
	double sliderRange = static_cast<double>(slider->maximum())-slider->minimum();
	double rate = static_cast<double>(sliderPos)/sliderRange;
	if (samplingMethod->currentIndex() == SPACE)
	{
		rate = pow(rate, SPACE_RANGE_EXPONENT);
		rate = 1.0 - rate;
	}

	double valueRange = static_cast<double>(samplingValue->maximum()-samplingValue->minimum());
	samplingValue->setValue(samplingValue->minimum() + rate * valueRange);
}

void ccSubsamplingDlg::samplingRateChanged(double value)
{
	double valueRange = static_cast<double>(samplingValue->maximum()-samplingValue->minimum());
	double rate = static_cast<double>(value-samplingValue->minimum())/valueRange;

	CC_SUBSAMPLING_METHOD method = static_cast<CC_SUBSAMPLING_METHOD>(samplingMethod->currentIndex());
	if (method == SPACE)
	{
		rate = 1.0 - rate;
		rate = pow(rate, 1.0/SPACE_RANGE_EXPONENT);

		if (m_sfModEnabled && !sfGroupBox->isChecked())
		{
			minSFSpacingDoubleSpinBox->setValue(value);
			maxSFSpacingDoubleSpinBox->setValue(value);
		}
	}

	slider->blockSignals(true);
	double sliderRange = static_cast<double>(slider->maximum())-slider->minimum();
	slider->setSliderPosition(slider->minimum() + static_cast<int>(rate * sliderRange));
	slider->blockSignals(false);
}

void ccSubsamplingDlg::changeSamplingMethod(int index)
{
	int oldSliderPos = slider->sliderPosition();
	sfGroupBox->setEnabled(false);

	//update the labels
	samplingValue->blockSignals(true);
	switch(index)
	{
	case RANDOM:
		{
			samplingValue->setDecimals(0);
			samplingValue->setMinimum(1);
			samplingValue->setMaximum(static_cast<double>(m_maxPointCount));
			samplingValue->setSingleStep(1);
			samplingValue->setEnabled(true);
		}
		break;
	case SPACE:
		{
			samplingValue->setDecimals(4);
			samplingValue->setMinimum(0.0);
			samplingValue->setMaximum(m_maxRadius);
			double step = m_maxRadius / 1000.0;
			samplingValue->setSingleStep(step);
			minSFSpacingDoubleSpinBox->setMaximum(m_maxRadius);
			minSFSpacingDoubleSpinBox->setSingleStep(step);
			maxSFSpacingDoubleSpinBox->setMaximum(m_maxRadius);
			maxSFSpacingDoubleSpinBox->setSingleStep(step);
			sfGroupBox->setEnabled(m_sfModEnabled);
			samplingValue->setDisabled(sfGroupBox->isEnabled() && sfGroupBox->isChecked());
		}
		break;
	case OCTREE:
		{
			samplingValue->setDecimals(0);
			samplingValue->setMinimum(1);
			samplingValue->setMaximum(static_cast<double>(CCLib::DgmOctree::MAX_OCTREE_LEVEL));
			samplingValue->setSingleStep(1);
			samplingValue->setEnabled(true);
		}
		break;
	default:
		break;
	}
	samplingValue->blockSignals(false);

	updateLabels();
	//slider->setSliderPosition(oldSliderPos);
	sliderMoved(oldSliderPos);
}

void ccSubsamplingDlg::enableSFModulation(ScalarType sfMin, ScalarType sfMax)
{
	m_sfModEnabled = CCLib::ScalarField::ValidValue(sfMin) && CCLib::ScalarField::ValidValue(sfMax);
	if (!m_sfModEnabled)
	{
		ccLog::Warning("[ccSubsamplingDlg::enableSFModulation] Invalid input SF values");
		return;
	}

	m_sfMin = sfMin;
	m_sfMax = sfMax;

	sfGroupBox->setEnabled(samplingMethod->currentIndex() == SPACE);
	minSFlabel->setText(QString::number(sfMin));
	maxSFlabel->setText(QString::number(sfMax));
}