bilateral.h 5.98 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

namespace CGoGN
{

namespace Algo
{

34 35 36
namespace Surface
{

Pierre Kraemer's avatar
Pierre Kraemer committed
37
namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40
{

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
41
void sigmaBilateral(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& normal, float& sigmaC, float& sigmaS)
Pierre Kraemer's avatar
Pierre Kraemer committed
42 43 44 45 46 47 48
{
	typedef typename PFP::VEC3 VEC3 ;

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

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

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

62 63 64 65 66 67 68
/**
 * \brief Function applying a bilateral filter smoothing on the mesh.
 * \param map the map of the mesh
 * \param positionIn the current positions container of the mesh
 * \param positionOut the smoothed positions after the function call
 * \param normal the normals
 */
Pierre Kraemer's avatar
Pierre Kraemer committed
69
template <typename PFP>
70 71 72 73 74
void filterBilateral(
        typename PFP::MAP& map,
        const VertexAttribute<typename PFP::VEC3,typename PFP::MAP>& positionIn,
        VertexAttribute<typename PFP::VEC3,typename PFP::MAP>& positionOut,
        const VertexAttribute<typename PFP::VEC3,typename PFP::MAP>& normal)
Pierre Kraemer's avatar
Pierre Kraemer committed
75 76 77 78
{
	typedef typename PFP::VEC3 VEC3 ;

	float sigmaC, sigmaS ;
79
    sigmaBilateral<PFP>(map, positionIn, normal, sigmaC, sigmaS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
80

81
	TraversorV<typename PFP::MAP> t(map) ;
82
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
83
	{
84
		if(!map.isBoundaryVertex(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
85
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
86 87 88
			// get normal of vertex
			const VEC3& normal_d = normal[d] ;

89 90 91 92 93
			// 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())
			{
94
                VEC3 vec = Algo::Surface::Geometry::vectorOutOfDart<PFP>(map, it, positionIn) ;
95 96 97 98 99 100
				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
101

102
            positionOut[d] = positionIn[d] + ((sum / normalizer) * normal_d) ;
103 104
		}
		else
105
            positionOut[d] = positionIn[d] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
106 107 108 109
	}
}

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
110
void filterSUSAN(typename PFP::MAP& map, float SUSANthreshold, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position, VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& position2, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP>& normal)
Pierre Kraemer's avatar
Pierre Kraemer committed
111 112 113 114
{
	typedef typename PFP::VEC3 VEC3 ;

	float sigmaC, sigmaS ;
115
	sigmaBilateral<PFP>(map, position, normal, sigmaC, sigmaS) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
116 117 118 119

	long nbTot = 0 ;
	long nbSusan = 0 ;

120
	TraversorV<typename PFP::MAP> t(map) ;
121
	for(Dart d = t.begin(); d != t.end(); d = t.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
122
	{
123
		if(!map.isBoundaryVertex(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
124
		{
125 126 127 128 129 130 131 132 133
			// 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
134
			{
135 136 137 138
				const VEC3& neighborNormal = normal[map.phi1(it)] ;
				float angle = Geom::angle(normal_d, neighborNormal) ;
				if( angle <= SUSANthreshold )
				{
Sylvain Thery's avatar
Sylvain Thery committed
139
					VEC3 vec = Algo::Surface::Geometry::vectorOutOfDart<PFP>(map, it, position) ;
140 141 142 143 144 145 146 147
					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 ;
				}
				else
					SUSANregion = true ;
148
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
149

150 151 152
			if(SUSANregion)
				nbSusan++ ;
			nbTot++ ;
Pierre Kraemer's avatar
Pierre Kraemer committed
153

154 155 156 157 158
			if (normalizer != 0.0f)
				position2[d] = pos_d + ((sum / normalizer) * normal_d) ;
			else
				position2[d] = pos_d ;
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
159
		else
160
			position2[d] = position[d] ;
Pierre Kraemer's avatar
Pierre Kraemer committed
161 162
	}

163
//	CGoGNout <<" susan rate = "<< float(nbSusan)/float(nbTot)<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165
}

Pierre Kraemer's avatar
Pierre Kraemer committed
166
} //namespace Filtering
Pierre Kraemer's avatar
Pierre Kraemer committed
167

168 169
}

Pierre Kraemer's avatar
Pierre Kraemer committed
170 171 172
} //namespace Algo

} //namespace CGoGN