map2MR_PM.hpp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

untereiner's avatar
untereiner committed
25
#include "Container/attributeMultiVector.h"
26 27 28 29 30 31 32 33 34 35 36

namespace CGoGN
{

namespace Algo
{

namespace Multiresolution
{

template <typename PFP>
untereiner's avatar
untereiner committed
37 38
Map2MR_PM<PFP>::Map2MR_PM(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3>& position) : m_map(map), m_position(position)
{
untereiner's avatar
untereiner committed
39

untereiner's avatar
untereiner committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53
}

template <typename PFP>
Map2MR_PM<PFP>::~Map2MR_PM()
{
	if(m_selector)
		delete m_selector ;
	for(typename std::vector<Algo::Decimation::ApproximatorGen<PFP>*>::iterator it = m_approximators.begin(); it != m_approximators.end(); ++it)
		delete (*it) ;
	for(typename std::vector<Algo::Decimation::PredictorGen<PFP>*>::iterator it = m_predictors.begin(); it != m_predictors.end(); ++it)
		delete (*it) ;
}

template <typename PFP>
untereiner's avatar
untereiner committed
54
void Map2MR_PM<PFP>::createPM(Algo::Decimation::SelectorType s, Algo::Decimation::ApproximatorType a, const FunctorSelect& select)
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
{
	CGoGNout << "  creating approximator and predictor.." << CGoGNflush ;
	switch(a)
	{
	case Algo::Decimation::A_QEM : {
		m_approximators.push_back(new Algo::Decimation::Approximator_QEM<PFP>(m_map, m_position)) ;
		break ; }
	case Algo::Decimation::A_MidEdge : {
		m_approximators.push_back(new Algo::Decimation::Approximator_MidEdge<PFP>(m_map, m_position)) ;
		break ; }
	case Algo::Decimation::A_HalfCollapse : {
		Algo::Decimation::Predictor_HalfCollapse<PFP>* pred = new Algo::Decimation::Predictor_HalfCollapse<PFP>(m_map, m_position) ;
		m_predictors.push_back(pred) ;
		m_approximators.push_back(new Algo::Decimation::Approximator_HalfCollapse<PFP>(m_map, m_position, pred)) ;
		break ; }
	case Algo::Decimation::A_CornerCutting : {
		Algo::Decimation::Predictor_CornerCutting<PFP>* pred = new Algo::Decimation::Predictor_CornerCutting<PFP>(m_map, m_position) ;
		m_predictors.push_back(pred) ;
		m_approximators.push_back(new Algo::Decimation::Approximator_CornerCutting<PFP>(m_map, m_position, pred)) ;
		break ; }
	case Algo::Decimation::A_TangentPredict1 : {
		Algo::Decimation::Predictor_TangentPredict1<PFP>* pred = new Algo::Decimation::Predictor_TangentPredict1<PFP>(m_map, m_position) ;
		m_predictors.push_back(pred) ;
		m_approximators.push_back(new Algo::Decimation::Approximator_MidEdge<PFP>(m_map, m_position, pred)) ;
		break ; }
	case Algo::Decimation::A_TangentPredict2 : {
		Algo::Decimation::Predictor_TangentPredict2<PFP>* pred = new Algo::Decimation::Predictor_TangentPredict2<PFP>(m_map, m_position) ;
		m_predictors.push_back(pred) ;
		m_approximators.push_back(new Algo::Decimation::Approximator_MidEdge<PFP>(m_map, m_position, pred)) ;
		break ; }
	}
	CGoGNout << "..done" << CGoGNendl ;

	CGoGNout << "  creating selector.." << CGoGNflush ;
	switch(s)
	{
	case Algo::Decimation::S_MapOrder : {
untereiner's avatar
untereiner committed
92
		m_selector = new Algo::Decimation::EdgeSelector_MapOrder<PFP>(m_map, m_position, m_approximators, select) ;
93 94
		break ; }
	case Algo::Decimation::S_Random : {
untereiner's avatar
untereiner committed
95
		m_selector = new Algo::Decimation::EdgeSelector_Random<PFP>(m_map, m_position, m_approximators, select) ;
96 97
		break ; }
	case Algo::Decimation::S_EdgeLength : {
untereiner's avatar
untereiner committed
98
		m_selector = new Algo::Decimation::EdgeSelector_Length<PFP>(m_map, m_position, m_approximators, select) ;
99 100
		break ; }
	case Algo::Decimation::S_QEM : {
untereiner's avatar
untereiner committed
101
		m_selector = new Algo::Decimation::EdgeSelector_QEM<PFP>(m_map, m_position, m_approximators, select) ;
102 103
		break ; }
	case Algo::Decimation::S_MinDetail : {
untereiner's avatar
untereiner committed
104
		m_selector = new Algo::Decimation::EdgeSelector_MinDetail<PFP>(m_map, m_position, m_approximators, select) ;
105 106
		break ; }
	case Algo::Decimation::S_Curvature : {
untereiner's avatar
untereiner committed
107
		m_selector = new Algo::Decimation::EdgeSelector_Curvature<PFP>(m_map, m_position, m_approximators, select) ;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
		break ; }
	}
	CGoGNout << "..done" << CGoGNendl ;

	m_initOk = true ;

	CGoGNout << "  initializing approximators.." << CGoGNflush ;
	for(typename std::vector<Algo::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::Decimation::Approximator<PFP, VEC3>*>(*it) ;
	}
	CGoGNout << "..done" << CGoGNendl ;

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

untereiner's avatar
untereiner committed
130

131 132
}

untereiner's avatar
untereiner committed
133 134


135
template <typename PFP>
untereiner's avatar
untereiner committed
136
void Map2MR_PM<PFP>::addNewLevel(unsigned int percentWantedVertices)
137
{
138 139
	unsigned int nbVertices = m_map.template getNbOrbits<VERTEX>() ;
	unsigned int nbWantedVertices = nbVertices * percentWantedVertices / 100 ;
140 141 142 143 144

	unsigned int nbDeletedVertex=0;
	unsigned int percentWantedPerLevel = 20;
	unsigned int nbWantedPerLevel = nbWantedVertices * percentWantedPerLevel / 100 ;

untereiner's avatar
untereiner committed
145 146 147 148
	CGoGNout << "  initializing selector.." << CGoGNflush ;
	m_initOk = m_selector->init() ;
	CGoGNout << "..done" << CGoGNendl ;

untereiner's avatar
untereiner committed
149 150
	std::vector<Dart> edges;
	edges.reserve(nbWantedPerLevel);
151

untereiner's avatar
untereiner committed
152
	std::cout << "stops at  : " << nbWantedPerLevel << std::endl;
153

untereiner's avatar
untereiner committed
154
	DartMarkerStore me(m_map); 	//mark edges not to collapse
untereiner's avatar
untereiner committed
155

156 157 158 159 160 161 162
	bool finished = false ;
	Dart d ;
	while(!finished)
	{
		if(!m_selector->nextEdge(d))
			break ;

163
		if(!me.isMarked(d))
164
		{
165
			//Mark le 1 voisinage
166 167 168
			Dart dt = d;
			do
			{
169 170
				Traversor2VF<typename PFP::MAP> tf(m_map, dt) ;
				for(Dart it = tf.begin(); it != tf.end(); it = tf.next())
171
				{
172
					me.markOrbit<FACE>(it);
173 174 175 176 177
				}

				dt = m_map.phi1(dt);
			}while(dt != d);

178 179
			Traversor2VF<typename PFP::MAP> tf(m_map, m_map.phi_1(m_map.phi2(d))) ;
			for(Dart it = tf.begin(); it != tf.end(); it = tf.next())
180
			{
181 182
				me.markOrbit<FACE>(it);
			}
183

184
			++nbDeletedVertex ;
185

untereiner's avatar
untereiner committed
186
			edges.push_back(d);
187
		}
188

untereiner's avatar
untereiner committed
189 190 191
		//std::cout << "nbDeletedVertex  : " << nbDeletedVertex << std::endl;

		if(nbDeletedVertex >= nbWantedPerLevel)
untereiner's avatar
untereiner committed
192 193
			finished = true ;
	}
194

195

untereiner's avatar
untereiner committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	//create the new level
	m_map.addLevelFront();
	m_map.setCurrentLevel(0);

	AttributeContainer& attribs = m_map.getMRAttributeContainer();
	AttributeMultiVector<unsigned int>* attribLevel = m_map.getMRLevelAttributeVector();
	AttributeMultiVector<unsigned int>* attribDarts = m_map.getMRDartAttributeVector(0);

	for(unsigned int i = attribs.begin(); i != attribs.end(); attribs.next(i))
	{
		if((*attribDarts)[i] == MRNULL)
			++(*attribLevel)[i];
	}

	for(std::vector<Dart>::iterator it = edges.begin() ; it != edges.end() ; ++it)
	{
		collapseEdge(*it);
	}
untereiner's avatar
untereiner committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227

//	Dart d2 = m_map.phi2(m_map.phi_1(d)) ;
//	Dart dd2 = m_map.phi2(m_map.phi_1(m_map.phi2(d))) ;
//
//	m_selector->updateBeforeCollapse(d) ;		// update selector
//
//	collapseEdge(d);
//
//	m_selector->updateAfterCollapse(d2, dd2) ;	// update selector
//
//
//	CGoGNout << "..done (" << nbDeletedVertex << " vertices)" << CGoGNendl ;
//
//	m_map.printMR();
228 229
}

untereiner's avatar
untereiner committed
230 231 232 233

template <typename PFP>
void Map2MR_PM<PFP>::collapseEdge(Dart d)
{
234 235 236 237 238 239 240 241 242 243
	//incremente le dartLevel des brins des faces a supprimer
	m_map.incDartLevel(d);
	m_map.incDartLevel(m_map.phi1(d));
	m_map.incDartLevel(m_map.phi_1(d));
	m_map.incDartLevel(m_map.phi2(d));
	m_map.incDartLevel(m_map.phi_1(m_map.phi2(d)));
	m_map.incDartLevel(m_map.phi1(m_map.phi2(d)));

	m_map.printMR();

untereiner's avatar
untereiner committed
244 245 246 247 248 249
	m_map.duplicateDartAtOneLevel(d, 0);
	m_map.duplicateDartAtOneLevel(m_map.phi1(d), 0);
	m_map.duplicateDartAtOneLevel(m_map.phi_1(d), 0);
	m_map.duplicateDartAtOneLevel(m_map.phi2(d), 0);
	m_map.duplicateDartAtOneLevel(m_map.phi_1(m_map.phi2(d)), 0);
	m_map.duplicateDartAtOneLevel(m_map.phi1(m_map.phi2(d)), 0);
untereiner's avatar
untereiner committed
250

untereiner's avatar
untereiner committed
251 252 253 254
	m_map.duplicateDartAtOneLevel(m_map.phi2(m_map.phi1(d)), 0);
	m_map.duplicateDartAtOneLevel(m_map.phi2(m_map.phi_1(d)), 0);
	m_map.duplicateDartAtOneLevel(m_map.phi2(m_map.phi1(m_map.phi2(d))), 0);
	m_map.duplicateDartAtOneLevel(m_map.phi2(m_map.phi_1(m_map.phi2(d))), 0);
untereiner's avatar
untereiner committed
255

256
	m_map.collapseEdge(d);
untereiner's avatar
untereiner committed
257 258

	m_map.printMR();
untereiner's avatar
untereiner committed
259 260 261
}


262 263 264 265 266 267
template <typename PFP>
void Map2MR_PM<PFP>::coarsen()
{
	assert(m_map.getCurrentLevel() > 0 || !"coarsen : called on level 0") ;

	m_map.decCurrentLevel() ;
untereiner's avatar
untereiner committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

	TraversorV<typename PFP::MAP> tE(m_map);
	for(Dart d = tE.begin() ; d != tE.end() ; d = tE.next())
	{
		Dart dit = d;
		Dart dres = d;
		bool found = false;
		do
		{
			//std::cout << "emb["<<dit<<"] = " << m_map.template getEmbedding<VERTEX>(dit) << std::endl;

			if(m_map.getDartLevel(dit) == m_map.getCurrentLevel())
			{
				dres = dit;
				found = true;
			}

			dit = m_map.phi2(m_map.phi_1(dit));

		}while(!found & dit!=d);

		//std::cout << std::endl;

		m_map.template embedOrbit<VERTEX>(dres, m_map.template getEmbedding<VERTEX>(dres));
	}
293 294 295 296 297 298 299 300
}

template <typename PFP>
void Map2MR_PM<PFP>::refine()
{
	assert(m_map.getCurrentLevel() < m_map.getMaxLevel() || !"refine: called on max level") ;

	m_map.incCurrentLevel() ;
untereiner's avatar
untereiner committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

	TraversorV<typename PFP::MAP> tE(m_map);
	for(Dart d = tE.begin() ; d != tE.end() ; d = tE.next())
	{
		Dart dit = d;
		Dart dres = d;
		bool found = false;
		do
		{
			//std::cout << "emb["<<dit<<"] = " << m_map.template getEmbedding<VERTEX>(dit) << std::endl;

			if(m_map.getDartLevel(dit) == m_map.getCurrentLevel())
			{
				dres = dit;
				found = true;
			}

			dit = m_map.phi2(m_map.phi_1(dit));

		}while(!found & dit!=d);

		//std::cout << std::endl;

		m_map.template embedOrbit<VERTEX>(dres, m_map.template getEmbedding<VERTEX>(dres));
	}
326 327 328 329 330 331 332
}

} // namespace Multiresolution

} // namespace Algo

} // namespace CGoGN