Commit 73576e31 authored by Basile Sauvage's avatar Basile Sauvage

ajout des filtres taubin et moyenneur avec les collector + foncteurs

parent 6923415d
/*******************************************************************************
* 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
{
enum neighborhood {INSIDE = 1, BORDER = 2};
template <typename PFP, typename T>
void filterAverageAttribute_OneRing(typename PFP::MAP& map,
const typename AttributeHandler<T>& attIn,
typename AttributeHandler<T>& attOut,
neighborhood neigh,
const FunctorSelect& select = SelectorTrue())
{
typedef typename PFP::VEC3 VEC3 ;
FunctorAverage<typename PFP::VEC3> fa(attIn) ;
Algo::Selection::Collector_OneRing<PFP> col(map) ;
CellMarker markV(map, VERTEX);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && !markV.isMarked(d))
{
markV.mark(d);
if (neigh & INSIDE)
col.collectAll(d) ;
else
col.collectBorder(d) ;
fa.reset() ;
if (neigh & INSIDE){
switch (attIn.getOrbit()) {
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>
void filterAverageEdgesAttribute_WithinSphere(typename PFP::MAP& map,
const typename AttributeHandler<T>& attIn,
typename AttributeHandler<T>& attOut,
neighborhood neigh,
typename PFP::TVEC3 & position,
typename PFP::REAL radius,
const FunctorSelect& select = SelectorTrue())
{
typedef typename PFP::VEC3 VEC3 ;
FunctorAverage<typename PFP::VEC3> fa(attIn) ;
Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;
CellMarker markV(map, VERTEX);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && !markV.isMarked(d))
{
markV.mark(d);
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>
void filterAverageFacesAttribute_WithinSphere(typename PFP::MAP& map,
const typename AttributeHandler<T>& attIn,
typename AttributeHandler<T>& attOut,
neighborhood neigh,
typename PFP::TVEC3 & position,
typename PFP::REAL radius,
const FunctorSelect& select = SelectorTrue())
{
typedef typename PFP::VEC3 VEC3 ;
FunctorAverage<typename PFP::VEC3> fa(attIn) ;
Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;
CellMarker markV(map, VERTEX);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && !markV.isMarked(d))
{
markV.mark(d);
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>
void filterAverageVertexAttribute_WithinSphere(typename PFP::MAP& map,
const typename AttributeHandler<T>& attIn,
typename AttributeHandler<T>& attOut,
neighborhood neigh,
typename PFP::TVEC3 & position,
typename PFP::REAL radius,
const FunctorSelect& select = SelectorTrue())
{
typedef typename PFP::VEC3 VEC3 ;
FunctorAverage<typename PFP::VEC3> faInside(attIn) ;
FunctorAverageOnSphereBorder<PFP, typename PFP::VEC3> faBorder(map, attIn, position) ;
Algo::Selection::Collector_WithinSphere<PFP> col(map, position, radius) ;
CellMarker markV(map, VERTEX);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && !markV.isMarked(d))
{
markV.mark(d);
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
......@@ -39,7 +39,7 @@ void filterAveragePositions(typename PFP::MAP& map, const typename PFP::TVEC3& p
{
typedef typename PFP::VEC3 VEC3 ;
FunctorAverage<typename PFP::MAP, typename PFP::VEC3> fa(map, position) ;
FunctorAverage<typename PFP::VEC3> fa(position) ;
Algo::Selection::Collector_OneRing<PFP> c(map) ;
CellMarker markV(map, VERTEX);
......
......@@ -26,6 +26,7 @@
#define __FILTERING_FUNCTORS_H__
#include "Topology/generic/functor.h"
#include "Utils/intersection.h"
namespace CGoGN
{
......@@ -36,15 +37,15 @@ namespace Algo
namespace Filtering
{
template <typename MAP, typename T>
class FunctorAverage : public FunctorMap<MAP>
template <typename T>
class FunctorAverage : public FunctorType
{
protected:
const AttributeHandler<T>& attr ;
T sum ;
unsigned int count ;
public:
FunctorAverage(MAP& map, const AttributeHandler<T>& a) : FunctorMap<MAP>(map), attr(a), sum(0), count(0)
FunctorAverage(const AttributeHandler<T>& a) : FunctorType(), attr(a), sum(0), count(0)
{}
bool operator()(Dart d)
{
......@@ -54,6 +55,36 @@ public:
}
inline void reset() { sum = T(0) ; count = 0 ; }
inline T getSum() { return sum ; }
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;
protected:
const AttributeHandler<T>& attr ;
const AttributeHandler<VEC3>& position ;
VEC3 center;
typename PFP::REAL radius;
T sum ;
unsigned int count ;
public:
FunctorAverageOnSphereBorder(typename PFP::MAP& map, const AttributeHandler<T>& a, const AttributeHandler<VEC3>& p, VEC3& c = VEC3(0), typename PFP::REAL r = 0) :
FunctorMap<typename PFP::MAP>(map), attr(a), position(p), center(c), radius(r), sum(0), count(0)
{}
bool operator()(Dart d)
{
typename PFP::REAL alpha = 0;
Algo::Geometry::intersectionSphereEdge<PFP>(this->map, center, radius, d, position, alpha);
sum += (1 - alpha) * attr[d] + alpha * attr[this->map.phi1(d)] ;
++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 ; }
inline T getAverage() { return sum / typename T::DATA_TYPE(count) ; }
} ;
......
......@@ -39,13 +39,14 @@ void filterTaubin(typename PFP::MAP& map, typename PFP::TVEC3& position, typenam
{
typedef typename PFP::VEC3 VEC3 ;
FunctorAverage<typename PFP::MAP, typename PFP::VEC3> fa(map, position) ;
Algo::Selection::Collector_OneRing<PFP> c(map) ;
const float lambda = 0.6307 ;
const float mu = -0.6732 ;
CellMarkerNoUnmark mv(map, VERTEX) ;
FunctorAverage<VEC3> fa1(position) ;
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && !mv.isMarked(d))
......@@ -53,16 +54,17 @@ void filterTaubin(typename PFP::MAP& map, typename PFP::TVEC3& position, typenam
mv.mark(d);
c.collectBorder(d) ;
fa.reset() ;
c.applyOnBorder(fa) ;
fa1.reset() ;
c.applyOnBorder(fa1) ;
VEC3 p = position[d] ;
VEC3 displ = fa.getAverage() - p ;
VEC3 displ = fa1.getAverage() - p ;
displ *= lambda ;
position2[d] = p + displ ;
}
}
// unshrinking step
FunctorAverage<VEC3> fa2(position) ;
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && mv.isMarked(d))
......@@ -70,10 +72,10 @@ void filterTaubin(typename PFP::MAP& map, typename PFP::TVEC3& position, typenam
mv.unmark(d);
c.collectBorder(d) ;
fa.reset() ;
c.applyOnBorder(fa) ;
fa2.reset() ;
c.applyOnBorder(fa2) ;
VEC3 p = position2[d] ;
VEC3 displ = fa.getAverage() - p ;
VEC3 displ = fa2.getAverage() - p ;
displ *= mu ;
position[d] = p + displ ;
}
......@@ -84,16 +86,15 @@ void filterTaubin(typename PFP::MAP& map, typename PFP::TVEC3& position, typenam
* Taubin filter modified as proposed by [Lav09]
*/
template <typename PFP>
void filterTaubin_modified(typename PFP::MAP& map, typename PFP::TVEC3& position, typename PFP::TVEC3& position2, const FunctorSelect& select = SelectorTrue())
void filterTaubin_modified(typename PFP::MAP& map, typename PFP::TVEC3& position, typename PFP::TVEC3& position2, typename PFP::REAL radius, const FunctorSelect& select = SelectorTrue())
{
typedef typename PFP::VEC3 VEC3 ;
FunctorAverage<typename PFP::MAP, typename PFP::VEC3> fa(map, position) ;
Algo::Selection::Collector_OneRing<PFP> c(map) ;
const float lambda = 0.6307 ;
const float mu = -0.6732 ;
Algo::Selection::Collector_WithinSphere<PFP> c1(map, position, radius) ;
CellMarkerNoUnmark mv(map, VERTEX) ;
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
......@@ -101,30 +102,31 @@ void filterTaubin_modified(typename PFP::MAP& map, typename PFP::TVEC3& position
{
mv.mark(d);
c.collectBorder(d) ;
fa.reset() ;
c.applyOnBorder(fa) ;
VEC3 p = position[d] ;
VEC3 displ = fa.getAverage() - p ;
c1.collectBorder(d) ;
VEC3 center = position[d] ;
FunctorAverageOnSphereBorder<PFP, typename PFP::VEC3> fa(map, position, position, center, radius) ;
c1.applyOnBorder(fa) ;
VEC3 displ = fa.getAverage() - center ;
displ *= lambda ;
position2[d] = p + displ ;
position2[d] = center + displ ;
}
}
// unshrinking step
Algo::Selection::Collector_WithinSphere<PFP> c2(map, position2, radius) ;
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && mv.isMarked(d))
{
mv.unmark(d);
c.collectBorder(d) ;
fa.reset() ;
c.applyOnBorder(fa) ;
VEC3 p = position2[d] ;
VEC3 displ = fa.getAverage() - p ;
c2.collectBorder(d) ;
VEC3 center = position2[d] ;
FunctorAverageOnSphereBorder<PFP, typename PFP::VEC3> fa(map, position2, position2, center, radius) ;
c2.applyOnBorder(fa) ;
VEC3 displ = fa.getAverage() - center ;
displ *= mu ;
position[d] = p + displ ;
position[d] = center + displ ;
}
}
}
......
......@@ -79,8 +79,8 @@ bool areTrianglesInIntersection(typename PFP::MAP& map, Dart tri1, Dart tri2, co
/**
* alpha = coef d'interpolation dans [0 ,1] tel que v = (1-alpha)*pin + alpha*pout
* est le point d'intersection entre la sphère et le segment [pin, pout]
* avec pin = position[din] à l'intérieur de la sphère
* avec pout = position[dout] à l'extérieur de la sphère
* avec pin = position[d] à l'intérieur de la sphère
* avec pout = position[phi1(d)] à l'extérieur de la sphère
*/
template <typename PFP>
bool intersectionSphereEdge(typename PFP::MAP& map, typename PFP::VEC3& center, typename PFP::REAL radius, Dart d, const typename PFP::TVEC3& position, typename PFP::REAL& alpha) ;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment