00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <stdlib.h>
00014 #include <grass/gis.h>
00015 #include <grass/glocale.h>
00016
00017 #define REQ_KEYS 8
00018
00019 static int get_bool();
00020 static int read_old_format();
00021 static void pr_winerr ();
00022 static void edge_sort();
00023 static int compare_wind();
00024
00025 static int vers_major = 4;
00026 static int vers_minor = 1;
00027 static int Suppress_warn = 0;
00028
00029
00030
00031
00032
00033
00034 int G_3dview_warning(int b)
00035 {
00036 Suppress_warn = b? 0:1;
00037
00038 return 0;
00039 }
00040
00041
00042
00043
00044 int G_get_3dview_defaults(
00045 struct G_3dview *v,
00046 struct Cell_head *w)
00047 {
00048 if(!v || !w)
00049 return(-1);
00050
00051 v->exag = 1.0;
00052 v->fov = 40.0;
00053 v->from_to[0][0] = (w->east + w->west) / 2.0;
00054 v->from_to[0][1] = w->south - (w->north - w->south);
00055 v->from_to[0][2] = w->north - w->south;
00056 v->from_to[1][0] = (w->east + w->west) / 2.0;
00057 v->from_to[1][1] = (w->north + w->south) / 2.0;
00058 v->from_to[1][2] = 0.0;
00059
00060 v->twist = 0.0;
00061 v->mesh_freq = 15;
00062 v->poly_freq = 1;
00063 v->display_type = 2;
00064 v->colorgrid = v->fringe = v->surfonly = v->lightson = v->doavg = 0;
00065 v->dozero = v->shading = 1;
00066 strcpy(v->bg_col,"black");
00067 strcpy(v->grid_col,"white");
00068 strcpy(v->other_col,"red");
00069 v->ambient = v->shine = 0.3;
00070 v->lightcol[0] = v->lightcol[1] = v->lightcol[2] = 0.8;
00071 v->lightpos[0] = w->west;
00072 v->lightpos[1] = w->north;
00073 v->lightpos[2] = (w->east - w->west) / 2.0;
00074 v->lightpos[3] = 1.0;
00075
00076 v->vwin.north = w->north;
00077 v->vwin.south = w->south;
00078 v->vwin.east = w->east ;
00079 v->vwin.west = w->west ;
00080 v->vwin.format = w->format;
00081 v->vwin.compressed = w->compressed;
00082 v->vwin.proj = w->proj;
00083 v->vwin.zone = w->zone;
00084 v->vwin.ew_res = w->ew_res;
00085 v->vwin.ns_res = w->ns_res;
00086 v->vwin.cols = w->cols;
00087 v->vwin.rows = w->rows;
00088
00089 return (1);
00090
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 int G_put_3dview(
00155 char *fname,
00156 char *mapset,
00157 struct G_3dview *View,
00158 struct Cell_head *Win)
00159 {
00160 FILE *fp;
00161 char err_buf[80];
00162
00163 if(NULL == (fp = G_fopen_new("3d.view",fname))){
00164 sprintf(err_buf,_("Unable to open %s for writing"), fname);
00165 G_warning (err_buf);
00166 return(-1);
00167 }
00168
00169 fprintf(fp,"# %01d.%02d\n", vers_major,vers_minor);
00170 fprintf(fp,"PGM_ID: %s\n", View->pgm_id);
00171
00172 if(Win){
00173 fprintf(fp,"north: %f\n",Win->north);
00174 fprintf(fp,"south: %f\n",Win->south);
00175 fprintf(fp,"east: %f\n",Win->east);
00176 fprintf(fp,"west: %f\n",Win->west);
00177 fprintf(fp,"rows: %d\n",Win->rows);
00178 fprintf(fp,"cols: %d\n",Win->cols);
00179 }
00180 else{
00181 fprintf(fp,"north: %f\n",View->vwin.north);
00182 fprintf(fp,"south: %f\n",View->vwin.south);
00183 fprintf(fp,"east: %f\n",View->vwin.east);
00184 fprintf(fp,"west: %f\n",View->vwin.west);
00185 fprintf(fp,"rows: %d\n",View->vwin.rows);
00186 fprintf(fp,"cols: %d\n",View->vwin.cols);
00187 }
00188
00189 fprintf(fp,"TO_EASTING: %f\n",View->from_to[1][0]);
00190 fprintf(fp,"TO_NORTHING: %f\n",View->from_to[1][1]);
00191 fprintf(fp,"TO_HEIGHT: %f\n",View->from_to[1][2]);
00192 fprintf(fp,"FROM_EASTING: %f\n",View->from_to[0][0]);
00193 fprintf(fp,"FROM_NORTHING: %f\n",View->from_to[0][1]);
00194 fprintf(fp,"FROM_HEIGHT: %f\n",View->from_to[0][2]);
00195 fprintf(fp,"Z_EXAG: %f\n",View->exag);
00196 fprintf(fp,"TWIST: %f\n",View->twist);
00197 fprintf(fp,"FIELD_VIEW: %f\n",View->fov);
00198 fprintf(fp,"MESH_FREQ: %d\n",View->mesh_freq);
00199 fprintf(fp,"POLY_RES: %d\n",View->poly_freq);
00200 fprintf(fp,"DOAVG: %d\n",View->doavg);
00201 fprintf(fp,"DISPLAY_TYPE: %d\n",View->display_type);
00202 fprintf(fp,"DOZERO: %d\n",View->dozero);
00203
00204 fprintf(fp,"COLORGRID: %d\n",View->colorgrid);
00205 fprintf(fp,"SHADING: %d\n",View->shading);
00206 fprintf(fp,"FRINGE: %d\n",View->fringe);
00207 fprintf(fp,"BG_COL: %s\n",View->bg_col);
00208 fprintf(fp,"GRID_COL: %s\n",View->grid_col);
00209 fprintf(fp,"OTHER_COL: %s\n",View->other_col);
00210 fprintf(fp,"SURFACEONLY: %d\n",View->surfonly);
00211 fprintf(fp,"LIGHTS_ON: %d\n",View->lightson);
00212 fprintf(fp,"LIGHTPOS: %f %f %f %f\n",View->lightpos[0],View->lightpos[1],
00213 View->lightpos[2],View->lightpos[3]);
00214 fprintf(fp,"LIGHTCOL: %f %f %f\n",View->lightcol[0],
00215 View->lightcol[1],View->lightcol[2]);
00216 fprintf(fp,"LIGHTAMBIENT: %f\n",View->ambient);
00217 fprintf(fp,"SHINE: %f\n",View->shine);
00218
00219 fclose(fp);
00220 return (1);
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 int G_get_3dview(
00237 char *fname,
00238 char *mapset,
00239 struct G_3dview *View)
00240 {
00241 struct Cell_head curwin;
00242 FILE *fp;
00243 char err_buf[80], buffer[80], keystring[24], boo[8], nbuf[128], ebuf[128];
00244 int current = 0;
00245 int lap, v_maj, v_min, wind_keys = 0, reqkeys = 0;
00246
00247
00248 mapset = G_find_file2 ("3d.view", fname, mapset);
00249 if(mapset != NULL)
00250 {
00251
00252 if(NULL == (fp = G_fopen_old("3d.view",fname,mapset))){
00253 sprintf(err_buf,_("Unable to open %s for reading"), fname);
00254 G_warning (err_buf);
00255 return(-1);
00256 }
00257
00258 G_get_set_window (&curwin);
00259 G_get_3dview_defaults(View, &curwin);
00260
00261 if(NULL != fgets(buffer, 80, fp)){
00262 if(buffer[0] != '#'){
00263 rewind(fp);
00264 if(0 <= read_old_format(View, fp))
00265 return(0);
00266 else
00267 return(-1);
00268 }
00269 else{
00270 sscanf(buffer,"#%d.%d\n", &v_maj,&v_min);
00271 if(v_maj == vers_major && v_min == vers_minor)
00272 current = 1;
00273 }
00274 }
00275
00276 while(NULL != fgets(buffer, 75, fp)){
00277 if(buffer[0] != '#'){
00278
00279 sscanf(buffer,"%[^:]:", keystring);
00280
00281 if(!strcmp(keystring,"PGM_ID")){
00282 sscanf(buffer,"%*s%s", (View->pgm_id));
00283 continue;
00284 }
00285 if(!strcmp(keystring,"north")){
00286 sscanf(buffer,"%*s%lf", &(View->vwin.north));
00287 ++wind_keys;
00288 continue;
00289 }
00290 if(!strcmp(keystring,"south")){
00291 sscanf(buffer,"%*s%lf", &(View->vwin.south));
00292 ++wind_keys;
00293 continue;
00294 }
00295 if(!strcmp(keystring,"east")){
00296 sscanf(buffer,"%*s%lf", &(View->vwin.east));
00297 ++wind_keys;
00298 continue;
00299 }
00300 if(!strcmp(keystring,"west")){
00301 sscanf(buffer,"%*s%lf", &(View->vwin.west));
00302 ++wind_keys;
00303 continue;
00304 }
00305 if(!strcmp(keystring,"rows")){
00306 sscanf(buffer,"%*s%d", &(View->vwin.rows));
00307 ++wind_keys;
00308 continue;
00309 }
00310 if(!strcmp(keystring,"cols")){
00311 sscanf(buffer,"%*s%d", &(View->vwin.cols));
00312 ++wind_keys;
00313 continue;
00314 }
00315 if(!strcmp(keystring,"TO_EASTING")){
00316 sscanf(buffer,"%*s%f", &(View->from_to[1][0]));
00317 ++reqkeys;
00318 continue;
00319 }
00320 if(!strcmp(keystring,"TO_NORTHING")){
00321 sscanf(buffer,"%*s%f", &(View->from_to[1][1]));
00322 ++reqkeys;
00323 continue;
00324 }
00325 if(!strcmp(keystring,"TO_HEIGHT")){
00326 sscanf(buffer,"%*s%f", &(View->from_to[1][2]));
00327 ++reqkeys;
00328 continue;
00329 }
00330 if(!strcmp(keystring,"FROM_EASTING")){
00331 sscanf(buffer,"%*s%f", &(View->from_to[0][0]));
00332 ++reqkeys;
00333 continue;
00334 }
00335 if(!strcmp(keystring,"FROM_NORTHING")){
00336 sscanf(buffer,"%*s%f", &(View->from_to[0][1]));
00337 ++reqkeys;
00338 continue;
00339 }
00340 if(!strcmp(keystring,"FROM_HEIGHT")){
00341 sscanf(buffer,"%*s%f", &(View->from_to[0][2]));
00342 ++reqkeys;
00343 continue;
00344 }
00345 if(!strcmp(keystring,"Z_EXAG")){
00346 sscanf(buffer,"%*s%f", &(View->exag));
00347 ++reqkeys;
00348 continue;
00349 }
00350 if(!strcmp(keystring,"MESH_FREQ")){
00351 sscanf(buffer,"%*s%d", &(View->mesh_freq));
00352 continue;
00353 }
00354 if(!strcmp(keystring,"POLY_RES")){
00355 sscanf(buffer,"%*s%d", &(View->poly_freq));
00356 continue;
00357 }
00358 if(!strcmp(keystring,"DOAVG")){
00359 sscanf(buffer,"%*s%d", &(View->doavg));
00360 continue;
00361 }
00362 if(!strcmp(keystring,"FIELD_VIEW")){
00363 sscanf(buffer,"%*s%f", &(View->fov));
00364 ++reqkeys;
00365 continue;
00366 }
00367 if(!strcmp(keystring,"TWIST")){
00368 sscanf(buffer,"%*s%f", &(View->twist));
00369 continue;
00370 }
00371 if(!strcmp(keystring,"DISPLAY_TYPE")){
00372 sscanf(buffer,"%*s%d", &View->display_type);
00373 continue;
00374 }
00375 if(!strcmp(keystring,"DOZERO")){
00376 sscanf(buffer,"%*s%s", boo);
00377 View->dozero = get_bool(boo);
00378 continue;
00379 }
00380 if(!strcmp(keystring,"COLORGRID")){
00381 sscanf(buffer,"%*s%s", boo);
00382 View->colorgrid = get_bool(boo);
00383 continue;
00384 }
00385 if(!strcmp(keystring,"FRINGE")){
00386 sscanf(buffer,"%*s%s", boo);
00387 View->fringe = get_bool(boo);
00388 continue;
00389 }
00390 if(!strcmp(keystring,"SHADING")){
00391 sscanf(buffer,"%*s%s", boo);
00392 View->shading = get_bool(boo);
00393 continue;
00394 }
00395 if(!strcmp(keystring,"BG_COL")){
00396 sscanf(buffer,"%*s%s", View->bg_col);
00397 continue;
00398 }
00399 if(!strcmp(keystring,"GRID_COL")){
00400 sscanf(buffer,"%*s%s", View->grid_col);
00401 continue;
00402 }
00403 if(!strcmp(keystring,"OTHER_COL")){
00404 sscanf(buffer,"%*s%s", View->other_col);
00405 continue;
00406 }
00407 if(!strcmp(keystring,"SURFACEONLY")){
00408 sscanf(buffer,"%*s%s", boo);
00409 View->surfonly = get_bool(boo);
00410 continue;
00411 }
00412 if(!strcmp(keystring,"LIGHTS_ON")){
00413 sscanf(buffer,"%*s%s", boo);
00414 View->lightson = get_bool(boo);
00415 continue;
00416 }
00417 if(!strcmp(keystring,"LIGHTPOS")){
00418 sscanf(buffer,"%*s%f%f%f%f", &(View->lightpos[0]),
00419 &(View->lightpos[1]), &(View->lightpos[2]),
00420 &(View->lightpos[3]));
00421 continue;
00422 }
00423 if(!strcmp(keystring,"LIGHTCOL")){
00424 sscanf(buffer,"%*s%f%f%f", &(View->lightcol[0]),
00425 &(View->lightcol[1]), &(View->lightcol[2]));
00426 continue;
00427 }
00428 if(!strcmp(keystring,"LIGHTAMBIENT")){
00429 sscanf(buffer,"%*s%f", &(View->ambient));
00430 continue;
00431 }
00432 if(!strcmp(keystring,"SHINE")){
00433 sscanf(buffer,"%*s%f", &(View->shine));
00434 continue;
00435 }
00436 }
00437 }
00438
00439 fclose(fp);
00440
00441 if(reqkeys != REQ_KEYS)
00442 return (-1);
00443
00444
00445 if(wind_keys == 6){
00446 View->vwin.ew_res = (View->vwin.east - View->vwin.west) /
00447 View->vwin.cols;
00448 View->vwin.ns_res = (View->vwin.north - View->vwin.south) /
00449 View->vwin.rows;
00450 }
00451 else
00452 return(0);
00453
00454
00455 if(!Suppress_warn){
00456 if(95 > (lap = compare_wind(&(View->vwin),&curwin))){
00457
00458 fprintf(stderr,_("GRASS window when view was saved:\n"));
00459 G_format_northing(View->vwin.north, nbuf, G_projection()) ;
00460 fprintf(stderr,"north: %s\n", nbuf);
00461 G_format_northing(View->vwin.south, nbuf, G_projection()) ;
00462 fprintf(stderr,"south: %s\n", nbuf);
00463 G_format_easting (View->vwin.east, ebuf, G_projection()) ;
00464 fprintf(stderr,"east: %s\n", ebuf);
00465 G_format_easting (View->vwin.west, ebuf, G_projection()) ;
00466 fprintf(stderr,"west: %s\n", ebuf);
00467 pr_winerr(lap,fname);
00468 }
00469 }
00470 }
00471 else{
00472 sprintf(err_buf,_("Unable to open %s for reading"), fname);
00473 G_warning (err_buf);
00474 return(-1);
00475 }
00476 if(current) return(2);
00477 return(1);
00478 }
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488 static int compare_wind(
00489 struct Cell_head *savedwin,
00490 struct Cell_head *curwin)
00491 {
00492 float e_ings[4], n_ings[4], area_lap, area_saved;
00493 int outside = 0;
00494
00495 if(savedwin->north < curwin->south) outside = 1;
00496 if(savedwin->south > curwin->north) outside = 1;
00497 if(savedwin->east < curwin->west) outside = 1;
00498 if(savedwin->west > curwin->east) outside = 1;
00499 if(outside) return (0);
00500
00501 e_ings[0] = savedwin->west;
00502 e_ings[1] = savedwin->east;
00503 e_ings[2] = curwin->west;
00504 e_ings[3] = curwin->east;
00505 edge_sort(e_ings);
00506
00507 n_ings[0] = savedwin->south;
00508 n_ings[1] = savedwin->north;
00509 n_ings[2] = curwin->south;
00510 n_ings[3] = curwin->north;
00511 edge_sort(n_ings);
00512
00513 area_lap = (e_ings[2] - e_ings[1]) * (n_ings[2] - n_ings[1]);
00514 area_saved = (savedwin->east - savedwin->west) *
00515 (savedwin->north - savedwin->south);
00516
00517 return ((int)(area_lap * 100.0 / area_saved));
00518 }
00519
00520
00521
00522
00523 static int get_bool( char *str)
00524 {
00525 if (str[0] == 'y' || str[0] == 'Y')
00526 return (1);
00527 if (str[0] == 'n' || str[0] == 'N')
00528 return (0);
00529 return (atoi(str) ? 1 : 0);
00530 }
00531
00532
00533
00534
00535 static void pr_winerr (
00536 int vis,
00537 char *viewname)
00538 {
00539 char err_buf[80];
00540
00541 switch(vis)
00542 {
00543 case 0:
00544 sprintf(err_buf,_(" Window saved in \"%s\" is completely outside of current GRASS window."), viewname);
00545 G_warning (err_buf);
00546 break;
00547 default:
00548 sprintf(err_buf,_(" Only %d%% of window saved in \"%s\" overlaps with current GRASS window."), vis, viewname);
00549 G_warning (err_buf);
00550 break;
00551
00552 }
00553 }
00554
00555
00556
00557
00558 static void edge_sort (float sides[4])
00559 {
00560 int i,j;
00561 float temp;
00562
00563 for (i = 0; i < 4; ++i){
00564 for (j = i + 1; j < 4; ++j){
00565 if (sides[j] < sides[i]){
00566 temp = sides[i];
00567 sides[i] = sides[j];
00568 sides[j] = temp;
00569 }
00570 }
00571 }
00572 }
00573
00574
00575
00576
00577 static int read_old_format(
00578 struct G_3dview *v,
00579 FILE *fp)
00580 {
00581 char buffer[80];
00582 int req_keys = 0;
00583 double td;
00584 char boo[8];
00585
00586
00587 strcpy((v->pgm_id), "d.3d");
00588 if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[1][0])) )
00589 ++req_keys;
00590 if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[1][1])) )
00591 ++req_keys;
00592 if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[1][2])) )
00593 ++req_keys;
00594 if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[0][0])) )
00595 ++req_keys;
00596 if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[0][1])) )
00597 ++req_keys;
00598 if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[0][2])) )
00599 ++req_keys;
00600 if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->exag)) )
00601 ++req_keys;
00602 sscanf(fgets(buffer, 80, fp),"%d",&(v->mesh_freq)) ;
00603 if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->fov)) )
00604 ++req_keys;
00605 if(1 == sscanf(fgets(buffer, 80, fp),"%lf",&td) ){
00606 v->vwin.rows = (v->vwin.north - v->vwin.south) / td ;
00607 v->vwin.cols = (v->vwin.east - v->vwin.west) / td ;
00608 v->vwin.ew_res = v->vwin.ns_res = td;
00609 }
00610
00611 sscanf(fgets(buffer, 80, fp),"%s",boo) ;
00612 v->display_type = get_bool(boo)? 1 : 3;
00613 sscanf(fgets(buffer, 80, fp),"%s",boo) ;
00614 v->dozero = get_bool(boo);
00615 sscanf(fgets(buffer, 80, fp),"%s",v->grid_col) ;
00616 if (! strcmp(v->grid_col, "color"))
00617 v->colorgrid = 1;
00618
00619 sscanf(fgets(buffer, 80, fp),"%s",v->other_col) ;
00620 sscanf(fgets(buffer, 80, fp),"%s",v->bg_col) ;
00621 sscanf(fgets(buffer, 80, fp),"%s",boo) ;
00622 v->doavg = get_bool(boo);
00623
00624 if(v->exag){
00625 v->from_to[0][2] /= v->exag;
00626 v->from_to[1][2] /= v->exag;
00627 }
00628
00629
00630 fclose(fp) ;
00631 if(req_keys == REQ_KEYS)
00632 return (1);
00633 else
00634 return (-1);
00635 }
00636
00637
00638
00639
00640