decimation.hpp 9.87 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

31 32 33
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
34 35 36 37 38 39
namespace Decimation
{

template <typename PFP>
void decimate(
	typename PFP::MAP& map, SelectorType s, ApproximatorType a,
40
	std::vector<VertexAttribute<typename PFP::VEC3>* >& attribs, unsigned int nbWantedVertices,
41
	EdgeAttribute<typename PFP::REAL> *edgeErrors,
42
	void (*callback_wrapper)(void*, const void*), void* callback_object
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44
)
{
45 46 47 48
	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
49 50 51
	std::vector<ApproximatorGen<PFP>*> approximators ;
	EdgeSelector<PFP>* selector = NULL ;

52 53
	std::vector<VertexAttribute<typename PFP::VEC3>* > *v_approx = NULL ;

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

81
			// pos
82
			v_approx[0].push_back(attribs[0]) ;
83
			approximators.push_back(new Approximator_QEM<PFP>(map, v_approx[0])) ;
84 85

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

			// pos
			v_approx[0].push_back(attribs[0]) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
108
			approximators.push_back(new Approximator_HalfCollapse<PFP>(map, v_approx[0])) ;
109 110 111 112 113 114 115 116 117 118 119 120 121 122

			// 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 ;
123
		case A_Lightfield :
124
		{
125
			v_approx = new std::vector<VertexAttribute<typename PFP::VEC3>* >[3] ;
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
			// 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
142
		}
143
		break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146 147 148
	}

	switch(s)
	{
		case S_MapOrder :
149
			selector = new EdgeSelector_MapOrder<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
150 151
			break ;
		case S_Random :
152
			selector = new EdgeSelector_Random<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
153 154
			break ;
		case S_EdgeLength :
155
			selector = new EdgeSelector_Length<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
156
			break ;
157
		case S_QEMml :
158
			selector = new EdgeSelector_QEMml<PFP>(map, position, approximators) ;
159
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
160
		case S_QEM :
161
			selector = new EdgeSelector_QEM<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
162 163
			break ;
		case S_Curvature :
164
			selector = new EdgeSelector_Curvature<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
165
			break ;
166
		case S_NormalArea :
167
			selector = new EdgeSelector_NormalArea<PFP>(map, position, approximators) ;
168
			break ;
169
		case S_CurvatureTensor :
170
			selector = new EdgeSelector_CurvatureTensor<PFP>(map, position, approximators) ;
171
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
172
		case S_MinDetail :
173
			selector = new EdgeSelector_MinDetail<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
174
			break ;
175
		case S_ColorNaive :
176
			selector = new EdgeSelector_ColorNaive<PFP>(map, position, approximators) ;
177
			break ;
178
		case S_QEMextColor :
179
			selector = new EdgeSelector_QEMextColor<PFP>(map, position, approximators) ;
180
			break ;
181
		case S_hQEMextColor :
182
			selector = new HalfEdgeSelector_QEMextColor<PFP>(map, position, approximators) ;
183
			break ;
184
		case S_hQEMml :
185
			selector = new HalfEdgeSelector_QEMml<PFP>(map, position, approximators) ;
186
			break ;
187
		case S_Lightfield :
188
			selector = new EdgeSelector_Lightfield<PFP>(map, position, approximators) ;
189
			break ;
190
		case S_hLightfield :
191
			selector = new HalfEdgeSelector_Lightfield<PFP>(map, position, approximators) ;
192
			break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
193
		case S_hLightfieldExp :
194
			selector = new HalfEdgeSelector_LightfieldExp<PFP>(map, position, approximators) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
195
			break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
196
		case S_hLightfieldKCL :
197
			selector = new HalfEdgeSelector_LightfieldKCL<PFP>(map, position, approximators) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
198
			break ;
199 200 201 202 203 204
		case S_hColorExperimental:
			selector = new HalfEdgeSelector_ColorExperimental<PFP>(map, position, approximators, selected) ;
			break ;
		case S_hLFexperimental:
			selector = new HalfEdgeSelector_LFexperimental<PFP>(map, position, approximators, selected) ;
			break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
205 206 207 208 209 210
		case S_hColorPerFace:
			selector = new HalfEdgeSelector_ColorPerFace<PFP>(map, position, approximators, selected) ;
			break ;
		case S_hLFperFace:
			selector = new HalfEdgeSelector_LFperFace<PFP>(map, position, approximators, selected) ;
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
211 212 213 214 215 216
	}

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

	if(!selector->init())
217 218 219 220 221 222
	{
		delete selector ;

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

223 224
		delete[] v_approx ;

Pierre Kraemer's avatar
Pierre Kraemer committed
225
		return ;
226 227
	}

228
	unsigned int nbVertices = map.template getNbOrbits<VERTEX>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
229 230
	bool finished = false ;
	Dart d ;
231

Pierre Kraemer's avatar
Pierre Kraemer committed
232 233
	while(!finished)
	{
234 235
		if(!selector->nextEdge(d))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
236
			break ;
237
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

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

259 260
		if(nbVertices <= nbWantedVertices)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
261
			finished = true ;
262
		}
263 264 265 266

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

269 270 271
	if (edgeErrors != NULL)
		selector->getEdgeErrors(edgeErrors) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
272
	delete selector ;
273

Pierre Kraemer's avatar
Pierre Kraemer committed
274 275
	for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
		delete (*it) ;
276 277

	delete[] v_approx ;
Pierre Kraemer's avatar
Pierre Kraemer committed
278 279 280 281
}

} //namespace Decimation

282 283
}

Pierre Kraemer's avatar
Pierre Kraemer committed
284 285 286
} //namespace Algo

} //namespace CGoGN