centroid.hpp 8.63 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 39 40 41 42

namespace CGoGN
{

namespace Algo
{

namespace Geometry
{

template <typename PFP, typename EMBV, typename EMB>
43
EMB volumeCentroidGen(typename PFP::MAP& map, Dart d, const EMBV& attributs, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
44 45
{
	EMB center = AttribOps::zero<EMB,PFP>() ;
46
	unsigned int count = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
47

48
	Traversor3WV<typename PFP::MAP> tra(map,d,false,thread);
49
	for (Dart d = tra.begin(); d != tra.end(); d = tra.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
50
	{
51
		center += attributs[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
52 53 54 55 56 57 58 59 60 61 62 63
		++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 ;
64 65
	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
66 67 68
	{
		center += attributs[it];
		++count ;
69
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
70 71 72 73 74 75 76 77 78
	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 ;
79 80
	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
81
	{
82
		center += attributs[it];
Pierre Kraemer's avatar
Pierre Kraemer committed
83
		++count ;
84
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
85 86 87 88 89
	center /= count ;
	return center ;
}

template <typename PFP>
90
void computeCentroidVolumes(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid, const FunctorSelect& select, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
91
{
92
	TraversorW<typename PFP::MAP> t(map, select,thread) ;
93
	for(Dart d = t.begin(); d != t.end(); d = t.next())
94
		vol_centroid[d] = volumeCentroid<PFP>(map, d, position,thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
95 96 97
}

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

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

113 114 115 116 117 118 119 120 121 122 123 124 125 126


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

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


template <typename PFP>
void computeCentroidVolumes(typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position, VolumeAttribute<typename PFP::VEC3>& vol_centroid,
137
		const FunctorSelect& select, unsigned int nbth)
138 139
{
	FunctorComputeCentroidVolumes<PFP> funct(map,position,vol_centroid);
140
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true, select);
141 142 143 144 145 146 147 148 149 150 151 152 153
}


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

154
	void run(Dart d, unsigned int threadID)
155 156 157 158 159 160 161 162 163 164 165 166
	{
		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);
167
	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, funct, nbth, false, select, current_thread);
168 169 170 171 172 173 174 175 176 177 178 179 180
}


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

181
	void run(Dart d, unsigned int threadID)
182 183 184 185 186 187 188 189 190 191 192
	{
		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);
193
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false, select, current_thread);
194 195 196 197 198
}

}


Lionel Untereiner's avatar
Lionel Untereiner committed
199 200
namespace Volumes
{
201

Lionel Untereiner's avatar
Lionel Untereiner committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215
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 ;
}
216

Lionel Untereiner's avatar
Lionel Untereiner committed
217 218 219 220 221 222 223
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 thread)
{
	TraversorV<typename PFP::MAP> t(map, select, thread) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
		vertex_centroid[d] = Algo::Geometry::Volumes::vertexNeighborhoodCentroid<PFP>(map, d, position) ;
}
224

Lionel Untereiner's avatar
Lionel Untereiner committed
225
}
226 227 228 229




Pierre Kraemer's avatar
Pierre Kraemer committed
230 231 232 233 234
} // namespace Geometry

} // namespace Algo

} // namespace CGoGN