centroid.hpp 13.7 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"
Sylvain Thery's avatar
Sylvain Thery committed
31
#include "Topology/generic/cells_macros.h"
32

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

namespace Algo
{

39
40
41
namespace Surface
{

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

45
template <typename PFP, typename V_ATT>
46
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
47
{
48
	typename V_ATT::DATA_TYPE center(0.0);
49
	unsigned int count = 0 ;
50
51
52
53
54
55
56
57
//	Traversor3WV<typename PFP::MAP> tra(map,d,false,thread);
//	for (Dart d = tra.begin(); d != tra.end(); d = tra.next())
//	{
//		center += attributs[d];
//		++count;
//	}

	foreachIncident3MT(VOLUME,d,VERTEX,e,typename PFP::MAP,map,thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
58
	{
59
		center += attributs[e];
Pierre Kraemer's avatar
Pierre Kraemer committed
60
61
62
63
64
65
66
		++count;
	}

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

67
template <typename PFP, typename V_ATT>
68
typename V_ATT::DATA_TYPE volumeCentroidELW(typename PFP::MAP& map, Vol d, const V_ATT& attributs, unsigned int thread)
69
{
70
	typedef typename V_ATT::DATA_TYPE EMB;
71
	EMB center(0.0);
72

73
	double count=0.0;
74
75
76
//	Traversor3WE<typename PFP::MAP> t(map, d,false,thread) ;
//	for(Dart it = t.begin(); it != t.end();it = t.next())

77
78
//	foreachIncident3MT(VOLUME,d,EDGE,it,typename PFP::MAP,map,thread)
	foreach_incident3<EDGE>(map,d, [&] (Edge it)
79
	{
Sylvain Thery's avatar
Sylvain Thery committed
80
		EMB e1 = attributs[it.dart];
81
82
83
84
		EMB e2 = attributs[map.phi1(it)];
		double l = (e2-e1).norm();
		center += (e1+e2)*l;
		count += 2.0*l ;
85
	},false,thread);
86
87
88
89
	center /= double(count);	
	return center ;
}

90
template <typename PFP, typename V_ATT>
91
typename V_ATT::DATA_TYPE faceCentroid(typename PFP::MAP& map, Face f, const V_ATT& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
92
{
93
	typename V_ATT::DATA_TYPE center(0.0);
Pierre Kraemer's avatar
Pierre Kraemer committed
94
	unsigned int count = 0 ;
95
96
97

//	Traversor2FV<typename PFP::MAP> t(map, d) ;
//	for(Dart it = t.begin(); it != t.end(); it = t.next())
98
99
100
//	foreachIncident2(FACE,d,VERTEX,it,typename PFP::MAP,map)

	foreach_incident2<VERTEX>(map, f, [&](Vertex it)
Pierre Kraemer's avatar
Pierre Kraemer committed
101
102
103
	{
		center += attributs[it];
		++count ;
104
	});
Pierre Kraemer's avatar
Pierre Kraemer committed
105
106
107
108
	center /= double(count);
	return center ;
}

109
template <typename PFP, typename V_ATT>
110
typename V_ATT::DATA_TYPE faceCentroidELW(typename PFP::MAP& map, Face d, const V_ATT& attributs)
111
{
112
113
	typedef typename V_ATT::DATA_TYPE EMB;

114
	EMB center(0.0);
115
	double count=0.0;
116
117
118
119

//	Traversor2FE<typename PFP::MAP> t(map, d) ;
//	for(Dart it = t.begin(); it != t.end(); it = t.next())
	foreachIncident2(FACE,d,EDGE,it,typename PFP::MAP,map)
120
	{
Sylvain Thery's avatar
Sylvain Thery committed
121
		EMB e1 = attributs[it.dart];
122
123
124
125
126
127
128
129
130
		EMB e2 = attributs[map.phi1(it)];
		double l = (e2-e1).norm();
		center += (e1+e2)*l;
		count += 2.0*l ;
	}
	center /= double(count);
	return center ;
}

131
template <typename PFP, typename V_ATT>
132
typename V_ATT::DATA_TYPE vertexNeighborhoodCentroid(typename PFP::MAP& map, Vertex d, const V_ATT& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
133
{
134
135
	typename V_ATT::DATA_TYPE center(0.0);

Pierre Kraemer's avatar
Pierre Kraemer committed
136
	unsigned int count = 0 ;
137
138
139
//	Traversor2VVaE<typename PFP::MAP> t(map, d) ;
//	for(Dart it = t.begin(); it != t.end(); it = t.next())
	foreachAdjacent2(VERTEX,EDGE,d,it,typename PFP::MAP,map)
Pierre Kraemer's avatar
Pierre Kraemer committed
140
	{
141
		center += attributs[it];
Pierre Kraemer's avatar
Pierre Kraemer committed
142
		++count ;
143
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
144
145
146
147
	center /= count ;
	return center ;
}

148
149
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
150
{
151
152
153
//	TraversorF<typename PFP::MAP> t(map,thread) ;
//	for(Dart d = t.begin(); d != t.end(); d = t.next())
	foreachCellMT(FACE,d,typename PFP::MAP,map ,thread)
154
		face_centroid[d] = faceCentroid<PFP,V_ATT>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
155
156
}

157
158
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)
159
{
160
161
162
//	TraversorF<typename PFP::MAP> t(map,thread) ;
//	for(Dart d = t.begin(); d != t.end(); d = t.next())
	foreachCellMT(FACE,d,typename PFP::MAP,map ,thread)
163
		face_centroid[d] = faceCentroidELW<PFP,V_ATT>(map, d, position) ;
164
165
}

166
167
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
168
{
169
170
171
//	TraversorV<typename PFP::MAP> t(map, thread) ;
//	for(Dart d = t.begin(); d != t.end(); d = t.next())
	foreachCellMT(VERTEX,d,typename PFP::MAP,map ,thread)
172
		vertex_centroid[d] = vertexNeighborhoodCentroid<PFP,V_ATT>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
173
174
}

175
176
177
178

namespace Parallel
{

179
template <typename PFP, typename V_ATT, typename F_ATT>
Pierre Kraemer's avatar
Pierre Kraemer committed
180
class FunctorComputeCentroidFaces: public FunctorMapThreaded<typename PFP::MAP>
181
{
182
183
	 const V_ATT& m_position;
	 F_ATT& m_fcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
184

185
public:
186
	 FunctorComputeCentroidFaces<PFP,V_ATT,F_ATT>( typename PFP::MAP& map, const V_ATT& position, F_ATT& fcentroid):
187
188
189
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_fcentroid(fcentroid)
	 { }

190
	void run(Dart d, unsigned int /*threadID*/)
191
192
193
194
195
	{
		m_fcentroid[d] = faceCentroid<PFP>(this->m_map, d, m_position) ;
	}
};

196
template <typename PFP, typename V_ATT, typename F_ATT>
Pierre Kraemer's avatar
Pierre Kraemer committed
197
class FunctorComputeCentroidELWFaces: public FunctorMapThreaded<typename PFP::MAP>
198
{
199
200
	const V_ATT& m_position;
	F_ATT& m_fcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
201

202
public:
203
	 FunctorComputeCentroidELWFaces<PFP,V_ATT,F_ATT>( typename PFP::MAP& map, const V_ATT& position, F_ATT& fcentroid):
204
205
206
207
208
209
210
211
212
	 	 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) ;
	}
};

213
214
215
216
217
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
218

219
220
221
222
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)
	 { }
223

224
225
226
227
228
	void run(Dart d, unsigned int /*threadID*/)
	{
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP>(this->m_map, d, m_position) ;
	}
};
229

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

238
239
template <typename PFP, typename V_ATT, typename F_ATT>
void computeCentroidELWFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid,
240
		unsigned int nbth, unsigned int current_thread)
241
{
242
	FunctorComputeCentroidELWFaces<PFP,V_ATT,F_ATT> funct(map,position,face_centroid);
243
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, current_thread);
244
245
}

246
template <typename PFP, typename V_ATT>
247
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map,
248
		const V_ATT& position, V_ATT& vertex_centroid,
249
		unsigned int nbth, unsigned int current_thread)
250
{
251
252
	FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT> funct(map,position,vertex_centroid);
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false);
253
254
}

Pierre Kraemer's avatar
Pierre Kraemer committed
255
256
257
258
} // namespace Parallel



259
} // namespace Geometry
Pierre Kraemer's avatar
Pierre Kraemer committed
260

261
} // namespace Surface
262

263
264
namespace Volume
{
Pierre Kraemer's avatar
Pierre Kraemer committed
265

266
namespace Geometry
Lionel Untereiner's avatar
Lionel Untereiner committed
267
{
268

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

285
286
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
287
{
288
289
290
//	TraversorW<typename PFP::MAP> t(map, thread) ;
//	for(Dart d = t.begin(); d != t.end(); d = t.next())
	foreachCellMT(VOLUME,d,typename PFP::MAP,map ,thread)
291
		vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP,V_ATT>(map, d, position,thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
292
}
293

294
295
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)
296
{
297
298
299
//	TraversorW<typename PFP::MAP> t(map,thread) ;
//	for(Dart d = t.begin(); d != t.end(); d = t.next())
	foreachCellMT(VOLUME,d,typename PFP::MAP,map ,thread)
300
		vol_centroid[d] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(map, d, position,thread) ;
301
302
}

303
304
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
305
{
306
307
308
//	TraversorV<typename PFP::MAP> t(map, thread) ;
//	for(Dart d = t.begin(); d != t.end(); d = t.next())
	foreachCellMT(VERTEX,d,typename PFP::MAP,map ,thread)
309
		vertex_centroid[d] = Volume::Geometry::vertexNeighborhoodCentroid<PFP,V_ATT>(map, d, position) ;
Sylvain Thery's avatar
Sylvain Thery committed
310
311
312
}


313
314
namespace Parallel
{
Pierre Kraemer's avatar
Pierre Kraemer committed
315

316
template <typename PFP, typename V_ATT, typename W_ATT>
317
318
class FunctorComputeCentroidVolumes: public FunctorMapThreaded<typename PFP::MAP >
{
319
320
	 const V_ATT& m_position;
	 W_ATT& m_vol_centroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
321

322
public:
323
	 FunctorComputeCentroidVolumes<PFP,V_ATT,W_ATT>( typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid):
324
325
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vol_centroid(vol_centroid)
	 { }
326

327
328
	void run(Dart d, unsigned int threadID)
	{
329
		m_vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP,V_ATT,W_ATT>(this->m_map, d, m_position,threadID) ;
330
331
	}
};
332

333
334
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)
335
{
336
	FunctorComputeCentroidVolumes<PFP,V_ATT,W_ATT> funct(map,position,vol_centroid);
337
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
Lionel Untereiner's avatar
Lionel Untereiner committed
338
}
339

340
341
template <typename PFP, typename V_ATT, typename W_ATT>
class FunctorComputeCentroidELWVolumes: public FunctorMapThreaded<typename PFP::MAP >
342
{
343
344
345
	 const V_ATT& m_position;
	 W_ATT& m_vol_centroid;
//	 VolumeAttribute<typename PFP::VEC3>& m_vol_centroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
346

347
public:
348
	 FunctorComputeCentroidELWVolumes<PFP,V_ATT,W_ATT>( typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid):
349
350
351
352
353
		 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vol_centroid(vol_centroid)
	 { }

	void run(Dart d, unsigned int threadID)
	{
354
		m_vol_centroid[d] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(this->m_map, d, m_position, threadID) ;
355
356
357
	}
};

358
359
360
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
361
		unsigned int nbth)
362
{
363
	FunctorComputeCentroidELWVolumes<PFP,V_ATT,W_ATT> funct(map,position,vol_centroid);
364
365
366
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
}

367
template <typename PFP, typename V_ATT>
368
369
class FunctorComputeNeighborhoodCentroidVertices: public FunctorMapThreaded<typename PFP::MAP >
{
370
371
	 const V_ATT& m_position;
	 V_ATT& m_vcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
372

373
public:
374
	 FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT>( typename PFP::MAP& map, const V_ATT& position, V_ATT& vcentroid):
375
376
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vcentroid(vcentroid)
	 { }
377

378
	void run(Dart d, unsigned int /*threadID*/)
379
	{
380
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP,V_ATT>(this->m_map, d, m_position) ;
381
382
	}
};
383

384
385
386
template <typename PFP, typename V_ATT>
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map, const V_ATT& position, V_ATT& vertex_centroid,
		unsigned int nbth)
387
{
388
389
	FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT> funct(map,position,vertex_centroid);
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false);
390
}
391

untereiner's avatar
untereiner committed
392
} // namespace Parallel
Pierre Kraemer's avatar
Pierre Kraemer committed
393

394

Pierre Kraemer's avatar
Pierre Kraemer committed
395
} // namespace Geometry
396

Pierre Kraemer's avatar
Pierre Kraemer committed
397
} // namespace Volume
398

Pierre Kraemer's avatar
Pierre Kraemer committed
399
400
401
} // namespace Algo

} // namespace CGoGN