GRASS Programmer's Manual  6.4.2(2012)
map_obj.c
Go to the documentation of this file.
00001 
00018 #include <stdlib.h>
00019 #include <time.h>
00020 
00021 #include <grass/glocale.h>
00022 #include <grass/nviz.h>
00023 
00044 int Nviz_new_map_obj(int type, const char *name, double value, nv_data * data)
00045 {
00046     int new_id, i;
00047     int num_surfs, *surf_list;
00048 
00049     /*
00050      * For each type of map obj do the following --
00051      *   1) Verify we havn't maxed out the number of
00052      *      allowed objects.
00053      *   2) Call the internal library to generate a new
00054      *      map object of the specified type.
00055      */
00056     /* raster -> surface */
00057     if (type == MAP_OBJ_SURF) {
00058         if (GS_num_surfs() >= MAX_SURFS) {
00059             G_warning(_("Maximum surfaces loaded!"));
00060             return -1;
00061         }
00062 
00063         new_id = GS_new_surface();
00064 
00065         if (new_id < 0) {
00066             return -1;
00067         }
00068 
00069         if (name) {
00070             /* map */
00071             if (!Nviz_set_attr(new_id, MAP_OBJ_SURF, ATT_TOPO,
00072                                MAP_ATT, name, -1.0, data)) {
00073                 return -1;
00074             }
00075         }
00076         else {
00077             /* constant */
00078             if (!Nviz_set_attr(new_id, MAP_OBJ_SURF, ATT_TOPO,
00079                                CONST_ATT, NULL, value,
00080                  data)) {
00081                 return -1;
00082             }
00083         }
00084     }
00085     /* vector overlay */
00086     else if (type == MAP_OBJ_VECT) {
00087         if (GV_num_vects() >= MAX_VECTS) {
00088             G_warning(_("Maximum vector line maps loaded!"));
00089             return -1;
00090         }
00091 
00092         new_id = GV_new_vector();
00093 
00094         if (name) {
00095             if (GV_load_vector(new_id, name) < 0) {
00096                 GV_delete_vector(new_id);
00097                 G_warning(_("Error loading vector map <%s>"), name);
00098                 return -1;
00099             }
00100         }
00101 
00102         /* initialize display parameters
00103            automatically select all surfaces to draw vector */
00104         GV_set_vectmode(new_id, 1, 0xFF0000, 2, 0);
00105         surf_list = GS_get_surf_list(&num_surfs);
00106         if (num_surfs) {
00107             for (i = 0; i < num_surfs; i++) {
00108                 GV_select_surf(new_id, surf_list[i]);
00109             }
00110         }
00111         G_free(surf_list);
00112     }
00113     /* vector points overlay */
00114     else if (type == MAP_OBJ_SITE) {
00115         if (GP_num_sites() >= MAX_SITES) {
00116             G_warning(_("Maximum vector point maps loaded!"));
00117             return -1;
00118         }
00119 
00120         new_id = GP_new_site();
00121 
00122         /* initizalize site attributes */
00123         Nviz_set_vpoint_attr_default(new_id);
00124 
00125         /* load vector points */
00126         if (0 > GP_load_site(new_id, name)) {
00127             GP_delete_site(new_id);
00128             G_warning(_("Error loading vector map <%s>"), name);
00129             return -1;
00130         }
00131 
00132         /* initialize display parameters */
00133         GP_set_sitemode(new_id, ST_ATT_NONE, 0xFF0000, 2, 100, ST_X);
00134         surf_list = GS_get_surf_list(&num_surfs);
00135         for (i = 0; i < num_surfs; i++) {
00136             GP_select_surf(new_id, surf_list[i]);
00137         }
00138         G_free(surf_list);
00139     }
00140     /* 3d raster map -> volume */
00141     else if (type == MAP_OBJ_VOL) {
00142         if (GVL_num_vols() >= MAX_VOLS) {
00143             G_warning(_("Maximum volumes loaded!"));
00144             return -1;
00145         }
00146         
00147         new_id = GVL_new_vol();
00148 
00149         /* load volume */
00150         if (0 > GVL_load_vol(new_id, name)) {
00151             GVL_delete_vol(new_id);
00152             G_warning(_("Error loading 3d raster map <%s>"), name);
00153             return -1;
00154         }
00155 
00156         /* initilaze volume attributes */
00157         Nviz_set_volume_attr_default(new_id);
00158     }
00159     else {
00160         G_warning(_("Nviz_new_map_obj(): unsupported data type"));
00161         return -1;
00162     }
00163     
00164     return new_id;
00165 }
00166 
00180 int Nviz_set_attr(int id, int type, int desc, int src,
00181                   const char *str_value, double num_value, nv_data * data)
00182 {
00183     int ret;
00184     double value;
00185 
00186     switch (type) {
00187     case (MAP_OBJ_SURF):{
00188             /* Basically two cases, either we are setting to a constant field, or
00189              * we are loading an actual file. Setting a constant is the easy part
00190              * so we try and do that first.
00191              */
00192             if (src == CONST_ATT) {
00193                 /* Get the value for the constant
00194                  * Note that we require the constant to be an integer
00195                  */
00196                 if (str_value)
00197                     value = (double)atof(str_value);
00198                 else
00199                     value = num_value;
00200 
00201                 /* Only special case is setting constant color.
00202                  * In this case we have to decode the constant Tcl
00203                  * returns so that the gsf library understands it.
00204                  */
00205                 if (desc == ATT_COLOR) {
00206                     /* TODO check this - sometimes gets reversed when save state
00207                        saves a surface with constant color
00208 
00209                        int r, g, b;
00210                        r = (((int) value) & RED_MASK) >> 16;
00211                        g = (((int) value) & GRN_MASK) >> 8;
00212                        b = (((int) value) & BLU_MASK);
00213                        value = r + (g << 8) + (b << 16);
00214                      */
00215                 }
00216 
00217                 /* Once the value is parsed, set it */
00218                 ret = GS_set_att_const(id, desc, value);
00219             }
00220             else if (src == MAP_ATT) {
00221                 ret = GS_load_att_map(id, str_value, desc);
00222             }
00223 
00224             /* After we've loaded a constant map or a file,
00225              * may need to adjust resolution if we are resetting
00226              * topology (for example)
00227              */
00228             if (0 <= ret) {
00229                 if (desc == ATT_TOPO) {
00230                     int rows, cols, max;
00231                     int max2;
00232 
00233                     /* If topology attribute is being set then need to set
00234                      * resolution of incoming map to some sensible value so we
00235                      * don't wait all day for drawing.
00236                      */
00237                     GS_get_dims(id, &rows, &cols);
00238                     max = (rows > cols) ? rows : cols;
00239                     max = max / 50;
00240                     if (max < 1)
00241                         max = 1;
00242                     max2 = max / 5;
00243                     if (max2 < 1)
00244                         max2 = 1;
00245                     /* reset max to finer for coarse surf drawing */
00246                     max = max2 + max2 / 2;
00247                     if (max < 1)
00248                         max = 1;
00249 
00250                     GS_set_drawres(id, max2, max2, max, max);
00251                     GS_set_drawmode(id, DM_GOURAUD | DM_POLY | DM_GRID_SURF);
00252                 }
00253 
00254                 /* Not sure about this next line, should probably just
00255                  * create separate routines to figure the Z range as well
00256                  * as the XYrange
00257                  */
00258                 Nviz_update_ranges(data);
00259 
00260                 break;
00261             }
00262     default:{
00263                 return 0;
00264             }
00265         }
00266     }
00267 
00268     return 1;
00269 }
00270 
00274 void Nviz_set_surface_attr_default()
00275 {
00276     float defs[MAX_ATTS];
00277 
00278     defs[ATT_TOPO] = 0;
00279     defs[ATT_COLOR] = DEFAULT_SURF_COLOR;
00280     defs[ATT_MASK] = 0;
00281     defs[ATT_TRANSP] = 0;
00282     defs[ATT_SHINE] = 60;
00283     defs[ATT_EMIT] = 0;
00284 
00285     GS_set_att_defaults(defs, defs);
00286 
00287     return;
00288 }
00289 
00298 int Nviz_set_vpoint_attr_default(int id)
00299 {
00300     int i;
00301     geosite *gp;
00302 
00303     gp = gp_get_site(id);
00304 
00305     if (!gp)
00306         return 0;
00307 
00308     for (i = 0; i < GPT_MAX_ATTR; i++)
00309         gp->use_attr[i] = ST_ATT_NONE;
00310 
00311     return 1;
00312 }
00313 
00322 int Nviz_set_volume_attr_default(int id)
00323 {
00324     int rows, cols, depths;
00325     int max;
00326 
00327     GVL_get_dims(id, &rows, &cols, &depths);
00328     max = (rows > cols) ? rows : cols;
00329     max = (depths > max) ? depths : max;
00330     max = max / 35;
00331     if (max < 1)
00332         max = 1;
00333     
00334     if (max > cols)
00335         max = cols / 2;
00336     if (max > rows)
00337         max = rows / 2;
00338     if (max > depths)
00339         max = depths / 2;
00340     
00341     /* set default drawres and drawmode for isosurfaces */
00342     GVL_isosurf_set_drawres(id, max, max, max);
00343     GVL_isosurf_set_drawmode(id, DM_GOURAUD);
00344     
00345     /* set default drawres and drawmode for slices */
00346     GVL_slice_set_drawres(id, 1, 1, 1);
00347     GVL_slice_set_drawmode(id, DM_GOURAUD | DM_POLY);
00348 
00349     return 1;
00350 }
00351 
00362 int Nviz_unset_attr(int id, int type, int desc)
00363 {
00364     if (type == MAP_OBJ_SURF) {
00365         return GS_unset_att(id, desc);
00366     }
00367 
00368     return 0;
00369 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines