centroid.hpp 13.3 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
{
	EMB center = AttribOps::zero<EMB,PFP>() ;
49
	unsigned int count = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
50

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

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

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
template <typename PFP, typename EMBV, typename EMB>
EMB volumeCentroidELWGen(typename PFP::MAP& map, Dart d, const EMBV& attributs, unsigned int thread)
{
	EMB center = AttribOps::zero<EMB,PFP>();
	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
81 82 83 84 85
template <typename PFP, typename EMBV, typename EMB>
EMB faceCentroidGen(typename PFP::MAP& map, Dart d, const EMBV& attributs)
{
	EMB center = AttribOps::zero<EMB,PFP>();
	unsigned int count = 0 ;
86 87
	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
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

template <typename PFP, typename EMBV, typename EMB>
EMB faceCentroidELWGen(typename PFP::MAP& map, Dart d, const EMBV& attributs)
{
	EMB center = AttribOps::zero<EMB,PFP>();
	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
116 117 118 119 120
template <typename PFP, typename EMBV, typename EMB>
EMB vertexNeighborhoodCentroidGen(typename PFP::MAP& map, Dart d, const EMBV& attributs)
{
	EMB center = AttribOps::zero<EMB,PFP>();
	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 131 132
	center /= count ;
	return center ;
}


template <typename PFP>
133
void computeCentroidFaces(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, FaceAttribute<typename PFP::VEC3>& face_centroid, const FunctorSelect& select, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
134
{
135
	TraversorF<typename PFP::MAP> t(map, select,thread) ;
136
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
137
		face_centroid[d] = faceCentroid<PFP>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139
}

140 141 142 143 144 145 146 147
template <typename PFP>
void computeCentroidELWFaces(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, FaceAttribute<typename PFP::VEC3>& face_centroid, const FunctorSelect& select, unsigned int thread)
{
	TraversorF<typename PFP::MAP> t(map, select,thread) ;
	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
148
template <typename PFP>
149
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VertexAttribute<typename PFP::VEC3>& vertex_centroid, const FunctorSelect& select, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
150
{
151
	TraversorV<typename PFP::MAP> t(map, select, thread) ;
152
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
153
		vertex_centroid[d] = vertexNeighborhoodCentroid<PFP>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
154 155
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170


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)
	 { }

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

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
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) ;
	}
};


194 195 196 197 198 199 200

template <typename PFP>
void computeCentroidFaces(typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position, FaceAttribute<typename PFP::VEC3>& face_centroid,
		const FunctorSelect& select, unsigned int nbth, unsigned int current_thread)
{
	FunctorComputeCentroidFaces<PFP> funct(map,position,face_centroid);
201
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, select, current_thread);
202 203
}

204 205 206 207 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,
		const FunctorSelect& select, unsigned int nbth, unsigned int current_thread)
{
	FunctorComputeCentroidELWFaces<PFP> funct(map,position,face_centroid);
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, select, current_thread);
}

213 214 215 216 217 218 219 220 221 222 223

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)
	 { }

224
	void run(Dart d, unsigned int threadID)
225 226 227 228 229 230 231 232 233 234 235
	{
		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,
		const FunctorSelect& select, unsigned int nbth, unsigned int current_thread)
{
	FunctorComputeNeighborhoodCentroidVertices<PFP> funct(map,position,vertex_centroid);
236
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false, select, current_thread);
237 238 239
}

}
240 241
} // namespace Geometry
} // namespace Surface
242

243 244 245
namespace Volume
{
namespace Geometry
Lionel Untereiner's avatar
Lionel Untereiner committed
246
{
247

Lionel Untereiner's avatar
Lionel Untereiner committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261
template <typename PFP, typename EMBV, typename EMB>
EMB vertexNeighborhoodCentroidGen(typename PFP::MAP& map, Dart d, const EMBV& attributs)
{
	EMB center = AttribOps::zero<EMB,PFP>();
	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 ;
}
262

Lionel Untereiner's avatar
Lionel Untereiner committed
263
template <typename PFP>
264
void computeCentroidVolumes(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid, const FunctorSelect& select, unsigned int thread)
Lionel Untereiner's avatar
Lionel Untereiner committed
265
{
266
	TraversorW<typename PFP::MAP> t(map, select,thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
267
	for(Dart d = t.begin(); d != t.end(); d = t.next())
268
		vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP>(map, d, position,thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
269
}
270

271 272 273 274 275 276 277 278 279
template <typename PFP>
void computeCentroidELWVolumes(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid, const FunctorSelect& select, unsigned int thread)
{
	TraversorW<typename PFP::MAP> t(map, select,thread) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
		vol_centroid[d] = Surface::Geometry::volumeCentroidELW<PFP>(map, d, position,thread) ;
}


280 281 282 283 284 285 286 287 288 289 290
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)
	 { }
291

292 293 294 295 296
	void run(Dart d, unsigned int threadID)
	{
		m_vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP>(this->m_map, d, m_position,threadID) ;
	}
};
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
	
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) ;
	}
};
313

314 315 316 317 318 319 320
template <typename PFP>
void computeCentroidVolumes(typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid,
		const FunctorSelect& select, unsigned int nbth)
{
	FunctorComputeCentroidVolumes<PFP> funct(map,position,vol_centroid);
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true, select);
Lionel Untereiner's avatar
Lionel Untereiner committed
321
}
322 323


324 325 326 327 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,
		const FunctorSelect& select, unsigned int nbth)
{
	FunctorComputeCentroidELWVolumes<PFP> funct(map,position,vol_centroid);
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true, select);
}


334 335 336 337 338 339 340 341 342
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)
	 { }
343

344 345 346 347 348
	void run(Dart d, unsigned int threadID)
	{
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP>(this->m_map, d, m_position) ;
	}
};
349

350 351 352 353 354 355 356 357
template <typename PFP>
void computeNeighborhoodCentroidVertices(typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position, VertexAttribute<typename PFP::VEC3>& vertex_centroid,
		const FunctorSelect& select, unsigned int nbth, unsigned int current_thread)
{
	FunctorComputeNeighborhoodCentroidVertices<PFP> funct(map,position,vertex_centroid);
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false, select, current_thread);
}
358

Pierre Kraemer's avatar
Pierre Kraemer committed
359

untereiner's avatar
untereiner committed
360
} // namespace Parallel
Pierre Kraemer's avatar
Pierre Kraemer committed
361 362
} // namespace Geometry

untereiner's avatar
untereiner committed
363
} // namespace Volume
364 365 366



Pierre Kraemer's avatar
Pierre Kraemer committed
367 368 369
} // namespace Algo

} // namespace CGoGN