bilateral.h 5.29 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <math.h>
26
#include "Topology/generic/traversorCell.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
27 28 29 30 31 32 33

namespace CGoGN
{

namespace Algo
{

Pierre Kraemer's avatar
Pierre Kraemer committed
34
namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
35 36 37 38 39 40 41 42 43 44 45
{

template <typename PFP>
void sigmaBilateral(typename PFP::MAP& map, const typename PFP::TVEC3& position, const typename PFP::TVEC3& normal, float& sigmaC, float& sigmaS, const FunctorSelect& select)
{
	typedef typename PFP::VEC3 VEC3 ;

	float sumLengths = 0.0f ;
	float sumAngles = 0.0f ;
	long nbEdges = 0 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
46
	TraversorE<typename PFP::MAP> t(map, select) ;
47
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
48
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
49 50 51
		sumLengths += Algo::Geometry::edgeLength<PFP>(map, d, position) ;
		sumAngles += Geom::angle(normal[d], normal[map.phi1(d)]) ;
		++nbEdges ;
Pierre Kraemer's avatar
Pierre Kraemer committed
52 53 54 55 56 57 58 59
	}

	// update of returned values
	sigmaC = 1.0f * ( sumLengths / float(nbEdges) ) ;
	sigmaS = 2.5f * ( sumAngles / float(nbEdges) ) ;
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
60
void filterBilateral(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TVEC3& position2, const typename PFP::TVEC3& normal, const FunctorSelect& select = allDarts)
Pierre Kraemer's avatar
Pierre Kraemer committed
61 62 63 64 65 66
{
	typedef typename PFP::VEC3 VEC3 ;

	float sigmaC, sigmaS ;
	sigmaBilateral<PFP>(map, position, normal, sigmaC, sigmaS, select) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
67
	TraversorV<typename PFP::MAP> t(map, select) ;
68
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
69
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
70 71 72 73 74 75 76 77
		// get position & normal of vertex
		const VEC3& pos_d = position[d] ;
		const VEC3& normal_d = normal[d] ;

		// traversal of incident edges
		float sum = 0.0f, normalizer = 0.0f ;
		Traversor2VE<typename PFP::MAP> te(map, d) ;
		for(Dart it = te.begin(); it != te.end(); it = te.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
78
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
79 80 81 82 83 84
			VEC3 vec = Algo::Geometry::vectorOutOfDart<PFP>(map, it, position) ;
			float h = normal_d * vec ;
			float t = vec.norm() ;
			float wcs = exp( ( -1.0f * (t * t) / (2.0f * sigmaC * sigmaC) ) + ( -1.0f * (h * h) / (2.0f * sigmaS * sigmaS) ) ) ;
			sum += wcs * h ;
			normalizer += wcs ;
Pierre Kraemer's avatar
Pierre Kraemer committed
85
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87

		position2[d] = pos_d + ((sum / normalizer) * normal_d) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89 90 91
	}
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
92
void filterSUSAN(typename PFP::MAP& map, float SUSANthreshold, const typename PFP::TVEC3& position, typename PFP::TVEC3& position2, const typename PFP::TVEC3& normal, const FunctorSelect& select = allDarts)
Pierre Kraemer's avatar
Pierre Kraemer committed
93 94 95 96 97 98 99 100 101
{
	typedef typename PFP::VEC3 VEC3 ;

	float sigmaC, sigmaS ;
	sigmaBilateral<PFP>(map, position, normal, sigmaC, sigmaS, select) ;

	long nbTot = 0 ;
	long nbSusan = 0 ;

Pierre Kraemer's avatar
Pierre Kraemer committed
102
	TraversorV<typename PFP::MAP> t(map, select) ;
103
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
104
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
105 106 107 108 109 110 111 112 113
		// get position & normal of vertex
		const VEC3& pos_d = position[d] ;
		const VEC3& normal_d = normal[d] ;

		// traversal of incident edges
		float sum = 0.0f, normalizer = 0.0f ;
		bool SUSANregion = false ;
		Traversor2VE<typename PFP::MAP> te(map, d) ;
		for(Dart it = te.begin(); it != te.end(); it = te.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
114
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116 117
			const VEC3& neighborNormal = normal[map.phi1(it)] ;
			float angle = Geom::angle(normal_d, neighborNormal) ;
			if( angle <= SUSANthreshold )
Pierre Kraemer's avatar
Pierre Kraemer committed
118
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
119 120 121 122 123 124
				VEC3 vec = Algo::Geometry::vectorOutOfDart<PFP>(map, it, position) ;
				float h = normal_d * vec ;
				float t = vec.norm() ;
				float wcs = exp( ( -1.0f * (t * t) / (2.0f * sigmaC * sigmaC) ) + ( -1.0f * (h * h) / (2.0f * sigmaS * sigmaS) ) );
				sum += wcs * h ;
				normalizer += wcs ;
125
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
126
			else
Pierre Kraemer's avatar
Pierre Kraemer committed
127
				SUSANregion = true ;
Pierre Kraemer's avatar
Pierre Kraemer committed
128
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
129 130 131 132 133 134 135 136 137

		if(SUSANregion)
			nbSusan++ ;
		nbTot++ ;

		if (normalizer != 0.0f)
			position2[d] = pos_d + ((sum / normalizer) * normal_d) ;
		else
			position2[d] = pos_d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
138 139
	}

140
//	CGoGNout <<" susan rate = "<< float(nbSusan)/float(nbTot)<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
141 142
}

Pierre Kraemer's avatar
Pierre Kraemer committed
143
} //namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146 147

} //namespace Algo

} //namespace CGoGN