StdVectorConverter.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Fabian Reister ( fabian dot reister at kit dot edu )
17 * @date 2021
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#pragma once
23
24
26
27#include <RobotAPI/interface/aron.h>
29
31{
33 {
34 private:
35 template <typename T>
36 static std::vector<T>
37 convert_to_1d_vector(const unsigned char* data, const int offset, const int size)
38 {
40 ARMARX_CHECK(size % sizeof(T) == 0);
41 int elements = size / sizeof(T);
42
43 std::vector<T> v(elements);
44 memcpy(v.data(), data + offset, size);
45
46 return v;
47 }
48
49 public:
51
52 template <typename T>
53 static std::vector<std::vector<T>>
54 ConvertTo2DVector(const data::NDArray& nav, int dim0size = -1)
55 {
56 const auto& dims = nav.getShape();
57
58 if (dims.size() == 0 || dim0size == 0)
59 {
60 return {};
61 }
62
63 if (dim0size == -1)
64 {
65 dim0size = dims.at(0);
66 }
67
68 const int fullDataByteSize = nav.getDataAsVector().size();
69
70 ARMARX_CHECK(fullDataByteSize % sizeof(T) == 0);
71 const int fullDataSize = fullDataByteSize / sizeof(T);
72
73 ARMARX_CHECK(fullDataSize % dim0size == 0);
74 const int dim1size = fullDataSize / dim0size;
75
76 const int oneRowByteSize = dim1size * sizeof(T);
77 std::vector<std::vector<T>> v(dims.at(0));
78 for (int i = 0; i < dim0size; i++)
79 {
80 v[i] = convert_to_1d_vector<T>(nav.getData(), i * oneRowByteSize, oneRowByteSize);
81 }
82
83 return v;
84 }
85
86 template <typename T>
87 static std::vector<std::vector<T>>
94
95 template <typename T>
96 static std::vector<T>
98 {
99 const auto& dims = nav.getShape();
100
101 if (dims.size() == 0)
102 {
103 return {};
104 }
105
106 const int fullDataByteSize = nav.getDataAsVector().size();
107
108 ARMARX_CHECK(fullDataByteSize % sizeof(T) == 0);
109 const int dim0size = fullDataByteSize / sizeof(T);
110
111 const int oneRowByteSize = dim0size * sizeof(T);
112 std::vector<T> v = convert_to_1d_vector<T>(nav.getData(), 0, oneRowByteSize);
113 return v;
114 }
115
116 template <typename T>
117 static std::vector<T>
124
125 // alias
126 template <typename T>
127 static std::vector<T>
132
133 // Attention: If a vector was flattened, the reconstruction is flattened as well!
134 template <typename T>
135 static data::NDArrayPtr
136 ConvertFrom1DVector(const std::vector<T>& data)
137 {
139
140 ndArr->setShape({static_cast<int>(data.size()), sizeof(T)});
141 ndArr->setData(sizeof(T) * data.size(),
142 reinterpret_cast<const unsigned char*>(data.data()));
143
144 return ndArr;
145 }
146
147 // alias
148 template <typename T>
149 static data::NDArrayPtr
150 ConvertFromVector(const std::vector<T>& data)
151 {
153 }
154 };
155} // namespace armarx::aron::data::converter
156
157// legacy
159{
161}
static data::NDArrayPtr ConvertFromVector(const std::vector< T > &data)
static std::vector< std::vector< T > > ConvertTo2DVector(const data::NDArrayPtr &nav)
static std::vector< T > ConvertTo1DVector(const data::NDArray &nav)
static std::vector< std::vector< T > > ConvertTo2DVector(const data::NDArray &nav, int dim0size=-1)
static std::vector< T > ConvertToVector(const data::NDArrayPtr &nav)
static std::vector< T > ConvertTo1DVector(const data::NDArrayPtr &nav)
static data::NDArrayPtr ConvertFrom1DVector(const std::vector< T > &data)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
::armarx::aron::data::converter::AronVectorConverter AronVectorConverter
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< NDArray > NDArrayPtr
Definition NDArray.h:46