DGtal  1.4.beta
GeodesicsInHeat.h
1 
17 #pragma once
18 
31 #if defined(GeodesicsInHeat_RECURSES)
32 #error Recursive header files inclusion detected in GeodesicsInHeat.h
33 #else // defined(GeodesicsInHeat_RECURSES)
34 
35 #define GeodesicsInHeat_RECURSES
36 
37 #if !defined GeodesicsInHeat_h
38 
39 #define GeodesicsInHeat_h
40 
42 // Inclusions
43 #include <iostream>
44 #include "DGtal/base/Common.h"
45 #include "DGtal/base/ConstAlias.h"
46 #include "DGtal/math/linalg/DirichletConditions.h"
48 
49 namespace DGtal
50 {
52  // template class GeodesicsInHeat
61  template <typename TPolygonalCalculus>
63  {
64  // ----------------------- Standard services ------------------------------
65  public:
66 
67  typedef TPolygonalCalculus PolygonalCalculus;
76 
80  GeodesicsInHeat() = delete;
81 
85  {
86  myIsInit=false;
87  }
88 
92  ~GeodesicsInHeat() = default;
93 
98  GeodesicsInHeat ( const GeodesicsInHeat & other ) = delete;
99 
104  GeodesicsInHeat ( GeodesicsInHeat && other ) = delete;
105 
111  GeodesicsInHeat & operator= ( const GeodesicsInHeat & other ) = delete;
112 
118  GeodesicsInHeat & operator= ( GeodesicsInHeat && other ) = delete;
119 
120 
121 
122  // ----------------------- Interface --------------------------------------
123 
135  void init( double dt, double lambda = 1.0,
136  bool boundary_with_mixed_solution = false )
137  {
138  myIsInit = true;
139  myLambda = lambda;
140 
141  SparseMatrix laplacian = myCalculus->globalLaplaceBeltrami( lambda );
142  SparseMatrix mass = myCalculus->globalLumpedMassMatrix();
143  myHeatOpe = mass - dt*laplacian;
144 
145  //Prefactorizing
146  myPoissonSolver.compute( laplacian );
147  myHeatSolver.compute ( myHeatOpe );
148 
149  //empty source
150  mySource = Vector::Zero(myCalculus->nbVertices());
151 
152  // Manage boundaries
153  myManageBoundary = false;
154  if ( ! boundary_with_mixed_solution ) return;
155  myBoundary = IntegerVector::Zero(myCalculus->nbVertices());
156  const auto surfmesh = myCalculus->getSurfaceMeshPtr();
157  const auto edges = surfmesh->computeManifoldBoundaryEdges();
158  for ( auto e : edges )
159  {
160  const auto vtcs = surfmesh->edgeVertices( e );
161  myBoundary[ vtcs.first ] = 1;
162  myBoundary[ vtcs.second ] = 1;
163  }
164  myManageBoundary = ! edges.empty();
165  if ( ! myManageBoundary ) return;
166  // Prepare solver for a problem with Dirichlet conditions.
168  // Prefactoring
169  myHeatDirichletSolver.compute( heatOpe_d );
170  }
171 
175  void addSource(const Vertex aV)
176  {
177  ASSERT_MSG(aV < myCalculus->nbVertices(), "Vertex is not in the surface mesh vertex range");
178  myLastSourceIndex = aV;
179  mySource( aV ) = 1.0;
180  }
181 
184  void clearSource()
185  {
186  mySource = Vector::Zero(myCalculus->nbVertices());
187  }
188 
192  Vector source() const
193  {
194  FATAL_ERROR_MSG(myIsInit, "init() method must be called first");
195  return mySource;
196  }
197 
198 
201  Vector compute() const
202  {
203  FATAL_ERROR_MSG(myIsInit, "init() method must be called first");
204  //Heat diffusion
205  Vector heatDiffusion = myHeatSolver.solve(mySource);
206  // Take care of boundaries
207  if ( myManageBoundary )
208  {
209  Vector bValues = Vector::Zero( myCalculus->nbVertices() );
211  myBoundary, bValues );
212  Vector bSol = myHeatDirichletSolver.solve( bSources );
213  Vector heatDiffusionDirichlet
214  = Conditions::dirichletSolution( bSol, myBoundary, bValues );
215  heatDiffusion = 0.5 * ( heatDiffusion + heatDiffusionDirichlet );
216  }
217 
218  Vector divergence = Vector::Zero(myCalculus->nbVertices());
219  auto cpt=0;
220  auto surfmesh = myCalculus->getSurfaceMeshPtr();
221 
222  // Heat, normalization and divergence per face
223  for(auto f=0; f< myCalculus->nbFaces(); ++f)
224  {
225  Vector faceHeat( myCalculus->degree(f));
226  cpt=0;
227  auto vertices = surfmesh->incidentVertices(f);
228  for(auto v: vertices)
229  {
230  faceHeat(cpt) = heatDiffusion( v );
231  ++cpt;
232  }
233  // ∇heat / ∣∣∇heat∣∣
234  Vector grad = -myCalculus->gradient(f) * faceHeat;
235  grad.normalize();
236 
237  // div
238  DenseMatrix oneForm = myCalculus->flat(f)*grad;
239  Vector divergenceFace = myCalculus->divergence( f, myLambda ) * oneForm;
240  cpt=0;
241  for(auto v: vertices)
242  {
243  divergence(v) += divergenceFace(cpt);
244  ++cpt;
245  }
246  }
247 
248  // Last Poisson solve
249  Vector distVec = myPoissonSolver.solve(divergence);
250 
251  //Source val
252  auto sourceval = distVec(myLastSourceIndex);
253 
254  //shifting the distances to get 0 at sources
255  return distVec - sourceval*Vector::Ones(myCalculus->nbVertices());
256  }
257 
258 
260  bool isValid() const
261  {
262  return myIsInit && myCalculus->isValid();
263  }
264 
265  // ----------------------- Private --------------------------------------
266 
267  private:
268 
271 
274 
277 
280 
283 
286 
288  bool myIsInit;
289 
291  double myLambda;
292 
296 
299 
302 
303 
304  }; // end of class GeodesicsInHeat
305 } // namespace DGtal
306 
307 // //
309 
310 #endif // !defined GeodesicsInHeat_h
311 
312 #undef GeodesicsInHeat_RECURSES
313 #endif // else defined(GeodesicsInHeat_RECURSES)
DGtal::GeodesicsInHeat::Conditions
DirichletConditions< LinAlgBackend > Conditions
Definition: GeodesicsInHeat.h:74
DGtal::GeodesicsInHeat::myBoundary
IntegerVector myBoundary
The boundary characteristic vector.
Definition: GeodesicsInHeat.h:298
DGtal::GeodesicsInHeat::GeodesicsInHeat
GeodesicsInHeat()=delete
DGtal::ConstAlias
Aim: This class encapsulates its parameter class so that to indicate to the user that the object/poin...
Definition: ConstAlias.h:186
DGtal::GeodesicsInHeat::myCalculus
const PolygonalCalculus * myCalculus
The underlying PolygonalCalculus instance.
Definition: GeodesicsInHeat.h:270
DGtal::GeodesicsInHeat::DenseMatrix
PolygonalCalculus::DenseMatrix DenseMatrix
Definition: GeodesicsInHeat.h:69
DGtal::DirichletConditions::dirichletVector
static DenseVector dirichletVector(const SparseMatrix &A, const DenseVector &b, const IntegerVector &p, const DenseVector &u)
Definition: DirichletConditions.h:170
DGtal::PolygonalCalculus::SparseMatrix
LinAlg::SparseMatrix SparseMatrix
Type of sparse matrix.
Definition: PolygonalCalculus.h:102
DGtal::GeodesicsInHeat::PolygonalCalculus
TPolygonalCalculus PolygonalCalculus
Definition: GeodesicsInHeat.h:67
DGtal::SurfaceMesh::computeManifoldBoundaryEdges
Edges computeManifoldBoundaryEdges() const
DGtal::GeodesicsInHeat::source
Vector source() const
Definition: GeodesicsInHeat.h:192
DGtal::GeodesicsInHeat::myHeatOpe
SparseMatrix myHeatOpe
The operator for heat diffusion.
Definition: GeodesicsInHeat.h:273
DGtal::DirichletConditions::dirichletSolution
static DenseVector dirichletSolution(const DenseVector &xd, const IntegerVector &p, const DenseVector &u)
Definition: DirichletConditions.h:203
DGtal::DirichletConditions::dirichletOperator
static SparseMatrix dirichletOperator(const SparseMatrix &A, const IntegerVector &p)
Definition: DirichletConditions.h:130
DGtal::GeodesicsInHeat::LinAlgBackend
PolygonalCalculus::LinAlg LinAlgBackend
Definition: GeodesicsInHeat.h:73
DGtal::PolygonalCalculus::DenseMatrix
LinAlg::DenseMatrix DenseMatrix
Type of dense matrix.
Definition: PolygonalCalculus.h:100
DGtal::GeodesicsInHeat::mySource
Vector mySource
Source vector.
Definition: GeodesicsInHeat.h:282
laplacian
void laplacian(Shape &shape, const Options &options, std::function< double(const RealPoint3D &)> input_function, std::function< double(const RealPoint3D &)> target_function, int argc, char **argv)
Definition: sphereCotangentLaplaceOperator.cpp:87
DGtal::GeodesicsInHeat::GeodesicsInHeat
GeodesicsInHeat(ConstAlias< PolygonalCalculus > calculus)
Definition: GeodesicsInHeat.h:84
DGtal::GeodesicsInHeat::myManageBoundary
bool myManageBoundary
Definition: GeodesicsInHeat.h:295
DGtal::GeodesicsInHeat::Vertex
PolygonalCalculus::Vertex Vertex
Definition: GeodesicsInHeat.h:72
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::EigenLinearAlgebraBackend
Aim: Provide linear algebra backend using Eigen dense and sparse matrix as well as dense vector....
Definition: EigenSupport.h:96
DGtal::SurfaceMesh::edgeVertices
const VertexPair & edgeVertices(Edge e) const
Definition: SurfaceMesh.h:329
DGtal::GeodesicsInHeat::compute
Vector compute() const
Definition: GeodesicsInHeat.h:201
DGtal::PolygonalCalculus::Solver
LinAlg::SolverSimplicialLDLT Solver
Type of a sparse matrix solver.
Definition: PolygonalCalculus.h:107
DGtal::GeodesicsInHeat::SparseMatrix
PolygonalCalculus::SparseMatrix SparseMatrix
Definition: GeodesicsInHeat.h:68
DGtal::GeodesicsInHeat
This class implements on polygonal surfaces (using Discrete differential calculus on polygonal surfa...
Definition: GeodesicsInHeat.h:62
DGtal::DirichletConditions::IntegerVector
LinearAlgebraBackend::IntegerVector IntegerVector
Definition: DirichletConditions.h:105
DGtal::GeodesicsInHeat::clearSource
void clearSource()
Definition: GeodesicsInHeat.h:184
DGtal::GeodesicsInHeat::addSource
void addSource(const Vertex aV)
Definition: GeodesicsInHeat.h:175
DGtal::GeodesicsInHeat::IntegerVector
Conditions::IntegerVector IntegerVector
Definition: GeodesicsInHeat.h:75
DGtal::GeodesicsInHeat::operator=
GeodesicsInHeat & operator=(const GeodesicsInHeat &other)=delete
DGtal::GeodesicsInHeat::~GeodesicsInHeat
~GeodesicsInHeat()=default
DGtal::PolygonalCalculus::Vertex
MySurfaceMesh::Vertex Vertex
Vertex type.
Definition: PolygonalCalculus.h:85
DGtal::DirichletConditions
Aim: A helper class to solve a system with Dirichlet boundary conditions.
Definition: DirichletConditions.h:97
DGtal::GeodesicsInHeat::myHeatDirichletSolver
Solver myHeatDirichletSolver
Heat solver with Dirichlet boundary conditions.
Definition: GeodesicsInHeat.h:301
DGtal::PolygonalCalculus::Vector
LinAlg::DenseVector Vector
Type of Vector.
Definition: PolygonalCalculus.h:96
DGtal::GeodesicsInHeat::myHeatSolver
Solver myHeatSolver
Heat solver.
Definition: GeodesicsInHeat.h:279
DGtal::GeodesicsInHeat::isValid
bool isValid() const
Definition: GeodesicsInHeat.h:260
DGtal::GeodesicsInHeat::init
void init(double dt, double lambda=1.0, bool boundary_with_mixed_solution=false)
Definition: GeodesicsInHeat.h:135
DGtal::GeodesicsInHeat::myLambda
double myLambda
Lambda parameter.
Definition: GeodesicsInHeat.h:291
DGtal::GeodesicsInHeat::myLastSourceIndex
Vertex myLastSourceIndex
Vertex index to the last source point (to shift the distances)
Definition: GeodesicsInHeat.h:285
DGtal::SurfaceMesh::incidentVertices
const Vertices & incidentVertices(Face f) const
Definition: SurfaceMesh.h:307
DGtal::GeodesicsInHeat::myIsInit
bool myIsInit
Validitate flag.
Definition: GeodesicsInHeat.h:288
DGtal::GeodesicsInHeat::myPoissonSolver
Solver myPoissonSolver
Poisson solver.
Definition: GeodesicsInHeat.h:276
DGtal::GeodesicsInHeat::Solver
PolygonalCalculus::Solver Solver
Definition: GeodesicsInHeat.h:70
DGtal::GeodesicsInHeat::Vector
PolygonalCalculus::Vector Vector
Definition: GeodesicsInHeat.h:71