average.h 5.47 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,
46
	const FunctorSelect& select = SelectorTrue())
47
{
48
	FunctorAverage<T> fa(attIn) ;
49 50
	Algo::Selection::Collector_OneRing<PFP> col(map) ;

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

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

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

98 99
	TraversorV<typename PFP::MAP> t(map) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
100
	{
101
		if(select(d))
102 103 104 105 106 107
		{
			if (neigh & INSIDE)
				col.collectAll(d) ;
			else
				col.collectBorder(d) ;

108 109 110 111 112 113 114 115 116 117 118 119
			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() ;
120 121 122 123 124
		}
	}
}

template <typename PFP, typename T>
125
void filterAverageEdgeAttribute_WithinSphere(
126 127 128
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
129
	int neigh,
130 131 132
	typename PFP::TVEC3 & position,
	typename PFP::REAL radius,
	const FunctorSelect& select = SelectorTrue())
133
{
134
	FunctorAverage<T> fa(attIn) ;
135 136
	Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;

137 138
	TraversorV<typename PFP::MAP> t(map) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
139
	{
140
		if(select(d))
141 142 143 144 145 146 147
		{
			if (neigh & INSIDE)
				col.collectAll(d) ;
			else
				col.collectBorder(d) ;

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

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

168 169
	TraversorV<typename PFP::MAP> t(map) ;
	for(Dart d = t.begin(); d != t.end(); d = t.next())
170
	{
171
		if(select(d))
172 173 174 175 176 177
		{
			if (neigh & INSIDE)
				col.collectAll(d) ;
			else
				col.collectBorder(d) ;

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

} // namespace Filtering

} // namespace Algo

} // namespace CGoGN