18 #ifndef __invdyn_math_utils_hpp__
19 #define __invdyn_math_utils_hpp__
23 #include <pinocchio/spatial/se3.hpp>
24 #include <pinocchio/spatial/explog.hpp>
30 #define PRINT_VECTOR(a) \
31 std::cout << #a << "(" << a.rows() << "x" << a.cols() \
32 << "): " << a.transpose().format(math::CleanFmt) << std::endl
33 #define PRINT_MATRIX(a) \
34 std::cout << #a << "(" << a.rows() << "x" << a.cols() << "):\n" \
35 << a.format(math::CleanFmt) << std::endl
47 const std::string separator =
", ") {
49 for (
int i = 0; i < v.size() - 1; i++) ss << v[i] << separator;
50 ss << v[v.size() - 1];
54 template <
typename T,
int n>
55 std::string
toString(
const Eigen::MatrixBase<T>& v,
56 const std::string separator =
", ") {
57 if (v.rows() > v.cols())
return toString(v.transpose(), separator);
66 static const Eigen::IOFormat CleanFmt(1, 0,
", ",
"\n",
"[",
"]");
78 static const Eigen::IOFormat matlabPrintFormat(Eigen::FullPrecision,
79 Eigen::DontAlignCols,
" ",
";\n",
96 pinocchio::Motion& error);
100 double damping = 0.0);
103 double damping = 0.0);
106 unsigned int computationOptions = Eigen::ComputeThinU |
107 Eigen::ComputeThinV);
110 Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
112 unsigned int computationOptions);
115 Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
116 RefMatrix Apinv,
double tolerance,
double* nullSpaceBasisOfA,
117 int& nullSpaceRows,
int& nullSpaceCols,
118 unsigned int computationOptions);
121 Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
123 double dampingFactor,
124 unsigned int computationOptions = Eigen::ComputeThinU |
126 double* nullSpaceBasisOfA = 0,
int* nullSpaceRows = 0,
127 int* nullSpaceCols = 0);
130 const Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
double tolerance,
131 double* nullSpaceBasisMatrix,
int& rows,
int& cols);
134 const Eigen::JacobiSVD<Eigen::MatrixXd>& svdDecomposition,
int rank,
135 double* nullSpaceBasisMatrix,
int& rows,
int& cols);
137 template <
typename Derived>
138 inline bool isFinite(
const Eigen::MatrixBase<Derived>& x) {
139 return ((x - x).array() == (x - x).array()).all();
142 template <
typename Derived>
143 inline bool is_nan(
const Eigen::MatrixBase<Derived>& x) {
144 return ((x.array() == x.array())).all();
150 template <
class Matrix>
152 const Eigen::MatrixBase<Matrix>& matrix) {
156 std::ofstream out(filename.c_str(),
157 std::ios::out | std::ios::binary | std::ios::trunc);
158 if (!out.is_open())
return false;
159 Index rows = matrix.rows(), cols = matrix.cols();
160 out.write((
char*)(&rows),
sizeof(
Index));
161 out.write((
char*)(&cols),
sizeof(
Index));
162 out.write((
char*)matrix.data(), rows * cols *
sizeof(
Scalar));
170 template <
class Matrix>
172 const Eigen::MatrixBase<Matrix>& matrix) {
176 std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
177 if (!in.is_open())
return false;
178 Index rows = 0, cols = 0;
179 in.read((
char*)(&rows),
sizeof(
Index));
180 in.read((
char*)(&cols),
sizeof(
Index));
182 Eigen::MatrixBase<Matrix>& matrix_ =
183 const_cast<Eigen::MatrixBase<Matrix>&
>(matrix);
185 matrix_.resize(rows, cols);
186 in.read((
char*)matrix_.data(), rows * cols *
sizeof(
Scalar));
Eigen::Ref< Matrix > RefMatrix
Definition: fwd.hpp:50
void nullSpaceBasisFromDecomposition(const Eigen::JacobiSVD< Eigen::MatrixXd > &svdDecomposition, double tolerance, double *nullSpaceBasisMatrix, int &rows, int &cols)
Definition: utils.cpp:175
void svdSolveWithDamping(ConstRefMatrix A, ConstRefVector b, RefVector sol, double damping=0.0)
Definition: utils.cpp:73
std::size_t Index
Definition: fwd.hpp:53
bool is_nan(const Eigen::MatrixBase< Derived > &x)
Definition: utils.hpp:143
void vectorToSE3(RefVector vec, pinocchio::SE3 &M)
Definition: utils.cpp:38
void pseudoInverse(ConstRefMatrix A, RefMatrix Apinv, double tolerance, unsigned int computationOptions=Eigen::ComputeThinU|Eigen::ComputeThinV)
Definition: utils.cpp:82
bool isFinite(const Eigen::MatrixBase< Derived > &x)
Definition: utils.hpp:138
void solveWithDampingFromSvd(Eigen::JacobiSVD< Eigen::MatrixXd > &svd, ConstRefVector b, RefVector sol, double damping=0.0)
Definition: utils.cpp:55
void SE3ToXYZQUAT(const pinocchio::SE3 &M, RefVector xyzQuat)
Definition: utils.cpp:23
const Eigen::Ref< const Matrix > ConstRefMatrix
Definition: fwd.hpp:51
const Eigen::Ref< const Vector > ConstRefVector
Definition: fwd.hpp:48
bool readMatrixFromFile(const std::string &filename, const Eigen::MatrixBase< Matrix > &matrix)
Definition: utils.hpp:171
bool writeMatrixToFile(const std::string &filename, const Eigen::MatrixBase< Matrix > &matrix)
Definition: utils.hpp:151
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:136
double Scalar
Definition: fwd.hpp:34
void errorInSE3(const pinocchio::SE3 &M, const pinocchio::SE3 &Mdes, pinocchio::Motion &error)
Definition: utils.cpp:46
Eigen::Ref< Vector > RefVector
Definition: fwd.hpp:47
void SE3ToVector(const pinocchio::SE3 &M, RefVector vec)
Definition: utils.cpp:30
pinocchio::SE3 SE3
Definition: trajectory-base.hpp:31
Definition: constraint-bound.hpp:25
std::string toString(const T &v)
Definition: utils.hpp:39