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

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

CHG: greatly simplified the shared/io/Print routines. Replaced
_printf_ by _pprintString_ , then replaced all _printLine_ by _printString_
and _pprintLine_ by _pprintString_
We will then replace the _printString_ by _printf_ and _pprintString_ by _printf0_

File size: 12.1 KB
Line 
1/*!\file KMLMeshWritex
2 */
3
4#include "./KMLMeshWritex.h"
5#include "../../shared/shared.h"
6#include "../../toolkits/toolkits.h"
7
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){
9
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;
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);
28 _pprintString_("\nKMLMeshWritex Module -- " << ctime(&time0));
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();
50 kstyle->AddAttrib("id","BlackLineRandomPoly");
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();
63 kstyle->AddAttrib("id","BlackLineEmptyPoly");
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();
76 kstyle->AddAttrib("id","RedLineRedPoly");
77 kstyle->line =klsty;
78 kstyle->poly =kpsty;
79 (kdoc->style )->AddObject((Object*)kstyle);
80
81 if (cmap) {
82 _pprintString_("Writing " << mcmap << " Matlab colors as KML style templates." << "\n");
83 ipt=0;
84 for (i=0; i<mcmap; i++) {
85 klsty=new KML_LineStyle();
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));
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();
102 sprintf(cstr,"MatlabColor%d",i+1);
103 kstyle->AddAttrib("id",cstr);
104 kstyle->line =klsty;
105 kstyle->poly =kpsty;
106 (kdoc->style )->AddObject((Object*)kstyle);
107 ipt+=ncmap;
108 }
109 }
110// kdoc->DeepEcho();
111
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) */
115
116 if (!nodecon) {
117 _pprintString_("Creating the node connectivity table." << "\n");
118 nncon=mxepg+1;
119 nodecon=xNewZeroInit<int>(mncon*nncon);
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
132 _error_("Nodal connectivity table needs more than specified " << mxepg << " columns.\n");
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
143 if (data) {
144 if (mdata == melem)
145 edata=data;
146
147 else if (mdata == mncon) {
148 _pprintString_("Averaging nodal data to element data." << "\n");
149 edata=xNewZeroInit<double>(melem*ndata);
150 edfree=true;
151
152 ipt=0;
153 jpt=0;
154 for (i=0; i<melem; i++) {
155 nnodes=0;
156 for (j=0; j<nelem; j++) {
157 if (elem[jpt]) {
158 for (k=0; k<ndata; k++)
159 edata[ipt+k]+=data[(elem[jpt]-1)*ndata+k];
160 nnodes++;
161 }
162 jpt++;
163 }
164 if (nnodes)
165 for (k=0; k<ndata; k++)
166 edata[ipt+k]/=(double)nnodes;
167 ipt+=ndata;
168 }
169 }
170
171 else
172 _error_("Data matrix has incorrect number of " << mdata << " rows.\n");
173 }
174
175/* write folder for mesh */
176
177 (kdoc ->feature )->AddObject((Object*)KMLMeshElem(elem,melem,nelem,
178 nodecon,mncon,nncon,
179 lat,lng,
180 edata,
181 cmap,mcmap,ncmap));
182
183 if(edfree) xDelete<double>(edata);
184 if(ncfree) xDelete<int>(nodecon);
185 clock0a=clock();
186 time0a =time(NULL);
187 _printString_(" Constructed kml document -- " << ((double)(clock0a-clock0))/CLOCKS_PER_SEC << " CPU seconds; " << difftime(time0a,time0) << " elapsed seconds.\n\n" << "\n");
188
189/* write kml file */
190
191 _pprintString_("Writing kml document to file." << "\n");
192 fprintf(fid,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
193 fprintf(fid,"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n");
194 kdoc->Write(fid,indent);
195 fprintf(fid,"</kml>\n");
196 clock0b=clock();
197 time0b =time(NULL);
198 _printString_(" Wrote kml file -- " << ((double)(clock0b-clock0a))/CLOCKS_PER_SEC << " CPU seconds; " << difftime(time0b,time0a) << " elapsed seconds.\n\n" << "\n");
199
200 _pprintString_("Deleting kml document." << "\n");
201 delete kdoc;
202 clock0c=clock();
203 time0c =time(NULL);
204 _printString_(" Deleted kml document -- " << ((double)(clock0c-clock0b))/CLOCKS_PER_SEC << " CPU seconds; " << difftime(time0c,time0b) << " elapsed seconds.\n\n" << "\n");
205
206 clock1=clock();
207 time1 =time(NULL);
208 _printString_("KMLMeshWritex Module -- " << ((double)(clock1-clock0))/CLOCKS_PER_SEC << " CPU seconds; " << difftime(time1,time0) << " elapsed seconds.\n\n" << "\n");
209
210 return;
211}
212
213KML_Folder* KMLMeshElem(int* elem,int melem,int nelem,
214 int* nodecon,int mncon,int nncon,
215 double* lat, double* lng,
216 double* edata,
217 double* cmap, int mcmap, int ncmap){
218
219 int i,j,ipt=0;
220 double alt=0;
221 double cmin= DBL_MAX,
222 cmax=-DBL_MAX;
223 int imap;
224 KML_Folder* kfold =NULL;
225 KML_Placemark* kplace=NULL;
226 KML_Polygon* kpoly =NULL;
227 KML_LinearRing* kring =NULL;
228
229/* write folder for mesh */
230
231 kfold=new KML_Folder();
232// sprintf(kfold->name ,"Mesh");
233 sprintf(kfold->name ,"ISSM Targets");
234 kfold->visibility=1;
235// sprintf(kfold->descript ,"Elements=%d, Nodes=%d",melem,mncon);
236 sprintf(kfold->descript ,"campaign{\n");
237 strcat(kfold->descript ," evaluator ClaspTargetEvaluator;\n");
238 strcat(kfold->descript ," solver IssmSolver;\n");
239 strcat(kfold->descript ," spacecraft airplane ClaspSpacecraft(\n");
240 strcat(kfold->descript ," dutyCycleDuration=0,\n");
241 strcat(kfold->descript ," dutyCycleOnDuration=0,\n");
242 strcat(kfold->descript ," memoryInit=15000,\n");
243 strcat(kfold->descript ," memoryLimit=40000000,\n");
244 strcat(kfold->descript ," maxDataCollectionRate=27,\n");
245 strcat(kfold->descript ," maxDataDownlinkRate=10);\n");
246 strcat(kfold->descript ,"\n");
247 strcat(kfold->descript ," //sensor names\n");
248 strcat(kfold->descript ," sensor qqp_swath = 2,102,1002,1102;\n");
249 strcat(kfold->descript ,"\n");
250 strcat(kfold->descript ," //sensor ids to modes\n");
251 strcat(kfold->descript ," low_bandwidth_single_pol = 2,102,1002,1102;\n");
252 strcat(kfold->descript ," single_pol = 2,102,1002,1102;\n");
253 strcat(kfold->descript ," dual_pol = 2,102,1002,1102;\n");
254 strcat(kfold->descript ," quad_pol = 2,102,1002,1102;\n");
255 strcat(kfold->descript ,"\n");
256 strcat(kfold->descript ," //LRAD\n");
257 strcat(kfold->descript ," //Note all targets are \"ascending right\"-- i.e. mode=2\n");
258 strcat(kfold->descript ," left = 1002,1102;\n");
259 strcat(kfold->descript ," right = 2,102;\n");
260 strcat(kfold->descript ," ascending = 2,1002;\n");
261 strcat(kfold->descript ," descending = 102,1102;\n");
262 strcat(kfold->descript ,"\n");
263 strcat(kfold->descript ," //data rates\n");
264 strcat(kfold->descript ," low_bandwidth_single_pol datarate = 0.896;\n");
265 strcat(kfold->descript ," single_pol datarate = 4.214;\n");
266 strcat(kfold->descript ," dual_pol datarate = 8.428;\n");
267 strcat(kfold->descript ," quad_pol datarate = 16.856;\n");
268 strcat(kfold->descript ,"\n");
269 strcat(kfold->descript ," //mode domination relationships\n");
270 strcat(kfold->descript ," quad_pol dominates low_bandwidth_single_pol;\n");
271 strcat(kfold->descript ," quad_pol dominates single_pol;\n");
272 strcat(kfold->descript ," quad_pol dominates dual_pol;\n");
273 strcat(kfold->descript ," dual_pol dominates low_bandwidth_single_pol;\n");
274 strcat(kfold->descript ," dual_pol dominates single_pol;\n");
275 strcat(kfold->descript ," single_pol dominates low_bandwidth_single_pol;\n");
276 strcat(kfold->descript ,"\n");
277 strcat(kfold->descript ," //sensor styles\n");
278 strcat(kfold->descript ," 2 0xff00ffff 0xff000000;\n");
279 strcat(kfold->descript ," 102 0x7f00ffff 0xff00ffff;\n");
280 strcat(kfold->descript ," 1002 0xffffff00 0xffffff00;\n");
281 strcat(kfold->descript ," 1102 0x7fffff00 0xffffff00;\n");
282 strcat(kfold->descript ,"\n");
283 strcat(kfold->descript ," //discipline styles\n");
284 strcat(kfold->descript ," deformation 0xff006090 0xff006090 0xff0000ff 0xff1010ff;\n");
285 strcat(kfold->descript ," vegetation 0xff00ff00 0xff00ff00 0xff0000ff 0xff0020ff;\n");
286 strcat(kfold->descript ," ice 0xffff0000 0xffff0000 0xff0000ff 0xff2000ff;\n");
287 strcat(kfold->descript ,"}");
288
289 if (edata)
290 for (i=0; i<melem; i++) {
291 if (edata[i] < cmin)
292 cmin=edata[i];
293 if (edata[i] > cmax)
294 cmax=edata[i];
295 }
296
297/* write each element as a polygon placemark */
298
299 _pprintString_("Writing " << melem << " tria elements as KML polygons." << "\n");
300
301 for (i=0; i<melem; i++) {
302 kplace=new KML_Placemark();
303 sprintf(kplace->name ,"Element %d",(i+1));
304 kplace->visibility=1;
305 if (edata) {
306// sprintf(kplace->descript ,"Element data: %g",edata[i]);
307 sprintf(kplace->descript ,"campaign{\n deformation 1 %g quad_pol ascending right asap;\n}",edata[i]);
308 imap = (int)floor((edata[i]-cmin)/(cmax-cmin)*mcmap+0.5)+1;
309 if ((imap >= 1) && (imap <= mcmap))
310 sprintf(kplace->styleurl ,"#MatlabColor%d",imap);
311 else if (edata[i] == cmax)
312 sprintf(kplace->styleurl ,"#MatlabColor%d",mcmap);
313 else
314 sprintf(kplace->styleurl ,"#BlackLineEmptyPoly");
315 }
316 else {
317 sprintf(kplace->descript ,"");
318 sprintf(kplace->styleurl ,"#BlackLineRandomPoly");
319 }
320// kplace->DeepEcho();
321
322 kpoly=new KML_Polygon();
323 kpoly->extrude =1;
324 sprintf(kpoly->altmode ,"clampToGround");
325// kpoly->DeepEcho();
326
327 kring=new KML_LinearRing();
328 kring->ncoord =nelem+1;
329 kring->coords =xNew<double>((nelem+1)*3);
330
331/* write the nodal coordinates as a linear ring */
332
333 for (j=0; j<nelem; j++) {
334 kring->coords[3*j+0]=lng[elem[ipt]-1];
335 kring->coords[3*j+1]=lat[elem[ipt]-1];
336 kring->coords[3*j+2]=alt;
337 ipt++;
338 }
339 kring->coords[3*nelem+0]=kring->coords[3*0+0];
340 kring->coords[3*nelem+1]=kring->coords[3*0+1];
341 kring->coords[3*nelem+2]=kring->coords[3*0+2];
342// kring->DeepEcho();
343
344/* assemble the linear ring into polygon into placemark into folder */
345
346 (kpoly ->outer )->AddObject((Object*)kring);
347 (kplace->geometry)->AddObject((Object*)kpoly);
348 (kfold ->feature )->AddObject((Object*)kplace);
349
350// if (!(int)fmod((double)(i+1),1000))
351// _pprintString_(" " << (i+1) << " tria elements written." << "\n");
352 }
353 _pprintString_(" " << melem << " tria elements written." << "\n");
354
355 return(kfold);
356}
Note: See TracBrowser for help on using the repository browser.