XRPrimer (C++ API)  0.6.0
json_helper_internal.h
Go to the documentation of this file.
1 // Copyright (c) OpenXRLab. All rights reserved.
2 
3 #pragma once
4 
6 #include <iostream>
7 #include <json/json.h>
8 
9 template <typename T>
10 static void SaveMatrixToJson(Json::Value &obj, const std::string &key, T mat) {
11  int rows = mat.rows();
12  int cols = mat.cols();
13 
14  Json::Value array;
15  if (cols == 1) {
16  for (int r = 0; r < rows; r++) {
17  array.append(mat(r, 0));
18  }
19  } else {
20  for (int r = 0; r < rows; r++) {
21  Json::Value inner_arr;
22  for (int c = 0; c < cols; c++) {
23  inner_arr.append(mat(r, c));
24  }
25  array.append(inner_arr);
26  }
27  }
28  obj[key] = array;
29 }
30 
31 template <typename T>
32 static bool LoadMatrixFromJson(const Json::Value &obj, const std::string &key,
33  T &mat) {
34  int rows = mat.rows();
35  int cols = mat.cols();
36 
37  Json::Value array = obj[key];
38 
39  if (array.empty()) {
40  std::cerr << "Not found key:[" << key << "] in json file" << std::endl;
41  return false;
42  }
43 
44  if (cols == 1) {
45  for (int r = 0; r < rows; r++) {
46  mat(r, 0) = array[r].asFloat();
47  }
48  } else {
49  for (int r = 0; r < rows; r++) {
50  Json::Value inner_arr = array[r];
51  for (int c = 0; c < cols; c++) {
52  mat(r, c) = inner_arr[c].asFloat();
53  }
54  }
55  }
56  return true;
57 }
58 
59 static void SaveBaseCameraParameter(Json::Value &obj,
60  const BaseCameraParameter &param) {
61  obj["class_name"] = param.ClassName();
62  obj["name"] = param.name_;
63  obj["height"] = param.height_;
64  obj["width"] = param.width_;
65  SaveMatrixToJson(obj, "intrinsic", param.intrinsic_);
66  SaveMatrixToJson(obj, "extrinsic_r", param.extrinsic_r_);
67  SaveMatrixToJson(obj, "extrinsic_t", param.extrinsic_t_);
68  obj["convention"] = param.convention_;
69  obj["world2cam"] = param.world2cam_;
70 }
71 
72 static bool LoadBaseCameraParameter(const Json::Value &obj,
73  BaseCameraParameter &param) {
74 
75  std::string cls_name = obj["class_name"].asString();
76  bool ret = false;
77 
78  if (cls_name != "" && cls_name != param.ClassName()) {
79  std::cerr << "Invalid " << param.ClassName() << " format json file\n";
80  return ret;
81  }
82 
83  param.name_ = obj["name"].asString();
84  param.height_ = obj["height"].asInt();
85  param.width_ = obj["width"].asInt();
86  param.convention_ = obj["convention"].asString();
87  param.world2cam_ = obj["world2cam"].asBool();
88 
89  ret = LoadMatrixFromJson(obj, "intrinsic", param.intrinsic_);
90  ret &= LoadMatrixFromJson(obj, "extrinsic_r", param.extrinsic_r_);
91  ret &= LoadMatrixFromJson(obj, "extrinsic_t", param.extrinsic_t_);
92  return ret;
93 }
94 
95 static std::string JsonToString(const Json::Value &obj) {
96  Json::StyledWriter writer;
97  return writer.write(obj);
98 }
99 
100 static bool JsonToFile(const Json::Value &obj, const std::string &filename) {
101  std::ofstream jsonfile(filename, std::ios::out | std::ios::trunc);
102  if (jsonfile.is_open()) {
103  Json::StyledWriter writer;
104  jsonfile << writer.write(obj);
105  jsonfile.close();
106  return true;
107  }
108  std::cerr << "Save Failed!, filename: " << filename << std::endl;
109  return false;
110 }
111 
112 static bool JsonFromFile(Json::Value &obj, const std::string &filename) {
113  Json::Reader reader;
114  std::ifstream jsonfile(filename);
115  if (jsonfile.is_open()) {
116  if (reader.parse(jsonfile, obj, false)) {
117  return true;
118  }
119  }
120  std::cerr << "Parse Failed!, filename: " << filename << std::endl;
121  return false;
122 }
123 
124 static bool check_and_load_float(float *val, const Json::Value &obj,
125  const std::string &key) {
126  if (obj[key].empty()) {
127  std::cerr << "Not found key:[" << key << "] in json file" << std::endl;
128  return false;
129  }
130  *val = obj[key].asFloat();
131  return true;
132 };
Eigen::Matrix4f intrinsic_
Definition: camera.h:56
std::string name_
Definition: camera.h:55
virtual std::string ClassName() const =0
std::string convention_
Definition: camera.h:62
Eigen::Matrix3f extrinsic_r_
Definition: camera.h:57
Contains the base camera parameter.
Definition: camera.h:16
bool world2cam_
Definition: camera.h:61
int height_
Definition: camera.h:60
int width_
Definition: camera.h:59
std::ostream & cerr()
Eigen::Vector3f extrinsic_t_
Definition: camera.h:58