halfEdgeSelector.h 13.4 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
29
30
31
32
33
34
35
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __HALFEDGESELECTOR_H__
#define __HALFEDGESELECTOR_H__

#include "Algo/Decimation/selector.h"

namespace CGoGN
{

namespace Algo
{

36
37
38
namespace Surface
{

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
39
40
41
namespace Decimation
{

42
43
44
/*****************************************************************************************************************
 *                                 HALF-EDGE MEMORYLESS QEM METRIC                                               *
 *****************************************************************************************************************/
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
45
template <typename PFP>
46
class HalfEdgeSelector_QEMml : public Selector<PFP>
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
47
48
49
{
public:
	typedef typename PFP::MAP MAP ;
Pierre Kraemer's avatar
Pierre Kraemer committed
50
	typedef typename PFP::MAP::IMPL MAP_IMPL ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
51
52
53
54
55
56
57
58
59
60
	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 "QEMhalfEdgeInfo" ; }
	} QEMhalfEdgeInfo ;
61
	typedef NoTypeNameAttribute<QEMhalfEdgeInfo> HalfEdgeInfo ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
62

Pierre Kraemer's avatar
Pierre Kraemer committed
63
64
	DartAttribute<HalfEdgeInfo, MAP_IMPL> halfEdgeInfo ;
	VertexAttribute<Utils::Quadric<REAL>, MAP_IMPL> quadric ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
65
66
67
68

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

Pierre Kraemer's avatar
Pierre Kraemer committed
69
	Approximator<PFP, VEC3, DART>* m_positionApproximator ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
70
71
72
73
74
75
76

	void initHalfEdgeInfo(Dart d) ;
	void updateHalfEdgeInfo(Dart d, bool recompute) ;
	void computeHalfEdgeInfo(Dart d, HalfEdgeInfo& einfo) ;
	void recomputeQuadric(const Dart d, const bool recomputeNeighbors = false) ;

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
77
	HalfEdgeSelector_QEMml(MAP& m, VertexAttribute<VEC3, MAP_IMPL>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
78
		Selector<PFP>(m, pos, approx),
79
		m_positionApproximator(NULL)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
80
	{
81
		halfEdgeInfo = m.template addAttribute<HalfEdgeInfo, DART>("halfEdgeInfo") ;
82
		quadric = m.template addAttribute<Utils::Quadric<REAL>, VERTEX>("QEMquadric") ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
83
84
85
86
87
88
89
90
	}
	~HalfEdgeSelector_QEMml()
	{
		this->m_map.removeAttribute(quadric) ;
		this->m_map.removeAttribute(halfEdgeInfo) ;
	}
	SelectorType getType() { return S_hQEMml ; }
	bool init() ;
91
	bool nextEdge(Dart& d) const ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
92
93
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
94
95

	void updateWithoutCollapse() { }
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
96
97
} ;

98
99
100
101
/*****************************************************************************************************************
 *                                 HALF-EDGE QEMextColor METRIC                                                  *
 *****************************************************************************************************************/
template <typename PFP>
102
class HalfEdgeSelector_QEMextColor : public Selector<PFP>
103
104
105
{
public:
	typedef typename PFP::MAP MAP ;
Pierre Kraemer's avatar
Pierre Kraemer committed
106
	typedef typename PFP::MAP::IMPL MAP_IMPL ;
107
108
109
110
111
112
113
114
115
116
117
	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 "QEMextColorHalfEdgeInfo" ; }
	} QEMextColorHalfEdgeInfo ;
118
	typedef NoTypeNameAttribute<QEMextColorHalfEdgeInfo> HalfEdgeInfo ;
119

Pierre Kraemer's avatar
Pierre Kraemer committed
120
121
	DartAttribute<HalfEdgeInfo, MAP_IMPL> halfEdgeInfo ;
	VertexAttribute<Utils::QuadricNd<REAL,6>, MAP_IMPL> m_quadric ;
122

Pierre Kraemer's avatar
Pierre Kraemer committed
123
	VertexAttribute<VEC3, MAP_IMPL> m_pos, m_color ;
124
125
126
	int m_approxindex_pos, m_attrindex_pos ;
	int m_approxindex_color, m_attrindex_color ;

Pierre Kraemer's avatar
Pierre Kraemer committed
127
	std::vector<Approximator<PFP, VEC3, DART>*> m_approx ;
128
129
130
131
132
133
134
135
136
137

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

	void initHalfEdgeInfo(Dart d) ;
	void updateHalfEdgeInfo(Dart d, bool recompute) ;
	void computeHalfEdgeInfo(Dart d, HalfEdgeInfo& einfo) ;
	void recomputeQuadric(const Dart d, const bool recomputeNeighbors = false) ;

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
138
	HalfEdgeSelector_QEMextColor(MAP& m, VertexAttribute<VEC3, MAP_IMPL>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
139
		Selector<PFP>(m, pos, approx),
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
		m_approxindex_pos(-1),
		m_attrindex_pos(-1),
		m_approxindex_color(-1),
		m_attrindex_color(-1)
	{
		halfEdgeInfo = m.template addAttribute<HalfEdgeInfo, DART>("halfEdgeInfo") ;
		m_quadric = m.template addAttribute<Utils::QuadricNd<REAL,6>, VERTEX>("hQEMext-quadric") ;
	}
	~HalfEdgeSelector_QEMextColor()
	{
		this->m_map.removeAttribute(m_quadric) ;
		this->m_map.removeAttribute(halfEdgeInfo) ;
	}
	SelectorType getType() { return S_hQEMextColor ; }
	bool init() ;
155
	bool nextEdge(Dart& d) const ;
156
157
158
159
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;

	void updateWithoutCollapse() { }
160

Pierre Kraemer's avatar
Pierre Kraemer committed
161
	void getEdgeErrors(EdgeAttribute<REAL, MAP_IMPL> *errors) const
Kenneth Vanhoey's avatar
up    
Kenneth Vanhoey committed
162
163
164
165
166
167
	{
		assert(errors != NULL || !"EdgeSelector::setColorMap requires non null vertexattribute argument") ;
		if (!errors->isValid())
			std::cerr << "EdgeSelector::setColorMap requires valid edgeattribute argument" << std::endl ;
		assert(halfEdgeInfo.isValid()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
168
		TraversorE<MAP> travE(this->m_map) ;
Kenneth Vanhoey's avatar
up    
Kenneth Vanhoey committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
		for(Dart d = travE.begin() ; d != travE.end() ; d = travE.next())
		{
			Dart dd = this->m_map.phi2(d) ;
			if (halfEdgeInfo[d].valid)
			{
				(*errors)[d] = halfEdgeInfo[d].it->first ;
			}
			if (halfEdgeInfo[dd].valid && halfEdgeInfo[dd].it->first < (*errors)[d])
			{
				(*errors)[d] = halfEdgeInfo[dd].it->first ;
			}
			if (!(halfEdgeInfo[d].valid || halfEdgeInfo[dd].valid))
				(*errors)[d] = -1 ;
		}
	}
} ;

/*****************************************************************************************************************
 *                           HALF-EDGE QEMextColorNormal METRIC                                                  *
 *****************************************************************************************************************/
template <typename PFP>
class HalfEdgeSelector_QEMextColorNormal : public Selector<PFP>
{
public:
	typedef typename PFP::MAP MAP ;
Pierre Kraemer's avatar
Pierre Kraemer committed
194
	typedef typename PFP::MAP::IMPL MAP_IMPL ;
Kenneth Vanhoey's avatar
up    
Kenneth Vanhoey committed
195
196
197
198
199
200
201
202
203
204
205
206
207
	typedef typename PFP::REAL REAL ;
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename Geom::Vector<9,REAL> VEC9 ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "QEMextColorNormalHalfEdgeInfo" ; }
	} QEMextColorNormalHalfEdgeInfo ;
	typedef NoTypeNameAttribute<QEMextColorNormalHalfEdgeInfo> HalfEdgeInfo ;

Pierre Kraemer's avatar
Pierre Kraemer committed
208
209
	DartAttribute<HalfEdgeInfo, MAP_IMPL> halfEdgeInfo ;
	VertexAttribute<Utils::QuadricNd<REAL,9>, MAP_IMPL> m_quadric ;
Kenneth Vanhoey's avatar
up    
Kenneth Vanhoey committed
210

Pierre Kraemer's avatar
Pierre Kraemer committed
211
	VertexAttribute<VEC3, MAP_IMPL> m_pos, m_color, m_normal ;
Kenneth Vanhoey's avatar
up    
Kenneth Vanhoey committed
212
213
214
215
	int m_approxindex_pos, m_attrindex_pos ;
	int m_approxindex_color, m_attrindex_color ;
	int m_approxindex_normal, m_attrindex_normal ;

Pierre Kraemer's avatar
Pierre Kraemer committed
216
	std::vector<Approximator<PFP, VEC3, DART>*> m_approx ;
Kenneth Vanhoey's avatar
up    
Kenneth Vanhoey committed
217
218
219
220
221
222
223
224
225
226

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

	void initHalfEdgeInfo(Dart d) ;
	void updateHalfEdgeInfo(Dart d, bool recompute) ;
	void computeHalfEdgeInfo(Dart d, HalfEdgeInfo& einfo) ;
	void recomputeQuadric(const Dart d, const bool recomputeNeighbors = false) ;

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
227
	HalfEdgeSelector_QEMextColorNormal(MAP& m, VertexAttribute<VEC3, MAP_IMPL>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
Kenneth Vanhoey's avatar
up    
Kenneth Vanhoey committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		Selector<PFP>(m, pos, approx),
		m_approxindex_pos(-1),
		m_attrindex_pos(-1),
		m_approxindex_color(-1),
		m_attrindex_color(-1),
		m_approxindex_normal(-1),
		m_attrindex_normal(-1)
	{
		halfEdgeInfo = m.template addAttribute<HalfEdgeInfo, DART>("halfEdgeInfo") ;
		m_quadric = m.template addAttribute<Utils::QuadricNd<REAL,9>, VERTEX>("hQEMextNormal-quadric") ;
	}
	~HalfEdgeSelector_QEMextColorNormal()
	{
		this->m_map.removeAttribute(m_quadric) ;
		this->m_map.removeAttribute(halfEdgeInfo) ;
	}
	SelectorType getType() { return S_hQEMextColorNormal ; }
	bool init() ;
	bool nextEdge(Dart& d) const ;
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;

	void updateWithoutCollapse() { }

Pierre Kraemer's avatar
Pierre Kraemer committed
252
	void getEdgeErrors(EdgeAttribute<REAL, MAP_IMPL> *errors) const
253
254
255
256
257
258
	{
		assert(errors != NULL || !"EdgeSelector::setColorMap requires non null vertexattribute argument") ;
		if (!errors->isValid())
			std::cerr << "EdgeSelector::setColorMap requires valid edgeattribute argument" << std::endl ;
		assert(halfEdgeInfo.isValid()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
259
		TraversorE<MAP> travE(this->m_map) ;
260
261
		for(Dart d = travE.begin() ; d != travE.end() ; d = travE.next())
		{
262
			Dart dd = this->m_map.phi2(d) ;
263
264
265
266
			if (halfEdgeInfo[d].valid)
			{
				(*errors)[d] = halfEdgeInfo[d].it->first ;
			}
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
267
268
269
270
			if (halfEdgeInfo[dd].valid && halfEdgeInfo[dd].it->first < (*errors)[d])
			{
				(*errors)[d] = halfEdgeInfo[dd].it->first ;
			}
271
272
			if (!(halfEdgeInfo[d].valid || halfEdgeInfo[dd].valid))
				(*errors)[d] = -1 ;
273
274
		}
	}
275
276
277
} ;

/*****************************************************************************************************************
278
 *                                 HALF-EDGE COLOR GRADIENT ERR                                                  *
279
280
 *****************************************************************************************************************/
template <typename PFP>
281
class HalfEdgeSelector_ColorGradient : public Selector<PFP>
282
283
284
{
public:
	typedef typename PFP::MAP MAP ;
Pierre Kraemer's avatar
Pierre Kraemer committed
285
	typedef typename PFP::MAP::IMPL MAP_IMPL ;
286
287
288
289
290
291
292
293
294
295
	typedef typename PFP::REAL REAL ;
	typedef typename PFP::VEC3 VEC3 ;

private:
	typedef	struct
	{
		typename std::multimap<float,Dart>::iterator it ;
		bool valid ;
		static std::string CGoGNnameOfType() { return "ColorExperimentalHalfEdgeInfo" ; }
	} QEMextColorHalfEdgeInfo ;
296
	typedef NoTypeNameAttribute<QEMextColorHalfEdgeInfo> HalfEdgeInfo ;
297

Pierre Kraemer's avatar
Pierre Kraemer committed
298
299
	DartAttribute<HalfEdgeInfo, MAP_IMPL> halfEdgeInfo ;
	VertexAttribute<Utils::Quadric<REAL>, MAP_IMPL> m_quadric ;
300

Pierre Kraemer's avatar
Pierre Kraemer committed
301
	VertexAttribute<VEC3, MAP_IMPL> m_pos, m_color ;
302
303
304
	int m_approxindex_pos, m_attrindex_pos ;
	int m_approxindex_color, m_attrindex_color ;

Pierre Kraemer's avatar
Pierre Kraemer committed
305
	std::vector<Approximator<PFP, VEC3, DART>*> m_approx ;
306
307
308
309
310
311
312
313
314
315

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

	void initHalfEdgeInfo(Dart d) ;
	void updateHalfEdgeInfo(Dart d) ;
	void computeHalfEdgeInfo(Dart d, HalfEdgeInfo& einfo) ;
	//void recomputeQuadric(const Dart d, const bool recomputeNeighbors = false) ;
	void recomputeQuadric(const Dart d) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
316
	VEC3 computeGradientColorError(const Dart& v0, const Dart& v1) const ;
317
318

public:
Pierre Kraemer's avatar
Pierre Kraemer committed
319
	HalfEdgeSelector_ColorGradient(MAP& m, VertexAttribute<VEC3, MAP_IMPL>& pos, std::vector<ApproximatorGen<PFP>*>& approx) :
320
		Selector<PFP>(m, pos, approx),
321
322
323
324
325
326
327
328
		m_approxindex_pos(-1),
		m_attrindex_pos(-1),
		m_approxindex_color(-1),
		m_attrindex_color(-1)
	{
		halfEdgeInfo = m.template addAttribute<HalfEdgeInfo, DART>("halfEdgeInfo") ;
		m_quadric = m.template addAttribute<Utils::Quadric<REAL>, VERTEX>("QEMquadric") ;
	}
329
	~HalfEdgeSelector_ColorGradient()
330
331
332
333
	{
		this->m_map.removeAttribute(m_quadric) ;
		this->m_map.removeAttribute(halfEdgeInfo) ;
	}
334
	SelectorType getType() { return S_hColorGradient ; }
335
	bool init() ;
336
	bool nextEdge(Dart& d) const ;
337
338
339
340
341
	void updateBeforeCollapse(Dart d) ;
	void updateAfterCollapse(Dart d2, Dart dd2) ;

	void updateWithoutCollapse() { }

Pierre Kraemer's avatar
Pierre Kraemer committed
342
	void getEdgeErrors(EdgeAttribute<REAL, MAP_IMPL> *errors) const
343
344
345
346
347
348
	{
		assert(errors != NULL || !"EdgeSelector::setColorMap requires non null vertexattribute argument") ;
		if (!errors->isValid())
			std::cerr << "EdgeSelector::setColorMap requires valid edgeattribute argument" << std::endl ;
		assert(halfEdgeInfo.isValid()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
349
		TraversorE<MAP> travE(this->m_map) ;
350
351
		for(Dart d = travE.begin() ; d != travE.end() ; d = travE.next())
		{
352
			Dart dd = this->m_map.phi2(d) ;
353
354
355
356
			if (halfEdgeInfo[d].valid)
			{
				(*errors)[d] = halfEdgeInfo[d].it->first ;
			}
357
358
359
360
361
362
363
364
365
366
367
			if (halfEdgeInfo[dd].valid && halfEdgeInfo[dd].it->first < (*errors)[d])
			{
				(*errors)[d] = halfEdgeInfo[dd].it->first ;
			}
			if (!(halfEdgeInfo[d].valid || halfEdgeInfo[dd].valid))
				(*errors)[d] = -1 ;

		}
	}
} ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
368
369
} // namespace Decimation

370
} // namespace Surface
371

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
372
373
374
375
376
377
378
} // namespace Algo

} // namespace CGoGN

#include "Algo/Decimation/halfEdgeSelector.hpp"

#endif