root/library/bdm/mex/mex_parser.h @ 599

Revision 599, 4.3 kB (checked in by miro, 15 years ago)

mexBM fix (?):
* added line BM::fromSetting,
* estimator renamed from testBM to mexBM

Line 
1#ifndef MXPARSE_H
2#define MXPARSE_H
3
4
5#include "../base/libconfig/libconfig.h++"
6#include <itpp/itbase.h>
7#include <itpp/itmex.h>
8#include <stdlib.h>
9
10
11using namespace itpp;
12using namespace std;
13using namespace libconfig;
14
15//! Class for writing Settings into Matlab's mxArray
16class UImxArray : public Config {
17public:
18        //! Build an instance of Config with fields filled from the given \a mxarray
19        UImxArray ( const mxArray *mxarray ) : Config() {
20                Setting & setting = this->getRoot(); //setting is a group
21                if ( !mxIsStruct ( mxarray ) ) {
22                        mexErrMsgTxt ( "Given mxArray is not a struct." );
23                };
24                //mexCallMATLAB(0, NULL, 1, (mxArray **) &mxarray, "dump");
25                fillGroup ( setting, mxarray );
26                setAutoConvert ( true );
27        }
28        UImxArray() : Config() {
29                setAutoConvert ( true );
30        }
31        //! Add libconfig's \c list to the structure
32        void addList ( const mxArray *mxarray, const char* name ) {
33                Setting & setting = this->getRoot(); //setting is a group
34                Setting & child = setting.add ( name, Setting::TypeList );
35                fillList ( child, mxarray );
36        }
37        //! Add libconfig's \c group to the structure
38        void addGroup ( const mxArray *mxarray, const char* name ) {
39                Setting & setting = this->getRoot(); //setting is a group
40                Setting & child = setting.add ( name, Setting::TypeGroup );
41                fillGroup ( child, mxarray );
42        }
43        //! Operator for more convenient access to this Confic
44        operator Setting&() {
45                return getRoot();
46        }
47private:
48        void storeNumeric ( Setting &setting, const mxArray *value, string key = "" ) {
49                //TODO: integer matrices
50                if ( !mxIsNumeric ( value ) ) {
51                        mexErrMsgTxt ( "Given mxArray is not numeric." );
52                };
53                mat val = mxArray2mat ( value );
54                if ( ( val.rows() == 1 ) && ( val.cols() == 1 ) ) {
55                        Setting &child = ( key == "" ) ? setting.add ( Setting::TypeFloat )
56                                         : setting.add ( key, Setting::TypeFloat );
57                        child = val ( 0, 0 );
58                } else {
59                        Setting &child = ( key == "" ) ? setting.add ( Setting::TypeList )
60                                         : setting.add ( key, Setting::TypeList );
61                        Setting &label = child.add ( Setting::TypeString );
62                        label = "matrix";
63                        Setting &rows = child.add ( Setting::TypeInt );
64                        Setting &cols = child.add ( Setting::TypeInt );
65                        Setting &elements = child.add ( Setting::TypeArray );
66                        cols = val.cols();
67                        rows = val.rows();
68                        for ( int i = 0; i < val.rows(); i++ ) {
69                                for ( int j = 0; j < val.cols(); j++ ) {
70                                        Setting &el = elements.add ( Setting::TypeFloat );
71                                        el = val ( i, j );
72                                }
73                        }
74                }
75        }
76
77        void fillGroup ( Setting &setting, const mxArray *mxarray ) {
78                if ( !mxIsStruct ( mxarray ) ) {
79                        mexErrMsgTxt ( "Given mxArray is not a struct." );
80                };
81                for ( int i = 0; i < mxGetNumberOfFields ( mxarray ); i++ ) {
82                        const char *key = mxGetFieldNameByNumber ( mxarray, i );
83                        mxArray *value = mxGetField ( mxarray, 0, key );
84                        if ( mxIsChar ( value ) ) {
85                                Setting &child = setting.add ( key, Setting::TypeString );
86                                child = mxArray2string ( value );
87                        }
88                        if ( mxIsLogical ( value ) ) {
89                                Setting &child = setting.add ( key, Setting::TypeBoolean );
90                                child = ( bool ) mxArray2bin ( value );
91                        }
92                        if ( mxIsStruct ( value ) ) {
93                                Setting &child = setting.add ( key, Setting::TypeGroup );
94                                fillGroup ( child, value );
95                        }
96                        if ( mxIsCell ( value ) ) {
97                                Setting &child = setting.add ( key, Setting::TypeList );
98                                fillList ( child, value );
99                        }
100                        if ( mxIsNumeric ( value ) ) {
101                                storeNumeric ( setting, value, ( string ) key );
102                        }
103                }
104        }
105
106        void fillList ( Setting &setting, const mxArray *mxarray ) {
107                if ( !mxIsCell ( mxarray ) ) {
108                        mexErrMsgTxt ( "Given mxArray is not a cell." );
109                };
110                for ( unsigned int i = 0; i < (unsigned int) mxGetNumberOfElements ( mxarray ); i++ ) {
111                        mxArray *value = mxGetCell ( mxarray, i );
112                        if ( mxIsChar ( value ) ) {
113                                Setting &child = setting.add ( Setting::TypeString );
114                                child = mxArray2string ( value );
115                        }
116                        if ( mxIsLogical ( value ) ) {
117                                Setting &child = setting.add ( Setting::TypeBoolean );
118                                child = ( bool ) mxArray2bin ( value );
119                        }
120                        if ( mxIsStruct ( value ) ) {
121                                Setting &child = setting.add ( Setting::TypeGroup );
122                                fillGroup ( child, value );
123                        }
124                        if ( mxIsCell ( value ) ) {
125                                Setting &child = setting.add ( Setting::TypeList );
126                                fillList ( child, value );
127                        }
128                        if ( mxIsNumeric ( value ) ) {
129                                storeNumeric ( setting, value );
130                        }
131                }
132        }
133};
134
135#endif //MXPARSE_H
Note: See TracBrowser for help on using the browser.