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
- SurfaceMeshParameterization::harmonic(): discrete harmonic map (boundary mapped to the unit circle).
- SurfaceMeshParameterization::lscm(): least squares conformal map; requires a triangle mesh, so the mesh is triangulated first. Both fill the "v:texcoord" vertex property.
- DrawableTriangles::set_texture_coloring(): samples a texture using the per-vertex "v:texcoord" coordinates to paint the checkerboard onto the surface.
- Renderer::update(): re-extracts the rendering buffers after the texture coordinates change.
- Viewer::bind_shortcut() / set_usage(): register the Ctrl shortcuts and the help text.
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.
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>
89 bool parameterize(
int method,
Viewer *viewer,
Model *model) {
99 parameterization.
lscm();
102 auto drawable = mesh->renderer()->get_triangles_drawable(
"faces");
103 drawable->set_texture_coloring(State::VERTEX,
"v:texcoord");
104 mesh->renderer()->update();
111int main(
int argc,
char **argv) {
114 const std::string file = resource::directory() +
"/data/girl_face.ply";
116 Viewer viewer(EXAMPLE_TITLE);
117 auto mesh =
dynamic_cast<SurfaceMesh *
>(viewer.add_model(file,
true));
119 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
123 viewer.camera()->set_up_vector(
vec3(0, 1, 0));
124 viewer.camera()->set_view_direction(
vec3(0, 0, 1));
127 parameterize(0, &viewer, mesh);
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)");
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