GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
nviz.c
Go to the documentation of this file.
1 /*!
2  \file lib/nviz/nviz.c
3 
4  \brief Nviz library -- Data management
5 
6  Based on visualization/nviz/src/
7 
8  (C) 2008, 2010 by the GRASS Development Team
9  This program is free software under the GNU General Public License
10  (>=v2). Read the file COPYING that comes with GRASS for details.
11 
12  \author Updated/modified by Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
13  */
14 
15 #include <grass/colors.h>
16 #include <grass/raster.h>
17 #include <grass/glocale.h>
18 #include <grass/nviz.h>
19 
20 /*!
21  \brief Initialize Nviz data
22 
23  \param data nviz data
24  */
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 
63 /*! \brief Free allocated space by nv_data struct
64 
65  \param data nviz data
66 */
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 
91 /*!
92  \brief Set background color
93 
94  \param data nviz data
95  \param color color value
96  */
97 void Nviz_set_bgcolor(nv_data * data, int color)
98 {
99  data->bgcolor = color;
100 
101  return;
102 }
103 
104 /*!
105  \brief Get background color
106 
107  \param data nviz data
108 
109  \return color color value
110  */
112 {
113  return data->bgcolor;
114 }
115 
116 /*!
117  \brief Get color value from color string (name or RGB triplet)
118 
119  \param color_str color string
120 
121  \return color value
122  */
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 
137 /*! Add new fringe
138 
139  \param data nviz data
140  \param id surface id
141  \param color color
142  \param elev fringe elevation
143  \param nw,ne,sw,se 1 (turn on) 0 (turn off)
144 
145  \return pointer to allocated fringe_data structure
146  \return NULL on error
147 */
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 
181 /*! Set fringe
182 
183  \param data nviz data
184  \param id surface id
185  \param color color
186  \param elev fringe elevation
187  \param nw,ne,sw,se 1 (turn on) 0 (turn off)
188 
189  \return pointer to allocated fringe_data structure
190  \return NULL on error
191 */
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 }
229 /*! Draw fringe
230 
231  \param data nviz data
232  */
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 }
243 /*!
244  \brief Sets the North Arrow position and return world coords
245 
246  \param data nviz data
247  \param sx,sy screen coordinates
248  \param size arrow length
249  \param color arrow/text color
250  */
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 
293 /*!
294  \brief Draws the North Arrow
295 
296  \param data nviz data
297  */
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 
310 /*!
311  \brief Deletes the North Arrow
312 
313  \param data nviz data
314  */
316 {
317  data->draw_arrow = 0;
318 
319  return;
320 }
321 
322 /*! Add new scalebar
323 
324  \param data nviz data
325  \param bar_id scale bar id
326  \param coords real(?) coordinates
327  \param size scale bar length
328  \param color scalebar/text color
329 
330  \return pointer to allocated scalebar_data structure
331  \return NULL on error
332 */
333 
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 }
356 /*!
357  \brief Sets the scale bar position and return world coords
358 
359  \param data nviz data
360  \param bar_id scale bar id
361  \param sx,sy screen coordinates
362  \param size scale bar length
363  \param color scalebar/text color
364 
365  \return pointer to allocated scalebar_data structure
366  \return NULL when there's no surface
367  */
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 }
408 /*!
409  \brief Draws the Scale bar
410 
411  \param data nviz data
412  */
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 
428 /*!
429  \brief Deletes scale bar
430 
431  When scalebar is freed, array then contains NULL,
432  which must be tested during drawing.
433 
434  \param data nviz data
435  */
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 }
#define G_malloc(n)
Definition: defs/gis.h:112
#define BLU_MASK
Definition: ogsf.h:198
int Nviz_color_from_str(const char *color_str)
Get color value from color string (name or RGB triplet)
Definition: nviz.c:123
int cur_cplane
Definition: nviz.h:108
float size
Definition: nviz.h:97
#define MAX_CPLANES
Definition: ogsf.h:45
struct fringe_data ** fringe
Definition: nviz.h:117
#define RED_MASK
Definition: ogsf.h:196
#define MAX_LIGHTS
Definition: ogsf.h:44
void Nviz_delete_arrow(nv_data *data)
Deletes the North Arrow.
Definition: nviz.c:315
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
Definition: color_str.c:112
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
int id
Definition: nviz.h:80
int Nviz_new_light(nv_data *)
Define new light.
Definition: lights.c:164
float size
Definition: nviz.h:89
unsigned long color
Definition: nviz.h:96
int draw_arrow
Definition: nviz.h:120
#define NULL
Definition: ccmath.h:32
int num_cplanes
Definition: nviz.h:107
int Nviz_off_cplane(nv_data *, int)
Turn off (make inactive) the given clip plane.
Definition: cplanes_obj.c:63
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
float zrange
Definition: nviz.h:104
float elev
Definition: nviz.h:82
float xyrange
Definition: nviz.h:104
unsigned long color
Definition: nviz.h:81
void Nviz_draw_scalebar(nv_data *data)
Draws the Scale bar.
Definition: nviz.c:413
int * GS_get_surf_list(int *)
Get surface list.
Definition: gs2.c:1539
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
struct arrow_data * arrow
Definition: nviz.h:121
void GS_draw_fringe(int, unsigned long, float, int *)
Draw fringe around data (surface) at selected corners.
Definition: gs2.c:823
int num_scalebars
Definition: nviz.h:124
float where[3]
Definition: nviz.h:98
int num_fringes
Definition: nviz.h:116
void Nviz_destroy_data(nv_data *data)
Free allocated space by nv_data struct.
Definition: nviz.c:67
Definition: nviz.h:101
int Nviz_draw_arrow(nv_data *data)
Draws the North Arrow.
Definition: nviz.c:298
void G_warning(const char *,...) __attribute__((format(printf
void GS_set_light_reset(int)
Definition: gs2.c:252
int where[4]
Definition: nviz.h:83
int id
Definition: nviz.h:95
#define G_realloc(p, n)
Definition: defs/gis.h:114
#define _(str)
Definition: glocale.h:10
int gsd_scalebar_v2(float *, float, GLuint, unsigned long, unsigned long)
Draw Scalebar (as lines)
Definition: gsd_objs.c:1249
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
void GS_set_Narrow(int *, int, float *)
Set decoration, north arrow ??
Definition: gs2.c:568
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
struct scalebar_data ** scalebar
Definition: nviz.h:125
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
int Nviz_new_cplane(nv_data *, int)
Creates a clip plane object.
Definition: cplanes_obj.c:30
float where[3]
Definition: nviz.h:90
int bgcolor
Definition: nviz.h:128
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_surf_exists(int)
Definition: gs2.c:194
int gsd_north_arrow(float *, float, GLuint, unsigned long, unsigned long)
Draw North Arrow takes OpenGL coords and size.
Definition: gsd_objs.c:827
unsigned long color
Definition: nviz.h:88
#define GRN_MASK
Definition: ogsf.h:197
int GS_num_surfs(void)
Get number of surfaces.
Definition: gs2.c:1524