Easy3D 2.5.3
Tutorial_304_VectorField
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/drawable_lines.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/util/resource.h>
32#include <easy3d/util/initializer.h>
33
34
35using namespace easy3d;
36
37
38// This example shows how to
39// - rendering a vector field defined on a surface mesh;
40
41
42int main(int argc, char **argv) {
43 // initialize Easy3D.
44 initialize();
45
46 // Create the default Easy3D viewer.
47 // Note: a viewer must be created before creating any drawables.
48 Viewer viewer(EXAMPLE_TITLE);
49
50 // Load point cloud data from a file
51 const std::string file_name = resource::directory() + "/data/sphere.obj";
52 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file_name, true));
53 if (!mesh) {
54 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
55 return EXIT_FAILURE;
56 }
57
58 // Get the bounding box of the model. Then we defined the length of the
59 // normal vectors to be 5% of the bounding box diagonal.
60 const Box3 &box = mesh->bounding_box();
61 float length = norm(box.max_point() - box.min_point()) * 0.05f;
62
63 // Compute the face normals.
64 mesh->update_face_normals();
65 auto normals = mesh->get_face_property<vec3>("f:normal");
66
67 // Every consecutive two points represent a normal vector.
68 std::vector<vec3> points;
69 for (auto f : mesh->faces()) {
70 vec3 center(0, 0, 0); // face center
71 int count = 0;
72 for (auto v : mesh->vertices(f)) {
73 center += mesh->position(v);
74 ++count;
75 }
76
77 const vec3 s = center / count;
78 const vec3 t = s + normals[f] * length;
79 points.push_back(s);
80 points.push_back(t);
81 }
82
83 // Create a drawable for rendering the normal vectors.
84 auto drawable = mesh->renderer()->add_lines_drawable("normals");
85 // Upload the data to the GPU.
86 drawable->update_vertex_buffer(points);
87
88 // We will draw the normal vectors in a uniform green color
89 drawable->set_uniform_coloring(vec4(0.0f, 1.0f, 0.0f, 1.0f));
90
91 // Set the line width
92 drawable->set_line_width(3.0f);
93
94 // Also show the standard "edges"
95 mesh->renderer()->get_lines_drawable("edges")->set_visible(true);
96
97 // run the viewer
98 return viewer.run();
99}
100
const Point & min_point() const
Definition: box.h:93
const Point & max_point() const
Definition: box.h:98
A halfedge data structure for polygonal meshes of 2-manifold.
Definition: surface_mesh.h:52
The built-in Easy3D viewer.
Definition: viewer.h:61
virtual Model * add_model(const std::string &file_name, bool create_default_drawables=true)
Add a model from a file to the viewer to be visualized. On success, the viewer will be in charge of t...
Definition: viewer.cpp:1204
int run(bool see_all=true)
Run the viewer.
Definition: viewer.cpp:1090
Definition: collider.cpp:182
T length(const Vec< N, T > &v)
Computes the length/magnitude of a vector.
Definition: vec.h:289
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35
FT norm(const Matrix< FT > &)
utilities
Definition: matrix.h:1424