approximator.h 12 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 37 38 39 40 41 42 43 44 45
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Decimation/predictor.h"

#ifndef __APPROXIMATOR_H__
#define __APPROXIMATOR_H__

namespace CGoGN
{

namespace Algo
{

namespace Decimation
{

enum ApproximatorType
{
	A_QEM,
	A_MidEdge,
	A_CornerCutting,
	A_TangentPredict1,
	A_TangentPredict2,
46 47
	A_ColorNaive,
	A_ColorQEMext,
48
	A_Lightfield,
49 50
	// note: the following "h" prefix means that half-edges are prioritized instead of edges.
	A_hHalfCollapse,
51 52
	A_hQEM,
	A_hLightfieldHalf
Pierre Kraemer's avatar
Pierre Kraemer committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
} ;

template <typename PFP>
class ApproximatorGen
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

protected:
	MAP& m_map ;

public:
	ApproximatorGen(MAP& m) : m_map(m)
	{}
	virtual ~ApproximatorGen()
	{}
71
	virtual const std::string& getApproximatedAttributeName(unsigned int index = 0) const = 0 ;
72
//	virtual std::vector<std::string> getApproximatedAttributeNames() const = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
73
	virtual ApproximatorType getType() const = 0 ;
74
	virtual unsigned int getNbApproximated() const = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76 77 78 79 80 81 82 83
	virtual bool init() = 0 ;
	virtual void approximate(Dart d) = 0 ;
	virtual void saveApprox(Dart d) = 0 ;
	virtual void affectApprox(Dart d) = 0 ;
	virtual const PredictorGen<PFP>* getPredictor() const = 0 ;
//	virtual REAL detailMagnitude(Dart d) = 0 ;
	virtual void addDetail(Dart d, double amount, bool sign, typename PFP::MATRIX33* detailTransform) = 0 ;
} ;

84 85

template <typename PFP, typename T, unsigned int ORBIT>
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87 88 89 90 91 92 93 94 95
class Approximator : public ApproximatorGen<PFP>
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

protected:
	Predictor<PFP, T>* m_predictor ;

96
	std::vector<VertexAttribute<T>* > m_attrV ;	// vertex attributes to be approximated
97 98
	std::vector<AttributeHandler<T,ORBIT> > m_approx ;	// attributes to store approximation result
	std::vector<AttributeHandler<T,ORBIT> > m_detail ;	// attributes to store detail information for reconstruction
99
	std::vector<T> m_app ;
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101

public:
102 103
	Approximator(MAP& m, std::vector<VertexAttribute<T>* > va, Predictor<PFP, T> * predictor) :
		ApproximatorGen<PFP>(m), m_predictor(predictor), m_attrV(va)
Pierre Kraemer's avatar
Pierre Kraemer committed
104
	{
105 106
		const unsigned int& size = m_attrV.size() ;
		assert(size > 0 || !"Approximator: no attributes provided") ;
107

108 109 110
		m_approx.resize(size) ;
		m_detail.resize(size) ;
		m_app.resize(size) ;
111

112
		for (unsigned int i = 0 ; i < size ; ++i)
113
		{
114 115 116
			if (!m_attrV[i]->isValid())
				std::cerr << "Approximator Warning: attribute number " << i << " is not valid" << std::endl ;

117 118
			std::stringstream aname ;
			aname << "approx_" << m_attrV[i]->name() ;
119
			m_approx[i] = this->m_map.template addAttribute<T, ORBIT>(aname.str()) ;
120 121 122 123 124

			if(m_predictor)	// if predictors are associated to the approximator
			{				// create attributes to store the details needed for reconstruction
				std::stringstream dname ;
				dname << "detail_" << m_attrV[i]->name() ;
125
				m_detail[i] = this->m_map.template addAttribute<T, ORBIT>(dname.str()) ;
126
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128 129 130 131
		}
	}

	virtual ~Approximator()
	{
132 133 134 135 136 137 138 139 140 141 142
		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
		{
			this->m_map.template removeAttribute(m_approx[i]) ;
			if(m_predictor)
				this->m_map.template removeAttribute(m_detail[i]) ;
		}
	}

	const std::string& getApproximatedAttributeName(unsigned int index = 0) const
	{
		return m_attrV[index]->name() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144
	}

145 146 147 148 149 150 151 152 153 154 155
//	std::vector<std::string> getApproximatedAttributeNames() const
//	{
//		std::vector<std::string> names ;
//		names.resize(m_attrV.size()) ;
//		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
//			names[i] = m_attrV[i]->name() ;
//
//		return names ;
//	}

	unsigned int getNbApproximated() const
Pierre Kraemer's avatar
Pierre Kraemer committed
156
	{
157
		return m_attrV.size() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
158 159 160 161
	}

	void saveApprox(Dart d)
	{
162 163
		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
			m_app[i] = m_approx[i][d] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165 166 167
	}

	void affectApprox(Dart d)
	{
168 169
		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
			m_attrV[i]->operator[](d) = m_app[i] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
170 171
	}

172
	const T& getApprox(Dart d, unsigned int index = 0) const
Pierre Kraemer's avatar
Pierre Kraemer committed
173
	{
174 175 176 177 178 179 180 181 182 183 184
		return m_approx[index][d] ;
	}

	std::vector<T> getAllApprox(Dart d) const
	{
		std::vector<T> res ;
		res.resize(m_attrV.size()) ;
		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
			res[i] = m_approx[i][d] ;

		return res ;
Pierre Kraemer's avatar
Pierre Kraemer committed
185 186 187 188 189 190 191
	}

	const Predictor<PFP, T>* getPredictor() const
	{
		return m_predictor ;
	}

192 193 194 195 196 197 198
	const T& getDetail(Dart d, unsigned int index = 0) const
	{
		assert(m_predictor || !"Trying to get detail on a non-predictive scheme") ;
		return m_detail[index][d] ;
	}

	std::vector<T> getAllDetail(Dart d) const
Pierre Kraemer's avatar
Pierre Kraemer committed
199 200
	{
		assert(m_predictor || !"Trying to get detail on a non-predictive scheme") ;
201 202 203 204 205 206

		std::vector<T> res ;
		res.resize(m_attrV.size()) ;
		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
			res[i] = m_detail[i][d] ;
		return res ;
Pierre Kraemer's avatar
Pierre Kraemer committed
207 208
	}

209
	void setDetail(Dart d, unsigned int index, T& val)
Pierre Kraemer's avatar
Pierre Kraemer committed
210 211
	{
		assert(m_predictor || !"Trying to set detail on a non-predictive scheme") ;
212
		m_detail[index][d] = val ;
Pierre Kraemer's avatar
Pierre Kraemer committed
213 214
	}

215 216 217 218 219 220 221 222 223
	void setDetail(Dart d, std::vector<T>& val)
	{
		assert(m_predictor || !"Trying to set detail on a non-predictive scheme") ;

		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
			m_detail[index][d] = val[i] ;
	}


Pierre Kraemer's avatar
Pierre Kraemer committed
224 225 226 227 228 229 230 231 232 233
//	// TODO works only for vector types !!
//	REAL detailMagnitude(Dart d)
//	{
//		assert(m_predictor || !"Trying to get detail magnitude on a non-predictive scheme") ;
//		return m_detail[d].norm2() ;
//	}

	void addDetail(Dart d, double amount, bool sign, typename PFP::MATRIX33* detailTransform)
	{
		assert(m_predictor || !"Trying to add detail on a non-predictive scheme") ;
234 235 236 237 238 239 240 241 242 243 244

		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
		{
			T det = m_detail[i][d] ;
			if(detailTransform)
				det = (*detailTransform) * det ;
			det *= amount ;
			if(!sign)
				det *= REAL(-1) ;
			m_attrV[i]->operator[](d) += det ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
245 246 247
	}
} ;

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
//template <typename PFP, typename T>
//class ApproximatorEdge : public ApproximatorGen<PFP>
//{
//public:
//	typedef typename PFP::MAP MAP ;
//	typedef typename PFP::VEC3 VEC3 ;
//	typedef typename PFP::REAL REAL ;
//
//protected:
//	Predictor<PFP, T>* m_predictor ;
//
//	std::vector<VertexAttribute<T>* > m_attrV ;	// vertex attributes to be approximated
//	std::vector<EdgeAttribute<T> > m_approx ;	// attributes to store approximation result
//	std::vector<EdgeAttribute<T> > m_detail ;	// attributes to store detail information for reconstruction
//	std::vector<T> m_app ;
//
//public:
//	ApproximatorEdge(MAP& m, std::vector<VertexAttribute<T>* > va, Predictor<PFP, T> * predictor) :
//		ApproximatorGen<PFP>(m), m_predictor(predictor), m_attrV(va)
//	{
//		const unsigned int& size = m_attrV.size() ;
//		assert(size > 0 || !"ApproximatorEdge: no attributes provided") ;
//
//		m_approx.resize(size) ;
//		m_detail.resize(size) ;
//		m_app.resize(size) ;
//
//		for (unsigned int i = 0 ; i < size ; ++i)
//		{
//			if (!m_attrV[i]->isValid())
//				std::cerr << "ApproximatorEdge Warning: attribute number " << i << " is not valid" << std::endl ;
//
//			std::stringstream aname ;
//			aname << "approx_" << m_attrV[i]->name() ;
//			m_approx[i] = this->m_map.template addAttribute<T, EDGE>(aname.str()) ;
//
//			if(m_predictor)	// if predictors are associated to the approximator
//			{				// create attributes to store the details needed for reconstruction
//				std::stringstream dname ;
//				dname << "detail_" << m_attrV[i]->name() ;
//				m_detail[i] = this->m_map.template addAttribute<T, EDGE>(dname.str()) ;
//			}
//		}
//	}
//
//	virtual ~ApproximatorEdge()
//	{
//		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
//		{
//			this->m_map.template removeAttribute(m_approx[i]) ;
//			if(m_predictor)
//				this->m_map.template removeAttribute(m_detail[i]) ;
//		}
//	}
//
//	const std::string& getApproximatedAttributeName(unsigned int index = 0) const
//	{
//		return m_attrV[index]->name() ;
//	}
//
////	std::vector<std::string> getApproximatedAttributeNames() const
////	{
////		std::vector<std::string> names ;
////		names.resize(m_attrV.size()) ;
////		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
////			names[i] = m_attrV[i]->name() ;
////
////		return names ;
////	}
//
//	unsigned int getNbApproximated() const
//	{
//		return m_attrV.size() ;
//	}
//
//	void saveApprox(Dart d)
//	{
//		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
//			m_app[i] = m_approx[i][d] ;
//	}
//
//	void affectApprox(Dart d)
//	{
//		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
//			m_attrV[i]->operator[](d) = m_app[i] ;
//	}
//
//	const T& getApprox(Dart d, unsigned int index = 0) const
//	{
//		return m_approx[index][d] ;
//	}
//
//	std::vector<T> getAllApprox(Dart d) const
//	{
//		std::vector<T> res ;
//		res.resize(m_attrV.size()) ;
//		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
//			res[i] = m_approx[i][d] ;
//
//		return res ;
//	}
//
//	const Predictor<PFP, T>* getPredictor() const
//	{
//		return m_predictor ;
//	}
//
//	const T& getDetail(Dart d, unsigned int index = 0) const
//	{
//		assert(m_predictor || !"Trying to get detail on a non-predictive scheme") ;
//		return m_detail[index][d] ;
//	}
//
//	std::vector<T> getAllDetail(Dart d) const
//	{
//		assert(m_predictor || !"Trying to get detail on a non-predictive scheme") ;
//
//		std::vector<T> res ;
//		res.resize(m_attrV.size()) ;
//		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
//			res[i] = m_detail[i][d] ;
//		return res ;
//	}
//
//	void setDetail(Dart d, unsigned int index, T& val)
//	{
//		assert(m_predictor || !"Trying to set detail on a non-predictive scheme") ;
//		m_detail[index][d] = val ;
//	}
//
//	void setDetail(Dart d, std::vector<T>& val)
//	{
//		assert(m_predictor || !"Trying to set detail on a non-predictive scheme") ;
//
//		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
//			m_detail[index][d] = val[i] ;
//	}
//
//
////	// TODO works only for vector types !!
////	REAL detailMagnitude(Dart d)
////	{
////		assert(m_predictor || !"Trying to get detail magnitude on a non-predictive scheme") ;
////		return m_detail[d].norm2() ;
////	}
//
//	void addDetail(Dart d, double amount, bool sign, typename PFP::MATRIX33* detailTransform)
//	{
//		assert(m_predictor || !"Trying to add detail on a non-predictive scheme") ;
//
//		for (unsigned int i = 0 ; i < m_attrV.size() ; ++i)
//		{
//			T det = m_detail[i][d] ;
//			if(detailTransform)
//				det = (*detailTransform) * det ;
//			det *= amount ;
//			if(!sign)
//				det *= REAL(-1) ;
//			m_attrV[i]->operator[](d) += det ;
//		}
//	}
//} ;

Pierre Kraemer's avatar
Pierre Kraemer committed
411 412 413 414 415 416 417
} //namespace Decimation

} //namespace Algo

} //namespace CGoGN

#endif