Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_107_SurfaceMesh_IO

Loading a SurfaceMesh from a file and saving it to another with SurfaceMeshIO.

Overview

The mesh counterpart of Tutorial_103_PointCloud_IO. This console program loads the bundled "sphere.obj", reports its vertex/edge/face counts, leaves a placeholder where you would process the mesh, and writes it back out as "sphere-copy.obj". It shows the standard load -> use -> save -> free lifecycle for a surface mesh.

The technique

SurfaceMeshIO dispatches on the file extension and supports several mesh formats (ply, obj, off, stl, poly), in ASCII or binary as the format allows. The Wavefront OBJ used here lists vertex positions and face indices (and optionally normals/texture coordinates). On load, Easy3D reconstructs the halfedge connectivity from those faces — building a consistent, orientable 2-manifold — so the result is immediately ready for the adjacency queries of Tutorial_105. Two ownership/return conventions matter: load() returns std::shared_ptr<SurfaceMesh> (nullptr on failure), and save() returns a bool telling you whether the write succeeded. The output extension determines the format written.

Easy3D API

Running it

Console-only. It prints "mesh loaded." followed by the vertex, edge and face counts, then "mesh saved to './sphere-copy.obj'". The copy is written to the current working directory and can be re-loaded by any OBJ-capable tool.

1
27#include <easy3d/core/surface_mesh.h>
28#include <easy3d/fileio/surface_mesh_io.h>
29#include <easy3d/util/resource.h>
30#include <easy3d/util/initializer.h>
31
72using namespace easy3d;
73
74int main(int argc, char **argv) {
75 // initialize Easy3D.
76 initialize();
77
78 // Read a mesh specified by its file name. The ".obj" extension selects the format; load()
79 // returns a shared-ownership handle (nullptr on failure).
80 const std::string file_name = resource::directory() + "/data/sphere.obj";
81 auto mesh = SurfaceMeshIO::load(file_name);
82 if (!mesh) {
83 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
84 return EXIT_FAILURE;
85 }
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
91 // ...
92 // Do fancy stuff with the mesh
93 // ...
94
95 // Write the mesh to a new file. The ".obj" extension picks the output format; save() returns
96 // true on success so we can report whether the copy was actually written.
97 const std::string save_file_name = "./sphere-copy.obj";
98 if (SurfaceMeshIO::save(save_file_name, mesh.get()))
99 std::cout << "mesh saved to \'" << save_file_name << "\'" << std::endl;
100 else
101 std::cerr << "failed create the new file" << std::endl;
102
103 return EXIT_SUCCESS;
104}
Definition collider.cpp:182
See also
SurfaceMesh, SurfaceMeshIO, Tutorial_103_PointCloud_IO, Tutorial_111_Graph_IO, Tutorial_115_PolyMesh_IO