Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_304_VectorField

Visualizing a vector field on a surface mesh as line-segment glyphs (face normals).

Overview

This tutorial renders a vector field defined on a surface mesh: the normal direction of every face of a sphere model. Each vector is drawn as a short green line segment starting at the face center and pointing along the normal — the classic "glyph" visualization of direction fields. The mesh edges are shown as well, so the faceting is easy to read.

The technique

A vector field assigns a direction (and possibly a magnitude) to each element of a domain; here the domain is the mesh surface and the vectors are the per-face normals. The standard way to display such a field is with glyphs: small geometric marks placed at the sample locations whose orientation encodes the vector direction. A line segment from the sample point s to s + v * length is the simplest glyph — its direction shows the vector, and a common length keeps the display readable. Two practical details matter: (1) the glyph length is chosen relative to the model size (5% of the bounding-box diagonal here) so the arrows neither vanish nor dominate the shape; (2) the segment's start point is the face centroid (average of its vertices), which places the glyph on the surface it describes. The same pattern works for any vector data — velocities, displacements, gradients — by substituting your own vectors for the normals. For thicker, arrow-like glyphs, the segments can be rendered as cone imposters instead (see Tutorial_302_Imposters).

Easy3D API

Running it

The window shows the faceted sphere with a green normal segment sticking out of every face and the mesh edges overlaid. Navigate with the standard Viewer controls ('h' for help).

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/drawable_lines.h>
30#include <easy3d/renderer/drawable_triangles.h>
31#include <easy3d/renderer/renderer.h>
32#include <easy3d/util/resource.h>
33#include <easy3d/util/initializer.h>
34
84using namespace easy3d;
85
86int main(int argc, char **argv) {
87 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
88 initialize();
89
90 // Create the default Easy3D viewer.
91 // Note: a viewer must be created before creating any drawables.
92 Viewer viewer(EXAMPLE_TITLE);
93
94 // Load a surface mesh (a tessellated sphere) from a file. The 'true' flag centers and
95 // scales the model to fit the scene.
96 const std::string file_name = resource::directory() + "/data/sphere.obj";
97 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file_name, true));
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 // Keep this face-normal example uniformly colored and visibly faceted,
104 // regardless of texture coordinates stored in the example OBJ file.
105 // Flat shading makes each facet's constant normal match its glyph direction.
106 auto faces = mesh->renderer()->get_triangles_drawable("faces");
107 faces->set_uniform_coloring(faces->color());
108 faces->set_smooth_shading(false);
109
110 // Get the bounding box of the model. Then we defined the length of the
111 // normal vectors to be 5% of the bounding box diagonal.
112 // A model-relative length keeps the glyphs visible without hiding the shape.
113 const Box3 &box = mesh->bounding_box();
114 float length = norm(box.max_point() - box.min_point()) * 0.05f;
115
116 // Compute the face normals. They are stored in the per-face property "f:normal".
117 mesh->update_face_normals();
118 auto normals = mesh->get_face_property<vec3>("f:normal");
119
120 // Every consecutive two points represent a normal vector: the glyph runs from the face
121 // centroid s to the tip t = s + normal * length.
122 std::vector<vec3> points;
123 for (auto f : mesh->faces()) {
124 // The centroid is the average of the face's corner positions; it anchors the glyph
125 // on the surface, independent of how many corners the face has.
126 vec3 center(0, 0, 0); // face center
127 int count = 0;
128 for (auto v : mesh->vertices(f)) {
129 center += mesh->position(v);
130 ++count;
131 }
132
133 const vec3 s = center / count;
134 const vec3 t = s + normals[f] * length;
135 points.push_back(s);
136 points.push_back(t);
137 }
138
139 // Create a drawable for rendering the normal vectors.
140 auto drawable = mesh->renderer()->add_lines_drawable("normals");
141 // Copy the data into the portable snapshot. No indices are needed: consecutive vertex pairs
142 // are interpreted as line segments automatically.
143 drawable->update_vertex_buffer(points);
144
145 // We will draw the normal vectors in a uniform green color
146 drawable->set_uniform_coloring(vec4(0.0f, 1.0f, 0.0f, 1.0f));
147
148 // Set the line width (in pixels) so the glyphs stay visible at any zoom level.
149 drawable->set_line_width(1.0f);
150
151 // Also show the standard "edges" (hidden by default) to make the faceting explicit.
152 mesh->renderer()->get_lines_drawable("edges")->set_visible(true);
153
154 // Enter the interactive loop (returns when the window is closed).
155 return viewer.run();
156}
const Point & min_point() const
Return the coordinates of the min corner (const version).
Definition box.h:107
const Point & max_point() const
Return the coordinates of the max corner (const version).
Definition box.h:118
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
LinesDrawable, SurfaceMesh, Renderer, Tutorial_302_Imposters, Tutorial_303_ScalarField