16 if (mxGetM(source) != 1 || mxGetN(source) != 1) {
17 if (
log) *
log <<
"Expected scalar.";
24 return static_cast<int>(mxGetScalar(source));
29 if (!mxIsLogicalScalar(source)) {
30 if (
log) *
log <<
"Expected logical scalar.";
37 return mxGetLogicals(source)[0];
46 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
49 Eigen::MatrixBase<Eigen::Map<
50 const Eigen::Matrix<double, Rows, Cols, Options, MaxRows, MaxCols>>>*,
53 if (!mxIsNumeric(mex)) {
54 if (
log) *
log <<
"Expected a numeric array";
60 template <
int Rows,
int Cols,
int Options,
int MaxRows,
int MaxCols>
61 Eigen::Map<const Eigen::Matrix<double, Rows, Cols, Options, MaxRows, MaxCols>>
64 Eigen::MatrixBase<Eigen::Map<
65 const Eigen::Matrix<double, Rows, Cols, Options, MaxRows, MaxCols>>>*) {
66 return matlabToEigenMap<Rows, Cols>(mex);
72 Eigen::MatrixBase<Eigen::Map<const Eigen::VectorXi>>*,
74 if (!mxIsInt32(source)) {
75 if (
log) *
log <<
"Expected an int32 array";
78 if (mxGetM(source) != 1 && mxGetN(source) != 1) {
79 if (
log) *
log <<
"Expected a 1 x n or n x 1 int32 array";
86 const mxArray* source,
87 Eigen::MatrixBase<Eigen::Map<const Eigen::VectorXi>>*) {
88 auto num_elements = mxGetNumberOfElements(source);
89 return Eigen::Map<const Eigen::VectorXi>(
90 reinterpret_cast<int*
>(mxGetData(source)), num_elements);
96 if (!mxIsCell(source)) {
97 if (
log) *
log <<
"Expected a cell array";
100 if (mxGetM(source) != 1 && mxGetN(source) != 1) {
101 if (
log) *
log <<
"Expected a 1 x n or n x 1 cell array";
107 template <
typename T>
109 auto num_elements = mxGetNumberOfElements(source);
111 ret.reserve(num_elements);
112 for (
size_t i = 0; i < num_elements; i++) {
114 fromMexUnsafe(mxGetCell(source, i), static_cast<T*>(
nullptr)));
119 template <
typename DerType,
int Rows,
int Cols,
int Options,
int MaxRows,
123 Eigen::MatrixBase<Eigen::Matrix<Eigen::AutoDiffScalar<DerType>, Rows, Cols,
124 Options, MaxRows, MaxCols>>*,
126 using namespace Eigen;
129 if (!mxIsClass(mex,
"TaylorVar")) {
130 if (
log) *
log <<
"Expected an array of TaylorVar";
133 if (mxIsEmpty(mex)) {
134 if (
log) *
log <<
"Can't parse empty TaylorVar arrays";
138 if (mxGetNumberOfElements(derivs) > 1) {
140 *
log <<
"TaylorVars of order higher than 1 currently not supported";
143 auto df = mxGetCell(derivs, 0);
145 auto rows =
static_cast<int>(
147 auto cols =
static_cast<int>(
150 if ((MaxRows != Dynamic && rows > MaxRows) ||
151 (Rows != Dynamic && rows != Rows)) {
154 <<
", rows at compile time: " <<
to_string(Rows)
155 <<
", max rows at compile time: " <<
to_string(MaxRows);
159 if ((MaxCols != Dynamic && cols > MaxCols) ||
160 (Cols != Dynamic && cols != Cols)) {
163 <<
", cols at compile time: " <<
to_string(Cols)
164 <<
", max cols at compile time: " <<
to_string(MaxCols);
168 auto num_derivs = mxGetN(df);
169 if ((DerType::MaxRowsAtCompileTime != Dynamic &&
170 num_derivs > DerType::MaxRowsAtCompileTime) ||
171 (DerType::RowsAtCompileTime != Dynamic &&
172 num_derivs != DerType::RowsAtCompileTime)) {
174 *
log <<
"Derivative size mismatch. num_derivs: " <<
to_string(num_derivs)
175 <<
", num_derivs at compile time: " 177 <<
", max num_derivs at compile time: " 178 <<
to_string(DerType::MaxRowsAtCompileTime);
185 template <
typename DerType,
int Rows,
int Cols,
int Options,
int MaxRows,
187 Eigen::Matrix<Eigen::AutoDiffScalar<DerType>, Rows, Cols, Options, MaxRows,
191 Eigen::MatrixBase<Eigen::Matrix<Eigen::AutoDiffScalar<DerType>, Rows, Cols,
192 Options, MaxRows, MaxCols>>*) {
193 using namespace Eigen;
195 typedef AutoDiffScalar<DerType> ADScalar;
198 auto df = mxGetCell(derivs, 0);
201 auto rows =
static_cast<int>(
203 auto cols =
static_cast<int>(
206 Matrix<ADScalar, Rows, Cols, Options, MaxRows, MaxCols> ret(rows, cols);
207 if (!mxIsEmpty(mex)) {
208 typedef typename ADScalar::Scalar NormalScalar;
209 typedef Matrix<NormalScalar, Rows, Cols, Options, MaxRows, MaxCols>
211 ret = Map<ValueMatrixType>(
mxGetPrSafe(f), rows, cols)
212 .
template cast<ADScalar>();
216 typedef Matrix<NormalScalar, GradientType::RowsAtCompileTime,
217 GradientType::ColsAtCompileTime, 0,
218 GradientType::RowsAtCompileTime,
219 DerType::MaxRowsAtCompileTime> GradientTypeFixedMaxSize;
221 if (mxIsSparse(df)) {
222 auto gradient_matrix = GradientTypeFixedMaxSize(
226 auto num_derivs = mxGetN(df);
227 auto gradient_matrix = Map<GradientTypeFixedMaxSize>(
239 int toMex(
const bool& source, mxArray* dest[],
int nlhs) {
240 dest[0] = mxCreateLogicalScalar(source);
250 template <
typename Scalar,
int Rows,
int Cols,
int Options,
int MaxRows,
253 const Eigen::Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>& source,
254 mxArray* dest[],
int nlhs) {
255 if (nlhs > 0) dest[0] = eigenToMatlabGeneral<Rows, Cols>(source);
259 template <
typename A,
typename B>
260 int toMex(
const std::pair<A, B>& source, mxArray* dest[],
int nlhs) {
262 num_outputs +=
toMex(source.first, dest, nlhs - num_outputs);
263 num_outputs +=
toMex(source.second, &dest[num_outputs], nlhs - num_outputs);
267 template <
typename T>
268 int toMex(
const std::vector<T>& source, mxArray* dest[],
int nlhs) {
269 mwSize dims[] = {source.size()};
270 dest[0] = mxCreateCellArray(1, dims);
271 for (
size_t i = 0; i < source.size(); i++) {
273 toMex(source.at(i), cell, 1);
274 mxSetCell(dest[0], i, cell[0]);
282 template <
size_t Index>
284 template <
typename... Ts>
285 static int run(
const std::tuple<Ts...>& source, mxArray* dest[],
int nlhs,
286 int num_outputs = 0) {
287 constexpr
size_t tuple_index =
sizeof...(Ts)-Index;
288 num_outputs +=
toMex(std::get<tuple_index>(source), &dest[num_outputs],
296 template <
typename... Ts>
297 static int run(
const std::tuple<Ts...>& source, mxArray* dest[],
int nlhs,
298 int num_outputs = 0) {
305 template <
typename... Ts>
306 int toMex(
const std::tuple<Ts...>& source, mxArray* dest[],
int nlhs) {
Eigen::SparseMatrix< double > matlabToEigenSparse(const mxArray *mex)
Definition: drakeMexUtil.cpp:166
int fromMexUnsafe(const mxArray *source, int *)
Definition: standardMexConversions.h:23
FunctionalForm log(FunctionalForm const &x)
Definition: functional_form.cc:198
NOTE: The contents of this class are for the most part direct ports of drake/systems/plants//inverseK...
Definition: Function.h:14
double * mxGetPrSafe(const mxArray *pobj)
Definition: drakeMexUtil.cpp:236
Definition: standardMexConversions.h:283
bool isConvertibleFromMex(const mxArray *source, int *, std::ostream *log) NOEXCEPT
fromMex specializations
Definition: standardMexConversions.h:14
static int run(const std::tuple< Ts... > &source, mxArray *dest[], int nlhs, int num_outputs=0)
Definition: standardMexConversions.h:297
int toMex(const bool &source, mxArray *dest[], int nlhs)
toMex specializations
Definition: standardMexConversions.h:239
std::string to_string(const Eigen::MatrixBase< Derived > &a)
Definition: testUtil.h:29
mxArray * mxGetPropertySafe(const mxArray *array, std::string const &field_name)
Definition: drakeMexUtil.cpp:244
static int run(const std::tuple< Ts... > &source, mxArray *dest[], int nlhs, int num_outputs=0)
Definition: standardMexConversions.h:285
Definition: gradient.h:13
void gradientMatrixToAutoDiff(const Eigen::MatrixBase< DerivedGradient > &gradient, Eigen::MatrixBase< DerivedAutoDiff > &auto_diff_matrix)
Definition: drakeGradientUtil.h:296
#define NOEXCEPT
Definition: mexify.h:15