decimation.hpp 8.38 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-2013, IGG Team, ICube, 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
namespace Decimation
{

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

Pierre Kraemer's avatar
Pierre Kraemer committed
50
	std::vector<ApproximatorGen<PFP>*> approximators ;
51
	Selector<PFP>* selector = NULL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
52

Sylvain Thery's avatar
Sylvain Thery committed
53
	std::vector<VertexAttribute<typename PFP::VEC3, typename PFP::MAP>*>* v_approx = NULL ;
54

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

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

			// col
87 88
			assert(attribs.size() >= 2 || !"Decimate: A_ColorNaive --> not enough attribs provided") ;
			v_approx[1].push_back(attribs[1]) ;
89
			approximators.push_back(new Approximator_ColorNaive<PFP>(map, v_approx[1])) ;
90 91 92 93
		}
		break ;
		case A_ColorQEMext :
		{
94
			// pos + col
95 96
			assert(attribs.size() >= 2 || !"Decimate: A_ColorQEMext --> not enough attribs provided") ;
			approximators.push_back(new Approximator_ColorQEMext<PFP>(map, attribs)) ;
97 98
		}
		break;
99 100 101 102 103 104 105
		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 ;
106
		case A_hQEM :
107
			// pos
108
			approximators.push_back(new Approximator_QEMhalfEdge<PFP>(map, attribs)) ;
Sylvain Thery's avatar
Sylvain Thery committed
109 110 111 112
			break ;

		case A_OTHER:
			break;
Pierre Kraemer's avatar
Pierre Kraemer committed
113 114 115 116 117
	}

	switch(s)
	{
		case S_MapOrder :
118
			selector = new EdgeSelector_MapOrder<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
119 120
			break ;
		case S_Random :
121
			selector = new EdgeSelector_Random<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
122 123
			break ;
		case S_EdgeLength :
124
			selector = new EdgeSelector_Length<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
125
			break ;
126
		case S_QEMml :
127
			selector = new EdgeSelector_QEMml<PFP>(map, position, approximators) ;
128
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
129
		case S_QEM :
130
			selector = new EdgeSelector_QEM<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
131 132
			break ;
		case S_Curvature :
133
			selector = new EdgeSelector_Curvature<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
134
			break ;
135
		case S_NormalArea :
136
			selector = new EdgeSelector_NormalArea<PFP>(map, position, approximators) ;
137
			break ;
138
		case S_CurvatureTensor :
139
			selector = new EdgeSelector_CurvatureTensor<PFP>(map, position, approximators) ;
140
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
141
		case S_MinDetail :
142
			selector = new EdgeSelector_MinDetail<PFP>(map, position, approximators) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
143
			break ;
144
		case S_ColorNaive :
145
			selector = new EdgeSelector_ColorNaive<PFP>(map, position, approximators) ;
146
			break ;
147
		case S_QEMextColor :
148
			selector = new EdgeSelector_QEMextColor<PFP>(map, position, approximators) ;
149
			break ;
150
		case S_hQEMextColor :
151
			selector = new HalfEdgeSelector_QEMextColor<PFP>(map, position, approximators) ;
152
			break ;
153
		case S_hQEMml :
154
			selector = new HalfEdgeSelector_QEMml<PFP>(map, position, approximators) ;
155
			break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
156 157 158 159 160 161
		case S_hColorGradient:
			selector = new HalfEdgeSelector_ColorGradient<PFP>(map, position, approximators) ;
			break ;
		case S_GeomColOptGrad:
			selector = new EdgeSelector_GeomColOptGradient<PFP>(map, position, approximators) ;
			break ;
Sylvain Thery's avatar
Sylvain Thery committed
162 163
		case S_OTHER:
			break;
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165
	}

Sylvain Thery's avatar
Sylvain Thery committed
166
	int status = decimate<PFP>(map, selector, approximators, nbWantedVertices, true, edgeErrors, callback_wrapper, callback_object) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
167 168 169

	delete selector ;

Pierre Kraemer's avatar
Pierre Kraemer committed
170
	for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
171
		delete (*it) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
172

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
173
	delete[] v_approx ;
174

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
175 176
	return status ;
}
177

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
178 179 180
template <typename PFP>
int decimate(
		typename PFP::MAP& map,
181
		Selector<PFP>* selector, std::vector<ApproximatorGen<PFP>*>& approximators,
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
182
		unsigned int nbWantedVertices,
183
		bool recomputePriorityList,
Sylvain Thery's avatar
Sylvain Thery committed
184
		EdgeAttribute<typename PFP::REAL, typename PFP::MAP>* edgeErrors,
Pierre Kraemer's avatar
Pierre Kraemer committed
185
		void (*callback_wrapper)(void*, const void*), void* callback_object
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
186 187 188 189
)
{
	for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
		(*it)->init() ;
190

191 192 193 194 195 196
	Dart d ;
	if (recomputePriorityList || !selector->nextEdge(d))
	{
		if(!selector->init())
			return -1 ; // init failed
	}
197

198
    unsigned int nbVertices = Algo::Topo::getNbOrbits<VERTEX>(map) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
199
	bool finished = false ;
200

Pierre Kraemer's avatar
Pierre Kraemer committed
201 202
	while(!finished)
	{
203
		if(!selector->nextEdge(d))
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
204
			break ; // finished before achieving amount of required vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

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

226
		if(nbVertices <= nbWantedVertices)
Pierre Kraemer's avatar
Pierre Kraemer committed
227
			finished = true ;
228 229 230 231

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

234 235 236
	if (edgeErrors != NULL)
		selector->getEdgeErrors(edgeErrors) ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
237
	return finished == true ? 0 : 1 ; // finished correctly
Pierre Kraemer's avatar
Pierre Kraemer committed
238 239
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
240
} // namespace Decimation
Pierre Kraemer's avatar
Pierre Kraemer committed
241

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
242
} // namespace Surface
243

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
244
} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
245

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
246
} // namespace CGoGN