source: issm/trunk-jpl/src/c/modules/KMLMeshWritex/KMLMeshWritex.cpp@ 14999

Last change on this file since 14999 was 14999, checked in by Eric.Larour, 12 years ago

CHG: modules should include shared.h, and that's it.

File size: 11.9 KB
RevLine 
[7654]1/*!\file KMLMeshWritex
2 */
3
4#include "./KMLMeshWritex.h"
5#include "../../shared/shared.h"
6#include "../../toolkits/toolkits.h"
7
[12442]8void KMLMeshWritex(int* ierror,char* name,char* notes,int* elem,int melem,int nelem,int* nodecon,int mncon,int nncon,double* lat, double* lng,int* part,double* data, int mdata, int ndata,double* cmap, int mcmap, int ncmap,FILE* fid){
[7654]9
[12442]10 int i,j,k,ipt=0,jpt=0,nnodes;
11 int mxepg = 25;
12 int lwidth = 1;
13 double popac = 0.50;
14 char indent[81] = " ";
15 char cstr[81];
16 double *edata = NULL;
17 bool ncfree=false, edfree=false;
18 KML_Document *kdoc = NULL;
19 KML_Style *kstyle;
20 KML_LineStyle *klsty;
21 KML_PolyStyle *kpsty;
[7654]22
23 clock_t clock0,clock1,clock0a,clock0b,clock0c;
24 time_t time0, time1, time0a, time0b, time0c;
25
26 clock0=clock();
27 time0 =time(NULL);
[12519]28 _pprintString_("\nKMLMeshWritex Module -- " << ctime(&time0));
[7654]29
30/* construct kml document */
31
32 kdoc=new KML_Document();
33 sprintf(kdoc->name ,"ISSM Mesh: %s",name);
34 kdoc->open =1;
35 sprintf(kdoc->descript ,"%s",notes);
36
37/* write style templates for defaults and for each color of the matlab
38 colormap (note that matlab colormap format is rgb, where each varies
39 from 0 to 1, whereas the kml color format is aabbggrr, where each
40 varies from 00 to ff.) */
41
42 klsty=new KML_LineStyle();
43 sprintf(klsty->color ,"ff000000");
44 sprintf(klsty->colormode ,"normal");
45 klsty->width =lwidth;
46 kpsty=new KML_PolyStyle();
47 sprintf(kpsty->color ,"%02xffffff",(int)floor(popac*255+0.5));
48 sprintf(kpsty->colormode ,"random");
49 kstyle=new KML_Style();
[8461]50 kstyle->AddAttrib("id","BlackLineRandomPoly");
[7654]51 kstyle->line =klsty;
52 kstyle->poly =kpsty;
53 (kdoc->style )->AddObject((Object*)kstyle);
54
55 klsty=new KML_LineStyle();
56 sprintf(klsty->color ,"ff000000");
57 sprintf(klsty->colormode ,"normal");
58 klsty->width ,lwidth;
59 kpsty=new KML_PolyStyle();
60 sprintf(kpsty->color ,"00ffffff");
61 sprintf(kpsty->colormode ,"random");
62 kstyle=new KML_Style();
[8461]63 kstyle->AddAttrib("id","BlackLineEmptyPoly");
[7654]64 kstyle->line =klsty;
65 kstyle->poly =kpsty;
66 (kdoc->style )->AddObject((Object*)kstyle);
67
68 klsty=new KML_LineStyle();
69 sprintf(klsty->color ,"ff0000ff");
70 sprintf(klsty->colormode ,"normal");
71 klsty->width =lwidth;
72 kpsty=new KML_PolyStyle();
73 sprintf(kpsty->color ,"%02x0000ff",(int)floor(popac*255+0.5));
74 sprintf(kpsty->colormode ,"random");
75 kstyle=new KML_Style();
[8461]76 kstyle->AddAttrib("id","RedLineRedPoly");
[7654]77 kstyle->line =klsty;
78 kstyle->poly =kpsty;
79 (kdoc->style )->AddObject((Object*)kstyle);
80
81 if (cmap) {
[12519]82 _pprintLine_("Writing " << mcmap << " Matlab colors as KML style templates.");
[7658]83 ipt=0;
[7654]84 for (i=0; i<mcmap; i++) {
85 klsty=new KML_LineStyle();
[9259]86// sprintf(klsty->color ,"ff000000");
87 sprintf(klsty->color ,"%02x%02x%02x%02x",
88 (int)255,
89 (int)floor(cmap[ipt+2]*255+0.5),
90 (int)floor(cmap[ipt+1]*255+0.5),
91 (int)floor(cmap[ipt ]*255+0.5));
[7654]92 sprintf(klsty->colormode ,"normal");
93 klsty->width =lwidth;
94 kpsty=new KML_PolyStyle();
95 sprintf(kpsty->color ,"%02x%02x%02x%02x",
96 (int)floor(popac*255+0.5),
97 (int)floor(cmap[ipt+2]*255+0.5),
98 (int)floor(cmap[ipt+1]*255+0.5),
99 (int)floor(cmap[ipt ]*255+0.5));
100 sprintf(kpsty->colormode ,"normal");
101 kstyle=new KML_Style();
[8461]102 sprintf(cstr,"MatlabColor%d",i+1);
103 kstyle->AddAttrib("id",cstr);
[7654]104 kstyle->line =klsty;
105 kstyle->poly =kpsty;
106 (kdoc->style )->AddObject((Object*)kstyle);
[7658]107 ipt+=ncmap;
[7654]108 }
109 }
110// kdoc->DeepEcho();
111
[7660]112/* create the node connectivity table, if necessary
113 (noting that rows do not need to be sorted, since the elements
114 are consecutively numbered) */
[7658]115
[7660]116 if (!nodecon) {
[12519]117 _pprintLine_("Creating the node connectivity table.");
[7660]118 nncon=mxepg+1;
[12446]119 nodecon=xNewZeroInit<int>(mncon*nncon);
[7660]120 ncfree=true;
121
122 jpt=0;
123 for (i=0; i<melem; i++) {
124 for (j=0; j<nelem; j++) {
125 if (elem[jpt]) {
126 ipt=(elem[jpt]-1)*nncon;
127 if (nodecon[ipt+(nncon-1)] < mxepg) {
128 nodecon[ipt+nodecon[ipt+(nncon-1)]]=i+1;
129 nodecon[ipt+(nncon-1)]++;
130 }
131 else
[13056]132 _error_("Nodal connectivity table needs more than specified " << mxepg << " columns.\n");
[7660]133 }
134 jpt++;
135 }
136 }
137 }
138
139/* average nodal data to element data, if necessary
140 (noting that multiple columns of data are handled here, but not
141 yet below) */
142
[7658]143 if (data) {
144 if (mdata == melem)
145 edata=data;
[7660]146
[7658]147 else if (mdata == mncon) {
[12519]148 _pprintLine_("Averaging nodal data to element data.");
[12446]149 edata=xNewZeroInit<double>(melem*ndata);
[7660]150 edfree=true;
151
[7658]152 ipt=0;
153 jpt=0;
154 for (i=0; i<melem; i++) {
[7660]155 nnodes=0;
[7658]156 for (j=0; j<nelem; j++) {
[7660]157 if (elem[jpt]) {
158 for (k=0; k<ndata; k++)
159 edata[ipt+k]+=data[(elem[jpt]-1)*ndata+k];
160 nnodes++;
161 }
[7658]162 jpt++;
163 }
[7660]164 if (nnodes)
165 for (k=0; k<ndata; k++)
166 edata[ipt+k]/=(double)nnodes;
[7658]167 ipt+=ndata;
168 }
169 }
[7660]170
[7658]171 else
[13056]172 _error_("Data matrix has incorrect number of " << mdata << " rows.\n");
[7658]173 }
174
[7654]175/* write folder for mesh */
176
177 (kdoc ->feature )->AddObject((Object*)KMLMeshElem(elem,melem,nelem,
178 nodecon,mncon,nncon,
179 lat,lng,
[7658]180 edata,
[7654]181 cmap,mcmap,ncmap));
182
[12450]183 if(edfree) xDelete<double>(edata);
184 if(ncfree) xDelete<int>(nodecon);
[7654]185 clock0a=clock();
186 time0a =time(NULL);
187 _printf_(true," Constructed kml document -- %f CPU seconds; %f elapsed seconds.\n\n",
188 ((double)(clock0a-clock0))/CLOCKS_PER_SEC,difftime(time0a,time0));
189
190/* write kml file */
191
[12519]192 _pprintLine_("Writing kml document to file.");
[7654]193 fprintf(fid,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
194 fprintf(fid,"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n");
195 kdoc->Write(fid,indent);
196 fprintf(fid,"</kml>\n");
197 clock0b=clock();
198 time0b =time(NULL);
199 _printf_(true," Wrote kml file -- %f CPU seconds; %f elapsed seconds.\n\n",
200 ((double)(clock0b-clock0a))/CLOCKS_PER_SEC,difftime(time0b,time0a));
201
[12519]202 _pprintLine_("Deleting kml document.");
[7654]203 delete kdoc;
204 clock0c=clock();
205 time0c =time(NULL);
206 _printf_(true," Deleted kml document -- %f CPU seconds; %f elapsed seconds.\n\n",
207 ((double)(clock0c-clock0b))/CLOCKS_PER_SEC,difftime(time0c,time0b));
208
209 clock1=clock();
210 time1 =time(NULL);
211 _printf_(true,"KMLMeshWritex Module -- %f CPU seconds; %f elapsed seconds.\n\n",
212 ((double)(clock1-clock0))/CLOCKS_PER_SEC,difftime(time1,time0));
213
214 return;
215}
216
217KML_Folder* KMLMeshElem(int* elem,int melem,int nelem,
218 int* nodecon,int mncon,int nncon,
219 double* lat, double* lng,
220 double* edata,
221 double* cmap, int mcmap, int ncmap){
222
[7658]223 int i,j,ipt=0;
[7669]224 double alt=0;
[7658]225 double cmin= DBL_MAX,
[7660]226 cmax=-DBL_MAX;
[7658]227 int imap;
[7654]228 KML_Folder* kfold =NULL;
229 KML_Placemark* kplace=NULL;
230 KML_Polygon* kpoly =NULL;
231 KML_LinearRing* kring =NULL;
232
233/* write folder for mesh */
234
235 kfold=new KML_Folder();
[7685]236// sprintf(kfold->name ,"Mesh");
237 sprintf(kfold->name ,"ISSM Targets");
[7654]238 kfold->visibility=1;
[8303]239// sprintf(kfold->descript ,"Elements=%d, Nodes=%d",melem,mncon);
[7685]240 sprintf(kfold->descript ,"campaign{\n");
241 strcat(kfold->descript ," evaluator ClaspTargetEvaluator;\n");
[8046]242 strcat(kfold->descript ," solver IssmSolver;\n");
[7685]243 strcat(kfold->descript ," spacecraft airplane ClaspSpacecraft(\n");
244 strcat(kfold->descript ," dutyCycleDuration=0,\n");
245 strcat(kfold->descript ," dutyCycleOnDuration=0,\n");
246 strcat(kfold->descript ," memoryInit=15000,\n");
247 strcat(kfold->descript ," memoryLimit=40000000,\n");
248 strcat(kfold->descript ," maxDataCollectionRate=27,\n");
249 strcat(kfold->descript ," maxDataDownlinkRate=10);\n");
250 strcat(kfold->descript ,"\n");
251 strcat(kfold->descript ," //sensor names\n");
252 strcat(kfold->descript ," sensor qqp_swath = 2,102,1002,1102;\n");
253 strcat(kfold->descript ,"\n");
254 strcat(kfold->descript ," //sensor ids to modes\n");
255 strcat(kfold->descript ," low_bandwidth_single_pol = 2,102,1002,1102;\n");
256 strcat(kfold->descript ," single_pol = 2,102,1002,1102;\n");
257 strcat(kfold->descript ," dual_pol = 2,102,1002,1102;\n");
258 strcat(kfold->descript ," quad_pol = 2,102,1002,1102;\n");
259 strcat(kfold->descript ,"\n");
260 strcat(kfold->descript ," //LRAD\n");
261 strcat(kfold->descript ," //Note all targets are \"ascending right\"-- i.e. mode=2\n");
262 strcat(kfold->descript ," left = 1002,1102;\n");
263 strcat(kfold->descript ," right = 2,102;\n");
264 strcat(kfold->descript ," ascending = 2,1002;\n");
265 strcat(kfold->descript ," descending = 102,1102;\n");
266 strcat(kfold->descript ,"\n");
267 strcat(kfold->descript ," //data rates\n");
268 strcat(kfold->descript ," low_bandwidth_single_pol datarate = 0.896;\n");
269 strcat(kfold->descript ," single_pol datarate = 4.214;\n");
270 strcat(kfold->descript ," dual_pol datarate = 8.428;\n");
271 strcat(kfold->descript ," quad_pol datarate = 16.856;\n");
272 strcat(kfold->descript ,"\n");
273 strcat(kfold->descript ," //mode domination relationships\n");
274 strcat(kfold->descript ," quad_pol dominates low_bandwidth_single_pol;\n");
275 strcat(kfold->descript ," quad_pol dominates single_pol;\n");
276 strcat(kfold->descript ," quad_pol dominates dual_pol;\n");
277 strcat(kfold->descript ," dual_pol dominates low_bandwidth_single_pol;\n");
278 strcat(kfold->descript ," dual_pol dominates single_pol;\n");
279 strcat(kfold->descript ," single_pol dominates low_bandwidth_single_pol;\n");
280 strcat(kfold->descript ,"\n");
281 strcat(kfold->descript ," //sensor styles\n");
282 strcat(kfold->descript ," 2 0xff00ffff 0xff000000;\n");
283 strcat(kfold->descript ," 102 0x7f00ffff 0xff00ffff;\n");
284 strcat(kfold->descript ," 1002 0xffffff00 0xffffff00;\n");
285 strcat(kfold->descript ," 1102 0x7fffff00 0xffffff00;\n");
286 strcat(kfold->descript ,"\n");
287 strcat(kfold->descript ," //discipline styles\n");
288 strcat(kfold->descript ," deformation 0xff006090 0xff006090 0xff0000ff 0xff1010ff;\n");
289 strcat(kfold->descript ," vegetation 0xff00ff00 0xff00ff00 0xff0000ff 0xff0020ff;\n");
290 strcat(kfold->descript ," ice 0xffff0000 0xffff0000 0xff0000ff 0xff2000ff;\n");
291 strcat(kfold->descript ,"}");
[7654]292
293 if (edata)
294 for (i=0; i<melem; i++) {
295 if (edata[i] < cmin)
296 cmin=edata[i];
297 if (edata[i] > cmax)
298 cmax=edata[i];
299 }
300
301/* write each element as a polygon placemark */
302
[12519]303 _pprintLine_("Writing " << melem << " tria elements as KML polygons.");
[7654]304
305 for (i=0; i<melem; i++) {
306 kplace=new KML_Placemark();
307 sprintf(kplace->name ,"Element %d",(i+1));
308 kplace->visibility=1;
309 if (edata) {
310// sprintf(kplace->descript ,"Element data: %g",edata[i]);
311 sprintf(kplace->descript ,"campaign{\n deformation 1 %g quad_pol ascending right asap;\n}",edata[i]);
312 imap = (int)floor((edata[i]-cmin)/(cmax-cmin)*mcmap+0.5)+1;
313 if ((imap >= 1) && (imap <= mcmap))
314 sprintf(kplace->styleurl ,"#MatlabColor%d",imap);
315 else if (edata[i] == cmax)
316 sprintf(kplace->styleurl ,"#MatlabColor%d",mcmap);
317 else
318 sprintf(kplace->styleurl ,"#BlackLineEmptyPoly");
319 }
320 else {
321 sprintf(kplace->descript ,"");
322 sprintf(kplace->styleurl ,"#BlackLineRandomPoly");
323 }
324// kplace->DeepEcho();
325
326 kpoly=new KML_Polygon();
327 kpoly->extrude =1;
[7669]328 sprintf(kpoly->altmode ,"clampToGround");
[7654]329// kpoly->DeepEcho();
330
331 kring=new KML_LinearRing();
332 kring->ncoord =nelem+1;
[12442]333 kring->coords =xNew<double>((nelem+1)*3);
[7654]334
335/* write the nodal coordinates as a linear ring */
336
337 for (j=0; j<nelem; j++) {
[12442]338 kring->coords[3*j+0]=lng[elem[ipt]-1];
339 kring->coords[3*j+1]=lat[elem[ipt]-1];
340 kring->coords[3*j+2]=alt;
[7654]341 ipt++;
342 }
[12442]343 kring->coords[3*nelem+0]=kring->coords[3*0+0];
344 kring->coords[3*nelem+1]=kring->coords[3*0+1];
345 kring->coords[3*nelem+2]=kring->coords[3*0+2];
[7654]346// kring->DeepEcho();
347
348/* assemble the linear ring into polygon into placemark into folder */
349
[12442]350 (kpoly ->outer )->AddObject((Object*)kring);
351 (kplace->geometry)->AddObject((Object*)kpoly);
352 (kfold ->feature )->AddObject((Object*)kplace);
[7654]353
[7658]354// if (!(int)fmod((double)(i+1),1000))
[12519]355// _pprintLine_(" " << (i+1) << " tria elements written.");
[7654]356 }
[12519]357 _pprintLine_(" " << melem << " tria elements written.");
[7654]358
359 return(kfold);
360}
Note: See TracBrowser for help on using the repository browser.