DGtal 1.3.0
Loading...
Searching...
No Matches
dgtalCalculus-halfsphere.cpp
1
25#include <iostream>
26#include <string>
27#include <DGtal/base/Common.h>
28#include <DGtal/helpers/StdDefs.h>
29#include <DGtal/helpers/Shortcuts.h>
30#include <DGtal/helpers/ShortcutsGeometry.h>
31#include <DGtal/shapes/SurfaceMesh.h>
32
33#include <DGtal/dec/PolygonalCalculus.h>
34#include <DGtal/dec/GeodesicsInHeat.h>
35
36#include <polyscope/polyscope.h>
37#include <polyscope/surface_mesh.h>
38#include <polyscope/pick.h>
39
40#include "ConfigExamples.h"
41
42#include <Eigen/Dense>
43#include <Eigen/Sparse>
44
45using namespace DGtal;
46using namespace Z3i;
47
48// Using standard 3D digital space.
51// The following typedefs are useful
53typedef SurfMesh::Face Face;
56//Polyscope global
57polyscope::SurfaceMesh *psMesh;
58SurfMesh surfmesh;
59float dt = 2.0;
60float Lambda = 0.05;
61bool Mixed = false;
63PolyCalculus *calculus;
64int vertex_idx = -1;
65int face_idx = -1;
66int edge_idx = -1;
67
68
69
70void precompute()
71{
72 auto projEmbedder = [&]( int f, int v)
73 {
74 auto nn = surfmesh.faceNormal( f );
75 RealPoint centroid = surfmesh.faceCentroid( f );
76 RealPoint p = surfmesh.position( v );
77 const auto cp = p - centroid;
78 RealPoint q = p - nn.dot(cp)*nn;
79 return q;
80 };
81 calculus = new PolyCalculus(surfmesh);
82 calculus->setEmbedder( projEmbedder );
83 heat = new GeodesicsInHeat<PolyCalculus>(calculus);
84 trace.beginBlock("Init solvers");
85 heat->init(dt,Lambda,Mixed);
87}
88
89void picksource( int v_idx )
90{
91 heat->addSource( v_idx );
92 GeodesicsInHeat<PolyCalculus>::Vector source = heat->source();
93 psMesh->addVertexScalarQuantity("source", source);
94}
95
96void computeGeodesics()
97{
98 // heat->addSource( 0 ); //Forcing one seed (for screenshots)
99 GeodesicsInHeat<PolyCalculus>::Vector dist = heat->compute();
100 psMesh->addVertexDistanceQuantity("geodesic", dist);
101}
102
103bool isPrecomputed=false;
104void myCallback()
105{
106 // Select a vertex with the mouse
107 if (polyscope::pick::haveSelection()) {
108 bool goodSelection = false;
109 auto selection = polyscope::pick::getSelection();
110 auto selectedSurface = static_cast<polyscope::SurfaceMesh*>(selection.first);
111 size_t idx = selection.second;
112
113 // Only authorize selection on the input surface and the reconstruction
114 auto surf = polyscope::getSurfaceMesh("digital surface");
115 goodSelection = goodSelection || (selectedSurface == surf);
116 const auto nv = selectedSurface->nVertices();
117 // Validate that it its a face index
118 if ( goodSelection && idx < nv )
119 {
120 std::ostringstream otext;
121 otext << "Selected vertex = " << idx;
122 ImGui::Text( "%s", otext.str().c_str() );
123 vertex_idx = idx;
124 if (!isPrecomputed)
125 {
126 precompute();
127 isPrecomputed=true;
128 }
129 picksource( vertex_idx );
130 }
131 }
132
133 ImGui::SliderFloat("Lambda parameter", &Lambda, 0.0, 1.0);
134 ImGui::SliderFloat("dt", &dt, 0.,4.);
135 ImGui::Checkbox("Use mixed Neumann+Dirichlet heat solution", &Mixed);
136 if(ImGui::Button("Precomputation (required if you change the dt)"))
137 {
138 precompute();
139 isPrecomputed=true;
140 }
141
142 if(ImGui::Button("Compute geodesic"))
143 {
144 if (!isPrecomputed)
145 {
146 precompute();
147 isPrecomputed=true;
148 }
149 computeGeodesics();
150 }
151}
152
153int main()
154{
156 params("surfaceComponents", "All");
157 params("polynomial", "x^2+(y+1)^2+z^2-1.0")
158 ("minAABB",-1.0)("maxAABB",1.0)("offset",1.0)
159 ("gridstep",0.0625);
160 auto shape = SH3::makeImplicitShape3D( params );
161 auto dshape = SH3::makeDigitizedImplicitShape3D( shape, params );
162 auto K = SH3::getKSpace( params );
163 auto binary_image = SH3::makeBinaryImage( dshape, params );
164 auto surface = SH3::makeDigitalSurface( binary_image, K, params );
165 auto primalSurface= SH3::makePrimalSurfaceMesh(surface);
166 auto surfels = SH3::getSurfelRange( surface, params );
167 auto normals = SHG3::getNormalVectors( shape, K, surfels, params );
168
169 //Need to convert the faces
170 std::vector<std::vector<SH3::SurfaceMesh::Vertex>> faces;
171 std::vector<RealPoint> positions;
172
173 for(auto face= 0 ; face < primalSurface->nbFaces(); ++face)
174 faces.push_back(primalSurface->incidentVertices( face ));
175
176 //Recasting to vector of vertices
177 positions = primalSurface->positions();
178
179 surfmesh = SurfMesh(positions.begin(),
180 positions.end(),
181 faces.begin(),
182 faces.end());
183 surfmesh.setFaceNormals( normals.cbegin(), normals.cend() );
184 std::cout << surfmesh << std::endl;
185 std::cout<<"number of non-manifold Edges = " << surfmesh.computeNonManifoldEdges().size()<<std::endl;
186
187 // Initialize polyscope
188 polyscope::init();
189
190 psMesh = polyscope::registerSurfaceMesh("digital surface", positions, faces);
191
192 // Set the callback function
193 polyscope::state::userCallback = myCallback;
194 polyscope::show();
195 return EXIT_SUCCESS;
196}
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...
static Parameters parametersGeometryEstimation()
static RealVectors getNormalVectors(CountedPtr< ImplicitShape3D > shape, const KSpace &K, const SurfelRange &surfels, const Parameters &params=parametersShapeGeometry())
static Parameters defaultParameters()
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:105
static KSpace getKSpace(const Point &low, const Point &up, Parameters params=parametersKSpace())
Definition: Shortcuts.h:332
static CountedPtr< DigitizedImplicitShape3D > makeDigitizedImplicitShape3D(CountedPtr< ImplicitShape3D > shape, Parameters params=parametersDigitizedImplicitShape3D())
Definition: Shortcuts.h:523
static SurfelRange getSurfelRange(CountedPtr< ::DGtal::DigitalSurface< TDigitalSurfaceContainer > > surface, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1547
static CountedPtr< DigitalSurface > makeDigitalSurface(CountedPtr< TPointPredicate > bimage, const KSpace &K, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1209
static Parameters defaultParameters()
Definition: Shortcuts.h:203
static CountedPtr< SurfaceMesh > makePrimalSurfaceMesh(Cell2Index &c2i, CountedPtr< ::DGtal::DigitalSurface< TContainer > > aSurface)
Definition: Shortcuts.h:2372
static CountedPtr< BinaryImage > makeBinaryImage(Domain shapeDomain)
Definition: Shortcuts.h:561
static CountedPtr< ImplicitShape3D > makeImplicitShape3D(const Parameters &params=parametersImplicitShape3D())
Definition: Shortcuts.h:282
void beginBlock(const std::string &keyword="")
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
Aim: Represents an embedded mesh as faces and a list of vertices. Vertices may be shared among faces ...
Definition: SurfaceMesh.h:92
RealVector & faceNormal(Face f)
Definition: SurfaceMesh.h:677
RealPoint faceCentroid(Index f) const
bool setFaceNormals(RealVectorIterator itN, RealVectorIterator itNEnd)
RealPoint & position(Vertex v)
Definition: SurfaceMesh.h:637
Edges computeNonManifoldEdges() const
int main(int argc, char **argv)
KSpace K
TriMesh::Face Face
TriMesh::Vertex Vertex