Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

voronoiDiagrams.hpp 8.72 KB
Newer Older
Basile Sauvage's avatar
Basile Sauvage committed
1
2
3
4
5
6
7
8
9
namespace CGoGN
{

namespace Algo
{

namespace Geometry
{

10
11
12
13
/***********************************************************
 * class VoronoiDiagram
 ***********************************************************/

Basile Sauvage's avatar
Basile Sauvage committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
template <typename PFP>
VoronoiDiagram<PFP>::VoronoiDiagram (typename PFP::MAP& m, const EdgeAttribute<REAL>& p, VertexAttribute<unsigned int>& r) : map(m), edgeCost (p), regions (r), vmReached(m)
{
	vertexInfo = map.template addAttribute<VertexInfo, VERTEX>("vertexInfo");
}

template <typename PFP>
VoronoiDiagram<PFP>::~VoronoiDiagram ()
{
	map.removeAttribute(vertexInfo);
}

template <typename PFP>
void VoronoiDiagram<PFP>::clear ()
{
	regions.setAllValues(0);
	border.clear();
	front.clear();
	vmReached.unmarkAll();
}

template <typename PFP>
void VoronoiDiagram<PFP>::setSeeds (const std::vector<Dart>& s)
{
Basile Sauvage's avatar
Basile Sauvage committed
38
39
	seeds.clear();
//	clear();
Basile Sauvage's avatar
Basile Sauvage committed
40
41
42
43
44
45
	seeds = s;
}

template <typename PFP>
void VoronoiDiagram<PFP>::setRandomSeeds (unsigned int nseeds)
{
Basile Sauvage's avatar
Basile Sauvage committed
46
47
48
	seeds.clear();
//	clear();
	vmReached.unmarkAll();
Basile Sauvage's avatar
Basile Sauvage committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

	srand ( time(NULL) );
	unsigned int n = nseeds;
	while (n > 0)
	{ // TODO : correct this random init which assumes contiguous Dart table
		Dart d = rand() % map.getNbDarts() ;
		if (! vmReached.isMarked(d))
		{
			vmReached.mark(d);
			seeds.push_back(d);
			n--;
		}
	}
}

template <typename PFP>
void VoronoiDiagram<PFP>::initFrontWithSeeds ()
{
Basile Sauvage's avatar
Basile Sauvage committed
67
68
//	vmReached.unmarkAll();
	clear();
Basile Sauvage's avatar
Basile Sauvage committed
69
70
71
72
73
74
	for (unsigned int i = 0; i < seeds.size(); i++)
	{
		Dart d = seeds[i];
		vmReached.mark(d);
		vertexInfo[d].it = front.insert(std::pair<float,Dart>(0.0, d));
		vertexInfo[d].valid = true;
75
76
		regions[d] = i;
		vertexInfo[d].pathOrigin = d;
Basile Sauvage's avatar
Basile Sauvage committed
77
78
79
80
81
82
83
84
	}
}

template <typename PFP>
void VoronoiDiagram<PFP>::setCost (const EdgeAttribute<typename PFP::REAL>& c){
	edgeCost = c;
}

85
86
87
template <typename PFP>
void VoronoiDiagram<PFP>::collectVertexFromFront(Dart e){
	regions[e] = regions[vertexInfo[e].pathOrigin];
88
89
	front.erase(vertexInfo[e].it);
	vertexInfo[e].valid=false;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
}

template <typename PFP>
void VoronoiDiagram<PFP>::addVertexToFront(Dart f, float d){
	VertexInfo& vi (vertexInfo[f]);
	vi.it = front.insert(std::pair<float,Dart>(d + edgeCost[f], f));
	vi.valid=true;
	vi.pathOrigin = map.phi2(f);
	vmReached.mark(f);
}

template <typename PFP>
void VoronoiDiagram<PFP>::updateVertexInFront(Dart f, float d){
	VertexInfo& vi (vertexInfo[f]);
	float dist = d + edgeCost[f];
	if (dist < vi.it->first)
	{
		front.erase(vi.it);
		vi.it = front.insert(std::pair<float,Dart>(dist, f));
		vi.pathOrigin = map.phi2(f);
	}
}
Basile Sauvage's avatar
Basile Sauvage committed
112
113
114
115
116
117
118
119
120
121

template <typename PFP>
void VoronoiDiagram<PFP>::computeDiagram ()
{
	initFrontWithSeeds();

	while ( !front.empty() )
	{
		Dart e = front.begin()->second;
		float d = front.begin()->first;
122
123

		collectVertexFromFront(e);
Basile Sauvage's avatar
Basile Sauvage committed
124
125
126
127
128

		Traversor2VVaE<typename PFP::MAP> tv (map, e);
		for (Dart f = tv.begin(); f != tv.end(); f=tv.next())
		{
			if (vmReached.isMarked(f))
129
130
131
132
133
			{ // f has been reached
				if (vertexInfo[f].valid) // f is in the front : update
					updateVertexInFront(f,d);
				else // f is not in the front any more (already collected) : detect a border edge
					if ( regions[f] != regions[e] ) border.push_back(f);
Basile Sauvage's avatar
Basile Sauvage committed
134
135
			}
			else
136
137
			{ // f has not been reached : add it to the front
				addVertexToFront(f,d);
Basile Sauvage's avatar
Basile Sauvage committed
138
139
140
141
142
			}
		}
	}
}

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
template <typename PFP>
void VoronoiDiagram<PFP>::computeDistancesWithinRegion (Dart seed)
{ // TODO : this algo has not yet been tested
	// init
	front.clear();
	vmReached.unmarkAll();

	vmReached.mark(seed);
	vertexInfo[seed].it = front.insert(std::pair<float,Dart>(0.0, seed));
	vertexInfo[seed].valid = true;
	vertexInfo[seed].pathOrigin = seed;

	//compute
	while ( !front.empty() )
	{
		Dart e = front.begin()->second;
		float d = front.begin()->first;

		collectVertexFromFront(e);

		Traversor2VVaE<typename PFP::MAP> tv (map, e);
		for (Dart f = tv.begin(); f != tv.end(); f=tv.next())
		{
			if (vmReached.isMarked(f))
			{ // f has been reached
				if (vertexInfo[f].valid) updateVertexInFront(f,d); // f is in the front : update
			}
			else
			{ // f has not been reached : add it to the front
				if ( regions[f] == regions[e] ) addVertexToFront(f,d);
			}
		}
	}
}

178
179
180
181
182
/***********************************************************
 * class CentroidalVoronoiDiagram
 ***********************************************************/

template <typename PFP>
Basile Sauvage's avatar
Basile Sauvage committed
183
184
CentroidalVoronoiDiagram<PFP>::CentroidalVoronoiDiagram (typename PFP::MAP& m, const EdgeAttribute<REAL>& c, VertexAttribute<unsigned int>& r, VertexAttribute<REAL>& d, VertexAttribute<Dart>& o, VertexAttribute<REAL>& a) :
	VoronoiDiagram<PFP>(m,c,r), distances(d), pathOrigins(o), areaElts(a)
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{
}

template <typename PFP>
CentroidalVoronoiDiagram<PFP>::~CentroidalVoronoiDiagram ()
{
}

template <typename PFP>
void CentroidalVoronoiDiagram<PFP>::clear ()
{
	VoronoiDiagram<PFP>::clear();
	distances.setAllValues(0.0);
}

template <typename PFP>
void CentroidalVoronoiDiagram<PFP>::collectVertexFromFront(Dart e){
202
	distances[e] = this->vertexInfo[e].it->first;
203
204
205
206
207
	pathOrigins[e] = this->vertexInfo[e].pathOrigin;

	VoronoiDiagram<PFP>::collectVertexFromFront(e);
}

208
209


210
template <typename PFP>
Basile Sauvage's avatar
Basile Sauvage committed
211
212
void CentroidalVoronoiDiagram<PFP>::cumulateEnergyOnPaths(){
	globalEnergy = 0.0;
213
214
	for (unsigned int i = 0; i < this->seeds.size(); i++)
	{
Basile Sauvage's avatar
Basile Sauvage committed
215
216
		cumulateEnergyFromRoot(this->seeds[i]);
		globalEnergy += distances[this->seeds[i]];
217
218
219
220
	}
}

template <typename PFP>
Basile Sauvage's avatar
Basile Sauvage committed
221
222
unsigned int CentroidalVoronoiDiagram<PFP>::moveSeeds(){
	unsigned int m = 0;
Basile Sauvage's avatar
Basile Sauvage committed
223
	globalEnergy = 0.0;
Basile Sauvage's avatar
Basile Sauvage committed
224
225
	for (unsigned int i = 0; i < this->seeds.size(); i++)
	{
226
		Dart oldSeed = this->seeds[i];
Basile Sauvage's avatar
Basile Sauvage committed
227
		m += moveSeed(i);
228
		globalEnergy += distances[oldSeed];
Basile Sauvage's avatar
Basile Sauvage committed
229
230
231
232
	}
	return m;
}

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
template <typename PFP>
unsigned int CentroidalVoronoiDiagram<PFP>::moveSeeds2(){
	unsigned int m = 0;
	globalEnergy = 0.0;
	for (unsigned int i = 0; i < this->seeds.size(); i++)
	{
		Dart oldSeed = this->seeds[i];
		int r = moveSeed(i);
		Dart newSeed = this->seeds[i];
		globalEnergy += distances[oldSeed];
		REAL regionEnergy = distances[oldSeed];
		if (r==1)
		{
			this->computeDistancesWithinRegion(newSeed);
			cumulateEnergyFromRoot(newSeed);
			if (distances[newSeed] < regionEnergy)
				m+=1;
			else
				this->seeds[i] = oldSeed;
		}
	}
	return m;
}

Basile Sauvage's avatar
Basile Sauvage committed
257
template <typename PFP>
Basile Sauvage's avatar
Basile Sauvage committed
258
259
260
typename PFP::REAL CentroidalVoronoiDiagram<PFP>::cumulateEnergyFromRoot(Dart e){
	REAL distArea = areaElts[e] * distances[e];
	distances[e] = areaElts[e] * distances[e] * distances[e];
Basile Sauvage's avatar
Basile Sauvage committed
261

262
263
264
265
266
	Traversor2VVaE<typename PFP::MAP> tv (this->map, e);
	for (Dart f = tv.begin(); f != tv.end(); f=tv.next())
	{
		if ( pathOrigins[f] == this->map.phi2(f))
		{
Basile Sauvage's avatar
Basile Sauvage committed
267
			distArea += cumulateEnergyFromRoot(f);
268
269
270
			distances[e] += distances[f];
		}
	}
Basile Sauvage's avatar
Basile Sauvage committed
271
	return distArea;
Basile Sauvage's avatar
Basile Sauvage committed
272
273
274
}

template <typename PFP>
Basile Sauvage's avatar
Basile Sauvage committed
275
276
277
unsigned int CentroidalVoronoiDiagram<PFP>::moveSeed(unsigned int numSeed){
	Dart e = this->seeds[numSeed];
	unsigned int seedMoved = 0;
Basile Sauvage's avatar
Basile Sauvage committed
278
279
280
281

	std::vector<Dart> v;
	v.reserve(8);

Basile Sauvage's avatar
Basile Sauvage committed
282
283
	std::vector<float> da;
	da.reserve(8);
Basile Sauvage's avatar
Basile Sauvage committed
284
285
286
287
288
289
290
291

	distances[e] = 0.0;

	Traversor2VVaE<typename PFP::MAP> tv (this->map, e);
	for (Dart f = tv.begin(); f != tv.end(); f=tv.next())
	{
		if ( pathOrigins[f] == this->map.phi2(f))
		{
Basile Sauvage's avatar
Basile Sauvage committed
292
293
			float distArea = cumulateEnergyFromRoot(f);
			da.push_back(distArea);
Basile Sauvage's avatar
Basile Sauvage committed
294
295
296
297
298
			distances[e] += distances[f];
			v.push_back(f);
		}
	}

Basile Sauvage's avatar
Basile Sauvage committed
299
	/* second attempt */
Basile Sauvage's avatar
Basile Sauvage committed
300
	// TODO : check if the computation of grad and proj is still valid for other edgeCost than geodesic distances
Basile Sauvage's avatar
Basile Sauvage committed
301
302
303
	VEC3 grad (0.0);
	const VertexAttribute<VEC3>& pos = this->map.template getAttribute<VEC3,VERTEX>("position");

Basile Sauvage's avatar
Basile Sauvage committed
304
	// compute the gradient
Basile Sauvage's avatar
Basile Sauvage committed
305
306
307
308
309
	for (unsigned int j = 0; j<v.size(); ++j)
	{
		Dart f = v[j];
		VEC3 edgeV = pos[f] - pos[this->map.phi2(f)];
		edgeV.normalize();
Basile Sauvage's avatar
Basile Sauvage committed
310
		grad += da[j] * edgeV;
Basile Sauvage's avatar
Basile Sauvage committed
311
	}
Basile Sauvage's avatar
Basile Sauvage committed
312
	grad /= 2.0;
Basile Sauvage's avatar
Basile Sauvage committed
313
314

	float maxProj = 0.0;
Basile Sauvage's avatar
Basile Sauvage committed
315
//	float memoForTest = 0.0;
Basile Sauvage's avatar
Basile Sauvage committed
316
317
318
319
320
321
	for (unsigned int j = 0; j<v.size(); ++j)
	{
		Dart f = v[j];
		VEC3 edgeV = pos[f] - pos[this->map.phi2(f)];
//		edgeV.normalize();
		float proj = edgeV * grad;
Basile Sauvage's avatar
Basile Sauvage committed
322
//		proj -= areaElts[e] * this->edgeCost[f] * this->edgeCost[f];
Basile Sauvage's avatar
Basile Sauvage committed
323
324
		if (proj > maxProj)
		{
Basile Sauvage's avatar
Basile Sauvage committed
325
326
//			if (numSeed==1) memoForTest = (edgeV * grad) / (areaElts[e] * this->edgeCost[f] * this->edgeCost[f]);
//				CGoGNout << (edgeV * grad) / (areaElts[e] * this->edgeCost[f] * this->edgeCost[f]) * this->seeds.size() << CGoGNendl;
Basile Sauvage's avatar
Basile Sauvage committed
327
//				CGoGNout << edgeV * grad << "\t - \t" << areaElts[e] * this->edgeCost[f] * this->edgeCost[f] << CGoGNendl;
Basile Sauvage's avatar
Basile Sauvage committed
328
329
330
331
332
			maxProj = proj;
			seedMoved = 1;
			this->seeds[numSeed] = v[j];
		}
	}
Basile Sauvage's avatar
Basile Sauvage committed
333
334
335
336

//	if (numSeed==1 && seedMoved==1)
//		CGoGNout << memoForTest * this->seeds.size() << CGoGNendl;

Basile Sauvage's avatar
Basile Sauvage committed
337
338
339
	/* end second attempt */

/* first attempt by approximating the energy change
Basile Sauvage's avatar
Basile Sauvage committed
340
341
342
343
344
345
346
347
348
	float minDE = 0.0;
	for (unsigned int j = 0; j<v.size(); ++j)
	{
		float c = this->edgeCost[v[j]];
		float de = regionArea * c * c;
		de += 2 * c *( distances[e] - 2*distances[v[j]] );
		if (de < minDE)
		{
			minDE = de;
Basile Sauvage's avatar
Basile Sauvage committed
349
350
			seedMoved = 1;
			this->seeds[numSeed] = v[j];
Basile Sauvage's avatar
Basile Sauvage committed
351
352
		}
	}
Basile Sauvage's avatar
Basile Sauvage committed
353
354
*/
	return seedMoved;
355
}
356

Basile Sauvage's avatar
Basile Sauvage committed
357
358
359
}// end namespace Geometry
}// end namespace Algo
}// end namespace CGoGN