1 | #include "../../bdm/libconfig/libconfig.h++" |
---|
2 | #include <itpp/itbase.h> |
---|
3 | #include <itpp/itmex.h> |
---|
4 | #include <stdlib.h> |
---|
5 | |
---|
6 | using namespace itpp; |
---|
7 | using namespace std; |
---|
8 | using namespace libconfig; |
---|
9 | |
---|
10 | class UImxArray : public Config { |
---|
11 | public: |
---|
12 | UImxArray(const mxArray *mxarray) : Config() { |
---|
13 | Setting & setting = this->getRoot(); //setting is a group |
---|
14 | if (!mxIsStruct(mxarray)) { mexErrMsgTxt("Given mxArray is not a struct."); }; |
---|
15 | //mexCallMATLAB(0, NULL, 1, (mxArray **) &mxarray, "dump"); |
---|
16 | fillGroup(setting, mxarray); |
---|
17 | setAutoConvert(true); |
---|
18 | } |
---|
19 | |
---|
20 | private: |
---|
21 | void storeNumeric(Setting &setting, const mxArray *value, string key = "") { |
---|
22 | //TODO: integer matrices |
---|
23 | if (!mxIsNumeric(value)) { mexErrMsgTxt("Given mxArray is not numeric."); }; |
---|
24 | mat val = mxArray2mat(value); |
---|
25 | if ((val.rows() == 1) && (val.cols() == 1)) { |
---|
26 | Setting &child = (key=="") ? setting.add(Setting::TypeFloat) |
---|
27 | : setting.add(key, Setting::TypeFloat); |
---|
28 | child = val(0,0); |
---|
29 | } else { |
---|
30 | Setting &child = (key=="") ? setting.add(Setting::TypeList) |
---|
31 | : setting.add(key, Setting::TypeList); |
---|
32 | Setting &label = child.add(Setting::TypeString); |
---|
33 | label = "matrix"; |
---|
34 | Setting &cols = child.add( Setting::TypeInt ); |
---|
35 | Setting &rows = child.add( Setting::TypeInt ); |
---|
36 | Setting &elements = child.add( Setting::TypeArray ); |
---|
37 | cols = val.cols(); |
---|
38 | rows = val.rows(); |
---|
39 | for (int i=0; i<val.rows(); i++) { |
---|
40 | for (int j=0; j<val.cols(); j++) { |
---|
41 | Setting &el = elements.add(Setting::TypeFloat); |
---|
42 | el = val(i,j); |
---|
43 | } |
---|
44 | } |
---|
45 | } |
---|
46 | } |
---|
47 | |
---|
48 | void fillGroup(Setting &setting, const mxArray *mxarray) { |
---|
49 | if (!mxIsStruct(mxarray)) { mexErrMsgTxt("Given mxArray is not a struct."); }; |
---|
50 | for(int i=0; i < mxGetNumberOfFields(mxarray); i++) { |
---|
51 | const char *key = mxGetFieldNameByNumber(mxarray, i); |
---|
52 | mxArray *value = mxGetField(mxarray, 0, key); |
---|
53 | if (mxIsChar(value)) { |
---|
54 | Setting &child = setting.add(key, Setting::TypeString); |
---|
55 | child = mxArray2string(value); |
---|
56 | } |
---|
57 | if (mxIsLogical(value)) { |
---|
58 | Setting &child = setting.add(key, Setting::TypeBoolean); |
---|
59 | child = (bool) mxArray2bin(value); |
---|
60 | } |
---|
61 | if (mxIsStruct(value)) { |
---|
62 | Setting &child = setting.add(key, Setting::TypeGroup); |
---|
63 | fillGroup(child, value); |
---|
64 | } |
---|
65 | if (mxIsCell(value)) { |
---|
66 | Setting &child = setting.add(key, Setting::TypeList); |
---|
67 | fillList(child, value); |
---|
68 | } |
---|
69 | if (mxIsNumeric(value)) { |
---|
70 | storeNumeric(setting, value, (string)key); |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | void fillList(Setting &setting, const mxArray *mxarray) { |
---|
76 | if (!mxIsCell(mxarray)) { mexErrMsgTxt("Given mxArray is not a cell."); }; |
---|
77 | for(int i=0; i < mxGetNumberOfElements(mxarray); i++) { |
---|
78 | mxArray *value = mxGetCell(mxarray, i); |
---|
79 | if (mxIsChar(value)) { |
---|
80 | Setting &child = setting.add(Setting::TypeString); |
---|
81 | child = mxArray2string(value); |
---|
82 | } |
---|
83 | if (mxIsLogical(value)) { |
---|
84 | Setting &child = setting.add(Setting::TypeBoolean); |
---|
85 | child = (bool) mxArray2bin(value); |
---|
86 | } |
---|
87 | if (mxIsStruct(value)) { |
---|
88 | Setting &child = setting.add(Setting::TypeGroup); |
---|
89 | fillGroup(child, value); |
---|
90 | } |
---|
91 | if (mxIsCell(value)) { |
---|
92 | Setting &child = setting.add(Setting::TypeList); |
---|
93 | fillList(child, value); |
---|
94 | } |
---|
95 | if (mxIsNumeric(value)) { |
---|
96 | storeNumeric(setting, value); |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | }; |
---|