Ice Sheet System Model  4.18
Code documentation
FetchMatlabData.cpp
Go to the documentation of this file.
1 /*\file FetchData.cpp:
2  * \brief: general I/O interface to fetch data in matlab
3  */
4 
5 #ifdef HAVE_CONFIG_H
6  #include <config.h>
7 #else
8 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
9 #endif
10 
11 #include "./matlabio.h"
12 #include <cstring>
13 
14 /*Primitive data types*/
15 void FetchData(double** pmatrix,int* pM,int *pN,const mxArray* dataref){/*{{{*/
16 
17  double* outmatrix=NULL;
18  int outmatrix_rows,outmatrix_cols;
19 
20  if(mxIsEmpty(dataref) ){
21  /*Nothing to pick up. Just initialize matrix pointer to NULL: */
22  outmatrix_rows=0;
23  outmatrix_cols=0;
24  outmatrix=NULL;
25  }
26  else if( mxIsClass(dataref,"double") ||
27  mxIsClass(dataref,"single") ||
28  mxIsClass(dataref,"int16") ||
29  mxIsClass(dataref,"int8") ||
30  mxIsClass(dataref,"uint8")){
31  /*Check dataref is not pointing to NaN: */
32  if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetM(dataref)==1) && (mxGetN(dataref)==1) ){
33  outmatrix_rows=0;
34  outmatrix_cols=0;
35  outmatrix=NULL;
36  }
37  else{
38  if(!mxIsClass(dataref,"double") && !mxIsClass(dataref,"single")){
39  _printf_("Warning: converting matlab data from '" << mxGetClassName(dataref) << "' to 'double'\n");
40  }
41  /*Convert matlab matrix to double* matrix: */
42  MatlabMatrixToDoubleMatrix(&outmatrix,&outmatrix_rows,&outmatrix_cols,dataref);
43  }
44  }
45  else{
46  /*This is an error: we don't have the correct input!: */
47  _error_("Input parameter of class " << mxGetClassName(dataref) << " not supported yet");
48  }
49 
50  /*Assign output pointers:*/
51  *pmatrix=outmatrix;
52  if (pM)*pM=outmatrix_rows;
53  if (pN)*pN=outmatrix_cols;
54 
55 }
56 /*}}}*/
57 void FetchData(int** pmatrix,int* pM,int *pN,const mxArray* dataref){/*{{{*/
58 
59  int i,outmatrix_rows,outmatrix_cols;
60  double *doublematrix=NULL;
61  int *outmatrix=NULL;
62 
63  if(mxIsEmpty(dataref) ){
64  /*Nothing to pick up. Just initialize matrix pointer to NULL: */
65  outmatrix_rows=0;
66  outmatrix_cols=0;
67  outmatrix=NULL;
68  }
69  else if( mxIsClass(dataref,"double") ||
70  mxIsClass(dataref,"single") ||
71  mxIsClass(dataref,"int16") ||
72  mxIsClass(dataref,"int8") ||
73  mxIsClass(dataref,"uint8")){
74 
75  /*Check dataref is not pointing to NaN: */
76  if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetM(dataref)==1) && (mxGetN(dataref)==1) ){
77  outmatrix_rows=0;
78  outmatrix_cols=0;
79  outmatrix=NULL;
80  }
81  else{
82  if(!mxIsClass(dataref,"double") && !mxIsClass(dataref,"single")){
83  _printf_("Warning: converting matlab data from '" << mxGetClassName(dataref) << "' to 'double'\n");
84  }
85  /*Convert matlab matrix to double* matrix: */
86  MatlabMatrixToDoubleMatrix(&doublematrix,&outmatrix_rows,&outmatrix_cols,dataref);
87 
88  /*Convert double matrix into integer matrix: */
89  outmatrix=xNew<int>(outmatrix_rows*outmatrix_cols);
90  for(i=0;i<outmatrix_rows*outmatrix_cols;i++)outmatrix[i]=(int)doublematrix[i];
91  }
92  }
93  else{
94  /*This is an error: we don't have the correct input!: */
95  _error_("Input parameter of class " << mxGetClassName(dataref) << " not supported yet");
96  }
97 
98  /*Assign output pointers:*/
99  *pmatrix=outmatrix;
100  if (pM)*pM=outmatrix_rows;
101  if (pN)*pN=outmatrix_cols;
102 }
103 /*}}}*/
104 void FetchData(bool** pmatrix,int* pM,int *pN,const mxArray* dataref){/*{{{*/
105 
106  int i,outmatrix_rows,outmatrix_cols;
107  double *doublematrix=NULL;
108  bool *outmatrix=NULL;
109 
110  if(mxIsEmpty(dataref) ){
111  /*Nothing to pick up. Just initialize matrix pointer to NULL: */
112  outmatrix_rows=0;
113  outmatrix_cols=0;
114  outmatrix=NULL;
115  }
116  else if (mxIsClass(dataref,"double") ){
117 
118  /*Check dataref is not pointing to NaN: */
119  if ( mxIsNaN(*(mxGetPr(dataref))) && (mxGetM(dataref)==1) && (mxGetN(dataref)==1) ){
120  outmatrix_rows=0;
121  outmatrix_cols=0;
122  outmatrix=NULL;
123  }
124  else{
125 
126  /*Convert matlab matrix to double* matrix: */
127  MatlabMatrixToDoubleMatrix(&doublematrix,&outmatrix_rows,&outmatrix_cols,dataref);
128 
129  /*Convert double matrix into integer matrix: */
130  outmatrix=xNew<bool>(outmatrix_rows*outmatrix_cols);
131  for(i=0;i<outmatrix_rows;i++)outmatrix[i]=(bool)doublematrix[i];
132  }
133  }
134  else{
135  /*This is an error: we don't have the correct input!: */
136  _error_("Input parameter of class " << mxGetClassName(dataref) << " not supported yet");
137  }
138 
139  /*Assign output pointers:*/
140  *pmatrix=outmatrix;
141  if (pM)*pM=outmatrix_rows;
142  if (pN)*pN=outmatrix_cols;
143 }
144 /*}}}*/
145 void FetchData(double** pvector,int* pM,const mxArray* dataref){/*{{{*/
146 
147  double* outvector=NULL;
148  int M,N;
149 
150  /*Use Fetch matrix*/
151  FetchData(&outvector,&M,&N,dataref) ;
152 
153  /*Check that it is a vector*/
154  if(M*N>0 && (M!=1 && N!=1)){
155  _error_("input vector of size " << M << "x" << N << " should have only one column");
156  }
157 
158  /*Transpose Row vectors*/
159  if(M==1 && N>1) M=N;
160 
161  /*Assign output pointers:*/
162  *pvector=outvector;
163  if(pM)*pM=M;
164 }
165 /*}}}*/
166 void FetchData(int** pvector,int* pM,const mxArray* dataref){/*{{{*/
167 
168  int i;
169  double *doublevector = NULL;
170  int *outvector = NULL;
171  int outvector_rows;
172 
173  if(mxIsEmpty(dataref)){
174  /*Nothing to pick up. Just initialize matrix pointer to NULL: */
175  outvector_rows=0;
176  outvector=NULL;
177  }
178  else if (mxIsClass(dataref,"double") ){
179 
180  /*Convert matlab vector to double* vector: */
181  FetchData(&doublevector,&outvector_rows,dataref);
182 
183  /*Convert double vector into integer vector: */
184  outvector=xNew<int>(outvector_rows);
185  for(i=0;i<outvector_rows;i++)outvector[i]=(int)doublevector[i];
186  }
187  else{
188  /*This is an error: we don't have the correct input!: */
189  _error_("Input parameter of class " << mxGetClassName(dataref) << " not supported yet");
190  }
191 
192  /*Assign output pointers:*/
193  *pvector=outvector;
194  if (pM)*pM=outvector_rows;
195 }
196 /*}}}*/
197 void FetchData(bool** pvector,int* pM,const mxArray* dataref){/*{{{*/
198 
199  int i;
200  double *doublevector = NULL;
201  bool *outvector = NULL;
202  int outvector_rows;
203 
204  if(mxIsEmpty(dataref)){
205  /*Nothing to pick up. Just initialize matrix pointer to NULL: */
206  outvector_rows=0;
207  outvector=NULL;
208  }
209  else if (mxIsClass(dataref,"double") ){
210 
211  /*Convert matlab vector to double* vector: */
212  FetchData(&doublevector,&outvector_rows,dataref);
213 
214  /*Convert double vector into integer vector: */
215  outvector=xNew<bool>(outvector_rows);
216  for(i=0;i<outvector_rows;i++)outvector[i]=(bool)doublevector[i];
217  }
218  else{
219  /*This is an error: we don't have the correct input!: */
220  _error_("Input parameter of class " << mxGetClassName(dataref) << " not supported yet");
221  }
222 
223  /*Assign output pointers:*/
224  *pvector=outvector;
225  if (pM)*pM=outvector_rows;
226 }
227 /*}}}*/
228 void FetchData(float** pvector,int* pM,const mxArray* dataref){/*{{{*/
229 
230  int i;
231  double *doublevector = NULL;
232  float *outvector = NULL;
233  int outvector_rows;
234 
235  if(mxIsEmpty(dataref)){
236  /*Nothing to pick up. Just initialize matrix pointer to NULL: */
237  outvector_rows=0;
238  outvector=NULL;
239  }
240  else if (mxIsClass(dataref,"double") ){
241 
242  /*Convert matlab vector to double* vector: */
243  FetchData(&doublevector,&outvector_rows,dataref);
244 
245  /*Convert double vector into float vector: */
246  outvector=xNew<float>(outvector_rows);
247  for(i=0;i<outvector_rows;i++)outvector[i]=(float)doublevector[i];
248  }
249  else{
250  /*This is an error: we don't have the correct input!: */
251  _error_("Input parameter of class " << mxGetClassName(dataref) << " not supported yet");
252  }
253 
254  /*Assign output pointers:*/
255  *pvector=outvector;
256  if (pM)*pM=outvector_rows;
257 }
258 /*}}}*/
259 void FetchData(char** pstring,const mxArray* dataref){/*{{{*/
260 
261  char* outstring=NULL;
262 
263  /*Ok, the string should be coming directly from the matlab workspace: */
264  if (!mxIsClass(dataref,"char")){
265  _error_("input data_type is not a string!");
266  }
267  else{
268  /*Recover the string:*/
269  int stringlen;
270 
271  stringlen = mxGetM(dataref)*mxGetN(dataref)+1;
272  outstring =xNew<char>(stringlen);
273  mxGetString(dataref,outstring,stringlen);
274  }
275 
276  /*Assign output pointers:*/
277  *pstring=outstring;
278 }/*}}}*/
279 void FetchData(double* pscalar,const mxArray* dataref){/*{{{*/
280 
281  double scalar;
282 
283  if (!mxIsClass(dataref,"double")){
284  _error_("input data_type is not a double!");
285  }
286  else{
287  /*Recover the double: */
288  scalar=mxGetScalar(dataref);
289  }
290 
291  /*Assign output pointers:*/
292  *pscalar=scalar;
293 }
294 /*}}}*/
295 void FetchData(int* pinteger,const mxArray* dataref){/*{{{*/
296 
297  int integer;
298 
299  if (!mxIsClass(dataref,"double")){
300  _error_("input data_type is not a scalar!");
301  }
302  else{
303  /*Recover the double: */
304  integer=(int)mxGetScalar(dataref);
305  }
306 
307  /*Assign output pointers:*/
308  *pinteger=integer;
309 }
310 /*}}}*/
311 void FetchData(bool* pboolean,const mxArray* dataref){/*{{{*/
312 
313  bool* mxbool_ptr=NULL;
314 
315  if (mxIsClass(dataref,"logical")){
316  if(mxGetM(dataref)!=1) _error_("input data is not of size 1x1");
317  if(mxGetN(dataref)!=1) _error_("input data is not of size 1x1");
318  mxbool_ptr=mxGetLogicals(dataref);
319  }
320  else{
321  _error_("input data_type is not a bool!");
322  }
323 
324  *pboolean=*mxbool_ptr;
325 }
326 /*}}}*/
327 
328 /*ISSM objects*/
329 void FetchData(BamgGeom** pbamggeom,const mxArray* dataref){/*{{{*/
330 
331  /*Initialize output*/
332  BamgGeom* bamggeom=new BamgGeom();
333 
334  /*Fetch all fields*/
335  FetchData(&bamggeom->Vertices,&bamggeom->VerticesSize[0],&bamggeom->VerticesSize[1],mxGetAssignedField(dataref,0,"Vertices"));
336  FetchData(&bamggeom->Edges, &bamggeom->EdgesSize[0], &bamggeom->EdgesSize[1], mxGetAssignedField(dataref,0,"Edges"));
337  FetchData(&bamggeom->Corners, &bamggeom->CornersSize[0], &bamggeom->CornersSize[1], mxGetAssignedField(dataref,0,"Corners"));
338  FetchData(&bamggeom->RequiredVertices,&bamggeom->RequiredVerticesSize[0],&bamggeom->RequiredVerticesSize[1],mxGetAssignedField(dataref,0,"RequiredVertices"));
339  FetchData(&bamggeom->RequiredEdges, &bamggeom->RequiredEdgesSize[0], &bamggeom->RequiredEdgesSize[1], mxGetAssignedField(dataref,0,"RequiredEdges"));
340  FetchData(&bamggeom->CrackedEdges,&bamggeom->CrackedEdgesSize[0],&bamggeom->CrackedEdgesSize[1],mxGetAssignedField(dataref,0,"CrackedEdges"));
341  FetchData(&bamggeom->SubDomains,&bamggeom->SubDomainsSize[0],&bamggeom->SubDomainsSize[1],mxGetAssignedField(dataref,0,"SubDomains"));
342 
343  /*Assign output pointers:*/
344  *pbamggeom=bamggeom;
345 }
346 /*}}}*/
347 void FetchData(BamgMesh** pbamgmesh,const mxArray* dataref){/*{{{*/
348 
349  /*Initialize output*/
350  BamgMesh* bamgmesh=new BamgMesh();
351 
352  /*Fetch all fields*/
353  FetchData(&bamgmesh->Vertices,&bamgmesh->VerticesSize[0],&bamgmesh->VerticesSize[1],mxGetAssignedField(dataref,0,"Vertices"));
354  FetchData(&bamgmesh->Edges, &bamgmesh->EdgesSize[0], &bamgmesh->EdgesSize[1], mxGetAssignedField(dataref,0,"Edges"));
355  FetchData(&bamgmesh->Triangles, &bamgmesh->TrianglesSize[0], &bamgmesh->TrianglesSize[1], mxGetAssignedField(dataref,0,"Triangles"));
356  FetchData(&bamgmesh->CrackedEdges,&bamgmesh->CrackedEdgesSize[0],&bamgmesh->CrackedEdgesSize[1],mxGetAssignedField(dataref,0,"CrackedEdges"));
357  FetchData(&bamgmesh->VerticesOnGeomEdge,&bamgmesh->VerticesOnGeomEdgeSize[0],&bamgmesh->VerticesOnGeomEdgeSize[1],mxGetAssignedField(dataref,0,"VerticesOnGeomEdge"));
358  FetchData(&bamgmesh->VerticesOnGeomVertex,&bamgmesh->VerticesOnGeomVertexSize[0],&bamgmesh->VerticesOnGeomVertexSize[1],mxGetAssignedField(dataref,0,"VerticesOnGeomVertex"));
359  FetchData(&bamgmesh->EdgesOnGeomEdge, &bamgmesh->EdgesOnGeomEdgeSize[0], &bamgmesh->EdgesOnGeomEdgeSize[1], mxGetAssignedField(dataref,0,"EdgesOnGeomEdge"));
360  FetchData(&bamgmesh->IssmSegments,&bamgmesh->IssmSegmentsSize[0],&bamgmesh->IssmSegmentsSize[1],mxGetAssignedField(dataref,0,"IssmSegments"));
361 
362  /*Assign output pointers:*/
363  *pbamgmesh=bamgmesh;
364 }
365 /*}}}*/
366 void FetchData(BamgOpts** pbamgopts,const mxArray* dataref){/*{{{*/
367 
368  /*Initialize output*/
369  BamgOpts* bamgopts=new BamgOpts();
370 
371  /*Fetch all fields*/
372  FetchData(&bamgopts->anisomax,mxGetField(dataref,0,"anisomax"));
373  FetchData(&bamgopts->cutoff,mxGetField(dataref,0,"cutoff"));
374  FetchData(&bamgopts->coeff,mxGetField(dataref,0,"coeff"));
375  FetchData(&bamgopts->errg,mxGetField(dataref,0,"errg"));
376  FetchData(&bamgopts->gradation,mxGetField(dataref,0,"gradation"));
377  FetchData(&bamgopts->Hessiantype,mxGetField(dataref,0,"Hessiantype"));
378  FetchData(&bamgopts->maxnbv,mxGetField(dataref,0,"maxnbv"));
379  FetchData(&bamgopts->maxsubdiv,mxGetField(dataref,0,"maxsubdiv"));
380  FetchData(&bamgopts->Metrictype,mxGetField(dataref,0,"Metrictype"));
381  FetchData(&bamgopts->nbjacobi,mxGetField(dataref,0,"nbjacobi"));
382  FetchData(&bamgopts->nbsmooth,mxGetField(dataref,0,"nbsmooth"));
383  FetchData(&bamgopts->omega,mxGetField(dataref,0,"omega"));
384  FetchData(&bamgopts->power,mxGetField(dataref,0,"power"));
385  FetchData(&bamgopts->verbose,mxGetField(dataref,0,"verbose"));
386 
387  FetchData(&bamgopts->Crack,mxGetField(dataref,0,"Crack"));
388  FetchData(&bamgopts->KeepVertices,mxGetField(dataref,0,"KeepVertices"));
389  FetchData(&bamgopts->splitcorners,mxGetField(dataref,0,"splitcorners"));
390 
391  FetchData(&bamgopts->hmin,mxGetField(dataref,0,"hmin"));
392  FetchData(&bamgopts->hmax,mxGetField(dataref,0,"hmax"));
393  FetchData(&bamgopts->hminVertices,&bamgopts->hminVerticesSize[0],&bamgopts->hminVerticesSize[1],mxGetField(dataref,0,"hminVertices"));
394  FetchData(&bamgopts->hmaxVertices,&bamgopts->hmaxVerticesSize[0],&bamgopts->hmaxVerticesSize[1],mxGetField(dataref,0,"hmaxVertices"));
395  FetchData(&bamgopts->hVertices,&bamgopts->hVerticesLength,mxGetField(dataref,0,"hVertices"));
396  FetchData(&bamgopts->metric,&bamgopts->metricSize[0],&bamgopts->metricSize[1],mxGetField(dataref,0,"metric"));
397  FetchData(&bamgopts->field,&bamgopts->fieldSize[0],&bamgopts->fieldSize[1],mxGetField(dataref,0,"field"));
398  FetchData(&bamgopts->err,&bamgopts->errSize[0],&bamgopts->errSize[1],mxGetField(dataref,0,"err"));
399 
400  /*Additional checks*/
401  bamgopts->Check();
402 
403  /*Assign output pointers:*/
404  *pbamgopts=bamgopts;
405 }
406 /*}}}*/
407 void FetchData(Options** poptions,int istart, int nrhs,const mxArray** pdataref){/*{{{*/
408 
409  char *name = NULL;
410  Option *option = NULL;
411 
412  /*Initialize output*/
413  Options* options=new Options();
414 
415  /*Fetch all options*/
416  for (int i=istart; i<nrhs; i=i+2){
417  if (!mxIsClass(pdataref[i],"char")) _error_("Argument " << i+1 << " must be name of option");
418 
419  FetchData(&name,pdataref[i]);
420  if(i+1 == nrhs) _error_("Argument " << i+2 << " must exist and be value of option \"" << name << "\".");
421 
422  option=(Option*)OptionParse(name,&pdataref[i+1]);
423  options->AddOption(option);
424  option=NULL;
425  }
426 
427  /*Assign output pointers:*/
428  *poptions=options;
429 }
430 /*}}}*/
431 void FetchData(Contours** pcontours,const mxArray* dataref){/*{{{*/
432 
433  int numcontours,index,test1,test2;
434  char *contourname = NULL;
435  Contours *contours = NULL;
436  Contour<double> *contouri = NULL;
437 
438  if(mxIsClass(dataref,"char")){
439  FetchData(&contourname,dataref);
440  contours=ExpRead<double>(contourname);
441  }
442  else if(mxIsClass(dataref,"struct")){
443 
444  contours=new Contours();
445  numcontours=mxGetNumberOfElements(dataref);
446 
447  for(int i=0;i<numcontours;i++){
448 
449  contouri=new Contour<double>();
450 
451  index = mxGetFieldNumber(dataref,"nods");
452  if(index==-1) _error_("input structure does not have a 'nods' field");
453  FetchData(&contouri->nods,mxGetFieldByNumber(dataref,i,index));
454 
455  index = mxGetFieldNumber(dataref,"x");
456  if(index==-1) _error_("input structure does not have a 'x' field");
457  FetchData(&contouri->x,&test1,&test2,mxGetFieldByNumber(dataref,i,index));
458  if(test1!=contouri->nods || test2!=1) _error_("field x should be of size ["<<contouri->nods<<" 1]");
459 
460  index = mxGetFieldNumber(dataref,"y");
461  if(index==-1) _error_("input structure does not have a 'y' field");
462  FetchData(&contouri->y,&test1,&test2,mxGetFieldByNumber(dataref,i,index));
463  if(test1!=contouri->nods || test2!=1) _error_("field y should be of size ["<<contouri->nods<<" 1]");
464 
465  contours->AddObject(contouri);
466  }
467  }
468  else{
469  _error_("Contour is neither a string nor a structure and cannot be loaded ("<<mxGetClassName(dataref)<<" not supported)");
470  }
471 
472  /*clean-up and assign output pointer*/
473  xDelete<char>(contourname);
474  *pcontours=contours;
475 }
476 /*}}}*/
477 void FetchChacoData(int* pnvtxs,int** padjacency,int** pstart,float** pewgts,const mxArray* A_IN, const mxArray* EWGTS_IN){/*{{{*/
478 
479  /*Fetch adjacency matrix: */
480  int nvtxs = mxGetN(A_IN);
481  mwIndex* mwstart = mxGetJc(A_IN);
482  mwIndex* mwadjacency = mxGetIr(A_IN);
483  int nzmax = mxGetNzmax(A_IN);
484 
485  int* start = xNew<int>(nvtxs+1);
486  for(int i=0;i<nvtxs+1;i++) start[i]=(int)mwstart[i];
487 
488  int* adjacency = xNew<int>(nzmax);
489  for(int i=0; i<nzmax; i++) adjacency[i]= (int)mwadjacency[i];
490 
491  /*Get edges weights*/
492  int nedges = start[nvtxs];
493  float* ewgts = NULL;
494  if(!mxIsEmpty(EWGTS_IN)){
495  ewgts = xNewZeroInit<float>(nedges);
496  double* doublepointer = mxGetPr(A_IN);
497  for(int i = 0; i<nedges;i++) ewgts[i] = (float)doublepointer[i];
498  }
499 
500  /*Assign output pointers*/
501  *pnvtxs = nvtxs;
502  *padjacency = adjacency;
503  *pstart = start;
504  *pewgts = ewgts;
505 }
506 /*}}}*/
507 
508 /*Toolkit*/
509 int MatlabMatrixToDoubleMatrix(double** pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix){/*{{{*/
510 
511  /*Get Matrix size*/
512  int rows=mxGetM(mxmatrix);
513  int cols=mxGetN(mxmatrix);
514 
515  /*Return of Matrix is empty*/
516  if(rows*cols == 0){
517  *pmatrix = NULL;
518  *pmatrix_rows = rows;
519  *pmatrix_cols = cols;
520  return 1;
521  }
522 
523  /*Initialize output*/
524  double* matrix=xNewZeroInit<double>(rows*cols);
525 
526  /*First check if we are dealing with a sparse matrix: */
527  if(mxIsSparse(mxmatrix)){
528 
529  /*Dealing with sparse matrix: recover size first: */
530  double* pmxmatrix=(double*)mxGetPr(mxmatrix);
531 
532  /*Now, get ir,jc and pr: */
533  mwIndex* ir=mxGetIr(mxmatrix);
534  mwIndex* jc=mxGetJc(mxmatrix);
535 
536  /*Now, start inserting data into double* matrix: */
537  int count=0;
538  for(int i=0;i<cols;i++){
539  for(int j=0;j<(jc[i+1]-jc[i]);j++){
540  matrix[rows*ir[count]+i]=pmxmatrix[count];
541  count++;
542  }
543  }
544  }
545  else if(mxIsClass(mxmatrix,"double")){
546  double* pmxmatrix=(double*)mxGetPr(mxmatrix);
547  for(int i=0;i<rows;i++) for(int j=0;j<cols;j++) matrix[cols*i+j]=(double)pmxmatrix[rows*j+i];
548  }
549  else if(mxIsClass(mxmatrix,"single")){
550  float *pmxmatrix=(float*)mxGetPr(mxmatrix);
551  for(int i=0;i<rows;i++) for(int j=0;j<cols;j++) matrix[cols*i+j]=(double)pmxmatrix[rows*j+i];
552  }
553  else if(mxIsClass(mxmatrix,"int16")){
554  short int *pmxmatrix=(short*)mxGetPr(mxmatrix);
555  for(int i=0;i<rows;i++) for(int j=0;j<cols;j++) matrix[cols*i+j]=(double)pmxmatrix[rows*j+i];
556  }
557  else if(mxIsClass(mxmatrix,"uint8")){
558  char *pmxmatrix=(char*)mxGetPr(mxmatrix);
559  for(int i=0;i<rows;i++) for(int j=0;j<cols;j++) matrix[cols*i+j]=(double)pmxmatrix[rows*j+i];
560  }
561  else{
562  _error_("Matlab matrix type "<<mxGetClassName(mxmatrix)<<" Not implemented yet");
563  }
564 
565  /*Assign output pointer: */
566  *pmatrix=matrix;
567  *pmatrix_rows=rows;
568  *pmatrix_cols=cols;
569 
570  return 1;
571 }/*}}}*/
572 mxArray* mxGetAssignedField(const mxArray* pmxa_array,int number,const char* field){/*{{{*/
573 
574  /*Output*/
575  mxArray *mxfield = NULL;
576 
577  if(mxIsStruct(pmxa_array)){
578  mxfield = mxGetField(pmxa_array,number,field);
579  }
580  else{
581  /*This is an object, mxGetField returns NULL in old version of matlab (we do not have access to them)*/
582 
583  /*Intermediaries*/
584  mxArray *inputs[2];
585  mwSize ndim = 2;
586  mwSize onebyone[2] = {1,1};
587 
588  /*create index structure used in the assignment (index.type='.' and index.subs='x' for field x*/
589  const char *fnames[2];
590  fnames[0] = "type"; fnames[1] = "subs";
591  mxArray* pindex=mxCreateStructArray( ndim,onebyone,2,fnames);
592  mxSetField( pindex, 0, "type",mxCreateString("."));
593  mxSetField( pindex, 0, "subs",mxCreateString(field));
594  inputs[0]=(mxArray*)pmxa_array; //this is the model
595  inputs[1]=pindex;
596 
597  mexCallMATLAB( 1, &mxfield, 2, (mxArray**)inputs, "subsref");
598  }
599 
600  if(mxfield == NULL) _error_("Could not find field "<< field <<" in structure");
601 
602  return mxfield;
603 }/*}}}*/
604 
605 /*Options*/
606 Option* OptionParse(char* name, const mxArray* prhs[]){ /*{{{*/
607 
608  /*Initialize output*/
609  Option *option = NULL;
610 
611  /*parse the value according to the matlab data type */
612  if (mxIsClass(prhs[0],"double") && (mxGetNumberOfElements(prhs[0])==1)){
613  option=(Option*)OptionDoubleParse(name,prhs);
614  }
615  else if(mxIsClass(prhs[0],"double") && (mxGetNumberOfElements(prhs[0])>1)){
616  option=(Option*)OptionDoubleArrayParse(name,prhs);
617  }
618  else if(mxIsClass(prhs[0],"char")){
619  option=(Option*)OptionCharParse(name,prhs);
620  }
621  else {
622  _error_("Second argument value of option \""<< name <<"\" is of unrecognized class \""<< mxGetClassName(prhs[0]) <<"\".");
623  }
624 
625  return option;
626 }/*}}}*/
627 GenericOption<double>* OptionDoubleParse(char* name, const mxArray* prhs[]){ /*{{{*/
628 
629  /*Initialize option*/
631 
632  /*Copy name*/
633  option->name =xNew<char>(strlen(name)+1);
634  memcpy(option->name,name,(strlen(name)+1)*sizeof(char));
635 
636  /*Fetch data and initialize size*/
637  FetchData(&option->value,prhs[0]);
638  option->size[0] = 1;
639  option->size[1] = 1;
640 
641  return option;
642 }/*}}}*/
643 GenericOption<double*>* OptionDoubleArrayParse(char* name, const mxArray* prhs[]){ /*{{{*/
644 
645  /*Initialize option*/
647 
648  /*Copy name*/
649  option->name =xNew<char>(strlen(name)+1);
650  memcpy(option->name,name,(strlen(name)+1)*sizeof(char));
651 
652  /*check and parse the value */
653  if (!mxIsClass(prhs[0],"double")){
654  _error_("Value of option \"" << option->name << "\" must be class \"double\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
655  }
656 
657  /*Fetch data and initialize size*/
658  FetchData(&option->value,&option->size[0],&option->size[1],prhs[0]);
659 
660  return option;
661 }/*}}}*/
662 GenericOption<char*>* OptionCharParse(char* name, const mxArray* prhs[]){ /*{{{*/
663 
664  /*Initialize option*/
666 
667  /*Copy name*/
668  option->name =xNew<char>(strlen(name)+1);
669  memcpy(option->name,name,(strlen(name)+1)*sizeof(char));
670 
671  /*check and parse the value */
672  if(!mxIsClass(prhs[0],"char")){
673  _error_("Value of option \"" << option->name << "\" must be class \"char\", not class \"" << mxGetClassName(prhs[0]) <<"\".");
674  }
675 
676  /*Fetch data and initialize size*/
677  FetchData(&option->value,prhs[0]);
678  option->size[0] = strlen(name);
679  option->size[1] = 1;
680 
681  return(option);
682 }/*}}}*/
BamgGeom::RequiredVertices
double * RequiredVertices
Definition: BamgGeom.h:19
BamgOpts::cutoff
double cutoff
Definition: BamgOpts.h:14
BamgGeom::Edges
double * Edges
Definition: BamgGeom.h:13
GenericOption::size
int size[2]
Definition: GenericOption.h:27
BamgGeom::CrackedEdges
double * CrackedEdges
Definition: BamgGeom.h:23
FetchData
void FetchData(double **pmatrix, int *pM, int *pN, const mxArray *dataref)
Definition: FetchMatlabData.cpp:15
Options
Definition: Options.h:9
BamgOpts::power
double power
Definition: BamgOpts.h:25
BamgGeom::CornersSize
int CornersSize[2]
Definition: BamgGeom.h:16
BamgMesh::VerticesOnGeomVertexSize
int VerticesOnGeomVertexSize[2]
Definition: BamgMesh.h:19
BamgOpts::err
double * err
Definition: BamgOpts.h:47
BamgMesh::EdgesOnGeomEdge
double * EdgesOnGeomEdge
Definition: BamgMesh.h:24
DataSet::AddObject
int AddObject(Object *object)
Definition: DataSet.cpp:252
_printf_
#define _printf_(StreamArgs)
Definition: Print.h:22
GenericOption::name
char * name
Definition: GenericOption.h:25
BamgOpts::maxsubdiv
double maxsubdiv
Definition: BamgOpts.h:20
BamgGeom::Corners
double * Corners
Definition: BamgGeom.h:17
BamgMesh::VerticesOnGeomEdgeSize
int VerticesOnGeomEdgeSize[2]
Definition: BamgMesh.h:21
BamgGeom::RequiredEdgesSize
int RequiredEdgesSize[2]
Definition: BamgGeom.h:20
BamgOpts::verbose
int verbose
Definition: BamgOpts.h:26
BamgOpts::Crack
int Crack
Definition: BamgOpts.h:29
OptionParse
Option * OptionParse(char *name, const mxArray *prhs[])
Definition: FetchMatlabData.cpp:606
BamgMesh::Vertices
double * Vertices
Definition: BamgMesh.h:12
Contours
Declaration of Contours class.
Definition: Contours.h:10
BamgOpts::field
double * field
Definition: BamgOpts.h:45
BamgOpts::Hessiantype
int Hessiantype
Definition: BamgOpts.h:18
Contour
Definition: Contour.h:15
BamgMesh
Definition: BamgMesh.h:7
BamgGeom::SubDomainsSize
int SubDomainsSize[2]
Definition: BamgGeom.h:24
BamgOpts::coeff
double coeff
Definition: BamgOpts.h:15
GenericOption::value
OptionType value
Definition: GenericOption.h:26
BamgOpts::nbjacobi
int nbjacobi
Definition: BamgOpts.h:22
BamgOpts::omega
double omega
Definition: BamgOpts.h:24
BamgOpts::metricSize
int metricSize[2]
Definition: BamgOpts.h:42
BamgGeom::SubDomains
double * SubDomains
Definition: BamgGeom.h:25
BamgOpts::hVertices
double * hVertices
Definition: BamgOpts.h:41
BamgOpts::KeepVertices
int KeepVertices
Definition: BamgOpts.h:30
BamgMesh::EdgesOnGeomEdgeSize
int EdgesOnGeomEdgeSize[2]
Definition: BamgMesh.h:23
BamgOpts::gradation
double gradation
Definition: BamgOpts.h:17
BamgMesh::VerticesSize
int VerticesSize[2]
Definition: BamgMesh.h:11
BamgOpts::nbsmooth
int nbsmooth
Definition: BamgOpts.h:23
BamgGeom::CrackedEdgesSize
int CrackedEdgesSize[2]
Definition: BamgGeom.h:22
BamgMesh::VerticesOnGeomEdge
double * VerticesOnGeomEdge
Definition: BamgMesh.h:22
BamgOpts::fieldSize
int fieldSize[2]
Definition: BamgOpts.h:44
BamgMesh::EdgesSize
int EdgesSize[2]
Definition: BamgMesh.h:14
mxGetAssignedField
mxArray * mxGetAssignedField(const mxArray *pmxa_array, int number, const char *field)
Definition: FetchMatlabData.cpp:572
OptionDoubleParse
GenericOption< double > * OptionDoubleParse(char *name, const mxArray *prhs[])
Definition: FetchMatlabData.cpp:627
BamgOpts::hmaxVerticesSize
int hmaxVerticesSize[2]
Definition: BamgOpts.h:38
BamgGeom::RequiredVerticesSize
int RequiredVerticesSize[2]
Definition: BamgGeom.h:18
BamgOpts::hmaxVertices
double * hmaxVertices
Definition: BamgOpts.h:39
BamgOpts::hmax
double hmax
Definition: BamgOpts.h:35
matlabio.h
BamgOpts
Definition: BamgOpts.h:8
BamgMesh::Edges
double * Edges
Definition: BamgMesh.h:15
BamgOpts::errSize
int errSize[2]
Definition: BamgOpts.h:46
Contour::nods
int nods
Definition: Contour.h:20
BamgOpts::splitcorners
int splitcorners
Definition: BamgOpts.h:31
BamgMesh::IssmSegmentsSize
int IssmSegmentsSize[2]
Definition: BamgMesh.h:38
BamgOpts::maxnbv
int maxnbv
Definition: BamgOpts.h:19
BamgOpts::metric
double * metric
Definition: BamgOpts.h:43
Option
Definition: Option.h:13
BamgGeom::VerticesSize
int VerticesSize[2]
Definition: BamgGeom.h:10
BamgMesh::TrianglesSize
int TrianglesSize[2]
Definition: BamgMesh.h:16
BamgGeom::EdgesSize
int EdgesSize[2]
Definition: BamgGeom.h:12
BamgGeom
Definition: BamgGeom.h:7
BamgMesh::VerticesOnGeomVertex
double * VerticesOnGeomVertex
Definition: BamgMesh.h:20
Contour::y
doubletype * y
Definition: Contour.h:22
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49
BamgOpts::hmin
double hmin
Definition: BamgOpts.h:34
BamgOpts::Check
void Check(void)
Definition: BamgOpts.cpp:50
Contour::x
doubletype * x
Definition: Contour.h:21
BamgOpts::errg
double errg
Definition: BamgOpts.h:16
FetchChacoData
void FetchChacoData(int *pnvtxs, int **padjacency, int **pstart, float **pewgts, const mxArray *A_IN, const mxArray *EWGTS_IN)
Definition: FetchMatlabData.cpp:477
BamgGeom::Vertices
double * Vertices
Definition: BamgGeom.h:11
BamgOpts::hVerticesLength
int hVerticesLength
Definition: BamgOpts.h:40
OptionDoubleArrayParse
GenericOption< double * > * OptionDoubleArrayParse(char *name, const mxArray *prhs[])
Definition: FetchMatlabData.cpp:643
OptionCharParse
GenericOption< char * > * OptionCharParse(char *name, const mxArray *prhs[])
Definition: FetchMatlabData.cpp:662
BamgGeom::RequiredEdges
double * RequiredEdges
Definition: BamgGeom.h:21
BamgOpts::anisomax
double anisomax
Definition: BamgOpts.h:13
BamgOpts::Metrictype
int Metrictype
Definition: BamgOpts.h:21
BamgMesh::IssmSegments
double * IssmSegments
Definition: BamgMesh.h:39
MatlabMatrixToDoubleMatrix
int MatlabMatrixToDoubleMatrix(double **pmatrix, int *pmatrix_rows, int *pmatrix_cols, const mxArray *mxmatrix)
Definition: FetchMatlabData.cpp:509
BamgMesh::CrackedEdges
double * CrackedEdges
Definition: BamgMesh.h:33
Options::AddOption
int AddOption(Option *in_oobject)
Definition: Options.cpp:33
BamgOpts::hminVerticesSize
int hminVerticesSize[2]
Definition: BamgOpts.h:36
BamgMesh::Triangles
double * Triangles
Definition: BamgMesh.h:17
BamgMesh::CrackedEdgesSize
int CrackedEdgesSize[2]
Definition: BamgMesh.h:32
GenericOption
Definition: GenericOption.h:22
BamgOpts::hminVertices
double * hminVertices
Definition: BamgOpts.h:37