centroid.hpp 7.8 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
	center /= count ;
	return center ;
}

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

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

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

116
117
118
119
120
121
122
123
124
125
126
127
128
129


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

130
	void run(Dart d, unsigned int threadID)
131
132
133
134
135
136
137
138
139
	{
		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,
140
		const FunctorSelect& select, unsigned int nbth)
141
142
{
	FunctorComputeCentroidVolumes<PFP> funct(map,position,vol_centroid);
143
	Algo::Parallel::foreach_cell<typename PFP::MAP,VOLUME>(map, funct, nbth, true, select);
144
145
146
147
148
149
150
151
152
153
154
155
156
}


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

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


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

184
	void run(Dart d, unsigned int threadID)
185
186
187
188
189
190
191
192
193
194
195
	{
		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);
196
	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, funct, nbth, false, select, current_thread);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
}

}













Pierre Kraemer's avatar
Pierre Kraemer committed
213
214
} // namespace Geometry

215
216
}

Pierre Kraemer's avatar
Pierre Kraemer committed
217
218
219
} // namespace Algo

} // namespace CGoGN