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

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

60 61
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)
62
{
63
	typedef typename V_ATT::DATA_TYPE EMB;
64
	EMB center(0.0);
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79
	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 ;
}

80 81
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
82
{
83
	typename V_ATT::DATA_TYPE center(0.0);
Pierre Kraemer's avatar
Pierre Kraemer committed
84
	unsigned int count = 0 ;
85 86
	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
87 88 89
	{
		center += attributs[it];
		++count ;
90
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92 93 94
	center /= double(count);
	return center ;
}

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

100
	EMB center(0.0);
101 102 103 104 105 106 107 108 109 110 111 112 113 114
	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 ;
}

115 116
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
117
{
118 119
	typename V_ATT::DATA_TYPE center(0.0);

Pierre Kraemer's avatar
Pierre Kraemer committed
120
	unsigned int count = 0 ;
121 122
	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
123
	{
124
		center += attributs[it];
Pierre Kraemer's avatar
Pierre Kraemer committed
125
		++count ;
126
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
127 128 129 130
	center /= count ;
	return center ;
}

131 132
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
133
{
134
	TraversorF<typename PFP::MAP> t(map,thread) ;
135
	for(Dart d = t.begin(); d != t.end(); d = t.next())
136
		face_centroid[d] = faceCentroid<PFP,V_ATT>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
137 138
}

139 140
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)
141
{
142
	TraversorF<typename PFP::MAP> t(map,thread) ;
143
	for(Dart d = t.begin(); d != t.end(); d = t.next())
144
		face_centroid[d] = faceCentroidELW<PFP,V_ATT>(map, d, position) ;
145 146
}

147 148
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
149
{
150
	TraversorV<typename PFP::MAP> t(map, thread) ;
151
	for(Dart d = t.begin(); d != t.end(); d = t.next())
152
		vertex_centroid[d] = vertexNeighborhoodCentroid<PFP,V_ATT>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
153 154
}

155 156 157 158

namespace Parallel
{

159
template <typename PFP, typename V_ATT, typename F_ATT>
Pierre Kraemer's avatar
Pierre Kraemer committed
160
class FunctorComputeCentroidFaces: public FunctorMapThreaded<typename PFP::MAP>
161
{
162 163
	 const V_ATT& m_position;
	 F_ATT& m_fcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
164

165
public:
166
	 FunctorComputeCentroidFaces<PFP,V_ATT,F_ATT>( typename PFP::MAP& map, const V_ATT& position, F_ATT& fcentroid):
167 168 169
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_fcentroid(fcentroid)
	 { }

170
	void run(Dart d, unsigned int /*threadID*/)
171 172 173 174 175
	{
		m_fcentroid[d] = faceCentroid<PFP>(this->m_map, d, m_position) ;
	}
};

176
template <typename PFP, typename V_ATT, typename F_ATT>
Pierre Kraemer's avatar
Pierre Kraemer committed
177
class FunctorComputeCentroidELWFaces: public FunctorMapThreaded<typename PFP::MAP>
178
{
179 180
	const V_ATT& m_position;
	F_ATT& m_fcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
181

182
public:
183
	 FunctorComputeCentroidELWFaces<PFP,V_ATT,F_ATT>( typename PFP::MAP& map, const V_ATT& position, F_ATT& fcentroid):
184 185 186 187 188 189 190 191 192
	 	 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) ;
	}
};

193 194 195 196 197
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
198

199 200 201 202
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)
	 { }
203

204 205 206 207 208
	void run(Dart d, unsigned int /*threadID*/)
	{
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP>(this->m_map, d, m_position) ;
	}
};
209

210 211
template <typename PFP, typename V_ATT, typename F_ATT>
void computeCentroidFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid,
212
		unsigned int nbth, unsigned int current_thread)
213
{
214
	FunctorComputeCentroidFaces<PFP,V_ATT,F_ATT> funct(map,position,face_centroid);
215
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, current_thread);
216 217
}

218 219
template <typename PFP, typename V_ATT, typename F_ATT>
void computeCentroidELWFaces(typename PFP::MAP& map, const V_ATT& position, F_ATT& face_centroid,
220
		unsigned int nbth, unsigned int current_thread)
221
{
222
	FunctorComputeCentroidELWFaces<PFP,V_ATT,F_ATT> funct(map,position,face_centroid);
223
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, current_thread);
224 225
}

226
template <typename PFP, typename V_ATT>
227
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map,
228
		const V_ATT& position, V_ATT& vertex_centroid,
229
		unsigned int nbth, unsigned int current_thread)
230
{
231 232
	FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT> funct(map,position,vertex_centroid);
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false);
233 234
}

Pierre Kraemer's avatar
Pierre Kraemer committed
235 236 237 238
} // namespace Parallel



239
} // namespace Geometry
Pierre Kraemer's avatar
Pierre Kraemer committed
240

241
} // namespace Surface
242

243 244
namespace Volume
{
Pierre Kraemer's avatar
Pierre Kraemer committed
245

246
namespace Geometry
Lionel Untereiner's avatar
Lionel Untereiner committed
247
{
248

249 250
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
251
{
252
	typename V_ATT::DATA_TYPE  center(0.0);
Lionel Untereiner's avatar
Lionel Untereiner committed
253 254 255 256 257 258 259 260 261 262
	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 ;
}
263

264 265
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
266
{
267
	TraversorW<typename PFP::MAP> t(map, thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
268
	for(Dart d = t.begin(); d != t.end(); d = t.next())
269
		vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP,V_ATT>(map, d, position,thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
270
}
271

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

280 281
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
282 283 284
{
	TraversorV<typename PFP::MAP> t(map, thread) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
285
		vertex_centroid[d] = Volume::Geometry::vertexNeighborhoodCentroid<PFP,V_ATT>(map, d, position) ;
Sylvain Thery's avatar
Sylvain Thery committed
286 287 288
}


289 290
namespace Parallel
{
Pierre Kraemer's avatar
Pierre Kraemer committed
291

292
template <typename PFP, typename V_ATT, typename W_ATT>
293 294
class FunctorComputeCentroidVolumes: public FunctorMapThreaded<typename PFP::MAP >
{
295 296
	 const V_ATT& m_position;
	 W_ATT& m_vol_centroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
297

298
public:
299
	 FunctorComputeCentroidVolumes<PFP,V_ATT,W_ATT>( typename PFP::MAP& map, const V_ATT& position, W_ATT& vol_centroid):
300 301
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vol_centroid(vol_centroid)
	 { }
302

303 304
	void run(Dart d, unsigned int threadID)
	{
305
		m_vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP,V_ATT,W_ATT>(this->m_map, d, m_position,threadID) ;
306 307
	}
};
308

309 310
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)
311
{
312
	FunctorComputeCentroidVolumes<PFP,V_ATT,W_ATT> funct(map,position,vol_centroid);
313
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
Lionel Untereiner's avatar
Lionel Untereiner committed
314
}
315

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

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

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

334 335 336
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
337
		unsigned int nbth)
338
{
339
	FunctorComputeCentroidELWVolumes<PFP,V_ATT,W_ATT> funct(map,position,vol_centroid);
340 341 342
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true);
}

343
template <typename PFP, typename V_ATT>
344 345
class FunctorComputeNeighborhoodCentroidVertices: public FunctorMapThreaded<typename PFP::MAP >
{
346 347
	 const V_ATT& m_position;
	 V_ATT& m_vcentroid;
Pierre Kraemer's avatar
Pierre Kraemer committed
348

349
public:
350
	 FunctorComputeNeighborhoodCentroidVertices<PFP,V_ATT>( typename PFP::MAP& map, const V_ATT& position, V_ATT& vcentroid):
351 352
	 	 FunctorMapThreaded<typename PFP::MAP>(map), m_position(position), m_vcentroid(vcentroid)
	 { }
353

354
	void run(Dart d, unsigned int /*threadID*/)
355
	{
356
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP,V_ATT>(this->m_map, d, m_position) ;
357 358
	}
};
359

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

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

370

Pierre Kraemer's avatar
Pierre Kraemer committed
371
} // namespace Geometry
372

Pierre Kraemer's avatar
Pierre Kraemer committed
373
} // namespace Volume
374

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

} // namespace CGoGN