GLIM
Loading...
Searching...
No Matches
convert_to_string.hpp
1#pragma once
2
3#include <string>
4#include <vector>
5#include <fmt/format.h>
6#include <Eigen/Core>
7#include <Eigen/Geometry>
8
9namespace glim {
10
11// Convertion to string
12
13template <typename T>
14std::string convert_to_string(const T& value) {
15 return fmt::format("{}", value);
16}
17
18template <typename T2>
19std::string convert_to_string(const std::vector<T2>& values) {
20 std::stringstream sst;
21 sst << "[";
22 for (int i = 0; i < values.size(); i++) {
23 if (i) {
24 sst << ",";
25 }
26 sst << convert_to_string(values[i]);
27 }
28 sst << "]";
29 return sst.str();
30}
31
32template <int D>
33std::string convert_to_string(const Eigen::Matrix<double, D, 1>& value) {
34 std::stringstream sst;
35 sst << "vec(";
36 for (int i = 0; i < value.size(); i++) {
37 if (i) {
38 sst << ",";
39 }
40 sst << fmt::format("{:.6f}", value[i]);
41 }
42 sst << ")";
43 return sst.str();
44}
45
46template <>
47inline std::string convert_to_string(const Eigen::Quaterniond& quat) {
48 return fmt::format("quat({:.6f},{:.6f},{:.6f},{:.6f})", quat.x(), quat.y(), quat.z(), quat.w());
49}
50
51template <>
52inline std::string convert_to_string(const Eigen::Isometry3d& pose) {
53 const Eigen::Vector3d trans(pose.translation());
54 const Eigen::Quaterniond quat(pose.linear());
55 return fmt::format("se3({:.6f},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f},{:.6f})", trans.x(), trans.y(), trans.z(), quat.x(), quat.y(), quat.z(), quat.w());
56}
57} // namespace glim