Easy3D 2.5.3
Tutorial_110_Graph_Property
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/core/graph.h>
28#include <easy3d/util/initializer.h>
29
30
31using namespace easy3d;
32
33
34// This example shows how to
35// - add per-vertex/per-edge properties to a graph;
36// - access existing properties.
37//
38
39// the graph created in the previous tutorial (so you can skip it)
40Graph* old_graph_from_previous_example() {
41 // Create a graph
42 auto graph = new Graph;
43
44 // Add 4 vertices
45 Graph::Vertex v0 = graph->add_vertex(vec3(0, 0, 0));
46 Graph::Vertex v1 = graph->add_vertex(vec3(1, 0, 0));
47 Graph::Vertex v2 = graph->add_vertex(vec3(0, 1, 0));
48 Graph::Vertex v3 = graph->add_vertex(vec3(0, 0, 1));
49
50 // Add some edges
51 graph->add_edge(v0, v1); // e0
52 graph->add_edge(v1, v2); // e1
53 graph->add_edge(v2, v3); // e2
54 graph->add_edge(v3, v0); // e3
55 graph->add_edge(v1, v3); // e4
56
57 return graph;
58}
59
60
61int main(int argc, char** argv) {
62 // initialize Easy3D.
63 initialize();
64
65 Graph* graph = old_graph_from_previous_example();
66
67 auto points = graph->vertex_property<vec3>("v:point");
68
69 // We add a per-vertex property "v:color" to assign a color to each vertex
70 auto colors = graph->add_vertex_property<vec3>("v:color");
71 for (auto v : graph->vertices()) {
72 // We assign each vertex a color that is equal to his position (you should
73 // assign a more meaningful color in practice).
74 colors[v] = points[v];
75 std::cout << "vertex: " << v << ", position: " << points[v] << ", color: " << colors[v] << std::endl;
76 }
77
78 // We add a per-edge property "e:length" to store the edge lengths
79 auto lengths = graph->add_edge_property<float>("e:length");
80 for (auto e : graph->edges()) {
81 lengths[e] = graph->edge_length(e);
82 std::cout << "edge: " << e << ", length: " << lengths[e] << std::endl;
83 }
84
85 delete graph;
86
87 return EXIT_SUCCESS;
88}
89
A Graph data structure with easy property management.
Definition: graph.h:51
EdgeContainer edges() const
returns edge container for C++11 range-based for-loops
Definition: graph.h:816
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: graph.h:798
VertexProperty< T > vertex_property(const std::string &name, const T t=T())
Definition: graph.h:684
VertexProperty< T > add_vertex_property(const std::string &name, const T t=T())
Definition: graph.h:642
float edge_length(Edge e) const
compute the length of edge e.
Definition: graph.cpp:256
EdgeProperty< T > add_edge_property(const std::string &name, const T t=T())
Definition: graph.h:649
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
Definition: graph.h:103