File: ccScalarFieldArithmeticsDlg.cpp

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (434 lines) | stat: -rw-r--r-- 11,475 bytes parent folder | download | duplicates (2)
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
//##########################################################################
//#                                                                        #
//#                              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 "ccScalarFieldArithmeticsDlg.h"

//Qt
#include <QPushButton>
#include <QMessageBox>

//qCC_db
#include <ccPointCloud.h>
#include <ccScalarField.h>

//system
#include <assert.h>
#ifdef _MSC_VER
#include <windows.h>
#endif
#include <cmath>

//number of valid operations
static const unsigned s_opCount = 18;
//operation names
static const char s_opNames[s_opCount][12] = {"add", "sub", "mult", "div", "sqrt", "pow2", "pow3", "exp", "log", "log10", "cos", "sin", "tan", "acos", "asin", "atan", "int", "inverse" };

//semi persitent
static int s_previouslySelectedOperationIndex = 1;
static bool s_applyInPlace = false;
static double s_previousConstValue = 1.0;

ccScalarFieldArithmeticsDlg::ccScalarFieldArithmeticsDlg(	ccPointCloud* cloud,
															QWidget* parent/*=0*/)
	: QDialog(parent, Qt::Tool)
	, Ui::SFArithmeticsDlg()
{
	assert(cloud);

	setupUi(this);

	QStringList sfLabels;
	unsigned sfCount = cloud ? cloud->getNumberOfScalarFields() : 0;
	if (sfCount < 1)
	{
		sf1ComboBox->setEnabled(false);
		sf2ComboBox->setEnabled(false);
		buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
	}
	else
	{
		for (unsigned i=0; i<sfCount; ++i)
		{
			sfLabels << QString(cloud->getScalarFieldName(i));
		}

		sf1ComboBox->addItems(sfLabels);
		sf1ComboBox->setCurrentIndex(0);

		sfLabels << "[Constant value]";
		sf2ComboBox->addItems(sfLabels);
		sf2ComboBox->setCurrentIndex(std::min<unsigned>(1,sfCount-1));
	}

	//connect signals/slots
	connect(operationComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onOperationIndexChanged(int)));
	connect(sf2ComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onSF2IndexChanged(int)));
	
	operationComboBox->setCurrentIndex(s_previouslySelectedOperationIndex);
	constantDoubleSpinBox->setValue(s_previousConstValue);
	updateSF1CheckBox->setChecked(s_applyInPlace);
}

void ccScalarFieldArithmeticsDlg::onOperationIndexChanged(int index)
{
	sf2ComboBox->setEnabled(index <= DIVIDE); //only the 4 first operations are	applied with 2 SFs
}

void ccScalarFieldArithmeticsDlg::onSF2IndexChanged(int index)
{
	//the last element is always the 'constant' field
	constantDoubleSpinBox->setEnabled(sf2ComboBox->currentIndex()+1 == sf2ComboBox->count());
}

int ccScalarFieldArithmeticsDlg::getSF1Index()
{
	return sf1ComboBox->currentIndex();
}

int ccScalarFieldArithmeticsDlg::getSF2Index()
{
	return sf2ComboBox->currentIndex();
}

ccScalarFieldArithmeticsDlg::Operation ccScalarFieldArithmeticsDlg::getOperation() const
{
	int opIndex = operationComboBox->currentIndex();
	if (opIndex < s_opCount)
	{
		return static_cast<ccScalarFieldArithmeticsDlg::Operation>(opIndex);
	}
	else
	{
		assert(false);
		return INVALID;
	}
}

QString ccScalarFieldArithmeticsDlg::getOperationName(QString sf1, QString sf2/*=QString()*/) const
{
	Operation op = getOperation();
	return GetOperationName(op,sf1,sf2);
}

ccScalarFieldArithmeticsDlg::Operation ccScalarFieldArithmeticsDlg::GetOperationByName(QString name)
{
	name = name.toUpper();

	//test all known names...
	for (unsigned i=0; i<s_opCount; ++i)
	{
		if (name == QString(s_opNames[i]).toUpper())
		{
			return static_cast<ccScalarFieldArithmeticsDlg::Operation>(i);
		}
	}

	return INVALID;
}

QString ccScalarFieldArithmeticsDlg::GetOperationName(Operation op, QString sf1, QString sf2/*=QString()*/)
{
	switch (op)
	{
	case PLUS:
		return QString("%1 + %2").arg(sf1, sf2);
	case MINUS:
		return QString("%1 - %2").arg(sf1, sf2);
	case MULTIPLY:
		return QString("%1 * %2").arg(sf1, sf2);
	case DIVIDE:
		return QString("%1 / %2").arg(sf1, sf2);
	default:
		if (op != INVALID)
			return QString("%1(%2)").arg(s_opNames[op], sf1);
		else
			assert(false);
		break;
	}

	return QString();
}

bool ccScalarFieldArithmeticsDlg::apply(ccPointCloud* cloud)
{
	Operation op = getOperation();
	int sf1Idx = getSF1Index();
	int sf2Idx = getSF2Index();

	//save persistent parameters
	s_previouslySelectedOperationIndex = operationComboBox->currentIndex();
	s_previousConstValue = constantDoubleSpinBox->value();
	s_applyInPlace = updateSF1CheckBox->isChecked();

	SF2 sf2Desc;
	sf2Desc.isConstantValue = constantDoubleSpinBox->isEnabled();
	sf2Desc.constantValue = constantDoubleSpinBox->value();
	sf2Desc.sfIndex = sf2Desc.isConstantValue ? -1 : sf2Idx;

	return Apply(cloud, op, sf1Idx, s_applyInPlace, &sf2Desc, this);
}

bool ccScalarFieldArithmeticsDlg::Apply(ccPointCloud* cloud,
										Operation op,
										int sf1Idx,
										bool inplace,
										SF2* sf2Desc/*=0*/,
										QWidget* parent/*=0*/)
{
	assert(cloud);

	if (!cloud || !cloud->hasScalarFields())
	{
		ccLog::Warning("[ccScalarFieldArithmeticsDlg::apply] No input cloud, or cloud has no SF?!");
		assert(false);
		return false;
	}

	if (op == INVALID)
	{
		ccLog::Warning("[ccScalarFieldArithmeticsDlg::apply] Invalid/unhandled operation");
		assert(false);
		return false;
	}

	unsigned sfCount = cloud->getNumberOfScalarFields();
	CCLib::ScalarField* sf1 = 0;
	{
		if (sf1Idx >= static_cast<int>(sfCount))
		{
			ccLog::Warning("[ccScalarFieldArithmeticsDlg::apply] Invalid SF1 index!");
			assert(false);
			return false;
		}
		sf1 = cloud->getScalarField(sf1Idx);
		assert(sf1);
	}

	CCLib::ScalarField* sf2 = 0;
	if (op <= DIVIDE)
	{
		if (!sf2Desc || (!sf2Desc->isConstantValue && sf2Desc->sfIndex >= static_cast<int>(sfCount)))
		{
			ccLog::Warning("[ccScalarFieldArithmeticsDlg::apply] Invalid SF2 index/descriptor!");
			assert(false);
			return false;
		}
		else if (sf2Desc->isConstantValue)
		{
			if (op == DIVIDE && sf2Desc->constantValue == 0)
			{
				ccLog::Error("Invalid constant value (can't divide by zero)");
				return false;
			}
		}
		sf2 = (!sf2Desc->isConstantValue && sf2Desc->sfIndex >= 0 ? cloud->getScalarField(sf2Desc->sfIndex) : 0);
	}

	//output SF
	int sfIdx = -1;
	if (!inplace)
	{
		//generate new sf name based on the operation
		QString sf1Name(sf1->getName());
		QString sf2Name;
		if (sf2)
		{
			sf2Name = sf2->getName();
			QString sfName = GetOperationName(op,sf1Name,sf2Name);
			if (sfName.length() > 24)
			{
				//if the resulting SF name is too long, we use shortcuts instead
				sf1Name = QString("(SF#%1)").arg(sf1Idx);
				sf2Name = QString("(SF#%1)").arg(sf2Desc->sfIndex);
			}
		}
		QString sfName = GetOperationName(op,sf1Name,sf2Name);

		sfIdx = cloud->getScalarFieldIndexByName(qPrintable(sfName));
		if (sfIdx >= 0)
		{
			if (sfIdx == sf1Idx || sfIdx == sf2Desc->sfIndex)
			{
				ccLog::Warning(QString("[ccScalarFieldArithmeticsDlg::apply] Resulting scalar field would have the same name as one of the operand (%1)! Rename it first...").arg(sfName));
				return false;
			}
			if (parent && QMessageBox::warning(	parent,
												"Same scalar field name",
												"Resulting scalar field already exists! Overwrite it?",
												QMessageBox::Ok | QMessageBox::Cancel,
												QMessageBox::Ok ) != QMessageBox::Ok)
			{
				return false;
			}

			cloud->deleteScalarField(sfIdx);
		}

		sfIdx = cloud->addScalarField(qPrintable(sfName));
		if (sfIdx < 0)
		{
			ccLog::Warning("[ccScalarFieldArithmeticsDlg::apply] Failed to create destination SF! (not enough memory?)");
			return false;
		}
	}
	else
	{
		sfIdx = sf1Idx;
	}
	CCLib::ScalarField* sfDest = cloud->getScalarField(sfIdx);
	assert(sfDest);

	unsigned valCount = sf1->currentSize();
	assert(!sf2 || valCount == sf2->currentSize());

	//resize destination SF
	if (!sfDest->resizeSafe(valCount))
	{
		ccLog::Warning("[ccScalarFieldArithmeticsDlg::apply] Not enough memory!");
		cloud->deleteScalarField(sfIdx);
		sfDest = 0;
		return false;
	}
	assert(valCount == sfDest->currentSize());

	for (unsigned i = 0; i < valCount; ++i)
	{
		ScalarType val = NAN_VALUE;

		//we must handle 'invalid' values
		const ScalarType& val1 = sf1->getValue(i);
		if (ccScalarField::ValidValue(val1))
		{
			switch (op)
			{
			case PLUS:
				{
					if (sf2Desc->isConstantValue)
					{
						val = val1 + static_cast<ScalarType>(sf2Desc->constantValue);
					}
					else
					{
						const ScalarType& val2 = sf2->getValue(i);
						if (ccScalarField::ValidValue(val2))
							val = val1 + val2;
					}
				}
				break;
			case MINUS:
				{
					if (sf2Desc->isConstantValue)
					{
						val = val1 - static_cast<ScalarType>(sf2Desc->constantValue);
					}
					else
					{
						const ScalarType& val2 = sf2->getValue(i);
						if (ccScalarField::ValidValue(val2))
							val = val1 - val2;
					}
				}
				break;
			case MULTIPLY:
				{
					if (sf2Desc->isConstantValue)
					{
						val = val1 * static_cast<ScalarType>(sf2Desc->constantValue);
					}
					else
					{
						const ScalarType& val2 = sf2->getValue(i);
						if (ccScalarField::ValidValue(val2))
							val = val1 * val2;
					}
				}
				break;
			case DIVIDE:
				{
					if (sf2Desc->isConstantValue)
					{
						val = val1 / static_cast<ScalarType>(sf2Desc->constantValue);
					}
					else
					{
						const ScalarType& val2 = sf2->getValue(i);
						if (ccScalarField::ValidValue(val2) && std::abs(val2) > ZERO_TOLERANCE )
							val = val1 / val2;
					}
				}
				break;
			case SQRT:
				if (val1 >= 0)
					val = std::sqrt(val1);
				break;
			case POW2:
				val = val1*val1;
				break;
			case POW3:
				val = val1*val1*val1;
				break;
			case EXP:
				val = std::exp(val1);
				break;
			case LOG:
				if (val1 >= 0)
					val = std::log(val1);
				break;
			case LOG10:
				if (val1 >= 0)
					val = std::log10(val1);
				break;
			case COS:
				val = std::cos(val1);
				break;
			case SIN:
				val = std::sin(val1);
				break;
			case TAN:
				val = std::tan(val1);
				break;
			case ACOS:
				if (val1 >= -1 && val1 <= 1)
					val = std::acos(val1);
				break;
			case ASIN:
				if (val1 >= -1 && val1 <= 1)
					val = std::asin(val1);
				break;
			case ATAN:
				val = std::atan(val1);
				break;
			case INT:
				val = static_cast<ScalarType>(static_cast<int>(val1)); //integer part ('round' doesn't seem to be available on MSVC?!)
				break;
			case INVERSE:
				val = std::abs(val1) < ZERO_TOLERANCE ? NAN_VALUE : static_cast<ScalarType>(1.0/val1);
				break;
			default:
				assert(false);
				break;
			}
		}

		sfDest->setValue(i,val);
	}

	sfDest->computeMinAndMax();
	cloud->setCurrentDisplayedScalarField(sfIdx);

	return true;
}