Easy3D 2.5.3
renderer.h
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#ifndef EASY3D_RENDERER_RENDERER_H
28#define EASY3D_RENDERER_RENDERER_H
29
30
31#include <string>
32#include <vector>
33
34#include <easy3d/core/types.h>
35#include <easy3d/core/point_cloud.h>
36#include <easy3d/core/surface_mesh.h>
37
38namespace easy3d {
39
40 class Model;
41 class PointsDrawable;
42 class LinesDrawable;
43 class TrianglesDrawable;
44
61 class Renderer {
62 public:
82 explicit Renderer(Model *model, bool create_drawables = true);
83
84 virtual ~Renderer();
85
89 Model *model() { return model_; }
90
91 //-------------------- rendering functionalities -----------------------
92
94 bool is_visible() const { return visible_; }
95
97 void set_visible(bool b) { visible_ = b; }
98
100 bool is_selected() const { return selected_; }
101
103 void set_selected(bool b);
104
113 void update();
114
115 //-------------------- drawable management -----------------------
116
121 PointsDrawable *get_points_drawable(const std::string &name) const;
122
127 LinesDrawable *get_lines_drawable(const std::string &name) const;
128
133 TrianglesDrawable *get_triangles_drawable(const std::string &name) const;
134
141 PointsDrawable *add_points_drawable(const std::string &name);
142
149 LinesDrawable *add_lines_drawable(const std::string &name);
150
157 TrianglesDrawable *add_triangles_drawable(const std::string &name);
158
162 const std::vector<PointsDrawable *> &points_drawables() const { return points_drawables_; }
163
167 const std::vector<LinesDrawable *> &lines_drawables() const { return lines_drawables_; }
168
172 const std::vector<TrianglesDrawable *> &triangles_drawables() const { return triangles_drawables_; }
173
174 public:
187
188 // -------------------------------------------------------------------------------------------------------------
189
202
217
218 // -------------------------------------------------------------------------------------------------------------
219
228 template<typename FT>
229 static void color_from_segmentation(
230 SurfaceMesh *mesh,
233 );
234
243 template<typename FT>
244 static void color_from_segmentation(
245 SurfaceMesh *mesh,
248 );
249
258 template<typename FT>
259 static void color_from_segmentation(
260 PointCloud *cloud,
263 );
264
265 protected:
266 Model *model_;
267
268 bool visible_;
269 bool selected_;
270
271 std::vector<PointsDrawable *> points_drawables_;
272 std::vector<LinesDrawable *> lines_drawables_;
273 std::vector<TrianglesDrawable *> triangles_drawables_;
274 };
275
276}
277
278
279
280// ------------------------------------------- Implementation -------------------------------------------
281
282
283#include <easy3d/core/random.h>
284
285
286namespace easy3d {
287
288 template<typename FT> inline
291 if (mesh->empty()) {
292 LOG(WARNING) << "model has no valid geometry";
293 return;
294 }
295
296 if (!segments) {
297 LOG(WARNING) << "the surface mesh does not have face property \'" << segments.name() << "\'";
298 return;
299 }
300 if (!colors) {
301 LOG(WARNING) << "color property not allocated";
302 return;
303 }
304
305 int max_index = 0;
306 for (auto f : mesh->faces())
307 max_index = std::max(max_index, static_cast<int>(segments[f]));
308
309 // assign each segment a unique color
310 std::vector<vec3> color_table(max_index + 1); // index starts from 0
311 for (auto &c : color_table)
312 c = random_color();
313
314 for (auto f : mesh->faces()) {
315 int idx = static_cast<int>(segments[f]);
316 if (idx == -1)
317 colors[f] = vec3(0, 0, 0);
318 else
319 colors[f] = color_table[idx];
320 }
321 }
322
323
324 template<typename FT> inline
327 if (mesh->empty()) {
328 LOG(WARNING) << "model has no valid geometry";
329 return;
330 }
331
332 if (!segments) {
333 LOG(WARNING) << "the surface mesh does not have vertex property \'" << segments.name() << "\'";
334 return;
335 }
336 if (!colors) {
337 LOG(WARNING) << "color property not allocated";
338 return;
339 }
340
341 int max_index = 0;
342 for (auto v : mesh->vertices())
343 max_index = std::max(max_index, static_cast<int>(segments[v]));
344
345 // assign each segment a unique color
346 std::vector<vec3> color_table(max_index + 1); // index starts from 0
347 for (auto &c : color_table)
348 c = random_color();
349
350 for (auto v : mesh->vertices()) {
351 int idx = static_cast<int>(segments[v]);
352 if (idx == -1)
353 colors[v] = vec3(0, 0, 0);
354 else
355 colors[v] = color_table[idx];
356 }
357 }
358
359
360 template<typename FT> inline
362 const PointCloud::VertexProperty<FT> segments,
364 if (cloud->empty()) {
365 LOG(WARNING) << "model has no valid geometry";
366 return;
367 }
368
369 if (!segments) {
370 LOG(WARNING) << "the point cloud does not have vertex property \'" << segments.name() << "\'";
371 return;
372 }
373 if (!colors) {
374 LOG(WARNING) << "color property not allocated" << "\'";
375 return;
376 }
377
378 int max_index = 0;
379 for (auto v : cloud->vertices())
380 max_index = std::max(max_index, static_cast<int>(segments[v]));
381
382 // assign each segment a unique color
383 std::vector<vec3> color_table(max_index + 1); // index starts from 0
384 for (auto &c : color_table)
385 c = random_color();
386
387 for (auto v : cloud->vertices()) {
388 int idx = static_cast<int>(segments[v]);
389 if (idx == -1)
390 colors[v] = vec3(0, 0, 0);
391 else
392 colors[v] = color_table[idx];
393 }
394 }
395
396}
397
398
399#endif // EASY3D_RENDERER_RENDERER_H
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition: drawable_lines.h:40
The base class of renderable 3D models.
Definition: model.h:49
bool empty() const
Tests if the model is empty.
Definition: model.h:83
Vertex property of type T.
Definition: point_cloud.h:107
A data structure for point clouds.
Definition: point_cloud.h:45
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: point_cloud.h:444
The drawable for rendering a set of points, e.g., point clouds, vertices of a mesh.
Definition: drawable_points.h:42
const std::string & name() const
Return the name of the property.
Definition: property.h:318
A Renderer manages the drawables (and thus the rendering) of a model.
Definition: renderer.h:61
const std::vector< TrianglesDrawable * > & triangles_drawables() const
Definition: renderer.h:172
LinesDrawable * get_lines_drawable(const std::string &name) const
Definition: renderer.cpp:295
static void create_default_drawables(Model *model)
Create default drawables for rendering.
Definition: renderer.cpp:61
Model * model()
The model to which this renderer is attached.
Definition: renderer.h:89
bool is_visible() const
Returns whether the model is currently visible.
Definition: renderer.h:94
PointsDrawable * add_points_drawable(const std::string &name)
Definition: renderer.cpp:313
LinesDrawable * add_lines_drawable(const std::string &name)
Definition: renderer.cpp:327
TrianglesDrawable * add_triangles_drawable(const std::string &name)
Definition: renderer.cpp:346
Renderer(Model *model, bool create_drawables=true)
Constructor.
Definition: renderer.cpp:40
const std::vector< LinesDrawable * > & lines_drawables() const
Definition: renderer.h:167
void set_selected(bool b)
Select/Deselect the model. The state of all its drawables will change accordingly.
Definition: renderer.cpp:265
PointsDrawable * get_points_drawable(const std::string &name) const
Definition: renderer.cpp:286
TrianglesDrawable * get_triangles_drawable(const std::string &name) const
Definition: renderer.cpp:304
const std::vector< PointsDrawable * > & points_drawables() const
Definition: renderer.h:162
static void set_default_rendering_state(PointCloud *model, PointsDrawable *drawable)
Set the default rendering state of the "vertices" drawable of a point cloud.
Definition: renderer.cpp:159
bool is_selected() const
Returns whether the model has been selected.
Definition: renderer.h:100
void set_visible(bool b)
Shows/Hides the model.
Definition: renderer.h:97
void update()
Invalidates the rendering buffers of the model and thus updates the rendering (delayed in rendering).
Definition: renderer.cpp:276
static void color_from_segmentation(SurfaceMesh *mesh, SurfaceMesh::FaceProperty< FT > segments, SurfaceMesh::FaceProperty< vec3 > colors)
Generates random colors for visualizing face-based segmentation of a SurfaceMesh.
Definition: renderer.h:289
Definition: surface_mesh.h:257
Definition: surface_mesh.h:185
A halfedge data structure for polygonal meshes of 2-manifold.
Definition: surface_mesh.h:52
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: surface_mesh.h:1631
FaceContainer faces() const
returns face container for C++11 range-based for-loops
Definition: surface_mesh.h:1685
The drawable for rendering a set of triangles, e.g., the surface of a triangular mesh.
Definition: drawable_triangles.h:46
Definition: collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition: types.h:45
vec3 random_color(bool allow_dark=false)
Generates a random color. The parameter allow_dark controls if too dark colors are allowed.
Definition: random.h:49