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

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

180
181
182
183

namespace Parallel
{

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

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

201
202
}

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

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



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

219
} // namespace Surface
220

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

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

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

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

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

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

267
268
269
270
	foreach_cell<VOLUME>(map, [&] (Vol v)
	{
		vol_centroid[v] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(map, v, position,thread) ;
	}
271
	,AUTO,thread);
272
273
}

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

283
284
285
286
	foreach_cell<VERTEX>(map, [&] (Vertex v)
	{
		vertex_centroid[v] = Volume::Geometry::vertexNeighborhoodCentroid<PFP,V_ATT>(map, v, position) ;
	}
287
	,AUTO,thread);
Sylvain Thery's avatar
Sylvain Thery committed
288
289
290
}


291
292
namespace Parallel
{
Pierre Kraemer's avatar
Pierre Kraemer committed
293

294
template <typename PFP, typename V_ATT, typename W_ATT>
Sylvain Thery's avatar
Sylvain Thery committed
295
void computeCentroidVolumes(typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid)
296
{
Sylvain Thery's avatar
Sylvain Thery committed
297
	CGoGN::Parallel::foreach_cell<VOLUME>(map,[&](Vol v, unsigned int thr)
298
	{
Sylvain Thery's avatar
Sylvain Thery committed
299
300
		vol_centroid[v] = Surface::Geometry::volumeCentroid<PFP,V_ATT>(map, v, position, thr) ;
	},true,AUTO);
Sylvain Thery's avatar
Sylvain Thery committed
301
}
302

303
template <typename PFP, typename V_ATT, typename W_ATT>
Sylvain Thery's avatar
Sylvain Thery committed
304
void computeCentroidELWVolumes(typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid)
305
{
Sylvain Thery's avatar
Sylvain Thery committed
306
307
308
	CGoGN::Parallel::foreach_cell<VOLUME>(map,[&](Vol v, unsigned int thr)
	{
		vol_centroid[v] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(map, v, position, thr) ;
Sylvain Thery's avatar
Sylvain Thery committed
309
	},true,AUTO);
310
311
}

312

313
template <typename PFP, typename V_ATT>
Sylvain Thery's avatar
Sylvain Thery committed
314
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map, const V_ATT& position, V_ATT& vertex_centroid)
315
{
Sylvain Thery's avatar
Sylvain Thery committed
316
317
318
	CGoGN::Parallel::foreach_cell<VERTEX>(map,[&](Vertex v, unsigned int thr)
	{
		vertex_centroid[v] = Volume::Geometry::vertexNeighborhoodCentroid<PFP,V_ATT>(map, v, position,thr) ;
Sylvain Thery's avatar
Sylvain Thery committed
319
	},true,FORCE_CELL_MARKING);
320
}
321

untereiner's avatar
untereiner committed
322
} // namespace Parallel
Pierre Kraemer's avatar
Pierre Kraemer committed
323

324

Pierre Kraemer's avatar
Pierre Kraemer committed
325
} // namespace Geometry
326

Pierre Kraemer's avatar
Pierre Kraemer committed
327
} // namespace Volume
328

Pierre Kraemer's avatar
Pierre Kraemer committed
329
330
331
} // namespace Algo

} // namespace CGoGN