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:
50  AronVectorConverter() = delete;
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>>
89  {
91 
92  return ConvertTo2DVector<T>(*nav);
93  }
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>
119  {
121 
122  return ConvertTo1DVector<T>(*nav);
123  }
124 
125  // alias
126  template <typename T>
127  static std::vector<T>
129  {
130  return ConvertTo1DVector<T>(nav);
131  }
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  {
138  data::NDArrayPtr ndArr(new data::NDArray);
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  {
152  return ConvertFrom1DVector<T>(data);
153  }
154  };
155 } // namespace armarx::aron::data::converter
156 
157 // legacy
158 namespace armarx::aron::converter
159 {
161 }
armarx::aron::data::converter::AronVectorConverter::ConvertToVector
static std::vector< T > ConvertToVector(const data::NDArrayPtr &nav)
Definition: StdVectorConverter.h:128
armarx::aron::data::NDArray::getDataAsVector
std::vector< unsigned char > getDataAsVector() const
Definition: NDArray.cpp:141
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:147
armarx::aron::data::converter::AronVectorConverter::ConvertFromVector
static data::NDArrayPtr ConvertFromVector(const std::vector< T > &data)
Definition: StdVectorConverter.h:150
armarx::aron::data::converter::AronVectorConverter::ConvertTo1DVector
static std::vector< T > ConvertTo1DVector(const data::NDArrayPtr &nav)
Definition: StdVectorConverter.h:118
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::aron::data::converter::AronVectorConverter
Definition: StdVectorConverter.h:32
armarx::aron::converter::AronVectorConverter
::armarx::aron::data::converter::AronVectorConverter AronVectorConverter
Definition: StdVectorConverter.h:160
armarx::aron::data::converter::AronVectorConverter::ConvertFrom1DVector
static data::NDArrayPtr ConvertFrom1DVector(const std::vector< T > &data)
Definition: StdVectorConverter.h:136
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:54
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:38
armarx::aron::data::converter::AronVectorConverter::ConvertTo1DVector
static std::vector< T > ConvertTo1DVector(const data::NDArray &nav)
Definition: StdVectorConverter.h:97
armarx::aron::data::NDArray::getData
unsigned char * getData() const
Definition: NDArray.cpp:128
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:88
NDArray.h