average.h 5.77 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 25 26 27 28 29 30 31 32 33 34 35
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Filtering/functors.h"
#include "Algo/Selection/collector.h"

namespace CGoGN
{

namespace Algo
{

namespace Filtering
{
36 37

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

template <typename PFP, typename T>
40 41 42 43 44 45
void filterAverageAttribute_OneRing(
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
	neighborhood neigh,
	const FunctorSelect& select = SelectorTrue())
46 47 48
{
	typedef typename PFP::VEC3 VEC3 ;

49
	FunctorAverage<VEC3> fa(attIn) ;
50 51
	Algo::Selection::Collector_OneRing<PFP> col(map) ;

52
	CellMarker markV(map, VERTEX) ;
53 54 55 56
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !markV.isMarked(d))
		{
57
			markV.mark(d) ;
58 59 60 61 62 63 64

			if (neigh & INSIDE)
				col.collectAll(d) ;
			else
				col.collectBorder(d) ;

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

template <typename PFP, typename T>
87 88 89 90 91 92 93 94
void filterAverageEdgesAttribute_WithinSphere(
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
	neighborhood neigh,
	typename PFP::TVEC3 & position,
	typename PFP::REAL radius,
	const FunctorSelect& select = SelectorTrue())
95 96 97
{
	typedef typename PFP::VEC3 VEC3 ;

98
	FunctorAverage<VEC3> fa(attIn) ;
99 100
	Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;

101
	CellMarker markV(map, VERTEX) ;
102 103 104 105
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !markV.isMarked(d))
		{
106
			markV.mark(d) ;
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

			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() ;
		}
	}
}

template <typename PFP, typename T>
122 123 124 125 126 127 128 129
void filterAverageFacesAttribute_WithinSphere(
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
	neighborhood neigh,
	typename PFP::TVEC3 & position,
	typename PFP::REAL radius,
	const FunctorSelect& select = SelectorTrue())
130 131 132
{
	typedef typename PFP::VEC3 VEC3 ;

133
	FunctorAverage<VEC3> fa(attIn) ;
134 135
	Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;

136
	CellMarker markV(map, VERTEX) ;
137 138 139 140
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !markV.isMarked(d))
		{
141
			markV.mark(d) ;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

			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() ;
		}
	}
}

template <typename PFP, typename T>
157 158 159 160 161 162 163 164
void filterAverageVertexAttribute_WithinSphere(
	typename PFP::MAP& map,
	const AttributeHandler<T>& attIn,
	AttributeHandler<T>& attOut,
	neighborhood neigh,
	typename PFP::TVEC3 & position,
	typename PFP::REAL radius,
	const FunctorSelect& select = SelectorTrue())
165 166 167
{
	typedef typename PFP::VEC3 VEC3 ;

168
	FunctorAverage<VEC3> faInside(attIn) ;
169 170 171
	FunctorAverageOnSphereBorder<PFP, typename PFP::VEC3> faBorder(map, attIn, position) ;
	Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;

172
	CellMarker markV(map, VERTEX) ;
173 174 175 176
	for(Dart d = map.begin(); d != map.end(); map.next(d))
	{
		if(select(d) && !markV.isMarked(d))
		{
177
			markV.mark(d) ;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

			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.getCout() + faBorder.getCount() ;
		}
	}
}

} // namespace Filtering

} // namespace Algo

} // namespace CGoGN