area.hpp 9 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 26
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#include "Geometry/basic.h"
#include "Algo/Geometry/centroid.h"
27 28

#include "Topology/generic/autoAttributeHandler.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
29 30 31 32 33 34 35

namespace CGoGN
{

namespace Algo
{

36 37 38
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
39 40 41 42
namespace Geometry
{

template <typename PFP>
43
typename PFP::REAL triangleArea(typename PFP::MAP& map, Face d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
Pierre Kraemer's avatar
Pierre Kraemer committed
44
{
Sylvain Thery's avatar
Sylvain Thery committed
45
	typename PFP::VEC3 p1 = position[d.dart] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
	typename PFP::VEC3 p2 = position[map.phi1(d)] ;
	typename PFP::VEC3 p3 = position[map.phi_1(d)] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51 52

	return Geom::triangleArea(p1, p2, p3) ;
}

template <typename PFP>
53
typename PFP::REAL convexFaceArea(typename PFP::MAP& map, Face d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
Pierre Kraemer's avatar
Pierre Kraemer committed
54
{
Pierre Kraemer's avatar
Pierre Kraemer committed
55
	typedef typename PFP::VEC3 VEC3 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
56

57
	if(map.faceDegree(d) == 3)
Pierre Kraemer's avatar
Pierre Kraemer committed
58 59 60 61
		return triangleArea<PFP>(map, d, position) ;
	else
	{
		float area = 0.0f ;
62
		VEC3 centroid = faceCentroid<PFP>(map, d, position) ;
63 64
		Traversor2FE<typename PFP::MAP> t(map, d) ;
		for(Dart it = t.begin(); it != t.end(); it = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
65
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
66 67
			VEC3 p1 = position[it] ;
			VEC3 p2 = position[map.phi1(it)] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
68
			area += Geom::triangleArea(p1, p2, centroid) ;
69
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
70 71 72 73 74
		return area ;
	}
}

template <typename PFP>
75
typename PFP::REAL totalArea(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
76
{
77
	typename PFP::REAL area(0) ;
78

79 80
	foreach_cell<FACE>(map, [&] (Face f)
	{
81
		area += convexFaceArea<PFP>(map, f, position);
82
	}
Sylvain Thery's avatar
Sylvain Thery committed
83
	,AUTO,thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
84
	return area ;
85

Pierre Kraemer's avatar
Pierre Kraemer committed
86 87
}

88
template <typename PFP>
89
typename PFP::REAL vertexOneRingArea(typename PFP::MAP& map, Vertex v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
90 91
{
	typename PFP::REAL area(0) ;
92 93 94

	foreach_incident2<FACE>(map, v, [&] (Face f)
	{
95
		area += convexFaceArea<PFP>(map,f, position) ;
96
	});
97 98 99 100
	return area ;
}

template <typename PFP>
101
typename PFP::REAL vertexBarycentricArea(typename PFP::MAP& map, Vertex v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
102 103
{
	typename PFP::REAL area(0) ;
104 105 106

	foreach_incident2<FACE>(map, v, [&] (Face f)
	{
107
		area += convexFaceArea<PFP>(map, f, position) / 3 ;
108
	});
109 110 111
	return area ;
}

112
template <typename PFP>
113
typename PFP::REAL vertexVoronoiArea(typename PFP::MAP& map, Vertex v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
114 115
{
	typename PFP::REAL area(0) ;
116
	foreach_incident2<FACE>(map, v, [&] (Face it)
117
	{
118 119 120 121
		const typename PFP::VEC3& p1 = position[it] ;
		const typename PFP::VEC3& p2 = position[map.phi1(it)] ;
		const typename PFP::VEC3& p3 = position[map.phi_1(it)] ;
		if(!Geom::isTriangleObtuse(p1, p2, p3))
122
		{
123 124 125
			typename PFP::REAL a = Geom::angle(p3 - p2, p1 - p2) ;
			typename PFP::REAL b = Geom::angle(p1 - p3, p2 - p3) ;
			area += ( (p2 - p1).norm2() / tan(b) + (p3 - p1).norm2() / tan(a) ) / 8 ;
126 127 128
		}
		else
		{
129 130
			typename PFP::REAL tArea = Geom::triangleArea(p1, p2, p3) ;
			if(Geom::angle(p2 - p1, p3 - p1) > M_PI / 2)
131 132 133 134
				area += tArea / 2 ;
			else
				area += tArea / 4 ;
		}
135
	});
136 137 138
	return area ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
139
template <typename PFP>
140
void computeAreaFaces(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, FaceAttribute<typename PFP::REAL, typename PFP::MAP>& face_area, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
141
{
142 143 144 145 146 147

	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread==0))
	{
		Parallel::computeAreaFaces<PFP>(map,position,face_area);
		return;
	}
148 149 150

	foreach_cell<FACE>(map, [&] (Face f)
	{
151
		face_area[f] = convexFaceArea<PFP>(map, f, position) ;
152
	}
153
	,AUTO,thread);
154

Pierre Kraemer's avatar
Pierre Kraemer committed
155 156
}

157
template <typename PFP>
158
void computeOneRingAreaVertices(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::REAL, typename PFP::MAP>& vertex_area, unsigned int thread)
159
{
160 161 162 163 164 165
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread==0))
	{
		Parallel::computeOneRingAreaVertices<PFP>(map,position,vertex_area);
		return;
	}

166
	FaceAutoAttribute<typename PFP::REAL,typename PFP::MAP> areas(map);
167
	computeAreaFaces<PFP>(map,position,areas);
168 169

	foreach_cell<VERTEX>(map, [&] (Vertex v)
170 171
	{
		vertex_area[v] = typename PFP::REAL(0);
172 173
		foreach_incident2<FACE>(map, v, [&] (Face f)
		{
174
			vertex_area[v] += areas[f];
175
		});
176
	}
177
	,FORCE_CELL_MARKING,thread);
178 179
}

180

181
template <typename PFP>
182
void computeBarycentricAreaVertices(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::REAL, typename PFP::MAP>& vertex_area, unsigned int thread)
183
{
184 185 186 187 188 189
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread==0))
	{
		Parallel::computeBarycentricAreaVertices<PFP>(map,position,vertex_area);
		return;
	}

190 191 192 193
	foreach_cell<VERTEX>(map, [&] (Vertex v)
	{
		vertex_area[v] = vertexBarycentricArea<PFP>(map, v, position) ;
	}
194
	,FORCE_CELL_MARKING,thread);
195 196
}

197
template <typename PFP>
198
void computeVoronoiAreaVertices(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::REAL, typename PFP::MAP>& vertex_area, unsigned int thread)
199
{
200 201 202 203 204 205
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread==0))
	{
		Parallel::computeVoronoiAreaVertices<PFP>(map,position,vertex_area);
		return;
	}

206 207 208 209
	foreach_cell<VERTEX>(map, [&] (Vertex v)
	{
		vertex_area[v] = vertexVoronoiArea<PFP>(map, v, position) ;
	}
210
	,FORCE_CELL_MARKING,thread);
211 212
}

213

Pierre Kraemer's avatar
Pierre Kraemer committed
214

215 216
namespace Parallel
{
Pierre Kraemer's avatar
Pierre Kraemer committed
217

218
template <typename PFP>
219
void computeAreaFaces(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, FaceAttribute<typename PFP::REAL, typename PFP::MAP>& area)
220
{
Sylvain Thery's avatar
Sylvain Thery committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
//	if (map.isOrbitEmbedded<FACE>())
//	{
//		Parallel::foreach_cell<FACE>(map,[&](Face f, unsigned int thr)
//		{
//			area[f] = convexFaceArea<PFP>(map, f, position) ;
//		},nbth,false,FORCE_CELL_MARKING);
//	}
//	else
//	{
//		Parallel::foreach_cell<FACE>(map,[&](Face f, unsigned int thr)
//		{
//			area[f] = convexFaceArea<PFP>(map, f, position) ;
//		},nbth,false,AUTO);
//	}

Pierre Kraemer's avatar
Pierre Kraemer committed
236
	CGoGN::Parallel::foreach_cell<FACE>(map, [&] (Face f, unsigned int /*thr*/)
Sylvain Thery's avatar
Sylvain Thery committed
237 238
	{
		area[f] = convexFaceArea<PFP>(map, f, position) ;
Sylvain Thery's avatar
Sylvain Thery committed
239
	});
240 241 242 243
}


template <typename PFP>
244
void computeOneRingAreaVertices(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::REAL, typename PFP::MAP>& area)
245
{
Pierre Kraemer's avatar
Pierre Kraemer committed
246
	CGoGN::Parallel::foreach_cell<VERTEX>(map, [&] (Vertex v, unsigned int /*thr*/)
247
	{
Sylvain Thery's avatar
Sylvain Thery committed
248
		area[v] = vertexOneRingArea<PFP>(map, v, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
249
	}, FORCE_CELL_MARKING);
250 251 252
}

template <typename PFP>
253
void computeBarycentricAreaVertices(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::REAL, typename PFP::MAP>& vertex_area)
254 255 256 257
{
	CGoGN::Parallel::foreach_cell<VERTEX>(map, [&] (Vertex v, unsigned int thr)
	{
		vertex_area[v] = vertexBarycentricArea<PFP>(map, v, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
258
	}, FORCE_CELL_MARKING);
Sylvain Thery's avatar
Sylvain Thery committed
259
}
260 261

template <typename PFP>
262
void computeVoronoiAreaVertices(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::REAL, typename PFP::MAP>& area)
263
{
Pierre Kraemer's avatar
Pierre Kraemer committed
264
	CGoGN::Parallel::foreach_cell<VERTEX>(map, [&] (Vertex v, unsigned int /*thr*/)
Sylvain Thery's avatar
Sylvain Thery committed
265 266
	{
		area[v] = vertexVoronoiArea<PFP>(map, v, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
267
	}, FORCE_CELL_MARKING);
268 269
}

Pierre Kraemer's avatar
Pierre Kraemer committed
270
} // namespace Parallel
271 272 273



Pierre Kraemer's avatar
Pierre Kraemer committed
274 275
} // namespace Geometry

Pierre Kraemer's avatar
Pierre Kraemer committed
276
} // namespace Surface
277

Pierre Kraemer's avatar
Pierre Kraemer committed
278 279 280
} // namespace Algo

} // namespace CGoGN