taubin.h 4.6 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
25
26
27
#include "Algo/Filtering/functors.h"
#include "Algo/Selection/collector.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
28
29
30
31
32
33
namespace CGoGN
{

namespace Algo
{

Pierre Kraemer's avatar
Pierre Kraemer committed
34
35
36
37
38
namespace Filtering
{

template <typename PFP>
void filterTaubin(typename PFP::MAP& map, typename PFP::TVEC3& position, typename PFP::TVEC3& position2, const FunctorSelect& select = SelectorTrue())
Pierre Kraemer's avatar
Pierre Kraemer committed
39
{
Pierre Kraemer's avatar
Pierre Kraemer committed
40
41
42
	typedef typename PFP::VEC3 VEC3 ;

	Algo::Selection::Collector_OneRing<PFP> c(map) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
43

Pierre Kraemer's avatar
Pierre Kraemer committed
44
45
	const float lambda = 0.6307 ;
	const float mu = -0.6732 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
46

Pierre Kraemer's avatar
Pierre Kraemer committed
47
	CellMarkerNoUnmark mv(map, VERTEX) ;
48
49

	FunctorAverage<VEC3> fa1(position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
50
51
52
53
54
55
56
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !mv.isMarked(d))
		{
			mv.mark(d);

			c.collectBorder(d) ;
57
58
			fa1.reset() ;
			c.applyOnBorder(fa1) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
59
			VEC3 p = position[d] ;
60
			VEC3 displ = fa1.getAverage() - p ;
Basile Sauvage's avatar
Basile Sauvage committed
61
//			VEC3 displ = (fa1.getSum() - p * fa1.getCount()) / fa1.getCount() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
62
63
64
65
66
67
			displ *= lambda ;
			position2[d] = p + displ ;
		}
	}

	// unshrinking step
Basile Sauvage's avatar
Basile Sauvage committed
68
	FunctorAverage<VEC3> fa2(position2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
69
70
71
72
73
74
75
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && mv.isMarked(d))
		{
			mv.unmark(d);

			c.collectBorder(d) ;
76
77
			fa2.reset() ;
			c.applyOnBorder(fa2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
78
			VEC3 p = position2[d] ;
79
			VEC3 displ = fa2.getAverage() - p ;
Basile Sauvage's avatar
Basile Sauvage committed
80
//			VEC3 displ = (fa2.getSum() - p * fa2.getCount()) / fa2.getCount() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
81
82
83
84
85
86
87
88
89
			displ *= mu ;
			position[d] = p + displ ;
		}
	}
}

/**
 * Taubin filter modified as proposed by [Lav09]
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
90
template <typename PFP>
91
void filterTaubin_modified(typename PFP::MAP& map, typename PFP::TVEC3& position, typename PFP::TVEC3& position2, typename PFP::REAL radius, const FunctorSelect& select = SelectorTrue())
Pierre Kraemer's avatar
Pierre Kraemer committed
92
93
94
{
	typedef typename PFP::VEC3 VEC3 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
95

Pierre Kraemer's avatar
Pierre Kraemer committed
96
97
98
	const float lambda = 0.6307 ;
	const float mu = -0.6732 ;

99
	FunctorAverageOnSphereBorder<PFP, typename PFP::VEC3> fa1(map, position, position) ;
100
	Algo::Selection::Collector_WithinSphere<PFP> c1(map, position, radius) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
101
	CellMarkerNoUnmark mv(map, VERTEX) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
102
103
104
105
106
107
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !mv.isMarked(d))
		{
			mv.mark(d);

108
			c1.collectBorder(d) ;
109

110
			VEC3 center = position[d] ;
111
112
113
114
			fa1.reset (center, radius) ;

			c1.applyOnBorder(fa1) ;
			VEC3 displ = fa1.getAverage() - center ;
Pierre Kraemer's avatar
Pierre Kraemer committed
115
			displ *= lambda ;
116
			position2[d] = center + displ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
117
118
119
120
		}
	}

	// unshrinking step
121
	FunctorAverageOnSphereBorder<PFP, typename PFP::VEC3> fa2(map, position2, position2) ;
122
	Algo::Selection::Collector_WithinSphere<PFP> c2(map, position2, radius) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
123
124
125
126
127
128
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && mv.isMarked(d))
		{
			mv.unmark(d);

129
130
			c2.collectBorder(d) ;
			VEC3 center = position2[d] ;
131
132
133
			fa2.reset(center, radius) ;
			c2.applyOnBorder(fa2) ;
			VEC3 displ = fa2.getAverage() - center ;
Pierre Kraemer's avatar
Pierre Kraemer committed
134
			displ *= mu ;
135
			position[d] = center + displ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
136
137
138
139
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
140
} // namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
141

Pierre Kraemer's avatar
Pierre Kraemer committed
142
} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
143

Pierre Kraemer's avatar
Pierre Kraemer committed
144
} // namespace CGoGN