* Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
* https://3d.bk.tudelft.nl/liangliang/
*
* This file is part of Easy3D. If it is useful in your research/work,
* I would be grateful if you show your appreciation by citing it:
* ------------------------------------------------------------------
* Liangliang Nan.
* Easy3D: a lightweight, easy-to-use, and efficient C++ library
* for processing and rendering 3D data.
* Journal of Open Source Software, 6(64), 3255, 2021.
* ------------------------------------------------------------------
*
* Easy3D is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 3
* as published by the Free Software Foundation.
*
* Easy3D is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/
#include <easy3d/viewer/viewer.h>
#include <easy3d/core/point_cloud.h>
#include <easy3d/core/random.h>
#include <easy3d/renderer/renderer.h>
#include <easy3d/renderer/drawable_points.h>
#include <easy3d/util/initializer.h>
#include <easy3d/util/timer.h>
* \example{lineno} Tutorial_312_MultiThread/main.cpp
* This example shows how to use another thread for repeatedly modifying a model and updating the viewer thread.
*/
using namespace easy3d;
void edit_model(PointCloud *cloud, Viewer *viewer) {
if (cloud->n_vertices() >= 1000000)
return;
auto colors = cloud->vertex_property<vec3>("v:color");
for (int i = 0; i < 100; ++i) {
auto v = cloud->add_vertex(vec3(random_float(), random_float(), random_float()));
colors[v] = vec3(random_float(), random_float(), random_float());
}
cloud->renderer()->update();
viewer->update();
std::cout << "#points: " << cloud->n_vertices() << std::endl;
}
int main(int argc, char **argv) {
initialize();
Viewer viewer(EXAMPLE_TITLE);
auto cloud = new PointCloud;
auto colors = cloud->add_vertex_property<vec3>("v:color");
for (int i = 0; i < 100; ++i) {
auto v = cloud->add_vertex(easy3d::vec3(random_float(), random_float(), random_float()));
colors[v] = vec3(1.0f, 0.0f, 0.0f);
}
viewer.add_model(std::shared_ptr<PointCloud>(cloud));
auto drawable = cloud->renderer()->get_points_drawable("vertices");
drawable->set_point_size(10.0f);
drawable->set_coloring(Drawable::COLOR_PROPERTY, Drawable::VERTEX, "v:color");
#if 1
Timer<PointCloud *, Viewer *> timer;
timer.set_interval(300, edit_model, cloud, &viewer);
Timer<>::single_shot(20000, &timer, &Timer<PointCloud*, Viewer*>::stop);
#else
Timer<>::single_shot(0, [&]() {
for (int i = 0; i < 50; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(300));
std::thread t(edit_model, cloud, &viewer);
t.detach();
}
});
#endif
return viewer.run();
}