-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconversions.hpp
More file actions
281 lines (237 loc) · 7.04 KB
/
Copy pathconversions.hpp
File metadata and controls
281 lines (237 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/***************************************************************************
* Copyright (c) 2016, Wolf Vollprecht, Johan Mabille and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef XTENSOR_ROS_CONVERSIONS
#define XTENSOR_ROS_CONVERSIONS
#include <ros/message_traits.h>
#include <ros/serialization.h>
// for tuple magic
#include <tuple>
#include <type_traits>
#include <xtensor/xarray.hpp>
#include <xtensor/xtensor.hpp>
#include <xtensor/xadapt.hpp>
#include <xtensor/xutils.hpp>
// Including the messages
#include "xtensor_ros/f32.h"
#include "xtensor_ros/f64.h"
#include "xtensor_ros/u8.h"
#include "xtensor_ros/u16.h"
#include "xtensor_ros/u32.h"
#include "xtensor_ros/u64.h"
#include "xtensor_ros/i8.h"
#include "xtensor_ros/i16.h"
#include "xtensor_ros/i32.h"
#include "xtensor_ros/i64.h"
namespace xmsg
{
using namespace xtensor_ros;
template <typename T, typename Tuple>
struct has_type;
template <typename T, typename Tuple>
static constexpr bool has_type_v = has_type<T, Tuple>::value;
template <typename T>
struct has_type<T, std::tuple<>> : std::false_type {};
template <typename T, typename U, typename... Ts>
struct has_type<T, std::tuple<U, Ts...>> : has_type<T, std::tuple<Ts...>> {};
template <typename T, typename... Ts>
struct has_type<T, std::tuple<T, Ts...>> : std::true_type {};
template<typename T>
using is_xtensor_msg = std::enable_if_t<has_type<T,
std::tuple<f64, f32, u64, u32, u16, u8, i64, i32, i16, i8>>;
}
template <class T>
struct get_xmsg_type;
template <class T>
using xmsg_t = typename get_xmsg_type<T>::type;
template <>
struct get_xmsg_type<float>
{
using type = xmsg::f32;
};
template <>
struct get_xmsg_type<double>
{
using type = xmsg::f64;
};
template <>
struct get_xmsg_type<uint8_t>
{
using type = xmsg::u8;
};
template <>
struct get_xmsg_type<uint16_t>
{
using type = xmsg::u16;
};
template <>
struct get_xmsg_type<uint32_t>
{
using type = xmsg::u32;
};
template <>
struct get_xmsg_type<uint64_t>
{
using type = xmsg::u64;
};
template <>
struct get_xmsg_type<int8_t>
{
using type = xmsg::i8;
};
template <>
struct get_xmsg_type<int16_t>
{
using type = xmsg::i16;
};
template <>
struct get_xmsg_type<int32_t>
{
using type = xmsg::i32;
};
template <>
struct get_xmsg_type<int64_t>
{
using type = xmsg::i64;
};
template <class T>
xmsg_t<T> as_msg(xt::xarray<T>& arr)
{
using msg_type = xmsg_t<T>;
using _data_type = typename msg_type::_data_type;
msg_type msg;
msg.strides = arr.strides();
msg.shape = arr.shape();
msg.data = _data_type(arr.data().begin(), arr.data().end());
return msg;
}
template <class MsgT, typename = xmsg::is_xtensor_msg<MsgT>>
auto from_msg(const MsgT& arr)
{
return xt::xadapt(arr.data, arr.shape, arr.strides);
}
namespace ros
{
namespace message_traits
{
template <class T>
struct MD5Sum<xt::xarray<T>>
{
using msg_type = xmsg_t<T>;
using msg_md5 = MD5Sum<msg_type>;
static const char* value()
{
return msg_md5::value();
}
static const char* value(const xt::xarray<T>& ) { return value(); }
static const uint64_t static_value1 = msg_md5::static_value1;
static const uint64_t static_value2 = msg_md5::static_value2;
};
template <class T>
struct DataType<xt::xarray<T>>
{
static const char* value()
{
return DataType<xmsg_t<T>>::value();
}
static const char* value(const xt::xarray<T>& m)
{
return DataType<xmsg_t<T>>::value();
}
};
template <class T>
struct Definition<xt::xarray<T>>
{
static const char* value()
{
return Definition<xmsg_t<T>>::value();
}
static const char* value(const xt::xarray<T>& m)
{
return Definition<xmsg_t<T>>::value();
}
};
template <class T>
struct IsFixedSize<xt::xarray<T>> : public FalseType {};
template <class T>
struct IsSimple<xt::xarray<T>> : public FalseType {};
}
namespace serialization
{
template<typename T, class Enabled = void>
struct UVectorSerializer
{};
template<typename T>
struct UVectorSerializer<xt::uvector<T>,
std::enable_if_t<xt::xtrivially_default_constructible<T>::value>>
{
typedef xt::uvector<T> vec_type;
typedef typename vec_type::iterator iterator;
typedef typename vec_type::const_iterator const_iterator;
template<typename Stream>
inline static void write(Stream& stream, const vec_type& v)
{
uint32_t len = (uint32_t)v.size();
stream.next(len);
if (!v.empty())
{
const uint32_t data_len = len * sizeof(T);
memcpy(stream.advance(data_len), &v.front(), data_len);
}
}
template<typename Stream>
inline static void read(Stream& stream, vec_type& v)
{
uint32_t len;
stream.next(len);
v.resize(len);
if (len > 0)
{
const uint32_t data_len = sizeof(T) * len;
memcpy(&v.front(), stream.advance(data_len), data_len);
}
}
inline static uint32_t serializedLength(const vec_type& v)
{
return 4 + v.size() * sizeof(T);
}
};
template<class T>
struct Serializer<xt::xarray<T>>
{
using xtype = xt::xarray<T>;
using shape_type = typename xtype::shape_type;
using shape_serializer_type = VectorSerializer<typename shape_type::value_type,
typename shape_type::allocator_type>;
using data_serializer_type = UVectorSerializer<typename xtype::container_type>;
template<typename Stream>
inline static void write(Stream& stream, const xtype& t)
{
shape_serializer_type::write(stream, t.shape());
shape_serializer_type::write(stream, t.strides());
data_serializer_type::write(stream, t.data());
}
template<typename Stream>
inline static void read(Stream& stream, xtype& t)
{
std::vector<std::size_t> shape, strides;
shape_serializer_type::read(stream, shape);
shape_serializer_type::read(stream, strides);
t.reshape(std::move(shape));
data_serializer_type::read(stream, t.data());
}
inline static uint32_t serializedLength(const xtype& t)
{
uint32_t size = 0;
size += shape_serializer_type::serializedLength(t.shape()) * 2;
size += data_serializer_type::serializedLength(t.data());
return size;
}
};
}
}
#endif