map2MR_PM.hpp 8.95 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 25 26 27 28 29 30 31 32 33 34 35
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/


namespace CGoGN
{

namespace Algo
{

namespace Multiresolution
{

template <typename PFP>
untereiner's avatar
untereiner committed
36 37
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
38

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

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
53
void Map2MR_PM<PFP>::createPM(Algo::Decimation::SelectorType s, Algo::Decimation::ApproximatorType a, const FunctorSelect& select)
54 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
{
	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
91
		m_selector = new Algo::Decimation::EdgeSelector_MapOrder<PFP>(m_map, m_position, m_approximators, select) ;
92 93
		break ; }
	case Algo::Decimation::S_Random : {
untereiner's avatar
untereiner committed
94
		m_selector = new Algo::Decimation::EdgeSelector_Random<PFP>(m_map, m_position, m_approximators, select) ;
95 96
		break ; }
	case Algo::Decimation::S_EdgeLength : {
untereiner's avatar
untereiner committed
97
		m_selector = new Algo::Decimation::EdgeSelector_Length<PFP>(m_map, m_position, m_approximators, select) ;
98 99
		break ; }
	case Algo::Decimation::S_QEM : {
untereiner's avatar
untereiner committed
100
		m_selector = new Algo::Decimation::EdgeSelector_QEM<PFP>(m_map, m_position, m_approximators, select) ;
101 102
		break ; }
	case Algo::Decimation::S_MinDetail : {
untereiner's avatar
untereiner committed
103
		m_selector = new Algo::Decimation::EdgeSelector_MinDetail<PFP>(m_map, m_position, m_approximators, select) ;
104 105
		break ; }
	case Algo::Decimation::S_Curvature : {
untereiner's avatar
untereiner committed
106
		m_selector = new Algo::Decimation::EdgeSelector_Curvature<PFP>(m_map, m_position, m_approximators, select) ;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		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
129 130 131
	CGoGNout << "  initializing selector.." << CGoGNflush ;
	m_initOk = m_selector->init() ;
	CGoGNout << "..done" << CGoGNendl ;
132 133
}

untereiner's avatar
untereiner committed
134 135


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

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

	//create the new level
	m_map.addLevelFront();
	m_map.setCurrentLevel(0);

	m_map.printMR();

	DartMarkerStore me(m_map); 	//mark edges not to collapse
153 154 155 156 157 158 159 160

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

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

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

176 177
			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())
178
			{
179 180
				me.markOrbit<FACE>(it);
			}
181 182


183
			++nbDeletedVertex ;
184

185 186
			Dart d2 = m_map.phi2(m_map.phi_1(d)) ;
			Dart dd2 = m_map.phi2(m_map.phi_1(m_map.phi2(d))) ;
187

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

190 191 192 193 194 195 196
			collapseEdge(d);

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

			if(nbDeletedVertex <= nbWantedPerLevel)
				finished = true ;
		}
197 198
	}

199

200 201 202
	CGoGNout << "..done (" << nbDeletedVertex << " vertices)" << CGoGNendl ;

	m_map.printMR();
203 204
}

untereiner's avatar
untereiner committed
205 206 207 208

template <typename PFP>
void Map2MR_PM<PFP>::collapseEdge(Dart d)
{
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	//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)));

//	//duplication :
	m_map.incDartLevel(m_map.phi2(m_map.phi1(d)));
	m_map.incDartLevel(m_map.phi2(m_map.phi_1(d)));
	m_map.incDartLevel(m_map.phi2(m_map.phi1(m_map.phi2(d))));
	m_map.incDartLevel(m_map.phi2(m_map.phi_1(m_map.phi2(d))));

	m_map.printMR();

	m_map.duplicateDart2(m_map.phi2(m_map.phi1(d)));
	m_map.duplicateDart2(m_map.phi2(m_map.phi_1(d)));
	m_map.duplicateDart2(m_map.phi2(m_map.phi1(m_map.phi2(d))));
	m_map.duplicateDart2(m_map.phi2(m_map.phi_1(m_map.phi2(d))));

	m_map.printMR();
untereiner's avatar
untereiner committed
231 232


233
	m_map.collapseEdge(d);
untereiner's avatar
untereiner committed
234 235 236
}


237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
template <typename PFP>
void Map2MR_PM<PFP>::coarsen()
{
	assert(m_map.getCurrentLevel() > 0 || !"coarsen : called on level 0") ;

	m_map.decCurrentLevel() ;
}

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

	m_map.incCurrentLevel() ;
}

} // namespace Multiresolution

} // namespace Algo

} // namespace CGoGN