decimation.hpp 7.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 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
	VertexAttribute<typename PFP::VEC3>& position, unsigned int nbWantedVertices, const FunctorSelect& selected
Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40 41 42 43 44 45 46 47
)
{
	std::vector<ApproximatorGen<PFP>*> approximators ;
	EdgeSelector<PFP>* selector = NULL ;

	switch(a)
	{
		case A_QEM :
			approximators.push_back(new Approximator_QEM<PFP>(map, position)) ;
			break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
48 49
		case A_QEMhalfEdge :
			approximators.push_back(new Approximator_QEMhalfEdge<PFP>(map, position)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
50
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		case A_MidEdge :
			approximators.push_back(new Approximator_MidEdge<PFP>(map, position)) ;
			break ;
		case A_CornerCutting :
			approximators.push_back(new Approximator_CornerCutting<PFP>(map, position)) ;
			break ;
		case A_TangentPredict1 :
			approximators.push_back(new Approximator_MidEdge<PFP>(map, position)) ;
			break ;
		case A_TangentPredict2 :
			approximators.push_back(new Approximator_MidEdge<PFP>(map, position)) ;
			break ;
		case A_HalfCollapse :
			approximators.push_back(new Approximator_HalfCollapse<PFP>(map, position)) ;
			break ;
		case A_LightfieldFull :
		{
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
68
			approximators.push_back(new Approximator_QEMhalfEdge<PFP>(map, position)) ;
69
			/*
70
			PFP::TVEC3 frame[3] ;
71 72 73
			frame[0] = map.template getAttribute<typename PFP::VEC3, VERTEX>("frame_T") ; // Tangent
			frame[1] = map.template getAttribute<typename PFP::VEC3, VERTEX>("frame_B") ; // Bitangent
			frame[2] = map.template getAttribute<typename PFP::VEC3, VERTEX>("frame_N") ; // Normal
74 75 76 77
			for (unsigned int i = 0 ; i < 3 ; ++i)
				if (!frame[i].isValid()) {
					CGoGNerr << "In function decimate : frame[" << i << "] is not valid" << CGoGNendl ;
				}
78

79 80 81 82 83 84 85
			VertexAttribute<typename PFP::VEC3> colorPTM[6] ;
			colorPTM[0] = map.template getAttribute<typename PFP::VEC3, VERTEX>("colorPTM_a") ;
			colorPTM[1] = map.template getAttribute<typename PFP::VEC3, VERTEX>("colorPTM_b") ;
			colorPTM[2] = map.template getAttribute<typename PFP::VEC3, VERTEX>("colorPTM_c") ;
			colorPTM[3] = map.template getAttribute<typename PFP::VEC3, VERTEX>("colorPTM_d") ;
			colorPTM[4] = map.template getAttribute<typename PFP::VEC3, VERTEX>("colorPTM_e") ;
			colorPTM[5] = map.template getAttribute<typename PFP::VEC3, VERTEX>("colorPTM_f") ;
86 87 88 89
			for (unsigned int i = 0 ; i < 6 ; ++i)
				if (!colorPTM[i].isValid()) {
					CGoGNerr << "In function decimate : colorPTM[" << i << "] is not valid" << CGoGNendl ;
				}
90
			*/
91 92
			VertexAttribute<Geom::Matrix<3,3,typename PFP::REAL> > frame = map.template getAttribute<Geom::Matrix<3,3,typename PFP::REAL>, VERTEX>("frame") ;
			VertexAttribute<Geom::Matrix<3,6,typename PFP::REAL> > RGBfunctions = map.template getAttribute<Geom::Matrix<3,6,typename PFP::REAL>, VERTEX>("colorPTM") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
93 94 95 96
			approximators.push_back(new Approximator_Frame<PFP>(map, frame)) ;
			approximators.push_back(new Approximator_RGBfunctions<PFP>(map, RGBfunctions)) ;
			break ;
		}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
97 98
		case A_LightfieldHalf :
		{
99
			approximators.push_back(new Approximator_HalfCollapse<PFP>(map, position)) ;
100 101
			VertexAttribute<Geom::Matrix<3,3,typename PFP::REAL> > frame = map.template getAttribute<Geom::Matrix<3,3,typename PFP::REAL>, VERTEX>("frame") ;
			VertexAttribute<Geom::Matrix<3,6,typename PFP::REAL> > RGBfunctions = map.template getAttribute<Geom::Matrix<3,6,typename PFP::REAL>, VERTEX>("colorPTM") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
102
			approximators.push_back(new Approximator_FrameHalf<PFP>(map, frame)) ;
103
			approximators.push_back(new Approximator_RGBfunctionsHalf<PFP>(map, RGBfunctions)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
104 105
			break ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107 108 109 110
	}

	switch(s)
	{
		case S_MapOrder :
111
			selector = new EdgeSelector_MapOrder<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113
			break ;
		case S_Random :
114
			selector = new EdgeSelector_Random<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116
			break ;
		case S_EdgeLength :
117
			selector = new EdgeSelector_Length<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
118
			break ;
119
		case S_QEMml :
120
			selector = new EdgeSelector_QEMml<PFP>(map, position, approximators, selected) ;
121
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
122
		case S_QEM :
123
			selector = new EdgeSelector_QEM<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125
			break ;
		case S_Curvature :
126
			selector = new EdgeSelector_Curvature<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128
			break ;
		case S_MinDetail :
129
			selector = new EdgeSelector_MinDetail<PFP>(map, position, approximators, selected) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
130
			break ;
131
		case S_hLightfield :
132
			selector = new HalfEdgeSelector_Lightfield<PFP>(map, position, approximators, selected) ;
133 134
			break ;
		case S_hQEMml :
135
			selector = new HalfEdgeSelector_QEMml<PFP>(map, position, approximators, selected) ;
136
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
137 138 139 140 141 142 143 144
	}

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

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

145
	unsigned int nbVertices = map.template getNbOrbits<VERTEX>() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147
	bool finished = false ;
	Dart d ;
148

Pierre Kraemer's avatar
Pierre Kraemer committed
149 150
	while(!finished)
	{
151 152
//		CGoGNout << "Countdown : " ;
//		CGoGNout << std::setprecision(8) << (nbVertices - nbWantedVertices) << "\r" << CGoGNflush ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
153

154
		if(!selector->nextEdge(d)) {
155
//			CGoGNout << CGoGNendl << "out" << CGoGNendl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
156
			break ;
157
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

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

179
		if(nbVertices <= nbWantedVertices) {
Pierre Kraemer's avatar
Pierre Kraemer committed
180
			finished = true ;
181
//			CGoGNout << CGoGNendl << "done" << CGoGNendl ;
182
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
183 184 185 186 187 188 189 190 191 192 193 194
	}

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

} //namespace Decimation

} //namespace Algo

} //namespace CGoGN