Ejemplo n.º 1
0
int G_get_3dview(const char *fname, const char *mapset, struct G_3dview *View)
{
    struct Cell_head curwin;
    FILE *fp;
    char buffer[80], keystring[24], boo[8], nbuf[128], ebuf[128];
    int lap, v_maj, v_min, wind_keys = 0, reqkeys = 0;
    int current = 0;		/* current version flag */

    mapset = G_find_file2("3d.view", fname, mapset);
    if (mapset != NULL) {
	if (NULL == (fp = G_fopen_old("3d.view", fname, mapset))) {
	    G_warning(_("Unable to open %s for reading"), fname);
	    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 {
	G_warning(_("Unable to open %s for reading"), fname);
	return (-1);
    }

    if (current)
	return (2);

    return (1);
}
Ejemplo n.º 2
0
Archivo: Gs3.c Proyecto: caomw/grass
/*!
   \brief Save 3dview

   \param vname view name
   \param gv pointer to geoview struct
   \param gd pointer to geodisplay struct
   \param w current window
   \param defsurf default geosurf struct

   \return -1 on error
   \return ?
 */
int Gs_save_3dview(const char *vname, geoview * gv, geodisplay * gd,
		   struct Cell_head *w, geosurf * defsurf)
{
    const char *mapset;
    struct G_3dview v;
    float zmax, zmin;

    GS_get_zrange(&zmin, &zmax, 0);

    G_get_3dview_defaults(&v, w);
    mapset = G_mapset();

    if (mapset != NULL) {
	if (defsurf) {
	    if (defsurf->draw_mode & DM_WIRE_POLY) {
		v.display_type = 3;
	    }
	    else if (defsurf->draw_mode & DM_WIRE ||
		     defsurf->draw_mode & DM_COL_WIRE) {
		v.display_type = 1;
	    }
	    else if (defsurf->draw_mode & DM_POLY) {
		v.display_type = 2;
	    }

	    v.mesh_freq = defsurf->x_modw;	/* mesh resolution */
	    v.poly_freq = defsurf->x_mod;	/* poly resolution */
	    v.dozero = !(defsurf->nz_topo);
	    v.colorgrid = (defsurf->draw_mode & DM_COL_WIRE) ? 1 : 0;
	    v.shading = (defsurf->draw_mode & DM_GOURAUD) ? 1 : 0;
	}

	if (gv->infocus) {
	    GS_v3eq(v.from_to[TO], gv->real_to);
	    v.from_to[TO][Z] -= zmin;
	    GS_v3mult(v.from_to[TO], gv->scale);
	    v.from_to[TO][Z] *= gv->vert_exag;
	}
	else {
	    GS_v3eq(v.from_to[TO], gv->from_to[TO]);
	}

	gsd_model2real(v.from_to[TO]);

	GS_v3eq(v.from_to[FROM], gv->from_to[FROM]);
	gsd_model2real(v.from_to[FROM]);

	v.exag = gv->vert_exag;
	v.fov = gv->fov / 10.;
	v.twist = gv->twist;
	v.fringe = 0;		/* not implemented here */

	v.lightson = 1;		/* always true, curently */

	if (gv->lights[0].position[W] == 1) {
	    /* local */
	    v.lightpos[X] = gv->lights[0].position[X];
	    v.lightpos[Y] = gv->lights[0].position[Y];
	    v.lightpos[Z] = gv->lights[0].position[Z];
	    gsd_model2real(v.lightpos);
	    v.lightpos[W] = 1.0;	/* local */
	}
	else {
	    v.lightpos[X] = gv->lights[0].position[X];
	    v.lightpos[Y] = gv->lights[0].position[Y];
	    v.lightpos[Z] = gv->lights[0].position[Z];
	    v.lightpos[W] = 0.0;	/* inf */
	}

	v.lightcol[0] = gv->lights[0].color[0];
	v.lightcol[1] = gv->lights[0].color[1];
	v.lightcol[2] = gv->lights[0].color[2];

	v.ambient = (gv->lights[0].ambient[0] + gv->lights[0].ambient[1] +
		     gv->lights[0].ambient[2]) / 3.;
	v.shine = gv->lights[0].shine;

	v.surfonly = 0;		/* N/A - now uses constant color */
	strcpy((v.pgm_id), "Nvision-ALPHA!");

	return (G_put_3dview(vname, mapset, &v, w));
    }
    else {
	return (-1);
    }
}