| 1 | /*\file KMLMeshWrite.c | 
|---|
| 2 | *\brief: KML mesh writer module. | 
|---|
| 3 | */ | 
|---|
| 4 | #include "./KMLMeshWrite.h" | 
|---|
| 5 |  | 
|---|
| 6 | void mexFunction( int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]){ | 
|---|
| 7 |  | 
|---|
| 8 | int i,j,nnodes=0,verbose=1; | 
|---|
| 9 |  | 
|---|
| 10 | /*input: */ | 
|---|
| 11 | char*   name=NULL; | 
|---|
| 12 | char*   notes=NULL; | 
|---|
| 13 | const mxArray* notesi; | 
|---|
| 14 | mwIndex        nindex; | 
|---|
| 15 | int*    elem=NULL; | 
|---|
| 16 | int     melem=0,nelem=0; | 
|---|
| 17 | int*    nodecon=NULL; | 
|---|
| 18 | int     mncon=0,nncon=0; | 
|---|
| 19 | double* lat=NULL; | 
|---|
| 20 | int     mlat=0,nlat=0,llat=0; | 
|---|
| 21 | double* lng=NULL; | 
|---|
| 22 | int     mlng=0,nlng=0,llng=0; | 
|---|
| 23 | int     nparts=0; | 
|---|
| 24 | int*    part=NULL; | 
|---|
| 25 | int     mprt=0,nprt=0,lprt=0; | 
|---|
| 26 | double* data=NULL; | 
|---|
| 27 | int     mdata=0,ndata=0; | 
|---|
| 28 | double* cmap=NULL; | 
|---|
| 29 | int     mcmap=0,ncmap=0; | 
|---|
| 30 | char*   filnam=NULL; | 
|---|
| 31 | FILE*   fid=NULL; | 
|---|
| 32 | Options* options=NULL; | 
|---|
| 33 |  | 
|---|
| 34 | /* output: */ | 
|---|
| 35 | int     ierror=0; | 
|---|
| 36 |  | 
|---|
| 37 | /*Boot module: */ | 
|---|
| 38 | MODULEBOOT(); | 
|---|
| 39 |  | 
|---|
| 40 | /*checks on arguments on the matlab side: */ | 
|---|
| 41 | if (nlhs > NLHS) { | 
|---|
| 42 | KMLMeshWriteUsage(); _error_("KMLMeshWrite usage error"); | 
|---|
| 43 | } | 
|---|
| 44 | if (nrhs < NRHS) { | 
|---|
| 45 | KMLMeshWriteUsage(); _error_("KMLMeshWrite usage error"); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /*Input datasets: */ | 
|---|
| 49 | FetchData(&name,NAME); | 
|---|
| 50 |  | 
|---|
| 51 | /*  notes is typically a cell array of character strings  */ | 
|---|
| 52 | if (mxIsCell(NOTES)) { | 
|---|
| 53 | for (nindex=0; nindex<mxGetNumberOfElements(NOTES); nindex++) { | 
|---|
| 54 | notesi=mxGetCell(NOTES,nindex); | 
|---|
| 55 | if (notesi && mxIsChar(notesi) && mxGetNumberOfElements(notesi)) { | 
|---|
| 56 | if (!notes) { | 
|---|
| 57 | notes=(char *) xmalloc((mxGetNumberOfElements(notesi)+1)*sizeof(char)); | 
|---|
| 58 | mxGetString(notesi,notes,mxGetNumberOfElements(notesi)+1); | 
|---|
| 59 | } | 
|---|
| 60 | else { | 
|---|
| 61 | /*  note that strlen does not include trailing null  */ | 
|---|
| 62 | notes=(char*)xrealloc(notes,(strlen(notes)+1+mxGetNumberOfElements(notesi)+1)*sizeof(char)); | 
|---|
| 63 | strcat(notes,"\n"); | 
|---|
| 64 | mxGetString(notesi,¬es[strlen(notes)],mxGetNumberOfElements(notesi)+1); | 
|---|
| 65 | } | 
|---|
| 66 | } | 
|---|
| 67 | } | 
|---|
| 68 | } | 
|---|
| 69 | else | 
|---|
| 70 | FetchData(¬es,NOTES); | 
|---|
| 71 | FetchData(&elem,&melem,&nelem,ELEMHANDLE); | 
|---|
| 72 | FetchData(&nodecon,&mncon,&nncon,NODECONHANDLE); | 
|---|
| 73 | FetchData(&lat,&mlat,&nlat,LATHANDLE); | 
|---|
| 74 | llat=mlat*nlat; | 
|---|
| 75 | FetchData(&lng,&mlng,&nlng,LNGHANDLE); | 
|---|
| 76 | llng=mlng*nlng; | 
|---|
| 77 | FetchData(&part,&mprt,&nprt,PARTHANDLE); | 
|---|
| 78 | lprt=mprt*nprt; | 
|---|
| 79 | FetchData(&data,&mdata,&ndata,DATAHANDLE); | 
|---|
| 80 | FetchData(&cmap,&mcmap,&ncmap,CMAPHANDLE); | 
|---|
| 81 | FetchData(&filnam,FILENAME); | 
|---|
| 82 | FetchData(&options,NRHS,nrhs,prhs); | 
|---|
| 83 |  | 
|---|
| 84 | /*some checks*/ | 
|---|
| 85 | for (i=0; i<melem*nelem; i++) if(elem[i]>nnodes) nnodes=elem[i]; | 
|---|
| 86 | if(part) for (i=0; i<lprt; i++) if (part[i]+1 > nparts) nparts=part[i]+1; | 
|---|
| 87 |  | 
|---|
| 88 | if (nodecon && (mncon != nnodes)) | 
|---|
| 89 | {_error_("Nodal connectivity table, if supplied, must be supplied for all nodes.");} | 
|---|
| 90 | else if (!nodecon) | 
|---|
| 91 | mncon=nnodes; | 
|---|
| 92 | if ((llat != nnodes) || (llng != nnodes) || (llat != llng)) | 
|---|
| 93 | _error_("Latitude and longitude vectors must be supplied for all nodes."); | 
|---|
| 94 | if (part && (lprt != nnodes)) | 
|---|
| 95 | _error_("Partitioning vector, if supplied, must be supplied for all nodes."); | 
|---|
| 96 | if (data && !((mdata == nnodes) || (mdata == melem))) | 
|---|
| 97 | _error_("Data matrix, if supplied, must be supplied for all nodes or all elements."); | 
|---|
| 98 | if (cmap && (ncmap != 3)) | 
|---|
| 99 | _error_("Colormap matrix, if supplied, must have three columns for rgb."); | 
|---|
| 100 | if (!strlen(filnam)) | 
|---|
| 101 | strcpy(filnam,"stdout"); | 
|---|
| 102 |  | 
|---|
| 103 | /* Run core computations: */ | 
|---|
| 104 | fid=fopen(filnam,"w"); | 
|---|
| 105 | KMLMeshWritex(&ierror,name,notes,elem,melem,nelem,nodecon,mncon,nncon,lat,lng,part,data,mdata,ndata,cmap,mcmap,ncmap,fid); | 
|---|
| 106 | fclose(fid); | 
|---|
| 107 |  | 
|---|
| 108 | /*Write data: */ | 
|---|
| 109 | WriteData(ERRORFLAG,ierror); | 
|---|
| 110 |  | 
|---|
| 111 | /*Clean-up*/ | 
|---|
| 112 | delete options; | 
|---|
| 113 | if (mxIsCell(NOTES) && notes) xfree((void**)¬es); | 
|---|
| 114 |  | 
|---|
| 115 | /*end module: */ | 
|---|
| 116 | MODULEEND(); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | void KMLMeshWriteUsage(void){ | 
|---|
| 120 | _pprintLine_("KMLMeshWrite - KML mesh writer module:"); | 
|---|
| 121 | _pprintLine_(""); | 
|---|
| 122 | _pprintLine_("   This module writes the mesh of a model as KML polygons into the specified KML file."); | 
|---|
| 123 | _pprintLine_(""); | 
|---|
| 124 | _pprintLine_("   Usage:"); | 
|---|
| 125 | _pprintLine_("      ierror=KMLMeshWrite(name,notes,elem,nodecon,lat,long,part,data,cmap,kmlfile);"); | 
|---|
| 126 | _pprintLine_(""); | 
|---|
| 127 | _pprintLine_("      name       model name (string, may be empty)"); | 
|---|
| 128 | _pprintLine_("      notes      model notes (string or cell array of strings, may be empty)"); | 
|---|
| 129 | _pprintLine_("      elem       elements (double array)"); | 
|---|
| 130 | _pprintLine_("      nodecon    nodal connectivity array (double array, may be empty)"); | 
|---|
| 131 | _pprintLine_("      lat        nodal latititudes (double vector)"); | 
|---|
| 132 | _pprintLine_("      long       nodal longitudes (double vector)"); | 
|---|
| 133 | _pprintLine_("      part       nodal partitions (double vector, may be empty)"); | 
|---|
| 134 | _pprintLine_("      data       nodal or element data (double vector, may be empty)"); | 
|---|
| 135 | _pprintLine_("      cmap       color map (double nx3 array, may be empty)"); | 
|---|
| 136 | _pprintLine_("      kmlfile    KML file name (string)"); | 
|---|
| 137 | _pprintLine_(""); | 
|---|
| 138 | _pprintLine_("      ierror     error flag (double, non-zero for error)"); | 
|---|
| 139 | _pprintLine_(""); | 
|---|
| 140 | _pprintLine_("   Example:"); | 
|---|
| 141 | _pprintLine_("      KMLMeshWrite(md.name,md.notes,md.elements,md.nodeconnectivity,md.lat,md.long,md.part,md.fm_criterion,options.cmap,filekml);"); | 
|---|
| 142 | _pprintLine_(""); | 
|---|
| 143 | } | 
|---|