Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

decimation.hpp 10.3 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
		}
		break;
98
99
100
101
102
103
104
		case A_GeomColorOpt :
		{
			// pos + col
			assert(attribs.size() >= 2 || !"Decimate: A_GeomColorOpt --> not enough attribs provided") ;
			approximators.push_back(new Approximator_GeomColOpt<PFP>(map, attribs)) ;
		}
		break ;
105
		case A_hQEM :
106
			// pos
107
			approximators.push_back(new Approximator_QEMhalfEdge<PFP>(map, attribs)) ;
108
		break ;
109
110
111
112
113
114
		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
115
			approximators.push_back(new Approximator_HalfCollapse<PFP>(map, v_approx[0])) ;
116
117
118
119
120
121
122
123
124
125
126
127
128
129

			// 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 ;
130
		case A_Lightfield :
131
		{
132
			v_approx = new std::vector<VertexAttribute<typename PFP::VEC3>* >[3] ;
133

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
			// 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
149
		}
150
		break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
151
152
153
154
155
	}

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

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

	if(!selector->init())
230
231
232
233
234
235
	{
		delete selector ;

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

236
237
		delete[] v_approx ;

Pierre Kraemer's avatar
Pierre Kraemer committed
238
		return ;
239
240
	}

241
	unsigned int nbVertices = map.template getNbOrbits<VERTEX>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
242
243
	bool finished = false ;
	Dart d ;
244

Pierre Kraemer's avatar
Pierre Kraemer committed
245
246
	while(!finished)
	{
247
248
		if(!selector->nextEdge(d))
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
249
			break ;
250
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

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

272
273
		if(nbVertices <= nbWantedVertices)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
274
			finished = true ;
275
		}
276
277
278
279

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

282
283
284
	if (edgeErrors != NULL)
		selector->getEdgeErrors(edgeErrors) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
285
	delete selector ;
286

Pierre Kraemer's avatar
Pierre Kraemer committed
287
288
	for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
		delete (*it) ;
289
290

	delete[] v_approx ;
Pierre Kraemer's avatar
Pierre Kraemer committed
291
292
293
294
}

} //namespace Decimation

295
296
}

Pierre Kraemer's avatar
Pierre Kraemer committed
297
298
299
} //namespace Algo

} //namespace CGoGN