File: qPCV.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 (321 lines) | stat: -rw-r--r-- 9,199 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
//##########################################################################
//#                                                                        #
//#                       CLOUDCOMPARE PLUGIN: qPCV                        #
//#                                                                        #
//#  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: Daniel Girardeau-Montaut                   #
//#                                                                        #
//##########################################################################

#include "qPCV.h"
#include "ccPcvDlg.h"

//CCLib
#include <ScalarField.h>
#include <PCV.h>

//qCC_db
#include <ccHObjectCaster.h>
#include <ccGenericPointCloud.h>
#include <ccPointCloud.h>
#include <ccGenericMesh.h>
#include <ccProgressDialog.h>
#include <ccScalarField.h>
#include <ccColorScalesManager.h>

//Qt
#include <QtGui>
#include <QMainWindow>
#include <QProgressBar>

#ifndef CC_PCV_FIELD_LABEL_NAME
#define CC_PCV_FIELD_LABEL_NAME "Illuminance (PCV)"
#endif

qPCV::qPCV(QObject* parent/*=0*/)
	: QObject(parent)
	, ccStdPluginInterface(":/CC/plugin/qPCV/info.json")
	, m_action(nullptr)
{
}

void qPCV::onNewSelection(const ccHObject::Container& selectedEntities)
{
	if (m_action)
	{
		bool elligibleEntitiies = false;
		for (ccHObject* obj : selectedEntities)
		{
			if (obj && (obj->isKindOf(CC_TYPES::POINT_CLOUD) || obj->isKindOf(CC_TYPES::MESH)))
			{
				elligibleEntitiies = true;
				break;
			}
		}
		m_action->setEnabled(elligibleEntitiies);
	}
}

QList<QAction *> qPCV::getActions()
{
	//default action
	if (!m_action)
	{
		m_action = new QAction(getName(),this);
		m_action->setToolTip(getDescription());
		m_action->setIcon(getIcon());
		//connect signal
		connect(m_action, &QAction::triggered, this, &qPCV::doAction);
	}

	return QList<QAction *>{ m_action };
}

//persistent settings during a single session
static bool s_firstLaunch				= true;
static int s_raysSpinBoxValue			= 256;
static int s_resSpinBoxValue			= 1024;
static bool s_mode180CheckBoxState		= true;
static bool s_closedMeshCheckBoxState	= false;

void qPCV::doAction()
{
	assert(m_app);
	if (!m_app)
		return;

	const ccHObject::Container& selectedEntities = m_app->getSelectedEntities();

	ccHObject::Container candidates;
	bool hasMeshes = false;
	for (ccHObject* obj : selectedEntities)
	{
		if (!obj)
		{
			assert(false);
			continue;
		}
		
		if (obj->isA(CC_TYPES::POINT_CLOUD))
		{
			//we need a real point cloud
			candidates.push_back(obj);
		}
		else if (obj->isKindOf(CC_TYPES::MESH))
		{
			ccGenericMesh* mesh = ccHObjectCaster::ToGenericMesh(obj);
			if (mesh->getAssociatedCloud() && mesh->getAssociatedCloud()->isA(CC_TYPES::POINT_CLOUD))
			{
				//we need a mesh with a real point cloud
				candidates.push_back(obj);
				hasMeshes = true;
			}
		}
	}

	ccPcvDlg dlg(m_app->getMainWindow());

	//restore previous dialog state
	if (!s_firstLaunch)
	{
		dlg.raysSpinBox->setValue(s_raysSpinBoxValue);
		dlg.mode180CheckBox->setChecked(s_mode180CheckBoxState);
		dlg.resSpinBox->setValue(s_resSpinBoxValue);
		dlg.closedMeshCheckBox->setChecked(s_closedMeshCheckBoxState);
	}

	dlg.closedMeshCheckBox->setEnabled(hasMeshes); //for meshes only

	//for using clouds normals as rays
	std::vector<ccGenericPointCloud*> cloudsWithNormals;
	ccHObject* root = m_app->dbRootObject();
	if (root)
	{
		ccHObject::Container clouds;
		root->filterChildren(clouds, true, CC_TYPES::POINT_CLOUD);
		for (size_t i = 0; i < clouds.size(); ++i)
		{
			//we keep only clouds with normals
			ccGenericPointCloud* cloud = ccHObjectCaster::ToGenericPointCloud(clouds[i]);
			if (cloud && cloud->hasNormals())
			{
				cloudsWithNormals.push_back(cloud);
				QString cloudTitle = QString("%1 - %2 points").arg(cloud->getName()).arg(cloud->size());
				if (cloud->getParent() && cloud->getParent()->isKindOf(CC_TYPES::MESH))
				{
					cloudTitle.append(QString(" (%1)").arg(cloud->getParent()->getName()));
				}

				dlg.cloudsComboBox->addItem(cloudTitle);
			}
		}
	}
	if (cloudsWithNormals.empty())
	{
		dlg.useCloudRadioButton->setEnabled(false);
	}

	if (!dlg.exec())
	{
		return;
	}

	//save dialog state
	{
		s_firstLaunch				= false;
		s_raysSpinBoxValue			= dlg.raysSpinBox->value();
		s_mode180CheckBoxState		= dlg.mode180CheckBox->isChecked();
		s_resSpinBoxValue			= dlg.resSpinBox->value();
		s_closedMeshCheckBoxState	= dlg.closedMeshCheckBox->isChecked();
	}

	unsigned raysNumber = dlg.raysSpinBox->value();
	unsigned resolution = dlg.resSpinBox->value();
	bool meshIsClosed = (hasMeshes ? dlg.closedMeshCheckBox->isChecked() : false);
	bool mode360 = !dlg.mode180CheckBox->isChecked();

	//PCV type ShadeVis
	std::vector<CCVector3> rays;
	if (!cloudsWithNormals.empty() && dlg.useCloudRadioButton->isChecked())
	{
		//Version with cloud normals as light rays
		assert(dlg.cloudsComboBox->currentIndex() < static_cast<int>(cloudsWithNormals.size()));
		ccGenericPointCloud* pc = cloudsWithNormals[dlg.cloudsComboBox->currentIndex()];
		unsigned count = pc->size();
		try
		{
			rays.resize(count);
		}
		catch (std::bad_alloc)
		{
			m_app->dispToConsole("Not enough memory to generate the set of rays", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
			return;
		}
		for (unsigned i = 0; i < count; ++i)
		{
			rays[i] = CCVector3(pc->getPointNormal(i));
		}
	}
	else
	{
		//generates light directions
		if (!PCV::GenerateRays(raysNumber, rays, mode360))
		{
			m_app->dispToConsole("Failed to generate the set of rays", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
			return;
		}
	}

	if (rays.empty())
	{
		assert(false);
		m_app->dispToConsole("No ray was generated?!", ccMainAppInterface::WRN_CONSOLE_MESSAGE);
		return;
	}

	ccProgressDialog pcvProgressCb(true, m_app->getMainWindow());
	pcvProgressCb.setAutoClose(false);

	size_t count = 0;
	for (ccHObject* obj : candidates)
	{
		ccPointCloud* cloud = nullptr;
		ccGenericMesh* mesh = nullptr;
		QString objName = "unknown";

		assert(obj);
		if (obj->isA(CC_TYPES::POINT_CLOUD))
		{
			//we need a real point cloud
			cloud = ccHObjectCaster::ToPointCloud(obj);
			objName = cloud->getName();
		}
		else if (obj->isKindOf(CC_TYPES::MESH))
		{
			mesh = ccHObjectCaster::ToGenericMesh(obj);
			cloud = ccHObjectCaster::ToPointCloud(mesh->getAssociatedCloud());
			objName = mesh->getName();
		}
		assert(cloud);

		//we get the PCV field if it already exists
		int sfIdx = cloud->getScalarFieldIndexByName(CC_PCV_FIELD_LABEL_NAME);
		//otherwise we create it
		if (sfIdx < 0)
		{
			sfIdx = cloud->addScalarField(CC_PCV_FIELD_LABEL_NAME);
		}
		if (sfIdx < 0)
		{
			m_app->dispToConsole("Couldn't allocate a new scalar field for computing PCV field ! Try to free some memory ...", ccMainAppInterface::ERR_CONSOLE_MESSAGE);
			return;
		}
		cloud->setCurrentScalarField(sfIdx);

		QString objNameForPorgressDialog = objName;
		if (candidates.size() > 1)
		{
			objNameForPorgressDialog += QString("(%1/%2)").arg(++count).arg(candidates.size());
		}

		bool wasEnabled = obj->isEnabled();
		bool wasVisible = obj->isVisible();
		obj->setEnabled(true);
		obj->setVisible(true);
		bool success = PCV::Launch(rays, cloud, mesh, meshIsClosed, resolution, resolution, &pcvProgressCb, objNameForPorgressDialog);
		obj->setEnabled(wasEnabled);
		obj->setVisible(wasVisible);

		if (!success)
		{
			cloud->deleteScalarField(sfIdx);
			m_app->dispToConsole(tr("An error occurred during entity '%1' illumination!").arg(objName), ccMainAppInterface::ERR_CONSOLE_MESSAGE);
		}
		else
		{
			ccScalarField* sf = static_cast<ccScalarField*>(cloud->getScalarField(sfIdx));
			if (sf)
			{
				sf->computeMinAndMax();
				cloud->setCurrentDisplayedScalarField(sfIdx);
				sf->setColorScale(ccColorScalesManager::GetDefaultScale(ccColorScalesManager::GREY));
				if (obj->hasNormals() && obj->normalsShown())
				{
					m_app->dispToConsole(tr("Entity '%1' normals have been automatically disabled").arg(objName), ccMainAppInterface::WRN_CONSOLE_MESSAGE);
				}
				obj->showNormals(false);
				obj->showSF(true);
				if (obj != cloud)
				{
					cloud->showSF(true);
				}
				obj->prepareDisplayForRefresh_recursive();
			}
			else
			{
				assert(false);
			}
		}

		if (pcvProgressCb.wasCanceled())
		{
			m_app->dispToConsole(tr("Process has been cancelled by the user"), ccMainAppInterface::WRN_CONSOLE_MESSAGE);
			break;
		}
	}

	pcvProgressCb.close();

	//currently selected entities parameters may have changed!
	m_app->updateUI();
	//currently selected entities appearance may have changed!
	m_app->refreshAll();
}