Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_808_Mesh_Parameterization

Computing a 2D texture parameterization of an open surface and visualizing it with a checkerboard.

Overview

This tutorial demonstrates surface parameterization with SurfaceMeshParameterization. It computes a (u, v) coordinate for every vertex of the girl_face mesh — an open surface patch (a scanned face) with a boundary — and visualizes the result by mapping a checkerboard texture onto the surface. Two methods are available: discrete harmonic parameterization and least squares conformal mapping (LSCM). Parameterization is the foundation of texture mapping: it flattens a 3D surface into the 2D domain where images live, so a texture can be painted or stored without seams or excessive distortion.

The technique

A parameterization is a map from the surface to the plane; the goal is to minimize distortion (of angles and/or areas) while keeping the map bijective. Both methods here require a mesh with a boundary (disk topology) and store the resulting 2D coordinates in the "v:texcoord" vertex property. Discrete harmonic parameterization (Desbrun et al., "Intrinsic parameterizations of surface meshes", CGF 2002) fixes the boundary to a convex polygon (the unit circle) and solves a Laplace equation for the interior vertices, minimizing the Dirichlet (stretching) energy; it is simple and guaranteed flip-free for a convex boundary, but can distort areas. LSCM (Lévy et al., "Least squares conformal maps for automatic texture atlas generation", SIGGRAPH 2002) instead pins only two boundary vertices and solves a least- squares system enforcing the Cauchy-Riemann equations, producing a conformal (angle-preserving) map with free boundaries; it preserves local shapes well but may stretch areas. The checkerboard makes the distortion visible: square, evenly sized cells indicate a good parameterization, while sheared or stretched cells reveal angle/area distortion.

Easy3D API

Running it

The harmonic parameterization is shown first. Press Ctrl+a for harmonic parameterization and Ctrl+l for LSCM; compare how the checkerboard cells deform under each method.

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_parameterization.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
85using namespace easy3d;
86
87namespace {
88
89 bool parameterize(int method, Viewer *viewer, Model *model) {
90 auto mesh = dynamic_cast<SurfaceMesh *>(model);
91 if (!mesh)
92 return false;
93
94 SurfaceMeshParameterization parameterization(mesh);
95 if (method == 0)
96 parameterization.harmonic();
97 else {
98 mesh->triangulate(); // LSCM requires a triangle mesh
99 parameterization.lscm();
100 }
101
102 auto drawable = mesh->renderer()->get_triangles_drawable("faces");
103 drawable->set_texture_coloring(State::VERTEX, "v:texcoord");
104 mesh->renderer()->update();
105 viewer->update();
106 return true;
107 }
108
109} // namespace
110
111int main(int argc, char **argv) {
112 initialize();
113
114 const std::string file = resource::directory() + "/data/girl_face.ply";
115
116 Viewer viewer(EXAMPLE_TITLE);
117 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file, true));
118 if (!mesh) {
119 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
120 return EXIT_FAILURE;
121 }
122
123 viewer.camera()->set_up_vector(vec3(0, 1, 0));
124 viewer.camera()->set_view_direction(vec3(0, 0, 1));
125
126 // Show the harmonic parameterization initially.
127 parameterize(0, &viewer, mesh);
128
129 viewer.bind_shortcut([](Viewer *v, Model *m) { return parameterize(0, v, m); }, mesh, Viewer::KEY_A, Viewer::MODIF_CTRL);
130 viewer.bind_shortcut([](Viewer *v, Model *m) { return parameterize(1, v, m); }, mesh, Viewer::KEY_L, Viewer::MODIF_CTRL);
131 viewer.set_usage("default",
132 "Ctrl + a: harmonic parameterization\n" "Ctrl + l: least squares conformal mapping (LSCM)");
133
134 return viewer.run();
135}
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 surface parameterization.
Definition surface_mesh_parameterization.h:28
void harmonic(bool use_uniform_weights=false)
Compute discrete harmonic parameterization.
Definition surface_mesh_parameterization.cpp:93
void lscm()
Compute parameterization based on least squares conformal mapping.
Definition surface_mesh_parameterization.cpp:228
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
SurfaceMeshParameterization, SurfaceMesh, DrawableTriangles, Tutorial_801_Mesh_Curvature