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
namespace PMesh
{

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

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

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

	m_initOk = true ;

112
	CGoGNout << "  initializing approximators.." << CGoGNflush ;
113
	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
114 115 116 117
	{
		if(! (*it)->init())
			m_initOk = false ;
		if((*it)->getApproximatedAttributeName() == "position")
118
			m_positionApproximator = reinterpret_cast<Algo::Surface::Decimation::Approximator<PFP, VEC3, EDGE>*>(*it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
119
	}
120
	CGoGNout << "..done" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
121

122
	CGoGNout << "  initializing predictors.." << CGoGNflush ;
123
	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
124 125
		if(! (*it)->init())
			m_initOk = false ;
126
	CGoGNout << "..done" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
127

128
	CGoGNout << "  initializing selector.." << CGoGNflush ;
129 130 131 132 133 134 135 136 137 138 139
	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(
140
        MAP& map, DartMarker<MAP>& inactive,
141
		Algo::Surface::Decimation::Selector<PFP>* selector, std::vector<Algo::Surface::Decimation::ApproximatorGen<PFP>*>& approximators,
142
        VertexAttribute<VEC3,MAP>& position
143
	) :
144
    m_map(map), position(position), inactiveMarker(inactive), m_selector(selector), m_approximators(approximators)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
{
	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
163
	m_initOk = m_selector->init() ;
164
	CGoGNout << "..done" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178

	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 ;
179
	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
180
		delete (*it) ;
181
	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
182 183 184 185 186 187 188 189
		delete (*it) ;
	if(quantizationInitialized)
		delete q ;
}

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

	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

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

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

		edgeCollapse(vs) ;							// collapse edge

218 219 220
		unsigned int newV = Algo::Topo::setOrbitEmbeddingOnNewCell<VERTEX>(m_map,d2);
		unsigned int newE1 = Algo::Topo::setOrbitEmbeddingOnNewCell<EDGE>(m_map,d2);
		unsigned int newE2 = Algo::Topo::setOrbitEmbeddingOnNewCell<EDGE>(m_map,dd2);
Pierre Kraemer's avatar
Pierre Kraemer committed
221 222 223 224
		vs->setApproxV(newV) ;
		vs->setApproxE1(newE1) ;
		vs->setApproxE2(newE2) ;

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

	initQuantization() ;
}

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

248 249
	inactiveMarker.template markOrbit<FACE>(d) ;
	inactiveMarker.template markOrbit<FACE>(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263

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

264 265
	inactiveMarker.template unmarkOrbit<FACE>(d) ;
	inactiveMarker.template unmarkOrbit<FACE>(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
266 267 268 269 270 271 272 273 274 275 276
}

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 ;

277 278
	// Dart d = vs->getEdge() ;
	// Dart dd = m_map.phi2(d) ;		// get some darts
Pierre Kraemer's avatar
Pierre Kraemer committed
279 280 281 282 283
	Dart d2 = vs->getLeftEdge() ;
	Dart dd2 = vs->getRightEdge() ;

	edgeCollapse(vs) ;	// collapse edge

284
    Algo::Topo::setOrbitEmbedding<VERTEX>(m_map, d2, vs->getApproxV()) ;
285 286 287
	Algo::Topo::setOrbitEmbedding<EDGE>(m_map, d2, vs->getApproxE1()) ;
	Algo::Topo::setOrbitEmbedding<EDGE>(m_map, dd2, vs->getApproxE2()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
}

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

306 307 308 309 310 311
	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
312 313 314

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

	typename PFP::MATRIX33 invLocalFrame ;
	if(m_localFrameDetailVectors)
	{
326
		typename PFP::MATRIX33 localFrame = Algo::Geometry::vertexLocalFrame<PFP>(m_map, dd2, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
327 328 329 330 331
		localFrame.invert(invLocalFrame) ;
	}

	vertexSplit(vs) ; // split vertex

332 333 334 335 336 337
	Algo::Topo::setOrbitEmbedding<VERTEX>(m_map, d, v1) ;	// embed the
	Algo::Topo::setOrbitEmbedding<VERTEX>(m_map, dd, v2) ;	// new vertices
	Algo::Topo::setOrbitEmbedding<EDGE>(m_map, d1, e1) ;
	Algo::Topo::setOrbitEmbedding<EDGE>(m_map, d2, e2) ;	// and new edges
	Algo::Topo::setOrbitEmbedding<EDGE>(m_map, dd1, e3) ;
	Algo::Topo::setOrbitEmbedding<EDGE>(m_map, dd2, e4) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
338 339 340

	if(!m_predictors.empty())
	{
341 342
		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
343 344 345 346 347 348 349 350 351
		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) ;
352
			if((*ait)->getType() == Algo::Surface::Decimation::A_hHalfCollapse)
Pierre Kraemer's avatar
Pierre Kraemer committed
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
			{
				(*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)
{
368
	if(l == m_cur || l > m_splits.size())
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		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() ;
423
			typename PFP::MATRIX33 localFrame = Algo::Geometry::vertexLocalFrame<PFP>(m_map, dd2, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 449 450 451 452 453
			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() ;
454
			typename PFP::MATRIX33 localFrame = Algo::Geometry::vertexLocalFrame<PFP>(m_map, dd2, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
			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)
477
			originalDetailVectors[i] = m_positionApproximator->getDetail(m_splits[i]->getEdge(),0) ;
478
		q = new Algo::PMesh::Quantization<VEC3>(originalDetailVectors) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
479
		quantizationInitialized = true ;
480
		CGoGNout << "  Differential Entropy -> " << q->getDifferentialEntropy() << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
481 482 483 484 485 486 487 488 489 490 491 492 493
	}
}

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)
494
			m_positionApproximator->setDetail(m_splits[i]->getEdge(), 0, resultat[i]) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
495 496
		quantizationApplied = true ;
		gotoLevel(0) ;
497
		CGoGNout << "Discrete Entropy -> " << q->getDiscreteEntropy() << " (codebook size : " << q->getNbCodeVectors() << ")" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
/*
		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) ;
521
		CGoGNout << "Discrete Entropy -> " << q->getDiscreteEntropy() << " (codebook size : " << q->getNbCodeVectors() << ")" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
547
    DartMarker<MAP> mUpdate(m_map) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
548 549 550 551
	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
552
			mUpdate.markOrbit<VERTEX>(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
			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;

573
	CGoGNout << "calcul de la courbe débit distortion " << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 606 607 608 609 610

	// 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());
611
		CGoGNout << "..." << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
612 613 614 615 616
	}
	q.erase();
}
*/

617
} // namespace PMesh
618

619
} // namespace Surface
620

621
} // namespace Algo
622

623
} // namespace CGoGN