centroid.hpp 9.47 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 62 63 64 65 66
		++count;
	}

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

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 ;
67 68
	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
69 70 71
	{
		center += attributs[it];
		++count ;
72
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
73 74 75 76 77 78 79 80 81
	center /= double(count);
	return center ;
}

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 ;
82 83
	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
84
	{
85
		center += attributs[it];
Pierre Kraemer's avatar
Pierre Kraemer committed
86
		++count ;
87
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89 90 91 92 93
	center /= count ;
	return center ;
}


template <typename PFP>
94
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
95
{
96
	TraversorF<typename PFP::MAP> t(map, select,thread) ;
97
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
98
		face_centroid[d] = faceCentroid<PFP>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
99 100 101
}

template <typename PFP>
102
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
103
{
104
	TraversorV<typename PFP::MAP> t(map, select, thread) ;
105
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
106
		vertex_centroid[d] = vertexNeighborhoodCentroid<PFP>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
107 108
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123


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

124
	void run(Dart d, unsigned int threadID)
125 126 127 128 129 130 131 132 133 134 135 136
	{
		m_fcentroid[d] = faceCentroid<PFP>(this->m_map, d, m_position) ;
	}
};


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);
137
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, select, current_thread);
138 139 140 141 142 143 144 145 146 147 148 149 150
}


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

151
	void run(Dart d, unsigned int threadID)
152 153 154 155 156 157 158 159 160 161 162
	{
		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);
163
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false, select, current_thread);
164 165 166
}

}
167 168
} // namespace Geometry
} // namespace Surface
169

170 171 172
namespace Volume
{
namespace Geometry
Lionel Untereiner's avatar
Lionel Untereiner committed
173
{
174

Lionel Untereiner's avatar
Lionel Untereiner committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188
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 ;
}
189

Lionel Untereiner's avatar
Lionel Untereiner committed
190
template <typename PFP>
191
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
192
{
193
	TraversorW<typename PFP::MAP> t(map, select,thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
194
	for(Dart d = t.begin(); d != t.end(); d = t.next())
195
		vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP>(map, d, position,thread) ;
Lionel Untereiner's avatar
Lionel Untereiner committed
196
}
197

198 199 200 201 202 203 204 205 206 207 208
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)
	 { }
209

210 211 212 213 214
	void run(Dart d, unsigned int threadID)
	{
		m_vol_centroid[d] = Surface::Geometry::volumeCentroid<PFP>(this->m_map, d, m_position,threadID) ;
	}
};
215

216 217 218 219 220 221 222
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
223
}
224 225


226 227 228 229 230 231 232 233 234
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)
	 { }
235

236 237 238 239 240
	void run(Dart d, unsigned int threadID)
	{
		m_vcentroid[d] = vertexNeighborhoodCentroid<PFP>(this->m_map, d, m_position) ;
	}
};
241

242 243 244 245 246 247 248 249
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);
}
250

Pierre Kraemer's avatar
Pierre Kraemer committed
251

untereiner's avatar
untereiner committed
252
} // namespace Parallel
Pierre Kraemer's avatar
Pierre Kraemer committed
253 254
} // namespace Geometry

untereiner's avatar
untereiner committed
255
} // namespace Volume
256 257 258 259




260

Pierre Kraemer's avatar
Pierre Kraemer committed
261 262 263
} // namespace Algo

} // namespace CGoGN