volume.hpp 5.72 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
#include "Topology/generic/traversor/traversorCell.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
26
#include "Algo/Geometry/centroid.h"
Thomas's avatar
Thomas committed
27
#include "Algo/Modelisation/tetrahedralization.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
28 29 30 31 32 33 34 35 36 37 38

namespace CGoGN
{

namespace Algo
{

namespace Geometry
{

template <typename PFP>
39
typename PFP::REAL tetrahedronSignedVolume(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
Pierre Kraemer's avatar
Pierre Kraemer committed
40
{
Pierre Kraemer's avatar
Pierre Kraemer committed
41 42
	typedef typename PFP::VEC3 VEC3;

43 44 45 46
	VEC3 p1 = position[v.dart] ;
	VEC3 p2 = position[map.phi1(v)] ;
	VEC3 p3 = position[map.phi_1(v)] ;
	VEC3 p4 = position[map.phi_1(map.phi2(v))] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
47 48 49 50 51

	return Geom::tetraSignedVolume(p1, p2, p3, p4) ;
}

template <typename PFP>
52
typename PFP::REAL tetrahedronVolume(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
Pierre Kraemer's avatar
Pierre Kraemer committed
53
{
Pierre Kraemer's avatar
Pierre Kraemer committed
54 55
	typedef typename PFP::VEC3 VEC3;

56 57 58 59
	VEC3 p1 = position[v.dart] ;
	VEC3 p2 = position[map.phi1(v)] ;
	VEC3 p3 = position[map.phi_1(v)] ;
	VEC3 p4 = position[map.phi_1(map.phi2(v))] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
60 61 62 63 64

	return Geom::tetraVolume(p1, p2, p3, p4) ;
}

template <typename PFP>
65
typename PFP::REAL convexPolyhedronVolume(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
66
{
Pierre Kraemer's avatar
Pierre Kraemer committed
67
	typedef typename PFP::MAP MAP;
Pierre Kraemer's avatar
Pierre Kraemer committed
68 69
	typedef typename PFP::VEC3 VEC3;

70 71
	if (Volume::Modelisation::Tetrahedralization::isTetrahedron<PFP>(map, v, thread))
		return tetrahedronVolume<PFP>(map, v, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
72 73 74
	else
	{
		typename PFP::REAL vol = 0 ;
75
		VEC3 vCentroid = Algo::Surface::Geometry::volumeCentroid<PFP>(map, v, position, thread) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
76

77
		DartMarkerStore<MAP> mark(map, thread) ;	// Lock a marker
Pierre Kraemer's avatar
Pierre Kraemer committed
78

79
		std::vector<Face> visitedFaces ;
Pierre Kraemer's avatar
Pierre Kraemer committed
80
		visitedFaces.reserve(100) ;
81

82 83 84
		Face f(v.dart) ;
		visitedFaces.push_back(f) ;
		mark.markOrbit(f) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
85

86
		for(unsigned int iface = 0; iface != visitedFaces.size(); ++iface)
Pierre Kraemer's avatar
Pierre Kraemer committed
87
		{
88 89
			f = visitedFaces[iface] ;
			if(map.isCycleTriangle(f))
Pierre Kraemer's avatar
Pierre Kraemer committed
90
			{
91 92 93
				VEC3 p1 = position[f.dart] ;
				VEC3 p2 = position[map.phi1(f)] ;
				VEC3 p3 = position[map.phi_1(f)] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
94 95 96 97
				vol += Geom::tetraVolume(p1, p2, p3, vCentroid) ;
			}
			else
			{
98 99
				VEC3 fCentroid = Algo::Surface::Geometry::faceCentroid<PFP>(map, f, position) ;
				Dart d = f.dart ;
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101
				do
				{
102 103
					VEC3 p1 = position[d] ;
					VEC3 p2 = position[map.phi1(d)] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
104
					vol += Geom::tetraVolume(p1, p2, fCentroid, vCentroid) ;
105 106
					d = map.phi1(d) ;
				} while(d != f.dart) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
107
			}
108
			Dart d = f.dart;
Pierre Kraemer's avatar
Pierre Kraemer committed
109 110
			do	// add all face neighbours to the table
			{
111 112
				Dart dd = map.phi2(d) ;
				if(!mark.isMarked(dd)) // not already marked
Pierre Kraemer's avatar
Pierre Kraemer committed
113
				{
114 115 116
					Face ff(dd);
					visitedFaces.push_back(ff) ;
					mark.markOrbit(ff) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
117
				}
118 119
				d = map.phi1(d) ;
			} while(d != f.dart) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121 122 123 124 125 126
		}

		return vol ;
	}
}

template <typename PFP>
127
typename PFP::REAL totalVolume(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
128
{
Pierre Kraemer's avatar
Pierre Kraemer committed
129
	if ((CGoGN::Parallel::NumberOfThreads > 1) && (thread == 0))
Sylvain Thery's avatar
Sylvain Thery committed
130
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
131
		return Parallel::totalVolume<PFP>(map, position);
Sylvain Thery's avatar
Sylvain Thery committed
132 133
	}

134
	double vol = 0.0 ;
135

136
	TraversorW<typename PFP::MAP> t(map, thread) ;
137
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
138
		vol += convexPolyhedronVolume<PFP>(map, d, position, thread) ;
139
	return typename PFP::REAL(vol) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141
}

142 143 144 145 146

namespace Parallel
{

template <typename PFP>
147
typename PFP::REAL totalVolume(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
148
{
Sylvain Thery's avatar
Sylvain Thery committed
149
	// allocate a vector of 1 accumulator for each thread
Pierre Kraemer's avatar
Pierre Kraemer committed
150
	std::vector<typename PFP::REAL> vols(CGoGN::Parallel::NumberOfThreads-1, 0.0);
151

Sylvain Thery's avatar
Sylvain Thery committed
152
	// foreach volume
Pierre Kraemer's avatar
Pierre Kraemer committed
153
	CGoGN::Parallel::foreach_cell<VOLUME>(map, [&] (Vol v, unsigned int thr)
Sylvain Thery's avatar
Sylvain Thery committed
154 155 156
	{
		// add volume to the thread accumulator
		vols[thr-1] += convexPolyhedronVolume<PFP>(map, v, position, thr) ;
Sylvain Thery's avatar
Sylvain Thery committed
157
	});
158

Sylvain Thery's avatar
Sylvain Thery committed
159 160
	// compute the sum of volumes
	typename PFP::REAL total(0);
Sylvain Thery's avatar
Sylvain Thery committed
161
	for (unsigned int i=0; i< CGoGN::Parallel::NumberOfThreads-1; ++i )
Sylvain Thery's avatar
Sylvain Thery committed
162
		total += vols[i];
163

Sylvain Thery's avatar
Sylvain Thery committed
164
	return total;
165 166
}

Pierre Kraemer's avatar
Pierre Kraemer committed
167 168 169
} // namespace Parallel


Pierre Kraemer's avatar
Pierre Kraemer committed
170 171 172 173 174
} // namespace Geometry

} // namespace Algo

} // namespace CGoGN