39 #include <boost/shared_ptr.hpp> 40 #include <boost/make_shared.hpp> 50 using namespace boost;
54 #define GTSAM_MAGIC_GAUSSIAN 57 #if defined(__LP64__) || defined(_WIN64) 59 #define mxUINT32OR64_CLASS mxUINT64_CLASS 61 #define mxUINT32OR64_CLASS mxUINT32_CLASS 82 mexErrMsgIdAndTxt(
"wrap:error", str);
86 mwSize dims[1]; dims[0]=1;
87 return mxCreateNumericArray(1, dims, classid, mxREAL);
91 int m = mxGetM(array),
n = mxGetN(array);
93 mexErrMsgIdAndTxt(
"wrap: not a scalar in ", str);
100 virtual std::streamsize
xsputn(
const char *
s, std::streamsize
n) {
101 mexPrintf(
"%.*s",n,s);
106 mexPrintf(
"%.1s",&
c);
118 err << name <<
" expects " << expected <<
" arguments, not " << nargin;
119 if (nargin!=expected)
120 error(err.str().c_str());
128 template <
typename Class>
130 error(
"wrap internal error: attempted wrap of invalid type");
138 return mxCreateString(
value.c_str());
145 *(
char*)mxGetData(result) =
value;
153 *(
unsigned char*)mxGetData(result) =
value;
161 *(
bool*)mxGetData(result) =
value;
169 *(
size_t*)mxGetData(result) =
value;
177 *(
int*)mxGetData(result) =
value;
184 return mxCreateDoubleScalar(
value);
190 mxArray *
result = mxCreateDoubleMatrix(m, 1, mxREAL);
191 double *
data = mxGetPr(result);
192 for (
int i=0;
i<
m;
i++) data[
i]=
v(
i);
216 int m = A.rows(),
n = A.cols();
218 mexPrintf(
"wrap_Matrix called with A = \n", m,
n);
221 mxArray *
result = mxCreateDoubleMatrix(m,
n, mxREAL);
222 double *
data = mxGetPr(result);
224 for (
int j=0;
j<
n;
j++)
for (
int i=0;
i<
m;
i++,data++) *data = A(
i,
j);
240 template <
typename T>
242 error(
"wrap internal error: attempted unwrap of invalid type");
252 if (data==
NULL)
error(
"unwrap<string>: not a character array");
259 template <
typename T>
261 switch (mxGetClassID(array)) {
268 return (
T) mxGetScalar(array);
276 return myGetScalar<bool>(
array);
283 return myGetScalar<char>(
array);
290 return myGetScalar<unsigned char>(
array);
297 return myGetScalar<int>(
array);
304 return myGetScalar<size_t>(
array);
311 return myGetScalar<double>(
array);
318 if (mxIsDouble(
array)==
false ||
n!=1)
error(
"unwrap<vector>: not a vector");
320 mexPrintf(
"unwrap< gtsam::Vector > called with %dx%d argument\n", m,
n);
322 double*
data = (
double*)mxGetData(
array);
335 if (mxIsDouble(
array)==
false ||
n!=1)
error(
"unwrap<vector>: not a vector");
337 mexPrintf(
"unwrap< gtsam::Vector > called with %dx%d argument\n", m,
n);
339 double*
data = (
double*)mxGetData(
array);
352 if (mxIsDouble(
array)==
false ||
n!=1)
error(
"unwrap<vector>: not a vector");
354 mexPrintf(
"unwrap< gtsam::Vector > called with %dx%d argument\n", m,
n);
356 double*
data = (
double*)mxGetData(
array);
369 if (mxIsDouble(
array)==
false)
error(
"unwrap<matrix>: not a matrix");
372 mexPrintf(
"unwrap< gtsam::Matrix > called with %dx%d argument\n", m,
n);
374 double*
data = (
double*)mxGetData(
array);
377 for (
int j=0;
j<
n;
j++)
for (
int i=0;
i<
m;
i++,data++) A(
i,
j) = *
data;
397 mxArray*
create_object(
const std::string& classname,
void *pointer,
bool isVirtual,
const char *rttiName) {
402 input[0] = mxCreateNumericMatrix(1, 1, mxUINT64_CLASS, mxREAL);
406 *
reinterpret_cast<void**
>(mxGetData(input[1])) = pointer;
408 const char *derivedClassName;
410 const mxArray *rttiRegistry = mexGetVariablePtr(
"global",
"gtsamwrap_rttiRegistry");
413 "gtsam wrap: RTTI registry is missing - it could have been cleared from the workspace." 414 " You can issue 'clear all' to completely clear the workspace, and next time a wrapped object is" 415 " created the RTTI registry will be recreated.");
416 const mxArray *derivedNameMx = mxGetField(rttiRegistry, 0, rttiName);
419 "gtsam wrap: The derived class type " +
string(rttiName) +
" was not found in the RTTI registry. " 420 "Try calling 'clear all' twice consecutively - we have seen things not get unloaded properly the " 421 "first time. If this does not work, this may indicate an inconsistency in your wrap interface file. " 422 "The most likely cause for this is that a base class was marked virtual in the wrap interface " 423 "definition header file for gtsam or for your module, but a derived type was returned by a C++ " 424 "function and that derived type was not marked virtual (or was not specified in the wrap interface " 425 "definition header at all).").
c_str());
426 size_t strLen = mxGetN(derivedNameMx);
427 char *buf =
new char[strLen+1];
428 if(mxGetString(derivedNameMx, buf, strLen+1))
429 mexErrMsgTxt(
"gtsam wrap: Internal error reading RTTI table, try 'clear all' to clear your workspace and reinitialize the toolbox.");
430 derivedClassName = buf;
431 input[2] = mxCreateString(
"void");
434 derivedClassName = classname.c_str();
437 mexCallMATLAB(1,&result, nargin, input, derivedClassName);
439 mxDestroyArray(input[0]);
440 mxDestroyArray(input[1]);
442 mxDestroyArray(input[2]);
443 delete[] derivedClassName;
453 template <
typename Class>
454 mxArray*
wrap_shared_ptr(boost::shared_ptr< Class > shared_ptr,
const std::string& matlabName,
bool isVirtual) {
458 boost::shared_ptr<void> void_ptr(shared_ptr);
459 result =
create_object(matlabName, &void_ptr, isVirtual,
typeid(*shared_ptr).name());
461 boost::shared_ptr<Class> *heapPtr =
new boost::shared_ptr<Class>(shared_ptr);
467 template <
typename Class>
470 mxArray* mxh = mxGetProperty(obj,0, propertyName.c_str());
472 || mxGetM(mxh) != 1 || mxGetN(mxh) != 1)
error(
473 "Parameter is not an Shared type.");
475 boost::shared_ptr<Class>* spp = *
reinterpret_cast<boost::shared_ptr<Class>**
> (mxGetData(mxh));
int unwrap< int >(const mxArray *array)
void print(const Matrix &A, const string &s, ostream &stream)
gtsam::Vector unwrap< gtsam::Vector >(const mxArray *array)
char unwrap< char >(const mxArray *array)
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)
size_t unwrap< size_t >(const mxArray *array)
unsigned char unwrap< unsigned char >(const mxArray *array)
mxArray * wrap_shared_ptr(boost::shared_ptr< Class > shared_ptr, const std::string &matlabName, bool isVirtual)
mxArray * wrap< size_t >(const size_t &value)
const char * c_str(Args &&...args)
T unwrap(const mxArray *array)
boost::shared_ptr< Class > unwrap_shared_ptr(const mxArray *obj, const string &propertyName)
mxArray * create_object(const std::string &classname, void *pointer, bool isVirtual, const char *rttiName)
double unwrap< double >(const mxArray *array)
virtual int overflow(int c=EOF)
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)
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
static const boost::uint64_t ptr_constructor_key
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)
Annotation for function names.
mxArray * wrap< bool >(const bool &value)
bool unwrap< bool >(const mxArray *array)
mxArray * scalar(mxClassID classid)
mxArray * wrap_Matrix(const gtsam::Matrix &A)
void error(const char *str)
T myGetScalar(const mxArray *array)