laplacian.hpp 6.27 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/traversor/traversorCell.h"
#include "Topology/generic/traversor/traversor2.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
27 28 29 30 31 32 33 34
#include "Algo/Geometry/basic.h"

namespace CGoGN
{

namespace Algo
{

35 36 37
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40
namespace Geometry
{

41 42
template <typename PFP, typename ATTR_TYPE>
ATTR_TYPE computeLaplacianTopoVertex(
43 44
	typename PFP::MAP& map,
	Dart d,
45
	const VertexAttribute<ATTR_TYPE, typename PFP::MAP>& attr)
46
{
47
	ATTR_TYPE l(0) ;
48 49
	ATTR_TYPE value = attr[d] ;
	unsigned int wSum = 0 ;
50 51 52

	Traversor2VE<typename PFP::MAP> t(map, d) ;
	for(Dart it = t.begin(); it != t.end(); it = t.next())
53
	{
54 55
		l += attr[map.phi1(it)] - value ;
		++wSum ;
56 57
	}

58
	l /= wSum ;
59 60 61
	return l ;
}

62 63
template <typename PFP, typename ATTR_TYPE>
ATTR_TYPE computeLaplacianCotanVertex(
64 65
	typename PFP::MAP& map,
	Dart d,
66 67 68
	const EdgeAttribute<typename PFP::REAL, typename PFP::MAP>& edgeWeight,
	const VertexAttribute<typename PFP::REAL, typename PFP::MAP>& vertexArea,
	const VertexAttribute<ATTR_TYPE, typename PFP::MAP>& attr)
69
{
70 71
	typedef typename PFP::REAL REAL;

72
	ATTR_TYPE l(0) ;
73
	REAL vArea = vertexArea[d] ;
74
	ATTR_TYPE value = attr[d] ;
75
	REAL wSum = 0 ;
76 77 78

	Traversor2VE<typename PFP::MAP> t(map, d) ;
	for(Dart it = t.begin(); it != t.end(); it = t.next())
79
	{
80
		REAL w = edgeWeight[it] / vArea ;
81 82
		l += (attr[map.phi1(it)] - value) * w ;
		wSum += w ;
83 84
	}

85
	l /= wSum ;
86 87 88
	return l ;
}

89
template <typename PFP, typename ATTR_TYPE>
90
void computeLaplacianTopoVertices(
91
	typename PFP::MAP& map,
92 93
	const VertexAttribute<ATTR_TYPE, typename PFP::MAP>& attr,
	VertexAttribute<ATTR_TYPE, typename PFP::MAP>& laplacian)
94
{
95
	TraversorV<typename PFP::MAP> t(map) ;
96
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
97
		laplacian[d] = computeLaplacianTopoVertex<PFP, ATTR_TYPE>(map, d, attr) ;
98 99
}

100
template <typename PFP, typename ATTR_TYPE>
101
void computeLaplacianCotanVertices(
102
	typename PFP::MAP& map,
103 104 105 106
	const EdgeAttribute<typename PFP::REAL, typename PFP::MAP>& edgeWeight,
	const VertexAttribute<typename PFP::REAL, typename PFP::MAP>& vertexArea,
	const VertexAttribute<ATTR_TYPE, typename PFP::MAP>& attr,
	VertexAttribute<ATTR_TYPE, typename PFP::MAP>& laplacian)
Pierre Kraemer's avatar
Pierre Kraemer committed
107
{
108
	TraversorV<typename PFP::MAP> t(map) ;
109
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
110
		laplacian[d] = computeLaplacianCotanVertex<PFP, ATTR_TYPE>(map, d, edgeWeight, vertexArea, attr) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
111 112 113
}

template <typename PFP>
114
typename PFP::REAL computeCotanWeightEdge(
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116
	typename PFP::MAP& map,
	Dart d,
117
	const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position)
118
{
119 120 121
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

122
	if(map.isBoundaryEdge(d))
123
	{
124 125 126
		const VEC3& p1 = position[d] ;
		const VEC3& p2 = position[map.phi1(d)] ;
		const VEC3& p3 = position[map.phi_1(d)] ;
127

128
		REAL cot_alpha = 1 / tan(Geom::angle(p1 - p3, p2 - p3)) ;
129 130 131 132
		return 0.5 * cot_alpha ;
	}
	else
	{
133 134 135 136
		const VEC3& p1 = position[d] ;
		const VEC3& p2 = position[map.phi1(d)] ;
		const VEC3& p3 = position[map.phi_1(d)] ;
		const VEC3& p4 = position[map.phi_1(map.phi2(d))] ;
137

138 139
		REAL cot_alpha = 1 / tan(Geom::angle(p1 - p3, p2 - p3)) ;
		REAL cot_beta = 1 / tan(Geom::angle(p2 - p4, p1 - p4)) ;
140
		return 0.5 * ( cot_alpha + cot_beta ) ;
141
	}
142 143 144 145 146
}

template <typename PFP>
void computeCotanWeightEdges(
	typename PFP::MAP& map,
147 148
	const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position,
	EdgeAttribute<typename PFP::REAL, typename PFP::MAP>& edgeWeight)
Pierre Kraemer's avatar
Pierre Kraemer committed
149
{
150
	TraversorE<typename PFP::MAP> t(map) ;
151
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
152
		edgeWeight[d] = computeCotanWeightEdge<PFP>(map, d, position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
153 154 155 156
}

} // namespace Geometry

157 158
} // namespace Surface

159 160 161 162 163 164 165 166

namespace Volume
{

namespace Geometry
{

template <typename PFP, typename ATTR_TYPE>
167 168 169
ATTR_TYPE computeLaplacianTopoVertex(
	typename PFP::MAP& map,
	Dart d,
170
	const VertexAttribute<ATTR_TYPE, typename PFP::MAP>& attr)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
	ATTR_TYPE l(0) ;
	ATTR_TYPE value = attr[d] ;
	unsigned int wSum = 0 ;

	Traversor3VE<typename PFP::MAP> t(map, d) ;
	for(Dart it = t.begin(); it != t.end(); it = t.next())
	{
		l += attr[map.phi1(it)] - value ;
		++wSum ;
	}

	l /= wSum ;
	return l ;
}

template <typename PFP, typename ATTR_TYPE>
188 189
void computeLaplacianTopoVertices(
	typename PFP::MAP& map,
190 191
	const VertexAttribute<ATTR_TYPE, typename PFP::MAP>& attr,
	VertexAttribute<ATTR_TYPE, typename PFP::MAP>& laplacian)
192
{
193
	TraversorV<typename PFP::MAP> t(map) ;
194 195 196 197 198 199 200 201
	for(Dart d = t.begin(); d != t.end(); d = t.next())
		laplacian[d] = computeLaplacianTopoVertex<PFP, ATTR_TYPE>(map, d, attr) ;
}

} // namespace Geometry

} // namespace Volume

Pierre Kraemer's avatar
Pierre Kraemer committed
202 203 204
} // namespace Algo

} // namespace CGoGN