decimation.hpp 8.95 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

namespace Decimation
{

template <typename PFP>
void decimate(
	typename PFP::MAP& map, SelectorType s, ApproximatorType a,
	typename PFP::TVEC3& position, unsigned int nbWantedVertices, const FunctorSelect& selected
)
{
	std::vector<ApproximatorGen<PFP>*> approximators ;
	EdgeSelector<PFP>* selector = NULL ;

	switch(a)
	{
		case A_QEM :
			approximators.push_back(new Approximator_QEM<PFP>(map, position)) ;
			break ;
		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 :
		{
			approximators.push_back(new Approximator_QEM<PFP>(map, position)) ;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
			/*
						PFP::TVEC3 frame[3] ;
						frame[0] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "frame_T") ; // Tangent
						frame[1] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "frame_B") ; // Bitangent
						frame[2] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "frame_N") ; // Normal
						for (unsigned int i = 0 ; i < 3 ; ++i)
							if (!frame[i].isValid()) {
								std::cerr << "In function decimate : frame[" << i << "] is not valid" << std::endl ;
							}

						AttributeHandler<typename PFP::VEC3> colorPTM[6] ;
						colorPTM[0] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_a") ;
						colorPTM[1] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_b") ;
						colorPTM[2] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_c") ;
						colorPTM[3] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_d") ;
						colorPTM[4] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_e") ;
						colorPTM[5] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_f") ;
						for (unsigned int i = 0 ; i < 6 ; ++i)
							if (!colorPTM[i].isValid()) {
								std::cerr << "In function decimate : colorPTM[" << i << "] is not valid" << std::endl ;
							}
			*/
Pierre Kraemer's avatar
Pierre Kraemer committed
88
			AttributeHandler<Geom::Matrix<3,3,typename PFP::REAL> > frame = map.template getAttribute<Geom::Matrix<3,3,typename PFP::REAL> >(VERTEX_ORBIT, "frame") ;
89
			AttributeHandler<Geom::Matrix<3,6,typename PFP::REAL> > RGBfunctions = map.template getAttribute<Geom::Matrix<3,6,typename PFP::REAL> >(VERTEX_ORBIT, "colorPTM") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
90 91 92 93
			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
94 95
		case A_LightfieldHalf :
		{
96
			approximators.push_back(new Approximator_HalfCollapse<PFP>(map, position)) ;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
			/*
						PFP::TVEC3 frame[3] ;
						frame[0] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "frame_T") ; // Tangent
						frame[1] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "frame_B") ; // Bitangent
						frame[2] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "frame_N") ; // Normal
						for (unsigned int i = 0 ; i < 3 ; ++i)
							if (!frame[i].isValid()) {
								std::cerr << "In function decimate : frame[" << i << "] is not valid" << std::endl ;
							}

						AttributeHandler<typename PFP::VEC3> colorPTM[6] ;
						colorPTM[0] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_a") ;
						colorPTM[1] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_b") ;
						colorPTM[2] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_c") ;
						colorPTM[3] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_d") ;
						colorPTM[4] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_e") ;
						colorPTM[5] = map.template getAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "colorPTM_f") ;
						for (unsigned int i = 0 ; i < 6 ; ++i)
							if (!colorPTM[i].isValid()) {
								std::cerr << "In function decimate : colorPTM[" << i << "] is not valid" << std::endl ;
							}
			*/
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
119 120 121
			AttributeHandler<Geom::Matrix<3,3,typename PFP::REAL> > frame = map.template getAttribute<Geom::Matrix<3,3,typename PFP::REAL> >(VERTEX_ORBIT, "frame") ;
			AttributeHandler<Geom::Matrix<3,6,typename PFP::REAL> > RGBfunctions = map.template getAttribute<Geom::Matrix<3,6,typename PFP::REAL> >(VERTEX_ORBIT, "colorPTM") ;
			approximators.push_back(new Approximator_FrameHalf<PFP>(map, frame)) ;
122
			approximators.push_back(new Approximator_RGBfunctionsHalf<PFP>(map, RGBfunctions)) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
123 124
			break ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
125 126 127 128 129 130 131 132 133 134 135 136 137
	}

	switch(s)
	{
		case S_MapOrder :
			selector = new EdgeSelector_MapOrder<PFP>(map, position, approximators) ;
			break ;
		case S_Random :
			selector = new EdgeSelector_Random<PFP>(map, position, approximators) ;
			break ;
		case S_EdgeLength :
			selector = new EdgeSelector_Length<PFP>(map, position, approximators) ;
			break ;
138 139 140
		case S_QEMml :
			selector = new EdgeSelector_QEMml<PFP>(map, position, approximators) ;
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
141 142 143 144 145 146
		case S_QEM :
			selector = new EdgeSelector_QEM<PFP>(map, position, approximators) ;
			break ;
		case S_Lightfield :
			selector = new EdgeSelector_Lightfield<PFP>(map, position, approximators) ;
			break ;
147 148 149
		case S_LightfieldHalf :
			selector = new HalfEdgeSelector_Lightfield<PFP>(map, position, approximators) ;
			break ;
Pierre Kraemer's avatar
Pierre Kraemer committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		case S_Curvature :
			selector = new EdgeSelector_Curvature<PFP>(map, position, approximators) ;
			break ;
		case S_MinDetail :
			selector = new EdgeSelector_Random<PFP>(map, position, approximators) ;
			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.getNbOrbits(VERTEX_ORBIT) ;
	bool finished = false ;
	Dart d ;
167

Pierre Kraemer's avatar
Pierre Kraemer committed
168 169
	while(!finished)
	{
170 171
		if(!selector->nextEdge(d)) {
			std::cout << "out" << std::endl ;
Pierre Kraemer's avatar
Pierre Kraemer committed
172
			break ;
173
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

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

195
		if(nbVertices <= nbWantedVertices) {
Pierre Kraemer's avatar
Pierre Kraemer committed
196
			finished = true ;
197 198
			std::cout << "done" << std::endl ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
199 200 201 202 203 204 205 206 207 208 209 210
	}

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

} //namespace Decimation

} //namespace Algo

} //namespace CGoGN