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