Commit 3ea0e63a authored by Pierre Kraemer's avatar Pierre Kraemer

cotangent weight laplacian matrix

parent 7b93a4a0
...@@ -35,17 +35,29 @@ namespace Geometry ...@@ -35,17 +35,29 @@ namespace Geometry
{ {
template <typename PFP> template <typename PFP>
typename PFP::REAL triangleArea(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position); typename PFP::REAL triangleArea(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position) ;
template <typename PFP> template <typename PFP>
typename PFP::REAL convexFaceArea(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position); typename PFP::REAL convexFaceArea(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position) ;
template <typename PFP> template <typename PFP>
typename PFP::REAL totalArea(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& select = SelectorTrue(), unsigned int th=0) ; typename PFP::REAL totalArea(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& select = SelectorTrue(), unsigned int th=0) ;
template <typename PFP>
typename PFP::REAL vertexOneRingArea(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position) ;
template <typename PFP>
typename PFP::REAL vertexVoronoiArea(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position) ;
template <typename PFP> template <typename PFP>
void computeAreaFaces(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TREAL& face_area, const FunctorSelect& select = SelectorTrue()) ; void computeAreaFaces(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TREAL& face_area, const FunctorSelect& select = SelectorTrue()) ;
template <typename PFP>
void computeOneRingAreaVertices(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TREAL& vertex_area, const FunctorSelect& select = SelectorTrue()) ;
template <typename PFP>
void computeVoronoiAreaVertices(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TREAL& vertex_area, const FunctorSelect& select = SelectorTrue()) ;
} // namespace Geometry } // namespace Geometry
} // namespace Algo } // namespace Algo
......
...@@ -70,7 +70,7 @@ typename PFP::REAL convexFaceArea(typename PFP::MAP& map, Dart d, const typename ...@@ -70,7 +70,7 @@ typename PFP::REAL convexFaceArea(typename PFP::MAP& map, Dart d, const typename
template <typename PFP> template <typename PFP>
typename PFP::REAL totalArea(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& select, unsigned int th) typename PFP::REAL totalArea(typename PFP::MAP& map, const typename PFP::TVEC3& position, const FunctorSelect& select, unsigned int th)
{ {
float area = 0.0f ; typename PFP::REAL area(0) ;
DartMarker mark(map,th) ; DartMarker mark(map,th) ;
for(Dart d = map.begin(); d != map.end(); map.next(d)) for(Dart d = map.begin(); d != map.end(); map.next(d))
{ {
...@@ -83,6 +83,32 @@ typename PFP::REAL totalArea(typename PFP::MAP& map, const typename PFP::TVEC3& ...@@ -83,6 +83,32 @@ typename PFP::REAL totalArea(typename PFP::MAP& map, const typename PFP::TVEC3&
return area ; return area ;
} }
template <typename PFP>
typename PFP::REAL vertexOneRingArea(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position)
{
typename PFP::REAL area(0) ;
Dart it = d ;
do
{
area += convexFaceArea<PFP>(map, it, position) ;
it = map.alpha1(it) ;
} while(it != d) ;
return area ;
}
template <typename PFP>
typename PFP::REAL vertexVoronoiArea(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position)
{
typename PFP::REAL area(0) ;
Dart it = d ;
do
{
area += convexFaceArea<PFP>(map, it, position) ;
it = map.alpha1(it) ;
} while(it != d) ;
return area ;
}
template <typename PFP> template <typename PFP>
void computeAreaFaces(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TREAL& face_area, const FunctorSelect& select) void computeAreaFaces(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TREAL& face_area, const FunctorSelect& select)
{ {
...@@ -97,6 +123,34 @@ void computeAreaFaces(typename PFP::MAP& map, const typename PFP::TVEC3& positio ...@@ -97,6 +123,34 @@ void computeAreaFaces(typename PFP::MAP& map, const typename PFP::TVEC3& positio
} }
} }
template <typename PFP>
void computeOneRingAreaVertices(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TREAL& vertex_area, const FunctorSelect& select)
{
CellMarker marker(map, VERTEX);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && !marker.isMarked(d))
{
marker.mark(d);
vertex_area[d] = vertexOneRingArea<PFP>(map, d, position) ;
}
}
}
template <typename PFP>
void computeVoronoiAreaVertices(typename PFP::MAP& map, const typename PFP::TVEC3& position, typename PFP::TREAL& vertex_area, const FunctorSelect& select)
{
CellMarker marker(map, VERTEX);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && !marker.isMarked(d))
{
marker.mark(d);
vertex_area[d] = vertexVoronoiArea<PFP>(map, d, position) ;
}
}
}
} // namespace Geometry } // namespace Geometry
} // namespace Algo } // namespace Algo
......
...@@ -40,25 +40,48 @@ namespace Algo ...@@ -40,25 +40,48 @@ namespace Algo
namespace Geometry namespace Geometry
{ {
enum LaplacianType template <typename PFP>
{ typename PFP::VEC3 computeLaplacianTopoVertex(
TOPOLOGICAL typename PFP::MAP& map,
}; Dart d,
const typename PFP::TVEC3& position) ;
template <typename PFP>
typename PFP::VEC3 computeLaplacianCotanVertex(
typename PFP::MAP& map,
Dart d,
const typename PFP::TVEC3& position,
const typename PFP::TREAL& edgeWeight,
const typename PFP::TREAL& vertexArea) ;
template <typename PFP> template <typename PFP>
void computeLaplacianVertices( void computeLaplacianTopoVertices(
typename PFP::MAP& map, typename PFP::MAP& map,
LaplacianType type,
const typename PFP::TVEC3& position, const typename PFP::TVEC3& position,
typename PFP::TVEC3& laplacian, typename PFP::TVEC3& laplacian,
const FunctorSelect& select = SelectorTrue()) ; const FunctorSelect& select = SelectorTrue()) ;
template <typename PFP> template <typename PFP>
void computeLaplacianVertex_Topo( void computeLaplacianCotanVertices(
typename PFP::MAP& map,
const typename PFP::TVEC3& position,
const typename PFP::TREAL& edgeWeight,
const typename PFP::TREAL& vertexArea,
typename PFP::TVEC3& laplacian,
const FunctorSelect& select = SelectorTrue()) ;
template <typename PFP>
typename PFP::REAL computeCotanWeightEdge(
typename PFP::MAP& map, typename PFP::MAP& map,
Dart d, Dart d,
const typename PFP::TVEC3& position) ;
template <typename PFP>
void computeCotanWeightEdges(
typename PFP::MAP& map,
const typename PFP::TVEC3& position, const typename PFP::TVEC3& position,
typename PFP::TVEC3& laplacian) ; typename PFP::TREAL& edgeWeight,
const FunctorSelect& select = SelectorTrue()) ;
} // namespace Geoemtry } // namespace Geoemtry
......
...@@ -34,10 +34,75 @@ namespace Geometry ...@@ -34,10 +34,75 @@ namespace Geometry
{ {
template <typename PFP> template <typename PFP>
void computeLaplacianVertices( typename PFP::VEC3 computeLaplacianTopoVertex(
typename PFP::MAP& map,
Dart d,
const typename PFP::TVEC3& position)
{
typedef typename PFP::VEC3 VEC3 ;
VEC3 l(0) ;
unsigned int val = 0 ;
Dart dd = d ;
do
{
l += vectorOutOfDart<PFP>(map, dd, position) ;
val++ ;
dd = map.alpha1(dd) ;
} while(dd != d) ;
l /= val ;
return l ;
}
template <typename PFP>
typename PFP::VEC3 computeLaplacianCotanVertex(
typename PFP::MAP& map,
Dart d,
const typename PFP::TVEC3& position,
const typename PFP::TREAL& edgeWeight,
const typename PFP::TREAL& vertexArea)
{
typedef typename PFP::VEC3 VEC3 ;
typedef typename PFP::REAL REAL ;
VEC3 l(0) ;
Dart it = d ;
REAL vArea = vertexArea[d] ;
REAL val = 0 ;
do
{
REAL w = edgeWeight[it] / vArea ;
VEC3 v = vectorOutOfDart<PFP>(map, it, position) * w ;
l += v ;
val += w ;
it = map.alpha1(it) ;
} while(it != d) ;
l /= val ;
return l ;
}
template <typename PFP>
void computeLaplacianTopoVertices(
typename PFP::MAP& map,
const typename PFP::TVEC3& position,
typename PFP::TVEC3& laplacian,
const FunctorSelect& select)
{
CellMarker marker(map, VERTEX);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
if(select(d) && !marker.isMarked(d))
{
marker.mark(d);
laplacian[d] = computeLaplacianTopoVertex<PFP>(map, d, position) ;
}
}
}
template <typename PFP>
void computeLaplacianCotanVertices(
typename PFP::MAP& map, typename PFP::MAP& map,
LaplacianType type,
const typename PFP::TVEC3& position, const typename PFP::TVEC3& position,
const typename PFP::TREAL& edgeWeight,
const typename PFP::TREAL& vertexArea,
typename PFP::TVEC3& laplacian, typename PFP::TVEC3& laplacian,
const FunctorSelect& select) const FunctorSelect& select)
{ {
...@@ -47,35 +112,39 @@ void computeLaplacianVertices( ...@@ -47,35 +112,39 @@ void computeLaplacianVertices(
if(select(d) && !marker.isMarked(d)) if(select(d) && !marker.isMarked(d))
{ {
marker.mark(d); marker.mark(d);
switch(type) laplacian[d] = computeLaplacianCotanVertex<PFP>(map, d, position, edgeWeight, vertexArea) ;
{
case TOPOLOGICAL : {
computeLaplacianVertex_Topo<PFP>(map, d, position, laplacian) ;
break ; }
}
} }
} }
} }
template <typename PFP> template <typename PFP>
void computeLaplacianVertex_Topo( typename PFP::REAL computeCotanWeightEdge(
typename PFP::MAP& map, typename PFP::MAP& map,
Dart d, Dart d,
const typename PFP::TVEC3& position)
{
typename PFP::REAL alpha = angle<PFP>(map, map.phi_1(d), map.phi2(map.phi1(d)), position) ;
Dart dd = map.phi2(d) ;
typename PFP::REAL beta = angle<PFP>(map, map.phi_1(dd), map.phi2(map.phi1(dd)), position) ;
return 0.5 * ( 1 / tan(alpha) + 1 / tan(beta) ) ;
}
template <typename PFP>
void computeCotanWeightEdges(
typename PFP::MAP& map,
const typename PFP::TVEC3& position, const typename PFP::TVEC3& position,
typename PFP::TVEC3& laplacian) typename PFP::TREAL& edgeWeight,
const FunctorSelect& select = SelectorTrue())
{ {
typedef typename PFP::VEC3 VEC3 ; CellMarker marker(map, EDGE);
VEC3 l(0) ; for(Dart d = map.begin(); d != map.end(); map.next(d))
unsigned int val = 0 ;
Dart dd = d ;
do
{ {
l += vectorOutOfDart<PFP>(map, dd, position) ; if(select(d) && !marker.isMarked(d))
val++ ; {
dd = map.alpha1(dd) ; marker.mark(d);
} while(dd != d) ; edgeWeight[d] = computeCotanWeightEdge<PFP>(map, d, position) ;
l /= val ; }
laplacian[d] = l ; }
} }
} // namespace Geometry } // namespace Geometry
......
...@@ -50,24 +50,22 @@ typename PFP::VEC3 triangleNormal(typename PFP::MAP& map, Dart d, const typename ...@@ -50,24 +50,22 @@ typename PFP::VEC3 triangleNormal(typename PFP::MAP& map, Dart d, const typename
return N ; return N ;
} }
template<typename PFP> template<typename PFP>
typename PFP::VEC3 newellNormal(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position) typename PFP::VEC3 newellNormal(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& position)
{ {
Dart e=d; Dart e = d;
typename PFP::VEC3 normal(0); typename PFP::VEC3 normal(0);
do do
{ {
const typename PFP::VEC3& P = position[e]; const typename PFP::VEC3& P = position[e];
e = map.phi1(e); e = map.phi1(e);
const typename PFP::VEC3& Q = position[e]; const typename PFP::VEC3& Q = position[e];
normal[0] += (P[1]-Q[1])*(P[2]+Q[2]); normal[0] += (P[1] - Q[1]) * (P[2] + Q[2]);
normal[1] += (P[2]-Q[2])*(P[0]+Q[0]); normal[1] += (P[2] - Q[2]) * (P[0] + Q[0]);
normal[2] += (P[0]-Q[0])*(P[1]+Q[1]); normal[2] += (P[0] - Q[0]) * (P[1] + Q[1]);
}while (e !=d); } while (e != d);
normal.normalize(); normal.normalize();
return normal; return normal;
} }
......
...@@ -31,14 +31,8 @@ namespace CGoGN ...@@ -31,14 +31,8 @@ namespace CGoGN
namespace LinearSolving namespace LinearSolving
{ {
enum LaplacianType
{
TOPOLOGICAL,
COTWEIGHT
};
template<typename PFP, class SOLVER_TRAITS> template<typename PFP, class SOLVER_TRAITS>
class LaplacianTopo : public FunctorMap<typename PFP::MAP> class FunctorLaplacianTopo : public FunctorMap<typename PFP::MAP>
{ {
protected: protected:
LinearSolver<SOLVER_TRAITS>* solver ; LinearSolver<SOLVER_TRAITS>* solver ;
...@@ -46,9 +40,8 @@ protected: ...@@ -46,9 +40,8 @@ protected:
public: public:
typedef typename PFP::MAP MAP ; typedef typename PFP::MAP MAP ;
LaplacianTopo(MAP& m, LinearSolver<SOLVER_TRAITS>* s, const AttributeHandler<unsigned int> index) : FunctorLaplacianTopo(MAP& m, LinearSolver<SOLVER_TRAITS>* s, const AttributeHandler<unsigned int> index) :
FunctorMap<MAP>(m), solver(s), indexTable(index) FunctorMap<MAP>(m), solver(s), indexTable(index)
{} {}
...@@ -72,100 +65,43 @@ public: ...@@ -72,100 +65,43 @@ public:
} }
} ; } ;
/*
template<typename PFP, class SOLVER_TRAITS> template<typename PFP, class SOLVER_TRAITS>
class LaplacianCotWeight : public FunctorMap<typename PFP::MAP> class FunctorLaplacianCotan : public FunctorMap<typename PFP::MAP>
{ {
protected: protected:
LinearSolver<SOLVER_TRAITS>* solver ; LinearSolver<SOLVER_TRAITS>* solver ;
const AttributeHandler<unsigned int>& indexTable ;
const typename PFP::TREAL& edgeWeight ;
const typename PFP::TREAL& vertexArea ;
public: public:
LaplacianCotWeight(MAP& m, LinearSolver<SOLVER_TRAITS>* s) : typedef typename PFP::MAP MAP ;
FunctorMap<MAP>(m), solver(s) typedef typename PFP::REAL REAL ;
{}
// float half_w(Dart d) FunctorLaplacianCotan(MAP& m, LinearSolver<SOLVER_TRAITS>* s, const AttributeHandler<unsigned int> index, const typename PFP::TREAL& eWeight, const typename PFP::TREAL& vArea) :
// { FunctorMap<MAP>(m), solver(s), indexTable(index), edgeWeight(eWeight), vertexArea(vArea)
// Dart d1 = this->m_map.phi2( this->m_map.phi1( d)) ; {}
// gmtl::Vec3f V1 = this->m_map.getVertexEmb(d1)->getPosition() ;
// V1 -= this->m_map.getVertexEmb(this->m_map.phi2(d1))->getPosition() ;
// Dart d2 = this->m_map.phi_1( d) ;
// gmtl::Vec3f V2 = this->m_map.getVertexEmb(d2)->getPosition() ;
// V1 -= this->m_map.getVertexEmb(this->m_map.phi2(d2))->getPosition() ;
// float nV1 = gmtl::lengthSquared(V1) ;
// float nV2 = gmtl::lengthSquared(V2) ;
// float cos_angle = gmtl::dot(V1, V2) / sqrt(nV1 * nV2) ;
// float angle = acos(cos_angle) ;
// return 1.0 / tan(angle) ;
// }
//
// float w(Dart d)
// {
// float result = 0.0f ;
// Dart dd = this->m_map.phi2(d) ;
// result += half_w(d) ;
// result += half_w(dd) ;
// gmtl::Vec3f vec = this->m_map.getVertexEmb(d)->getPosition() ;
// vec -= this->m_map.getVertexEmb(dd)->getPosition() ;
// result *= gmtl::lengthSquared(vec) ;
// return result ;
// }
//
// float one_ring_area(Dart d)
// {
// float result = 0.0 ;
// Dart dd = d ;
// do
// {
// gmtl::Vec3f n ;
// Algo::Geometry::faceNormal<PFP>(this->m_map, d, n) ;
// result += 0.5f * gmtl::length(n) ;
// dd = this->m_map.phi1(this->m_map.phi2(dd)) ;
// } while(dd != d) ;
// return result ;
// }
bool operator()(Dart d) bool operator()(Dart d)
{ {
solver->begin_row() ; solver->begin_row() ;
Dart dd = d ; Dart it = d ;
EMB* e = reinterpret_cast<EMB*>(this->m_map.getVertexEmb(d)) ; REAL vArea = vertexArea[d] ;
unsigned int idx = e->getIndex() ; REAL aii = 0 ;
float area = one_ring_area(d) ;
float aii = 0.0f ;
do do
{ {
float aij = w(dd) / area ; typename PFP::REAL aij = edgeWeight[it] / vArea ;
aii += aij ; aii += aij ;
EMB* ne = reinterpret_cast<EMB*>(this->m_map.getVertexEmb(this->m_map.phi2(dd))) ; solver->add_coefficient(indexTable[this->m_map.phi2(it)], aij) ;
unsigned int neighbour_idx = ne->getIndex() ; it = this->m_map.alpha1(it) ;
this->solver->add_coefficient(neighbour_idx, aij) ; } while(it != d) ;
dd = this->m_map.phi1(this->m_map.phi2(dd)) ; solver->add_coefficient(indexTable[d], -aii) ;
} while(dd != d) ;
solver->add_coefficient(idx, -aii) ;
solver->set_right_hand_side(0.0f) ; solver->set_right_hand_side(0.0f) ;
solver->normalize_row() ; solver->normalize_row() ;
solver->end_row() ; solver->end_row() ;
return false ; return false ;
} }
} ; } ;
*/
template <typename PFP, class SOLVER_TRAITS>
void setupLaplacianMatrix(typename PFP::MAP& m, LinearSolver<SOLVER_TRAITS>* s, LaplacianType lt, const AttributeHandler<unsigned int> index)
{
switch(lt)
{
case TOPOLOGICAL : {
LaplacianTopo<PFP, SOLVER_TRAITS> lt(m, s, index) ;
m.foreach_orbit(VERTEX, lt) ;
break ; }
case COTWEIGHT : {
LaplacianTopo<PFP, SOLVER_TRAITS> lt(m, s, index) ;
m.foreach_orbit(VERTEX, lt) ;
break ; }
}
}
} }
......
...@@ -179,45 +179,30 @@ void startMatrix(LinearSolver<SOLVER_TRAITS>* s) ...@@ -179,45 +179,30 @@ void startMatrix(LinearSolver<SOLVER_TRAITS>* s)
s->begin_system() ; s->begin_system() ;
} }
enum ConstraintType template <typename PFP, class SOLVER_TRAITS>
void addRows_Laplacian_Topo(typename PFP::MAP& m, LinearSolver<SOLVER_TRAITS>* s, const AttributeHandler<unsigned int> index)
{ {
LAPLACIAN_TOPO, FunctorLaplacianTopo<PFP, SOLVER_TRAITS> lt(m, s, index) ;
LAPLACIAN_COTWEIGHT, m.foreach_orbit(VERTEX, lt) ;
EQUALITY }
};
template <typename PFP, class SOLVER_TRAITS>
void addRows_Laplacian_Cotan(typename PFP::MAP& m, LinearSolver<SOLVER_TRAITS>* s, const AttributeHandler<unsigned int> index, const typename PFP::TREAL& edgeWeight, const typename PFP::TREAL& vertexArea)
{
FunctorLaplacianCotan<PFP, SOLVER_TRAITS> lt(m, s, index, edgeWeight, vertexArea) ;
m.foreach_orbit(VERTEX, lt) ;
}
template <typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS> template <typename PFP, typename ATTR_TYPE, class SOLVER_TRAITS>
void addMatrixRows(typename PFP::MAP& m, LinearSolver<SOLVER_TRAITS>* s, const AttributeHandler<ATTR_TYPE>& attr, ConstraintType ct, const AttributeHandler<unsigned int> index, float* params = NULL) void addRows_Equality(typename PFP::MAP& m, LinearSolver<SOLVER_TRAITS>* s, const AttributeHandler<ATTR_TYPE>& attr, const AttributeHandler<unsigned int> index, float amount)
{ {
switch(ct) setupEqualityMatrix<PFP, ATTR_TYPE, SOLVER_TRAITS>(m, s, attr, index, amount) ;
{
case LAPLACIAN_TOPO :
setupLaplacianMatrix<PFP, SOLVER_TRAITS>(m, s, TOPOLOGICAL, index) ;
break ;
case LAPLACIAN_COTWEIGHT :
setupLaplacianMatrix<PFP, SOLVER_TRAITS>(m, s, COTWEIGHT, index) ;
break ;
case EQUALITY :
setupEqualityMatrix<PFP, ATTR_TYPE, SOLVER_TRAITS>(m, s, attr, index, params[0]) ;