Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_103_PointCloud_IO

Loading a point cloud from disk and writing it back out, including a hand-rolled text export.

Overview

This console tutorial covers file input/output for PointCloud. It loads the bundled "bunny" cloud, reports how many points it contains, and then exports the data to a plain-text file, one point per line. It complements Tutorial_102_PointCloud_Property by showing that the same named properties used to store attributes are also what get read from and written to files.

The technique

Easy3D dispatches on the file extension: PointCloudIO::load()/save() understand several point formats (bin, xyz/bxyz, ply, las/laz, vg/bvg), so the same call works regardless of the concrete format. Here we load Easy3D's compact binary ".bin" file. For the export we deliberately do NOT call save(); instead we stream the attributes ourselves to demonstrate that a loaded cloud is just properties we can iterate. Each output line holds the xyz coordinates, followed by the normal and the color when those properties exist. Because normals/colors are optional, we test each property handle for validity before dereferencing it: get_vertex_property<T>() returns an invalid (null) handle when the name is absent, and a VertexProperty converts to bool so that "if (normals)" is a safe existence check.

Easy3D API

Running it

Console-only. It prints the loaded point count, then "saving the point cloud..." and "point cloud saved to './bunny-copy.txt'". The export is written to the current working directory; open it to see one "x y z [nx ny nz] [r g b]" line per point.

1
27#include <easy3d/core/point_cloud.h>
28#include <easy3d/fileio/point_cloud_io.h>
29#include <easy3d/util/resource.h>
30#include <easy3d/util/initializer.h>
31
75using namespace easy3d;
76
77int main(int argc, char **argv) {
78 // initialize Easy3D.
79 initialize();
80
81 // Read the point cloud from a known file.
82 auto cloud = PointCloudIO::load(resource::directory() + "/data/bunny.bin");
83 if (!cloud) {
84 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
85 return EXIT_FAILURE;
86 }
87
88 std::cout << "point cloud has " << cloud->n_vertices() << " points" << std::endl;
89
90 // You can save the model into a file of one of the supported file formats using 'PointCloudIO::save()'.
91 // In this example, let's save the model into a file with a special format. In each line we store the
92 // x, y, z coordinates, followed by the normal (nx, ny, nz) and color (r, g, b) if they exist.
93 std::ofstream output("./bunny-copy.txt");
94 if (output.is_open()) { // if the file has been successfully created
95 // The point coordinates. "v:point" always exists for a loaded cloud.
96 PointCloud::VertexProperty<vec3> points = cloud->get_vertex_property<vec3>("v:point");
97 // The point normals. Optional: the handle is invalid (null) if the file stored none.
98 PointCloud::VertexProperty<vec3> normals = cloud->get_vertex_property<vec3>("v:normal");
99 // The point colors. Optional too, so we test it for validity before use below.
100 PointCloud::VertexProperty<vec3> colors = cloud->get_vertex_property<vec3>("v:color");
101 std::cout << "saving the point cloud..." << std::endl;
102
103 for (auto v : cloud->vertices()) {
104 output << points[v];
105 if (normals) // if normals exist
106 output << " " << normals[v];
107 if (colors) // if colors exist
108 output << " " << colors[v];
109 output << std::endl;
110 }
111 std::cout << "point cloud saved to './bunny-copy.txt'" << std::endl;
112 }
113
114 return EXIT_SUCCESS;
115}
Vertex property of type T.
Definition point_cloud.h:152
Definition collider.cpp:182
See also
PointCloud, PointCloudIO, Tutorial_102_PointCloud_Property, Tutorial_107_SurfaceMesh_IO