source: issm/trunk-jpl/src/modules/KMLMeshWrite/KMLMeshWrite.cpp@ 12060

Last change on this file since 12060 was 12060, checked in by Mathieu Morlighem, 13 years ago

do not use Matlab's memory management for internal variable (big mistake)

File size: 4.8 KB
Line 
1/*\file KMLMeshWrite.c
2 *\brief: KML mesh writer module.
3 */
4#include "./KMLMeshWrite.h"
5
6void 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,&notes[strlen(notes)],mxGetNumberOfElements(notesi)+1);
65 }
66 }
67 }
68 }
69 else
70 FetchData(&notes,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**)&notes);
114
115 /*end module: */
116 MODULEEND();
117}
118
119void KMLMeshWriteUsage(void){
120 _printf_(true,"KMLMeshWrite - KML mesh writer module:\n");
121 _printf_(true,"\n");
122 _printf_(true," This module writes the mesh of a model as KML polygons into the specified KML file.\n");
123 _printf_(true,"\n");
124 _printf_(true," Usage:\n");
125 _printf_(true," ierror=KMLMeshWrite(name,notes,elem,nodecon,lat,long,part,data,cmap,kmlfile);\n");
126 _printf_(true,"\n");
127 _printf_(true," name model name (string, may be empty)\n");
128 _printf_(true," notes model notes (string or cell array of strings, may be empty)\n");
129 _printf_(true," elem elements (double array)\n");
130 _printf_(true," nodecon nodal connectivity array (double array, may be empty)\n");
131 _printf_(true," lat nodal latititudes (double vector)\n");
132 _printf_(true," long nodal longitudes (double vector)\n");
133 _printf_(true," part nodal partitions (double vector, may be empty)\n");
134 _printf_(true," data nodal or element data (double vector, may be empty)\n");
135 _printf_(true," cmap color map (double nx3 array, may be empty)\n");
136 _printf_(true," kmlfile KML file name (string)\n");
137 _printf_(true,"\n");
138 _printf_(true," ierror error flag (double, non-zero for error)\n");
139 _printf_(true,"\n");
140 _printf_(true," Example:\n");
141 _printf_(true," KMLMeshWrite(md.name,md.notes,md.elements,md.nodeconnectivity,md.lat,md.long,md.part,md.fm_criterion,options.cmap,filekml);\n");
142 _printf_(true,"\n");
143}
Note: See TracBrowser for help on using the repository browser.