DGtal  1.5.beta
dgtalCalculus-halfsphere.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 
34 #include <DGtal/dec/PolygonalCalculus.h>
35 #include <DGtal/dec/GeodesicsInHeat.h>
36 
37 #include <polyscope/polyscope.h>
38 #include <polyscope/surface_mesh.h>
39 #include <polyscope/pick.h>
40 
41 #include "ConfigExamples.h"
42 
43 #include <Eigen/Dense>
44 #include <Eigen/Sparse>
45 
46 using namespace DGtal;
47 using namespace Z3i;
48 
49 // Using standard 3D digital space.
52 // The following typedefs are useful
57 //Polyscope global
58 polyscope::SurfaceMesh *psMesh;
60 float dt = 2.0;
61 float Lambda = 0.05;
62 bool Mixed = false;
65 int vertex_idx = -1;
66 int face_idx = -1;
67 int edge_idx = -1;
68 
69 
70 
71 void precompute()
72 {
73  auto projEmbedder = [&]( int f, int v)
74  {
75  auto nn = surfmesh.faceNormal( f );
76  RealPoint centroid = surfmesh.faceCentroid( f );
77  RealPoint p = surfmesh.position( v );
78  const auto cp = p - centroid;
79  RealPoint q = p - nn.dot(cp)*nn;
80  return q;
81  };
83  calculus->setEmbedder( projEmbedder );
85  trace.beginBlock("Init solvers");
86  heat->init(dt,Lambda,Mixed);
87  trace.endBlock();
88 }
89 
90 void picksource( int v_idx )
91 {
92  heat->addSource( v_idx );
94  psMesh->addVertexScalarQuantity("source", source);
95 }
96 
98 {
99  // heat->addSource( 0 ); //Forcing one seed (for screenshots)
101  psMesh->addVertexDistanceQuantity("geodesic", dist);
102 }
103 
104 bool isPrecomputed=false;
106 {
107  // Select a vertex with the mouse
108  if (polyscope::pick::haveSelection()) {
109  bool goodSelection = false;
110  auto selection = polyscope::pick::getSelection();
111  auto selectedSurface = static_cast<polyscope::SurfaceMesh*>(selection.first);
112  size_t idx = selection.second;
113 
114  // Only authorize selection on the input surface and the reconstruction
115  auto surf = polyscope::getSurfaceMesh("digital surface");
116  goodSelection = goodSelection || (selectedSurface == surf);
117  const auto nv = selectedSurface->nVertices();
118  // Validate that it its a face index
119  if ( goodSelection && idx < nv )
120  {
121  std::ostringstream otext;
122  otext << "Selected vertex = " << idx;
123  ImGui::Text( "%s", otext.str().c_str() );
124  vertex_idx = idx;
125  if (!isPrecomputed)
126  {
127  precompute();
128  isPrecomputed=true;
129  }
131  }
132  }
133 
134  ImGui::SliderFloat("Lambda parameter", &Lambda, 0.0, 1.0);
135  ImGui::SliderFloat("dt", &dt, 0.,4.);
136  ImGui::Checkbox("Use mixed Neumann+Dirichlet heat solution", &Mixed);
137  if(ImGui::Button("Precomputation (required if you change the dt)"))
138  {
139  precompute();
140  isPrecomputed=true;
141  }
142 
143  if(ImGui::Button("Compute geodesic"))
144  {
145  if (!isPrecomputed)
146  {
147  precompute();
148  isPrecomputed=true;
149  }
151  }
152 }
153 
154 int main()
155 {
156  auto params = SH3::defaultParameters() | SHG3::defaultParameters() | SHG3::parametersGeometryEstimation();
157  params("surfaceComponents", "All");
158  params("polynomial", "x^2+(y+1)^2+z^2-1.0")
159  ("minAABB",-1.0)("maxAABB",1.0)("offset",1.0)
160  ("gridstep",0.0625);
161  auto shape = SH3::makeImplicitShape3D( params );
162  auto dshape = SH3::makeDigitizedImplicitShape3D( shape, params );
163  auto K = SH3::getKSpace( params );
164  auto binary_image = SH3::makeBinaryImage( dshape, params );
165  auto surface = SH3::makeDigitalSurface( binary_image, K, params );
166  auto primalSurface= SH3::makePrimalSurfaceMesh(surface);
167  auto surfels = SH3::getSurfelRange( surface, params );
168  auto normals = SHG3::getNormalVectors( shape, K, surfels, params );
169 
170  //Need to convert the faces
171  std::vector<std::vector<SH3::SurfaceMesh::Vertex>> faces;
172  std::vector<RealPoint> positions;
173 
174  for(auto face= 0 ; face < primalSurface->nbFaces(); ++face)
175  faces.push_back(primalSurface->incidentVertices( face ));
176 
177  //Recasting to vector of vertices
178  positions = primalSurface->positions();
179 
180  surfmesh = SurfMesh(positions.begin(),
181  positions.end(),
182  faces.begin(),
183  faces.end());
184  surfmesh.setFaceNormals( normals.cbegin(), normals.cend() );
185  std::cout << surfmesh << std::endl;
186  std::cout<<"number of non-manifold Edges = " << surfmesh.computeNonManifoldEdges().size()<<std::endl;
187 
188  // Initialize polyscope
189  polyscope::init();
190 
191  psMesh = polyscope::registerSurfaceMesh("digital surface", positions, faces);
192 
193  // Set the callback function
194  polyscope::state::userCallback = myCallback;
195  polyscope::show();
196  return EXIT_SUCCESS;
197 }
This class implements on polygonal surfaces (using Discrete differential calculus on polygonal surfa...
PolygonalCalculus::Vector Vector
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
auto dot(const PointVector< dim, OtherComponent, OtherStorage > &v) const -> decltype(DGtal::dotProduct(*this, v))
Dot product with a PointVector.
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...
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="")
double endBlock()
CountedPtr< SH3::DigitalSurface > surface
CountedPtr< SH3::BinaryImage > binary_image
void precompute()
SurfaceMesh< RealPoint, RealVector > SurfMesh
PolyCalculus * calculus
SurfMesh::Face Face
bool isPrecomputed
PolygonalCalculus< SH3::RealPoint, SH3::RealVector > PolyCalculus
void computeGeodesics()
void myCallback()
polyscope::SurfaceMesh * psMesh
SurfMesh::Vertex Vertex
void picksource(int v_idx)
Shortcuts< Z3i::KSpace > SH3
SurfMesh surfmesh
ShortcutsGeometry< Z3i::KSpace > SHG3
GeodesicsInHeat< PolyCalculus > * heat
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
RealPoint & position(Vertex v)
Definition: SurfaceMesh.h:647
RealPoint faceCentroid(Index f) const
RealVector & faceNormal(Face f)
Definition: SurfaceMesh.h:687
bool setFaceNormals(RealVectorIterator itN, RealVectorIterator itNEnd)
Edges computeNonManifoldEdges() const
K init(Point(0, 0, 0), Point(512, 512, 512), true)
KSpace K