pmesh.hpp 19.2 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library 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 Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Geometry/localFrame.h"

namespace CGoGN
{

namespace Algo
{

33 34 35
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
36 37 38 39 40 41
namespace PMesh
{

template <typename PFP>
ProgressiveMesh<PFP>::ProgressiveMesh(
		MAP& map, DartMarker& inactive,
42
		Algo::Surface::Decimation::SelectorType s, Algo::Surface::Decimation::ApproximatorType a,
43
		VertexAttribute<typename PFP::VEC3>& position
Pierre Kraemer's avatar
Pierre Kraemer committed
44 45 46
	) :
	m_map(map), positionsTable(position), inactiveMarker(inactive), dartSelect(inactiveMarker)
{
47
	CGoGNout << "  creating approximator and predictor.." << CGoGNflush ;
48 49 50

	std::vector<VertexAttribute< typename PFP::VEC3>* > pos_v ;
	pos_v.push_back(&positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
51 52
	switch(a)
	{
53 54
		case Algo::Surface::Decimation::A_QEM : {
			m_approximators.push_back(new Algo::Surface::Decimation::Approximator_QEM<PFP>(m_map, pos_v)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
55
			break ; }
56 57
		case Algo::Surface::Decimation::A_MidEdge : {
			m_approximators.push_back(new Algo::Surface::Decimation::Approximator_MidEdge<PFP>(m_map, pos_v)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
58
			break ; }
59 60
		case Algo::Surface::Decimation::A_hHalfCollapse : {
			Algo::Surface::Decimation::Predictor_HalfCollapse<PFP>* pred = new Algo::Surface::Decimation::Predictor_HalfCollapse<PFP>(m_map, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
61
			m_predictors.push_back(pred) ;
62
			m_approximators.push_back(new Algo::Surface::Decimation::Approximator_HalfCollapse<PFP>(m_map, pos_v, pred)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
63
			break ; }
64 65
		case Algo::Surface::Decimation::A_CornerCutting : {
			Algo::Surface::Decimation::Predictor_CornerCutting<PFP>* pred = new Algo::Surface::Decimation::Predictor_CornerCutting<PFP>(m_map, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
66
			m_predictors.push_back(pred) ;
67
			m_approximators.push_back(new Algo::Surface::Decimation::Approximator_CornerCutting<PFP>(m_map, pos_v, pred)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
68
			break ; }
69 70
		case Algo::Surface::Decimation::A_TangentPredict1 : {
			Algo::Surface::Decimation::Predictor_TangentPredict1<PFP>* pred = new Algo::Surface::Decimation::Predictor_TangentPredict1<PFP>(m_map, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
71
			m_predictors.push_back(pred) ;
72
			m_approximators.push_back(new Algo::Surface::Decimation::Approximator_MidEdge<PFP>(m_map, pos_v, pred)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
73
			break ; }
74 75
		case Algo::Surface::Decimation::A_TangentPredict2 : {
			Algo::Surface::Decimation::Predictor_TangentPredict2<PFP>* pred = new Algo::Surface::Decimation::Predictor_TangentPredict2<PFP>(m_map, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
76
			m_predictors.push_back(pred) ;
77
			m_approximators.push_back(new Algo::Surface::Decimation::Approximator_MidEdge<PFP>(m_map, pos_v, pred)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
78 79
			break ; }
	}
80
	CGoGNout << "..done" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
81

82
	CGoGNout << "  creating selector.." << CGoGNflush ;
Pierre Kraemer's avatar
Pierre Kraemer committed
83 84
	switch(s)
	{
85 86
		case Algo::Surface::Decimation::S_MapOrder : {
			m_selector = new Algo::Surface::Decimation::EdgeSelector_MapOrder<PFP>(m_map, positionsTable, m_approximators, dartSelect) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
87
			break ; }
88 89
		case Algo::Surface::Decimation::S_Random : {
			m_selector = new Algo::Surface::Decimation::EdgeSelector_Random<PFP>(m_map, positionsTable, m_approximators, dartSelect) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
90
			break ; }
91 92
		case Algo::Surface::Decimation::S_EdgeLength : {
			m_selector = new Algo::Surface::Decimation::EdgeSelector_Length<PFP>(m_map, positionsTable, m_approximators, dartSelect) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
93
			break ; }
94 95
		case Algo::Surface::Decimation::S_QEM : {
			m_selector = new Algo::Surface::Decimation::EdgeSelector_QEM<PFP>(m_map, positionsTable, m_approximators, dartSelect) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
96
			break ; }
97 98
		case Algo::Surface::Decimation::S_MinDetail : {
			m_selector = new Algo::Surface::Decimation::EdgeSelector_MinDetail<PFP>(m_map, positionsTable, m_approximators, dartSelect) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
99
			break ; }
100 101
		case Algo::Surface::Decimation::S_Curvature : {
			m_selector = new Algo::Surface::Decimation::EdgeSelector_Curvature<PFP>(m_map, positionsTable, m_approximators, dartSelect) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103
			break ; }
	}
104
	CGoGNout << "..done" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
105 106 107

	m_initOk = true ;

108
	CGoGNout << "  initializing approximators.." << CGoGNflush ;
109
	for(typename std::vector<Algo::Surface::Decimation::ApproximatorGen<PFP>*>::iterator it = m_approximators.begin(); it != m_approximators.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111 112 113
	{
		if(! (*it)->init())
			m_initOk = false ;
		if((*it)->getApproximatedAttributeName() == "position")
114
			m_positionApproximator = reinterpret_cast<Algo::Surface::Decimation::Approximator<PFP, VEC3, EDGE>*>(*it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
115
	}
116
	CGoGNout << "..done" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
117

118
	CGoGNout << "  initializing predictors.." << CGoGNflush ;
119
	for(typename std::vector<Algo::Surface::Decimation::PredictorGen<PFP>*>::iterator it = m_predictors.begin(); it != m_predictors.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121
		if(! (*it)->init())
			m_initOk = false ;
122
	CGoGNout << "..done" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
123

124
	CGoGNout << "  initializing selector.." << CGoGNflush ;
Pierre Kraemer's avatar
Pierre Kraemer committed
125
	m_initOk = m_selector->init() ;
126
	CGoGNout << "..done" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140

	m_detailAmount = REAL(1) ;
	m_localFrameDetailVectors = false ;
	quantizationInitialized = false ;
	quantizationApplied = false ;
}

template <typename PFP>
ProgressiveMesh<PFP>::~ProgressiveMesh()
{
	for(unsigned int i = 0; i < m_splits.size(); ++i)
		delete m_splits[i] ;
	if(m_selector)
		delete m_selector ;
141
	for(typename std::vector<Algo::Surface::Decimation::ApproximatorGen<PFP>*>::iterator it = m_approximators.begin(); it != m_approximators.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
142
		delete (*it) ;
143
	for(typename std::vector<Algo::Surface::Decimation::PredictorGen<PFP>*>::iterator it = m_predictors.begin(); it != m_predictors.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146 147 148 149 150 151
		delete (*it) ;
	if(quantizationInitialized)
		delete q ;
}

template <typename PFP>
void ProgressiveMesh<PFP>::createPM(unsigned int percentWantedVertices)
{
152
	unsigned int nbVertices = m_map.template getNbOrbits<VERTEX>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
153
	unsigned int nbWantedVertices = nbVertices * percentWantedVertices / 100 ;
154
	CGoGNout << "  creating PM (" << nbVertices << " vertices).." << /* flush */ CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

	bool finished = false ;
	Dart d ;
	while(!finished)
	{
		if(!m_selector->nextEdge(d))
			break ;

		--nbVertices ;
		Dart d2 = m_map.phi2(m_map.phi_1(d)) ;
		Dart dd2 = m_map.phi2(m_map.phi_1(m_map.phi2(d))) ;

		VSplit<PFP>* vs = new VSplit<PFP>(m_map, d, dd2, d2) ;	// create new VSplit node
		m_splits.push_back(vs) ;								// and store it

170
		for(typename std::vector<Algo::Surface::Decimation::ApproximatorGen<PFP>*>::iterator it = m_approximators.begin(); it != m_approximators.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
171 172 173 174 175 176 177 178 179
		{
			(*it)->approximate(d) ;					// compute approximated attributes with its associated detail
			(*it)->saveApprox(d) ;
		}

		m_selector->updateBeforeCollapse(d) ;		// update selector

		edgeCollapse(vs) ;							// collapse edge

180 181 182
		unsigned int newV = m_map.template setOrbitEmbeddingOnNewCell<VERTEX>(d2) ;
		unsigned int newE1 = m_map.template setOrbitEmbeddingOnNewCell<EDGE>(d2) ;
		unsigned int newE2 = m_map.template setOrbitEmbeddingOnNewCell<EDGE>(dd2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
183 184 185 186
		vs->setApproxV(newV) ;
		vs->setApproxE1(newE1) ;
		vs->setApproxE2(newE2) ;

187
		for(typename std::vector<Algo::Surface::Decimation::ApproximatorGen<PFP>*>::iterator it = m_approximators.begin(); it != m_approximators.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
188 189 190 191 192 193 194 195 196 197 198
			(*it)->affectApprox(d2);				// affect data to the resulting vertex

		m_selector->updateAfterCollapse(d2, dd2) ;	// update selector

		if(nbVertices <= nbWantedVertices)
			finished = true ;
	}
	delete m_selector ;
	m_selector = NULL ;

	m_cur = m_splits.size() ;
199
	CGoGNout << "..done (" << nbVertices << " vertices)" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
200 201 202 203 204 205 206 207 208 209

	initQuantization() ;
}

template <typename PFP>
void ProgressiveMesh<PFP>::edgeCollapse(VSplit<PFP>* vs)
{
	Dart d = vs->getEdge() ;
	Dart dd = m_map.phi2(d) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
210 211
	inactiveMarker.markOrbit<FACE>(d) ;
	inactiveMarker.markOrbit<FACE>(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225

	m_map.extractTrianglePair(d) ;
}

template <typename PFP>
void ProgressiveMesh<PFP>::vertexSplit(VSplit<PFP>* vs)
{
	Dart d = vs->getEdge() ;
	Dart dd = m_map.phi2(d) ;
	Dart d2 = vs->getLeftEdge() ;
	Dart dd2 = vs->getRightEdge() ;

	m_map.insertTrianglePair(d, d2, dd2) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
226 227
	inactiveMarker.unmarkOrbit<FACE>(d) ;
	inactiveMarker.unmarkOrbit<FACE>(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
228 229 230 231 232 233 234 235 236 237 238
}

template <typename PFP>
void ProgressiveMesh<PFP>::coarsen()
{
	if(m_cur == m_splits.size())
		return ;

	VSplit<PFP>* vs = m_splits[m_cur] ; // get the split node
	++m_cur ;

239 240
	// Dart d = vs->getEdge() ;
	// Dart dd = m_map.phi2(d) ;		// get some darts
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243 244 245
	Dart d2 = vs->getLeftEdge() ;
	Dart dd2 = vs->getRightEdge() ;

	edgeCollapse(vs) ;	// collapse edge

246 247 248
	m_map.template setOrbitEmbedding<VERTEX>(d2, vs->getApproxV()) ;
	m_map.template setOrbitEmbedding<EDGE>(d2, vs->getApproxE1()) ;
	m_map.template setOrbitEmbedding<EDGE>(dd2, vs->getApproxE2()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
}

template <typename PFP>
void ProgressiveMesh<PFP>::refine()
{
	if(m_cur == 0)
		return ;

	--m_cur ;
	VSplit<PFP>* vs = m_splits[m_cur] ; // get the split node

	Dart d = vs->getEdge() ;
	Dart dd = m_map.phi2(d) ; 		// get some darts
	Dart dd2 = vs->getRightEdge() ;
	Dart d2 = vs->getLeftEdge() ;
	Dart d1 = m_map.phi2(d2) ;
	Dart dd1 = m_map.phi2(dd2) ;

267 268 269 270 271 272
	unsigned int v1 = m_map.template getEmbedding<VERTEX>(d) ;				// get the embedding
	unsigned int v2 = m_map.template getEmbedding<VERTEX>(dd) ;			// of the new vertices
	unsigned int e1 = m_map.template getEmbedding<EDGE>(m_map.phi1(d)) ;
	unsigned int e2 = m_map.template getEmbedding<EDGE>(m_map.phi_1(d)) ;	// and new edges
	unsigned int e3 = m_map.template getEmbedding<EDGE>(m_map.phi1(dd)) ;
	unsigned int e4 = m_map.template getEmbedding<EDGE>(m_map.phi_1(dd)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
273 274 275

	if(!m_predictors.empty())
	{
276
		for(typename std::vector<Algo::Surface::Decimation::PredictorGen<PFP>*>::iterator pit = m_predictors.begin();
Pierre Kraemer's avatar
Pierre Kraemer committed
277 278 279 280 281 282 283 284 285 286
			pit != m_predictors.end();
			++pit)
		{
			(*pit)->predict(d2, dd2) ;
		}
	}

	typename PFP::MATRIX33 invLocalFrame ;
	if(m_localFrameDetailVectors)
	{
287
		typename PFP::MATRIX33 localFrame = Algo::Surface::Geometry::vertexLocalFrame<PFP>(m_map, dd2, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
288 289 290 291 292
		localFrame.invert(invLocalFrame) ;
	}

	vertexSplit(vs) ; // split vertex

293 294 295 296 297 298
	m_map.template setOrbitEmbedding<VERTEX>(d, v1) ;		// embed the
	m_map.template setOrbitEmbedding<VERTEX>(dd, v2) ;	// new vertices
	m_map.template setOrbitEmbedding<EDGE>(d1, e1) ;
	m_map.template setOrbitEmbedding<EDGE>(d2, e2) ;		// and new edges
	m_map.template setOrbitEmbedding<EDGE>(dd1, e3) ;
	m_map.template setOrbitEmbedding<EDGE>(dd2, e4) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
299 300 301

	if(!m_predictors.empty())
	{
302 303
		typename std::vector<Algo::Surface::Decimation::PredictorGen<PFP>*>::iterator pit ;
		typename std::vector<Algo::Surface::Decimation::ApproximatorGen<PFP>*>::iterator ait ;
Pierre Kraemer's avatar
Pierre Kraemer committed
304 305 306 307 308 309 310 311 312
		for(pit = m_predictors.begin(), ait = m_approximators.begin();
			pit != m_predictors.end();
			++pit, ++ait)
		{
			typename PFP::MATRIX33* detailTransform = NULL ;
			if(m_localFrameDetailVectors)
				detailTransform = &invLocalFrame ;

			(*pit)->affectPredict(d) ;
313
			if((*ait)->getType() == Algo::Surface::Decimation::A_hHalfCollapse)
Pierre Kraemer's avatar
Pierre Kraemer committed
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
			{
				(*ait)->addDetail(dd, m_detailAmount, true, detailTransform) ;
			}
			else
			{
				(*ait)->addDetail(d, m_detailAmount, true, detailTransform) ;
				(*ait)->addDetail(dd, m_detailAmount, false, detailTransform) ;
			}
		}
	}
}

template <typename PFP>
void ProgressiveMesh<PFP>::gotoLevel(unsigned int l)
{
	if(l == m_cur || l > m_splits.size() || l < 0)
		return ;

	if(l > m_cur)
		while(m_cur != l)
			coarsen() ;
	else
		while(m_cur != l)
			refine() ;
}

template <typename PFP>
void ProgressiveMesh<PFP>::recomputeApproxAndDetails()
{
//	if(!m_predictors.empty())
//	{
//		gotoLevel(0) ;
//		while(m_cur < nbSplits())
//		{
//			VSplit<PFP>* vs = m_splits[m_cur] ;
//			++m_cur ;
//			unsigned int e = vs->getApprox() ;
//			m_approximator->approximate(vs, e) ;
//			edgeCollapse(vs, e) ;
//		}
//	}
}

template <typename PFP>
void ProgressiveMesh<PFP>::setDetailAmount(double a)
{
	m_detailAmount = a ;
	unsigned int c = m_cur ;
	gotoLevel(nbSplits()) ;
	gotoLevel(c) ;
}

template <typename PFP>
void ProgressiveMesh<PFP>::localizeDetailVectors()
{
	if(m_positionApproximator->getPredictor() && !m_localFrameDetailVectors)
	{
		bool quantizationWasApplied = quantizationApplied ;
		unsigned int nbCodeVectors = 0 ;
		if(quantizationWasApplied)
		{
			nbCodeVectors = q->getNbCodeVectors() ;
			resetDetailVectors() ;
		}
		m_localFrameDetailVectors = true ;
		gotoLevel(nbSplits()) ;
		while(m_cur > 0)
		{
			Dart d = m_splits[m_cur-1]->getEdge() ;
			Dart dd2 = m_splits[m_cur-1]->getRightEdge() ;
384
			typename PFP::MATRIX33 localFrame = Algo::Surface::Geometry::vertexLocalFrame<PFP>(m_map, dd2, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
			VEC3 det = m_positionApproximator->getDetail(d) ;
			det = localFrame * det ;
			m_positionApproximator->setDetail(d, det) ;
			refine() ;
		}
		quantizationInitialized = false ;
		initQuantization() ;
		if(quantizationWasApplied)
			quantizeDetailVectors(nbCodeVectors) ;
	}
}

template <typename PFP>
void ProgressiveMesh<PFP>::globalizeDetailVectors()
{
	if(!m_predictors.empty() && m_localFrameDetailVectors)
	{
		bool quantizationWasApplied = quantizationApplied ;
		unsigned int nbCodeVectors = 0 ;
		if(quantizationWasApplied)
		{
			nbCodeVectors = q->getNbCodeVectors() ;
			resetDetailVectors() ;
		}
		m_localFrameDetailVectors = false ;
		gotoLevel(nbSplits()) ;
		while(m_cur > 0)
		{
			Dart d = m_splits[m_cur-1]->getEdge() ;
			Dart dd2 = m_splits[m_cur-1]->getRightEdge() ;
415
			typename PFP::MATRIX33 localFrame = Algo::Surface::Geometry::vertexLocalFrame<PFP>(m_map, dd2, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
			typename PFP::MATRIX33 invLocalFrame ;
			localFrame.invert(invLocalFrame) ;
			VEC3 det = m_positionApproximator->getDetail(d) ;
			det = invLocalFrame * det ;
			m_positionApproximator->setDetail(d, det) ;
			refine() ;
		}
		quantizationInitialized = false ;
		initQuantization() ;
		if(quantizationWasApplied)
			quantizeDetailVectors(nbCodeVectors) ;
	}
}

template <typename PFP>
void ProgressiveMesh<PFP>::initQuantization()
{
	if(m_positionApproximator->getPredictor() && !quantizationInitialized)
	{
		gotoLevel(nbSplits()) ;
		originalDetailVectors.resize(m_splits.size()) ;
		for(unsigned int i = 0; i < m_splits.size(); ++i)
438
			originalDetailVectors[i] = m_positionApproximator->getDetail(m_splits[i]->getEdge(),0) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
439 440
		q = new Quantization<VEC3>(originalDetailVectors) ;
		quantizationInitialized = true ;
441
		CGoGNout << "  Differential Entropy -> " << q->getDifferentialEntropy() << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
442 443 444 445 446 447 448 449 450 451 452 453 454
	}
}

template <typename PFP>
void ProgressiveMesh<PFP>::quantizeDetailVectors(unsigned int nbClasses)
{
	initQuantization() ;
	if(quantizationInitialized)
	{
		gotoLevel(nbSplits()) ;
		std::vector<VEC3> resultat;
		q->vectorQuantizationNbRegions(nbClasses, resultat) ;
		for(unsigned int i = 0; i < m_splits.size(); ++i)
455
			m_positionApproximator->setDetail(m_splits[i]->getEdge(), 0, resultat[i]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
456 457
		quantizationApplied = true ;
		gotoLevel(0) ;
458
		CGoGNout << "Discrete Entropy -> " << q->getDiscreteEntropy() << " (codebook size : " << q->getNbCodeVectors() << ")" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
/*
		Point p;
		p.x = q->getEntropieDiscrete() ;
		p.y = computeDistance2() ;
		p.nbClasses = q->getNbClasses() ;
		courbe.push_back(p) ;
*/
	}
}

template <typename PFP>
void ProgressiveMesh<PFP>::quantizeDetailVectors(float distortion)
{
	initQuantization() ;
	if(quantizationInitialized)
	{
		gotoLevel(nbSplits()) ;
		std::vector<typename PFP::VEC3> resultat;
		q->vectorQuantizationDistortion(distortion, resultat) ;
		for(unsigned int i = 0; i < m_splits.size(); ++i)
			m_positionApproximator->setDetail(m_splits[i]->getEdge(), resultat[i]) ;
		quantizationApplied = true ;
		gotoLevel(0) ;
482
		CGoGNout << "Discrete Entropy -> " << q->getDiscreteEntropy() << " (codebook size : " << q->getNbCodeVectors() << ")" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	}
}

template <typename PFP>
void ProgressiveMesh<PFP>::resetDetailVectors()
{
	if(quantizationInitialized)
	{
		gotoLevel(nbSplits()) ;
		for(unsigned int i = 0; i < m_splits.size(); ++i)
			m_positionApproximator->setDetail(m_splits[i]->getEdge(), originalDetailVectors[i]) ;
		delete q ;
		quantizationInitialized = false ;
		quantizationApplied = false ;
		gotoLevel(0) ;
	}
}

/*
template <typename PFP>
float ProgressiveMesh<PFP>::computeDistance2()
{
	float distance = 0; // sum of 2-distance between original vertices and new vertices

	gotoLevel(0) ; // mesh reconstruction from detail vectors
	DartMarker mUpdate(m_map) ;
	for(Dart d = m_map.begin(); d != m_map.end(); m_map.next(d)) // vertices loop
	{
		if(!mUpdate.isMarked(d))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
513
			mUpdate.markOrbit<VERTEX>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
			EMB* dEmb = reinterpret_cast<EMB*>(m_map.getVertexEmb(d)) ;
			// computes the 2-distance between original vertex and new vertex
			dEmb->updateDistance2() ;
			distance += dEmb->getDistance2() ;
		}
	}

	return distance ;
}

template <typename PFP>
void ProgressiveMesh<PFP>::calculCourbeDebitDistortion()
{
	Dart d;
	EMB* dEmb;
	std::vector<Vector3f> source;
	std::vector<Vector3f> resultat;
	float distance;
	Point p;

534
	CGoGNout << "calcul de la courbe débit distortion " << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

	// get original detail vectors
	for(unsigned int i = 0; i < m_splits.size(); ++i)
	{
		source.push_back(Vector3f(*(m_splits.at(i)->getDetailDown())));
		source.push_back(Vector3f(*(m_splits.at(i)->getDetailUp())));
	}

	// vector quantization initialisation
	Quantization<Vector3f> q (source);
	q.vectorQuantizationInit();
	entropieDifferentielle = q.getEntropieDifferentielle();
	determinantSigma = q.getDeterminantSigma();
	traceSigma = q.getTraceSigma();


	// several quantizations of the same detail vectors to compute the curve
	for(unsigned int i = 8 ; i < m_splits.size() ; i *= 2)
	{
		q.vectorQuantization(i, resultat);

		// insert new vectors into the model to compute the distance
		for(unsigned int j = 0; j < m_splits.size(); ++j)
		{
			gmtl::Vec3f* v = resultat.at(j*2).getGmtl();
			m_splits.at(j)->setDetailDown(v);
			v = resultat.at(j*2+1).getGmtl();
			m_splits.at(j)->setDetailUp(v);
		}
		distance = computeDistance2();

		p.x = q.getEntropieDiscrete();
		p.y = distance;
		p.nbClasses = q.getNbClasses();
		courbe.push_back(p);
		// returns to coarse mesh
		gotoLevel(nbSplits());
572
		CGoGNout << "..." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
573 574 575 576 577 578
	}
	q.erase();
}
*/

} //namespace PMesh
579
} // Surface
Pierre Kraemer's avatar
Pierre Kraemer committed
580
} //namespace Algo
581
}
Pierre Kraemer's avatar
Pierre Kraemer committed
582
} //namespace CGoGN