Easy3D 2.6.1
Loading...
Searching...
No Matches
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#include <string>
31#include <vector>
32#include <memory>
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 Graph;
42 class PointsDrawable;
43 class LinesDrawable;
45
58 class Renderer {
59 public:
79 explicit Renderer(Model *model, bool create_drawables = true);
80
81 virtual ~Renderer();
82
86 Model *model() { return model_; }
87
88 //-------------------- rendering functionalities -----------------------
89
91 bool is_visible() const { return visible_; }
92
94 void set_visible(bool b) { visible_ = b; }
95
97 bool is_selected() const { return selected_; }
98
100 void set_selected(bool b);
101
110 void update();
111
112 //-------------------- drawable management -----------------------
113
118 PointsDrawable *get_points_drawable(const std::string &name, bool warning_not_found = true) const;
119
124 LinesDrawable *get_lines_drawable(const std::string &name, bool warning_not_found = true) const;
125
130 TrianglesDrawable *get_triangles_drawable(const std::string &name, bool warning_not_found = true) const;
131
138 PointsDrawable *add_points_drawable(const std::string &name);
139
146 LinesDrawable *add_lines_drawable(const std::string &name);
147
154 TrianglesDrawable *add_triangles_drawable(const std::string &name);
155
159 const std::vector< std::shared_ptr<PointsDrawable> > &points_drawables() const { return points_drawables_; }
160
164 const std::vector< std::shared_ptr<LinesDrawable> > &lines_drawables() const { return lines_drawables_; }
165
169 const std::vector< std::shared_ptr<TrianglesDrawable> > &triangles_drawables() const { return triangles_drawables_; }
170
171 public:
172
181 template<typename FT>
182 static void color_from_segmentation(
183 SurfaceMesh *mesh,
186 );
187
196 template<typename FT>
197 static void color_from_segmentation(
198 SurfaceMesh *mesh,
201 );
202
211 template<typename FT>
212 static void color_from_segmentation(
213 PointCloud *cloud,
216 );
217
218 private:
219 /*
220 * @brief Create default drawables for rendering.
221 * @details This method creates defaults drawables for rendering a model. The supported default drawables are
222 * - PointCloud: "vertices".
223 * - SurfaceMesh: "faces", "vertices", "edges", "borders".
224 * - Graph: "vertices", "edges".
225 * These drawables are usually sufficient for basic rendering of the model. In case
226 * the default drawables don't meet the particular visualization purpose, you can
227 * override this function or set 'create_default_drawables' to false and create the
228 * drawables by calling Model::add_[type]_drawable().
229 */
230 void create_default_drawables();
231
232 // -------------------------------------------------------------------------------------------------------------
233
234 /*
235 * @brief Set the default rendering state of the "vertices" drawable of a point cloud.
236 * @details The default rendering state is determined by the availability of the vertex properties.
237 * The motivation is that the most appealing rendering is demonstrated by default. The following
238 * priority applies:
239 * 1. per-vertex color: in "v:color";
240 * 2. per-vertex texture coordinates: in "v:texcoord";
241 * 3. segmentation: in "v:primitive_index";
242 * 4. scalar field;
243 * 5. uniform color.
244 */
245 void set_default_rendering_state(PointCloud *model, PointsDrawable *drawable);
246
247 /*
248 * @brief Set the default rendering state of the "faces" drawable of a surface mesh.
249 * @details The default rendering state is determined by the availability of the vertex/face properties.
250 * The motivation is that the most appealing rendering is demonstrated by default. The following
251 * priority applies:
252 * 1. per-face color: in "f:color";
253 * 2. per-vertex color: in "v:color";
254 * 3. per-halfedge texture coordinates: in "h:texcoord";
255 * 4. per-vertex texture coordinates: in "v:texcoord";
256 * 5. segmentation: in "f:chart";
257 * 6. scalar field on faces;
258 * 7. scalar field on vertices;
259 * 8. uniform color
260 */
261 void set_default_rendering_state(SurfaceMesh *model, TrianglesDrawable *drawable);
262
263 /*
264 * @brief Set the default rendering state of the "vertices" drawable of a graph.
265 * @details The default rendering state is determined by the availability of the vertex properties.
266 * The motivation is that the most appealing rendering is demonstrated by default. The following
267 * priority applies:
268 * 1. per-vertex color: in "v:color";
269 * 2. per-vertex texture coordinates: in "v:texcoord";
270 * 3. scalar field;
271 * 4. uniform color.
272 */
273 void set_default_rendering_state(Graph *model, PointsDrawable *drawable);
274
275 protected:
276 Model* model_;
277
278 bool visible_;
279 bool selected_;
280
281 std::vector< std::shared_ptr<PointsDrawable> > points_drawables_;
282 std::vector< std::shared_ptr<LinesDrawable> > lines_drawables_;
283 std::vector< std::shared_ptr<TrianglesDrawable> > triangles_drawables_;
284 };
285
286}
287
288
289
290// ------------------------------------------- Implementation -------------------------------------------
291
292
293#include <easy3d/core/random.h>
294
295
296namespace easy3d {
297
298 template<typename FT> inline
301 if (mesh->empty()) {
302 LOG(WARNING) << "model has no valid geometry";
303 return;
304 }
305
306 if (!segments) {
307 LOG(WARNING) << "the surface mesh does not have face property \'" << segments.name() << "\'";
308 return;
309 }
310 if (!colors) {
311 LOG(WARNING) << "color property not allocated";
312 return;
313 }
314
315 int max_index = 0;
316 for (auto f : mesh->faces())
317 max_index = std::max(max_index, static_cast<int>(segments[f]));
318
319 // assign each segment a unique color
320 std::vector<vec3> color_table(max_index + 1); // index starts from 0
321 for (auto &c : color_table)
322 c = random_color();
323
324 for (auto f : mesh->faces()) {
325 int idx = static_cast<int>(segments[f]);
326 if (idx == -1)
327 colors[f] = vec3(0, 0, 0);
328 else
329 colors[f] = color_table[idx];
330 }
331 }
332
333
334 template<typename FT> inline
337 if (mesh->empty()) {
338 LOG(WARNING) << "model has no valid geometry";
339 return;
340 }
341
342 if (!segments) {
343 LOG(WARNING) << "the surface mesh does not have vertex property \'" << segments.name() << "\'";
344 return;
345 }
346 if (!colors) {
347 LOG(WARNING) << "color property not allocated";
348 return;
349 }
350
351 int max_index = 0;
352 for (auto v : mesh->vertices())
353 max_index = std::max(max_index, static_cast<int>(segments[v]));
354
355 // assign each segment a unique color
356 std::vector<vec3> color_table(max_index + 1); // index starts from 0
357 for (auto &c : color_table)
358 c = random_color();
359
360 for (auto v : mesh->vertices()) {
361 int idx = static_cast<int>(segments[v]);
362 if (idx == -1)
363 colors[v] = vec3(0, 0, 0);
364 else
365 colors[v] = color_table[idx];
366 }
367 }
368
369
370 template<typename FT> inline
372 const PointCloud::VertexProperty<FT> segments,
374 if (cloud->empty()) {
375 LOG(WARNING) << "model has no valid geometry";
376 return;
377 }
378
379 if (!segments) {
380 LOG(WARNING) << "the point cloud does not have vertex property \'" << segments.name() << "\'";
381 return;
382 }
383 if (!colors) {
384 LOG(WARNING) << "color property not allocated" << "\'";
385 return;
386 }
387
388 int max_index = 0;
389 for (auto v : cloud->vertices())
390 max_index = std::max(max_index, static_cast<int>(segments[v]));
391
392 // assign each segment a unique color
393 std::vector<vec3> color_table(max_index + 1); // index starts from 0
394 for (auto &c : color_table)
395 c = random_color();
396
397 for (auto v : cloud->vertices()) {
398 int idx = static_cast<int>(segments[v]);
399 if (idx == -1)
400 colors[v] = vec3(0, 0, 0);
401 else
402 colors[v] = color_table[idx];
403 }
404 }
405
406}
407
408
409#endif // EASY3D_RENDERER_RENDERER_H
A Graph data structure with easy property management.
Definition graph.h:50
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:50
bool empty() const
Tests if the model is empty.
Definition model.h:84
Vertex property of type T.
Definition point_cloud.h:151
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:671
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
Returns the name of the property.
Definition property.h:436
const std::vector< std::shared_ptr< LinesDrawable > > & lines_drawables() const
Definition renderer.h:164
const std::vector< std::shared_ptr< TrianglesDrawable > > & triangles_drawables() const
Definition renderer.h:169
Model * model()
The model to which this renderer is attached.
Definition renderer.h:86
bool is_visible() const
Returns whether the model is currently visible.
Definition renderer.h:91
PointsDrawable * add_points_drawable(const std::string &name)
Definition renderer.cpp:362
LinesDrawable * add_lines_drawable(const std::string &name)
Definition renderer.cpp:376
TrianglesDrawable * add_triangles_drawable(const std::string &name)
Definition renderer.cpp:395
TrianglesDrawable * get_triangles_drawable(const std::string &name, bool warning_not_found=true) const
Definition renderer.cpp:352
LinesDrawable * get_lines_drawable(const std::string &name, bool warning_not_found=true) const
Definition renderer.cpp:342
Renderer(Model *model, bool create_drawables=true)
Constructor.
Definition renderer.cpp:41
void set_selected(bool b)
Select/Deselect the model. The state of all its drawables will change accordingly.
Definition renderer.cpp:311
PointsDrawable * get_points_drawable(const std::string &name, bool warning_not_found=true) const
Definition renderer.cpp:332
bool is_selected() const
Returns whether the model has been selected.
Definition renderer.h:97
void set_visible(bool b)
Shows/Hides the model.
Definition renderer.h:94
void update()
Invalidates the rendering buffers of the model and thus updates the rendering (delayed in rendering).
Definition renderer.cpp:322
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:299
const std::vector< std::shared_ptr< PointsDrawable > > & points_drawables() const
Definition renderer.h:159
Face property of type T.
Definition surface_mesh.h:363
Vertex property of type T.
Definition surface_mesh.h:255
A halfedge data structure for polygonal meshes of 2-manifold.
Definition surface_mesh.h:51
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:44
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