AutoAPMS
Resilient Robot Mission Management
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
convert.cpp
1// Copyright 2024 Robin Müller
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "auto_apms_behavior_tree_core/convert.hpp"
16
17#include <sstream>
18
19#include "auto_apms_util/string.hpp"
20
22namespace BT
23{
24
25template <>
26std::vector<uint8_t> convertFromString<std::vector<uint8_t>>(StringView str)
27{
28 auto parts = BT::splitString(str, ';');
29 std::vector<uint8_t> output;
30 output.reserve(parts.size());
31 for (const StringView & part : parts) {
32 output.push_back(convertFromString<uint8_t>(part));
33 }
34 return output;
35}
36
37template <>
38std::vector<bool> convertFromString<std::vector<bool>>(StringView str)
39{
40 auto parts = BT::splitString(str, ';');
41 std::vector<bool> output;
42 output.reserve(parts.size());
43 for (const StringView & part : parts) {
44 output.push_back(convertFromString<bool>(part));
45 }
46 return output;
47}
48
49template <>
50std::vector<int64_t> convertFromString<std::vector<int64_t>>(StringView str)
51{
52 auto parts = BT::splitString(str, ';');
53 std::vector<int64_t, std::allocator<int64_t>> output;
54 output.reserve(parts.size());
55 for (const StringView & part : parts) {
56 output.push_back(convertFromString<int64_t>(part));
57 }
58 return output;
59}
60
61template <>
62Eigen::MatrixXd convertFromString<Eigen::MatrixXd>(StringView str)
63{
64 const std::string input_str(str);
65 std::vector<std::vector<double>> values;
66 std::stringstream matrix_stream(input_str);
67 std::string row_str;
68
69 // Parse each row (separated by commas)
70 while (std::getline(matrix_stream, row_str, ',')) {
71 std::stringstream row_stream(row_str);
72 std::string value_str;
73 std::vector<double> row_values;
74
75 // Parse each value in the row (separated by semicolons)
76 while (std::getline(row_stream, value_str, ';')) {
77 row_values.push_back(convertFromString<double>(value_str));
78 }
79
80 if (!row_values.empty()) {
81 values.push_back(row_values);
82 }
83 }
84
85 // Create the matrix
86 if (values.empty()) {
87 return Eigen::MatrixXd();
88 }
89 size_t rows = values.size();
90 size_t cols = values[0].size();
91 Eigen::MatrixXd matrix(rows, cols);
92
93 for (size_t i = 0; i < rows; ++i) {
94 for (size_t j = 0; j < std::min(cols, values[i].size()); ++j) {
95 matrix(i, j) = values[i][j];
96 }
97 }
98 return matrix;
99}
100
101template <>
102std::string toStr(const std::vector<uint8_t> & value)
103{
104 return auto_apms_util::join(value, ";");
105}
106
107template <>
108std::string toStr<std::vector<bool>>(const std::vector<bool> & value)
109{
110 return auto_apms_util::join(value, ";");
111}
112
113template <>
114std::string toStr<std::vector<int64_t>>(const std::vector<int64_t> & value)
115{
116 return auto_apms_util::join(value, ";");
117}
118
119template <>
120std::string toStr<std::vector<double>>(const std::vector<double> & value)
121{
122 return auto_apms_util::join(value, ";");
123}
124
125template <>
126std::string toStr<Eigen::MatrixXd>(const Eigen::MatrixXd & value)
127{
128 Eigen::IOFormat fmt(Eigen::StreamPrecision, Eigen::DontAlignCols, ";", ",", "", "", "", "");
129 std::stringstream ss;
130 ss << value.format(fmt);
131 return ss.str();
132}
133
134template <>
135std::string toStr<std::vector<std::string>>(const std::vector<std::string> & value)
136{
137 return auto_apms_util::join(value, ";");
138}
139
140} // namespace BT
const char * toStr(const ActionNodeErrorCode &err)
Convert the action error code to string.