laplacian.hpp 5.23 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#include "Algo/Geometry/basic.h"

namespace CGoGN
{

namespace Algo
{

namespace Geometry
{

36
37
template <typename PFP, typename ATTR_TYPE>
ATTR_TYPE computeLaplacianTopoVertex(
38
39
	typename PFP::MAP& map,
	Dart d,
40
	const AttributeHandler<ATTR_TYPE>& attr)
41
{
42
	ATTR_TYPE l(0) ;
43
44
	ATTR_TYPE value = attr[d] ;
	unsigned int wSum = 0 ;
45
	Dart it = d ;
46
47
	do
	{
48
49
50
51
52
53
54
55
		l += attr[map.phi1(it)] - value ;
		++wSum ;
		Dart dboundary = map.phi_1(it) ;
		if(map.phi2(dboundary) == dboundary)
		{
			l += attr[dboundary] - value ;
			++wSum ;
		}
56
57
		it = map.alpha1(it) ;
	} while(it != d) ;
58
	l /= wSum ;
59
60
61
	return l ;
}

62
63
template <typename PFP, typename ATTR_TYPE>
ATTR_TYPE computeLaplacianCotanVertex(
64
65
66
	typename PFP::MAP& map,
	Dart d,
	const typename PFP::TREAL& edgeWeight,
67
68
	const typename PFP::TREAL& vertexArea,
	const AttributeHandler<ATTR_TYPE>& attr)
69
70
{
	typedef typename PFP::REAL REAL ;
71
	ATTR_TYPE l(0) ;
72
73
	Dart it = d ;
	REAL vArea = vertexArea[d] ;
74
75
	ATTR_TYPE value = attr[d] ;
	REAL wSum = 0 ;
76
77
78
	do
	{
		REAL w = edgeWeight[it] / vArea ;
79
80
81
82
83
84
85
86
87
		l += (attr[map.phi1(it)] - value) * w ;
		wSum += w ;
		Dart dboundary = map.phi_1(it) ;
		if(map.phi2(dboundary) == dboundary)
		{
			w = edgeWeight[dboundary] / vArea ;
			l += (attr[dboundary] - value) * w ;
			wSum += w ;
		}
88
89
		it = map.alpha1(it) ;
	} while(it != d) ;
90
	l /= wSum ;
91
92
93
	return l ;
}

94
template <typename PFP, typename ATTR_TYPE>
95
void computeLaplacianTopoVertices(
96
97
98
99
	typename PFP::MAP& map,
	const AttributeHandler<ATTR_TYPE>& attr,
	AttributeHandler<ATTR_TYPE>& laplacian,
	const FunctorSelect& select)
100
101
102
103
104
105
106
{
	CellMarker marker(map, VERTEX);
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !marker.isMarked(d))
		{
			marker.mark(d);
107
			laplacian[d] = computeLaplacianTopoVertex<PFP, ATTR_TYPE>(map, d, attr) ;
108
109
110
111
		}
	}
}

112
template <typename PFP, typename ATTR_TYPE>
113
void computeLaplacianCotanVertices(
114
115
116
117
118
119
	typename PFP::MAP& map,
	const typename PFP::TREAL& edgeWeight,
	const typename PFP::TREAL& vertexArea,
	const AttributeHandler<ATTR_TYPE>& attr,
	AttributeHandler<ATTR_TYPE>& laplacian,
	const FunctorSelect& select)
Pierre Kraemer's avatar
Pierre Kraemer committed
120
{
121
	CellMarker marker(map, VERTEX);
Pierre Kraemer's avatar
Pierre Kraemer committed
122
123
124
125
126
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !marker.isMarked(d))
		{
			marker.mark(d);
127
			laplacian[d] = computeLaplacianCotanVertex<PFP, ATTR_TYPE>(map, d, edgeWeight, vertexArea, attr) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
128
129
130
131
132
		}
	}
}

template <typename PFP>
133
typename PFP::REAL computeCotanWeightEdge(
Pierre Kraemer's avatar
Pierre Kraemer committed
134
135
	typename PFP::MAP& map,
	Dart d,
136
137
	const typename PFP::TVEC3& position)
{
138
139
140
141
	const typename PFP::VEC3& p1 = position[d] ;
	const typename PFP::VEC3& p2 = position[map.phi1(d)] ;
	const typename PFP::VEC3& p3 = position[map.phi_1(d)] ;
	typename PFP::REAL cot_alpha = 1 / tan(Geom::angle(p1 - p3, p2 - p3)) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
142
	typename PFP::REAL cot_beta = 0 ;
143
	Dart dd = map.phi2(d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
144
	if(dd != d)
145
146
147
148
	{
		const typename PFP::VEC3& p4 = position[map.phi_1(map.phi2(d))] ;
		cot_beta = 1 / tan(Geom::angle(p2 - p4, p1 - p4)) ;
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
149
	return 0.5 * ( cot_alpha + cot_beta ) ;
150
151
152
153
154
}

template <typename PFP>
void computeCotanWeightEdges(
	typename PFP::MAP& map,
Pierre Kraemer's avatar
Pierre Kraemer committed
155
	const typename PFP::TVEC3& position,
156
157
	typename PFP::TREAL& edgeWeight,
	const FunctorSelect& select = SelectorTrue())
Pierre Kraemer's avatar
Pierre Kraemer committed
158
{
159
160
	CellMarker marker(map, EDGE);
	for(Dart d = map.begin(); d != map.end(); map.next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
161
	{
162
163
164
165
166
167
		if(select(d) && !marker.isMarked(d))
		{
			marker.mark(d);
			edgeWeight[d] = computeCotanWeightEdge<PFP>(map, d, position) ;
		}
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
168
169
170
171
172
173
174
}

} // namespace Geometry

} // namespace Algo

} // namespace CGoGN