taubin.h 5.04 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                                        *
*                                                                              *
*******************************************************************************/

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
{

34 35 36
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
37 38 39 40
namespace Filtering
{

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
41
void filterTaubin(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position2)
Pierre Kraemer's avatar
Pierre Kraemer committed
42
{
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44
	typedef typename PFP::MAP MAP ;
	typedef typename PFP::MAP::IMPL MAP_IMPL ;
Pierre Kraemer's avatar
Pierre Kraemer committed
45 46
	typedef typename PFP::VEC3 VEC3 ;

Sylvain Thery's avatar
Sylvain Thery committed
47
	Algo::Surface::Selection::Collector_OneRing<PFP> c(map) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
48

Pierre Kraemer's avatar
Pierre Kraemer committed
49 50
	const float lambda = 0.6307 ;
	const float mu = -0.6732 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
51

Pierre Kraemer's avatar
Pierre Kraemer committed
52
	CellMarkerNoUnmark<MAP, VERTEX> mv(map) ;
53

Pierre Kraemer's avatar
Pierre Kraemer committed
54
	FunctorAverage<VertexAttribute<VEC3, MAP_IMPL> > fa1(position) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
55 56
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
57
		if( !mv.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
58 59 60
		{
			mv.mark(d);

61 62 63 64 65 66 67 68 69 70 71 72
			if(!map.isBoundaryVertex(d))
			{
				c.collectBorder(d) ;
				fa1.reset() ;
				c.applyOnBorder(fa1) ;
				VEC3 p = position[d] ;
				VEC3 displ = fa1.getAverage() - p ;
				displ *= lambda ;
				position2[d] = p + displ ;
			}
			else
				position2[d] = position[d] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
73 74 75 76
		}
	}

	// unshrinking step
Pierre Kraemer's avatar
Pierre Kraemer committed
77
	FunctorAverage<VertexAttribute<VEC3, MAP_IMPL> > fa2(position2) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
78 79
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
80
		if( mv.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
81 82 83
		{
			mv.unmark(d);

84 85 86 87 88 89 90 91 92 93 94 95
			if(!map.isBoundaryVertex(d))
			{
				c.collectBorder(d) ;
				fa2.reset() ;
				c.applyOnBorder(fa2) ;
				VEC3 p = position2[d] ;
				VEC3 displ = fa2.getAverage() - p ;
				displ *= mu ;
				position[d] = p + displ ;
			}
			else
				position[d] = position2[d] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97 98 99 100 101 102
		}
	}
}

/**
 * Taubin filter modified as proposed by [Lav09]
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
103
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
104
void filterTaubin_modified(typename PFP::MAP& map, VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position2, typename PFP::REAL radius)
Pierre Kraemer's avatar
Pierre Kraemer committed
105
{
Pierre Kraemer's avatar
Pierre Kraemer committed
106
	typedef typename PFP::MAP MAP ;
Pierre Kraemer's avatar
Pierre Kraemer committed
107 108 109 110 111
	typedef typename PFP::VEC3 VEC3 ;

	const float lambda = 0.6307 ;
	const float mu = -0.6732 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
112
	CellMarkerNoUnmark<MAP, VERTEX> mv(map) ;
113

114
	FunctorAverageOnSphereBorder<PFP, VEC3> fa1(map, position, position) ;
Sylvain Thery's avatar
Sylvain Thery committed
115
	Algo::Surface::Selection::Collector_WithinSphere<PFP> c1(map, position, radius) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
116 117
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
118
		if( !mv.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
119 120 121
		{
			mv.mark(d);

122 123 124 125 126 127 128 129 130 131 132 133
			if(!map.isBoundaryVertex(d))
			{
				c1.collectBorder(d) ;
				VEC3 center = position[d] ;
				fa1.reset(center, radius) ;
				c1.applyOnBorder(fa1) ;
				VEC3 displ = fa1.getAverage() - center ;
				displ *= lambda ;
				position2[d] = center + displ ;
			}
			else
				position2[d] = position[d] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
134 135 136 137
		}
	}

	// unshrinking step
138
	FunctorAverageOnSphereBorder<PFP, VEC3> fa2(map, position2, position2) ;
Sylvain Thery's avatar
Sylvain Thery committed
139
	Algo::Surface::Selection::Collector_WithinSphere<PFP> c2(map, position2, radius) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
142
		if( mv.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144 145
		{
			mv.unmark(d);

146 147 148 149 150 151 152 153 154 155 156 157
			if(!map.isBoundaryVertex(d))
			{
				c2.collectBorder(d) ;
				VEC3 center = position2[d] ;
				fa2.reset(center, radius) ;
				c2.applyOnBorder(fa2) ;
				VEC3 displ = fa2.getAverage() - center ;
				displ *= mu ;
				position[d] = center + displ ;
			}
			else
				position[d] = position2[d] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
158 159 160 161
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
162
} // namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
163

Pierre Kraemer's avatar
Pierre Kraemer committed
164
} // namespace Surface
165

Pierre Kraemer's avatar
Pierre Kraemer committed
166
} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
167

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