Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_115_PolyMesh_IO

Loading a polyhedral mesh from a file and saving it back out.

Overview

This console tutorial completes the PolyMesh series (112-114) by showing file input/output. It loads a polyhedral sphere from disk, reports how many vertices, edges, faces, and cells it contains, then writes an identical copy to a new file. No viewer is opened.

The technique

Persisting geometry means serializing both the element coordinates and the connectivity (which vertices form each face, which faces bound each cell). Easy3D provides a dedicated PolyMeshIO helper for this. The file extension selects the format: ".plm" is Easy3D's built-in ASCII polyhedral format (".pm" is the binary counterpart). load() returns std::shared_ptr<PolyMesh>, or nullptr if the file is missing or malformed — so the result must always be checked. save() returns true on success. The returned handle shares ownership with any other holder.

Easy3D API

Running it

Console-only. It prints "mesh loaded." followed by the four element counts, then "mesh saved to './sphere-copy.plm'". A copy of the sphere is written to the current working directory. The shared mesh handle is released automatically at exit.

1
27#include <easy3d/core/poly_mesh.h>
28#include <easy3d/fileio/poly_mesh_io.h>
29#include <easy3d/util/resource.h>
30#include <easy3d/util/initializer.h>
31
71using namespace easy3d;
72
73int main(int argc, char **argv) {
74 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
75 initialize();
76
77 // Read a mesh specified by its file name. The ".plm" extension selects Easy3D's ASCII
78 // polyhedral format. load() returns a shared-ownership handle (nullptr on failure).
79 const std::string file_name = resource::directory() + "/data/sphere.plm";
80 auto mesh = PolyMeshIO::load(file_name);
81 if (!mesh) {
82 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
83 return EXIT_FAILURE;
84 }
85 // Report the four element counts that characterize a volumetric (polyhedral) mesh.
86 std::cout << "mesh loaded. " << std::endl;
87 std::cout << "\tvertices: " << mesh->n_vertices() << std::endl;
88 std::cout << "\tedges: " << mesh->n_edges() << std::endl;
89 std::cout << "\tfaces: " << mesh->n_faces() << std::endl;
90 std::cout << "\tcells: " << mesh->n_cells() << std::endl;
91
92 // ...
93 // Do fancy stuff with the mesh
94 // ...
95
96 // Write the mesh to a new file. save() returns true on success; the ".plm" extension again
97 // selects the ASCII format. The copy lands in the current working directory.
98 const std::string save_file_name = "./sphere-copy.plm";
99 if (PolyMeshIO::save(save_file_name, mesh.get()))
100 std::cout << "mesh saved to \'" << save_file_name << "\'" << std::endl;
101 else
102 std::cerr << "failed create the new file" << std::endl;
103
104 return EXIT_SUCCESS;
105}
Definition collider.cpp:182
See also
PolyMesh, PolyMeshIO, Tutorial_112_PolyMesh, Tutorial_107_SurfaceMesh_IO