5#ifndef __invdyn_math_utils_hpp__ 6#define __invdyn_math_utils_hpp__ 10#include <pinocchio/spatial/se3.hpp> 11#include <pinocchio/spatial/explog.hpp> 17#define PRINT_VECTOR(a) \ 18 std::cout << #a << "(" << a.rows() << "x" << a.cols() \ 19 << "): " << a.transpose().format(math::CleanFmt) << std::endl 20#define PRINT_MATRIX(a) \ 21 std::cout << #a << "(" << a.rows() << "x" << a.cols() << "):\n" \ 22 << a.format(math::CleanFmt) << std::endl 34 const std::string separator =
", ") {
36 for (
int i = 0; i < v.size() - 1; i++) ss << v[i] << separator;
37 ss << v[v.size() - 1];
41template <
typename T,
int n>
42std::string
toString(
const Eigen::MatrixBase<T>& v,
43 const std::string separator =
", ") {
44 if (v.rows() > v.cols())
return toString(v.transpose(), separator);
53static const Eigen::IOFormat CleanFmt(1, 0,
", ",
"\n",
"[",
"]");
65static const Eigen::IOFormat matlabPrintFormat(Eigen::FullPrecision,
66 Eigen::DontAlignCols,
" ",
";\n",
82void errorInSE3(
const pinocchio::SE3& M,
const pinocchio::SE3& Mdes,
83 pinocchio::Motion& error);
87 double damping = 0.0);
90 double damping = 0.0);
93 unsigned int computationOptions = Eigen::ComputeThinU |
97 Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
99 unsigned int computationOptions);
102 Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
103 RefMatrix Apinv,
double tolerance,
double* nullSpaceBasisOfA,
104 int& nullSpaceRows,
int& nullSpaceCols,
105 unsigned int computationOptions);
108 Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
110 double dampingFactor,
111 unsigned int computationOptions = Eigen::ComputeThinU |
113 double* nullSpaceBasisOfA = 0,
int* nullSpaceRows = 0,
114 int* nullSpaceCols = 0);
117 const Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
double tolerance,
118 double* nullSpaceBasisMatrix,
int& rows,
int& cols);
121 const Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
int rank,
122 double* nullSpaceBasisMatrix,
int& rows,
int& cols);
124template <
typename Derived>
125inline bool isFinite(
const Eigen::MatrixBase<Derived>& x) {
126 return ((x - x).array() == (x - x).array()).all();
129template <
typename Derived>
130inline bool is_nan(
const Eigen::MatrixBase<Derived>& x) {
131 return ((x.array() == x.array())).all();
137template <
class Matrix>
139 const Eigen::MatrixBase<Matrix>& matrix) {
140 typedef typename Matrix::Index
Index;
141 typedef typename Matrix::Scalar
Scalar;
143 std::ofstream out(filename.c_str(),
144 std::ios::out | std::ios::binary | std::ios::trunc);
145 if (!out.is_open())
return false;
146 Index rows = matrix.rows(), cols = matrix.cols();
147 out.write((
char*)(&rows),
sizeof(
Index));
148 out.write((
char*)(&cols),
sizeof(
Index));
149 out.write((
char*)matrix.data(), rows * cols *
sizeof(
Scalar));
157template <
class Matrix>
159 const Eigen::MatrixBase<Matrix>& matrix) {
160 typedef typename Matrix::Index
Index;
161 typedef typename Matrix::Scalar
Scalar;
163 std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
164 if (!in.is_open())
return false;
165 Index rows = 0, cols = 0;
166 in.read((
char*)(&rows),
sizeof(
Index));
167 in.read((
char*)(&cols),
sizeof(
Index));
169 Eigen::MatrixBase<Matrix>& matrix_ =
170 const_cast<Eigen::MatrixBase<Matrix>&
>(matrix);
172 matrix_.resize(rows, cols);
173 in.read((
char*)matrix_.data(), rows * cols *
sizeof(
Scalar));
Definition constraint-base.hpp:13
Eigen::Ref< Matrix > RefMatrix
Definition fwd.hpp:37
void nullSpaceBasisFromDecomposition(const Eigen::JacobiSVD< Eigen::MatrixXd > &svdDecomposition, double tolerance, double *nullSpaceBasisMatrix, int &rows, int &cols)
Definition utils.cpp:162
void svdSolveWithDamping(ConstRefMatrix A, ConstRefVector b, RefVector sol, double damping=0.0)
Definition utils.cpp:60
std::size_t Index
Definition fwd.hpp:40
bool is_nan(const Eigen::MatrixBase< Derived > &x)
Definition utils.hpp:130
void vectorToSE3(RefVector vec, pinocchio::SE3 &M)
Definition utils.cpp:25
void pseudoInverse(ConstRefMatrix A, RefMatrix Apinv, double tolerance, unsigned int computationOptions=Eigen::ComputeThinU|Eigen::ComputeThinV)
Definition utils.cpp:69
bool isFinite(const Eigen::MatrixBase< Derived > &x)
Definition utils.hpp:125
void solveWithDampingFromSvd(Eigen::JacobiSVD< Eigen::MatrixXd > &svd, ConstRefVector b, RefVector sol, double damping=0.0)
Definition utils.cpp:42
void SE3ToXYZQUAT(const pinocchio::SE3 &M, RefVector xyzQuat)
Definition utils.cpp:10
const Eigen::Ref< const Matrix > ConstRefMatrix
Definition fwd.hpp:38
const Eigen::Ref< const Vector > ConstRefVector
Definition fwd.hpp:35
bool readMatrixFromFile(const std::string &filename, const Eigen::MatrixBase< Matrix > &matrix)
Definition utils.hpp:158
bool writeMatrixToFile(const std::string &filename, const Eigen::MatrixBase< Matrix > &matrix)
Definition utils.hpp:138
void dampedPseudoInverse(ConstRefMatrix A, Eigen::JacobiSVD< Eigen::MatrixXd > &svdDecomposition, RefMatrix Apinv, double tolerance, double dampingFactor, unsigned int computationOptions=Eigen::ComputeThinU|Eigen::ComputeThinV, double *nullSpaceBasisOfA=0, int *nullSpaceRows=0, int *nullSpaceCols=0)
Definition utils.cpp:123
double Scalar
Definition fwd.hpp:21
void errorInSE3(const pinocchio::SE3 &M, const pinocchio::SE3 &Mdes, pinocchio::Motion &error)
Definition utils.cpp:33
Eigen::Ref< Vector > RefVector
Definition fwd.hpp:34
void SE3ToVector(const pinocchio::SE3 &M, RefVector vec)
Definition utils.cpp:17
Definition constraint-bound.hpp:25
std::string toString(const T &v)
Definition utils.hpp:26