Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_111_Graph_IO

Loading a Graph from a PLY file and saving it back out with GraphIO.

Overview

The graph counterpart of Tutorial_103 (point clouds) and Tutorial_107 (surface meshes). This console program loads the bundled "graph.ply", prints its vertex and edge counts, leaves a placeholder for processing, and writes a copy "graph-copy.ply". It follows the same load -> use -> save -> free lifecycle as the other I/O tutorials.

The technique

GraphIO currently supports only the PLY format. PLY (the Polygon File Format, from Stanford) is a flexible, self-describing format: a header declares the elements and their properties, followed by the data, in ASCII or binary. For a graph it stores the vertex positions and the edge list (pairs of vertex indices), which is exactly the 1D connectivity a Graph needs. As with the other loaders, load() returns std::shared_ptr<Graph> (nullptr on failure), and save() returns a bool indicating success. Because only PLY is supported, both the input and output files use the ".ply" extension.

Easy3D API

Running it

Console-only. It prints "graph loaded." with the vertex and edge counts, then "graph saved to './graph-copy.ply'". The copy is written to the current working directory.

1
27#include <easy3d/core/graph.h>
28#include <easy3d/fileio/graph_io.h>
29#include <easy3d/util/resource.h>
30#include <easy3d/util/initializer.h>
31
70using namespace easy3d;
71
72int main(int argc, char **argv) {
73 // initialize Easy3D.
74 initialize();
75
76 // Read a graph specified by its file name. GraphIO supports only PLY, so the file is ".ply";
77 // load() returns a shared-ownership handle (nullptr on failure).
78 const std::string file_name = resource::directory() + "/data/graph.ply";
79 auto graph = GraphIO::load(file_name);
80 if (!graph) {
81 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
82 return EXIT_FAILURE;
83 }
84 std::cout << "graph loaded. " << std::endl;
85 std::cout << "\tvertices: " << graph->n_vertices() << std::endl;
86 std::cout << "\tedges: " << graph->n_edges() << std::endl;
87
88 // ...
89 // Do fancy stuff with the graph
90 // ...
91
92 // Write the graph to a new PLY file. save() returns true on success, letting us confirm the
93 // copy was written (the ".ply" extension is required since PLY is the only supported format).
94 const std::string save_file_name = "./graph-copy.ply";
95 if (GraphIO::save(save_file_name, graph.get()))
96 std::cout << "graph saved to \'" << save_file_name << "\'" << std::endl;
97 else
98 std::cerr << "failed create the new file" << std::endl;
99
100 return EXIT_SUCCESS;
101}
Definition collider.cpp:182
See also
Graph, GraphIO, Tutorial_107_SurfaceMesh_IO, Tutorial_103_PointCloud_IO, Tutorial_115_PolyMesh_IO