[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;
|
---|
| 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;
|
---|
[7738] | 32 | Options* options=NULL;
|
---|
[7734] | 33 |
|
---|
[7655] | 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) {
|
---|
[12497] | 42 | KMLMeshWriteUsage(); _error2_("KMLMeshWrite usage error");
|
---|
[7655] | 43 | }
|
---|
[7734] | 44 | if (nrhs < NRHS) {
|
---|
[12497] | 45 | KMLMeshWriteUsage(); _error2_("KMLMeshWrite usage error");
|
---|
[7655] | 46 | }
|
---|
| 47 |
|
---|
| 48 | /*Input datasets: */
|
---|
[11933] | 49 | FetchData(&name,NAME);
|
---|
[7655] | 50 |
|
---|
| 51 | /* notes is typically a cell array of character strings */
|
---|
| 52 | if (mxIsCell(NOTES)) {
|
---|
[7734] | 53 | for (nindex=0; nindex<mxGetNumberOfElements(NOTES); nindex++) {
|
---|
[7655] | 54 | notesi=mxGetCell(NOTES,nindex);
|
---|
| 55 | if (notesi && mxIsChar(notesi) && mxGetNumberOfElements(notesi)) {
|
---|
| 56 | if (!notes) {
|
---|
[12060] | 57 | notes=(char *) xmalloc((mxGetNumberOfElements(notesi)+1)*sizeof(char));
|
---|
[7655] | 58 | mxGetString(notesi,notes,mxGetNumberOfElements(notesi)+1);
|
---|
| 59 | }
|
---|
| 60 | else {
|
---|
| 61 | /* note that strlen does not include trailing null */
|
---|
[12060] | 62 | notes=(char*)xrealloc(notes,(strlen(notes)+1+mxGetNumberOfElements(notesi)+1)*sizeof(char));
|
---|
[7655] | 63 | strcat(notes,"\n");
|
---|
| 64 | mxGetString(notesi,¬es[strlen(notes)],mxGetNumberOfElements(notesi)+1);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | else
|
---|
[11933] | 70 | FetchData(¬es,NOTES);
|
---|
| 71 | FetchData(&elem,&melem,&nelem,ELEMHANDLE);
|
---|
| 72 | FetchData(&nodecon,&mncon,&nncon,NODECONHANDLE);
|
---|
| 73 | FetchData(&lat,&mlat,&nlat,LATHANDLE);
|
---|
[7655] | 74 | llat=mlat*nlat;
|
---|
[11933] | 75 | FetchData(&lng,&mlng,&nlng,LNGHANDLE);
|
---|
[7655] | 76 | llng=mlng*nlng;
|
---|
[11933] | 77 | FetchData(&part,&mprt,&nprt,PARTHANDLE);
|
---|
[7655] | 78 | lprt=mprt*nprt;
|
---|
[11933] | 79 | FetchData(&data,&mdata,&ndata,DATAHANDLE);
|
---|
| 80 | FetchData(&cmap,&mcmap,&ncmap,CMAPHANDLE);
|
---|
| 81 | FetchData(&filnam,FILENAME);
|
---|
[12049] | 82 | FetchData(&options,NRHS,nrhs,prhs);
|
---|
[7655] | 83 |
|
---|
| 84 | /*some checks*/
|
---|
[12049] | 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;
|
---|
[7655] | 87 |
|
---|
[12049] | 88 | if (nodecon && (mncon != nnodes))
|
---|
[12497] | 89 | {_error2_("Nodal connectivity table, if supplied, must be supplied for all nodes.");}
|
---|
[7661] | 90 | else if (!nodecon)
|
---|
| 91 | mncon=nnodes;
|
---|
[7655] | 92 | if ((llat != nnodes) || (llng != nnodes) || (llat != llng))
|
---|
[12497] | 93 | _error2_("Latitude and longitude vectors must be supplied for all nodes.");
|
---|
[7655] | 94 | if (part && (lprt != nnodes))
|
---|
[12497] | 95 | _error2_("Partitioning vector, if supplied, must be supplied for all nodes.");
|
---|
[7659] | 96 | if (data && !((mdata == nnodes) || (mdata == melem)))
|
---|
[12497] | 97 | _error2_("Data matrix, if supplied, must be supplied for all nodes or all elements.");
|
---|
[7655] | 98 | if (cmap && (ncmap != 3))
|
---|
[12497] | 99 | _error2_("Colormap matrix, if supplied, must have three columns for rgb.");
|
---|
[7655] | 100 | if (!strlen(filnam))
|
---|
| 101 | strcpy(filnam,"stdout");
|
---|
| 102 |
|
---|
[12049] | 103 | /* Run core computations: */
|
---|
[7655] | 104 | fid=fopen(filnam,"w");
|
---|
[12049] | 105 | KMLMeshWritex(&ierror,name,notes,elem,melem,nelem,nodecon,mncon,nncon,lat,lng,part,data,mdata,ndata,cmap,mcmap,ncmap,fid);
|
---|
[7655] | 106 | fclose(fid);
|
---|
| 107 |
|
---|
| 108 | /*Write data: */
|
---|
[11933] | 109 | WriteData(ERRORFLAG,ierror);
|
---|
[7655] | 110 |
|
---|
[7738] | 111 | /*Clean-up*/
|
---|
| 112 | delete options;
|
---|
| 113 | if (mxIsCell(NOTES) && notes) xfree((void**)¬es);
|
---|
[7734] | 114 |
|
---|
[7655] | 115 | /*end module: */
|
---|
| 116 | MODULEEND();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[12049] | 119 | void KMLMeshWriteUsage(void){
|
---|
[12518] | 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_("");
|
---|
[7655] | 143 | }
|
---|