average.h 5.51 KB
Newer Older
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           *
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/                                           *
21 22 23 24
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

25
#include "Topology/generic/traversorCell.h"
26 27 28 29 30 31 32 33 34 35 36
#include "Algo/Filtering/functors.h"
#include "Algo/Selection/collector.h"

namespace CGoGN
{

namespace Algo
{

namespace Filtering
{
37 38

enum neighborhood { INSIDE = 1, BORDER = 2 };
39 40

template <typename PFP, typename T>
41 42 43 44
void filterAverageAttribute_OneRing(
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
45
	int neigh,
Pierre Kraemer's avatar
Pierre Kraemer committed
46
	const FunctorSelect& select = allDarts)
47
{
48
	FunctorAverage<T> fa(attIn) ;
49 50
	Algo::Selection::Collector_OneRing<PFP> col(map) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
51
	TraversorV<typename PFP::MAP> t(map, select) ;
52
	for(Dart d = t.begin(); d != t.end(); d = t.next())
53
	{
54
		if(!map.isBoundaryVertex(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
55
		{
56 57 58 59 60 61 62
			if (neigh & INSIDE)
				col.collectAll(d) ;
			else
				col.collectBorder(d) ;

			fa.reset() ;
			if (neigh & INSIDE)
63
			{
64 65 66 67 68 69 70 71 72 73 74 75
				switch (attIn.getOrbit())
				{
				case VERTEX :
					col.applyOnInsideVertices(fa) ;
					break;
				case EDGE :
					col.applyOnInsideEdges(fa) ;
					break;
				case FACE :
					col.applyOnInsideFaces(fa) ;
					break;
				}
76
			}
77 78 79
			if (neigh & BORDER)
				col.applyOnBorder(fa) ;
			attOut[d] = fa.getAverage() ;
80
		}
81 82
		else
			attOut[d] = attIn[d] ;
83 84 85 86
	}
}

template <typename PFP, typename T>
87
void filterAverageVertexAttribute_WithinSphere(
88 89 90
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
91
	int neigh,
92 93
	typename PFP::TVEC3 & position,
	typename PFP::REAL radius,
Pierre Kraemer's avatar
Pierre Kraemer committed
94
	const FunctorSelect& select = allDarts)
95
{
96 97
	FunctorAverage<T> faInside(attIn) ;
	FunctorAverageOnSphereBorder<PFP, T> faBorder(map, attIn, position) ;
98 99
	Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
100
	TraversorV<typename PFP::MAP> t(map, select) ;
101
	for(Dart d = t.begin(); d != t.end(); d = t.next())
102
	{
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		if(!map.isBoundaryVertex(d))
		{
			if (neigh & INSIDE)
				col.collectAll(d) ;
			else
				col.collectBorder(d) ;

			attOut[d] = T(0);
			if (neigh & INSIDE){
				faInside.reset() ;
				col.applyOnInsideVertices(faInside) ;
				attOut[d] += faInside.getSum();
			}
			if (neigh & BORDER){
				faBorder.reset(position[d], radius);
				col.applyOnBorder(faBorder) ;
				attOut[d] += faBorder.getSum();
			}
			attOut[d] /= faInside.getCount() + faBorder.getCount() ;
Pierre Kraemer's avatar
Pierre Kraemer committed
122
		}
123 124
		else
			attOut[d] = attIn[d] ;
125 126 127 128
	}
}

template <typename PFP, typename T>
129
void filterAverageEdgeAttribute_WithinSphere(
130 131 132
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
133
	int neigh,
134 135
	typename PFP::TVEC3 & position,
	typename PFP::REAL radius,
Pierre Kraemer's avatar
Pierre Kraemer committed
136
	const FunctorSelect& select = allDarts)
137
{
138
	FunctorAverage<T> fa(attIn) ;
139 140
	Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;

141
	TraversorE<typename PFP::MAP> t(map, select) ;
142
	for(Dart d = t.begin(); d != t.end(); d = t.next())
143
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146 147 148 149 150 151 152
		if (neigh & INSIDE)
			col.collectAll(d) ;
		else
			col.collectBorder(d) ;

		fa.reset() ;
		if (neigh & INSIDE) col.applyOnInsideEdges(fa) ;
		if (neigh & BORDER) col.applyOnBorder(fa) ;
		attOut[d] = fa.getAverage() ;
153 154 155 156
	}
}

template <typename PFP, typename T>
157
void filterAverageFaceAttribute_WithinSphere(
158 159 160
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
161
	int neigh,
162 163
	typename PFP::TVEC3 & position,
	typename PFP::REAL radius,
Pierre Kraemer's avatar
Pierre Kraemer committed
164
	const FunctorSelect& select = allDarts)
165
{
166
	FunctorAverage<T> fa(attIn) ;
167 168
	Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;

169
	TraversorF<typename PFP::MAP> t(map, select) ;
170
	for(Dart d = t.begin(); d != t.end(); d = t.next())
171
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
172 173 174 175 176 177 178 179 180
		if (neigh & INSIDE)
			col.collectAll(d) ;
		else
			col.collectBorder(d) ;

		fa.reset() ;
		if (neigh & INSIDE) col.applyOnInsideFaces(fa) ;
		if (neigh & BORDER) col.applyOnBorder(fa) ;
		attOut[d] = fa.getAverage() ;
181 182 183 184 185 186 187 188
	}
}

} // namespace Filtering

} // namespace Algo

} // namespace CGoGN