Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_805_Mesh_Remeshing

Improving triangle quality with uniform and curvature-adaptive incremental remeshing.

Overview

This tutorial demonstrates surface remeshing with SurfaceMeshRemeshing. Remeshing rebuilds the triangulation of a surface so that the triangles are well-shaped and appropriately sized, while staying close to the original geometry. Two schemes are shown on the bunny: uniform remeshing toward a single target edge length, and adaptive remeshing that puts smaller triangles where the surface curves sharply. The edges are drawn so the new triangulation is visible. Remeshing is a key preprocessing step for simulation, deformation, and multiresolution modeling, where poor or uneven triangles cause artifacts.

The technique

Easy3D uses incremental remeshing (Botsch and Kobbelt, "A remeshing approach to multiresolution modeling", SGP 2004): it repeatedly applies four local operations — split edges that are too long, collapse edges that are too short, flip edges to improve valence/shape (toward the Delaunay-like optimum), and relax vertices tangentially to equalize triangle areas — followed by back-projection onto the original surface so the shape is preserved. Uniform remeshing drives every edge toward one target length, giving an isotropic mesh. Adaptive remeshing (Dunyach et al., "Adaptive remeshing for real-time mesh deformation", EG 2013) instead uses a curvature-based sizing field: the local target edge length shrinks where curvature is high, bounded by a minimum/maximum edge length and a maximum approximation error, so detail is concentrated where it matters and flat regions stay coarse.

Easy3D API

Running it

Press Ctrl+u for uniform remeshing (target = 1.5 x the current average edge length) and Ctrl+a for adaptive remeshing; the console prints the resulting vertex/face counts after each operation.

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/drawable_lines.h>
31#include <easy3d/renderer/renderer.h>
32#include <easy3d/algo/surface_mesh_remeshing.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
83using namespace easy3d;
84
85namespace {
86
87 // Compute the mean edge length of the mesh; used to derive a scale-relative target for uniform
88 // remeshing so the result is neither too coarse nor too fine for this particular model.
89 float average_edge_length(const SurfaceMesh *mesh) {
90 float len = 0.0f;
91 for (auto e : mesh->edges())
92 len += distance(mesh->position(mesh->vertex(e, 0)), mesh->position(mesh->vertex(e, 1)));
93 return mesh->n_edges() > 0 ? len / static_cast<float>(mesh->n_edges()) : 0.0f;
94 }
95
96 // Shortcut handler: isotropic remeshing toward 1.5 x the current average edge length.
97 bool uniform_remesh(Viewer *viewer, Model *model) {
98 auto mesh = dynamic_cast<SurfaceMesh *>(model);
99 if (!mesh)
100 return false;
101 const float target = average_edge_length(mesh) * 1.5f;
102 SurfaceMeshRemeshing remeshing(mesh);
103 remeshing.uniform_remeshing(target); // split/collapse/flip/relax + back-projection
104 std::cout << "uniform remeshing: " << mesh->n_vertices() << " vertices / " << mesh->n_faces() << " faces"
105 << std::endl;
106 mesh->renderer()->update(); // connectivity changed: refresh the rendering buffers
107 viewer->update(); // request a redraw
108 return true;
109 }
110
111 // Shortcut handler: curvature-adaptive remeshing. The three lengths are fractions of the
112 // bounding-box diagonal: minimum edge length, maximum edge length, and max approximation error.
113 bool adaptive_remesh(Viewer *viewer, Model *model) {
114 auto mesh = dynamic_cast<SurfaceMesh *>(model);
115 if (!mesh)
116 return false;
117 const float bb = mesh->bounding_box().diagonal_length();
118 SurfaceMeshRemeshing remeshing(mesh);
119 remeshing.adaptive_remeshing(0.001f * bb, 0.100f * bb, 0.001f * bb);
120 std::cout << "adaptive remeshing: " << mesh->n_vertices() << " vertices / " << mesh->n_faces() << " faces"
121 << std::endl;
122 mesh->renderer()->update(); // connectivity changed: refresh the rendering buffers
123 viewer->update();
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 const std::string file = resource::directory() + "/data/bunny.ply";
134
135 // Create the viewer and load the mesh (centered/scaled to fit the scene).
136 Viewer viewer(EXAMPLE_TITLE);
137 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file, true));
138 if (!mesh) {
139 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
140 return EXIT_FAILURE;
141 }
142
143 viewer.camera()->set_view_direction(vec3(0, 1, 0));
144
145 // Show the edges so the new triangulation is visible.
146 mesh->renderer()->get_lines_drawable("edges")->set_visible(true);
147
148 // Register the remeshing shortcuts and the on-screen help text.
149 viewer.bind_shortcut([](Viewer *v, Model *m) { return uniform_remesh(v, m); }, mesh, Viewer::KEY_U, Viewer::MODIF_CTRL);
150 viewer.bind_shortcut([](Viewer *v, Model *m) { return adaptive_remesh(v, m); }, mesh, Viewer::KEY_A, Viewer::MODIF_CTRL);
151 viewer.set_usage(
152 "default",
153 "Ctrl + u: uniform remeshing (target = 1.5 x average edge length)\n" "Ctrl + a: adaptive remeshing (curvature-driven)");
154
155 // Enter the interactive loop (returns when the window is closed).
156 return viewer.run();
157}
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
A class for uniform and adaptive surface remeshing.
Definition surface_mesh_remeshing.h:31
void adaptive_remeshing(float min_edge_length, float max_edge_length, float approx_error, unsigned int iterations=10, bool use_projection=true)
Perform adaptive remeshing.
Definition surface_mesh_remeshing.cpp:70
void uniform_remeshing(float edge_length, unsigned int iterations=10, bool use_projection=true)
Perform uniform remeshing.
Definition surface_mesh_remeshing.cpp:37
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
SurfaceMeshRemeshing, SurfaceMesh, DrawableLines, Tutorial_806_Mesh_Simplification, Tutorial_807_Mesh_Subdivision