$darkmode
with_eigen.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
26 #pragma once
27 #ifndef FABLE_JSON_WITH_EIGEN_HPP_
28 #define FABLE_JSON_WITH_EIGEN_HPP_
29 
30 #include <nlohmann/json.hpp>
31 
32 #include <Eigen/Geometry>
33 
34 namespace nlohmann {
35 
36 template <>
37 struct adl_serializer<Eigen::Vector3d> {
38  static void to_json(json& j, const Eigen::Vector3d& v) {
39  j = json{
40  {"x", v.x()},
41  {"y", v.y()},
42  {"z", v.z()},
43  };
44  }
45 
46  static void from_json(const json& j, Eigen::Vector3d& v) {
47  v.x() = j["x"].get<double>();
48  v.y() = j["y"].get<double>();
49  v.z() = j["z"].get<double>();
50  }
51 };
52 
53 template <>
54 struct adl_serializer<Eigen::Quaterniond> {
55  static void to_json(json& j, const Eigen::Quaterniond& q) {
56  j = json{
57  {"w", q.w()},
58  {"x", q.x()},
59  {"y", q.y()},
60  {"z", q.z()},
61  };
62  }
63 
64  static void from_json(const json& j, Eigen::Quaterniond& q) {
65  q.w() = j["w"].get<double>();
66  q.x() = j["x"].get<double>();
67  q.y() = j["y"].get<double>();
68  q.z() = j["z"].get<double>();
69  }
70 };
71 
72 template <>
73 struct adl_serializer<Eigen::Isometry3d> {
74  static void to_json(json& j, const Eigen::Isometry3d& o) {
75  Eigen::Vector3d trans = o.translation();
76  j = json{
77  {"translation", trans},
78  {"rotation", Eigen::Quaterniond(o.rotation())},
79  };
80  }
81 
82  static void from_json(const json& j, Eigen::Isometry3d& o) {
83  o.setIdentity();
84  o.linear() = j["rotation"].get<Eigen::Quaterniond>().matrix();
85  o.translation() = j["translation"].get<Eigen::Vector3d>();
86  }
87 };
88 
89 } // namespace nlohmann
90 
91 #endif // FABLE_JSON_WITH_EIGEN_HPP_
Definition: confable.hpp:132