/*! \brief Set vector lines mode \param params parameters \return 1 on success \return 0 on failure */ int vlines_set_attrb(const struct GParams *params) { int i, color, width, flat, height; int *vect_list, nvects; vect_list = GV_get_vect_list(&nvects); for (i = 0; i < nvects; i++) { /* mode -- use memory by default */ color = Nviz_color_from_str(params->vline_color->answers[i]); width = atoi(params->vline_width->answers[i]); if (strcmp(params->vline_mode->answers[i], "flat") == 0) flat = 1; else flat = 0; if (GV_set_vectmode(vect_list[i], 1, color, width, flat) < 0) return 0; /* height */ height = atoi(params->vline_height->answers[i]); if (height > 0) GV_set_trans(vect_list[i], 0.0, 0.0, height); } return 1; }
/*! \brief Set vector points style \param params parameters \return 1 on success \return 0 on failure */ int vpoints_set_attrb(const struct GParams *params) { int i, layer, have_colors, with_z; int *site_list, nsites; int marker, color, width; float size; char *marker_str, *color_column, *size_column, *width_column, *marker_column; struct Colors colors; site_list = GP_get_site_list(&nsites); for (i = 0; i < nsites; i++) { check_map(params, i, FALSE, &layer, &with_z); color = Nviz_color_from_str(params->vpoint_color->answers[i]); color_column = params->vpoint_color_column->answers ? params->vpoint_color_column->answers[i] : NULL; size = atof(params->vpoint_size->answers[i]); size_column = params->vpoint_size_column->answers ? params->vpoint_size_column->answers[i] : NULL; width = atoi(params->vpoint_width->answers[i]); width_column = params->vpoint_width_column->answers ? params->vpoint_width_column->answers[i] : NULL; marker_str = params->vpoint_marker->answers[i]; marker_column = params->vpoint_marker_column->answers ? params->vpoint_marker_column->answers[i] : NULL; marker = GP_str_to_marker(marker_str); if (with_z) { if (strcmp(params->vpoint_mode->answers[i], "surface") == 0) GP_set_zmode(site_list[i], FALSE); else GP_set_zmode(site_list[i], TRUE); } if (GP_set_style(site_list[i], color, width, size, marker) < 0) return 0; /* check for vector color table */ have_colors = Vect_read_colors(params->vpoints->answers[i], "", &colors); if (have_colors || color_column || width_column || size_column || marker_column) { if (GP_set_style_thematic(site_list[i], layer, color_column, width_column, size_column, marker_column, have_colors ? &colors : NULL) < 0) return 0; } } return 1; }
/*! \brief Set vector lines mode \param params parameters \return 1 on success \return 0 on failure */ int vlines_set_attrb(const struct GParams *params) { int i, layer, color, width, flat, height; int *vect_list, nvects; int have_colors; char *color_column, *width_column; struct Colors colors; vect_list = GV_get_vect_list(&nvects); for (i = 0; i < nvects; i++) { check_map(params, i, TRUE, &layer, NULL); color = Nviz_color_from_str(params->vline_color->answers[i]); color_column = params->vline_color_column->answers ? params->vline_color_column->answers[i] : NULL; width = atoi(params->vline_width->answers[i]); width_column = params->vline_width_column->answers ? params->vline_width_column->answers[i] : NULL; if (strcmp(params->vline_mode->answers[i], "flat") == 0) flat = 1; else flat = 0; /* style (mode -- use memory by default) */ if (GV_set_style(vect_list[i], TRUE, color, width, flat) < 0) return 0; /* check for vector color table */ have_colors = Vect_read_colors(params->vlines->answers[i], "", &colors); if (have_colors || color_column || width_column) if (GV_set_style_thematic(vect_list[i], layer, color_column, width_column, have_colors ? &colors : NULL) < 0) return 0; /* height */ height = atoi(params->vline_height->answers[i]); if (height > 0) GV_set_trans(vect_list[i], 0.0, 0.0, height); } return 1; }
/*! \brief Set vector points mode \param params parameters \return 1 on success \return 0 on failure */ int vpoints_set_attrb(const struct GParams *params) { int i; int *site_list, nsites; int marker, color, width; float size; char *marker_str; site_list = GP_get_site_list(&nsites); for (i = 0; i < nsites; i++) { color = Nviz_color_from_str(params->vpoint_color->answers[i]); size = atof(params->vpoint_size->answers[i]); width = atoi(params->vpoint_width->answers[i]); marker_str = params->vpoint_marker->answers[i]; if (strcmp(marker_str, "x") == 0) marker = ST_X; else if (strcmp(marker_str, "sphere") == 0) marker = ST_SPHERE; else if (strcmp(marker_str, "diamond") == 0) marker = ST_DIAMOND; else if (strcmp(marker_str, "cube") == 0) marker = ST_CUBE; else if (strcmp(marker_str, "box") == 0) marker = ST_BOX; else if (strcmp(marker_str, "gyro") == 0) marker = ST_GYRO; else if (strcmp(marker_str, "aster") == 0) marker = ST_ASTER; else if (strcmp(marker_str, "histogram") == 0) marker = ST_HISTOGRAM; else G_fatal_error(_("Unknown icon marker")); GP_set_sitemode(site_list[i], ST_ATT_NONE, color, width, size, marker); /* GP_set_style(site_list[i], color, width, size, marker); */ } return 1; }
/*! \brief Load raster maps/constants and set surface attributes \param params module parameters \param data nviz data */ int load_rasters(const struct GParams *params, nv_data * data) { const char *mapset; int i; int nelevs, nelev_map, nelev_const, ncolor_map, ncolor_const, nmask_map; int ntransp_map, ntransp_const, nshine_map, nshine_const; int nemit_map, nemit_const; int *surf_list, nsurfs; int id; double x, y, z; nelev_map = opt_get_num_answers(params->elev_map); nelev_const = opt_get_num_answers(params->elev_const); nelevs = nelev_const + nelev_map; /* topography (required) */ for (i = 0; i < nelevs; i++) { /* check maps */ if (i < nelev_map && strcmp(params->elev_map->answers[i], "")) { mapset = G_find_raster2(params->elev_map->answers[i], ""); if (mapset == NULL) { G_fatal_error(_("Raster map <%s> not found"), params->elev_map->answers[i]); } id = Nviz_new_map_obj(MAP_OBJ_SURF, G_fully_qualified_name(params->elev_map-> answers[i], mapset), 0.0, data); } else { if (i-nelev_map < nelev_const && strcmp(params->elev_const->answers[i-nelev_map], "")) { id = Nviz_new_map_obj(MAP_OBJ_SURF, NULL, atof(params->elev_const->answers[i-nelev_map]), data); } else { G_fatal_error(_("Missing topography attribute for surface %d"), i + 1); } } /* set position */ if (opt_get_num_answers(params->surface_pos) != 3 * nelevs){ x = atof(params->surface_pos->answers[0]); y = atof(params->surface_pos->answers[1]); z = atof(params->surface_pos->answers[2]); } else{ x = atof(params->surface_pos->answers[i*3+0]); y = atof(params->surface_pos->answers[i*3+1]); z = atof(params->surface_pos->answers[i*3+2]); } GS_set_trans(id, x, y, z); } /* set surface attributes */ surf_list = GS_get_surf_list(&nsurfs); ncolor_map = opt_get_num_answers(params->color_map); ncolor_const = opt_get_num_answers(params->color_const); nmask_map = opt_get_num_answers(params->mask_map); ntransp_map = opt_get_num_answers(params->transp_map); ntransp_const = opt_get_num_answers(params->transp_const); nshine_map = opt_get_num_answers(params->shine_map); nshine_const = opt_get_num_answers(params->shine_const); nemit_map = opt_get_num_answers(params->emit_map); nemit_const = opt_get_num_answers(params->emit_const); for (i = 0; i < nsurfs; i++) { id = surf_list[i]; /* color */ /* check for color map */ if (i < ncolor_map && strcmp(params->color_map->answers[i], "")) { mapset = G_find_raster2(params->color_map->answers[i], ""); if (mapset == NULL) { G_fatal_error(_("Raster map <%s> not found"), params->color_map->answers[i]); } Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT, G_fully_qualified_name(params->color_map-> answers[i], mapset), -1.0, data); } /* check for color value */ else if (i-ncolor_map < ncolor_const && strcmp(params->color_const->answers[i-ncolor_map], "")) { Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT, NULL, Nviz_color_from_str(params->color_const-> answers[i-ncolor_map]), data); } else { /* use by default elevation map for coloring */ if (nelev_map > 0){ Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT, G_fully_qualified_name(params->elev_map->answers[i], mapset), -1.0, data); G_verbose_message(_("Color attribute not defined, using default <%s>"), G_fully_qualified_name(params->elev_map-> answers[i], mapset)); } else{ G_fatal_error(_("Missing color attribute for surface %d"), i + 1); } } /* mask */ if (i < nmask_map && strcmp(params->mask_map->answers[i], "")) { Nviz_set_attr(id, MAP_OBJ_SURF, ATT_MASK, MAP_ATT, G_fully_qualified_name(params->mask_map->answers[i], mapset), -1.0, data); } /* transparency */ if (i < ntransp_map && strcmp(params->transp_map->answers[i], "")) { Nviz_set_attr(id, MAP_OBJ_SURF, ATT_TRANSP, MAP_ATT, G_fully_qualified_name(params->transp_map-> answers[i], mapset), -1.0, data); } else if (i-ntransp_map < ntransp_const && strcmp(params->transp_const->answers[i-ntransp_map], "")) { Nviz_set_attr(id, MAP_OBJ_SURF, ATT_TRANSP, CONST_ATT, NULL, atof(params->transp_const->answers[i-ntransp_map]), data); } /* shininess */ if (i < nshine_map && strcmp(params->shine_map->answers[i], "")) { Nviz_set_attr(id, MAP_OBJ_SURF, ATT_SHINE, MAP_ATT, G_fully_qualified_name(params->shine_map-> answers[i], mapset), -1.0, data); } else if (i-nshine_map < nshine_const && strcmp(params->shine_const->answers[i-nshine_map], "")) { Nviz_set_attr(id, MAP_OBJ_SURF, ATT_SHINE, CONST_ATT, NULL, atof(params->shine_const->answers[i-nshine_map]), data); } /* emission */ if (i < nemit_map && strcmp(params->emit_map->answers[i], "")) { Nviz_set_attr(id, MAP_OBJ_SURF, ATT_EMIT, MAP_ATT, G_fully_qualified_name(params->emit_map->answers[i], mapset), -1.0, data); } else if (i-nemit_map < nemit_const && strcmp(params->emit_const->answers[i-nemit_map], "")) { Nviz_set_attr(id, MAP_OBJ_SURF, ATT_EMIT, CONST_ATT, NULL, atof(params->emit_const->answers[i-nemit_map]), data); } /* if (i > 1) set_default_wirecolors(data, i); */ } return nsurfs; }
/*! \brief Set draw mode for loaded surfaces \param params module parameters */ void surface_set_draw_mode(const struct GParams *params) { int *surf_list, nsurfs; int i, id, draw_mode; int resol_fine, resol_coarse; char *mode, *style, *shade, *res_fine, *res_coarse, *wire_color; surf_list = GS_get_surf_list(&nsurfs); for (i = 0; i < nsurfs; i++) { draw_mode = 0; id = surf_list[i]; if (!GS_surf_exists(id)) G_fatal_error(_("Surface id %d doesn't exist"), id); if (params->mode_all->answer) { /* use one mode for all surfaces */ mode = params->mode->answers[0]; style = params->style->answers[0]; shade = params->shade->answers[0]; res_fine = params->res_fine->answers[0]; res_coarse = params->res_coarse->answers[0]; wire_color = params->wire_color->answers[0]; } else { mode = params->mode->answers[i]; style = params->style->answers[i]; shade = params->shade->answers[i]; res_fine = params->res_fine->answers[i]; res_coarse = params->res_coarse->answers[i]; wire_color = params->wire_color->answers[i]; } /* mode */ if (strcmp(mode, "coarse") == 0) { draw_mode |= DM_WIRE; } else if (strcmp(mode, "fine") == 0) { draw_mode |= DM_POLY; } else { /* both */ draw_mode |= DM_WIRE_POLY; } /* style */ if (strcmp(style, "wire") == 0) { draw_mode |= DM_GRID_WIRE; } else { /* surface */ draw_mode |= DM_GRID_SURF; } /* shading */ if (strcmp(shade, "flat") == 0) { draw_mode |= DM_FLAT; } else { /* gouraud */ draw_mode |= DM_GOURAUD; } if (GS_set_drawmode(id, draw_mode) < 0) G_fatal_error(_("Unable to set draw mode for surface id %d"), id); /* resolution */ resol_fine = atoi(res_fine); resol_coarse = atoi(res_coarse); if (GS_set_drawres(id, resol_fine, resol_fine, resol_coarse, resol_coarse) < 0) G_fatal_error(_("Unable to set draw mode for surface id %d"), id); /* wire color */ GS_set_wire_color(id, Nviz_color_from_str(wire_color)); } return; }
int main(int argc, char *argv[]) { struct GModule *module; struct GParams *params; int i, ret; int red, grn, blu; float size; double vp_height, z_exag; /* calculated viewpoint height, z-exag */ int width, height; /* output image size */ char *output_name; nv_data data; struct render_window *offscreen; /* initialize GRASS */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("visualization")); G_add_keyword(_("graphics")); G_add_keyword(_("raster")); G_add_keyword(_("vector")); G_add_keyword(_("raster3d")); module->label = _("Creates a 3D rendering of GIS data."); module->description = _("Renders surfaces (raster data), " "2D/3D vector data, and " "volumes (3D raster data) in 3D."); params = (struct GParams *)G_malloc(sizeof(struct GParams)); /* define options, call G_parser() */ parse_command(argc, argv, params); /* check parameters consistency */ check_parameters(params); width = atoi(params->size->answers[0]); height = atoi(params->size->answers[1]); G_asprintf(&output_name, "%s.%s", params->output->answer, params->format->answer); GS_libinit(); GVL_libinit(); GS_set_swap_func(swap_gl); /* define render window */ offscreen = Nviz_new_render_window(); Nviz_init_render_window(offscreen); if (Nviz_create_render_window(offscreen, NULL, width, height) == -1) G_fatal_error(_("Unable to render data")); Nviz_make_current_render_window(offscreen); /* initialize nviz data */ Nviz_init_data(&data); /* define default attributes for map objects */ Nviz_set_surface_attr_default(); /* set background color */ Nviz_set_bgcolor(&data, Nviz_color_from_str(params->bgcolor->answer)); /* init view, lights */ Nviz_init_view(&data); /* load raster maps (surface topography) & set attributes (map/constant) */ load_rasters(params, &data); /* set draw mode of loaded surfaces */ surface_set_draw_mode(params); /* load line vector maps */ if (params->vlines->answer) { load_vlines(params, &data); /* set attributes of 2d lines */ vlines_set_attrb(params); } /* load point vector maps */ if (params->vpoints->answer) { load_vpoints(params, &data); /* set attributes for points */ vpoints_set_attrb(params); } /* load volumes */ if (params->volume->answer) { load_rasters3d(params, &data); } /* define isosurfaces for displaying volumes */ if (params->isosurf_level->answer) { add_isosurfs(params, &data); } /* define slices for displaying volumes */ if (params->slice->answer) { add_slices(params, &data); } /* focus on loaded data */ Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1); /* define view point */ if (params->exag->answer) { z_exag = atof(params->exag->answer); } else { z_exag = Nviz_get_exag(); G_verbose_message(_("Vertical exaggeration not given, using calculated " "value %.0f"), z_exag); } Nviz_change_exag(&data, z_exag); if (params->height->answer) { vp_height = atof(params->height->answer); } else { double min, max; Nviz_get_exag_height(&vp_height, &min, &max); G_verbose_message(_("Viewpoint height not given, using calculated " "value %.0f"), vp_height); } Nviz_set_viewpoint_height(vp_height); Nviz_set_viewpoint_position(atof(params->pos->answers[0]), atof(params->pos->answers[1])); Nviz_set_viewpoint_twist(atoi(params->twist->answer)); Nviz_set_viewpoint_persp(atoi(params->persp->answer)); if (params->focus->answer) { Nviz_set_focus(&data, atof(params->focus->answers[0]), atof(params->focus->answers[1]), atof(params->focus->answers[2])); } /* set lights */ Nviz_set_light_position(&data, 1, atof(params->light_pos->answers[0]), atof(params->light_pos->answers[1]), atof(params->light_pos->answers[2]), 0.0); Nviz_set_light_bright(&data, 1, atoi(params->light_bright->answer) / 100.0); if (G_str_to_color(params->light_color->answer, &red, &grn, &blu) != 1) { red = grn = blu = 255; } Nviz_set_light_color(&data, 1, red, grn, blu); Nviz_set_light_ambient(&data, 1, atof(params->light_ambient->answer) / 100.0); /* define fringes */ if (params->fringe->answer) { int nw, ne, sw, se; i = 0; nw = ne = sw = se = 0; while (params->fringe->answers[i]) { const char *edge = params->fringe->answers[i++]; if (strcmp(edge, "nw") == 0) nw = 1; else if (strcmp(edge, "ne") == 0) ne = 1; else if (strcmp(edge, "sw") == 0) sw = 1; else if (strcmp(edge, "se") == 0) se = 1; } Nviz_new_fringe(&data, -1, Nviz_color_from_str(params->fringe_color->answer), atof(params->fringe_elev->answer), nw, ne, sw, se); } /* draw north arrow */ if (params->north_arrow->answer) { if (!params->north_arrow_size->answer) size = Nviz_get_longdim(&data) / 8.; else size = atof(params->north_arrow_size->answer); Nviz_set_arrow(&data, atoi(params->north_arrow->answers[0]), atoi(params->north_arrow->answers[1]), size, Nviz_color_from_str(params->north_arrow_color-> answer)); Nviz_draw_arrow(&data); } GS_clear(data.bgcolor); /* cutting planes */ if (params->cplane->answer) draw_cplane(params, &data); /* draw */ Nviz_draw_all(&data); /* write to image */ ret = 0; if (strcmp(params->format->answer, "ppm") == 0) ret = write_img(output_name, FORMAT_PPM); if (strcmp(params->format->answer, "tif") == 0) ret = write_img(output_name, FORMAT_TIF); if (!ret) G_fatal_error(_("Unsupported output format")); G_done_msg(_("File <%s> created."), output_name); Nviz_destroy_data(&data); Nviz_destroy_render_window(offscreen); G_free((void *)output_name); G_free((void *)params); exit(EXIT_SUCCESS); }