51 #define GTSAM_MAGIC_GAUSSIAN 54 #if defined(__LP64__) || defined(_WIN64) 56 #define mxUINT32OR64_CLASS mxUINT64_CLASS 58 #define mxUINT32OR64_CLASS mxUINT32_CLASS 79 mexErrMsgIdAndTxt(
"wrap:error", str);
83 mwSize dims[1]; dims[0]=1;
84 return mxCreateNumericArray(1, dims, classid, mxREAL);
88 int m = mxGetM(array),
n = mxGetN(array);
90 mexErrMsgIdAndTxt(
"wrap: not a scalar in ", str);
97 virtual std::streamsize
xsputn(
const char *
s, std::streamsize
n) {
98 mexPrintf(
"%.*s",n,s);
103 mexPrintf(
"%.1s",&
c);
115 err << name <<
" expects " << expected <<
" arguments, not " << nargin;
116 if (nargin!=expected)
117 error(err.str().c_str());
125 template <
typename Class>
127 error(
"wrap internal error: attempted wrap of invalid type");
135 return mxCreateString(
value.c_str());
142 *(
char*)mxGetData(result) =
value;
150 *(
unsigned char*)mxGetData(result) =
value;
158 *(
bool*)mxGetData(result) =
value;
166 *(
size_t*)mxGetData(result) =
value;
174 *(
int*)mxGetData(result) =
value;
181 return mxCreateDoubleScalar(
value);
187 mxArray *
result = mxCreateDoubleMatrix(m, 1, mxREAL);
188 double *
data = mxGetPr(result);
189 for (
int i=0;
i<
m;
i++) data[
i]=
v(
i);
213 int m = A.rows(),
n = A.cols();
215 mexPrintf(
"wrap_Matrix called with A = \n", m,
n);
218 mxArray *
result = mxCreateDoubleMatrix(m,
n, mxREAL);
219 double *
data = mxGetPr(result);
221 for (
int j=0;
j<
n;
j++)
for (
int i=0;
i<
m;
i++,data++) *data = A(
i,
j);
235 template <
typename T>
238 mxArray*
a = mxCreateDoubleMatrix(1, 1, mxREAL);
239 double*
data = mxGetPr(a);
240 data[0] =
static_cast<double>(
x);
244 mexCallMATLAB(1, &result, 1, &a, classname.c_str());
255 template <
typename T>
257 error(
"wrap internal error: attempted unwrap of invalid type");
264 template <
typename T>
267 mxArray*
a = mxDuplicateArray(array);
271 mexCallMATLAB(1, &a_int32, 1, &a,
"int32");
274 int32_T*
value = (int32_T*)mxGetData(a_int32);
276 return static_cast<T>(*value);
285 if (data==
NULL)
error(
"unwrap<string>: not a character array");
292 template <
typename T>
294 switch (mxGetClassID(array)) {
301 return (
T) mxGetScalar(array);
309 return myGetScalar<bool>(
array);
316 return myGetScalar<char>(
array);
323 return myGetScalar<unsigned char>(
array);
330 return myGetScalar<int>(
array);
337 return myGetScalar<size_t>(
array);
344 return myGetScalar<double>(
array);
351 if (mxIsDouble(
array)==
false ||
n!=1)
error(
"unwrap<vector>: not a vector");
353 mexPrintf(
"unwrap< gtsam::Vector > called with %dx%d argument\n", m,
n);
355 double*
data = (
double*)mxGetData(
array);
368 if (mxIsDouble(
array)==
false ||
n!=1)
error(
"unwrap<vector>: not a vector");
370 mexPrintf(
"unwrap< gtsam::Vector > called with %dx%d argument\n", m,
n);
372 double*
data = (
double*)mxGetData(
array);
385 if (mxIsDouble(
array)==
false ||
n!=1)
error(
"unwrap<vector>: not a vector");
387 mexPrintf(
"unwrap< gtsam::Vector > called with %dx%d argument\n", m,
n);
389 double*
data = (
double*)mxGetData(
array);
402 if (mxIsDouble(
array)==
false)
error(
"unwrap<matrix>: not a matrix");
405 mexPrintf(
"unwrap< gtsam::Matrix > called with %dx%d argument\n", m,
n);
407 double*
data = (
double*)mxGetData(
array);
410 for (
int j=0;
j<
n;
j++)
for (
int i=0;
i<
m;
i++,data++) A(
i,
j) = *
data;
430 mxArray*
create_object(
const std::string& classname,
void *pointer,
bool isVirtual,
const char *rttiName) {
435 input[0] = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
439 *
reinterpret_cast<void**
>(mxGetData(input[1])) = pointer;
441 const char *derivedClassName;
443 const mxArray *rttiRegistry = mexGetVariablePtr(
"global",
"gtsamwrap_rttiRegistry");
446 "gtsam wrap: RTTI registry is missing - it could have been cleared from the workspace." 447 " You can issue 'clear all' to completely clear the workspace, and next time a wrapped object is" 448 " created the RTTI registry will be recreated.");
449 const mxArray *derivedNameMx = mxGetField(rttiRegistry, 0, rttiName);
452 "gtsam wrap: The derived class type " +
string(rttiName) +
" was not found in the RTTI registry. " 453 "Try calling 'clear all' twice consecutively - we have seen things not get unloaded properly the " 454 "first time. If this does not work, this may indicate an inconsistency in your wrap interface file. " 455 "The most likely cause for this is that a base class was marked virtual in the wrap interface " 456 "definition header file for gtsam or for your module, but a derived type was returned by a C++ " 457 "function and that derived type was not marked virtual (or was not specified in the wrap interface " 458 "definition header at all).").
c_str());
459 size_t strLen = mxGetN(derivedNameMx);
460 char *buf =
new char[strLen+1];
461 if(mxGetString(derivedNameMx, buf, strLen+1))
462 mexErrMsgTxt(
"gtsam wrap: Internal error reading RTTI table, try 'clear all' to clear your workspace and reinitialize the toolbox.");
463 derivedClassName = buf;
464 input[2] = mxCreateString(
"void");
467 derivedClassName = classname.c_str();
470 mexCallMATLAB(1,&result, nargin, input, derivedClassName);
472 mxDestroyArray(input[0]);
473 mxDestroyArray(input[1]);
475 mxDestroyArray(input[2]);
476 delete[] derivedClassName;
486 template <
typename Class>
487 mxArray*
wrap_shared_ptr(std::shared_ptr< Class > shared_ptr,
const std::string& matlabName,
bool isVirtual) {
491 std::shared_ptr<void> void_ptr(shared_ptr);
492 result =
create_object(matlabName, &void_ptr, isVirtual,
typeid(*shared_ptr).name());
494 std::shared_ptr<Class> *heapPtr =
new std::shared_ptr<Class>(shared_ptr);
500 template <
typename Class>
503 mxArray* mxh = mxGetProperty(obj,0, propertyName.c_str());
505 || mxGetM(mxh) != 1 || mxGetN(mxh) != 1)
error(
506 "Parameter is not an Shared type.");
508 std::shared_ptr<Class>* spp = *
reinterpret_cast<std::shared_ptr<Class>**
> (mxGetData(mxh));
512 template <
typename Class>
513 Class*
unwrap_ptr(
const mxArray* obj,
const string& propertyName) {
515 mxArray* mxh = mxGetProperty(obj,0, propertyName.c_str());
516 Class*
x =
reinterpret_cast<Class*
> (mxGetData(mxh));
int unwrap< int >(const mxArray *array)
void print(const Matrix &A, const string &s, ostream &stream)
mxArray * wrap_shared_ptr(std::shared_ptr< Class > shared_ptr, const std::string &matlabName, bool isVirtual)
gtsam::Vector unwrap< gtsam::Vector >(const mxArray *array)
char unwrap< char >(const mxArray *array)
static const std::uint64_t ptr_constructor_key
void checkArguments(const string &name, int nargout, int nargin, int expected)
gtsam::Point3 unwrap< gtsam::Point3 >(const mxArray *array)
string unwrap< string >(const mxArray *array)
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
size_t unwrap< size_t >(const mxArray *array)
unsigned char unwrap< unsigned char >(const mxArray *array)
mxArray * wrap< size_t >(const size_t &value)
const char * c_str(Args &&...args)
T unwrap(const mxArray *array)
T unwrap_enum(const mxArray *array)
Unwrap from matlab array to C++ enum type.
mxArray * create_object(const std::string &classname, void *pointer, bool isVirtual, const char *rttiName)
std::shared_ptr< Class > unwrap_shared_ptr(const mxArray *obj, const string &propertyName)
double unwrap< double >(const mxArray *array)
virtual int overflow(int c=EOF)
Array< int, Dynamic, 1 > v
mxArray * wrap_Vector(const gtsam::Vector &v)
unsigned __int64 uint64_t
Eigen::Triplet< double > T
mxArray * wrap< char >(const char &value)
virtual std::streamsize xsputn(const char *s, std::streamsize n)
Class * unwrap_ptr(const mxArray *obj, const string &propertyName)
mxArray * wrap< unsigned char >(const unsigned char &value)
gtsam::Matrix unwrap< gtsam::Matrix >(const mxArray *array)
mxArray * wrap< int >(const int &value)
typedef and functions to augment Eigen's VectorXd
mxArray * wrap< double >(const double &value)
#define mxUINT32OR64_CLASS
void checkScalar(const mxArray *array, const char *str)
mxArray * wrap(const Class &value)
mxArray * wrap< string >(const string &value)
gtsam::Point2 unwrap< gtsam::Point2 >(const mxArray *array)
mxArray * wrap_enum(const T x, const std::string &classname)
Wrap the C++ enum to Matlab mxArray.
Annotation for function names.
mxArray * wrap< bool >(const bool &value)
bool unwrap< bool >(const mxArray *array)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
mxArray * scalar(mxClassID classid)
mxArray * wrap_Matrix(const gtsam::Matrix &A)
void error(const char *str)
T myGetScalar(const mxArray *array)