DGtal  1.5.beta
dgtalCalculus-geodesic.cpp
Go to the documentation of this file.
1 
26 #include <iostream>
27 #include <string>
28 #include <DGtal/base/Common.h>
29 #include <DGtal/helpers/StdDefs.h>
30 #include <DGtal/helpers/Shortcuts.h>
31 #include <DGtal/helpers/ShortcutsGeometry.h>
32 #include <DGtal/shapes/SurfaceMesh.h>
33 #include <DGtal/geometry/surfaces/DigitalSurfaceRegularization.h>
34 
35 #include <DGtal/dec/PolygonalCalculus.h>
36 #include <DGtal/dec/GeodesicsInHeat.h>
37 
38 #include <polyscope/polyscope.h>
39 #include <polyscope/surface_mesh.h>
40 #include <polyscope/point_cloud.h>
41 
42 #include "ConfigExamples.h"
43 
44 #include <Eigen/Dense>
45 #include <Eigen/Sparse>
46 
47 using namespace DGtal;
48 using namespace Z3i;
49 
50 // Using standard 3D digital space.
53 // The following typedefs are useful
58 //Polyscope global
59 polyscope::SurfaceMesh *psMesh;
60 polyscope::SurfaceMesh *psMeshReg;
63 float dt = 2.0;
64 
67 
68 
71 
74 
76 
78 float radiusII = 3.0;
79 
80 bool skipReg = true; //Global flag to enable/disable the regularization example.
81 bool useProjectedCalculus = true; //Use estimated normal vectors to set up te embedding
82 
83 void precompute()
84 {
85 
88  else
89  {
90  //Using the projection embedder
91  auto params2 = SH3::defaultParameters() | SHG3::defaultParameters() | SHG3::parametersGeometryEstimation();
92  params2("r-radius", (double) radiusII);
93  auto surfels = SH3::getSurfelRange( surface, params2 );
94  iinormals = SHG3::getIINormalVectors(binary_image, surfels,params2);
95  trace.info()<<iinormals.size()<<std::endl;
96  psMesh->addFaceVectorQuantity("II normals", iinormals);
97 
100  calculus->setEmbedder( embedderFromNormals );
101  }
102 
104 
105  if (!skipReg)
106  {
109  }
110  trace.beginBlock("Init solvers");
111  heat->init(dt);
112  if (!skipReg)
113  heatReg->init(dt);
114  trace.endBlock();
115 }
116 
117 
118 
119 void addSource()
120 {
121  auto pos =rand() % surfmesh.nbVertices();
122  heat->addSource( pos );
123  GeodesicsInHeat<PolyCalculus>::Vector source = heat->source();
124  psMesh->addVertexScalarQuantity("Sources", source);
125 
126  if (!skipReg)
127  {
128  heatReg->addSource( pos );
130  psMeshReg->addVertexScalarQuantity("Sources", source);
131  }
132 }
133 
135 {
136  heat->clearSource();
137  psMesh->addVertexScalarQuantity("source", heat->source());
138 }
139 
141 {
142  heat->addSource( sourceVertexId ); //Forcing one seed (for screenshots)
143  GeodesicsInHeat<PolyCalculus>::Vector source = heat->source();
144  psMesh->addVertexScalarQuantity("Sources", source);
146  psMesh->addVertexDistanceQuantity("geodesic", dist);
147 
148  if (!skipReg)
149  {
150  heatReg->addSource( sourceVertexId ); //Forcing one seed (for screenshots)371672
151  GeodesicsInHeat<PolyCalculus>::Vector sourceReg = heatReg->source();
152  psMeshReg->addVertexScalarQuantity("Sources", sourceReg);
154  psMeshReg->addVertexDistanceQuantity("geodesic", dist);
155  }
156 }
157 
158 bool isPrecomputed=false;
160 {
161  ImGui::SliderFloat("dt", &dt, 0.,4.);
162  ImGui::SliderFloat("ii radius for normal vector estimation", &radiusII , 0.,10.);
163  ImGui::Checkbox("Skip regularization", &skipReg);
164  ImGui::Checkbox("Using projection", &useProjectedCalculus);
165  ImGui::InputInt("Index of the first source vertex", &sourceVertexId);
166 
167 
168  if(ImGui::Button("Precomputation (required if you change parameters)"))
169  {
170  precompute();
171  isPrecomputed=true;
172  }
173  ImGui::Separator();
174  if(ImGui::Button("Add a random source"))
175  {
176  if (!isPrecomputed)
177  {
178  precompute();
179  isPrecomputed=true;
180  }
181  addSource();
182  }
183  if(ImGui::Button("Clear sources"))
184  {
185  if (!isPrecomputed)
186  {
187  precompute();
188  isPrecomputed=true;
189  }
190  clearSources();
191  }
192 
193  if(ImGui::Button("Compute geodesic"))
194  {
195  if (!isPrecomputed)
196  {
197  precompute();
198  isPrecomputed=true;
199  }
201  }
202 }
203 
204 int main()
205 {
206  auto params = SH3::defaultParameters() | SHG3::defaultParameters() | SHG3::parametersGeometryEstimation();
207  params("surfaceComponents", "All");
208  params("r-radius", (double) radiusII);
209  std::string filename = examplesPath + std::string("/samples/bunny-128.vol");
210  binary_image = SH3::makeBinaryImage(filename, params );
211  auto K = SH3::getKSpace( binary_image, params );
212  surface = SH3::makeDigitalSurface( binary_image, K, params );
213  auto primalSurface = SH3::makePrimalSurfaceMesh(surface);
214 
215  //Need to convert the faces
216  std::vector<std::vector<SH3::SurfaceMesh::Vertex>> faces;
217  std::vector<RealPoint> positions;
218 
219  for(auto face= 0 ; face < primalSurface->nbFaces(); ++face)
220  faces.push_back(primalSurface->incidentVertices( face ));
221 
222  //Recasting to vector of vertices
223  positions = primalSurface->positions();
224 
225  surfmesh = SurfMesh(positions.begin(),
226  positions.end(),
227  faces.begin(),
228  faces.end());
229  std::cout << surfmesh << std::endl;
230  std::cout<<"number of non-manifold Edges = " << surfmesh.computeNonManifoldEdges().size()<<std::endl;
231 
232  //Construction of a regularized surface
234  regul.init();
236  regul.regularize();
237  auto regularizedPosition = regul.getRegularizedPositions();
238 
239  surfmeshReg = SurfMesh(regularizedPosition.begin(),
240  regularizedPosition.end(),
241  faces.begin(),
242  faces.end());
243 
244  // Initialize polyscope
245  polyscope::init();
246 
247  psMesh = polyscope::registerSurfaceMesh("digital surface", positions, faces);
248  psMeshReg = polyscope::registerSurfaceMesh("regularized surface", regularizedPosition, faces);
249  psMeshReg->setEnabled(false);
250 
251  // Set the callback function
252  polyscope::state::userCallback = myCallback;
253  polyscope::show();
254  return EXIT_SUCCESS;
255 }
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:80
Aim: Implements Digital Surface Regularization as described in .
void init(const double alpha=0.001, const double beta=1.0, const double gamma=0.05)
Initialize the parameters of the energy function.
void attachConvolvedTrivialNormalVectors(const Parameters someParams=SH3::defaultParameters()|SHG3::defaultParameters())
double regularize(const unsigned int nbIters, const double dt, const double epsilon, const AdvectionFunction &advectionFunc)
Main regularization loop.
This class implements on polygonal surfaces (using Discrete differential calculus on polygonal surfa...
PolygonalCalculus::Vector Vector
Implements differential operators on polygonal surfaces from .
void setEmbedder(const std::function< Real3dPoint(Face, Vertex)> &externalFunctor)
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
std::vector< RealVector > RealVectors
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:105
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
void addSource()
PolyCalculus * calculusReg
void precompute()
SurfaceMesh< RealPoint, RealVector > SurfMesh
bool useProjectedCalculus
bool skipReg
GeodesicsInHeat< PolyCalculus > * heatReg
PolyCalculus * calculus
void clearSources()
SurfMesh::Face Face
CountedPtr< SH3::DigitalSurface > surface
bool isPrecomputed
float radiusII
PolygonalCalculus< SH3::RealPoint, SH3::RealVector > PolyCalculus
void computeGeodesics()
CountedPtr< SH3::BinaryImage > binary_image
int sourceVertexId
void myCallback()
polyscope::SurfaceMesh * psMesh
SurfMesh::Vertex Vertex
Shortcuts< Z3i::KSpace > SH3
SurfMesh surfmesh
ShortcutsGeometry< Z3i::KSpace > SHG3
SHG3::RealVectors iinormals
polyscope::SurfaceMesh * psMeshReg
GeodesicsInHeat< PolyCalculus > * heat
SurfMesh surfmeshReg
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Aim: Represents an embedded mesh as faces and a list of vertices. Vertices may be shared among faces ...
Definition: SurfaceMesh.h:92
Size nbVertices() const
Definition: SurfaceMesh.h:288
Edges computeNonManifoldEdges() const
Functor that projects a face vertex of a surface mesh onto the tangent plane given by a per-face norm...
K init(Point(0, 0, 0), Point(512, 512, 512), true)
KSpace K