decimation.hpp 8.91 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 33 34 35 36
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

namespace Decimation
{

template <typename PFP>
void decimate(
	typename PFP::MAP& map, SelectorType s, ApproximatorType a,
37
	std::vector<VertexAttribute<typename PFP::VEC3>* >& attribs, unsigned int nbWantedVertices, const FunctorSelect& selected,
38
	EdgeAttribute<typename PFP::REAL> *edgeErrors,
39
	void (*callback_wrapper)(void*, const void*), void* callback_object
Pierre Kraemer's avatar
Pierre Kraemer committed
40 41
)
{
42 43 44 45
	assert(attribs.size() >= 1 || !"Decimate: not enough attribs provided") ;
	assert(attribs[0]->name() == "position" || !"Decimate: first attribute is not position") ;
	VertexAttribute<typename PFP::VEC3> position = *(attribs[0]) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
46 47 48
	std::vector<ApproximatorGen<PFP>*> approximators ;
	EdgeSelector<PFP>* selector = NULL ;

49 50
	std::vector<VertexAttribute<typename PFP::VEC3>* > *v_approx = NULL ;

Pierre Kraemer's avatar
Pierre Kraemer committed
51 52 53
	switch(a)
	{
		case A_QEM :
54
			approximators.push_back(new Approximator_QEM<PFP>(map, attribs)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
55 56
			break ;
		case A_MidEdge :
57
			approximators.push_back(new Approximator_MidEdge<PFP>(map, attribs)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
58 59
			break ;
		case A_CornerCutting :
60
			approximators.push_back(new Approximator_CornerCutting<PFP>(map, attribs)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
61 62
			break ;
		case A_TangentPredict1 :
63
			approximators.push_back(new Approximator_MidEdge<PFP>(map, attribs)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
64 65
			break ;
		case A_TangentPredict2 :
66
			approximators.push_back(new Approximator_MidEdge<PFP>(map, attribs)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
67
			break ;
68
		case A_hHalfCollapse :
69
			approximators.push_back(new Approximator_HalfCollapse<PFP>(map, attribs)) ;
70
			break ;
71 72
		case A_ColorNaive :
		{
73 74
			v_approx = new std::vector<VertexAttribute<typename PFP::VEC3>* >[2] ;

75
			// pos
76
			v_approx[0].push_back(attribs[0]) ;
77
			approximators.push_back(new Approximator_QEM<PFP>(map, v_approx[0])) ;
78 79

			// col
80 81
			assert(attribs.size() >= 2 || !"Decimate: A_ColorNaive --> not enough attribs provided") ;
			v_approx[1].push_back(attribs[1]) ;
82
			approximators.push_back(new Approximator_ColorNaive<PFP>(map, v_approx[1])) ;
83 84 85 86
		}
		break ;
		case A_ColorQEMext :
		{
87
			// pos + col
88 89
			assert(attribs.size() >= 2 || !"Decimate: A_ColorQEMext --> not enough attribs provided") ;
			approximators.push_back(new Approximator_ColorQEMext<PFP>(map, attribs)) ;
90 91 92
		}
		break;
		case A_hQEM :
93
			// pos
94
			approximators.push_back(new Approximator_QEMhalfEdge<PFP>(map, attribs)) ;
95
		break ;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
		case A_hLightfieldHalf:
		{
			v_approx = new std::vector<VertexAttribute<typename PFP::VEC3>* >[3] ;

			// pos
			v_approx[0].push_back(attribs[0]) ;
			approximators.push_back(new Approximator_QEMhalfEdge<PFP>(map, v_approx[0])) ;

			// frame
			assert(attribs.size() >= 4 || !"Decimate: A_hLightfieldHalf --> not enough attribs provided") ;
			for (unsigned int i = 0 ; i < 3 ; ++i)
				v_approx[1].push_back(attribs[i+1]) ;
			approximators.push_back(new Approximator_FrameInterpolationHalfEdge<PFP>(map, v_approx[1])) ;

			// hemifunction
			assert(attribs.size() >= 5 || !"Decimate: A_hLightfieldHalf --> not enough attribs provided") ;
			for (unsigned int i = 0 ; i < attribs.size() - 4 ; ++i)
				v_approx[2].push_back(attribs[i+4]) ;
			approximators.push_back(new Approximator_HemiFuncCoefsHalfEdge<PFP>(map, v_approx[2])) ;
		}
		break ;
117
		case A_Lightfield :
118
		{
119
			v_approx = new std::vector<VertexAttribute<typename PFP::VEC3>* >[3] ;
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
			// pos
			v_approx[0].push_back(attribs[0]) ;
			approximators.push_back(new Approximator_QEM<PFP>(map, v_approx[0])) ;

			// frame
			assert(attribs.size() >= 4 || !"Decimate: A_Lightfield --> not enough attribs provided") ;
			for (unsigned int i = 0 ; i < 3 ; ++i)
				v_approx[1].push_back(attribs[i+1]) ;
			approximators.push_back(new Approximator_FrameInterpolation<PFP>(map, v_approx[1])) ;

			// hemifunction
			assert(attribs.size() >= 5 || !"Decimate: A_Lightfield --> not enough attribs provided") ;
			for (unsigned int i = 0 ; i < attribs.size() - 4 ; ++i)
				v_approx[2].push_back(attribs[i+4]) ;
			approximators.push_back(new Approximator_HemiFuncCoefs<PFP>(map, v_approx[2])) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
136
		}
137
		break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140 141 142
	}

	switch(s)
	{
		case S_MapOrder :
143
			selector = new EdgeSelector_MapOrder<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145
			break ;
		case S_Random :
146
			selector = new EdgeSelector_Random<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
147 148
			break ;
		case S_EdgeLength :
149
			selector = new EdgeSelector_Length<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
150
			break ;
151
		case S_QEMml :
152
			selector = new EdgeSelector_QEMml<PFP>(map, position, approximators, selected) ;
153
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
154
		case S_QEM :
155
			selector = new EdgeSelector_QEM<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157
			break ;
		case S_Curvature :
158
			selector = new EdgeSelector_Curvature<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
159 160
			break ;
		case S_MinDetail :
161
			selector = new EdgeSelector_MinDetail<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
162
			break ;
163 164 165
		case S_ColorNaive :
			selector = new EdgeSelector_ColorNaive<PFP>(map, position, approximators, selected) ;
			break ;
166 167 168
		case S_QEMextColor :
			selector = new EdgeSelector_QEMextColor<PFP>(map, position, approximators, selected) ;
			break ;
169 170 171
		case S_hQEMextColor :
			selector = new HalfEdgeSelector_QEMextColor<PFP>(map, position, approximators, selected) ;
			break ;
172
		case S_hQEMml :
173
			selector = new HalfEdgeSelector_QEMml<PFP>(map, position, approximators, selected) ;
174
			break ;
175 176
		case S_Lightfield :
			selector = new EdgeSelector_Lightfield<PFP>(map, position, approximators, selected) ;
177
			break ;
178 179 180
		case S_hLightfield :
			selector = new HalfEdgeSelector_Lightfield<PFP>(map, position, approximators, selected) ;
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183 184 185 186
	}

	for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
		(*it)->init() ;

	if(!selector->init())
187 188 189 190 191 192
	{
		delete selector ;

		for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
			delete (*it) ;

193 194
		delete[] v_approx ;

Pierre Kraemer's avatar
Pierre Kraemer committed
195
		return ;
196 197
	}

198 199
	if (edgeErrors != NULL)
		selector->getEdgeErrors(edgeErrors) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
200

201
	unsigned int nbVertices = map.template getNbOrbits<VERTEX>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
202 203
	bool finished = false ;
	Dart d ;
204

Pierre Kraemer's avatar
Pierre Kraemer committed
205 206
	while(!finished)
	{
207 208
		if(!selector->nextEdge(d))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
209
			break ;
210
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231

		--nbVertices ;

		Dart d2 = map.phi2(map.phi_1(d)) ;
		Dart dd2 = map.phi2(map.phi_1(map.phi2(d))) ;

		for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
		{
			(*it)->approximate(d) ;				// compute approximated attributes
			(*it)->saveApprox(d) ;
		}

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

		map.collapseEdge(d) ;					// collapse edge

		for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
			(*it)->affectApprox(d2);			// affect data to the resulting vertex

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

232 233
		if(nbVertices <= nbWantedVertices)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
234
			finished = true ;
235
		}
236 237 238 239

		// Progress bar support
		if (callback_wrapper != NULL && callback_object != NULL)
			callback_wrapper(callback_object, &nbVertices) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
240 241 242
	}

	delete selector ;
243

Pierre Kraemer's avatar
Pierre Kraemer committed
244 245
	for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
		delete (*it) ;
246 247

	delete[] v_approx ;
Pierre Kraemer's avatar
Pierre Kraemer committed
248 249 250 251 252 253 254
}

} //namespace Decimation

} //namespace Algo

} //namespace CGoGN