bitrl & cuberl Documentation
Simulation engine for reinforcement learning agents
Loading...
Searching...
No Matches
vector_message.h
Go to the documentation of this file.
1//
2// Created by alex on 11/30/25.
3//
4
5#ifndef VECTOR_MESSAGE_H
6#define VECTOR_MESSAGE_H
7
8#include "bitrl/bitrl_types.h"
9
10#include <iosfwd>
11#include <iostream>
12#include <locale>
13#include <optional>
14#include <string>
15#include <vector>
16
17namespace bitrl
18{
19namespace sensors
20{
21template <typename T> struct EigenVectorMessage
22{
24 static std::optional<EigenVectorMessage<T>> parse(const std::string &msg);
25};
26
27template <typename T>
28std::optional<EigenVectorMessage<T>> EigenVectorMessage<T>::parse(const std::string &msg)
29{
30 // --- Trim whitespace ---
31 auto trim = [](const std::string &s)
32 {
33 size_t start = 0;
34 while (start < s.size() && std::isspace(s[start]))
35 start++;
36 size_t end = s.size();
37 while (end > start && std::isspace(s[end - 1]))
38 end--;
39 return s.substr(start, end - start);
40 };
41
42 std::string s = trim(msg);
43
44 if (s.empty() || s.front() != '[' || s.back() != ']')
45 return std::nullopt;
46
47 // Remove '[' and ']'
48 s = s.substr(1, s.size() - 2);
49
50 std::vector<T> values;
51 std::stringstream ss(s);
52 std::string item;
53
54 while (std::getline(ss, item, ','))
55 {
56 item = trim(item);
57 if (item.empty())
58 return std::nullopt;
59
60 std::stringstream item_ss(item);
61 T value;
62
63 if (!(item_ss >> value)) // Parsing failed
64 return std::nullopt;
65
66 values.push_back(value);
67 }
68
70 result.message = DynVec<T>::Zero(values.size());
71 for (size_t i = 0; i < values.size(); ++i)
72 result.message[i] = values[i];
73 return result;
74}
75
76} // namespace sensors
77} // namespace bitrl
78
79#endif // VECTOR_MESSAGE_H
Definition bitrl_consts.h:14
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitrl_types.h:74
Definition vector_message.h:22
DynVec< T > message
Definition vector_message.h:23
static std::optional< EigenVectorMessage< T > > parse(const std::string &msg)
Definition vector_message.h:28