pmesh.hpp 20.5 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
	m_map(map), positionsTable(position), inactiveMarker(inactive)
Pierre Kraemer's avatar
Pierre Kraemer committed
46
{
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
		case Algo::Surface::Decimation::S_MapOrder : {
86
			m_selector = new Algo::Surface::Decimation::EdgeSelector_MapOrder<PFP>(m_map, positionsTable, m_approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
87
			break ; }
88
		case Algo::Surface::Decimation::S_Random : {
89
			m_selector = new Algo::Surface::Decimation::EdgeSelector_Random<PFP>(m_map, positionsTable, m_approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
90
			break ; }
91
		case Algo::Surface::Decimation::S_EdgeLength : {
92
			m_selector = new Algo::Surface::Decimation::EdgeSelector_Length<PFP>(m_map, positionsTable, m_approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
93
			break ; }
94
		case Algo::Surface::Decimation::S_QEM : {
95
			m_selector = new Algo::Surface::Decimation::EdgeSelector_QEM<PFP>(m_map, positionsTable, m_approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
96
			break ; }
97
		case Algo::Surface::Decimation::S_MinDetail : {
98
			m_selector = new Algo::Surface::Decimation::EdgeSelector_MinDetail<PFP>(m_map, positionsTable, m_approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
99
			break ; }
100
		case Algo::Surface::Decimation::S_Curvature : {
101
			m_selector = new Algo::Surface::Decimation::EdgeSelector_Curvature<PFP>(m_map, positionsTable, m_approximators) ;
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 ;
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
	m_initOk = m_selector->init() ;
	CGoGNout << "..done" << CGoGNendl ;

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

template <typename PFP>
ProgressiveMesh<PFP>::ProgressiveMesh(
		MAP& map, DartMarker& inactive,
		Algo::Surface::Decimation::Selector<PFP>* selector, std::vector<Algo::Surface::Decimation::ApproximatorGen<PFP>*>& approximators,
		VertexAttribute<typename PFP::VEC3>& position
	) :
	m_map(map), m_selector(selector), m_approximators(approximators), positionsTable(position), inactiveMarker(inactive)
{
	CGoGNout << "  initializing approximators.." << CGoGNflush ;
	for(typename std::vector<Algo::Surface::Decimation::ApproximatorGen<PFP>*>::iterator it = m_approximators.begin(); it != m_approximators.end(); ++it)
	{
		if(! (*it)->init())
			m_initOk = false ;
		if((*it)->getApproximatedAttributeName() == "position")
			m_positionApproximator = reinterpret_cast<Algo::Surface::Decimation::Approximator<PFP, VEC3, EDGE>*>(*it) ;
	}
	CGoGNout << "..done" << CGoGNendl ;

	CGoGNout << "  initializing predictors.." << CGoGNflush ;
	for(typename std::vector<Algo::Surface::Decimation::PredictorGen<PFP>*>::iterator it = m_predictors.begin(); it != m_predictors.end(); ++it)
		if(! (*it)->init())
			m_initOk = false ;
	CGoGNout << "..done" << CGoGNendl ;

	CGoGNout << "  initializing selector.." << CGoGNflush ;
Pierre Kraemer's avatar
Pierre Kraemer committed
159
	m_initOk = m_selector->init() ;
160
	CGoGNout << "..done" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174

	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 ;
175
	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
176
		delete (*it) ;
177
	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
178 179 180 181 182 183 184 185
		delete (*it) ;
	if(quantizationInitialized)
		delete q ;
}

template <typename PFP>
void ProgressiveMesh<PFP>::createPM(unsigned int percentWantedVertices)
{
186
	unsigned int nbVertices = m_map.template getNbOrbits<VERTEX>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
187
	unsigned int nbWantedVertices = nbVertices * percentWantedVertices / 100 ;
188
	CGoGNout << "  creating PM (" << nbVertices << " vertices).." << /* flush */ CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	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

204
		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
205 206 207 208 209 210 211 212 213
		{
			(*it)->approximate(d) ;					// compute approximated attributes with its associated detail
			(*it)->saveApprox(d) ;
		}

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

		edgeCollapse(vs) ;							// collapse edge

214 215 216
		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
217 218 219 220
		vs->setApproxV(newV) ;
		vs->setApproxE1(newE1) ;
		vs->setApproxE2(newE2) ;

221
		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
222 223 224 225 226 227 228 229 230 231 232
			(*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() ;
233
	CGoGNout << "..done (" << nbVertices << " vertices)" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
234 235 236 237 238 239 240 241 242 243

	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
244 245
	inactiveMarker.markOrbit<FACE>(d) ;
	inactiveMarker.markOrbit<FACE>(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259

	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
260 261
	inactiveMarker.unmarkOrbit<FACE>(d) ;
	inactiveMarker.unmarkOrbit<FACE>(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
262 263 264 265 266 267 268 269 270 271 272
}

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 ;

273 274
	// Dart d = vs->getEdge() ;
	// Dart dd = m_map.phi2(d) ;		// get some darts
Pierre Kraemer's avatar
Pierre Kraemer committed
275 276 277 278 279
	Dart d2 = vs->getLeftEdge() ;
	Dart dd2 = vs->getRightEdge() ;

	edgeCollapse(vs) ;	// collapse edge

280 281 282
	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
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
}

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) ;

301 302 303 304 305 306
	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
307 308 309

	if(!m_predictors.empty())
	{
310
		for(typename std::vector<Algo::Surface::Decimation::PredictorGen<PFP>*>::iterator pit = m_predictors.begin();
Pierre Kraemer's avatar
Pierre Kraemer committed
311 312 313 314 315 316 317 318 319 320
			pit != m_predictors.end();
			++pit)
		{
			(*pit)->predict(d2, dd2) ;
		}
	}

	typename PFP::MATRIX33 invLocalFrame ;
	if(m_localFrameDetailVectors)
	{
321
		typename PFP::MATRIX33 localFrame = Algo::Geometry::vertexLocalFrame<PFP>(m_map, dd2, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
322 323 324 325 326
		localFrame.invert(invLocalFrame) ;
	}

	vertexSplit(vs) ; // split vertex

327 328 329 330 331 332
	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
333 334 335

	if(!m_predictors.empty())
	{
336 337
		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
338 339 340 341 342 343 344 345 346
		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) ;
347
			if((*ait)->getType() == Algo::Surface::Decimation::A_hHalfCollapse)
Pierre Kraemer's avatar
Pierre Kraemer committed
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
			{
				(*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() ;
418
			typename PFP::MATRIX33 localFrame = Algo::Geometry::vertexLocalFrame<PFP>(m_map, dd2, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
			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() ;
449
			typename PFP::MATRIX33 localFrame = Algo::Geometry::vertexLocalFrame<PFP>(m_map, dd2, positionsTable) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
			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)
472
			originalDetailVectors[i] = m_positionApproximator->getDetail(m_splits[i]->getEdge(),0) ;
473
		q = new Algo::PMesh::Quantization<VEC3>(originalDetailVectors) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
474
		quantizationInitialized = true ;
475
		CGoGNout << "  Differential Entropy -> " << q->getDifferentialEntropy() << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
476 477 478 479 480 481 482 483 484 485 486 487 488
	}
}

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)
489
			m_positionApproximator->setDetail(m_splits[i]->getEdge(), 0, resultat[i]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
490 491
		quantizationApplied = true ;
		gotoLevel(0) ;
492
		CGoGNout << "Discrete Entropy -> " << q->getDiscreteEntropy() << " (codebook size : " << q->getNbCodeVectors() << ")" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
/*
		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) ;
516
		CGoGNout << "Discrete Entropy -> " << q->getDiscreteEntropy() << " (codebook size : " << q->getNbCodeVectors() << ")" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	}
}

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
547
			mUpdate.markOrbit<VERTEX>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
			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;

568
	CGoGNout << "calcul de la courbe débit distortion " << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

	// 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());
606
		CGoGNout << "..." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
607 608 609 610 611
	}
	q.erase();
}
*/

612 613 614 615
} // namespace PMesh
} // namespace Surface
} // namespace Algo
} // namespace CGoGN