decimator.hpp 5.32 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
36
/*******************************************************************************
* 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 DecimationVolumes
{

template <typename PFP>
void decimate(
	typename PFP::MAP& map, SelectorType s, ApproximatorType a,
untereiner's avatar
untereiner committed
37
	VertexAttribute<typename PFP::VEC3>& position, unsigned int percentWantedVertices, const FunctorSelect& selected
38
39
40
)
{
	std::vector<ApproximatorGen<PFP>*> approximators ;
41
	EdgeSelector<PFP>* selector = NULL ;
42
43
44
45

	//choose the Approximator
	switch(a)
	{
46
47
		case A_QEM :
			approximators.push_back(new Approximator_QEM<PFP>(map, position)) ;
48
49
50
51
52
53
54
55
56
			break ;
		default :
			CGoGNout << "not yet implemented" << CGoGNendl;
			break;
	}

	//choose the Selector
	switch(s)
	{
57
58
		case S_QEM :
			selector = new EdgeSelector_QEM<PFP>(map, position, approximators, selected) ;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
			break ;
		default:
			CGoGNout << "not yet implemented" << CGoGNendl;
			break;
	}

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

	if(!selector->init())
		return ;


	unsigned int nbVertices = map.template getNbOrbits<VERTEX>() ;
untereiner's avatar
untereiner committed
73
74
	unsigned int nbWantedVertices = nbVertices * percentWantedVertices / 100 ;
	CGoGNout << " decimate (" << nbVertices << " vertices).." << /* flush */ CGoGNendl ;
75
76
	bool finished = false ;

77
78
	Dart d;

79
80
81
82
83
	while(!finished)
	{
		//Next Operator to perform
		Operator<PFP> *op;

84
85
		//if(!selector->nextOperator(&op))
		if((op = selector->nextOperator()) == NULL)
86
87
			break;

untereiner's avatar
untereiner committed
88
		// compute approximated attributes
89
90
		for(typename std::vector<ApproximatorGen<PFP>*>::iterator it = approximators.begin(); it != approximators.end(); ++it)
		{
untereiner's avatar
untereiner committed
91
			(*it)->approximate(op) ;
92
			(*it)->saveApprox(op) ;
93
94
95
		}

		//Update the selector before performing operation
96
97
		if(!selector->updateBeforeOperation(op))
			break;
98
99
100
101
102

		//Perform the topological operation and
		//compute the number of resulting cells
		nbVertices -= op->perform(map, position);

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

		//Update the embedded position and
		//search the next operation to perform
108
		selector->updateAfterOperation(op);
109
110
111
112
113
114
115
116
117

		if(nbVertices <= nbWantedVertices)
			finished = true ;

		delete op;
	}

	selector->finish() ;

untereiner's avatar
untereiner committed
118
119
	CGoGNout << "..done (" << nbVertices << " vertices)" << CGoGNendl ;

120
121
122
123
124
125
126
127
128
129
130
	delete selector ;

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

} //namespace DecimationVolumes

} //namespace Algo

} //namespace CGoGN
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

//		if(!selector->nextEdge(d))
//			break ;
//
//
//		Dart d2 = map.phi2(map.phi_1(d)) ;
//		Dart dd2 = map.phi2(map.phi_1(map.phi2(d))) ;
//
//		std::cout << "bin a contracter : " << d << std::endl;
//		std::cout << "voisin d2 : " << d2 << std::endl;
//		std::cout << "voisin dd2 : " << dd2 << std::endl;
//
//		--nbVertices ;
//
//		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
//
//		if(nbVertices <= nbWantedVertices)
//			finished = true ;