Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_303_ScalarField

Visualizing a scalar field on a mesh by mapping values to colors with a colormap.

Overview

This tutorial renders a scalar field defined on the vertices of a surface mesh: the elevation (Z coordinate) of each vertex of a building model. The values are turned into colors through a colormap texture, producing the familiar "heatmap" look used to inspect simulation results, measured quantities, or any per-vertex/per-face attribute.

The technique

A scalar field assigns one number to each mesh element (here: one float per vertex). To visualize it, the numbers are first normalized to the field's [min, max] range and then used as a 1D texture coordinate into a colormap — a thin image whose pixels encode a color ramp (e.g. blue for low values through green/yellow to red for high ones). The GPU samples that ramp per fragment, and because the scalar is a vertex attribute the color interpolates smoothly across each triangle. This "pseudocolor" mapping is the standard way to make invisible quantities (temperature, pressure, error, height, ...) visible at a glance. Easy3D implements the ramp as an ordinary Texture: a default colormap is created automatically, and you can substitute any image (the commented block below loads "colormaps/rainbow.png"). Optional clamp percentages trim outliers from the range so a few extreme values do not wash out the color contrast; here both clamps are 0, i.e. the full data range is used.

Easy3D API

Running it

The building is displayed colored by height: low parts take the low end of the colormap and high parts the high end. Navigate with the standard Viewer controls ('h' for help). To try a different colormap, uncomment the block that requests "rainbow.png" before running.

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/renderer.h>
30#include <easy3d/renderer/drawable_triangles.h>
31#include <easy3d/renderer/texture_manager.h>
32#include <easy3d/fileio/surface_mesh_io.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
84using namespace easy3d;
85
86int main(int argc, char **argv) {
87 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
88 initialize();
89
90 const std::string file_name = resource::directory() + "/data/building.off";
91
92 // Create the default Easy3D viewer.
93 Viewer viewer(EXAMPLE_TITLE);
94
95 // Load a mesh model to the viewer. add_model() also creates the default drawables
96 // (including the "faces" triangles drawable we configure below).
97 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file_name));
98 if (!mesh) {
99 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
100 return EXIT_FAILURE;
101 }
102
103 // By default, Easy3D renders the model using either a uniform color, or a per-face/vertex color
104 // if the file contains such information. In this tutorial, we define a scalar field on the mesh
105 // vertices: elevation (i.e., the Z-component of each vertex). The visualization is done by mapping
106 // the scalar value to a colormap.
107 // Properties are Easy3D's way of attaching arbitrary per-element data to a mesh; the "v:"
108 // prefix marks this one as a vertex property.
109 auto elevation = mesh->add_vertex_property<float>("v:elevation");
110 for (auto v : mesh->vertices())
111 elevation[v] = mesh->position(v).z;
112
113 // Tell the drawable to use the scalar property "v:elevation" to color the model.
114 // State::VERTEX: the scalar is defined per vertex (colors interpolate across faces).
115 // nullptr: use the automatically created default colormap texture.
116 // The two 0.0f arguments disable lower/upper range clamping, so the full min-max range
117 // of the data is mapped onto the colormap.
118 auto drawable = mesh->renderer()->get_triangles_drawable("faces");
119 drawable->set_scalar_coloring(State::VERTEX, "v:elevation", nullptr, 0.0f, 0.0f);
120
121 // A colormap is just a 1D texture, so any image can serve as one. The optional block
122 // below replaces the default colormap with the bundled "rainbow" ramp.
123 // // A default scalar texture is automatically created.
124 // // However, you can choose a more suitable one using the code below:
125 // const std::string texture_file = resource::directory() + "/colormaps/rainbow.png";
126 // auto texture = TextureManager::request(texture_file);
127 // if (!texture) {
128 // std::cerr << "failed to create texture" << std::endl;
129 // return EXIT_FAILURE;
130 // }
131 // drawable->set_texture(texture.get()); // Use the texture
132
133 // Enter the interactive loop (returns when the window is closed).
134 return viewer.run();
135}
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
DrawableTriangles, SurfaceMesh, Texture, TextureManager, Tutorial_304_VectorField, Tutorial_305_Texture