33 using namespace Eigen;
38 namespace drwnMatlabUtils {
41 void mexMsgTxt(
const char *message) { mexPrintf(
"%s\n", message); }
44 void setupLoggerCallbacks();
49 const mxArray *factor,
int factorIndx = 0);
52 void printStandardOptions();
55 void initializeStandardOptions(map<string, string>& options);
59 bool parseOptions(
const mxArray *optionStruct, map<string, string>& options);
62 void processStandardOptions(map<string, string>& options);
65 void mxArrayToVector(
const mxArray *m, vector<vector<double> > &v);
68 void mxArrayToEigen(
const mxArray *m, MatrixXd& A);
71 void mxArrayToEigen(
const mxArray *m, VectorXd& b);
74 void mxArrayToEigen(
const mxArray *m, vector<MatrixXd>& A);
96 const mxArray *factor,
int factorIndx)
98 mxAssert(mxIsStruct(factor),
"invalid factor datastructure");
101 mxArray *vars = mxGetField(factor, factorIndx,
"vars");
102 mxAssert(vars != NULL,
"field 'vars' missing");
105 for (
int i = 0; i < mxGetNumberOfElements(vars); i++) {
110 mxArray *data = mxGetField(factor, factorIndx,
"data");
111 if ((data == NULL) || mxIsEmpty(data)) {
114 mxAssert(mxGetNumberOfElements(data) == (
int)phi->entries(),
115 "wrong number of elements in field 'data'");
117 for (
int i = 0; i < phi->entries(); i++) {
118 (*phi)[i] = mxGetPr(data)[i];
126 mexPrintf(
" config :: configure Darwin from XML file\n");
127 mexPrintf(
" set :: set configuration (module, property, value triplets)\n");
128 mexPrintf(
" profile :: profile code\n");
129 mexPrintf(
" verbose :: show verbose messages\n");
130 mexPrintf(
" debug :: show debug messages\n");
135 options[string(
"config")] = string(
"");
136 options[string(
"set")] = string(
"");
137 options[string(
"profile")] = string(
"0");
138 options[string(
"verbose")] = string(
"0");
139 options[string(
"debug")] = string(
"0");
144 if ((optionStruct == NULL) || (mxIsEmpty(optionStruct))) {
148 if (!mxIsStruct(optionStruct)) {
149 DRWN_LOG_FATAL(
"options must be a Matlab structure");
152 int N = mxGetNumberOfFields(optionStruct);
153 for (
int i = 0; i < N; i++) {
155 const char *name = mxGetFieldNameByNumber(optionStruct, i);
156 mxArray *value = mxGetFieldByNumber(optionStruct, 0, i);
157 if ((value == NULL) || (mxIsEmpty(value))) {
162 if (mxIsChar(value)) {
163 char *v = mxArrayToString(value);
164 options[string(name)] = string(v);
166 }
else if (mxIsNumeric(value)) {
167 if (mxGetNumberOfElements(value) == 1) {
168 options[string(name)] =
toString(mxGetScalar(value));
171 for (
int i = 0; i < mxGetNumberOfElements(value); i++) {
172 if (i > 0) v = v + string(
" ");
173 v = v +
toString(mxGetPr(value)[i]);
175 options[string(name)] = v;
178 mexErrMsgTxt(
"invalid option value");
188 if (!options[
string(
"config")].empty()) {
193 if (!options[
string(
"set")].empty()) {
194 vector<string> tokens;
195 const int n = drwn::parseString(options[
string(
"set")], tokens);
201 DRWN_LOG_FATAL(
"'set' option requires triplets of the form 'module property value'");
204 for (
int i = 0; i < n; i += 3) {
206 tokens[i + 1].c_str(), tokens[i + 2].c_str());
214 if (atoi(options[
string(
"verbose")].c_str()) != 0) {
217 if (atoi(options[
string(
"debug")].c_str()) != 0) {
224 if (mxGetClassID(m) != mxDOUBLE_CLASS) {
225 DRWN_LOG_FATAL(
"expecting matrix of type double");
228 const int nRows = mxGetM(m);
229 const int nCols = mxGetN(m);
231 const double *p = mxGetPr(m);
232 for (
int i = 0; i < nRows; i++) {
234 for (
int j = 0; j < nCols; j++) {
235 v[i][j] = p[j * nRows + i];
242 if (mxGetClassID(m) != mxDOUBLE_CLASS) {
243 DRWN_LOG_FATAL(
"expecting matrix of type double");
246 const int nRows = mxGetM(m);
247 const int nCols = mxGetN(m);
248 DRWN_LOG_DEBUG(
"parsing a " << nRows <<
"-by-" << nCols <<
" matrix...");
250 A.resize(nRows, nCols);
251 const double *p = mxGetPr(m);
252 for (
int i = 0; i < nRows; i++) {
253 for (
int j = 0; j < nCols; j++) {
254 A(i, j) = p[j * nRows + i];
261 if (mxGetClassID(m) != mxDOUBLE_CLASS) {
262 DRWN_LOG_FATAL(
"expecting matrix of type double");
265 const int nRows = mxGetM(m);
266 const int nCols = mxGetN(m);
268 b = Eigen::Map<VectorXd>(mxGetPr(m), nRows * nCols);
273 if (mxGetNumberOfDimensions(m) < 3) {
279 DRWN_ASSERT(mxGetNumberOfDimensions(m) == 3);
280 const int nRows = mxGetDimensions(m)[0];
281 const int nCols = mxGetDimensions(m)[1];
282 const int nChannels = mxGetDimensions(m)[2];
283 DRWN_LOG_DEBUG(
"parsing a " << nRows <<
"-by-" << nCols <<
"-by-" << nChannels <<
" matrix...");
285 A.resize(nChannels, MatrixXd::Zero(nRows, nCols));
286 const double *p = mxGetPr(m);
287 for (
int c = 0; c < nChannels; c++) {
288 for (
int x = 0; x < nCols; x++) {
289 for (
int y = 0; y < nRows; y++) {
void configure(const char *filename)
configure all registered modules from XML file
Definition: drwnConfigManager.cpp:98
bool parseOptions(const mxArray *optionStruct, map< string, string > &options)
parses Matlab structure of options, assuming that each field is either a scalar or a string...
Definition: drwnMatlabUtils.h:142
static void(* showErrorCallback)(const char *message)
callback for non-fatal errors
Definition: drwnLogger.h:99
static void(* showWarningCallback)(const char *message)
callback for warnings
Definition: drwnLogger.h:101
void mxArrayToEigen(const mxArray *m, MatrixXd &A)
converts a Matlab matrix to an eigen matrix
Definition: drwnMatlabUtils.h:240
void showModuleUsage(const char *module) const
show usage for a specific module
Definition: drwnConfigManager.cpp:137
static void(* showStatusCallback)(const char *message)
callback for status updates
Definition: drwnLogger.h:103
static bool enabled
set true to enable profiling
Definition: drwnCodeProfiler.h:70
void processStandardOptions(map< string, string > &options)
processes standard options
Definition: drwnMatlabUtils.h:185
static void(* showFatalCallback)(const char *message)
callback for fatal errors
Definition: drwnLogger.h:97
Factor which stores the value of each assignment explicitly in table form.
Definition: drwnTableFactor.h:144
void setupLoggerCallbacks()
sets drwnLogger callbacks to output to Matlab
Definition: drwnMatlabUtils.h:81
void initializeStandardOptions(map< string, string > &options)
initializes standard options structure
Definition: drwnMatlabUtils.h:133
void mxArrayToVector(const mxArray *m, vector< vector< double > > &v)
converts a Matlab matrix to an stl vector of vectors
Definition: drwnMatlabUtils.h:222
std::string toString(const T &v)
Templated function to make conversion from simple data types like int and double to strings easy for ...
Definition: drwnStrUtils.h:134
void mexMsgTxt(const char *message)
print a message to the matlab console
Definition: drwnMatlabUtils.h:41
drwnTableFactor * parseFactor(const drwnVarUniversePtr &pUniverse, const mxArray *factor, int factorIndx=0)
parse a table factor from a structure array s with fields s.vars and s.data
Definition: drwnMatlabUtils.h:95
void addVariable(int var)
add variable by id
Definition: drwnTableFactor.cpp:236
static void(* showMessageCallback)(const char *message)
callback for messages (standard, verbose and debug)
Definition: drwnLogger.h:105
static void setLogLevel(drwnLogLevel level)
set the current verbosity level
Definition: drwnLogger.h:132
static drwnConfigurationManager & get()
get the configuration manager (singleton object)
Definition: drwnConfigManager.cpp:92
void printStandardOptions()
prints standard options to Matlab console
Definition: drwnMatlabUtils.h:124