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 "TriangleMeshIO.h"
28 
29 #include <unordered_map>
30 #include <Core/Utility/Console.h>
31 #include <Core/Utility/FileSystem.h>
32 
33 namespace three{
34 
35 namespace {
36 
37 static const std::unordered_map<std::string,
38         std::function<bool(const std::string &, TriangleMesh &)>>
39         file_extension_to_trianglemesh_read_function
40         {{"ply", ReadTriangleMeshFromPLY},
41         };
42 
43 static const std::unordered_map<std::string,
44         std::function<bool(const std::string &, const TriangleMesh &,
45         const bool, const bool)>>
46         file_extension_to_trianglemesh_write_function
47         {{"ply", WriteTriangleMeshToPLY},
48         };
49 
50 }    // unnamed namespace
51 
CreateMeshFromFile(const std::string & filename)52 std::shared_ptr<TriangleMesh> CreateMeshFromFile(const std::string &filename)
53 {
54     auto mesh = std::make_shared<TriangleMesh>();
55     ReadTriangleMesh(filename, *mesh);
56     return mesh;
57 }
58 
ReadTriangleMesh(const std::string & filename,TriangleMesh & mesh)59 bool ReadTriangleMesh(const std::string &filename, TriangleMesh &mesh)
60 {
61     std::string filename_ext =
62             filesystem::GetFileExtensionInLowerCase(filename);
63     if (filename_ext.empty()) {
64         PrintWarning("Read TriangleMesh failed: unknown file extension.\n");
65         return false;
66     }
67     auto map_itr =
68             file_extension_to_trianglemesh_read_function.find(filename_ext);
69     if (map_itr == file_extension_to_trianglemesh_read_function.end()) {
70         PrintWarning("Read TriangleMesh failed: unknown file extension.\n");
71         return false;
72     }
73     bool success = map_itr->second(filename, mesh);
74     PrintDebug("Read TriangleMesh: %d triangles and %d vertices.\n",
75             (int)mesh.triangles_.size(), (int)mesh.vertices_.size());
76     return success;
77 }
78 
WriteTriangleMesh(const std::string & filename,const TriangleMesh & mesh,bool write_ascii,bool compressed)79 bool WriteTriangleMesh(const std::string &filename, const TriangleMesh &mesh,
80         bool write_ascii/* = false*/, bool compressed/* = false*/)
81 {
82     std::string filename_ext =
83             filesystem::GetFileExtensionInLowerCase(filename);
84     if (filename_ext.empty()) {
85         PrintWarning("Write TriangleMesh failed: unknown file extension.\n");
86         return false;
87     }
88     auto map_itr =
89             file_extension_to_trianglemesh_write_function.find(filename_ext);
90     if (map_itr == file_extension_to_trianglemesh_write_function.end()) {
91         PrintWarning("Write TriangleMesh failed: unknown file extension.\n");
92         return false;
93     }
94     bool success = map_itr->second(filename, mesh, write_ascii, compressed);
95     PrintDebug("Write TriangleMesh: %d triangles and %d vertices.\n",
96             (int)mesh.triangles_.size(), (int)mesh.vertices_.size());
97     return success;
98 }
99 
100 }    // namespace three
101