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

25
26
#include "Topology/generic/traversorCell.h"
#include "Topology/generic/traversor2.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
27
#include "Topology/generic/cellmarker.h"
28
29
#include "Topology/generic/traversorCell.h"
#include "Topology/generic/traversor3.h"
30
#include "Algo/Parallel/parallel_foreach.h"
31

Pierre Kraemer's avatar
Pierre Kraemer committed
32
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
46
template <typename PFP, typename V_ATT>
typename V_ATT::DATA_TYPE volumeCentroid(typename PFP::MAP& map, Dart 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
	Traversor3WV<typename PFP::MAP> tra(map,d,false,thread);
51
	for (Dart d = tra.begin(); d != tra.end(); d = tra.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
52
	{
53
		center += attributs[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
54
55
56
57
58
59
60
		++count;
	}

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

61
62
63

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

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
	double count=0.0;
	Traversor3WE<typename PFP::MAP> t(map, d,false,thread) ;
	for(Dart it = t.begin(); it != t.end();it = t.next())
	{
		EMB e1 = attributs[it];
		EMB e2 = attributs[map.phi1(it)];
		double l = (e2-e1).norm();
		center += (e1+e2)*l;
		count += 2.0*l ;
	}
	center /= double(count);	
	return center ;
}


83
84
template <typename PFP, typename V_ATT>
typename V_ATT::DATA_TYPE faceCentroid(typename PFP::MAP& map, Dart d, 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) ;
	for(Dart it = t.begin(); it != t.end(); it = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
90
91
92
	{
		center += attributs[it];
		++count ;
93
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
94
95
96
97
	center /= double(count);
	return center ;
}

98

99
100
101

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

105
	EMB center(0.0);
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
	double count=0.0;
	Traversor2FE<typename PFP::MAP> t(map, d) ;
	for(Dart it = t.begin(); it != t.end(); it = t.next())
	{
		EMB e1 = attributs[it];
		EMB e2 = attributs[map.phi1(it)];
		double l = (e2-e1).norm();
		center += (e1+e2)*l;
		count += 2.0*l ;
	}
	center /= double(count);
	return center ;
}


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

Pierre Kraemer's avatar
Pierre Kraemer committed
126
	unsigned int count = 0 ;
127
128
	Traversor2VVaE<typename PFP::MAP> t(map, d) ;
	for(Dart it = t.begin(); it != t.end(); it = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
129
	{
130
		center += attributs[it];
Pierre Kraemer's avatar
Pierre Kraemer committed
131
		++count ;
132
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
133
134
135
136
137
	center /= count ;
	return center ;
}


138
139
140
141
142



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
143
{
144
	TraversorF<typename PFP::MAP> t(map,thread) ;
145
	for(Dart d = t.begin(); d != t.end(); d = t.next())
146
		face_centroid[d] = faceCentroid<PFP,V_ATT>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
147
148
}

149
150
151
152


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)
153
{
154
	TraversorF<typename PFP::MAP> t(map,thread) ;
155
	for(Dart d = t.begin(); d != t.end(); d = t.next())
156
		face_centroid[d] = faceCentroidELW<PFP,V_ATT>(map, d, position) ;
157
158
}

159
160
161
162


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
163
{
164
	TraversorV<typename PFP::MAP> t(map, thread) ;
165
	for(Dart d = t.begin(); d != t.end(); d = t.next())
166
		vertex_centroid[d] = vertexNeighborhoodCentroid<PFP,V_ATT>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
}

169
170
171
172

namespace Parallel
{

173
template <typename PFP, typename V_ATT, typename F_ATT>
174
175
class FunctorComputeCentroidFaces: public FunctorMapThreaded<typename PFP::MAP >
{
176
177
	 const V_ATT& m_position;
	 F_ATT& m_fcentroid;
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>
190
191
class FunctorComputeCentroidELWFaces: public FunctorMapThreaded<typename PFP::MAP >
{
192
193
	const V_ATT& m_position;
	F_ATT& m_fcentroid;
194
public:
195
	 FunctorComputeCentroidELWFaces<PFP,V_ATT,F_ATT>( typename PFP::MAP& map, const V_ATT& position, F_ATT& fcentroid):
196
197
198
199
200
201
202
203
204
	 	 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) ;
	}
};

205
206
207
208
209
210
211
212
213
template <typename PFP, typename V_ATT>
class FunctorComputeNeighborhoodCentroidVertices: public FunctorMapThreaded<typename PFP::MAP >
{
	 const V_ATT& m_position;
	 V_ATT& m_vcentroid;
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)
	 { }
214

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

221
222
223

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

230
231
template <typename PFP, typename V_ATT, typename F_ATT>
void computeCentroidELWFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid,
232
		unsigned int nbth, unsigned int current_thread)
233
{
234
	FunctorComputeCentroidELWFaces<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


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

}
250
251
} // namespace Geometry
} // namespace Surface
252

253
254
255
namespace Volume
{
namespace Geometry
Lionel Untereiner's avatar
Lionel Untereiner committed
256
{
257

258
259
template <typename PFP, typename V_ATT>
typename V_ATT::DATA_TYPE vertexNeighborhoodCentroid(typename PFP::MAP& map, Dart d, const V_ATT& attributs)
Lionel Untereiner's avatar
Lionel Untereiner committed
260
{
261
	typename V_ATT::DATA_TYPE  center(0.0);
Lionel Untereiner's avatar
Lionel Untereiner committed
262
263
264
265
266
267
268
269
270
271
	unsigned int count = 0 ;
	Traversor3VVaE<typename PFP::MAP> t(map, d) ;
	for(Dart it = t.begin(); it != t.end(); it = t.next())
	{
		center += attributs[it];
		++count ;
	}
	center /= count ;
	return center ;
}
272

273
274
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
275
{
276
	TraversorW<typename PFP::MAP> t(map, thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
277
	for(Dart d = t.begin(); d != t.end(); d = t.next())
278
		vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP,V_ATT>(map, d, position,thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
279
}
280

281
282
283
284


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)
285
{
286
	TraversorW<typename PFP::MAP> t(map,thread) ;
287
	for(Dart d = t.begin(); d != t.end(); d = t.next())
288
		vol_centroid[d] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(map, d, position,thread) ;
289
290
291
}


292
293
294

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
295
296
297
{
	TraversorV<typename PFP::MAP> t(map, thread) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
298
		vertex_centroid[d] = Volume::Geometry::vertexNeighborhoodCentroid<PFP,V_ATT>(map, d, position) ;
Sylvain Thery's avatar
Sylvain Thery committed
299
300
301
}


302

303
304
namespace Parallel
{
305
template <typename PFP, typename V_ATT, typename W_ATT>
306
307
class FunctorComputeCentroidVolumes: public FunctorMapThreaded<typename PFP::MAP >
{
308
309
	 const V_ATT& m_position;
	 W_ATT& m_vol_centroid;
310
public:
311
	 FunctorComputeCentroidVolumes<PFP,V_ATT,W_ATT>( typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid):
312
313
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vol_centroid(vol_centroid)
	 { }
314

315
316
	void run(Dart d, unsigned int threadID)
	{
317
		m_vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP,V_ATT,W_ATT>(this->m_map, d, m_position,threadID) ;
318
319
	}
};
320

321

322
323
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)
324
{
325
	FunctorComputeCentroidVolumes<PFP,V_ATT,W_ATT> funct(map,position,vol_centroid);
326
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
Lionel Untereiner's avatar
Lionel Untereiner committed
327
}
328
329


330
331


332
333
334
335


template <typename PFP, typename V_ATT, typename W_ATT>
class FunctorComputeCentroidELWVolumes: public FunctorMapThreaded<typename PFP::MAP >
336
{
337
338
339
	 const V_ATT& m_position;
	 W_ATT& m_vol_centroid;
//	 VolumeAttribute<typename PFP::VEC3>& m_vol_centroid;
340
public:
341
	 FunctorComputeCentroidELWVolumes<PFP,V_ATT,W_ATT>( typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid):
342
343
344
345
346
		 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vol_centroid(vol_centroid)
	 { }

	void run(Dart d, unsigned int threadID)
	{
347
		m_vol_centroid[d] = Surface::Geometry::volumeCentroidELW<PFP,V_ATT>(this->m_map, d, m_position, threadID) ;
348
349
350
351
	}
};


352

353
354
355
template <typename PFP, typename V_ATT, typename W_ATT>
void computeCentroidELWVolumes(typename PFP::MAP& map,
		const V_ATT& position, W_ATT& vol_centroid,
356
357
		unsigned int nbth = 0)
{
358
	FunctorComputeCentroidELWVolumes<PFP,V_ATT,W_ATT> funct(map,position,vol_centroid);
359
360
361
362
363
364
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
}




365
template <typename PFP, typename V_ATT>
366
367
class FunctorComputeNeighborhoodCentroidVertices: public FunctorMapThreaded<typename PFP::MAP >
{
368
369
	 const V_ATT& m_position;
	 V_ATT& m_vcentroid;
370
public:
371
	 FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT>( typename PFP::MAP& map, const V_ATT& position, V_ATT& vcentroid):
372
373
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vcentroid(vcentroid)
	 { }
374

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

381
382
383
384

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

Pierre Kraemer's avatar
Pierre Kraemer committed
390

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

untereiner's avatar
untereiner committed
394
} // namespace Volume
395
396
397



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

} // namespace CGoGN