1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
|
/*
** Written by Bill Brown 1992
** US Army Construction Engineering Research Lab
*/
/*
** Copyright USA CERL 1992. All rights reserved.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gis.h"
#include "glocale.h"
#define REQ_KEYS 8
static int get_bool();
static int read_old_format();
static void pr_winerr ();
static void edge_sort();
static int compare_wind();
static int vers_major = 4;
static int vers_minor = 1;
static int Suppress_warn = 0;
/* ********************************************************************
* If Suppress_warn is 0, a warning will be printed if less than 95% of
* the window when the view was saved overlaps the current window.
*/
int G_3dview_warning(int b)
{
Suppress_warn = b? 0:1;
return 0;
}
/*********************************************************************/
int G_get_3dview_defaults(
struct G_3dview *v,
struct Cell_head *w)
{
if(!v || !w)
return(-1);
v->exag = 1.0;
v->fov = 40.0;
v->from_to[0][0] = (w->east + w->west) / 2.0;
v->from_to[0][1] = w->south - (w->north - w->south);
v->from_to[0][2] = w->north - w->south;
v->from_to[1][0] = (w->east + w->west) / 2.0;
v->from_to[1][1] = (w->north + w->south) / 2.0;
v->from_to[1][2] = 0.0;
v->twist = 0.0;
v->mesh_freq = 15;
v->poly_freq = 1;
v->display_type = 2;
v->colorgrid = v->fringe = v->surfonly = v->lightson = v->doavg = 0;
v->dozero = v->shading = 1;
strcpy(v->bg_col,"black");
strcpy(v->grid_col,"white");
strcpy(v->other_col,"red");
v->ambient = v->shine = 0.3;
v->lightcol[0] = v->lightcol[1] = v->lightcol[2] = 0.8;
v->lightpos[0] = w->west;
v->lightpos[1] = w->north;
v->lightpos[2] = (w->east - w->west) / 2.0;
v->lightpos[3] = 1.0; /* local source */
v->vwin.north = w->north;
v->vwin.south = w->south;
v->vwin.east = w->east ;
v->vwin.west = w->west ;
v->vwin.format = w->format;
v->vwin.compressed = w->compressed;
v->vwin.proj = w->proj;
v->vwin.zone = w->zone;
v->vwin.ew_res = w->ew_res;
v->vwin.ns_res = w->ns_res;
v->vwin.cols = w->cols;
v->vwin.rows = w->rows;
return (1);
}
/*********************************************************************/
/* ********************************************************************
* Library routine for saving info in a 3d.view file. Requires:
*
* name of file
* name of mapset
* address of a struct G_3dview with values set.
*
* The address of a window (struct Cell_head *) may
* be passed, or if NULL is passed, the Cell_head structure inside
* the G_3dview struct will be used. e.g., if you called
* G_get_3dview_defaults with the Cell_head you want saved, the
* G_3dview returned already contains the new Cell_head. But if
* you're using all the keywords, so didn't need defaults, pass this
* routine the address of a Cell_head.
*
* These keywords are constant in all 3d.view files:
*
* PGM_ID
* cell keywords:
* north
* south
* east
* west
* rows
* cols
* required keywords:
* TO_EASTING
* TO_NORTHING
* TO_HEIGHT
* FROM_EASTING
* FROM_NORTHING
* FROM_HEIGHT
* Z_EXAG
* FIELD_VIEW
* optional keywords: (defaults provided when reading)
* TWIST
* MESH_FREQ
* POLY_RES
* DOAVG
* DISPLAY_TYPE User should call G_get_3dview_defaults
* DOZERO before filling a G_3dview struct
* COLORGRID to be written if not using all of
* SHADING the optional keywords.
* FRINGE
* BG_COL
* GRID_COL
* OTHER_COL
* LIGHTS_ON
* LIGHTPOS
* LIGHTCOL
* LIGHTAMBIENT
* SHINE
* SURFACEONLY
*
*
* Returns -1 on failure
*/
int G_put_3dview(
char *fname,
char *mapset,
struct G_3dview *View,
struct Cell_head *Win)
{
FILE *fp;
char err_buf[80];
if(NULL == (fp = G_fopen_new("3d.view",fname))){
sprintf(err_buf,_("Unable to open %s for writing"), fname);
G_warning (err_buf);
return(-1);
}
fprintf(fp,"# %01d.%02d\n", vers_major,vers_minor);
fprintf(fp,"PGM_ID: %s\n", View->pgm_id);
if(Win){
fprintf(fp,"north: %f\n",Win->north);
fprintf(fp,"south: %f\n",Win->south);
fprintf(fp,"east: %f\n",Win->east);
fprintf(fp,"west: %f\n",Win->west);
fprintf(fp,"rows: %d\n",Win->rows);
fprintf(fp,"cols: %d\n",Win->cols);
}
else{
fprintf(fp,"north: %f\n",View->vwin.north);
fprintf(fp,"south: %f\n",View->vwin.south);
fprintf(fp,"east: %f\n",View->vwin.east);
fprintf(fp,"west: %f\n",View->vwin.west);
fprintf(fp,"rows: %d\n",View->vwin.rows);
fprintf(fp,"cols: %d\n",View->vwin.cols);
}
fprintf(fp,"TO_EASTING: %f\n",View->from_to[1][0]);
fprintf(fp,"TO_NORTHING: %f\n",View->from_to[1][1]);
fprintf(fp,"TO_HEIGHT: %f\n",View->from_to[1][2]);
fprintf(fp,"FROM_EASTING: %f\n",View->from_to[0][0]);
fprintf(fp,"FROM_NORTHING: %f\n",View->from_to[0][1]);
fprintf(fp,"FROM_HEIGHT: %f\n",View->from_to[0][2]);
fprintf(fp,"Z_EXAG: %f\n",View->exag);
fprintf(fp,"TWIST: %f\n",View->twist);
fprintf(fp,"FIELD_VIEW: %f\n",View->fov);
fprintf(fp,"MESH_FREQ: %d\n",View->mesh_freq);
fprintf(fp,"POLY_RES: %d\n",View->poly_freq);
fprintf(fp,"DOAVG: %d\n",View->doavg);
fprintf(fp,"DISPLAY_TYPE: %d\n",View->display_type);
fprintf(fp,"DOZERO: %d\n",View->dozero);
fprintf(fp,"COLORGRID: %d\n",View->colorgrid); /* 1 = use color */
fprintf(fp,"SHADING: %d\n",View->shading);
fprintf(fp,"FRINGE: %d\n",View->fringe);
fprintf(fp,"BG_COL: %s\n",View->bg_col);
fprintf(fp,"GRID_COL: %s\n",View->grid_col);
fprintf(fp,"OTHER_COL: %s\n",View->other_col);
fprintf(fp,"SURFACEONLY: %d\n",View->surfonly);
fprintf(fp,"LIGHTS_ON: %d\n",View->lightson);
fprintf(fp,"LIGHTPOS: %f %f %f %f\n",View->lightpos[0],View->lightpos[1],
View->lightpos[2],View->lightpos[3]);
fprintf(fp,"LIGHTCOL: %f %f %f\n",View->lightcol[0],
View->lightcol[1],View->lightcol[2]);
fprintf(fp,"LIGHTAMBIENT: %f\n",View->ambient);
fprintf(fp,"SHINE: %f\n",View->shine);
fclose(fp);
return (1);
}
/*********************************************************************/
/* ********************************************************************
* returns -1 on failure, 0 for old (through 4.0) d.3d format file
* or if the window was not saved in the 3dview file,
* 2 if file was written using current version of this routine, otherwise 1.
* Defaults provided for non-required keywords. If reading an old format,
* the window boundaries are not checked against the current window since
* boundaries weren't saved.
*/
int G_get_3dview(
char *fname,
char *mapset,
struct G_3dview *View)
{
struct Cell_head curwin;
FILE *fp;
char err_buf[80], buffer[80], keystring[24], boo[8], nbuf[128], ebuf[128];
int current = 0; /* current version flag */
int lap, v_maj, v_min, wind_keys = 0, reqkeys = 0;
mapset = G_find_file2 ("3d.view", fname, mapset);
if(mapset != NULL)
{
if(NULL == (fp = G_fopen_old("3d.view",fname,mapset))){
sprintf(err_buf,_("Unable to open %s for reading"), fname);
G_warning (err_buf);
return(-1);
}
G_get_set_window (&curwin);
G_get_3dview_defaults(View, &curwin);
if(NULL != fgets(buffer, 80, fp)){
if(buffer[0] != '#'){ /* old d.3d format */
rewind(fp);
if(0 <= read_old_format(View, fp))
return(0);
else
return(-1);
}
else{
sscanf(buffer,"#%d.%d\n", &v_maj,&v_min);
if(v_maj == vers_major && v_min == vers_minor)
current = 1; /* same version */
}
}
while(NULL != fgets(buffer, 75, fp)){
if(buffer[0] != '#'){
sscanf(buffer,"%[^:]:", keystring);
if(!strcmp(keystring,"PGM_ID")){
sscanf(buffer,"%*s%s", (View->pgm_id));
continue;
}
if(!strcmp(keystring,"north")){
sscanf(buffer,"%*s%lf", &(View->vwin.north));
++wind_keys;
continue;
}
if(!strcmp(keystring,"south")){
sscanf(buffer,"%*s%lf", &(View->vwin.south));
++wind_keys;
continue;
}
if(!strcmp(keystring,"east")){
sscanf(buffer,"%*s%lf", &(View->vwin.east));
++wind_keys;
continue;
}
if(!strcmp(keystring,"west")){
sscanf(buffer,"%*s%lf", &(View->vwin.west));
++wind_keys;
continue;
}
if(!strcmp(keystring,"rows")){
sscanf(buffer,"%*s%d", &(View->vwin.rows));
++wind_keys;
continue;
}
if(!strcmp(keystring,"cols")){
sscanf(buffer,"%*s%d", &(View->vwin.cols));
++wind_keys;
continue;
}
if(!strcmp(keystring,"TO_EASTING")){
sscanf(buffer,"%*s%f", &(View->from_to[1][0]));
++reqkeys;
continue;
}
if(!strcmp(keystring,"TO_NORTHING")){
sscanf(buffer,"%*s%f", &(View->from_to[1][1]));
++reqkeys;
continue;
}
if(!strcmp(keystring,"TO_HEIGHT")){
sscanf(buffer,"%*s%f", &(View->from_to[1][2]));
++reqkeys;
continue;
}
if(!strcmp(keystring,"FROM_EASTING")){
sscanf(buffer,"%*s%f", &(View->from_to[0][0]));
++reqkeys;
continue;
}
if(!strcmp(keystring,"FROM_NORTHING")){
sscanf(buffer,"%*s%f", &(View->from_to[0][1]));
++reqkeys;
continue;
}
if(!strcmp(keystring,"FROM_HEIGHT")){
sscanf(buffer,"%*s%f", &(View->from_to[0][2]));
++reqkeys;
continue;
}
if(!strcmp(keystring,"Z_EXAG")){
sscanf(buffer,"%*s%f", &(View->exag));
++reqkeys;
continue;
}
if(!strcmp(keystring,"MESH_FREQ")){
sscanf(buffer,"%*s%d", &(View->mesh_freq));
continue;
}
if(!strcmp(keystring,"POLY_RES")){
sscanf(buffer,"%*s%d", &(View->poly_freq));
continue;
}
if(!strcmp(keystring,"DOAVG")){
sscanf(buffer,"%*s%d", &(View->doavg));
continue;
}
if(!strcmp(keystring,"FIELD_VIEW")){
sscanf(buffer,"%*s%f", &(View->fov));
++reqkeys;
continue;
}
if(!strcmp(keystring,"TWIST")){
sscanf(buffer,"%*s%f", &(View->twist));
continue;
}
if(!strcmp(keystring,"DISPLAY_TYPE")){
sscanf(buffer,"%*s%d", &View->display_type);
continue;
}
if(!strcmp(keystring,"DOZERO")){
sscanf(buffer,"%*s%s", boo);
View->dozero = get_bool(boo);
continue;
}
if(!strcmp(keystring,"COLORGRID")){
sscanf(buffer,"%*s%s", boo);
View->colorgrid = get_bool(boo);
continue;
}
if(!strcmp(keystring,"FRINGE")){
sscanf(buffer,"%*s%s", boo);
View->fringe = get_bool(boo);
continue;
}
if(!strcmp(keystring,"SHADING")){
sscanf(buffer,"%*s%s", boo);
View->shading = get_bool(boo);
continue;
}
if(!strcmp(keystring,"BG_COL")){
sscanf(buffer,"%*s%s", View->bg_col);
continue;
}
if(!strcmp(keystring,"GRID_COL")){
sscanf(buffer,"%*s%s", View->grid_col);
continue;
}
if(!strcmp(keystring,"OTHER_COL")){
sscanf(buffer,"%*s%s", View->other_col);
continue;
}
if(!strcmp(keystring,"SURFACEONLY")){
sscanf(buffer,"%*s%s", boo);
View->surfonly = get_bool(boo);
continue;
}
if(!strcmp(keystring,"LIGHTS_ON")){
sscanf(buffer,"%*s%s", boo);
View->lightson = get_bool(boo);
continue;
}
if(!strcmp(keystring,"LIGHTPOS")){
sscanf(buffer,"%*s%f%f%f%f", &(View->lightpos[0]),
&(View->lightpos[1]), &(View->lightpos[2]),
&(View->lightpos[3]));
continue;
}
if(!strcmp(keystring,"LIGHTCOL")){
sscanf(buffer,"%*s%f%f%f", &(View->lightcol[0]),
&(View->lightcol[1]), &(View->lightcol[2]));
continue;
}
if(!strcmp(keystring,"LIGHTAMBIENT")){
sscanf(buffer,"%*s%f", &(View->ambient));
continue;
}
if(!strcmp(keystring,"SHINE")){
sscanf(buffer,"%*s%f", &(View->shine));
continue;
}
}
}
fclose(fp);
if(reqkeys != REQ_KEYS) /* required keys not found */
return (-1);
/* fill rest of View->vwin */
if(wind_keys == 6){
View->vwin.ew_res = (View->vwin.east - View->vwin.west) /
View->vwin.cols;
View->vwin.ns_res = (View->vwin.north - View->vwin.south) /
View->vwin.rows;
}
else
return(0); /* older format */
if(!Suppress_warn){
if(95 > (lap = compare_wind(&(View->vwin),&curwin))){
fprintf(stderr,_("GRASS window when view was saved:\n"));
G_format_northing(View->vwin.north, nbuf, G_projection()) ;
fprintf(stderr,"north: %s\n", nbuf);
G_format_northing(View->vwin.south, nbuf, G_projection()) ;
fprintf(stderr,"south: %s\n", nbuf);
G_format_easting (View->vwin.east, ebuf, G_projection()) ;
fprintf(stderr,"east: %s\n", ebuf);
G_format_easting (View->vwin.west, ebuf, G_projection()) ;
fprintf(stderr,"west: %s\n", ebuf);
pr_winerr(lap,fname);
}
}
}
else{
sprintf(err_buf,_("Unable to open %s for reading"), fname);
G_warning (err_buf);
return(-1);
}
if(current) return(2);
return(1);
}
/*********************************************************************/
/* *********************************************************************
* returns the percentage of savedwin that overlaps curwin
*/
static int compare_wind(
struct Cell_head *savedwin,
struct Cell_head *curwin)
{
float e_ings[4], n_ings[4], area_lap, area_saved;
int outside = 0;
if(savedwin->north < curwin->south) outside = 1;
if(savedwin->south > curwin->north) outside = 1;
if(savedwin->east < curwin->west) outside = 1;
if(savedwin->west > curwin->east) outside = 1;
if(outside) return (0);
e_ings[0] = savedwin->west;
e_ings[1] = savedwin->east;
e_ings[2] = curwin->west;
e_ings[3] = curwin->east;
edge_sort(e_ings);
n_ings[0] = savedwin->south;
n_ings[1] = savedwin->north;
n_ings[2] = curwin->south;
n_ings[3] = curwin->north;
edge_sort(n_ings);
area_lap = (e_ings[2] - e_ings[1]) * (n_ings[2] - n_ings[1]);
area_saved = (savedwin->east - savedwin->west) *
(savedwin->north - savedwin->south);
return ((int)(area_lap * 100.0 / area_saved));
}
/*********************************************************************/
static int get_bool( char *str)
{
if (str[0] == 'y' || str[0] == 'Y')
return (1);
if (str[0] == 'n' || str[0] == 'N')
return (0);
return (atoi(str) ? 1 : 0);
}
/*********************************************************************/
static void pr_winerr (
int vis, /* % of saved window overlapping current window */
char *viewname)
{
char err_buf[80];
switch(vis)
{
case 0:
sprintf(err_buf,_(" Window saved in \"%s\" is completely outside of current GRASS window."), viewname);
G_warning (err_buf);
break;
default:
sprintf(err_buf,_(" Only %d%% of window saved in \"%s\" overlaps with current GRASS window."), vis, viewname);
G_warning (err_buf);
break;
}
}
/*********************************************************************/
/* sorts 4 floats from lowest to highest */
static void edge_sort (float sides[4])
{
int i,j;
float temp;
for (i = 0; i < 4; ++i){
for (j = i + 1; j < 4; ++j){
if (sides[j] < sides[i]){ /* then swap */
temp = sides[i];
sides[i] = sides[j];
sides[j] = temp;
}
}
}
}
/*********************************************************************/
static int read_old_format(
struct G_3dview *v,
FILE *fp)
{
char buffer[80];
int req_keys = 0;
double td;
char boo[8];
strcpy((v->pgm_id), "d.3d");
if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[1][0])) )
++req_keys;
if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[1][1])) )
++req_keys;
if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[1][2])) )
++req_keys;
if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[0][0])) )
++req_keys;
if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[0][1])) )
++req_keys;
if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->from_to[0][2])) )
++req_keys;
if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->exag)) )
++req_keys;
sscanf(fgets(buffer, 80, fp),"%d",&(v->mesh_freq)) ;
if(1 == sscanf(fgets(buffer, 80, fp),"%f",&(v->fov)) )
++req_keys;
if(1 == sscanf(fgets(buffer, 80, fp),"%lf",&td) ){ /* resolution */
v->vwin.rows = (v->vwin.north - v->vwin.south) / td ;
v->vwin.cols = (v->vwin.east - v->vwin.west) / td ;
v->vwin.ew_res = v->vwin.ns_res = td;
}
sscanf(fgets(buffer, 80, fp),"%s",boo) ; /* linesonly */
v->display_type = get_bool(boo)? 1 : 3;
sscanf(fgets(buffer, 80, fp),"%s",boo) ;
v->dozero = get_bool(boo);
sscanf(fgets(buffer, 80, fp),"%s",v->grid_col) ;
if (! strcmp(v->grid_col, "color"))
v->colorgrid = 1;
sscanf(fgets(buffer, 80, fp),"%s",v->other_col) ;
sscanf(fgets(buffer, 80, fp),"%s",v->bg_col) ;
sscanf(fgets(buffer, 80, fp),"%s",boo) ;
v->doavg = get_bool(boo);
if(v->exag){ /* old 3d.view files saved height with no exag */
v->from_to[0][2] /= v->exag;
v->from_to[1][2] /= v->exag;
}
fclose(fp) ;
if(req_keys == REQ_KEYS)
return (1);
else
return (-1);
}
/*********************************************************************/
|