surface_differentialProperties.cpp 12.7 KB
Newer Older
1
#include "surface_differentialProperties.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
2 3 4 5 6 7

#include "mapHandler.h"

#include "Algo/Geometry/normal.h"
#include "Algo/Geometry/curvature.h"

8
namespace CGoGN
Pierre Kraemer's avatar
Pierre Kraemer committed
9
{
10 11 12 13 14 15

namespace SCHNApps
{

bool Surface_DifferentialProperties_Plugin::enable()
{
16 17 18
	//	magic line that init static variables of GenericMap in the plugins
		GenericMap::copyAllStatics(m_schnapps->getStaticPointers());

19 20
	m_computeNormalDialog = new Dialog_ComputeNormal(m_schnapps);
	m_computeCurvatureDialog = new Dialog_ComputeCurvature(m_schnapps);
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24

	m_computeNormalAction = new QAction("Compute Normal", this);
	m_computeCurvatureAction = new QAction("Compute Curvature", this);

25 26
	m_schnapps->addMenuAction(this, "Surface;Differential Properties;Compute Normal", m_computeNormalAction);
	m_schnapps->addMenuAction(this, "Surface;Differential Properties;Compute Curvature", m_computeCurvatureAction);
Pierre Kraemer's avatar
Pierre Kraemer committed
27 28 29 30 31 32 33 34 35 36

	connect(m_computeNormalAction, SIGNAL(triggered()), this, SLOT(openComputeNormalDialog()));
	connect(m_computeCurvatureAction, SIGNAL(triggered()), this, SLOT(openComputeCurvatureDialog()));

	connect(m_computeNormalDialog, SIGNAL(accepted()), this, SLOT(computeNormalFromDialog()));
	connect(m_computeNormalDialog->button_apply, SIGNAL(clicked()), this, SLOT(computeNormalFromDialog()));

	connect(m_computeCurvatureDialog, SIGNAL(accepted()), this, SLOT(computeCurvatureFromDialog()));
	connect(m_computeCurvatureDialog->button_apply, SIGNAL(clicked()), this, SLOT(computeCurvatureFromDialog()));

37 38
	connect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
	connect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));
Pierre Kraemer's avatar
Pierre Kraemer committed
39

40
	connect(m_schnapps, SIGNAL(schnappsClosing()), this, SLOT(schnappsClosing()));
Sylvain Thery's avatar
Sylvain Thery committed
41 42


43 44 45
	foreach(MapHandlerGen* map, m_schnapps->getMapSet().values())
		mapAdded(map);

Pierre Kraemer's avatar
Pierre Kraemer committed
46 47 48
	return true;
}

49 50 51 52 53 54 55 56 57 58 59 60 61
void Surface_DifferentialProperties_Plugin::disable()
{
	disconnect(m_computeNormalAction, SIGNAL(triggered()), this, SLOT(openComputeNormalDialog()));
	disconnect(m_computeCurvatureAction, SIGNAL(triggered()), this, SLOT(openComputeCurvatureDialog()));

	disconnect(m_computeNormalDialog, SIGNAL(accepted()), this, SLOT(computeNormalFromDialog()));
	disconnect(m_computeNormalDialog->button_apply, SIGNAL(clicked()), this, SLOT(computeNormalFromDialog()));

	disconnect(m_computeCurvatureDialog, SIGNAL(accepted()), this, SLOT(computeCurvatureFromDialog()));
	disconnect(m_computeCurvatureDialog->button_apply, SIGNAL(clicked()), this, SLOT(computeCurvatureFromDialog()));

	disconnect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
	disconnect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));
Sylvain Thery's avatar
Sylvain Thery committed
62

63
	disconnect(m_schnapps, SIGNAL(schnappsClosing()), this, SLOT(schnappsClosing()));
64 65
}

66
void Surface_DifferentialProperties_Plugin::mapAdded(MapHandlerGen *map)
Pierre Kraemer's avatar
Pierre Kraemer committed
67 68 69 70
{
	connect(map, SIGNAL(attributeModified(unsigned int, QString)), this, SLOT(attributeModified(unsigned int, QString)));
}

71
void Surface_DifferentialProperties_Plugin::mapRemoved(MapHandlerGen *map)
Pierre Kraemer's avatar
Pierre Kraemer committed
72 73 74 75
{
	disconnect(map, SIGNAL(attributeModified(unsigned int, QString)), this, SLOT(attributeModified(unsigned int, QString)));
}

76
void Surface_DifferentialProperties_Plugin::attributeModified(unsigned int orbit, QString nameAttr)
Pierre Kraemer's avatar
Pierre Kraemer committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
{
	if(orbit == VERTEX)
	{
		MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());
		if(computeNormalLastParameters.contains(map->getName()))
		{
			ComputeNormalParameters& params = computeNormalLastParameters[map->getName()];
			if(params.autoUpdate && params.positionName == nameAttr)
				computeNormal(map->getName(), params.positionName, params.normalName, true);
		}
		if(computeCurvatureLastParameters.contains(map->getName()))
		{
			ComputeCurvatureParameters& params = computeCurvatureLastParameters[map->getName()];
			if(params.autoUpdate && (params.positionName == nameAttr || params.normalName == nameAttr))
				computeCurvature(
					map->getName(),
					params.positionName, params.normalName,
					params.KmaxName, params.kmaxName, params.KminName, params.kminName, params.KnormalName,
					true
				);
		}
	}
}

101
void Surface_DifferentialProperties_Plugin::openComputeNormalDialog()
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103 104 105
{
	m_computeNormalDialog->show();
}

106
void Surface_DifferentialProperties_Plugin::openComputeCurvatureDialog()
Pierre Kraemer's avatar
Pierre Kraemer committed
107 108 109 110
{
	m_computeCurvatureDialog->show();
}

111
void Surface_DifferentialProperties_Plugin::computeNormalFromDialog()
Pierre Kraemer's avatar
Pierre Kraemer committed
112
{
113
	QList<QListWidgetItem*> currentItems = m_computeNormalDialog->list_maps->selectedItems();
Pierre Kraemer's avatar
Pierre Kraemer committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	if(!currentItems.empty())
	{
		const QString& mapName = currentItems[0]->text();

		QString positionName = m_computeNormalDialog->combo_positionAttribute->currentText();

		QString normalName;
		if(m_computeNormalDialog->normalAttributeName->text().isEmpty())
			normalName = m_computeNormalDialog->combo_normalAttribute->currentText();
		else
			normalName = m_computeNormalDialog->normalAttributeName->text();

		bool autoUpdate = (currentItems[0]->checkState() == Qt::Checked);

		computeNormal(mapName, positionName, normalName, autoUpdate);
129 130 131 132 133 134 135 136

		// create VBO if asked
		if (m_computeNormalDialog->enableVBO->isChecked())
		{
			MapHandlerGen* mhg = getSCHNApps()->getMap(mapName);
			if (mhg != NULL)
				mhg->createVBO(normalName);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
137 138 139
	}
}

140
void Surface_DifferentialProperties_Plugin::computeCurvatureFromDialog()
Pierre Kraemer's avatar
Pierre Kraemer committed
141
{
142
	QList<QListWidgetItem*> currentItems = m_computeCurvatureDialog->list_maps->selectedItems();
Pierre Kraemer's avatar
Pierre Kraemer committed
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
	if(!currentItems.empty())
	{
		const QString& mapName = currentItems[0]->text();

		QString positionName = m_computeCurvatureDialog->combo_positionAttribute->currentText();
		QString normalName = m_computeCurvatureDialog->combo_normalAttribute->currentText();

		QString KmaxName;
		if(m_computeCurvatureDialog->KmaxAttributeName->text().isEmpty())
			KmaxName = m_computeCurvatureDialog->combo_KmaxAttribute->currentText();
		else
			KmaxName = m_computeCurvatureDialog->KmaxAttributeName->text();

		QString kmaxName;
		if(m_computeCurvatureDialog->kmaxAttributeName->text().isEmpty())
			kmaxName = m_computeCurvatureDialog->combo_kmaxAttribute->currentText();
		else
			kmaxName = m_computeCurvatureDialog->kmaxAttributeName->text();

		QString KminName;
		if(m_computeCurvatureDialog->KminAttributeName->text().isEmpty())
			KminName = m_computeCurvatureDialog->combo_KminAttribute->currentText();
		else
			KminName = m_computeCurvatureDialog->KminAttributeName->text();

		QString kminName;
		if(m_computeCurvatureDialog->kminAttributeName->text().isEmpty())
			kminName = m_computeCurvatureDialog->combo_kminAttribute->currentText();
		else
			kminName = m_computeCurvatureDialog->kminAttributeName->text();

		QString KnormalName;
		if(m_computeCurvatureDialog->KnormalAttributeName->text().isEmpty())
			KnormalName = m_computeCurvatureDialog->combo_KnormalAttribute->currentText();
		else
			KnormalName = m_computeCurvatureDialog->KnormalAttributeName->text();

		bool compute_kmean = (m_computeCurvatureDialog->check_computeKmean->checkState() == Qt::Checked);
		bool compute_kgaussian = (m_computeCurvatureDialog->check_computeKgaussian->checkState() == Qt::Checked);
		bool autoUpdate = (currentItems[0]->checkState() == Qt::Checked);

		computeCurvature(
			mapName,
			positionName, normalName,
			KmaxName, kmaxName, KminName, kminName, KnormalName,
			compute_kmean, compute_kgaussian,
			autoUpdate
		);
	}
}

194
void Surface_DifferentialProperties_Plugin::computeNormal(
Pierre Kraemer's avatar
Pierre Kraemer committed
195 196 197 198 199
	const QString& mapName,
	const QString& positionAttributeName,
	const QString& normalAttributeName,
	bool autoUpdate)
{
200
	MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m_schnapps->getMap(mapName));
Pierre Kraemer's avatar
Pierre Kraemer committed
201 202 203
	if(mh == NULL)
		return;

Sylvain Thery's avatar
Sylvain Thery committed
204
	VertexAttribute<PFP2::VEC3, PFP2::MAP> position = mh->getAttribute<PFP2::VEC3, VERTEX>(positionAttributeName);
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206 207
	if(!position.isValid())
		return;

Sylvain Thery's avatar
Sylvain Thery committed
208
	VertexAttribute<PFP2::VEC3, PFP2::MAP> normal = mh->getAttribute<PFP2::VEC3, VERTEX>(normalAttributeName);
Pierre Kraemer's avatar
Pierre Kraemer committed
209 210 211 212 213 214 215 216 217 218
	if(!normal.isValid())
		normal = mh->addAttribute<PFP2::VEC3, VERTEX>(normalAttributeName);

	PFP2::MAP* map = mh->getMap();
	Algo::Surface::Geometry::computeNormalVertices<PFP2>(*map, position, normal);

	computeNormalLastParameters[mapName] =
		ComputeNormalParameters(positionAttributeName, normalAttributeName, autoUpdate);

	mh->notifyAttributeModification(normal);
Sylvain Thery's avatar
Sylvain Thery committed
219 220 221 222

	//this->pythonRecording("computeNormal", "", mh->getName(), QString(position.name().c_str()), QString(normal.name().c_str()), autoUpdate);
	this->pythonRecording("computeNormal", "", mh->getName(), positionAttributeName, normalAttributeName, autoUpdate);

Pierre Kraemer's avatar
Pierre Kraemer committed
223 224
}

Sylvain Thery's avatar
Sylvain Thery committed
225

226
void Surface_DifferentialProperties_Plugin::computeCurvature(
Pierre Kraemer's avatar
Pierre Kraemer committed
227 228 229 230 231 232 233 234 235 236 237 238
	const QString& mapName,
	const QString& positionAttributeName,
	const QString& normalAttributeName,
	const QString& KmaxAttributeName,
	const QString& kmaxAttributeName,
	const QString& KminAttributeName,
	const QString& kminAttributeName,
	const QString& KnormalAttributeName,
	bool compute_kmean,
	bool compute_kgaussian,
	bool autoUpdate)
{
239
	MapHandler<PFP2>* mh = static_cast<MapHandler<PFP2>*>(m_schnapps->getMap(mapName));
Pierre Kraemer's avatar
Pierre Kraemer committed
240 241 242
	if(mh == NULL)
		return;

Sylvain Thery's avatar
Sylvain Thery committed
243
	VertexAttribute<PFP2::VEC3, PFP2::MAP> position = mh->getAttribute<PFP2::VEC3, VERTEX>(positionAttributeName);
Pierre Kraemer's avatar
Pierre Kraemer committed
244 245 246
	if(!position.isValid())
		return;

Sylvain Thery's avatar
Sylvain Thery committed
247
	VertexAttribute<PFP2::VEC3, PFP2::MAP> normal = mh->getAttribute<PFP2::VEC3, VERTEX>(normalAttributeName);
Pierre Kraemer's avatar
Pierre Kraemer committed
248 249 250
	if(!normal.isValid())
		return;

Sylvain Thery's avatar
Sylvain Thery committed
251
	VertexAttribute<PFP2::VEC3, PFP2::MAP> Kmax = mh->getAttribute<PFP2::VEC3, VERTEX>(KmaxAttributeName);
Pierre Kraemer's avatar
Pierre Kraemer committed
252 253 254
	if(!Kmax.isValid())
		Kmax = mh->addAttribute<PFP2::VEC3, VERTEX>(KmaxAttributeName);

Sylvain Thery's avatar
Sylvain Thery committed
255
	VertexAttribute<PFP2::REAL, PFP2::MAP> kmax = mh->getAttribute<PFP2::REAL, VERTEX>(kmaxAttributeName);
Pierre Kraemer's avatar
Pierre Kraemer committed
256 257 258
	if(!kmax.isValid())
		kmax = mh->addAttribute<PFP2::REAL, VERTEX>(kmaxAttributeName);

Sylvain Thery's avatar
Sylvain Thery committed
259
	VertexAttribute<PFP2::VEC3, PFP2::MAP> Kmin = mh->getAttribute<PFP2::VEC3, VERTEX>(KminAttributeName);
Pierre Kraemer's avatar
Pierre Kraemer committed
260 261 262
	if(!Kmin.isValid())
		Kmin = mh->addAttribute<PFP2::VEC3, VERTEX>(KminAttributeName);

Sylvain Thery's avatar
Sylvain Thery committed
263
	VertexAttribute<PFP2::REAL, PFP2::MAP> kmin = mh->getAttribute<PFP2::REAL, VERTEX>(kminAttributeName);
Pierre Kraemer's avatar
Pierre Kraemer committed
264 265 266
	if(!kmin.isValid())
		kmin = mh->addAttribute<PFP2::REAL, VERTEX>(kminAttributeName);

Sylvain Thery's avatar
Sylvain Thery committed
267
	VertexAttribute<PFP2::VEC3, PFP2::MAP> Knormal = mh->getAttribute<PFP2::VEC3, VERTEX>(KnormalAttributeName);
Pierre Kraemer's avatar
Pierre Kraemer committed
268 269 270
	if(!Knormal.isValid())
		Knormal = mh->addAttribute<PFP2::VEC3, VERTEX>(KnormalAttributeName);

Sylvain Thery's avatar
Sylvain Thery committed
271
	EdgeAttribute<PFP2::REAL, PFP2::MAP> edgeAngle = mh->getAttribute<PFP2::REAL, EDGE>("edgeAngle");
Pierre Kraemer's avatar
Pierre Kraemer committed
272 273 274
	if(!edgeAngle.isValid())
		edgeAngle = mh->addAttribute<PFP2::REAL, EDGE>("edgeAngle");

275 276 277 278
	EdgeAttribute<PFP2::REAL, PFP2::MAP> edgeArea = mh->getAttribute<PFP2::REAL, EDGE>("edgeArea");
	if(!edgeArea.isValid())
		edgeArea = mh->addAttribute<PFP2::REAL, EDGE>("edgeArea");

Pierre Kraemer's avatar
Pierre Kraemer committed
279
	PFP2::MAP* map = mh->getMap();
280

Pierre Kraemer's avatar
Pierre Kraemer committed
281
	Algo::Surface::Geometry::computeAnglesBetweenNormalsOnEdges<PFP2>(*map, position, edgeAngle);
282 283
	Algo::Surface::Geometry::computeAreaEdges<PFP2>(*map, position, edgeArea);

Sylvain Thery's avatar
Sylvain Thery committed
284
	PFP2::REAL meanEdgeLength = Algo::Geometry::meanEdgeLength<PFP2>(*map, position);
285 286 287

	float radius = 2.0f * meanEdgeLength;
	Algo::Surface::Geometry::computeCurvatureVertices_NormalCycles_Projected<PFP2>(*map, radius, position, normal, edgeAngle, edgeArea, kmax, kmin, Kmax, Kmin, Knormal);
Pierre Kraemer's avatar
Pierre Kraemer committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

	computeCurvatureLastParameters[mapName] =
		ComputeCurvatureParameters(
			positionAttributeName, normalAttributeName,
			KmaxAttributeName, kmaxAttributeName, KminAttributeName, kminAttributeName, KnormalAttributeName,
			compute_kmean, compute_kgaussian, autoUpdate);

	mh->notifyAttributeModification(Kmax);
	mh->notifyAttributeModification(kmax);
	mh->notifyAttributeModification(Kmin);
	mh->notifyAttributeModification(kmin);
	mh->notifyAttributeModification(Knormal);

	if(compute_kmean)
	{
Sylvain Thery's avatar
Sylvain Thery committed
303
		VertexAttribute<PFP2::REAL, PFP2::MAP> kmean = mh->getAttribute<PFP2::REAL, VERTEX>("kmean");
Pierre Kraemer's avatar
Pierre Kraemer committed
304 305 306 307 308 309 310 311 312 313 314
		if(!kmean.isValid())
			kmean = mh->addAttribute<PFP2::REAL, VERTEX>("kmean");

		for(unsigned int i = kmin.begin(); i != kmin.end(); kmin.next(i))
			kmean[i] = (kmin[i] + kmax[i]) / 2.0;

		mh->notifyAttributeModification(kmean);
	}

	if(compute_kgaussian)
	{
Sylvain Thery's avatar
Sylvain Thery committed
315
		VertexAttribute<PFP2::REAL, PFP2::MAP> kgaussian = mh->getAttribute<PFP2::REAL, VERTEX>("kgaussian");
Pierre Kraemer's avatar
Pierre Kraemer committed
316 317 318 319 320 321 322 323
		if(!kgaussian.isValid())
			kgaussian = mh->addAttribute<PFP2::REAL, VERTEX>("kgaussian");

		for(unsigned int i = kmin.begin(); i != kmin.end(); kmin.next(i))
			kgaussian[i] = kmin[i] * kmax[i];

		mh->notifyAttributeModification(kgaussian);
	}
Sylvain Thery's avatar
Sylvain Thery committed
324 325 326

	this->pythonRecording("computeCurvature", "", mh->getName(),
		positionAttributeName, normalAttributeName, KmaxAttributeName, kmaxAttributeName, KminAttributeName, kminAttributeName, KnormalAttributeName, compute_kmean, compute_kgaussian, autoUpdate);
Pierre Kraemer's avatar
Pierre Kraemer committed
327 328
}

329
void Surface_DifferentialProperties_Plugin::schnappsClosing()
Sylvain Thery's avatar
Sylvain Thery committed
330 331 332 333 334
{
	m_computeNormalDialog->close();
	m_computeCurvatureDialog->close();
}

Sylvain Thery's avatar
Sylvain Thery committed
335 336 337 338 339
#if CGOGN_QT_DESIRED_VERSION == 5
	Q_PLUGIN_METADATA(IID "CGoGN.SCHNapps.Plugin")
#else
	Q_EXPORT_PLUGIN2(Surface_DifferentialProperties_Plugin, Surface_DifferentialProperties_Plugin)
#endif
340 341 342 343

} // namespace SCHNApps

} // namespace CGoGN