taubin.h 4.96 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 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
{

37 38 39 40
namespace Filtering
{

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

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

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

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

53
	FunctorAverage<VertexAttribute<VEC3, MAP> > fa1(position) ;
54 55
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
56
		if( !mv.isMarked(d))
57 58 59
		{
			mv.mark(d);

60 61 62 63 64 65 66 67 68 69 70 71
			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] ;
72 73 74 75
		}
	}

	// unshrinking step
76
	FunctorAverage<VertexAttribute<VEC3, MAP> > fa2(position2) ;
77 78
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
79
		if( mv.isMarked(d))
80 81 82
		{
			mv.unmark(d);

83 84 85 86 87 88 89 90 91 92 93 94
			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] ;
95 96 97 98 99 100 101
		}
	}
}

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

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

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

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

121 122 123 124 125 126 127 128 129 130 131 132
			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
133 134 135 136
		}
	}

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

145 146 147 148 149 150 151 152 153 154 155 156
			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
157 158 159 160
		}
	}
}

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

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

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

167
} // namespace CGoGN