Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_801_Mesh_Curvature

Estimating per-vertex curvature on a triangle mesh and visualizing it as a color-mapped scalar field.

Overview

This tutorial is the first of the surface-mesh geometry-processing series. It loads the bunny triangle mesh, estimates discrete curvature at every vertex with SurfaceMeshCurvature, and paints the result on the surface with a colormap. Three scalar fields are available (mean, Gaussian, and maximum absolute curvature) and can be switched interactively. Curvature is a fundamental shape descriptor: it tells you where a surface is flat, where it bends, and where it has sharp features, and it drives many downstream algorithms (smoothing, remeshing, feature detection).

The technique

On a smooth surface, curvature describes how fast the surface turns. At each point there are two principal curvatures k1 and k2 (the extreme bending rates over all tangent directions); from them we form the mean curvature H = (k1 + k2) / 2 and the Gaussian curvature K = k1 * k2. H is zero on minimal (soap-film-like) surfaces and measures average bending; K is positive on dome-like (elliptic) regions, negative on saddle-like (hyperbolic) regions, and zero on flat or developable regions — it is an intrinsic measure tied to the angle defect of the surface. A triangle mesh has no smooth derivatives, so curvature must be estimated discretely from the local neighborhood. Easy3D follows the discrete differential-geometry operators of Meyer et al. (2003, "Discrete Differential-Geometry Operators for Triangulated 2-Manifolds") and the normal-cycle / restricted-Delaunay approach of Cohen-Steiner and Morvan (2003): principal curvatures are recovered per vertex from the surrounding faces/edges, boundary vertices are interpolated from their interior neighbors, and a few smoothing iterations suppress estimation noise. The maximum absolute curvature max(|k1|, |k2|) highlights the strongest bending regardless of sign, which is useful for spotting creases and corners.

Easy3D API

Running it

The mean curvature is shown first. Press Ctrl+e for mean curvature, Ctrl+g for Gaussian curvature, and Ctrl+k for maximum absolute curvature; the surface is recolored each time.

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/algo/surface_mesh_curvature.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
87using namespace easy3d;
88
89namespace {
90
91 // Recompute the requested curvature field and recolor the mesh with it.
92 // Bound to the Ctrl shortcuts below; returns false if the model is not a surface mesh.
93 bool apply_curvature(int type, Viewer *viewer, Model *model) {
94 // The viewer stores models as generic Model pointers; curvature needs a SurfaceMesh.
95 auto mesh = dynamic_cast<SurfaceMesh *>(model);
96 if (!mesh)
97 return false;
98
99 // Estimate the principal curvatures k1/k2 at every vertex. The argument (2) asks for a
100 // couple of post-smoothing iterations so the discrete estimate is less noisy.
101 SurfaceMeshCurvature curvature(mesh);
102 curvature.analyze(2); // a few post-smoothing steps
103
104 // Derive the requested scalar field from the principal curvatures and remember the name of
105 // the vertex property it is stored in (these names are fixed by SurfaceMeshCurvature).
106 std::string property;
107 if (type == 0) {
108 curvature.compute_mean_curvature(); // H = (k1 + k2) / 2
109 property = "v:curv-mean";
110 } else if (type == 1) {
111 curvature.compute_gauss_curvature(); // K = k1 * k2
112 property = "v:curv-gauss";
113 } else {
114 curvature.compute_max_abs_curvature(); // max(|k1|, |k2|)
115 property = "v:curv-max_abs";
116 }
117
118 // Color the surface by the scalar field: "faces" is the mesh's standard triangles drawable,
119 // and set_scalar_coloring() maps the per-vertex property through a colormap.
120 auto drawable = mesh->renderer()->get_triangles_drawable("faces");
121 drawable->set_scalar_coloring(State::VERTEX, property);
122 mesh->renderer()->update(); // refresh portable geometry snapshots from the updated mesh/properties
123 viewer->update(); // request a redraw so the new colors appear
124 return true;
125 }
126
127} // namespace
128
129int main(int argc, char **argv) {
130 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
131 initialize();
132
133 // The bunny is a classic closed triangle mesh with rich curvature variation.
134 const std::string file = resource::directory() + "/data/bunny.ply";
135
136 // Create the viewer and load the mesh; add_model() centers/scales it to fit the scene (true).
137 Viewer viewer(EXAMPLE_TITLE);
138 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file, true));
139 if (!mesh) {
140 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
141 return EXIT_FAILURE;
142 }
143
144 // Look at the bunny from the front so the curvature pattern is easy to read.
145 viewer.camera()->set_view_direction(vec3(0, 1, 0));
146
147 // Show the mean curvature initially.
148 apply_curvature(0, &viewer, mesh);
149
150 // Bind one shortcut per curvature field; each lambda forwards to apply_curvature() with the
151 // matching type. set_usage() documents the keys in the viewer's help overlay.
152 viewer.bind_shortcut([](Viewer *v, Model *m) { return apply_curvature(0, v, m); }, mesh, Viewer::KEY_E, Viewer::MODIF_CTRL);
153 viewer.bind_shortcut([](Viewer *v, Model *m) { return apply_curvature(1, v, m); }, mesh, Viewer::KEY_G, Viewer::MODIF_CTRL);
154 viewer.bind_shortcut([](Viewer *v, Model *m) { return apply_curvature(2, v, m); }, mesh, Viewer::KEY_K, Viewer::MODIF_CTRL);
155 viewer.set_usage(
156 "default",
157 "Ctrl + e: mean curvature\n" "Ctrl + g: Gaussian curvature\n" "Ctrl + k: maximum absolute curvature");
158
159 // Enter the interactive loop (returns when the window is closed).
160 return viewer.run();
161}
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
Compute per-vertex curvatures, i.e., principal (min, max), mean, Gaussian.
Definition surface_mesh_curvature.h:29
void compute_gauss_curvature()
Definition surface_mesh_curvature.cpp:323
void compute_max_abs_curvature()
Computes the maximum absolute curvature.
Definition surface_mesh_curvature.cpp:331
void compute_mean_curvature()
Definition surface_mesh_curvature.cpp:313
void analyze(unsigned int post_smoothing_steps=0)
Computes principal curvature information for each vertex.
Definition surface_mesh_curvature.cpp:30
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
SurfaceMeshCurvature, SurfaceMesh, DrawableTriangles, Tutorial_802_Mesh_Smoothing