Easy3D 2.5.3
Tutorial_312_MultiThread
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/point_cloud.h>
29#include <easy3d/core/random.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/renderer/drawable_points.h>
32#include <easy3d/util/initializer.h>
33#include <easy3d/util/timer.h>
34
35
36using namespace easy3d;
37
38// This example shows how to use another thread for
39// - repeatedly modifying a model, and
40// - notifying the viewer thread
41
42
43// a function that modifies the model.
44// in this simple example, we add more points (with per point colors) to a point cloud.
45void edit_model(PointCloud *cloud, Viewer *viewer) {
46 if (cloud->n_vertices() >= 1000000) // stop growing when the model is too big
47 return;
48
49 auto colors = cloud->vertex_property<vec3>("v:color");
50 for (int i = 0; i < 100; ++i) {
51 auto v = cloud->add_vertex(vec3(random_float(), random_float(), random_float()));
52 colors[v] = vec3(random_float(), random_float(), random_float()); // we use a random color
53 }
54
55 // notify the renderer to update the OpenGL buffers
56 cloud->renderer()->update();
57 // notify the viewer to update the display
58 viewer->update();
59
60 std::cout << "#points: " << cloud->n_vertices() << std::endl;
61}
62
63
64int main(int argc, char **argv) {
65 // initialize Easy3D.
66 initialize();
67
68 // create the viewer
69 Viewer viewer(EXAMPLE_TITLE);
70
71 // create a point cloud (with a per point color property) from a set of random points
72 auto cloud = new PointCloud;
73 // add a per point color property
74 auto colors = cloud->add_vertex_property<vec3>("v:color");
75 for (int i = 0; i < 100; ++i) {
76 auto v = cloud->add_vertex(easy3d::vec3(random_float(), random_float(), random_float()));
77 // assign a color to each point (here simply a red color)
78 colors[v] = vec3(1.0f, 0.0f, 0.0f);
79 }
80 // add the point cloud to the viewer for visualization
81 viewer.add_model(cloud);
82
83 // set up visualization parameters
84 auto drawable = cloud->renderer()->get_points_drawable("vertices");
85 // set point size
86 drawable->set_point_size(10.0f);
87 // set coloring method: we want to visualize the point cloud using the per point color property
88 drawable->set_coloring(Drawable::COLOR_PROPERTY, Drawable::VERTEX, "v:color");
89
90 // run the process in another thread.
91#if 1 // we use a timer to repeatedly edit the point cloud every 300 milliseconds
93 timer.set_interval(300, edit_model, cloud, &viewer);
94
95 // stop editing the model after 20 seconds
96 Timer<>::single_shot(20000, &timer, &Timer<PointCloud*, Viewer*>::stop); // or Timer<>::single_shot(4900, [&]() -> void { timer.stop(); });
97#else // use a simple for loop to repeat the editing a fix number of iterations
98 Timer<>::single_shot(0, [&]() {
99 // in this example, we modify the point cloud 50 times
100 for (int i = 0; i < 50; ++i) {
101 // allow sufficient time for each edit to complete (each edit runs in a separate thread)
102 std::this_thread::sleep_for(std::chrono::milliseconds(300));
103 std::thread t(edit_model, cloud, &viewer);
104 t.detach();
105 }
106 });
107#endif
108
109 // run the viewer
110 return viewer.run();
111}
Renderer * renderer()
Gets the renderer of this model.
Definition: model.h:94
A data structure for point clouds.
Definition: point_cloud.h:45
VertexProperty< T > vertex_property(const std::string &name, const T t=T())
if a vertex property of type T with name name exists, it is returned. otherwise this property is adde...
Definition: point_cloud.h:361
VertexProperty< T > add_vertex_property(const std::string &name, const T t=T())
add a vertex property of type T with name name and default value t. fails if a property named name ex...
Definition: point_cloud.h:319
unsigned int n_vertices() const
returns number of vertices in the cloud
Definition: point_cloud.h:279
Vertex add_vertex(const vec3 &p)
add a new vertex with position p
Definition: point_cloud.cpp:177
void update()
Invalidates the rendering buffers of the model and thus updates the rendering (delayed in rendering).
Definition: renderer.cpp:276
A light-weight implementation of the timer mechanism.
Definition: timer.h:49
void set_interval(int interval, std::function< void(Args...)> const &func, Args... args) const
Executes function func for every interval milliseconds.
Definition: timer.h:268
The built-in Easy3D viewer.
Definition: viewer.h:61
void update() const
Update the display (i.e., repaint).
Definition: viewer.cpp:631
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35
float random_float()
Random real in [0, 1].
Definition: random.h:39