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  ImGui::Text("You can use the mouse left-click to select the new source point.");
153 }
154 
155 int main()
156 {
157  auto params = SH3::defaultParameters() | SHG3::defaultParameters() | SHG3::parametersGeometryEstimation();
158  params("surfaceComponents", "All");
159  params("polynomial", "x^2+(y+1)^2+z^2-1.0")
160  ("minAABB",-1.0)("maxAABB",1.0)("offset",1.0)
161  ("gridstep",0.0625);
162  auto shape = SH3::makeImplicitShape3D( params );
163  auto dshape = SH3::makeDigitizedImplicitShape3D( shape, params );
164  auto K = SH3::getKSpace( params );
165  auto binary_image = SH3::makeBinaryImage( dshape, params );
166  auto surface = SH3::makeDigitalSurface( binary_image, K, params );
167  auto primalSurface= SH3::makePrimalSurfaceMesh(surface);
168  auto surfels = SH3::getSurfelRange( surface, params );
169  auto normals = SHG3::getNormalVectors( shape, K, surfels, params );
170 
171  //Need to convert the faces
172  std::vector<std::vector<SH3::SurfaceMesh::Vertex>> faces;
173  std::vector<RealPoint> positions;
174 
175  for(auto face= 0 ; face < primalSurface->nbFaces(); ++face)
176  faces.push_back(primalSurface->incidentVertices( face ));
177 
178  //Recasting to vector of vertices
179  positions = primalSurface->positions();
180 
181  surfmesh = SurfMesh(positions.begin(),
182  positions.end(),
183  faces.begin(),
184  faces.end());
185  surfmesh.setFaceNormals( normals.cbegin(), normals.cend() );
186  std::cout << surfmesh << std::endl;
187  std::cout<<"number of non-manifold Edges = " << surfmesh.computeNonManifoldEdges().size()<<std::endl;
188 
189  // Initialize polyscope
190  polyscope::init();
191 
192  psMesh = polyscope::registerSurfaceMesh("digital surface", positions, faces);
193 
194  // Set the callback function
195  polyscope::state::userCallback = myCallback;
196  polyscope::show();
197  return EXIT_SUCCESS;
198 }
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