Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_108_Graph

Building a Graph from vertices and edges — the 1D connectivity counterpart of a mesh.

Overview

This console tutorial creates the simplest Graph: four vertices positioned in 3D and five edges joining them, then prints the vertex and edge counts. It is the starting point for the graph series (109 connectivity, 110 properties, 111 I/O), just as Tutorial_104 was for surface meshes.

The technique

A Graph stores only vertices and edges — there are no faces and no halfedges. It models pure 1D connectivity: networks, curves, skeletons, road maps, or the edge graph extracted from a mesh. Each vertex still carries a 3D position (the built-in "v:point" property) so the graph can be laid out and drawn in space, and each edge simply records the pair of vertices it joins. Internally the structure keeps per-vertex adjacency so incident edges/neighbors can be queried efficiently (Tutorial_109). Compare the three core structures: PointCloud has points only, Graph adds 1D edges, SurfaceMesh adds 2D faces (halfedges), and PolyMesh adds 3D cells (halffaces). The five edges here form a 4-cycle (v0-v1-v2-v3-v0) plus one diagonal (v1-v3).

Easy3D API

Running it

Console-only. It prints "vertices: 4" and "edges: 5". The graph is heap-allocated with new and explicitly deleted at the end to release its memory.

1
27#include <easy3d/core/graph.h>
28#include <easy3d/util/initializer.h>
29
69using namespace easy3d;
70
71int main(int argc, char **argv) {
72 // initialize Easy3D.
73 initialize();
74
75 // Create an empty graph. Unlike a SurfaceMesh it holds only vertices and edges (no faces).
76 auto graph = new Graph;
77
78 // Add 4 vertices
79 Graph::Vertex v0 = graph->add_vertex(vec3(0, 0, 0));
80 Graph::Vertex v1 = graph->add_vertex(vec3(1, 0, 0));
81 Graph::Vertex v2 = graph->add_vertex(vec3(0, 1, 0));
82 Graph::Vertex v3 = graph->add_vertex(vec3(0, 0, 1));
83
84 // Add some edges. add_edge() returns an Edge handle; the trailing comments label them e0..e4
85 // in creation order. Together they form a 4-cycle (v0-v1-v2-v3-v0) plus the diagonal v1-v3.
86 graph->add_edge(v0, v1); // e0
87 graph->add_edge(v1, v2); // e1
88 graph->add_edge(v2, v3); // e2
89 graph->add_edge(v3, v0); // e3
90 graph->add_edge(v1, v3); // e4
91
92 std::cout << "vertices: " << graph->n_vertices() << std::endl;
93 std::cout << "edges: " << graph->n_edges() << std::endl;
94
95 // Delete the graph (i.e., release memory)
96 delete graph;
97
98 return EXIT_SUCCESS;
99}
A Graph data structure with easy property management.
Definition graph.h:51
Definition collider.cpp:182
This type represents a vertex (internally it is basically an index).
Definition graph.h:133
See also
Graph, Tutorial_109_Graph_Connectivity, Tutorial_110_Graph_Property, Tutorial_104_SurfaceMesh