centroid.hpp 9.67 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
	foreach_incident3<VERTEX>(map,d, [&] (Vertex v)
Pierre Kraemer's avatar
Pierre Kraemer committed
51
	{
52
		center += attributs[v];
Pierre Kraemer's avatar
Pierre Kraemer committed
53
54
		++count;
	}
55
	,false,thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
56
57
58
59
60

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

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

67
	double count=0.0;
68

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

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

87
	foreach_incident2<VERTEX>(map, f, [&](Vertex it)
Pierre Kraemer's avatar
Pierre Kraemer committed
88
89
90
	{
		center += attributs[it];
		++count ;
91
	});
Pierre Kraemer's avatar
Pierre Kraemer committed
92
93
94
95
	center /= double(count);
	return center ;
}

96
template <typename PFP, typename V_ATT>
97
typename V_ATT::DATA_TYPE faceCentroidELW(typename PFP::MAP& map, Face f, const V_ATT& attributs)
98
{
99
100
	typedef typename V_ATT::DATA_TYPE EMB;

101
	EMB center(0.0);
102
	double count=0.0;
103

104
	foreach_incident2<EDGE>(map, f, [&](Edge it)
105
	{
Sylvain Thery's avatar
Sylvain Thery committed
106
		EMB e1 = attributs[it.dart];
107
108
109
110
		EMB e2 = attributs[map.phi1(it)];
		double l = (e2-e1).norm();
		center += (e1+e2)*l;
		count += 2.0*l ;
111
	});
112
113
114
115
	center /= double(count);
	return center ;
}

116
template <typename PFP, typename V_ATT>
117
typename V_ATT::DATA_TYPE vertexNeighborhoodCentroid(typename PFP::MAP& map, Vertex v, const V_ATT& attributs)
Pierre Kraemer's avatar
Pierre Kraemer committed
118
{
119
120
	typename V_ATT::DATA_TYPE center(0.0);

Pierre Kraemer's avatar
Pierre Kraemer committed
121
	unsigned int count = 0 ;
122
//	Traversor2VVaE<typename PFP::MAP> t(map, d) ;
123
	foreach_adjacent2<EDGE>(map, v, [&](Vertex it)
Pierre Kraemer's avatar
Pierre Kraemer committed
124
	{
125
		center += attributs[it];
Pierre Kraemer's avatar
Pierre Kraemer committed
126
		++count ;
127
	});
Pierre Kraemer's avatar
Pierre Kraemer committed
128
129
130
131
	center /= count ;
	return center ;
}

132
133
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
134
{
Sylvain Thery's avatar
Sylvain Thery committed
135
136
137
138
139
140
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread==0))
	{
		Parallel::computeCentroidFaces<PFP,V_ATT,F_ATT>(map,position,face_centroid);
		return;
	}

141
142
143
144
	foreach_cell<FACE>(map, [&] (Face f)
	{
		face_centroid[f] = faceCentroid<PFP,V_ATT>(map, f, position) ;
	}
145
	,AUTO,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
{
Sylvain Thery's avatar
Sylvain Thery committed
151
152
153
154
155
156
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread==0))
	{
		Parallel::computeCentroidELWFaces<PFP,V_ATT,F_ATT>(map,position,face_centroid);
		return;
	}

157
158
159
160
	foreach_cell<FACE>(map, [&] (Face f)
	{
		face_centroid[f] = faceCentroidELW<PFP,V_ATT>(map, f, position) ;
	}
161
	,AUTO,thread);
162
163
}

164
165
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
166
{
Pierre Kraemer's avatar
Pierre Kraemer committed
167
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread == 0))
Sylvain Thery's avatar
Sylvain Thery committed
168
169
170
171
172
	{
		Parallel::computeNeighborhoodCentroidVertices<PFP,V_ATT>(map,position,vertex_centroid);
		return;
	}

173
174
175
	foreach_cell<VERTEX>(map, [&] (Vertex v)
	{
		vertex_centroid[v] = vertexNeighborhoodCentroid<PFP,V_ATT>(map, v, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
176
	}, AUTO, thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
177
178
}

179
180
181
182

namespace Parallel
{

183
template <typename PFP, typename V_ATT, typename F_ATT>
Sylvain Thery's avatar
Sylvain Thery committed
184
void computeCentroidFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid)
185
{
Pierre Kraemer's avatar
Pierre Kraemer committed
186
	CGoGN::Parallel::foreach_cell<FACE>(map,[&](Face f, unsigned int /*thr*/)
Sylvain Thery's avatar
Sylvain Thery committed
187
188
	{
		face_centroid[f] = faceCentroid<PFP>(map, f, position) ;
Sylvain Thery's avatar
Sylvain Thery committed
189
	});
190
191
}

192
template <typename PFP, typename V_ATT, typename F_ATT>
Sylvain Thery's avatar
Sylvain Thery committed
193
void computeCentroidELWFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid)
194
{
Pierre Kraemer's avatar
Pierre Kraemer committed
195
	CGoGN::Parallel::foreach_cell<FACE>(map,[&](Face f, unsigned int /*thr*/)
Sylvain Thery's avatar
Sylvain Thery committed
196
197
	{
		face_centroid[f] = faceCentroidELW<PFP>(map, f, position) ;
Sylvain Thery's avatar
Sylvain Thery committed
198
	});
Sylvain Thery's avatar
Sylvain Thery committed
199

200
201
}

202
template <typename PFP, typename V_ATT>
203
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map,
Sylvain Thery's avatar
Sylvain Thery committed
204
		const V_ATT& position, V_ATT& vertex_centroid)
205
{
Pierre Kraemer's avatar
Pierre Kraemer committed
206
	CGoGN::Parallel::foreach_cell<VERTEX>(map,[&](Vertex v, unsigned int /*thr*/)
Sylvain Thery's avatar
Sylvain Thery committed
207
208
	{
		vertex_centroid[v] = vertexNeighborhoodCentroid<PFP>(map, v, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
209
	}, FORCE_CELL_MARKING);
210
211
}

Pierre Kraemer's avatar
Pierre Kraemer committed
212
213
214
215
} // namespace Parallel



216
} // namespace Geometry
Pierre Kraemer's avatar
Pierre Kraemer committed
217

218
} // namespace Surface
219

220
221
namespace Volume
{
Pierre Kraemer's avatar
Pierre Kraemer committed
222

223
namespace Geometry
Lionel Untereiner's avatar
Lionel Untereiner committed
224
{
225

226
template <typename PFP, typename V_ATT>
227
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
228
{
229
	typename V_ATT::DATA_TYPE  center(0.0);
Lionel Untereiner's avatar
Lionel Untereiner committed
230
	unsigned int count = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
231
	foreach_adjacent3<EDGE>(map, v, [&] (Vertex it)
Lionel Untereiner's avatar
Lionel Untereiner committed
232
233
234
	{
		center += attributs[it];
		++count ;
Pierre Kraemer's avatar
Pierre Kraemer committed
235
	}, false, thread);
Lionel Untereiner's avatar
Lionel Untereiner committed
236
237
238
	center /= count ;
	return center ;
}
239

240
241
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
242
{
Pierre Kraemer's avatar
Pierre Kraemer committed
243
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread == 0))
Sylvain Thery's avatar
Sylvain Thery committed
244
245
246
247
248
	{
		Parallel::computeCentroidVolumes<PFP,V_ATT,W_ATT>(map,position,vol_centroid);
		return;
	}

249
250
251
	foreach_cell<VOLUME>(map, [&] (Vol v)
	{
		vol_centroid[v] = Surface::Geometry::volumeCentroid<PFP,V_ATT>(map, v, position,thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
252
	}, AUTO, thread);
Lionel Untereiner's avatar
Lionel Untereiner committed
253
}
254

255
256
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)
257
{
Pierre Kraemer's avatar
Pierre Kraemer committed
258
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread == 0))
Sylvain Thery's avatar
Sylvain Thery committed
259
260
261
262
263
	{
		Parallel::computeCentroidELWVolumes<PFP,V_ATT,W_ATT>(map,position,vol_centroid);
		return;
	}

264
265
266
	foreach_cell<VOLUME>(map, [&] (Vol v)
	{
		vol_centroid[v] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(map, v, position,thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
267
	}, AUTO, thread);
268
269
}

270
271
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
272
{
Pierre Kraemer's avatar
Pierre Kraemer committed
273
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread == 0))
Sylvain Thery's avatar
Sylvain Thery committed
274
275
276
277
278
	{
		Parallel::computeNeighborhoodCentroidVertices<PFP,V_ATT>(map,position,vertex_centroid);
		return;
	}

279
280
281
	foreach_cell<VERTEX>(map, [&] (Vertex v)
	{
		vertex_centroid[v] = Volume::Geometry::vertexNeighborhoodCentroid<PFP,V_ATT>(map, v, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
282
	}, AUTO, thread);
Sylvain Thery's avatar
Sylvain Thery committed
283
284
285
}


286
287
namespace Parallel
{
Pierre Kraemer's avatar
Pierre Kraemer committed
288

289
template <typename PFP, typename V_ATT, typename W_ATT>
Sylvain Thery's avatar
Sylvain Thery committed
290
void computeCentroidVolumes(typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid)
291
{
Pierre Kraemer's avatar
Pierre Kraemer committed
292
	CGoGN::Parallel::foreach_cell<VOLUME>(map, [&] (Vol v, unsigned int thr)
293
	{
Sylvain Thery's avatar
Sylvain Thery committed
294
		vol_centroid[v] = Surface::Geometry::volumeCentroid<PFP,V_ATT>(map, v, position, thr) ;
Sylvain Thery's avatar
Sylvain Thery committed
295
	});
Sylvain Thery's avatar
Sylvain Thery committed
296
}
297

298
template <typename PFP, typename V_ATT, typename W_ATT>
Sylvain Thery's avatar
Sylvain Thery committed
299
void computeCentroidELWVolumes(typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid)
300
{
Pierre Kraemer's avatar
Pierre Kraemer committed
301
	CGoGN::Parallel::foreach_cell<VOLUME>(map, [&] (Vol v, unsigned int thr)
Sylvain Thery's avatar
Sylvain Thery committed
302
303
	{
		vol_centroid[v] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(map, v, position, thr) ;
Sylvain Thery's avatar
Sylvain Thery committed
304
	});
305
306
}

307
template <typename PFP, typename V_ATT>
Sylvain Thery's avatar
Sylvain Thery committed
308
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map, const V_ATT& position, V_ATT& vertex_centroid)
309
{
Pierre Kraemer's avatar
Pierre Kraemer committed
310
	CGoGN::Parallel::foreach_cell<VERTEX>(map, [&] (Vertex v, unsigned int thr)
Sylvain Thery's avatar
Sylvain Thery committed
311
312
	{
		vertex_centroid[v] = Volume::Geometry::vertexNeighborhoodCentroid<PFP,V_ATT>(map, v, position,thr) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
313
	}, FORCE_CELL_MARKING);
314
}
315

untereiner's avatar
untereiner committed
316
} // namespace Parallel
Pierre Kraemer's avatar
Pierre Kraemer committed
317

318

Pierre Kraemer's avatar
Pierre Kraemer committed
319
} // namespace Geometry
320

Pierre Kraemer's avatar
Pierre Kraemer committed
321
} // namespace Volume
322

Pierre Kraemer's avatar
Pierre Kraemer committed
323
324
325
} // namespace Algo

} // namespace CGoGN