GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
nviz.c
Go to the documentation of this file.
1 
15 #include <grass/colors.h>
16 #include <grass/raster.h>
17 #include <grass/glocale.h>
18 #include <grass/nviz.h>
19 
25 void Nviz_init_data(nv_data * data)
26 {
27  unsigned int i;
28 
29  /* data range */
30  data->zrange = 0;
31  data->xyrange = 0;
32 
33  /* clip planes, turn off by default */
34  data->num_cplanes = 0;
35  data->cur_cplane = 0;
36  for (i = 0; i < MAX_CPLANES; i++) {
37  Nviz_new_cplane(data, i);
38  Nviz_off_cplane(data, i);
39  }
40 
41  /* lights */
43 
44  for (i = 0; i < MAX_LIGHTS - 1; i++) {
45  Nviz_new_light(data);
46  }
47 
48  /* fringe */
49  data->num_fringes = 0;
50  data->fringe = NULL;
51 
52  /* north arrow */
53  data->draw_arrow = 0;
54  data->arrow = NULL;
55 
56  /* scale bar*/
57  data->num_scalebars = 0;
58  data->scalebar = NULL;
59 
60  return;
61 }
62 
67 void Nviz_destroy_data(nv_data *data)
68 {
69  int i;
70  for (i = 0; i < data->num_fringes; i++) {
71  G_free(data->fringe[i]);
72  data->fringe[i] = NULL;
73  }
74  data->num_fringes = 0;
75  data->fringe = NULL;
76 
77  if (data->arrow) {
78  G_free(data->arrow);
79  data->arrow = NULL;
80  data->draw_arrow = 0;
81  }
82 
83  for (i = 0; i < data->num_scalebars; i++) {
84  G_free(data->scalebar[i]);
85  data->scalebar[i] = NULL;
86  }
87  data->num_scalebars = 0;
88  data->scalebar = NULL;
89 }
90 
97 void Nviz_set_bgcolor(nv_data * data, int color)
98 {
99  data->bgcolor = color;
100 
101  return;
102 }
103 
111 int Nviz_get_bgcolor(nv_data * data)
112 {
113  return data->bgcolor;
114 }
115 
123 int Nviz_color_from_str(const char *color_str)
124 {
125  int red, grn, blu;
126 
127  if (G_str_to_color(color_str, &red, &grn, &blu) != 1) {
128  G_warning(_("Invalid color (%s), using \"white\" as default"),
129  color_str);
130  red = grn = blu = 255;
131  }
132 
133  return (red & RED_MASK) + ((int)((grn) << 8) & GRN_MASK) +
134  ((int)((blu) << 16) & BLU_MASK);
135 }
136 
148 struct fringe_data *Nviz_new_fringe(nv_data *data,
149  int id, unsigned long color,
150  double elev, int nw, int ne, int sw, int se)
151 {
152  int num;
153  int *surf;
154  struct fringe_data *f;
155 
156  if (!GS_surf_exists(id)) {
157  /* select first surface from the list */
158  surf = GS_get_surf_list(&num);
159  if (num < 1)
160  return NULL;
161  id = surf[0];
162  G_free(surf);
163  }
164 
165 
166  f = (struct fringe_data *) G_malloc(sizeof(struct fringe_data));
167  f->id = id;
168  f->color = color;
169  f->elev = elev;
170  f->where[0] = nw;
171  f->where[1] = ne;
172  f->where[2] = sw;
173  f->where[3] = se;
174 
175  data->fringe = (struct fringe_data **) G_realloc(data->fringe, data->num_fringes + 1 * sizeof(struct fringe_data *));
176  data->fringe[data->num_fringes++] = f;
177 
178  return f;
179 }
180 
192 struct fringe_data *Nviz_set_fringe(nv_data *data,
193  int id, unsigned long color,
194  double elev, int nw, int ne, int sw, int se)
195 {
196  int i, num;
197  int *surf;
198  struct fringe_data *f;
199 
200  if (!GS_surf_exists(id)) {
201  /* select first surface from the list */
202  surf = GS_get_surf_list(&num);
203  if (num < 1)
204  return NULL;
205  id = surf[0];
206  G_free(surf);
207  }
208 
209  for (i = 0; i < data->num_fringes; i++) {
210  f = data->fringe[i];
211  if (f->id == id) {
212  f->color = color;
213  f->elev = elev;
214  f->where[0] = nw;
215  f->where[1] = ne;
216  f->where[2] = sw;
217  f->where[3] = se;
218 
219  return f;
220  }
221  }
222 
223  f = Nviz_new_fringe(data,
224  id, color,
225  elev, nw, ne, sw, se);
226 
227  return f;
228 }
233 void Nviz_draw_fringe(nv_data *data)
234 {
235  int i;
236 
237  for (i = 0; i < data->num_fringes; i++) {
238  struct fringe_data *f = data->fringe[i];
239 
240  GS_draw_fringe(f->id, f->color, f->elev, f->where);
241  }
242 }
251 int Nviz_set_arrow(nv_data *data,
252  int sx, int sy, float size,
253  unsigned int color)
254 {
255  int id, pt[2];
256  int *surf_list, num_surfs;
257  float coords[3];
258  struct arrow_data *arw;
259 
260  if (GS_num_surfs() > 0) {
261  surf_list = GS_get_surf_list(&num_surfs);
262  id = surf_list[0];
263  G_free(surf_list);
264 
265  pt[0] = sx;
266  pt[1] = sy;
267 
268  GS_set_Narrow(pt, id, coords);
269 
270  if (data->arrow) {
271  data->arrow->color = color;
272  data->arrow->size = size;
273  data->arrow->where[0] = coords[0];
274  data->arrow->where[1] = coords[1];
275  data->arrow->where[2] = coords[2];
276  }
277  else {
278  arw = (struct arrow_data *) G_malloc(sizeof(struct arrow_data));
279  arw->color = color;
280  arw->size = size;
281  arw->where[0] = coords[0];
282  arw->where[1] = coords[1];
283  arw->where[2] = coords[2];
284 
285  data->arrow = arw;
286  }
287  return 1;
288  }
289  return 0;
290 }
291 
292 
298 int Nviz_draw_arrow(nv_data *data)
299 {
300 
301  struct arrow_data *arw = data->arrow;
302  GLuint FontBase = 0; /* don't know how to get fontbase*/
303 
304  data->draw_arrow = 1;
305  gsd_north_arrow(arw->where, arw->size, FontBase, arw->color, arw->color);
306 
307  return 1;
308 }
309 
315 void Nviz_delete_arrow(nv_data *data)
316 {
317  data->draw_arrow = 0;
318 
319  return;
320 }
321 
334 struct scalebar_data *Nviz_new_scalebar(nv_data *data,
335  int bar_id, float *coords, float size,
336  unsigned int color)
337 {
338  struct scalebar_data *s;
339 
340 
341  s = (struct scalebar_data *) G_malloc(sizeof(struct scalebar_data));
342  s->id = bar_id;
343  s->color = color;
344  s->size = size;
345  s->where[0] = coords[0];
346  s->where[1] = coords[1];
347  s->where[2] = coords[2];
348 
349  data->scalebar = (struct scalebar_data **) G_realloc(data->scalebar,
350  (data->num_scalebars + 1) * sizeof(struct scalebar_data *));
351  data->scalebar[data->num_scalebars++] = s;
352 
353  return s;
354 
355 }
368 struct scalebar_data *Nviz_set_scalebar(nv_data *data, int bar_id,
369  int sx, int sy, float size,
370  unsigned int color)
371 {
372  int i, id, pt[2];
373  int *surf_list, num_surfs;
374  float coords[3];
375  struct scalebar_data *s;
376 
377  if (GS_num_surfs() > 0) {
378  surf_list = GS_get_surf_list(&num_surfs);
379  id = surf_list[0];
380  G_free(surf_list);
381 
382  pt[0] = sx;
383  pt[1] = sy;
384 
385  GS_set_Narrow(pt, id, coords); /* the same like arrow */
386 
387  for (i = 0; i < data->num_scalebars; i++) {
388  if (data->scalebar[i]) {
389  s = data->scalebar[i];
390  if (s->id == bar_id) {
391  s->color = color;
392  s->size = size;
393  s->where[0] = coords[0];
394  s->where[1] = coords[1];
395  s->where[2] = coords[2];
396 
397  return s;
398  }
399  }
400  }
401 
402  s = Nviz_new_scalebar(data, bar_id, coords, size, color);
403 
404  return s;
405  }
406  return NULL;
407 }
413 void Nviz_draw_scalebar(nv_data *data)
414 {
415  int i;
416 
417  GLuint FontBase = 0; /* don't know how to get fontbase*/
418 
419  for (i = 0; i < data->num_scalebars; i++) {
420  if (data->scalebar[i]) {
421  struct scalebar_data *s = data->scalebar[i];
422 
423  gsd_scalebar_v2(s->where, s->size, FontBase, s->color, s->color);
424  }
425  }
426 }
427 
436 void Nviz_delete_scalebar(nv_data *data, int bar_id)
437 {
438  if (bar_id < data->num_scalebars && data->scalebar[bar_id] != NULL) {
439  G_free(data->scalebar[bar_id]);
440  data->scalebar[bar_id] = NULL;
441  }
442 }
int Nviz_color_from_str(const char *color_str)
Get color value from color string (name or RGB triplet)
Definition: nviz.c:123
#define RED_MASK
Definition: gsd_prim.c:38
void Nviz_delete_arrow(nv_data *data)
Deletes the North Arrow.
Definition: nviz.c:315
void GS_set_Narrow(int *pt, int id, float *pos2)
Set decoration, north arrow ??
Definition: gs2.c:569
#define BLU_MASK
Definition: gsd_prim.c:40
#define GRN_MASK
Definition: gsd_prim.c:39
#define NULL
Definition: ccmath.h:32
int gsd_north_arrow(float *pos2, float len, GLuint fontbase, unsigned long arw_clr, unsigned long text_clr)
Draw North Arrow takes OpenGL coords and size.
Definition: gsd_objs.c:827
int GS_surf_exists(int id)
Definition: gs2.c:195
void Nviz_init_data(nv_data *data)
Initialize Nviz data.
Definition: nviz.c:25
int Nviz_get_bgcolor(nv_data *data)
Get background color.
Definition: nviz.c:111
void GS_set_light_reset(int i)
Definition: gs2.c:253
int Nviz_off_cplane(nv_data *data, int id)
Turn off (make inactive) the given clip plane.
Definition: cplanes_obj.c:63
void Nviz_draw_scalebar(nv_data *data)
Draws the Scale bar.
Definition: nviz.c:413
int GS_num_surfs(void)
Get number of surfaces.
Definition: gs2.c:1525
struct fringe_data * Nviz_new_fringe(nv_data *data, int id, unsigned long color, double elev, int nw, int ne, int sw, int se)
Definition: nviz.c:148
struct scalebar_data * Nviz_new_scalebar(nv_data *data, int bar_id, float *coords, float size, unsigned int color)
Definition: nviz.c:334
void GS_draw_fringe(int id, unsigned long clr, float elev, int *where)
Draw fringe around data (surface) at selected corners.
Definition: gs2.c:824
void Nviz_destroy_data(nv_data *data)
Free allocated space by nv_data struct.
Definition: nviz.c:67
int Nviz_draw_arrow(nv_data *data)
Draws the North Arrow.
Definition: nviz.c:298
int Nviz_new_cplane(nv_data *data, int id)
Creates a clip plane object.
Definition: cplanes_obj.c:30
struct fringe_data * Nviz_set_fringe(nv_data *data, int id, unsigned long color, double elev, int nw, int ne, int sw, int se)
Definition: nviz.c:192
void Nviz_set_bgcolor(nv_data *data, int color)
Set background color.
Definition: nviz.c:97
int G_str_to_color(const char *str, int *red, int *grn, int *blu)
Parse color string and set red,green,blue.
Definition: color_str.c:112
int gsd_scalebar_v2(float *pos, float len, GLuint fontbase, unsigned long bar_clr, unsigned long text_clr)
Draw Scalebar (as lines)
Definition: gsd_objs.c:1249
void Nviz_draw_fringe(nv_data *data)
Definition: nviz.c:233
void Nviz_delete_scalebar(nv_data *data, int bar_id)
Deletes scale bar.
Definition: nviz.c:436
int Nviz_new_light(nv_data *data)
Define new light.
Definition: lights.c:164
void G_free(void *buf)
Free allocated memory.
Definition: alloc.c:149
struct scalebar_data * Nviz_set_scalebar(nv_data *data, int bar_id, int sx, int sy, float size, unsigned int color)
Sets the scale bar position and return world coords.
Definition: nviz.c:368
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
int Nviz_set_arrow(nv_data *data, int sx, int sy, float size, unsigned int color)
Sets the North Arrow position and return world coords.
Definition: nviz.c:251
int * GS_get_surf_list(int *numsurfs)
Get surface list.
Definition: gs2.c:1540