Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_807_Mesh_Subdivision

Refining a coarse mesh into a smooth surface with Catmull-Clark, Loop, and sqrt(3) subdivision.

Overview

This tutorial demonstrates subdivision surfaces with SurfaceMeshSubdivision. Starting from a coarse cube, each shortcut applies one subdivision step (and can be repeated), turning the faceted control mesh into an increasingly smooth limit surface. Three classic schemes are available: Catmull-Clark (for polygon meshes), Loop and sqrt(3) (for triangle meshes). The edges are drawn so the refinement is visible. Subdivision is the standard way to model smooth organic shapes from a coarse control cage and is widely used in animation and CAD.

The technique

Subdivision repeatedly refines the mesh topology and repositions vertices with weighted averaging rules so that, in the limit, the mesh converges to a smooth surface. Catmull-Clark (Catmull and Clark, 1978) works on arbitrary polygon meshes: each face gains a face point, each edge an edge point, and original vertices are updated; an n-sided face becomes n quads, and the limit surface is bicubic B-spline-like (C2 away from extraordinary vertices). Loop (Loop, 1987) is the triangle analogue: each triangle is split into four by inserting edge midpoints, with weights that approximate a quartic box spline; it is an approximating scheme (the surface generally does not pass through the control vertices). sqrt(3) (Kobbelt, 2000) also operates on triangles but inserts a vertex at each face centroid and flips the old edges, tripling the face count per step (edge length shrinks by sqrt(3)), which gives a slower, more uniform refinement than Loop's factor-of-four. Because Loop and sqrt(3) require triangles, the mesh is triangulated first for those two schemes.

Easy3D API

Running it

Press Ctrl+c for Catmull-Clark, Ctrl+l for Loop, and Ctrl+t for sqrt(3) subdivision; press repeatedly to apply further refinement steps and watch the cube smooth out.

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_lines.h>
32#include <easy3d/algo/surface_mesh_subdivision.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
83using namespace easy3d;
84
85namespace {
86
87 // Apply one subdivision step of the chosen scheme (0 = Catmull-Clark, 1 = Loop, 2 = sqrt(3)).
88 bool subdivide(int scheme, Viewer *viewer, Model *model) {
89 auto mesh = dynamic_cast<SurfaceMesh *>(model);
90 if (!mesh)
91 return false;
92
93 bool ok = false;
94 if (scheme == 0) {
95 // Catmull-Clark works directly on the polygon (quad) mesh.
96 ok = SurfaceMeshSubdivision::catmull_clark(mesh);
97 } else {
98 // Loop and sqrt(3) are triangle schemes, so triangulate the mesh first.
99 mesh->triangulate();
100 ok = (scheme == 1) ? SurfaceMeshSubdivision::loop(mesh) : SurfaceMeshSubdivision::sqrt3(mesh);
101 }
102 if (!ok) {
103 std::cerr << "subdivision failed" << std::endl;
104 return false;
105 }
106 mesh->renderer()->update(); // connectivity changed: refresh the rendering buffers
107 viewer->update(); // request a redraw
108 return true;
109 }
110
111} // namespace
112
113int main(int argc, char **argv) {
114 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
115 initialize();
116
117 // A coarse quad cube: the control cage that subdivision will smooth out.
118 const std::string file = resource::directory() + "/data/quad_mesh/cube_quad.off";
119
120 // Create the viewer and load the mesh (centered/scaled to fit the scene).
121 Viewer viewer(EXAMPLE_TITLE);
122 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file, true));
123 if (!mesh) {
124 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
125 return EXIT_FAILURE;
126 }
127
128 // View the cube from a corner so all three visible faces show the refinement.
129 viewer.camera()->set_view_direction(vec3(1, 1, -1));
130
131 // Show the edges so the refinement is visible.
132 mesh->renderer()->get_lines_drawable("edges")->set_visible(true);
133
134 // Register one shortcut per subdivision scheme and the on-screen help text.
135 viewer.bind_shortcut([](Viewer *v, Model *m) { return subdivide(0, v, m); }, mesh, Viewer::KEY_C, Viewer::MODIF_CTRL);
136 viewer.bind_shortcut([](Viewer *v, Model *m) { return subdivide(1, v, m); }, mesh, Viewer::KEY_L, Viewer::MODIF_CTRL);
137 viewer.bind_shortcut([](Viewer *v, Model *m) { return subdivide(2, v, m); }, mesh, Viewer::KEY_T, Viewer::MODIF_CTRL);
138 viewer.set_usage(
139 "default",
140 "Ctrl + c: Catmull-Clark subdivision\n" "Ctrl + l: Loop subdivision\n" "Ctrl + t: sqrt(3) subdivision");
141
142 // Enter the interactive loop (returns when the window is closed).
143 return viewer.run();
144}
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
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
SurfaceMeshSubdivision, SurfaceMesh, DrawableLines, Tutorial_805_Mesh_Remeshing, Tutorial_806_Mesh_Simplification