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 
ReadPointCloudFromXYZN(const std::string & filename,PointCloud & pointcloud)34 bool ReadPointCloudFromXYZN(const std::string &filename, PointCloud &pointcloud)
35 {
36     FILE *file = fopen(filename.c_str(), "r");
37     if (file == NULL) {
38         PrintWarning("Read XYZN 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, nx, ny, nz;
44     pointcloud.Clear();
45 
46     while (fgets(line_buffer, DEFAULT_IO_BUFFER_SIZE, file)) {
47         if (sscanf(line_buffer, "%lf %lf %lf %lf %lf %lf",
48                 &x, &y, &z, &nx, &ny, &nz) == 6)
49         {
50             pointcloud.points_.push_back(Eigen::Vector3d(x, y, z));
51             pointcloud.normals_.push_back(Eigen::Vector3d(nx, ny, nz));
52         }
53     }
54 
55     fclose(file);
56     return true;
57 }
58 
WritePointCloudToXYZN(const std::string & filename,const PointCloud & pointcloud,bool write_ascii,bool compressed)59 bool WritePointCloudToXYZN(const std::string &filename,
60         const PointCloud &pointcloud, bool write_ascii/* = false*/,
61         bool compressed/* = false*/)
62 {
63     if (pointcloud.HasNormals() == false) {
64         return false;
65     }
66 
67     FILE *file = fopen(filename.c_str(), "w");
68     if (file == NULL) {
69         PrintWarning("Write XYZN failed: unable to open file: %s\n", filename.c_str());
70         return false;
71     }
72 
73     for (size_t i = 0; i < pointcloud.points_.size(); i++) {
74         const Eigen::Vector3d &point = pointcloud.points_[i];
75         const Eigen::Vector3d &normal = pointcloud.normals_[i];
76         if (fprintf(file, "%.10f %.10f %.10f %.10f %.10f %.10f\n",
77                 point(0), point(1), point(2),
78                 normal(0), normal(1), normal(2)) < 0)
79         {
80             PrintWarning("Write XYZN failed: unable to write file: %s\n", filename.c_str());
81             fclose(file);
82             return false;    // error happens during writing.
83         }
84     }
85 
86     fclose(file);
87     return true;
88 }
89 
90 }    // namespace three
91