functors.h 3.73 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 25 26 27 28
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __FILTERING_FUNCTORS_H__
#define __FILTERING_FUNCTORS_H__

#include "Topology/generic/functor.h"
29
#include "Algo/Geometry/intersection.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30 31 32 33 34 35 36

namespace CGoGN
{

namespace Algo
{

37 38 39
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
40 41 42
namespace Filtering
{

43
template <typename T, unsigned int ORBIT>
44
class FunctorAverage : public virtual FunctorType
Pierre Kraemer's avatar
Pierre Kraemer committed
45 46
{
protected:
47
	const AttributeHandler<T, ORBIT>& attr ;
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49
	T sum ;
	unsigned int count ;
50

Pierre Kraemer's avatar
Pierre Kraemer committed
51
public:
52
	FunctorAverage(const AttributeHandler<T, ORBIT>& a) : FunctorType(), attr(a), sum(0), count(0)
Pierre Kraemer's avatar
Pierre Kraemer committed
53 54 55 56 57 58 59 60 61
	{}
	bool operator()(Dart d)
	{
		sum += attr[d] ;
		++count ;
		return false ;
	}
	inline void reset() { sum = T(0) ; count = 0 ; }
	inline T getSum() { return sum ; }
62 63 64 65 66 67 68 69
	inline unsigned int getCount() { return count ; }
	inline T getAverage() { return sum / typename T::DATA_TYPE(count) ; }
} ;

template <typename PFP, typename T>
class FunctorAverageOnSphereBorder : public FunctorMap<typename PFP::MAP>
{
	typedef typename PFP::VEC3 VEC3;
70

71
protected:
72
	const VertexAttribute<T>& attr ;
73
	const VertexAttribute<VEC3>& position ;
74 75 76 77
	VEC3 center;
	typename PFP::REAL radius;
	T sum ;
	unsigned int count ;
78

79
public:
80
	FunctorAverageOnSphereBorder(typename PFP::MAP& map, const AttributeHandler<T, VERTEX>& a, const VertexAttribute<VEC3>& p) :
81
		FunctorMap<typename PFP::MAP>(map), attr(a), position(p), sum(0), count(0)
Pierre Kraemer's avatar
Pierre Kraemer committed
82 83 84 85
	{
		center = VEC3(0);
		radius = 0;
	}
86 87 88
	bool operator()(Dart d)
	{
		typename PFP::REAL alpha = 0;
Thery Sylvain's avatar
Thery Sylvain committed
89
		Geometry::intersectionSphereEdge<PFP>(this->m_map, center, radius, d, position, alpha);
90
		sum += (1 - alpha) * attr[d] + alpha * attr[this->m_map.phi1(d)] ;
91 92 93 94 95 96
		++count ;
		return false ;
	}
	inline void reset(VEC3& c, typename PFP::REAL r) { center = c; radius = r; sum = T(0) ; count = 0 ; }
	inline T getSum() { return sum ; }
	inline unsigned int getCount() { return count ; }
Pierre Kraemer's avatar
Pierre Kraemer committed
97 98 99 100 101
	inline T getAverage() { return sum / typename T::DATA_TYPE(count) ; }
} ;

} // namespace Filtering

102 103
}

Pierre Kraemer's avatar
Pierre Kraemer committed
104 105 106 107 108
} // namespace Algo

} // namespace CGoGN

#endif