Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_110_Graph_Property

Adding per-vertex and per-edge properties (colors, lengths) to a Graph and reading them back.

Overview

This console tutorial extends the property lessons of Tutorial_102 and Tutorial_106 to graphs. On the graph from Tutorial_108 it creates a per-vertex color property "v:color" and a per-edge length property "e:length", fills them, and prints each vertex's position/color and each edge's length. It demonstrates that graphs support both vertex and edge properties.

The technique

Properties are parallel arrays indexed by element handle, named with a "v:" (vertex) or "e:" (edge) prefix. The vertex positions are the built-in "v:point" property; here we fetch it with vertex_property<T>(), a get-or-create convenience that returns the existing property (or adds it if missing). For demonstration the color of each vertex is set equal to its position — in real code you would assign a meaningful color. The length of an edge is the Euclidean distance between its two endpoints; Graph::edge_length() computes it, and we cache the result in "e:length" so later algorithms (e.g. shortest paths) can reuse it without recomputing.

Easy3D API

Running it

Console-only. It prints one line per vertex ("vertex: ..., position: ..., color: ...") then one line per edge ("edge: ..., length: ..."). The graph is heap-allocated and deleted at the end.

1
27#include <easy3d/core/graph.h>
28#include <easy3d/util/initializer.h>
29
69using namespace easy3d;
70
71// the graph created in the previous tutorial (so you can skip it)
72Graph *old_graph_from_previous_example() {
73 // Create a graph
74 auto graph = new Graph;
75
76 // Add 4 vertices
77 Graph::Vertex v0 = graph->add_vertex(vec3(0, 0, 0));
78 Graph::Vertex v1 = graph->add_vertex(vec3(1, 0, 0));
79 Graph::Vertex v2 = graph->add_vertex(vec3(0, 1, 0));
80 Graph::Vertex v3 = graph->add_vertex(vec3(0, 0, 1));
81
82 // Add some edges
83 graph->add_edge(v0, v1); // e0
84 graph->add_edge(v1, v2); // e1
85 graph->add_edge(v2, v3); // e2
86 graph->add_edge(v3, v0); // e3
87 graph->add_edge(v1, v3); // e4
88
89 return graph;
90}
91
92int main(int argc, char **argv) {
93 // initialize Easy3D.
94 initialize();
95
96 Graph *graph = old_graph_from_previous_example();
97
98 // Fetch the built-in position property. vertex_property() is "get-or-add": it returns the
99 // existing "v:point" array (creating it only if it were somehow absent).
100 auto points = graph->vertex_property<vec3>("v:point");
101
102 // We add a per-vertex property "v:color" to assign a color to each vertex
103 auto colors = graph->add_vertex_property<vec3>("v:color");
104 for (auto v : graph->vertices()) {
105 // We assign each vertex a color that is equal to his position (you should
106 // assign a more meaningful color in practice).
107 colors[v] = points[v];
108 std::cout << "vertex: " << v << ", position: " << points[v] << ", color: " << colors[v] << std::endl;
109 }
110
111 // We add a per-edge property "e:length" to store the edge lengths
112 auto lengths = graph->add_edge_property<float>("e:length");
113 for (auto e : graph->edges()) {
114 // edge_length() is the Euclidean distance between the edge's two endpoints; we cache it.
115 lengths[e] = graph->edge_length(e);
116 std::cout << "edge: " << e << ", length: " << lengths[e] << std::endl;
117 }
118
119 delete graph;
120
121 return EXIT_SUCCESS;
122}
A Graph data structure with easy property management.
Definition graph.h:51
VertexProperty< T > vertex_property(const std::string &name, const T t=T())
Gets or adds a vertex property.
Definition graph.h:1006
VertexProperty< T > add_vertex_property(const std::string &name, const T t=T())
Adds a vertex property.
Definition graph.h:936
float edge_length(Edge e) const
Computes the length of an edge.
Definition graph.cpp:261
EdgeProperty< T > add_edge_property(const std::string &name, const T t=T())
Adds an edge property.
Definition graph.h:948
Definition collider.cpp:182
This type represents a vertex (internally it is basically an index).
Definition graph.h:133
See also
Graph, Tutorial_102_PointCloud_Property, Tutorial_106_SurfaceMesh_Property, Tutorial_108_Graph