average.h 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.u-strasbg.fr/                                         *
* 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
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
54 55 56 57
		if (neigh & INSIDE)
			col.collectAll(d) ;
		else
			col.collectBorder(d) ;
58

Pierre Kraemer's avatar
Pierre Kraemer committed
59 60 61 62
		fa.reset() ;
		if (neigh & INSIDE)
		{
			switch (attIn.getOrbit())
63
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
64 65 66 67 68 69 70 71 72
			case VERTEX :
				col.applyOnInsideVertices(fa) ;
				break;
			case EDGE :
				col.applyOnInsideEdges(fa) ;
				break;
			case FACE :
				col.applyOnInsideFaces(fa) ;
				break;
73 74
			}
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76 77
		if (neigh & BORDER)
			col.applyOnBorder(fa) ;
		attOut[d] = fa.getAverage() ;
78 79 80 81
	}
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
95
	TraversorV<typename PFP::MAP> t(map, select) ;
96
	for(Dart d = t.begin(); d != t.end(); d = t.next())
97
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
98 99 100 101 102 103 104 105 106 107
		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();
108
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
109 110 111 112 113 114
		if (neigh & BORDER){
			faBorder.reset(position[d], radius);
			col.applyOnBorder(faBorder) ;
			attOut[d] += faBorder.getSum();
		}
		attOut[d] /= faInside.getCount() + faBorder.getCount() ;
115 116 117 118
	}
}

template <typename PFP, typename T>
119
void filterAverageEdgeAttribute_WithinSphere(
120 121 122
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
123
	int neigh,
124 125
	typename PFP::TVEC3 & position,
	typename PFP::REAL radius,
Pierre Kraemer's avatar
Pierre Kraemer committed
126
	const FunctorSelect& select = allDarts)
127
{
128
	FunctorAverage<T> fa(attIn) ;
129 130
	Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
131
	TraversorV<typename PFP::MAP> t(map, select) ;
132
	for(Dart d = t.begin(); d != t.end(); d = t.next())
133
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
134 135 136 137 138 139 140 141 142
		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() ;
143 144 145 146
	}
}

template <typename PFP, typename T>
147
void filterAverageFaceAttribute_WithinSphere(
148 149 150
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
151
	int neigh,
152 153
	typename PFP::TVEC3 & position,
	typename PFP::REAL radius,
Pierre Kraemer's avatar
Pierre Kraemer committed
154
	const FunctorSelect& select = allDarts)
155
{
156
	FunctorAverage<T> fa(attIn) ;
157 158
	Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
159
	TraversorV<typename PFP::MAP> t(map, select) ;
160
	for(Dart d = t.begin(); d != t.end(); d = t.next())
161
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
162 163 164 165 166 167 168 169 170
		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() ;
171 172 173 174 175 176 177 178
	}
}

} // namespace Filtering

} // namespace Algo

} // namespace CGoGN