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
- Graph::vertex_property<T>(name): get-or-add a vertex property (used for "v:point").
- Graph::add_vertex_property<T>(name) / add_edge_property<T>(name): create new properties, returning VertexProperty<T> / EdgeProperty<T> accessors indexed by handle.
- Graph::vertices()/edges(): ranges to iterate; Graph::edge_length(e): endpoint distance.
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.
27#include <easy3d/core/graph.h>
28#include <easy3d/util/initializer.h>
72Graph *old_graph_from_previous_example() {
74 auto graph =
new Graph;
83 graph->add_edge(v0, v1);
84 graph->add_edge(v1, v2);
85 graph->add_edge(v2, v3);
86 graph->add_edge(v3, v0);
87 graph->add_edge(v1, v3);
92int main(
int argc,
char **argv) {
96 Graph *graph = old_graph_from_previous_example();
104 for (
auto v : graph->vertices()) {
107 colors[v] = points[v];
108 std::cout <<
"vertex: " << v <<
", position: " << points[v] <<
", color: " << colors[v] << std::endl;
113 for (
auto e : graph->edges()) {
116 std::cout <<
"edge: " << e <<
", length: " << lengths[e] << std::endl;
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