Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

centroid.hpp 13.1 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
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#include "Topology/generic/cellmarker.h"
26
27
28
29
#include "Topology/generic/traversor/traversorCell.h"
#include "Topology/generic/traversor/traversor2.h"
#include "Topology/generic/traversor/traversorCell.h"
#include "Topology/generic/traversor/traversor3.h"
30
#include "Algo/Parallel/parallel_foreach.h"
31

Pierre Kraemer's avatar
Pierre Kraemer committed
32
33
34
35
36
37
namespace CGoGN
{

namespace Algo
{

38
39
40
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
41
42
43
namespace Geometry
{

44
template <typename PFP, typename V_ATT>
45
typename V_ATT::DATA_TYPE volumeCentroid(typename PFP::MAP& map, Vol d, const V_ATT& attributs, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
46
{
47
	typename V_ATT::DATA_TYPE center(0.0);
48
	unsigned int count = 0 ;
49
50
//	Traversor3WV<typename PFP::MAP> tra(map,d,false,thread);

51
	foreach_incident3<VERTEX>(map,d, [&] (Vertex v)
Pierre Kraemer's avatar
Pierre Kraemer committed
52
	{
53
		center += attributs[v];
Pierre Kraemer's avatar
Pierre Kraemer committed
54
55
		++count;
	}
56
	,false,thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
57
58
59
60
61

	center /= double(count) ;
	return center ;
}

62
template <typename PFP, typename V_ATT>
63
typename V_ATT::DATA_TYPE volumeCentroidELW(typename PFP::MAP& map, Vol d, const V_ATT& attributs, unsigned int thread)
64
{
65
	typedef typename V_ATT::DATA_TYPE EMB;
66
	EMB center(0.0);
67

68
	double count=0.0;
69
70
//	Traversor3WE<typename PFP::MAP> t(map, d,false,thread) ;

71
	foreach_incident3<EDGE>(map,d, [&] (Edge it)
72
	{
Sylvain Thery's avatar
Sylvain Thery committed
73
		EMB e1 = attributs[it.dart];
74
75
76
77
		EMB e2 = attributs[map.phi1(it)];
		double l = (e2-e1).norm();
		center += (e1+e2)*l;
		count += 2.0*l ;
78
	},false,thread);
79
80
81
82
	center /= double(count);	
	return center ;
}

83
template <typename PFP, typename V_ATT>
84
typename V_ATT::DATA_TYPE faceCentroid(typename PFP::MAP& map, Face f, const V_ATT& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
85
{
86
	typename V_ATT::DATA_TYPE center(0.0);
Pierre Kraemer's avatar
Pierre Kraemer committed
87
	unsigned int count = 0 ;
88
89

//	Traversor2FV<typename PFP::MAP> t(map, d) ;
90
91

	foreach_incident2<VERTEX>(map, f, [&](Vertex it)
Pierre Kraemer's avatar
Pierre Kraemer committed
92
93
94
	{
		center += attributs[it];
		++count ;
95
	});
Pierre Kraemer's avatar
Pierre Kraemer committed
96
97
98
99
	center /= double(count);
	return center ;
}

100
template <typename PFP, typename V_ATT>
101
typename V_ATT::DATA_TYPE faceCentroidELW(typename PFP::MAP& map, Face f, const V_ATT& attributs)
102
{
103
104
	typedef typename V_ATT::DATA_TYPE EMB;

105
	EMB center(0.0);
106
	double count=0.0;
107
108

//	Traversor2FE<typename PFP::MAP> t(map, d) ;
109
110

	foreach_incident2<EDGE>(map, f, [&](Edge it)
111
	{
Sylvain Thery's avatar
Sylvain Thery committed
112
		EMB e1 = attributs[it.dart];
113
114
115
116
		EMB e2 = attributs[map.phi1(it)];
		double l = (e2-e1).norm();
		center += (e1+e2)*l;
		count += 2.0*l ;
117
	});
118
119
120
121
	center /= double(count);
	return center ;
}

122
template <typename PFP, typename V_ATT>
123
typename V_ATT::DATA_TYPE vertexNeighborhoodCentroid(typename PFP::MAP& map, Vertex v, const V_ATT& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
124
{
125
126
	typename V_ATT::DATA_TYPE center(0.0);

Pierre Kraemer's avatar
Pierre Kraemer committed
127
	unsigned int count = 0 ;
128
//	Traversor2VVaE<typename PFP::MAP> t(map, d) ;
129
	foreach_adjacent2<EDGE>(map, v, [&](Vertex it)
Pierre Kraemer's avatar
Pierre Kraemer committed
130
	{
131
		center += attributs[it];
Pierre Kraemer's avatar
Pierre Kraemer committed
132
		++count ;
133
	});
Pierre Kraemer's avatar
Pierre Kraemer committed
134
135
136
137
	center /= count ;
	return center ;
}

138
139
template <typename PFP, typename V_ATT, typename F_ATT>
void computeCentroidFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
140
{
141
142
143
144
145
	foreach_cell<FACE>(map, [&] (Face f)
	{
		face_centroid[f] = faceCentroid<PFP,V_ATT>(map, f, position) ;
	}
	,false,thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
146
147
}

148
149
template <typename PFP, typename V_ATT, typename F_ATT>
void computeCentroidELWFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid, unsigned int thread)
150
{
151
152
153
154
155
	foreach_cell<FACE>(map, [&] (Face f)
	{
		face_centroid[f] = faceCentroidELW<PFP,V_ATT>(map, f, position) ;
	}
	,false,thread);
156
157
}

158
159
template <typename PFP, typename V_ATT>
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map, const V_ATT& position, V_ATT& vertex_centroid, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
160
{
161
162
163
164
165
	foreach_cell<VERTEX>(map, [&] (Vertex v)
	{
		vertex_centroid[v] = vertexNeighborhoodCentroid<PFP,V_ATT>(map, v, position) ;
	}
	,false,thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
166
167
}

168
169
170
171

namespace Parallel
{

172
template <typename PFP, typename V_ATT, typename F_ATT>
Pierre Kraemer's avatar
Pierre Kraemer committed
173
class FunctorComputeCentroidFaces: public FunctorMapThreaded<typename PFP::MAP>
174
{
175
176
	 const V_ATT& m_position;
	 F_ATT& m_fcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
177

178
public:
179
	 FunctorComputeCentroidFaces<PFP,V_ATT,F_ATT>( typename PFP::MAP& map, const V_ATT& position, F_ATT& fcentroid):
180
181
182
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_fcentroid(fcentroid)
	 { }

183
	void run(Dart d, unsigned int /*threadID*/)
184
185
186
187
188
	{
		m_fcentroid[d] = faceCentroid<PFP>(this->m_map, d, m_position) ;
	}
};

189
template <typename PFP, typename V_ATT, typename F_ATT>
Pierre Kraemer's avatar
Pierre Kraemer committed
190
class FunctorComputeCentroidELWFaces: public FunctorMapThreaded<typename PFP::MAP>
191
{
192
193
	const V_ATT& m_position;
	F_ATT& m_fcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
194

195
public:
196
	 FunctorComputeCentroidELWFaces<PFP,V_ATT,F_ATT>( typename PFP::MAP& map, const V_ATT& position, F_ATT& fcentroid):
197
198
199
200
201
202
203
204
205
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_fcentroid(fcentroid)
	 { }

	void run(Dart d, unsigned int threadID)
	{
		m_fcentroid[d] = faceCentroidELW<PFP>(this->m_map, d, m_position) ;
	}
};

206
207
208
209
210
template <typename PFP, typename V_ATT>
class FunctorComputeNeighborhoodCentroidVertices: public FunctorMapThreaded<typename PFP::MAP >
{
	 const V_ATT& m_position;
	 V_ATT& m_vcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
211

212
213
214
215
public:
	 FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT>( typename PFP::MAP& map, const V_ATT& position, V_ATT& vcentroid):
		 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vcentroid(vcentroid)
	 { }
216

217
218
219
220
221
	void run(Dart d, unsigned int /*threadID*/)
	{
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP>(this->m_map, d, m_position) ;
	}
};
222

223
224
template <typename PFP, typename V_ATT, typename F_ATT>
void computeCentroidFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid,
225
		unsigned int nbth, unsigned int current_thread)
226
{
227
	FunctorComputeCentroidFaces<PFP,V_ATT,F_ATT> funct(map,position,face_centroid);
228
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, current_thread);
229
230
}

231
232
template <typename PFP, typename V_ATT, typename F_ATT>
void computeCentroidELWFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid,
233
		unsigned int nbth, unsigned int current_thread)
234
{
235
	FunctorComputeCentroidELWFaces<PFP,V_ATT,F_ATT> funct(map,position,face_centroid);
236
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, current_thread);
237
238
}

239
template <typename PFP, typename V_ATT>
240
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map,
241
		const V_ATT& position, V_ATT& vertex_centroid,
242
		unsigned int nbth, unsigned int current_thread)
243
{
244
245
	FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT> funct(map,position,vertex_centroid);
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false);
246
247
}

Pierre Kraemer's avatar
Pierre Kraemer committed
248
249
250
251
} // namespace Parallel



252
} // namespace Geometry
Pierre Kraemer's avatar
Pierre Kraemer committed
253

254
} // namespace Surface
255

256
257
namespace Volume
{
Pierre Kraemer's avatar
Pierre Kraemer committed
258

259
namespace Geometry
Lionel Untereiner's avatar
Lionel Untereiner committed
260
{
261

262
template <typename PFP, typename V_ATT>
263
typename V_ATT::DATA_TYPE vertexNeighborhoodCentroid(typename PFP::MAP& map, Vertex v, const V_ATT& attributs, unsigned int thread)
Lionel Untereiner's avatar
Lionel Untereiner committed
264
{
265
	typename V_ATT::DATA_TYPE  center(0.0);
Lionel Untereiner's avatar
Lionel Untereiner committed
266
	unsigned int count = 0 ;
267
268
//	Traversor3VVaE<typename PFP::MAP> t(map, d) ;
//	for(Dart it = t.begin(); it != t.end(); it = t.next())
269
270
//	foreachAdjacent3MT(VERTEX,EDGE,d,it,typename PFP::MAP,map,thread)
	foreach_adjacent3<EDGE>(map, v, [&](Vertex it)
Lionel Untereiner's avatar
Lionel Untereiner committed
271
272
273
274
	{
		center += attributs[it];
		++count ;
	}
275
	,false,thread);
Lionel Untereiner's avatar
Lionel Untereiner committed
276
277
278
	center /= count ;
	return center ;
}
279

280
281
template <typename PFP, typename V_ATT, typename W_ATT>
void computeCentroidVolumes(typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid, unsigned int thread)
Lionel Untereiner's avatar
Lionel Untereiner committed
282
{
283
284
//	TraversorW<typename PFP::MAP> t(map, thread) ;
//	for(Dart d = t.begin(); d != t.end(); d = t.next())
285
286
287
288
289
290
//	foreachCellMT(VOLUME,d,typename PFP::MAP,map ,thread)
	foreach_cell<VOLUME>(map, [&] (Vol v)
	{
		vol_centroid[v] = Surface::Geometry::volumeCentroid<PFP,V_ATT>(map, v, position,thread) ;
	}
	,false,thread);
Lionel Untereiner's avatar
Lionel Untereiner committed
291
}
292

293
294
template <typename PFP, typename V_ATT, typename W_ATT>
void computeCentroidELWVolumes(typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid, unsigned int thread)
295
{
296
297
//	TraversorW<typename PFP::MAP> t(map,thread) ;
//	for(Dart d = t.begin(); d != t.end(); d = t.next())
298
299
300
301
302
303
//	foreachCellMT(VOLUME,d,typename PFP::MAP,map ,thread)
	foreach_cell<VOLUME>(map, [&] (Vol v)
	{
		vol_centroid[v] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(map, v, position,thread) ;
	}
	,false,thread);
304
305
}

306
307
template <typename PFP, typename V_ATT>
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map, const V_ATT& position, V_ATT& vertex_centroid, unsigned int thread)
Sylvain Thery's avatar
Sylvain Thery committed
308
{
309
310
//	TraversorV<typename PFP::MAP> t(map, thread) ;
//	for(Dart d = t.begin(); d != t.end(); d = t.next())
311
312
313
314
315
316
//	foreachCellMT(VERTEX,d,typename PFP::MAP,map ,thread)
	foreach_cell<VERTEX>(map, [&] (Vertex v)
	{
		vertex_centroid[v] = Volume::Geometry::vertexNeighborhoodCentroid<PFP,V_ATT>(map, v, position) ;
	}
	,false,thread);
Sylvain Thery's avatar
Sylvain Thery committed
317
318
319
}


320
321
namespace Parallel
{
Pierre Kraemer's avatar
Pierre Kraemer committed
322

323
template <typename PFP, typename V_ATT, typename W_ATT>
324
325
class FunctorComputeCentroidVolumes: public FunctorMapThreaded<typename PFP::MAP >
{
326
327
	 const V_ATT& m_position;
	 W_ATT& m_vol_centroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
328

329
public:
330
	 FunctorComputeCentroidVolumes<PFP,V_ATT,W_ATT>( typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid):
331
332
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vol_centroid(vol_centroid)
	 { }
333

334
335
	void run(Dart d, unsigned int threadID)
	{
336
		m_vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP,V_ATT,W_ATT>(this->m_map, d, m_position,threadID) ;
337
338
	}
};
339

340
341
template <typename PFP, typename V_ATT, typename W_ATT>
void computeCentroidVolumes(typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid,	unsigned int nbth)
342
{
343
	FunctorComputeCentroidVolumes<PFP,V_ATT,W_ATT> funct(map,position,vol_centroid);
344
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
Lionel Untereiner's avatar
Lionel Untereiner committed
345
}
346

347
348
template <typename PFP, typename V_ATT, typename W_ATT>
class FunctorComputeCentroidELWVolumes: public FunctorMapThreaded<typename PFP::MAP >
349
{
350
351
352
	 const V_ATT& m_position;
	 W_ATT& m_vol_centroid;
//	 VolumeAttribute<typename PFP::VEC3>& m_vol_centroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
353

354
public:
355
	 FunctorComputeCentroidELWVolumes<PFP,V_ATT,W_ATT>( typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid):
356
357
358
359
360
		 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vol_centroid(vol_centroid)
	 { }

	void run(Dart d, unsigned int threadID)
	{
361
		m_vol_centroid[d] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(this->m_map, d, m_position, threadID) ;
362
363
364
	}
};

365
366
367
template <typename PFP, typename V_ATT, typename W_ATT>
void computeCentroidELWVolumes(typename PFP::MAP& map,
		const V_ATT& position, W_ATT& vol_centroid,
Thery Sylvain's avatar
Thery Sylvain committed
368
		unsigned int nbth)
369
{
370
	FunctorComputeCentroidELWVolumes<PFP,V_ATT,W_ATT> funct(map,position,vol_centroid);
371
372
373
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
}

374
template <typename PFP, typename V_ATT>
375
376
class FunctorComputeNeighborhoodCentroidVertices: public FunctorMapThreaded<typename PFP::MAP >
{
377
378
	 const V_ATT& m_position;
	 V_ATT& m_vcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
379

380
public:
381
	 FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT>( typename PFP::MAP& map, const V_ATT& position, V_ATT& vcentroid):
382
383
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vcentroid(vcentroid)
	 { }
384

385
	void run(Dart d, unsigned int /*threadID*/)
386
	{
387
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP,V_ATT>(this->m_map, d, m_position) ;
388
389
	}
};
390

391
392
393
template <typename PFP, typename V_ATT>
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map, const V_ATT& position, V_ATT& vertex_centroid,
		unsigned int nbth)
394
{
395
396
	FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT> funct(map,position,vertex_centroid);
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false);
397
}
398

untereiner's avatar
untereiner committed
399
} // namespace Parallel
Pierre Kraemer's avatar
Pierre Kraemer committed
400

401

Pierre Kraemer's avatar
Pierre Kraemer committed
402
} // namespace Geometry
403

Pierre Kraemer's avatar
Pierre Kraemer committed
404
} // namespace Volume
405

Pierre Kraemer's avatar
Pierre Kraemer committed
406
407
408
} // namespace Algo

} // namespace CGoGN