1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26
27 #include <IO/ClassIO/PointCloudIO.h>
28
29 #include <cstdio>
30 #include <Core/Utility/Console.h>
31
32 namespace three{
33
ReadPointCloudFromXYZ(const std::string & filename,PointCloud & pointcloud)34 bool ReadPointCloudFromXYZ(const std::string &filename, PointCloud &pointcloud)
35 {
36 FILE *file = fopen(filename.c_str(), "r");
37 if (file == NULL) {
38 PrintWarning("Read XYZ failed: unable to open file: %s\n", filename.c_str());
39 return false;
40 }
41
42 char line_buffer[DEFAULT_IO_BUFFER_SIZE];
43 double x, y, z;
44 pointcloud.Clear();
45
46 while (fgets(line_buffer, DEFAULT_IO_BUFFER_SIZE, file)) {
47 if (sscanf(line_buffer, "%lf %lf %lf", &x, &y, &z) == 3) {
48 pointcloud.points_.push_back(Eigen::Vector3d(x, y, z));
49 }
50 }
51
52 fclose(file);
53 return true;
54 }
55
WritePointCloudToXYZ(const std::string & filename,const PointCloud & pointcloud,bool write_ascii,bool compressed)56 bool WritePointCloudToXYZ(const std::string &filename,
57 const PointCloud &pointcloud, bool write_ascii/* = false*/,
58 bool compressed/* = false*/)
59 {
60 FILE *file = fopen(filename.c_str(), "w");
61 if (file == NULL) {
62 PrintWarning("Write XYZ failed: unable to open file: %s\n", filename.c_str());
63 return false;
64 }
65
66 for (size_t i = 0; i < pointcloud.points_.size(); i++) {
67 const Eigen::Vector3d &point = pointcloud.points_[i];
68 if (fprintf(file, "%.10f %.10f %.10f\n",
69 point(0), point(1), point(2)) < 0)
70 {
71 PrintWarning("Write XYZ failed: unable to write file: %s\n", filename.c_str());
72 fclose(file);
73 return false; // error happens during writing.
74 }
75 }
76
77 fclose(file);
78 return true;
79 }
80
81 } // namespace three
82