ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PoseGraphIO.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "io/PoseGraphIO.h"
9 
10 #include <FileSystem.h>
11 #include <IJsonConvertibleIO.h>
12 #include <Logging.h>
13 
14 #include <unordered_map>
15 
16 namespace cloudViewer {
17 
18 namespace {
19 using namespace io;
20 
21 bool ReadPoseGraphFromJSON(const std::string &filename,
23  return ReadIJsonConvertible(filename, pose_graph);
24 }
25 
26 bool WritePoseGraphToJSON(
27  const std::string &filename,
28  const pipelines::registration::PoseGraph &pose_graph) {
29  return WriteIJsonConvertibleToJSON(filename, pose_graph);
30 }
31 
32 static const std::unordered_map<
33  std::string,
34  std::function<bool(const std::string &,
36  file_extension_to_pose_graph_read_function{
37  {"json", ReadPoseGraphFromJSON},
38  };
39 
40 static const std::unordered_map<
41  std::string,
42  std::function<bool(const std::string &,
44  file_extension_to_pose_graph_write_function{
45  {"json", WritePoseGraphToJSON},
46  };
47 
48 } // unnamed namespace
49 
50 namespace io {
51 using namespace cloudViewer;
52 
53 std::shared_ptr<pipelines::registration::PoseGraph> CreatePoseGraphFromFile(
54  const std::string &filename) {
55  auto pose_graph = std::make_shared<pipelines::registration::PoseGraph>();
56  ReadPoseGraph(filename, *pose_graph);
57  return pose_graph;
58 }
59 
60 bool ReadPoseGraph(const std::string &filename,
62  std::string filename_ext =
64  if (filename_ext.empty()) {
66  "Read pipelines::registration::PoseGraph failed: unknown file "
67  "extension.");
68  return false;
69  }
70  auto map_itr =
71  file_extension_to_pose_graph_read_function.find(filename_ext);
72  if (map_itr == file_extension_to_pose_graph_read_function.end()) {
74  "Read pipelines::registration::PoseGraph failed: unknown file "
75  "extension.");
76  return false;
77  }
78  return map_itr->second(filename, pose_graph);
79 }
80 
81 bool WritePoseGraph(const std::string &filename,
82  const pipelines::registration::PoseGraph &pose_graph) {
83  std::string filename_ext =
85  if (filename_ext.empty()) {
87  "Write pipelines::registration::PoseGraph failed: unknown file "
88  "extension.");
89  return false;
90  }
91  auto map_itr =
92  file_extension_to_pose_graph_write_function.find(filename_ext);
93  if (map_itr == file_extension_to_pose_graph_write_function.end()) {
95  "Write pipelines::registration::PoseGraph failed: unknown file "
96  "extension.");
97  return false;
98  }
99  return map_itr->second(filename, pose_graph);
100 }
101 
102 } // namespace io
103 } // namespace cloudViewer
std::string filename
Data structure defining the pose graph.
Definition: PoseGraph.h:96
#define LogWarning(...)
Definition: Logging.h:72
bool ReadPoseGraph(const std::string &filename, pipelines::registration::PoseGraph &pose_graph)
Definition: PoseGraphIO.cpp:60
std::shared_ptr< pipelines::registration::PoseGraph > CreatePoseGraphFromFile(const std::string &filename)
Definition: PoseGraphIO.cpp:53
bool WriteIJsonConvertibleToJSON(const std::string &filename, const cloudViewer::utility::IJsonConvertible &object)
bool ReadIJsonConvertible(const std::string &filename, cloudViewer::utility::IJsonConvertible &object)
bool WritePoseGraph(const std::string &filename, const pipelines::registration::PoseGraph &pose_graph)
Definition: PoseGraphIO.cpp:81
cloudViewer::pipelines::registration::PoseGraph PoseGraph
Definition: SLACOptimizer.h:22
std::string GetFileExtensionInLowerCase(const std::string &filename)
Definition: FileSystem.cpp:281
Generic file read and write utility for python interface.