utils.cpp
Go to the documentation of this file.
1 #include "utils.h"
2 
3 #include <memory>
4 
5 #include <VirtualRobot/MathTools.h>
6 
8 
10 {
11 
12  Eigen::VectorXf
13  getEigenVec(nlohmann::json& userConfig,
14  nlohmann::json& defaultConfig,
15  const std::string& entryName,
16  int size,
17  int value)
18  {
19  if (userConfig.find(entryName) != userConfig.end())
20  {
21  auto vec = userConfig[entryName].get<std::vector<float>>();
22  if (vec.size() > 0)
23  {
24  return Eigen::Map<Eigen::VectorXf>(vec.data(), vec.size());
25  }
26  if (size > 0)
27  {
28  return Eigen::VectorXf::Ones(size) * value;
29  }
30  ARMARX_IMPORTANT << "user defined parameter " << entryName
31  << " have size 0, fall back to default";
32  }
33 
34  auto vec = defaultConfig[entryName].get<std::vector<float>>();
35  return Eigen::Map<Eigen::VectorXf>(vec.data(), vec.size());
36  }
37 
38  void
39  getEigenMatrix(nlohmann::json& userConfig, const std::string& entryName, Eigen::MatrixXf& mat)
40  {
41  if (userConfig.find(entryName) != userConfig.end())
42  {
43  auto vec = userConfig[entryName].get<std::vector<std::vector<float>>>();
44  int rows = vec.size();
45  int cols = vec[0].size();
46  ARMARX_INFO << VAROUT(rows) << VAROUT(cols);
49  mat.resize(rows, cols);
50  for (int i = 0; i < rows; i++)
51  {
52  for (int j = 0; j < cols; j++)
53  {
54  mat(i, j) = vec[i][j];
55  }
56  }
57  // memcpy(mat.data(), vec.data(), rows * cols * sizeof(float));
58  }
59  else
60  {
61  ARMARX_ERROR << "values not found in user config";
62  }
63  }
64 
65  Eigen::VectorXf
66  getEigenVecWithDefault(nlohmann::json& userConfig,
67  Eigen::VectorXf defaultValue,
68  const std::string& entryName)
69  {
70  if (userConfig.find(entryName) != userConfig.end())
71  {
72  auto vec = userConfig[entryName].get<std::vector<float>>();
73  if (vec.size() == (unsigned)defaultValue.size())
74  {
75  return Eigen::Map<Eigen::VectorXf>(vec.data(), vec.size());
76  }
77  ARMARX_WARNING << "size of the user defined parameter " << entryName
78  << " doesn't correspond to default size, use default value instead";
79  }
80  return defaultValue;
81  }
82 
83  void
84  getError(Eigen::Matrix4f& currentPose,
85  Eigen::Vector3d& targetPosition,
86  Eigen::Quaterniond& targetQuaternion,
87  double& posiError,
88  double& oriError)
89  {
90  Eigen::Matrix4d currentPosed = currentPose.cast<double>();
91  Eigen::Vector3d currentPosid = currentPosed.block<3, 1>(0, 3);
92  posiError = (currentPosid - targetPosition).norm();
93 
94  Eigen::Quaterniond currentOrientation = Eigen::Quaterniond{currentPosed.block<3, 3>(0, 0)};
95  oriError = targetQuaternion.angularDistance(currentOrientation);
96  if (oriError > M_PI)
97  {
98  oriError = 2 * M_PI - oriError;
99  }
100  }
101 
102  void
103  debugEigenPose(StringVariantBaseMap& datafields, const std::string& name, Eigen::Matrix4f pose)
104  {
105  Eigen::Vector3f rpy = VirtualRobot::MathTools::eigen4f2rpy(pose);
106  datafields[name + "_x"] = new Variant(pose(0, 3));
107  datafields[name + "_y"] = new Variant(pose(1, 3));
108  datafields[name + "_z"] = new Variant(pose(2, 3));
109  datafields[name + "_rx"] = new Variant(rpy(0));
110  datafields[name + "_ry"] = new Variant(rpy(1));
111  datafields[name + "_rz"] = new Variant(rpy(2));
112  }
113 
114  void
115  debugEigenVec(StringVariantBaseMap& datafields, const std::string& name, Eigen::VectorXf vec)
116  {
117  for (int i = 0; i < vec.size(); i++)
118  {
119  datafields[name + "_" + std::to_string(i)] = new Variant(vec(i));
120  }
121  }
122 
123  void
124  to_json(nlohmann::json& j, const Pose& fp)
125  {
126  j = nlohmann::json{{"qw", fp.orientation->qw},
127  {"qx", fp.orientation->qx},
128  {"qy", fp.orientation->qy},
129  {"qz", fp.orientation->qz},
130  {"x", fp.position->x},
131  {"y", fp.position->y},
132  {"z", fp.position->z}};
133  }
134 
135  void
136  from_json(const nlohmann::json& j, Pose& fp)
137  {
138  j.at("qw").get_to(fp.orientation->qw);
139  j.at("qx").get_to(fp.orientation->qx);
140  j.at("qy").get_to(fp.orientation->qy);
141  j.at("qz").get_to(fp.orientation->qz);
142  j.at("x").get_to(fp.position->x);
143  j.at("y").get_to(fp.position->y);
144  j.at("z").get_to(fp.position->z);
145  }
146 
147  void
148  to_json(nlohmann::json& j, const PosePtr& fp)
149  {
150  if (fp)
151  {
152  to_json(j, *fp);
153  }
154  }
155 
156  void
157  to_json(nlohmann::json& j, const PoseBasePtr& fp)
158  {
159  if (fp)
160  {
161  to_json(j, PosePtr::dynamicCast(fp));
162  }
163  }
164 
165  void
166  from_json(const nlohmann::json& j, PoseBasePtr& fp)
167  {
168  PosePtr p = new Pose();
169  from_json(j, p);
170  fp = p;
171  }
172 
173  void
174  from_json(const nlohmann::json& j, PosePtr& fp)
175  {
176  fp = new Pose();
177  from_json(j, *fp);
178  }
179 
180  PosePtr
182  {
183  return new Pose(dVecToMat4(dvec));
184  }
185 
188  {
189  ARMARX_CHECK_EQUAL(dvec.size(), 7);
190  Eigen::Matrix4f mat = VirtualRobot::MathTools::quat2eigen4f(static_cast<float>(dvec[4]),
191  static_cast<float>(dvec[5]),
192  static_cast<float>(dvec[6]),
193  static_cast<float>(dvec[3]));
194  mat(0, 3) = static_cast<float>(dvec[0]);
195  mat(1, 3) = static_cast<float>(dvec[1]);
196  mat(2, 3) = static_cast<float>(dvec[2]);
197 
198  return mat;
199  }
200 
203  {
204  Eigen::Matrix4f poseMat = pose->toEigen();
205  return mat4ToDVec(poseMat);
206  }
207 
210  {
211  std::vector<double> result;
212  result.resize(7);
213 
214  for (size_t i = 0; i < 3; ++i)
215  {
216  result.at(i) = static_cast<double>(mat(i, 3));
217  }
218 
219  VirtualRobot::MathTools::Quaternion quat = VirtualRobot::MathTools::eigen4f2quat(mat);
220 
221  result.at(3) = static_cast<double>(quat.w);
222  result.at(4) = static_cast<double>(quat.x);
223  result.at(5) = static_cast<double>(quat.y);
224  result.at(6) = static_cast<double>(quat.z);
225 
226  return result;
227  }
228 
229  std::vector<float>
231  {
232  Eigen::Matrix4f poseMat = pose->toEigen();
233  return mat4ToFVec(poseMat);
234  }
235 
236  std::vector<float>
238  {
239  std::vector<float> result;
240  result.resize(7);
241 
242  for (size_t i = 0; i < 3; ++i)
243  {
244  result.at(i) = mat(i, 3);
245  }
246 
247  VirtualRobot::MathTools::Quaternion quat = VirtualRobot::MathTools::eigen4f2quat(mat);
248 
249  result.at(3) = quat.w;
250  result.at(4) = quat.x;
251  result.at(5) = quat.y;
252  result.at(6) = quat.z;
253 
254  return result;
255  }
256 
257  std::string
259  {
260  std::string s = "";
261  for (const double& d : dvec)
262  {
263  s = s + std::to_string(d) + " | ";
264  }
265  return s;
266  }
267 
268  Eigen::VectorXf
269  vecToEigen(std::vector<float>& vec)
270  {
271  return Eigen::Map<Eigen::VectorXf>(vec.data(), vec.size());
272  }
273 
274  std::string
275  sVecToString(const std::vector<std::string>& vec, const std::string& delimiter)
276  {
277  std::string s = "";
278  for (size_t i = 0; i < vec.size(); ++i)
279  {
280  if (i != 0)
281  {
282  s = s + delimiter;
283  }
284  s + vec[i];
285  }
286  return s;
287  }
288 
289 
290 } // namespace armarx::control::common
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::control::common::debugEigenPose
void debugEigenPose(StringVariantBaseMap &datafields, const std::string &name, Eigen::Matrix4f pose)
Definition: utils.cpp:103
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::control::common::mat4ToFVec
std::vector< float > mat4ToFVec(Eigen::Matrix4f &mat)
Definition: utils.cpp:237
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::control::common::getEigenVecWithDefault
Eigen::VectorXf getEigenVecWithDefault(nlohmann::json &userConfig, Eigen::VectorXf defaultValue, const std::string &entryName)
Definition: utils.cpp:66
armarx::control::common::mat4ToDVec
mplib::core::DVec mat4ToDVec(Eigen::Matrix4f &mat)
Definition: utils.cpp:209
armarx::control::common::getError
void getError(Eigen::Matrix4f &currentPose, Eigen::Vector3d &targetPosition, Eigen::Quaterniond &targetQuaternion, double &posiError, double &oriError)
Definition: utils.cpp:84
armarx::control::common::from_json
void from_json(const nlohmann::json &j, Pose &fp)
Definition: utils.cpp:136
armarx::control::common
This file is part of ArmarX.
Definition: aron_conversions.cpp:29
armarx::VariantType::Quaternion
const VariantTypeId Quaternion
Definition: Pose.h:39
IceInternal::Handle< Pose >
armarx::control::common::poseToFVec
std::vector< float > poseToFVec(PosePtr &pose)
Definition: utils.cpp:230
armarx::control::common::dVecToString
std::string dVecToString(const mplib::core::DVec &dvec)
Definition: utils.cpp:258
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
M_PI
#define M_PI
Definition: MathTools.h:17
armarx::control::common::mp::DVec
Ice::DoubleSeq DVec
Definition: MP.h:46
armarx::control::common::poseToDVec
mplib::core::DVec poseToDVec(PosePtr &pose)
Definition: utils.cpp:202
armarx::control::common::dVecToMat4
Eigen::Matrix4f dVecToMat4(const mplib::core::DVec &dvec)
Definition: utils.cpp:187
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::control::common::dVecToPose
PosePtr dVecToPose(const mplib::core::DVec &dvec)
Definition: utils.cpp:181
ARMARX_CHECK_GREATER_EQUAL
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
Definition: ExpressionException.h:123
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::control::common::debugEigenVec
void debugEigenVec(StringVariantBaseMap &datafields, const std::string &name, Eigen::VectorXf vec)
Definition: utils.cpp:115
utils.h
armarx::control::common::vecToEigen
Eigen::VectorXf vecToEigen(std::vector< float > &vec)
Definition: utils.cpp:269
ExpressionException.h
armarx::Pose
The Pose class.
Definition: Pose.h:242
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
armarx::control::common::to_json
void to_json(nlohmann::json &j, const Pose &fp)
Definition: utils.cpp:124
Eigen::Quaterniond
Quaternion< double, 0 > Quaterniond
Definition: EigenForwardDeclarations.h:62
armarx::control::common::getEigenVec
Eigen::VectorXf getEigenVec(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName, int size, int value)
Definition: utils.cpp:13
armarx::control::common::sVecToString
std::string sVecToString(const std::vector< std::string > &vec, const std::string &delimiter)
Definition: utils.cpp:275
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::control::common::getEigenMatrix
void getEigenMatrix(nlohmann::json &userConfig, const std::string &entryName, Eigen::MatrixXf &mat)
Definition: utils.cpp:39
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
norm
double norm(const Point &a)
Definition: point.hpp:94