GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
nviz.c
Go to the documentation of this file.
00001 
00015 #include <grass/glocale.h>
00016 #include <grass/nviz.h>
00017 
00023 void Nviz_init_data(nv_data * data)
00024 {
00025     unsigned int i;
00026 
00027     /* data range */
00028     data->zrange = 0;
00029     data->xyrange = 0;
00030 
00031     /* clip planes, turn off by default */
00032     data->num_cplanes = 0;
00033     data->cur_cplane = 0;
00034     for (i = 0; i < MAX_CPLANES; i++) {
00035         Nviz_new_cplane(data, i);
00036         Nviz_off_cplane(data, i);
00037     }
00038 
00039     /* lights */
00040     GS_set_light_reset(1);
00041     
00042     for (i = 0; i < MAX_LIGHTS - 1; i++) {
00043         Nviz_new_light(data);
00044     }
00045 
00046     /* fringe */
00047     data->num_fringes = 0;
00048     data->fringe = NULL;
00049 
00050     /* north arrow */
00051     data->draw_arrow = 0;
00052     data->arrow = NULL;
00053 
00054     /* scale bar*/
00055     data->num_scalebars = 0;
00056     data->scalebar = NULL;
00057 
00058     return;
00059 }
00060 
00065 void Nviz_destroy_data(nv_data *data)
00066 {
00067     int i;
00068     for (i = 0; data->num_fringes; i++) {
00069         G_free(data->fringe[i]);
00070         data->fringe[i] = NULL;
00071     }
00072     data->num_fringes = 0;
00073     data->fringe = NULL;
00074     
00075     if (data->arrow) {
00076         G_free(data->arrow);
00077         data->arrow = NULL;
00078         data->draw_arrow = 0;
00079     }
00080 
00081     for (i = 0; data->num_scalebars; i++) {
00082         G_free(data->scalebar[i]);
00083         data->scalebar[i] = NULL;
00084     }
00085     data->num_scalebars = 0;
00086     data->scalebar = NULL;
00087 }
00088 
00095 void Nviz_set_bgcolor(nv_data * data, int color)
00096 {
00097     data->bgcolor = color;
00098 
00099     return;
00100 }
00101 
00109 int Nviz_get_bgcolor(nv_data * data)
00110 {
00111     return data->bgcolor;
00112 }
00113 
00121 int Nviz_color_from_str(const char *color_str)
00122 {
00123     int red, grn, blu;
00124 
00125     if (G_str_to_color(color_str, &red, &grn, &blu) != 1) {
00126         G_warning(_("Invalid color (%s), using \"white\" as default"),
00127                   color_str);
00128         red = grn = blu = 255;
00129     }
00130 
00131     return (red & RED_MASK) + ((int)((grn) << 8) & GRN_MASK) +
00132         ((int)((blu) << 16) & BLU_MASK);
00133 }
00134 
00146 struct fringe_data *Nviz_new_fringe(nv_data *data,
00147                                     int id, unsigned long color,
00148                                     double elev, int nw, int ne, int sw, int se)
00149 {
00150     int num;
00151     int *surf;
00152     struct fringe_data *f;
00153 
00154     if (!GS_surf_exists(id)) {
00155         /* select first surface from the list */
00156         surf = GS_get_surf_list(&num);
00157         if (num < 1)
00158             return NULL;
00159         id = surf[0];
00160         G_free(surf);
00161     }
00162      
00163 
00164     f = (struct fringe_data *) G_malloc(sizeof(struct fringe_data));
00165     f->id = id;
00166     f->color = color;
00167     f->elev = elev;
00168     f->where[0] = nw;
00169     f->where[1] = ne;
00170     f->where[2] = sw;
00171     f->where[3] = se;
00172 
00173     data->fringe = (struct fringe_data **) G_realloc(data->fringe, data->num_fringes + 1 * sizeof(struct fringe_data *));
00174     data->fringe[data->num_fringes++] = f;
00175     
00176     return f;
00177 }
00178 
00190 struct fringe_data *Nviz_set_fringe(nv_data *data,
00191                                     int id, unsigned long color,
00192                                     double elev, int nw, int ne, int sw, int se)
00193 {
00194     int i, num;
00195     int *surf;
00196     struct fringe_data *f;
00197 
00198     if (!GS_surf_exists(id)) {
00199         /* select first surface from the list */
00200         surf = GS_get_surf_list(&num);
00201         if (num < 1)
00202             return NULL;
00203         id = surf[0];
00204         G_free(surf);
00205     }
00206     
00207     for (i = 0; i < data->num_fringes; i++) {
00208         f = data->fringe[i];
00209         if (f->id == id) {
00210             f->color = color;
00211             f->elev  = elev;
00212             f->where[0] = nw;
00213             f->where[1] = ne;
00214             f->where[2] = sw;
00215             f->where[3] = se;
00216             
00217             return f;
00218         }
00219     }
00220 
00221     f = Nviz_new_fringe(data,
00222                         id, color,
00223                         elev, nw, ne, sw, se);
00224     
00225     return f;
00226 }
00231 void Nviz_draw_fringe(nv_data *data)
00232 {
00233     int i;
00234 
00235     for (i = 0; i < data->num_fringes; i++) {
00236         struct fringe_data *f = data->fringe[i];
00237 
00238         GS_draw_fringe(f->id, f->color, f->elev, f->where);
00239     }
00240 }
00249 int Nviz_set_arrow(nv_data *data,
00250                    int sx, int sy, float size,
00251                    unsigned int color)
00252 {
00253     int id, pt[2];
00254     int *surf_list, num_surfs;
00255     float coords[3];
00256     struct arrow_data *arw;
00257 
00258     if (GS_num_surfs() > 0) {
00259         surf_list = GS_get_surf_list(&num_surfs);
00260         id = surf_list[0];
00261         G_free(surf_list);
00262 
00263         pt[0] = sx;
00264         pt[1] = sy;
00265 
00266         GS_set_Narrow(pt, id, coords);
00267 
00268         if (data->arrow) {
00269             data->arrow->color = color;
00270             data->arrow->size  = size;
00271             data->arrow->where[0]  = coords[0];
00272             data->arrow->where[1]  = coords[1];
00273             data->arrow->where[2]  = coords[2];
00274         }    
00275         else {
00276             arw = (struct arrow_data *) G_malloc(sizeof(struct arrow_data));
00277             arw->color = color;
00278             arw->size  = size;
00279             arw->where[0]  = coords[0];
00280             arw->where[1]  = coords[1];
00281             arw->where[2]  = coords[2];
00282 
00283             data->arrow = arw;
00284         }
00285         return 1;
00286     }
00287     return 0;
00288 }
00289 
00290 
00296 int Nviz_draw_arrow(nv_data *data)
00297 {
00298 
00299     struct arrow_data *arw = data->arrow;
00300     GLuint FontBase = 0; /* don't know how to get fontbase*/
00301 
00302     data->draw_arrow = 1;
00303     gsd_north_arrow(arw->where, arw->size, FontBase, arw->color, arw->color);
00304 
00305     return 1;
00306 }
00307 
00313 void Nviz_delete_arrow(nv_data *data)
00314 {
00315     data->draw_arrow = 0;
00316 
00317     return;
00318 }
00319 
00332 struct scalebar_data *Nviz_new_scalebar(nv_data *data,
00333                       int bar_id, float *coords, float size,
00334                       unsigned int color)
00335 {
00336     struct scalebar_data *s;
00337      
00338 
00339     s = (struct scalebar_data *) G_malloc(sizeof(struct scalebar_data));
00340     s->id = bar_id;
00341     s->color = color;
00342     s->size = size;
00343     s->where[0] = coords[0];
00344     s->where[1] = coords[1];
00345     s->where[2] = coords[2];
00346 
00347     data->scalebar = (struct scalebar_data **) G_realloc(data->scalebar,
00348                       data->num_scalebars + 1 * sizeof(struct scalebar_data *));
00349     data->scalebar[data->num_scalebars++] = s;
00350 
00351     return s;
00352 
00353 }
00366 struct scalebar_data *Nviz_set_scalebar(nv_data *data, int bar_id,
00367                       int sx, int sy, float size,
00368                       unsigned int color)
00369 {
00370     int i, id, pt[2];
00371     int *surf_list, num_surfs;
00372     float coords[3];
00373     struct scalebar_data *s;
00374 
00375     if (GS_num_surfs() > 0) {
00376         surf_list = GS_get_surf_list(&num_surfs);
00377         id = surf_list[0];
00378         G_free(surf_list);
00379 
00380         pt[0] = sx;
00381         pt[1] = sy;
00382 
00383         GS_set_Narrow(pt, id, coords); /* the same like arrow */
00384 
00385         for (i = 0; i < data->num_scalebars; i++) {
00386             s = data->scalebar[i];
00387             if (s->id == bar_id) {
00388                 s->color = color;
00389                 s->size = size;
00390                 s->where[0] = coords[0];
00391                 s->where[1] = coords[1];
00392                 s->where[2] = coords[2];
00393 
00394                 return s;
00395             }
00396         }
00397         
00398         s = Nviz_new_scalebar(data, bar_id, coords, size, color);
00399 
00400         return s;
00401     }
00402     return NULL;
00403 }
00409 void Nviz_draw_scalebar(nv_data *data)
00410 {
00411     int i;
00412 
00413     GLuint FontBase = 0; /* don't know how to get fontbase*/
00414 
00415     for (i = 0; i < data->num_scalebars; i++) {
00416         struct scalebar_data *s = data->scalebar[i];
00417 
00418         gsd_scalebar_v2(s->where, s->size, FontBase, s->color, s->color);
00419     }
00420 }
00421 
00427 void Nviz_delete_scalebar(nv_data *data, int bar_id)
00428 {
00429     if (bar_id < data->num_scalebars) {
00430         G_free(data->scalebar[bar_id]);
00431         data->scalebar[bar_id] = NULL;
00432         data->num_scalebars--;
00433     }
00434 }