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
 * 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
45
namespace Geometry
{

template <typename PFP, typename EMBV, typename EMB>
46
EMB volumeCentroidGen(typename PFP::MAP& map, Dart d, const EMBV& attributs, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
47
{
48
49
50
//	EMB center = AttribOps::zero<EMB,PFP>() ;
	EMB center(0.0);

51
	unsigned int count = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
52

53
	Traversor3WV<typename PFP::MAP> tra(map,d,false,thread);
54
	for (Dart d = tra.begin(); d != tra.end(); d = tra.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
55
	{
56
		center += attributs[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
57
58
59
60
61
62
63
		++count;
	}

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

64
65
66
template <typename PFP, typename EMBV, typename EMB>
EMB volumeCentroidELWGen(typename PFP::MAP& map, Dart d, const EMBV& attributs, unsigned int thread)
{
67
68
//	EMB center = AttribOps::zero<EMB,PFP>();
	EMB center(0.0);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
	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 ;
}


Pierre Kraemer's avatar
Pierre Kraemer committed
84
85
86
template <typename PFP, typename EMBV, typename EMB>
EMB faceCentroidGen(typename PFP::MAP& map, Dart d, const EMBV& attributs)
{
87
88
//	EMB center = AttribOps::zero<EMB,PFP>();
	EMB center(0.0);
Pierre Kraemer's avatar
Pierre Kraemer committed
89
	unsigned int count = 0 ;
90
91
	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
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
101
102
103

template <typename PFP, typename EMBV, typename EMB>
EMB faceCentroidELWGen(typename PFP::MAP& map, Dart d, const EMBV& attributs)
{
104
105
//	EMB center = AttribOps::zero<EMB,PFP>();
	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 ;
}


Pierre Kraemer's avatar
Pierre Kraemer committed
121
122
123
template <typename PFP, typename EMBV, typename EMB>
EMB vertexNeighborhoodCentroidGen(typename PFP::MAP& map, Dart d, const EMBV& attributs)
{
124
125
//	EMB center = AttribOps::zero<EMB,PFP>();
	EMB 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
138
	center /= count ;
	return center ;
}


template <typename PFP>
139
void computeCentroidFaces(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, FaceAttribute<typename PFP::VEC3>& face_centroid, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
140
{
141
	TraversorF<typename PFP::MAP> t(map,thread) ;
142
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
143
		face_centroid[d] = faceCentroid<PFP>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
144
145
}

146
template <typename PFP>
147
void computeCentroidELWFaces(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, FaceAttribute<typename PFP::VEC3>& face_centroid, unsigned int thread)
148
{
149
	TraversorF<typename PFP::MAP> t(map,thread) ;
150
151
152
153
	for(Dart d = t.begin(); d != t.end(); d = t.next())
		face_centroid[d] = faceCentroidELW<PFP>(map, d, position) ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
154
template <typename PFP>
155
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VertexAttribute<typename PFP::VEC3>& vertex_centroid, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
156
{
157
	TraversorV<typename PFP::MAP> t(map, thread) ;
158
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
159
		vertex_centroid[d] = vertexNeighborhoodCentroid<PFP>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
160
161
}

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176


namespace Parallel
{

template <typename PFP>
class FunctorComputeCentroidFaces: public FunctorMapThreaded<typename PFP::MAP >
{
	 const VertexAttribute<typename PFP::VEC3>& m_position;
	 FaceAttribute<typename PFP::VEC3>& m_fcentroid;
public:
	 FunctorComputeCentroidFaces<PFP>( typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, FaceAttribute<typename PFP::VEC3>& fcentroid):
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_fcentroid(fcentroid)
	 { }

177
	void run(Dart d, unsigned int threadID)
178
179
180
181
182
	{
		m_fcentroid[d] = faceCentroid<PFP>(this->m_map, d, m_position) ;
	}
};

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
template <typename PFP>
class FunctorComputeCentroidELWFaces: public FunctorMapThreaded<typename PFP::MAP >
{
	 const VertexAttribute<typename PFP::VEC3>& m_position;
	 FaceAttribute<typename PFP::VEC3>& m_fcentroid;
public:
	 FunctorComputeCentroidELWFaces<PFP>( typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, FaceAttribute<typename PFP::VEC3>& fcentroid):
	 	 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) ;
	}
};


200
201
202
203

template <typename PFP>
void computeCentroidFaces(typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position, FaceAttribute<typename PFP::VEC3>& face_centroid,
204
		unsigned int nbth, unsigned int current_thread)
205
206
{
	FunctorComputeCentroidFaces<PFP> funct(map,position,face_centroid);
207
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, current_thread);
208
209
}

210
211
212
template <typename PFP>
void computeCentroidELWFaces(typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position, FaceAttribute<typename PFP::VEC3>& face_centroid,
213
		unsigned int nbth, unsigned int current_thread)
214
215
{
	FunctorComputeCentroidELWFaces<PFP> funct(map,position,face_centroid);
216
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, current_thread);
217
218
}

219
220
221
222
223
224
225
226
227
228
229

template <typename PFP>
class FunctorComputeNeighborhoodCentroidVertices: public FunctorMapThreaded<typename PFP::MAP >
{
	 const VertexAttribute<typename PFP::VEC3>& m_position;
	 VertexAttribute<typename PFP::VEC3>& m_vcentroid;
public:
	 FunctorComputeNeighborhoodCentroidVertices<PFP>( typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VertexAttribute<typename PFP::VEC3>& vcentroid):
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vcentroid(vcentroid)
	 { }

230
	void run(Dart d, unsigned int threadID)
231
232
233
234
235
236
237
238
	{
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP>(this->m_map, d, m_position) ;
	}
};

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

}
246
247
} // namespace Geometry
} // namespace Surface
248

249
250
251
namespace Volume
{
namespace Geometry
Lionel Untereiner's avatar
Lionel Untereiner committed
252
{
253

Lionel Untereiner's avatar
Lionel Untereiner committed
254
255
256
template <typename PFP, typename EMBV, typename EMB>
EMB vertexNeighborhoodCentroidGen(typename PFP::MAP& map, Dart d, const EMBV& attributs)
{
257
258
//	EMB center = AttribOps::zero<EMB,PFP>();
	EMB center(0.0);
Lionel Untereiner's avatar
Lionel Untereiner committed
259
260
261
262
263
264
265
266
267
268
	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 ;
}
269

Lionel Untereiner's avatar
Lionel Untereiner committed
270
template <typename PFP>
271
void computeCentroidVolumes(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid, unsigned int thread)
Lionel Untereiner's avatar
Lionel Untereiner committed
272
{
273
	TraversorW<typename PFP::MAP> t(map, thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
274
	for(Dart d = t.begin(); d != t.end(); d = t.next())
275
		vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP>(map, d, position,thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
276
}
277

278
template <typename PFP>
279
void computeCentroidELWVolumes(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid, unsigned int thread)
280
{
281
	TraversorW<typename PFP::MAP> t(map,thread) ;
282
283
284
285
286
	for(Dart d = t.begin(); d != t.end(); d = t.next())
		vol_centroid[d] = Surface::Geometry::volumeCentroidELW<PFP>(map, d, position,thread) ;
}


287
288
289
290
291
292
293
294
295
296
297
namespace Parallel
{
template <typename PFP>
class FunctorComputeCentroidVolumes: public FunctorMapThreaded<typename PFP::MAP >
{
	 const VertexAttribute<typename PFP::VEC3>& m_position;
	 VolumeAttribute<typename PFP::VEC3>& m_vol_centroid;
public:
	 FunctorComputeCentroidVolumes<PFP>( typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid):
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vol_centroid(vol_centroid)
	 { }
298

299
300
301
302
303
	void run(Dart d, unsigned int threadID)
	{
		m_vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP>(this->m_map, d, m_position,threadID) ;
	}
};
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
	
template <typename PFP>
class FunctorComputeCentroidELWVolumes: public FunctorMapThreaded<typename PFP::MAP >
{
	 const VertexAttribute<typename PFP::VEC3>& m_position;
	 VolumeAttribute<typename PFP::VEC3>& m_vol_centroid;
public:
	 FunctorComputeCentroidELWVolumes<PFP>( typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid):
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vol_centroid(vol_centroid)
	 { }

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

321
322
323
template <typename PFP>
void computeCentroidVolumes(typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid,
324
		unsigned int nbth)
325
326
{
	FunctorComputeCentroidVolumes<PFP> funct(map,position,vol_centroid);
327
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
Lionel Untereiner's avatar
Lionel Untereiner committed
328
}
329
330


331
332
333
template <typename PFP>
void computeCentroidELWVolumes(typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid,
334
		unsigned int nbth)
335
336
{
	FunctorComputeCentroidELWVolumes<PFP> funct(map,position,vol_centroid);
337
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
338
339
340
}


341
342
343
344
345
346
347
348
349
template <typename PFP>
class FunctorComputeNeighborhoodCentroidVertices: public FunctorMapThreaded<typename PFP::MAP >
{
	 const VertexAttribute<typename PFP::VEC3>& m_position;
	 VertexAttribute<typename PFP::VEC3>& m_vcentroid;
public:
	 FunctorComputeNeighborhoodCentroidVertices<PFP>( typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VertexAttribute<typename PFP::VEC3>& vcentroid):
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vcentroid(vcentroid)
	 { }
350

351
352
353
354
355
	void run(Dart d, unsigned int threadID)
	{
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP>(this->m_map, d, m_position) ;
	}
};
356

357
358
359
template <typename PFP>
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position, VertexAttribute<typename PFP::VEC3>& vertex_centroid,
360
		unsigned int nbth, unsigned int current_thread)
361
362
{
	FunctorComputeNeighborhoodCentroidVertices<PFP> funct(map,position,vertex_centroid);
363
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false, current_thread);
364
}
365

Pierre Kraemer's avatar
Pierre Kraemer committed
366

untereiner's avatar
untereiner committed
367
} // namespace Parallel
Pierre Kraemer's avatar
Pierre Kraemer committed
368
369
} // namespace Geometry

untereiner's avatar
untereiner committed
370
} // namespace Volume
371
372
373



Pierre Kraemer's avatar
Pierre Kraemer committed
374
375
376
} // namespace Algo

} // namespace CGoGN