ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
PoseGraph.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 
9 
10 #include <Logging.h>
11 #include <json/json.h>
12 
13 namespace cloudViewer {
14 namespace pipelines {
15 namespace registration {
16 
18 
19 bool PoseGraphNode::ConvertToJsonValue(Json::Value &value) const {
20  value["class_name"] = "PoseGraphNode";
21  value["version_major"] = 1;
22  value["version_minor"] = 0;
23 
24  Json::Value pose_object;
25  if (!EigenMatrix4dToJsonArray(pose_, pose_object)) {
26  return false;
27  }
28  value["pose"] = pose_object;
29  return true;
30 }
31 
32 bool PoseGraphNode::ConvertFromJsonValue(const Json::Value &value) {
33  if (!value.isObject()) {
35  "PoseGraphNode read JSON failed: unsupported json format.");
36  return false;
37  }
38  if (value.get("class_name", "").asString() != "PoseGraphNode" ||
39  value.get("version_major", 1).asInt() != 1 ||
40  value.get("version_minor", 0).asInt() != 0) {
42  "PoseGraphNode read JSON failed: unsupported json format.");
43  return false;
44  }
45 
46  const Json::Value &pose_object = value["pose"];
47  if (!EigenMatrix4dFromJsonArray(pose_, pose_object)) {
48  return false;
49  }
50  return true;
51 }
52 
54 
55 bool PoseGraphEdge::ConvertToJsonValue(Json::Value &value) const {
56  value["class_name"] = "PoseGraphEdge";
57  value["version_major"] = 1;
58  value["version_minor"] = 0;
59 
60  value["source_node_id"] = source_node_id_;
61  value["target_node_id"] = target_node_id_;
62  value["uncertain"] = uncertain_;
63  value["confidence"] = confidence_;
64  Json::Value transformation_object;
65  if (!EigenMatrix4dToJsonArray(transformation_, transformation_object)) {
66  return false;
67  }
68  value["transformation"] = transformation_object;
69  Json::Value information_object;
70  if (!EigenMatrix6dToJsonArray(information_, information_object)) {
71  return false;
72  }
73  value["information"] = information_object;
74  return true;
75 }
76 
77 bool PoseGraphEdge::ConvertFromJsonValue(const Json::Value &value) {
78  if (!value.isObject()) {
80  "PoseGraphEdge read JSON failed: unsupported json format.");
81  return false;
82  }
83  if (value.get("class_name", "").asString() != "PoseGraphEdge" ||
84  value.get("version_major", 1).asInt() != 1 ||
85  value.get("version_minor", 0).asInt() != 0) {
87  "PoseGraphEdge read JSON failed: unsupported json format.");
88  return false;
89  }
90 
91  source_node_id_ = value.get("source_node_id", -1).asInt();
92  target_node_id_ = value.get("target_node_id", -1).asInt();
93  uncertain_ = value.get("uncertain", false).asBool();
94  confidence_ = value.get("confidence", 1.0).asDouble();
95  const Json::Value &transformation_object = value["transformation"];
96  if (!EigenMatrix4dFromJsonArray(transformation_, transformation_object)) {
97  return false;
98  }
99  const Json::Value &information_object = value["information"];
100  if (!EigenMatrix6dFromJsonArray(information_, information_object)) {
101  return false;
102  }
103  return true;
104 }
105 
107 
109 
110 bool PoseGraph::ConvertToJsonValue(Json::Value &value) const {
111  value["class_name"] = "PoseGraph";
112  value["version_major"] = 1;
113  value["version_minor"] = 0;
114 
115  Json::Value node_array;
116  for (const auto &node : nodes_) {
117  Json::Value node_object;
118  if (!node.ConvertToJsonValue(node_object)) {
119  return false;
120  }
121  node_array.append(node_object);
122  }
123  value["nodes"] = node_array;
124 
125  Json::Value edge_array;
126  for (const auto &edge : edges_) {
127  Json::Value edge_object;
128  if (!edge.ConvertToJsonValue(edge_object)) {
129  return false;
130  }
131  edge_array.append(edge_object);
132  }
133  value["edges"] = edge_array;
134  return true;
135 }
136 
137 bool PoseGraph::ConvertFromJsonValue(const Json::Value &value) {
138  if (!value.isObject()) {
140  "PoseGraph read JSON failed: unsupported json format.");
141  return false;
142  }
143  if (value.get("class_name", "").asString() != "PoseGraph" ||
144  value.get("version_major", 1).asInt() != 1 ||
145  value.get("version_minor", 0).asInt() != 0) {
147  "PoseGraph read JSON failed: unsupported json format.");
148  return false;
149  }
150 
151  const Json::Value &node_array = value["nodes"];
152  if (node_array.empty()) {
153  utility::LogWarning("PoseGraph read JSON failed: empty nodes.");
154  return false;
155  }
156  nodes_.clear();
157  for (int i = 0; i < (int)node_array.size(); i++) {
158  const Json::Value &node_object = node_array[i];
159  PoseGraphNode new_node;
160  if (!new_node.ConvertFromJsonValue(node_object)) {
161  return false;
162  }
163  nodes_.push_back(new_node);
164  }
165 
166  const Json::Value &edge_array = value["edges"];
167  if (edge_array.empty()) {
168  utility::LogWarning("PoseGraph read JSON failed: empty edges.");
169  return false;
170  }
171  edges_.clear();
172  for (int i = 0; i < (int)edge_array.size(); i++) {
173  const Json::Value &edge_object = edge_array[i];
174  PoseGraphEdge new_edge;
175  if (!new_edge.ConvertFromJsonValue(edge_object)) {
176  return false;
177  }
178  edges_.push_back(new_edge);
179  }
180  return true;
181 }
182 
183 } // namespace registration
184 } // namespace pipelines
185 } // namespace cloudViewer
Eigen::Matrix4d_u transformation_
Transformation matrix.
Definition: PoseGraph.h:76
int target_node_id_
Target PoseGraphNode id.
Definition: PoseGraph.h:74
int source_node_id_
Source PoseGraphNode id.
Definition: PoseGraph.h:72
bool uncertain_
Whether the edge is uncertain.
Definition: PoseGraph.h:83
bool ConvertToJsonValue(Json::Value &value) const override
Definition: PoseGraph.cpp:55
bool ConvertFromJsonValue(const Json::Value &value) override
Definition: PoseGraph.cpp:77
Eigen::Matrix6d_u information_
Information matrix.
Definition: PoseGraph.h:78
double confidence_
Confidence value of the edge.
Definition: PoseGraph.h:90
bool ConvertToJsonValue(Json::Value &value) const override
Definition: PoseGraph.cpp:19
bool ConvertFromJsonValue(const Json::Value &value) override
Definition: PoseGraph.cpp:32
bool ConvertFromJsonValue(const Json::Value &value) override
Definition: PoseGraph.cpp:137
bool ConvertToJsonValue(Json::Value &value) const override
Definition: PoseGraph.cpp:110
std::vector< PoseGraphNode > nodes_
List of PoseGraphNode.
Definition: PoseGraph.h:108
std::vector< PoseGraphEdge > edges_
List of PoseGraphEdge.
Definition: PoseGraph.h:111
static bool EigenMatrix6dFromJsonArray(Eigen::Matrix6d &mat, const Json::Value &value)
static bool EigenMatrix6dToJsonArray(const Eigen::Matrix6d &mat, Json::Value &value)
static bool EigenMatrix4dFromJsonArray(Eigen::Matrix4d &mat, const Json::Value &value)
static bool EigenMatrix4dToJsonArray(const Eigen::Matrix4d &mat, Json::Value &value)
#define LogWarning(...)
Definition: Logging.h:72
Generic file read and write utility for python interface.