edgeSelector.h 23.7 KB
Newer Older
Kenneth Vanhoey's avatar
Kenneth Vanhoey 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           *
Kenneth Vanhoey's avatar
Kenneth Vanhoey 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/                                           *
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
21 22 23 24 25 26 27 28
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __EDGESELECTOR_H__
#define __EDGESELECTOR_H__

#include "Algo/Decimation/selector.h"
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
29
#include "Algo/Decimation/approximator.h"
30
#include "Algo/Geometry/boundingbox.h"
31
#include "Utils/qem.h"
32 33
#include "Algo/Geometry/normal.h"
#include "Algo/Selection/collector.h"
34
#include "Algo/Geometry/curvature.h"
35
#include "Algo/Geometry/area.h"
36

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
37 38 39 40 41 42
namespace CGoGN
{

namespace Algo
{

43 44 45
namespace Surface
{

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
46 47 48 49
namespace Decimation
{

template <typename PFP>
50
class EdgeSelector_MapOrder : public Selector<PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
51 52 53 54 55 56 57 58 59 60
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	Dart cur ;

public:
61
	EdgeSelector_MapOrder(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
62
		Selector<PFP>(m, pos, approx)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
63 64 65 66 67
	{}
	~EdgeSelector_MapOrder()
	{}
	SelectorType getType() { return S_MapOrder ; }
	bool init() ;
68
	bool nextEdge(Dart& d) const ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
69 70 71
	void updateBeforeCollapse(Dart d)
	{}
	void updateAfterCollapse(Dart d2, Dart dd2) ;
untereiner's avatar
untereiner committed
72

73
	void updateWithoutCollapse() { }
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
74

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
75 76 77
} ;

template <typename PFP>
78
class EdgeSelector_Random : public Selector<PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
79 80 81 82 83 84 85 86 87 88 89 90
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	std::vector<Dart> darts ;
	unsigned int cur ;
	bool allSkipped ;

public:
91
	EdgeSelector_Random(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
92
		Selector<PFP>(m, pos, approx),
93 94
		cur(0),
		allSkipped(false)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
95 96 97 98 99
	{}
	~EdgeSelector_Random()
	{}
	SelectorType getType() { return S_Random ; }
	bool init() ;
100
	bool nextEdge(Dart& d) const ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
101 102 103
	void updateBeforeCollapse(Dart d2)
	{}
	void updateAfterCollapse(Dart d2, Dart dd2) ;
untereiner's avatar
untereiner committed
104

105
	void updateWithoutCollapse();
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
106 107 108
} ;

template <typename PFP>
109
class EdgeSelector_Length : public Selector<PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
110 111 112 113 114 115 116 117 118 119 120 121 122
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	typedef struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "LengthEdgeInfo" ; }
	} LengthEdgeInfo ;
123
	typedef NoTypeNameAttribute<LengthEdgeInfo> EdgeInfo ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
124

125
	EdgeAttribute<EdgeInfo> edgeInfo ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
126 127 128 129 130 131 132 133 134

	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

	void initEdgeInfo(Dart d) ;
	void updateEdgeInfo(Dart d, bool recompute) ;
	void computeEdgeInfo(Dart d, EdgeInfo& einfo) ;

public:
135
	EdgeSelector_Length(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
136
		Selector<PFP>(m, pos, approx)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
137
	{
138
		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
139 140 141 142 143 144 145
	}
	~EdgeSelector_Length()
	{
		this->m_map.removeAttribute(edgeInfo) ;
	}
	SelectorType getType() { return S_EdgeLength ; }
	bool init() ;
146
	bool nextEdge(Dart& d) const ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
147 148
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;
untereiner's avatar
untereiner committed
149

150
	void updateWithoutCollapse();
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
151

152
	void getEdgeErrors(EdgeAttribute<typename PFP::REAL> *errors) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	{
		assert(errors != NULL || !"EdgeSelector::setColorMap requires non null vertexattribute argument") ;
		if (!errors->isValid())
			std::cerr << "EdgeSelector::setColorMap requires valid edgeattribute argument" << std::endl ;
		assert(edgeInfo.isValid()) ;

		TraversorE<typename PFP::MAP> travE(this->m_map) ;
		for(Dart d = travE.begin() ; d != travE.end() ; d = travE.next())
		{
			(*errors)[d] = -1 ;
			if (edgeInfo[d].valid)
			{
				(*errors)[d] = edgeInfo[d].it->first ;
			}
		}
	}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
169 170 171
} ;

template <typename PFP>
172
class EdgeSelector_QEM : public Selector<PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
173 174 175 176 177 178 179 180 181 182 183 184 185
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "QEMedgeInfo" ; }
	} QEMedgeInfo ;
186
	typedef NoTypeNameAttribute<QEMedgeInfo> EdgeInfo ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
187

188
	EdgeAttribute<EdgeInfo> edgeInfo ;
189 190
	VertexAttribute<Utils::Quadric<REAL> > quadric ;
	Utils::Quadric<REAL> tmpQ ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
191 192 193 194

	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

195
	Approximator<PFP, typename PFP::VEC3, EDGE>* m_positionApproximator ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
196 197 198 199 200 201

	void initEdgeInfo(Dart d) ;
	void updateEdgeInfo(Dart d, bool recompute) ;
	void computeEdgeInfo(Dart d, EdgeInfo& einfo) ;

public:
202
	EdgeSelector_QEM(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
203
		Selector<PFP>(m, pos, approx),
204
		m_positionApproximator(NULL)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
205
	{
206
		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
207
		quadric = m.template addAttribute<Utils::Quadric<REAL>, VERTEX>("QEMquadric") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
208 209 210 211 212 213 214 215
	}
	~EdgeSelector_QEM()
	{
		this->m_map.removeAttribute(quadric) ;
		this->m_map.removeAttribute(edgeInfo) ;
	}
	SelectorType getType() { return S_QEM ; }
	bool init() ;
216
	bool nextEdge(Dart& d) const ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
217 218
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;
untereiner's avatar
untereiner committed
219

220
	void updateWithoutCollapse();
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
221 222 223
} ;

template <typename PFP>
224
class EdgeSelector_QEMml : public Selector<PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
225 226 227 228 229 230 231 232 233 234 235 236 237
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "QEMedgeInfo" ; }
	} QEMedgeInfo ;
238
	typedef NoTypeNameAttribute<QEMedgeInfo> EdgeInfo ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
239

240
	EdgeAttribute<EdgeInfo> edgeInfo ;
241
	VertexAttribute<Utils::Quadric<REAL> > quadric ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
242 243 244 245

	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

246
	Approximator<PFP, typename PFP::VEC3, EDGE>* m_positionApproximator ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
247 248 249 250 251 252 253

	void initEdgeInfo(Dart d) ;
	void updateEdgeInfo(Dart d, bool recompute) ;
	void computeEdgeInfo(Dart d, EdgeInfo& einfo) ;
	void recomputeQuadric(const Dart d, const bool recomputeNeighbors = false) ;

public:
254
	EdgeSelector_QEMml(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
255
		Selector<PFP>(m, pos, approx),
256
		m_positionApproximator(NULL)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
257
	{
258
		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
259
		quadric = m.template addAttribute<Utils::Quadric<REAL>, VERTEX>("QEMquadric") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
260 261 262 263 264 265 266 267
	}
	~EdgeSelector_QEMml()
	{
		this->m_map.removeAttribute(quadric) ;
		this->m_map.removeAttribute(edgeInfo) ;
	}
	SelectorType getType() { return S_QEMml ; }
	bool init() ;
268
	bool nextEdge(Dart& d) const ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
269 270
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;
untereiner's avatar
untereiner committed
271

272
	void updateWithoutCollapse();
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
273 274
} ;

275 276

template <typename PFP>
277
class EdgeSelector_NormalArea : public Selector<PFP>
278 279 280 281 282 283 284 285 286 287 288 289 290
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "NormalAreaEdgeInfo" ; }
	} NormalAreaEdgeInfo ;
291
	typedef NoTypeNameAttribute<NormalAreaEdgeInfo> EdgeInfo ;
292 293 294 295 296 297 298 299 300 301 302 303

	EdgeAttribute<EdgeInfo> edgeInfo ;
	EdgeAttribute<Geom::Matrix<3,3,REAL> > edgeMatrix ;

	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

	Approximator<PFP, typename PFP::VEC3, EDGE>* m_positionApproximator ;

	void initEdgeInfo(Dart d) ;
	void updateEdgeInfo(Dart d) ;
	void computeEdgeInfo(Dart d, EdgeInfo& einfo) ;
304
	void computeEdgeMatrix(Dart d) ;
305 306 307
//	void recomputeQuadric(const Dart d, const bool recomputeNeighbors = false) ;

public:
308
	EdgeSelector_NormalArea(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
309
		Selector<PFP>(m, pos, approx),
310 311 312 313 314 315 316 317 318 319 320 321
		m_positionApproximator(NULL)
	{
		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
		edgeMatrix = m.template addAttribute<Geom::Matrix<3,3,REAL>, EDGE>("NormalAreaMatrix") ;
	}
	~EdgeSelector_NormalArea()
	{
		this->m_map.removeAttribute(edgeMatrix) ;
		this->m_map.removeAttribute(edgeInfo) ;
	}
	SelectorType getType() { return S_NormalArea ; }
	bool init() ;
322
	bool nextEdge(Dart& d) const ;
323 324 325 326 327 328 329
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;

	void updateWithoutCollapse() { }
} ;


Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
330
template <typename PFP>
331
class EdgeSelector_Curvature : public Selector<PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
332 333 334 335 336 337 338 339 340 341 342 343 344
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "CurvatureEdgeInfo" ; }
	} CurvatureEdgeInfo ;
345
	typedef NoTypeNameAttribute<CurvatureEdgeInfo> EdgeInfo ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
346

347 348 349
	Geom::BoundingBox<VEC3> bb ;
	REAL radius ;

350
	VertexAttribute<VEC3> normal ;
351
	EdgeAttribute<EdgeInfo> edgeInfo ;
352 353 354 355 356 357
	EdgeAttribute<REAL> edgeangle ;
	VertexAttribute<REAL> kmax ;
	VertexAttribute<REAL> kmin ;
	VertexAttribute<VEC3> Kmax ;
	VertexAttribute<VEC3> Kmin ;
	VertexAttribute<VEC3> Knormal ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
358 359 360 361

	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

362
	Approximator<PFP, VEC3,EDGE>* m_positionApproximator ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
363 364 365 366 367 368

	void initEdgeInfo(Dart d) ;
	void updateEdgeInfo(Dart d, bool recompute) ;
	void computeEdgeInfo(Dart d, EdgeInfo& einfo) ;

public:
369
	EdgeSelector_Curvature(MAP& m, VertexAttribute<VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
370
		Selector<PFP>(m, pos, approx),
371
		m_positionApproximator(NULL)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
372
	{
373 374 375
		bb = Algo::Geometry::computeBoundingBox<PFP>(m, pos) ;
		radius = bb.diagSize() * 0.003 ;

376
		normal = m.template getAttribute<VEC3, VERTEX>("normal") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
377 378
		if(!normal.isValid())
		{
379
			normal = m.template addAttribute<VEC3, VERTEX>("normal") ;
380
			Algo::Surface::Geometry::computeNormalVertices<PFP>(m, pos, normal) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
381 382
		}

383
		edgeangle = m.template getAttribute<REAL, EDGE>("edgeangle") ;
384 385
		if(!edgeangle.isValid())
		{
386
			edgeangle = m.template addAttribute<REAL, EDGE>("edgeangle") ;
387
			Algo::Surface::Geometry::computeAnglesBetweenNormalsOnEdges<PFP>(m, pos, edgeangle) ;
388 389
		}

390 391 392 393 394
		kmax = m.template getAttribute<REAL, VERTEX>("kmax") ;
		kmin = m.template getAttribute<REAL, VERTEX>("kmin") ;
		Kmax = m.template getAttribute<VEC3, VERTEX>("Kmax") ;
		Kmin = m.template getAttribute<VEC3, VERTEX>("Kmin") ;
		Knormal = m.template getAttribute<VEC3, VERTEX>("Knormal") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
395 396
		// as all these attributes are computed simultaneously by computeCurvatureVertices
		// one can assume that if one of them is not valid, the others must be created too
397
		if(!kmax.isValid())
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
398
		{
399 400 401 402 403
			kmax = m.template addAttribute<REAL, VERTEX>("kmax") ;
			kmin = m.template addAttribute<REAL, VERTEX>("kmin") ;
			Kmax = m.template addAttribute<VEC3, VERTEX>("Kmax") ;
			Kmin = m.template addAttribute<VEC3, VERTEX>("Kmin") ;
			Knormal = m.template addAttribute<VEC3, VERTEX>("Knormal") ;
404
			Algo::Surface::Geometry::computeCurvatureVertices_NormalCycles<PFP>(m, radius, pos, normal, edgeangle, kmax, kmin, Kmax, Kmin, Knormal) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
405 406
		}

407
		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
408 409 410
	}
	~EdgeSelector_Curvature()
	{
411 412 413 414 415 416
		this->m_map.removeAttribute(edgeangle) ;
		this->m_map.removeAttribute(kmax) ;
		this->m_map.removeAttribute(kmin) ;
		this->m_map.removeAttribute(Kmax) ;
		this->m_map.removeAttribute(Kmin) ;
		this->m_map.removeAttribute(Knormal) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
417 418 419 420
		this->m_map.removeAttribute(edgeInfo) ;
	}
	SelectorType getType() { return S_Curvature ; }
	bool init() ;
421
	bool nextEdge(Dart& d) const ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
422 423
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;
untereiner's avatar
untereiner committed
424

425
	void updateWithoutCollapse();
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
426 427
} ;

428 429

template <typename PFP>
430
class EdgeSelector_CurvatureTensor : public Selector<PFP>
431
{
Sauvage's avatar
Sauvage committed
432
	// TODO : this selector still needs to be tested
433 434 435 436 437 438 439 440 441 442 443 444
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "CurvatureTensorEdgeInfo" ; }
	} CurvatureTensorEdgeInfo ;
445
	typedef NoTypeNameAttribute<CurvatureTensorEdgeInfo> EdgeInfo ;
446 447 448 449 450 451 452 453 454 455

	EdgeAttribute<EdgeInfo> edgeInfo ;
	EdgeAttribute<REAL> edgeangle ;

	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

	Approximator<PFP, VEC3,EDGE>* m_positionApproximator ;

	void initEdgeInfo(Dart d) ;
456
	void updateEdgeInfo(Dart d) ; // TODO : usually has a 2nd arg (, bool recompute) : why ??
457 458 459
	void computeEdgeInfo(Dart d, EdgeInfo& einfo) ;

public:
460
	EdgeSelector_CurvatureTensor(MAP& m, VertexAttribute<VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
461
		Selector<PFP>(m, pos, approx),
462 463 464 465 466 467
		m_positionApproximator(NULL)
	{
		edgeangle = m.template getAttribute<REAL, EDGE>("edgeangle") ;
		if(!edgeangle.isValid())
		{
			edgeangle = m.template addAttribute<REAL, EDGE>("edgeangle") ;
468
			Algo::Surface::Geometry::computeAnglesBetweenNormalsOnEdges<PFP>(m, pos, edgeangle) ;
469 470 471 472 473 474 475 476 477 478 479
		}

		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
	}
	~EdgeSelector_CurvatureTensor()
	{
		this->m_map.removeAttribute(edgeangle) ; // TODO : pas malin s'il existait avant
		this->m_map.removeAttribute(edgeInfo) ;
	}
	SelectorType getType() { return S_CurvatureTensor ; }
	bool init() ;
480
	bool nextEdge(Dart& d) const ;
481 482 483 484 485 486 487
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;

	void updateWithoutCollapse() {};
} ;


Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
488
template <typename PFP>
489
class EdgeSelector_MinDetail : public Selector<PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
490 491 492 493 494 495 496 497 498 499 500 501 502
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "MinDetailEdgeInfo" ; }
	} MinDetailEdgeInfo ;
503
	typedef NoTypeNameAttribute<MinDetailEdgeInfo> EdgeInfo ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
504

505
	EdgeAttribute<EdgeInfo> edgeInfo ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
506 507 508 509

	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

510
	Approximator<PFP, typename PFP::VEC3, EDGE>* m_positionApproximator ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
511 512 513 514 515 516

	void initEdgeInfo(Dart d) ;
	void updateEdgeInfo(Dart d, bool recompute) ;
	void computeEdgeInfo(Dart d, EdgeInfo& einfo) ;

public:
517
	EdgeSelector_MinDetail(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
518
		Selector<PFP>(m, pos, approx),
519
		m_positionApproximator(NULL)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
520
	{
521
		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
522 523 524 525 526 527 528
	}
	~EdgeSelector_MinDetail()
	{
		this->m_map.removeAttribute(edgeInfo) ;
	}
	SelectorType getType() { return S_MinDetail ; }
	bool init() ;
529
	bool nextEdge(Dart& d) const ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
530 531
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;
untereiner's avatar
untereiner committed
532

533
	void updateWithoutCollapse();
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
534 535
} ;

536
/*****************************************************************************************************************
537
 *                                      EDGE NAIVE COLOR METRIC (using QEMml)                                    *
538 539
 *****************************************************************************************************************/
template <typename PFP>
540
class EdgeSelector_ColorNaive : public Selector<PFP>
541 542 543 544 545 546 547 548 549 550 551 552 553
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "ColorNaiveEdgeInfo" ; }
	} ColorNaiveedgeInfo ;
554
	typedef NoTypeNameAttribute<ColorNaiveedgeInfo> EdgeInfo ;
555 556

	EdgeAttribute<EdgeInfo> edgeInfo ;
557
	VertexAttribute<Utils::Quadric<REAL> > m_quadric ;
558

559 560 561 562
	VertexAttribute<VEC3> m_pos, m_color ;
	int m_approxindex_pos, m_attrindex_pos ;
	int m_approxindex_color, m_attrindex_color ;

563
	std::vector<Approximator<PFP, typename PFP::VEC3, EDGE>* > m_approx ;
564

565 566 567 568 569 570 571 572 573
	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

	void initEdgeInfo(Dart d) ;
	void updateEdgeInfo(Dart d, bool recompute) ;
	void computeEdgeInfo(Dart d,EdgeInfo& einfo) ;
	void recomputeQuadric(const Dart d, const bool recomputeNeighbors = false) ;

public:
574
	EdgeSelector_ColorNaive(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
575
		Selector<PFP>(m, pos, approx),
576 577 578 579
		m_approxindex_pos(-1),
		m_attrindex_pos(-1),
		m_approxindex_color(-1),
		m_attrindex_color(-1)
580 581
	{
		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
582
		m_quadric = m.template addAttribute<Utils::Quadric<REAL>, VERTEX>("QEMquadric") ;
583 584 585 586 587 588 589 590
	}
	~EdgeSelector_ColorNaive()
	{
		this->m_map.removeAttribute(edgeInfo) ;
		this->m_map.removeAttribute(m_quadric) ;
	}
	SelectorType getType() { return S_ColorNaive ; }
	bool init() ;
591
	bool nextEdge(Dart& d) const ;
592 593
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
594 595

	void updateWithoutCollapse() { }
596 597
} ;

598 599 600 601
/*****************************************************************************************************************
 *                                  EDGE GEOMETRY+COLOR METRIC (using QEMml and Gradient norm)                   *
 *****************************************************************************************************************/
template <typename PFP>
602
class EdgeSelector_GeomColOptGradient : public Selector<PFP>
603 604 605 606 607 608 609 610 611 612 613 614 615
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "GeomColOptGradEdgeInfo" ; }
	} ColorNaiveedgeInfo ;
616
	typedef NoTypeNameAttribute<ColorNaiveedgeInfo> EdgeInfo ;
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

	EdgeAttribute<EdgeInfo> edgeInfo ;
	VertexAttribute<Utils::Quadric<REAL> > m_quadric ;

	VertexAttribute<VEC3> m_pos, m_color ;
	int m_approxindex_pos, m_attrindex_pos ;
	int m_approxindex_color, m_attrindex_color ;

	std::vector<Approximator<PFP, typename PFP::VEC3, EDGE>* > m_approx ;

	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

	void initEdgeInfo(Dart d) ;
	void updateEdgeInfo(Dart d) ;
	void computeEdgeInfo(Dart d,EdgeInfo& einfo) ;
	void recomputeQuadric(const Dart d, const bool recomputeNeighbors = false) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
634
	VEC3 computeEdgeGradientColorError(const Dart& v0, const VEC3& p, const VEC3& c) ;
635 636 637

public:
	EdgeSelector_GeomColOptGradient(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
638
		Selector<PFP>(m, pos, approx),
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
		m_approxindex_pos(-1),
		m_attrindex_pos(-1),
		m_approxindex_color(-1),
		m_attrindex_color(-1)
	{
		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
		m_quadric = m.template addAttribute<Utils::Quadric<REAL>, VERTEX>("QEMquadric") ;
	}
	~EdgeSelector_GeomColOptGradient()
	{
		this->m_map.removeAttribute(edgeInfo) ;
		this->m_map.removeAttribute(m_quadric) ;
	}
	SelectorType getType() { return S_GeomColOptGrad ; }
	bool init() ;
654
	bool nextEdge(Dart& d) const ;
655 656 657 658 659
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;

	void updateWithoutCollapse() { }

660
	void getEdgeErrors(EdgeAttribute<typename PFP::REAL> *errors) const
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
	{
		assert(errors != NULL || !"EdgeSelector::setColorMap requires non null vertexattribute argument") ;
		if (!errors->isValid())
			std::cerr << "EdgeSelector::setColorMap requires valid edgeattribute argument" << std::endl ;
		assert(edgeInfo.isValid()) ;

		TraversorE<typename PFP::MAP> travE(this->m_map) ;
		for(Dart d = travE.begin() ; d != travE.end() ; d = travE.next())
		{
			(*errors)[d] = -1 ;
			if (edgeInfo[d].valid)
			{
				(*errors)[d] = edgeInfo[d].it->first ;
			}
		}
	}
} ;

679 680 681 682
/*****************************************************************************************************************
 *                                 QEM extended to color metric                                                  *
 *****************************************************************************************************************/
template <typename PFP>
683
class EdgeSelector_QEMextColor : public Selector<PFP>
684 685 686 687 688 689 690 691 692 693 694 695 696 697
{
public:
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::REAL REAL ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename Geom::Vector<6,REAL> VEC6 ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "QEMextColorEdgeInfo" ; }
	} QEMextColorEdgeInfo ;
698
	typedef NoTypeNameAttribute<QEMextColorEdgeInfo> EdgeInfo ;
699 700

	EdgeAttribute<EdgeInfo> edgeInfo ;
701
	VertexAttribute<Utils::QuadricNd<REAL,6> > m_quadric ;
702

703 704 705 706
	VertexAttribute<VEC3> m_pos, m_color ;
	int m_approxindex_pos, m_attrindex_pos ;
	int m_approxindex_color, m_attrindex_color ;

707
	std::vector<Approximator<PFP, typename PFP::VEC3, EDGE>* > m_approx ;
708

709 710 711 712 713 714 715 716 717
	std::multimap<float,Dart> edges ;
	typename std::multimap<float,Dart>::iterator cur ;

	void initEdgeInfo(Dart d) ;
	void updateEdgeInfo(Dart d, bool recompute) ;
	void computeEdgeInfo(Dart d,EdgeInfo& einfo) ;
	void recomputeQuadric(const Dart d, const bool recomputeNeighbors = false) ;

public:
718
	EdgeSelector_QEMextColor(MAP& m, VertexAttribute<typename PFP::VEC3>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
719
		Selector<PFP>(m, pos, approx),
720 721 722 723
		m_approxindex_pos(-1),
		m_attrindex_pos(-1),
		m_approxindex_color(-1),
		m_attrindex_color(-1)
724 725
	{
		edgeInfo = m.template addAttribute<EdgeInfo, EDGE>("edgeInfo") ;
726
		m_quadric = m.template addAttribute<Utils::QuadricNd<REAL,6>, VERTEX>("QEMext-quadric") ;
727 728 729 730 731 732 733 734
	}
	~EdgeSelector_QEMextColor()
	{
		this->m_map.removeAttribute(edgeInfo) ;
		this->m_map.removeAttribute(m_quadric) ;
	}
	SelectorType getType() { return S_QEMextColor ; }
	bool init() ;
735
	bool nextEdge(Dart& d) const ;
736 737
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
738 739

	void updateWithoutCollapse() { }
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
740

741
	void getEdgeErrors(EdgeAttribute<typename PFP::REAL> *errors) const
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
	{
		assert(errors != NULL || !"EdgeSelector::setColorMap requires non null vertexattribute argument") ;
		if (!errors->isValid())
			std::cerr << "EdgeSelector::setColorMap requires valid edgeattribute argument" << std::endl ;
		assert(edgeInfo.isValid()) ;

		TraversorE<typename PFP::MAP> travE(this->m_map) ;
		for(Dart d = travE.begin() ; d != travE.end() ; d = travE.next())
		{
			(*errors)[d] = -1 ;
			if (edgeInfo[d].valid)
			{
				(*errors)[d] = edgeInfo[d].it->first ;
			}
		}
	}
758
} ;
759

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
760 761
} // namespace Decimation

762
} // namespace Surface
763

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
764 765 766 767 768 769 770
} // namespace Algo

} // namespace CGoGN

#include "Algo/Decimation/edgeSelector.hpp"

#endif