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)
00024 {
00025 int i;
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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
00058 pStructIndex=2;
00059
00060
00061
00062
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
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 if (strcmp(fnames[ifield],"reltol")==0)
00125 {
00126 fieldsStruct = mxGetField(prhs[pStructIndex], 0, "reltol");
00127 P->reltol = *mxGetPr(fieldsStruct);
00128 }
00129 if (strcmp(fnames[ifield],"abstol")==0)
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)
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
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));
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));
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
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
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 }