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

cotangent weight laplacian matrix

parent 7b93a4a0
......@@ -35,17 +35,29 @@ namespace Geometry
{
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>
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>
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>
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 Algo
......
......@@ -70,7 +70,7 @@ typename PFP::REAL convexFaceArea(typename PFP::MAP& map, Dart d, const typename
template <typename PFP>
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) ;
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&
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>
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
}
}
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 Algo
......
......@@ -40,25 +40,48 @@ namespace Algo
namespace Geometry
{
enum LaplacianType
{
TOPOLOGICAL
};
template <typename PFP>
typename PFP::VEC3 computeLaplacianTopoVertex(
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>
void computeLaplacianVertices(
void computeLaplacianTopoVertices(
typename PFP::MAP& map,
LaplacianType type,
const typename PFP::TVEC3& position,
typename PFP::TVEC3& laplacian,
const FunctorSelect& select = SelectorTrue()) ;
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,
Dart d,
const typename PFP::TVEC3& position) ;
template <typename PFP>
void computeCotanWeightEdges(
typename PFP::MAP& map,
const typename PFP::TVEC3& position,
typename PFP::TVEC3& laplacian) ;
typename PFP::TREAL& edgeWeight,
const FunctorSelect& select = SelectorTrue()) ;
} // namespace Geoemtry
......
......@@ -34,9 +34,54 @@ namespace Geometry
{
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,
LaplacianType type,
const typename PFP::TVEC3& position,
typename PFP::TVEC3& laplacian,
const FunctorSelect& select)
......@@ -47,35 +92,59 @@ void computeLaplacianVertices(
if(select(d) && !marker.isMarked(d))
{
marker.mark(d);
switch(type)
{
case TOPOLOGICAL : {
computeLaplacianVertex_Topo<PFP>(map, d, position, laplacian) ;
break ; }
laplacian[d] = computeLaplacianTopoVertex<PFP>(map, d, position) ;
}
}
}
template <typename PFP>
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)
{
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] = computeLaplacianCotanVertex<PFP>(map, d, position, edgeWeight, vertexArea) ;
}
}
}
template <typename PFP>
void computeLaplacianVertex_Topo(
typename PFP::REAL computeCotanWeightEdge(
typename PFP::MAP& map,
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,
typename PFP::TVEC3& laplacian)
typename PFP::TREAL& edgeWeight,
const FunctorSelect& select = SelectorTrue())
{
typedef typename PFP::VEC3 VEC3 ;
VEC3 l(0) ;
unsigned int val = 0 ;
Dart dd = d ;
do
CellMarker marker(map, EDGE);
for(Dart d = map.begin(); d != map.end(); map.next(d))
{
l += vectorOutOfDart<PFP>(map, dd, position) ;
val++ ;
dd = map.alpha1(dd) ;
} while(dd != d) ;
l /= val ;
laplacian[d] = l ;
if(select(d) && !marker.isMarked(d))
{
marker.mark(d);
edgeWeight[d] = computeCotanWeightEdge<PFP>(map, d, position) ;
}
}
}
} // namespace Geometry
......
......@@ -50,24 +50,22 @@ typename PFP::VEC3 triangleNormal(typename PFP::MAP& map, Dart d, const typename
return N ;
}
template<typename PFP>
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);
do
{
const typename PFP::VEC3& P = position[e];
e = map.phi1(e);
const typename PFP::VEC3& Q = position[e];
normal[0] += (P[1]-Q[1])*(P[2]+Q[2]);
normal[1] += (P[2]-Q[2])*(P[0]+Q[0]);
normal[2] += (P[0]-Q[0])*(P[1]+Q[1]);
}while (e !=d);
normal[0] += (P[1] - Q[1]) * (P[2] + Q[2]);
normal[1] += (P[2] - Q[2]) * (P[0] + Q[0]);
normal[2] += (P[0] - Q[0]) * (P[1] + Q[1]);
} while (e != d);
normal.normalize();
return normal;
}
......
......@@ -31,14 +31,8 @@ namespace CGoGN
namespace LinearSolving
{
enum LaplacianType
{
TOPOLOGICAL,
COTWEIGHT
};
template<typename PFP, class SOLVER_TRAITS>
class LaplacianTopo : public FunctorMap<typename PFP::MAP>
class FunctorLaplacianTopo : public FunctorMap<typename PFP::MAP>
{
protected:
LinearSolver<SOLVER_TRAITS>* solver ;
......@@ -47,8 +41,7 @@ protected:
public:
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)
{}
......@@ -72,100 +65,43 @@ public:
}
} ;
/*
template<typename PFP, class SOLVER_TRAITS>
class LaplacianCotWeight : public FunctorMap<typename PFP::MAP>
class FunctorLaplacianCotan : public FunctorMap<typename PFP::MAP>
{
protected:
LinearSolver<SOLVER_TRAITS>* solver ;
const AttributeHandler<unsigned int>& indexTable ;
const typename PFP::TREAL& edgeWeight ;
const typename PFP::TREAL& vertexArea ;
public:
LaplacianCotWeight(MAP& m, LinearSolver<SOLVER_TRAITS>* s) :
FunctorMap<MAP>(m), solver(s)
{}
typedef typename PFP::MAP MAP ;
typedef typename PFP::REAL REAL ;
// float half_w(Dart d)
// {
// 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 ;
// }
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)
{}
bool operator()(Dart d)
{
solver->begin_row() ;
Dart dd = d ;
EMB* e = reinterpret_cast<EMB*>(this->m_map.getVertexEmb(d)) ;
unsigned int idx = e->getIndex() ;
float area = one_ring_area(d) ;
float aii = 0.0f ;
Dart it = d ;
REAL vArea = vertexArea[d] ;
REAL aii = 0 ;
do
{
float aij = w(dd) / area ;
typename PFP::REAL aij = edgeWeight[it] / vArea ;
aii += aij ;
EMB* ne = reinterpret_cast<EMB*>(this->m_map.getVertexEmb(this->m_map.phi2(dd))) ;
unsigned int neighbour_idx = ne->getIndex() ;
this->solver->add_coefficient(neighbour_idx, aij) ;
dd = this->m_map.phi1(this->m_map.phi2(dd)) ;
} while(dd != d) ;
solver->add_coefficient(idx, -aii) ;
solver->add_coefficient(indexTable[this->m_map.phi2(it)], aij) ;
it = this->m_map.alpha1(it) ;
} while(it != d) ;
solver->add_coefficient(indexTable[d], -aii) ;
solver->set_right_hand_side(0.0f) ;
solver->normalize_row() ;
solver->end_row() ;
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)
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,
LAPLACIAN_COTWEIGHT,
EQUALITY
};
FunctorLaplacianTopo<PFP, SOLVER_TRAITS> lt(m, s, index) ;
m.foreach_orbit(VERTEX, lt) ;
}
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>
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)
{
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]) ;
break ;
}
setupEqualityMatrix<PFP, ATTR_TYPE, SOLVER_TRAITS>(m, s, attr, index, amount) ;
}
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, unsigned int coord, const AttributeHandler<unsigned int> index, float* params = NULL)
void addRows_Equality(typename PFP::MAP& m, LinearSolver<SOLVER_TRAITS>* s, const AttributeHandler<ATTR_TYPE>& attr, unsigned int coord, const AttributeHandler<unsigned int> index, float amount)
{
switch(ct)
{
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, coord, index, params[0]) ;
break ;
}
setupEqualityMatrix<PFP, ATTR_TYPE, SOLVER_TRAITS>(m, s, attr, coord, index, amount) ;
}
template <class SOLVER_TRAITS>
......
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