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
- SurfaceMesh: update_face_normals() computes the "f:normal" property; vertices(f) iterates the corners of a face; bounding_box() gives the model extents.
- Renderer: add_lines_drawable() creates an extra LinesDrawable attached to the model; get_lines_drawable("edges") retrieves the built-in edge drawable.
- LinesDrawable: update_vertex_buffer() copies the segment endpoints into its portable snapshot (each consecutive pair of points is one line); set_uniform_coloring() and set_line_width() style them.
- DrawableTriangles: set_smooth_shading(false) keeps the faceted look so each face's normal glyph visibly matches its flat facet.
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).
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>
86int main(
int argc,
char **argv) {
92 Viewer viewer(EXAMPLE_TITLE);
96 const std::string file_name = resource::directory() +
"/data/sphere.obj";
97 auto mesh =
dynamic_cast<SurfaceMesh *
>(viewer.add_model(file_name,
true));
99 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
106 auto faces = mesh->renderer()->get_triangles_drawable(
"faces");
107 faces->set_uniform_coloring(faces->color());
108 faces->set_smooth_shading(
false);
113 const Box3 &box = mesh->bounding_box();
117 mesh->update_face_normals();
118 auto normals = mesh->get_face_property<
vec3>(
"f:normal");
122 std::vector<vec3> points;
123 for (
auto f : mesh->faces()) {
126 vec3 center(0, 0, 0);
128 for (
auto v : mesh->vertices(f)) {
129 center += mesh->position(v);
133 const vec3 s = center / count;
134 const vec3 t = s + normals[f] * length;
140 auto drawable = mesh->renderer()->add_lines_drawable(
"normals");
143 drawable->update_vertex_buffer(points);
146 drawable->set_uniform_coloring(
vec4(0.0f, 1.0f, 0.0f, 1.0f));
149 drawable->set_line_width(1.0f);
152 mesh->renderer()->get_lines_drawable(
"edges")->set_visible(
true);
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