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 // STD/STL
25 #include <memory>
26 #include <numeric>
27 #include <string>
28 
29 // ArmarX
31 
32 #include <RobotAPI/interface/aron.h>
34 
36 {
38  {
39  private:
40  template <typename T>
41  static std::vector<T>
42  convert_to_1d_vector(const unsigned char* data, const int offset, const int size)
43  {
45  ARMARX_CHECK(size % sizeof(T) == 0);
46  int elements = size / sizeof(T);
47 
48  std::vector<T> v(elements);
49  memcpy(v.data(), data + offset, size);
50 
51  return v;
52  }
53 
54  public:
55  AronVectorConverter() = delete;
56 
57  template <typename T>
58  static std::vector<std::vector<T>>
59  ConvertTo2DVector(const data::NDArray& nav, int dim0size = -1)
60  {
61  const auto& dims = nav.getShape();
62 
63  if (dims.size() == 0 || dim0size == 0)
64  {
65  return {};
66  }
67 
68  if (dim0size == -1)
69  {
70  dim0size = dims.at(0);
71  }
72 
73  const int fullDataByteSize = nav.getDataAsVector().size();
74 
75  ARMARX_CHECK(fullDataByteSize % sizeof(T) == 0);
76  const int fullDataSize = fullDataByteSize / sizeof(T);
77 
78  ARMARX_CHECK(fullDataSize % dim0size == 0);
79  const int dim1size = fullDataSize / dim0size;
80 
81  const int oneRowByteSize = dim1size * sizeof(T);
82  std::vector<std::vector<T>> v(dims.at(0));
83  for (int i = 0; i < dim0size; i++)
84  {
85  v[i] = convert_to_1d_vector<T>(nav.getData(), i * oneRowByteSize, oneRowByteSize);
86  }
87 
88  return v;
89  }
90 
91  template <typename T>
92  static std::vector<std::vector<T>>
94  {
96 
97  return ConvertTo2DVector<T>(*nav);
98  }
99 
100  template <typename T>
101  static std::vector<T>
103  {
104  const auto& dims = nav.getShape();
105 
106  if (dims.size() == 0)
107  {
108  return {};
109  }
110 
111  const int fullDataByteSize = nav.getDataAsVector().size();
112 
113  ARMARX_CHECK(fullDataByteSize % sizeof(T) == 0);
114  const int dim0size = fullDataByteSize / sizeof(T);
115 
116  const int oneRowByteSize = dim0size * sizeof(T);
117  std::vector<T> v = convert_to_1d_vector<T>(nav.getData(), 0, oneRowByteSize);
118  return v;
119  }
120 
121  template <typename T>
122  static std::vector<T>
124  {
126 
127  return ConvertTo1DVector<T>(*nav);
128  }
129 
130  // alias
131  template <typename T>
132  static std::vector<T>
134  {
135  return ConvertTo1DVector<T>(nav);
136  }
137 
138  // Attention: If a vector was flattened, the reconstruction is flattened as well!
139  template <typename T>
140  static data::NDArrayPtr
141  ConvertFrom1DVector(const std::vector<T>& data)
142  {
143  data::NDArrayPtr ndArr(new data::NDArray);
144 
145  ndArr->setShape({static_cast<int>(data.size()), sizeof(T)});
146  ndArr->setData(sizeof(T) * data.size(),
147  reinterpret_cast<const unsigned char*>(data.data()));
148 
149  return ndArr;
150  }
151 
152  // alias
153  template <typename T>
154  static data::NDArrayPtr
155  ConvertFromVector(const std::vector<T>& data)
156  {
157  return ConvertFrom1DVector<T>(data);
158  }
159  };
160 } // namespace armarx::aron::data::converter
161 
162 // legacy
163 namespace armarx::aron::converter
164 {
166 }
armarx::aron::data::converter::AronVectorConverter::ConvertToVector
static std::vector< T > ConvertToVector(const data::NDArrayPtr &nav)
Definition: StdVectorConverter.h:133
armarx::aron::data::NDArray::getDataAsVector
std::vector< unsigned char > getDataAsVector() const
Definition: NDArray.cpp:139
armarx::aron::data::NDArray
Definition: NDArray.h:48
ARMARX_CHECK_NOT_NULL
#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...
Definition: ExpressionException.h:206
armarx::aron::data::NDArrayPtr
std::shared_ptr< NDArray > NDArrayPtr
Definition: NDArray.h:46
armarx::aron::data::NDArray::getShape
std::vector< int > getShape() const
Definition: NDArray.cpp:145
armarx::aron::data::converter::AronVectorConverter::ConvertFromVector
static data::NDArrayPtr ConvertFromVector(const std::vector< T > &data)
Definition: StdVectorConverter.h:155
armarx::aron::data::converter::AronVectorConverter::ConvertTo1DVector
static std::vector< T > ConvertTo1DVector(const data::NDArrayPtr &nav)
Definition: StdVectorConverter.h:123
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::aron::data::converter::AronVectorConverter
Definition: StdVectorConverter.h:37
armarx::aron::converter::AronVectorConverter
::armarx::aron::data::converter::AronVectorConverter AronVectorConverter
Definition: StdVectorConverter.h:165
armarx::aron::data::converter::AronVectorConverter::ConvertFrom1DVector
static data::NDArrayPtr ConvertFrom1DVector(const std::vector< T > &data)
Definition: StdVectorConverter.h:141
armarx::aron::converter
Definition: EigenConverter.h:235
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::data::converter::AronVectorConverter::AronVectorConverter
AronVectorConverter()=delete
armarx::aron::data::converter::AronVectorConverter::ConvertTo2DVector
static std::vector< std::vector< T > > ConvertTo2DVector(const data::NDArray &nav, int dim0size=-1)
Definition: StdVectorConverter.h:59
ExpressionException.h
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::aron::data::converter::AronVectorConverter::ConvertTo1DVector
static std::vector< T > ConvertTo1DVector(const data::NDArray &nav)
Definition: StdVectorConverter.h:102
armarx::aron::data::NDArray::getData
unsigned char * getData() const
Definition: NDArray.cpp:126
armarx::aron::data::converter
Definition: aron_conversions.cpp:3
armarx::aron::data::converter::AronVectorConverter::ConvertTo2DVector
static std::vector< std::vector< T > > ConvertTo2DVector(const data::NDArrayPtr &nav)
Definition: StdVectorConverter.h:93
NDArray.h