AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
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 <>
62std::vector<float> convertFromString<std::vector<float>>(StringView str)
63{
64 auto parts = splitString(str, ';');
65 std::vector<float> output;
66 output.reserve(parts.size());
67 for (const StringView & part : parts) {
68 output.push_back(convertFromString<float>(part));
69 }
70 return output;
71}
72
73template <>
74Eigen::MatrixXd convertFromString<Eigen::MatrixXd>(StringView str)
75{
76 const std::string input_str(str);
77 std::vector<std::vector<double>> values;
78 std::stringstream matrix_stream(input_str);
79 std::string row_str;
80
81 // Parse each row (separated by commas)
82 while (std::getline(matrix_stream, row_str, ',')) {
83 std::stringstream row_stream(row_str);
84 std::string value_str;
85 std::vector<double> row_values;
86
87 // Parse each value in the row (separated by semicolons)
88 while (std::getline(row_stream, value_str, ';')) {
89 row_values.push_back(convertFromString<double>(value_str));
90 }
91
92 if (!row_values.empty()) {
93 values.push_back(row_values);
94 }
95 }
96
97 // Create the matrix
98 if (values.empty()) {
99 return Eigen::MatrixXd();
100 }
101 size_t rows = values.size();
102 size_t cols = values[0].size();
103 Eigen::MatrixXd matrix(rows, cols);
104
105 for (size_t i = 0; i < rows; ++i) {
106 for (size_t j = 0; j < std::min(cols, values[i].size()); ++j) {
107 matrix(i, j) = values[i][j];
108 }
109 }
110 return matrix;
111}
112
113template <>
114std::string toStr(const std::vector<uint8_t> & value)
115{
116 return auto_apms_util::join(value, ";");
117}
118
119template <>
120std::string toStr<std::vector<bool>>(const std::vector<bool> & value)
121{
122 return auto_apms_util::join(value, ";");
123}
124
125template <>
126std::string toStr<std::vector<int64_t>>(const std::vector<int64_t> & value)
127{
128 return auto_apms_util::join(value, ";");
129}
130
131template <>
132std::string toStr<std::vector<double>>(const std::vector<double> & value)
133{
134 return auto_apms_util::join(value, ";");
135}
136
137template <>
138std::string toStr<Eigen::MatrixXd>(const Eigen::MatrixXd & value)
139{
140 Eigen::IOFormat fmt(Eigen::StreamPrecision, Eigen::DontAlignCols, ";", ",", "", "", "", "");
141 std::stringstream ss;
142 ss << value.format(fmt);
143 return ss.str();
144}
145
146template <>
147std::string toStr<std::vector<std::string>>(const std::vector<std::string> & value)
148{
149 return auto_apms_util::join(value, ";");
150}
151
152} // namespace BT
std::vector< std::string > splitString(const std::string &str, const std::string &delimiter, bool remove_empty=true)
Split a string into multiple tokens using a specific delimiter string (Delimiter may consist of multi...
Definition string.cpp:24
const char * toStr(const ActionNodeErrorCode &err)
Convert the action error code to string.