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

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

cosmetics

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