- Main Page
- Namespaces
- Data Structures
- Files

- File List
- Globals

# D:/Basile/VisualCppProjects/hydroMEX3/hydroMEX3/hydro_source/get_mex_arguments.cpp

Go to the documentation of this file.

    00001
    00009 #include "math.h"
    00010 #include "mex.h"
    00011 #include "string.h"
    00012
    00013 #include "Datas.h"
    00014 #include "get_mex_arguments.h"
    00015
    00016
    00017 #ifndef PI_DEF
    00018 #define pi 3.141592653589793
    00019 #define PI 3.141592653589793
    00020 #endif
    00021
    00022
    00023 void initParameters(struct parameters *P) //default values
    00024 {
    00025         int i;
    00026                 //P->x0 = 0;  //unused camera stuff
    00027                 //P->y0 = 0;
    00028                 //P->z0 = 0;
    00029                 //P->alpha_cam = 0;
    00030                 //P->beta_cam = 0;
    00031                 //P->x_rel = 0;
    00032                 //P->y_rel = 0;
    00033                 //P->z_rel = 0;
    00034                 //P->Gain = 0;
    00035                 //P->time_stop = 0;
    00036                 //P.param={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}; //compilateur aime pas ça!! Quel c**!!
    00037                 for (i=0; i<PARAMNUM; i++) P->param[i] = 0;
    00038                 for (i=0; i<STATENUM; i++) P->abstol[i] = (1e-6);
    00039                 P->reltol = (1.0e-4);
    00040         return;
    00041 }
    00042
    00043
    00044
    00045
    00046
    00047
    00048
    00049 void get_Parameters(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[], struct parameters *P)
    00050 {
    00051         int NStructElems, nfields, pStructIndex, ifield, i;
    00052         const char **fnames;
    00053         mxArray *fieldsStruct;
    00054         double* temp;
    00055         long rowLen, colLen;
    00056
    00057         //if (nrhs==4)
    00058                 pStructIndex=2;
    00059         //else if (nrhs==3)
    00060         //      pStructIndex=2;
    00061         //else
    00062         //      mexErrMsgTxt("Number of argument must be 3 or 4");
    00063
    00064         if (nrhs<3) mexErrMsgTxt("Number of argument must be 3 or 4");
    00065
    00066         nfields = mxGetNumberOfFields(prhs[pStructIndex]);
    00067         NStructElems = mxGetNumberOfElements(prhs[pStructIndex]);
    00068
    00069
    00070         fnames = (const char**)mxCalloc(nfields, sizeof(*fnames));
    00071         for (ifield=0; ifield<nfields; ifield++)
    00072         {
    00073                 fnames[ifield] = mxGetFieldNameByNumber(prhs[pStructIndex],ifield);
    00074                 /*if (strcmp(fnames[ifield],"x0")==0)  //unused camera stuff
    00075                 {
    00076                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "x0");
    00077                         P->x0 = *mxGetPr(fieldsStruct);
    00078                 }
    00079                 if (strcmp(fnames[ifield],"y0")==0)
    00080                 {
    00081                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "y0");
    00082                         P->y0 = *mxGetPr(fieldsStruct);
    00083                 }
    00084                 if (strcmp(fnames[ifield],"z0")==0)
    00085                 {
    00086                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "z0");
    00087                         P->z0 = *mxGetPr(fieldsStruct);
    00088                 }
    00089                 if (strcmp(fnames[ifield],"alpha_cam")==0)
    00090                 {
    00091                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "alpha_cam");
    00092                         P->alpha_cam = *mxGetPr(fieldsStruct);
    00093                 }
    00094                 if (strcmp(fnames[ifield],"beta_cam")==0)
    00095                 {
    00096                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "beta_cam");
    00097                         P->beta_cam = *mxGetPr(fieldsStruct);
    00098                 }
    00099                 if (strcmp(fnames[ifield],"x_rel")==0)
    00100                 {
    00101                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "x_rel");
    00102                         P->x_rel = *mxGetPr(fieldsStruct);
    00103                 }
    00104                 if (strcmp(fnames[ifield],"y_rel")==0)
    00105                 {
    00106                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "y_rel");
    00107                         P->y_rel = *mxGetPr(fieldsStruct);
    00108                 }
    00109                 if (strcmp(fnames[ifield],"z_rel")==0)
    00110                 {
    00111                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "z_rel");
    00112                         P->z_rel = *mxGetPr(fieldsStruct);
    00113                 }
    00114                 if (strcmp(fnames[ifield],"Gain")==0)
    00115                 {
    00116                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "Gain");
    00117                         P->Gain = *mxGetPr(fieldsStruct);
    00118                 }
    00119                 if (strcmp(fnames[ifield],"time_stop")==0)
    00120                 {
    00121                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "time_stop");
    00122                         P->time_stop = *mxGetPr(fieldsStruct);
    00123                 }*/
    00124                 if (strcmp(fnames[ifield],"reltol")==0) //scalar
    00125                 {
    00126                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "reltol");
    00127                         P->reltol = *mxGetPr(fieldsStruct);
    00128                 }
    00129                 if (strcmp(fnames[ifield],"abstol")==0) //vector
    00130                 {
    00131                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "abstol");
    00132                         temp=mxGetPr(fieldsStruct);
    00133                         rowLen = (long) mxGetN(fieldsStruct);
    00134                         colLen = (long) mxGetM(fieldsStruct);
    00135                         if (rowLen*colLen == 1)
    00136                                 for (i=0; i<STATENUM; i++) P->abstol[i] = temp[0];
    00137                         else if (rowLen*colLen == STATENUM)
    00138                                 for (i=0; i<STATENUM; i++) P->abstol[i] = temp[i];
    00139                         else
    00140                                 mexErrMsgTxt("P.abstol must be a scalar, a vector of length 12, or not given (default)");
    00141                 }
    00142                 if (strcmp(fnames[ifield],"param")==0) //vector
    00143                 {
    00144                         fieldsStruct = mxGetField(prhs[pStructIndex], 0, "param");
    00145                         temp=mxGetPr(fieldsStruct);
    00146                         for (i=0; i<PARAMNUM; i++) P->param[i] = temp[i];
    00147                 }
    00148
    00149         }
    00150
    00151         return;
    00152 }
    00153
    00154
    00155
    00156
    00157
    00158
    00159 double* get_state(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    00160 {
    00161         double *temp, *state;
    00162         int rowLen, colLen, k;
    00163         rowLen = mxGetN(prhs[1]);
    00164         colLen = mxGetM(prhs[1]);
    00165         if (!((rowLen==STATENUM && colLen==1) || (rowLen==1 && colLen==STATENUM)))
    00166                 mexErrMsgTxt("Second argument (state) must be a 12 elements vector");
    00167         temp = mxGetPr(prhs[1]);
    00168
    00169         //We don't want to write over the passed argument => allocate memory and copy. Don't forget to delete!
    00170         state = new double[rowLen*colLen];
    00171         for (k=0;k<rowLen*colLen;k++) state[k]=temp[k];
    00172         return state;
    00173 }
    00174
    00175 void delete_state(double *state)
    00176 {
    00177         delete state;
    00178 }
    00179
    00180
    00181
    00182 char* get_flag(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    00183 {
    00184         char *flag;
    00185         int flaglen;
    00186
    00187         if (nrhs==3)
    00188         {
    00189                 flag = (char*) mxCalloc(1, sizeof(char));//strcpy_s(flag, defFlag);
    00190                 strcpy_s(flag,1, "");
    00191
    00192
    00193         }
    00194         else if (nrhs==4 || nrhs==5)
    00195         {
    00196                 flaglen = mxGetN(prhs[2])+1;
    00197                 flag = (char*) mxCalloc(flaglen, sizeof(char)); //mxCalloc is similar to malloc in C
    00198                 mxGetString(prhs[2],flag,flaglen);
    00199
    00200         }
    00201         else
    00202         {
    00203                 mexErrMsgTxt("Need 3 or 4 or 5 arguments!");
    00204         }
    00205
    00206         return flag;
    00207 }
    00208
    00209
    00210
    00211
    00212
    00213 void get_joystick_parameters(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[], struct joy_parameters *jP)
    00214 {
    00215         int NStructElems, nfields, pStructIndex, ifield, i;
    00216         const char **fnames;
    00217         mxArray *fieldsStruct;
    00218         double* temp;
    00219
    00220         //default:
    00221         jP->dirX = 1.0;
    00222         jP->dirY = 1.0;
    00223         jP->dirZ = 1.0;
    00224         jP->dirR = 1.0;
    00225
    00226         jP->doX = TRUE;
    00227         jP->doY = TRUE;
    00228         jP->doZ = TRUE;
    00229         jP->doR = TRUE;
    00230
    00231         //If joystick parameters gived by MATLAB:
    00232         if (nrhs==4)
    00233         {
    00234                 pStructIndex=3;
    00235
    00236                 nfields = mxGetNumberOfFields(prhs[pStructIndex]);
    00237                 NStructElems = mxGetNumberOfElements(prhs[pStructIndex]);
    00238
    00239
    00240                 fnames = (const char**)mxCalloc(nfields, sizeof(*fnames));
    00241                 for (ifield=0; ifield<nfields; ifield++)
    00242                 {
    00243                         fnames[ifield] = mxGetFieldNameByNumber(prhs[pStructIndex],ifield);
    00244
    00245                         if (strcmp(fnames[ifield],"dirX")==0)
    00246                         {
    00247                                 fieldsStruct = mxGetField(prhs[pStructIndex], 0, "dirX");
    00248                                 jP->dirX = (float) *mxGetPr(fieldsStruct);
    00249                         }
    00250                         if (strcmp(fnames[ifield],"dirY")==0)
    00251                         {
    00252                                 fieldsStruct = mxGetField(prhs[pStructIndex], 0, "dirY");
    00253                                 jP->dirY = (float) *mxGetPr(fieldsStruct);
    00254                         }
    00255                         if (strcmp(fnames[ifield],"dirZ")==0)
    00256                         {
    00257                                 fieldsStruct = mxGetField(prhs[pStructIndex], 0, "dirZ");
    00258                                 jP->dirZ = (float) *mxGetPr(fieldsStruct);
    00259                         }
    00260                         if (strcmp(fnames[ifield],"dirR")==0)
    00261                         {
    00262                                 fieldsStruct = mxGetField(prhs[pStructIndex], 0, "dirR");
    00263                                 jP->dirR = (float) *mxGetPr(fieldsStruct);
    00264                         }
    00265
    00266                         if (strcmp(fnames[ifield],"doX")==0)
    00267                         {
    00268                                 fieldsStruct = mxGetField(prhs[pStructIndex], 0, "doX");
    00269                                 jP->doX = (bool) *mxGetPr(fieldsStruct);
    00270                         }
    00271                         if (strcmp(fnames[ifield],"doY")==0)
    00272                         {
    00273                                 fieldsStruct = mxGetField(prhs[pStructIndex], 0, "doY");
    00274                                 jP->doY = (bool) *mxGetPr(fieldsStruct);
    00275                         }
    00276                         if (strcmp(fnames[ifield],"doZ")==0)
    00277                         {
    00278                                 fieldsStruct = mxGetField(prhs[pStructIndex], 0, "doZ");
    00279                                 jP->doZ = (bool) *mxGetPr(fieldsStruct);
    00280                         }
    00281                         if (strcmp(fnames[ifield],"doR")==0)
    00282                         {
    00283                                 fieldsStruct = mxGetField(prhs[pStructIndex], 0, "doR");
    00284                                 jP->doR = (bool) *mxGetPr(fieldsStruct);
    00285                         }
    00286                 }
    00287         }
    00288         return;
    00289 }
    00290
    00291
    00292
    00293 void get_time(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[], ODE_data *f_dat)
    00294 {
    00295         long rowLen, colLen;
    00296         rowLen = (long) mxGetN(prhs[0]);
    00297         colLen = (long) mxGetM(prhs[0]);
    00298
    00299         if (rowLen!=1 || colLen!=1) mexErrMsgTxt("First argument must be a scalar:\n 0 => simulate without returning anything\n t => simulate and give state vector back from time 0 to t");
    00300
    00301         f_dat->time_param = *mxGetPr(prhs[0]);
    00302 }