Easy3D 2.5.3
Tutorial_103_PointCloud_IO
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
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
32
33using namespace easy3d;
34
35
36// This example shows how to
37// - load a point cloud from a file;
38// - save a point cloud to a file.
39
40
41int main(int argc, char** argv) {
42 // initialize Easy3D.
43 initialize();
44
45 // Read the point cloud from a known file.
46 PointCloud* cloud = PointCloudIO::load(resource::directory() + "/data/bunny.bin");
47 if (!cloud) {
48 LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
49 return EXIT_FAILURE;
50 }
51
52 std::cout << "point cloud has " << cloud->n_vertices() << " points" << std::endl;
53
54 // You can save the model into a file of one of the supported file formats using 'PointCloudIO::save()'.
55 // In this example, let's save the model into a file with a special format. In each line we store the
56 // x, y, z coordinates, followed by the normal (nx, ny, nz) and color (r, g, b) if they exist.
57 std::ofstream output("./bunny-copy.txt");
58 if (output.is_open()) { // if the file has been successfully created
59 // The point coordinates.
61 // The point normals.
62 PointCloud::VertexProperty<vec3> normals = cloud->get_vertex_property<vec3>("v:normal");
63 // The point colors.
65 std::cout << "saving the point cloud..." << std::endl;
66
67 for (auto v : cloud->vertices()) {
68 output << points[v];
69 if (normals) // if normals exist
70 output << " " << normals[v];
71 if (colors) // if colors exist
72 output << " " << colors[v];
73 output << std::endl;
74 }
75 std::cout << "point cloud saved to './bunny-copy.txt'" << std::endl;
76 }
77
78 // Delete the point cloud (i.e., release memory)
79 delete cloud;
80
81 return EXIT_SUCCESS;
82}
83
Vertex property of type T.
Definition: point_cloud.h:107
A data structure for point clouds.
Definition: point_cloud.h:45
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: point_cloud.h:444
VertexProperty< T > get_vertex_property(const std::string &name) const
get the vertex property named name of type T. returns an invalid VertexProperty if the property does ...
Definition: point_cloud.h:340
unsigned int n_vertices() const
returns number of vertices in the cloud
Definition: point_cloud.h:279
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35