GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
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
13  2008/2010)
14  */
15 
16 #include <grass/colors.h>
17 #include <grass/raster.h>
18 #include <grass/glocale.h>
19 #include <grass/nviz.h>
20 
21 /*!
22  \brief Initialize Nviz data
23 
24  \param data nviz data
25  */
27 {
28  unsigned int i;
29 
30  /* data range */
31  data->zrange = 0;
32  data->xyrange = 0;
33 
34  /* clip planes, turn off by default */
35  data->num_cplanes = 0;
36  data->cur_cplane = 0;
37  for (i = 0; i < MAX_CPLANES; i++) {
38  Nviz_new_cplane(data, i);
39  Nviz_off_cplane(data, i);
40  }
41 
42  /* lights */
44 
45  for (i = 0; i < MAX_LIGHTS - 1; i++) {
46  Nviz_new_light(data);
47  }
48 
49  /* fringe */
50  data->num_fringes = 0;
51  data->fringe = NULL;
52 
53  /* north arrow */
54  data->draw_arrow = 0;
55  data->arrow = NULL;
56 
57  /* scale bar */
58  data->num_scalebars = 0;
59  data->scalebar = NULL;
60 
61  return;
62 }
63 
64 /*! \brief Free allocated space by nv_data struct
65 
66  \param data nviz data
67  */
69 {
70  int i;
71 
72  for (i = 0; i < data->num_fringes; i++) {
73  G_free(data->fringe[i]);
74  data->fringe[i] = NULL;
75  }
76  data->num_fringes = 0;
77  data->fringe = NULL;
78 
79  if (data->arrow) {
80  G_free(data->arrow);
81  data->arrow = NULL;
82  data->draw_arrow = 0;
83  }
84 
85  for (i = 0; i < data->num_scalebars; i++) {
86  G_free(data->scalebar[i]);
87  data->scalebar[i] = NULL;
88  }
89  data->num_scalebars = 0;
90  data->scalebar = NULL;
91 }
92 
93 /*!
94  \brief Set background color
95 
96  \param data nviz data
97  \param color color value
98  */
99 void Nviz_set_bgcolor(nv_data *data, int color)
100 {
101  data->bgcolor = color;
102 
103  return;
104 }
105 
106 /*!
107  \brief Get background color
108 
109  \param data nviz data
110 
111  \return color color value
112  */
114 {
115  return data->bgcolor;
116 }
117 
118 /*!
119  \brief Get color value from color string (name or RGB triplet)
120 
121  \param color_str color string
122 
123  \return color value
124  */
125 int Nviz_color_from_str(const char *color_str)
126 {
127  int red, grn, blu;
128 
129  if (G_str_to_color(color_str, &red, &grn, &blu) != 1) {
130  G_warning(_("Invalid color (%s), using \"white\" as default"),
131  color_str);
132  red = grn = blu = 255;
133  }
134 
135  return (red & RED_MASK) + ((int)((grn) << 8) & GRN_MASK) +
136  ((int)((blu) << 16) & BLU_MASK);
137 }
138 
139 /*! Add new fringe
140 
141  \param data nviz data
142  \param id surface id
143  \param color color
144  \param elev fringe elevation
145  \param nw,ne,sw,se 1 (turn on) 0 (turn off)
146 
147  \return pointer to allocated fringe_data structure
148  \return NULL on error
149  */
150 struct fringe_data *Nviz_new_fringe(nv_data *data, int id, unsigned long color,
151  double elev, int nw, int ne, int sw, int se)
152 {
153  int num;
154  int *surf;
155  struct fringe_data *f;
156 
157  if (!GS_surf_exists(id)) {
158  /* select first surface from the list */
159  surf = GS_get_surf_list(&num);
160  if (num < 1)
161  return NULL;
162  id = surf[0];
163  G_free(surf);
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(
176  data->fringe, data->num_fringes + 1 * sizeof(struct fringe_data *));
177  data->fringe[data->num_fringes++] = f;
178 
179  return f;
180 }
181 
182 /*! Set fringe
183 
184  \param data nviz data
185  \param id surface id
186  \param color color
187  \param elev fringe elevation
188  \param nw,ne,sw,se 1 (turn on) 0 (turn off)
189 
190  \return pointer to allocated fringe_data structure
191  \return NULL on error
192  */
193 struct fringe_data *Nviz_set_fringe(nv_data *data, 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, id, color, elev, nw, ne, sw, se);
224 
225  return f;
226 }
227 
228 /*! Draw fringe
229 
230  \param data nviz data
231  */
233 {
234  int i;
235 
236  for (i = 0; i < data->num_fringes; i++) {
237  struct fringe_data *f = data->fringe[i];
238 
239  GS_draw_fringe(f->id, f->color, f->elev, f->where);
240  }
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  */
251 int Nviz_set_arrow(nv_data *data, int sx, int sy, float size,
252  unsigned int color)
253 {
254  int id, pt[2];
255  int *surf_list, num_surfs;
256  float coords[3];
257  struct arrow_data *arw;
258 
259  if (GS_num_surfs() > 0) {
260  surf_list = GS_get_surf_list(&num_surfs);
261  id = surf_list[0];
262  G_free(surf_list);
263 
264  pt[0] = sx;
265  pt[1] = sy;
266 
267  GS_set_Narrow(pt, id, coords);
268 
269  if (data->arrow) {
270  data->arrow->color = color;
271  data->arrow->size = size;
272  data->arrow->where[0] = coords[0];
273  data->arrow->where[1] = coords[1];
274  data->arrow->where[2] = coords[2];
275  }
276  else {
277  arw = (struct arrow_data *)G_malloc(sizeof(struct arrow_data));
278  arw->color = color;
279  arw->size = size;
280  arw->where[0] = coords[0];
281  arw->where[1] = coords[1];
282  arw->where[2] = coords[2];
283 
284  data->arrow = arw;
285  }
286  return 1;
287  }
288  return 0;
289 }
290 
291 /*!
292  \brief Draws the North Arrow
293 
294  \param data nviz data
295  */
297 {
298 
299  struct arrow_data *arw = data->arrow;
300  GLuint FontBase = 0; /* don't know how to get fontbase */
301 
302  data->draw_arrow = 1;
303  gsd_north_arrow(arw->where, arw->size, FontBase, arw->color, arw->color);
304 
305  return 1;
306 }
307 
308 /*!
309  \brief Deletes the North Arrow
310 
311  \param data nviz data
312  */
314 {
315  data->draw_arrow = 0;
316 
317  return;
318 }
319 
320 /*! Add new scalebar
321 
322  \param data nviz data
323  \param bar_id scale bar id
324  \param coords real(?) coordinates
325  \param size scale bar length
326  \param color scalebar/text color
327 
328  \return pointer to allocated scalebar_data structure
329  \return NULL on error
330  */
331 
332 struct scalebar_data *Nviz_new_scalebar(nv_data *data, int bar_id,
333  float *coords, float size,
334  unsigned int color)
335 {
336  struct scalebar_data *s;
337 
338  s = (struct scalebar_data *)G_malloc(sizeof(struct scalebar_data));
339  s->id = bar_id;
340  s->color = color;
341  s->size = size;
342  s->where[0] = coords[0];
343  s->where[1] = coords[1];
344  s->where[2] = coords[2];
345 
346  data->scalebar = (struct scalebar_data **)G_realloc(
347  data->scalebar,
348  (data->num_scalebars + 1) * sizeof(struct scalebar_data *));
349  data->scalebar[data->num_scalebars++] = s;
350 
351  return s;
352 }
353 
354 /*!
355  \brief Sets the scale bar position and return world coords
356 
357  \param data nviz data
358  \param bar_id scale bar id
359  \param sx,sy screen coordinates
360  \param size scale bar length
361  \param color scalebar/text color
362 
363  \return pointer to allocated scalebar_data structure
364  \return NULL when there's no surface
365  */
366 struct scalebar_data *Nviz_set_scalebar(nv_data *data, int bar_id, int sx,
367  int sy, float size, unsigned int color)
368 {
369  int i, id, pt[2];
370  int *surf_list, num_surfs;
371  float coords[3];
372  struct scalebar_data *s;
373 
374  if (GS_num_surfs() > 0) {
375  surf_list = GS_get_surf_list(&num_surfs);
376  id = surf_list[0];
377  G_free(surf_list);
378 
379  pt[0] = sx;
380  pt[1] = sy;
381 
382  GS_set_Narrow(pt, id, coords); /* the same like arrow */
383 
384  for (i = 0; i < data->num_scalebars; i++) {
385  if (data->scalebar[i]) {
386  s = data->scalebar[i];
387  if (s->id == bar_id) {
388  s->color = color;
389  s->size = size;
390  s->where[0] = coords[0];
391  s->where[1] = coords[1];
392  s->where[2] = coords[2];
393 
394  return s;
395  }
396  }
397  }
398 
399  s = Nviz_new_scalebar(data, bar_id, coords, size, color);
400 
401  return s;
402  }
403  return NULL;
404 }
405 
406 /*!
407  \brief Draws the Scale bar
408 
409  \param data nviz data
410  */
412 {
413  int i;
414 
415  GLuint FontBase = 0; /* don't know how to get fontbase */
416 
417  for (i = 0; i < data->num_scalebars; i++) {
418  if (data->scalebar[i]) {
419  struct scalebar_data *s = data->scalebar[i];
420 
421  gsd_scalebar_v2(s->where, s->size, FontBase, s->color, s->color);
422  }
423  }
424 }
425 
426 /*!
427  \brief Deletes scale bar
428 
429  When scalebar is freed, array then contains NULL,
430  which must be tested during drawing.
431 
432  \param data nviz data
433  */
434 void Nviz_delete_scalebar(nv_data *data, int bar_id)
435 {
436  if (bar_id < data->num_scalebars && data->scalebar[bar_id] != NULL) {
437  G_free(data->scalebar[bar_id]);
438  data->scalebar[bar_id] = NULL;
439  }
440 }
#define NULL
Definition: ccmath.h:32
int G_str_to_color(const char *, int *, int *, int *)
Parse color string and set red,green,blue.
Definition: color_str.c:101
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_realloc(p, n)
Definition: defs/gis.h:96
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition: defs/gis.h:94
int Nviz_off_cplane(nv_data *, int)
Turn off (make inactive) the given clip plane.
Definition: cplanes_obj.c:63
int Nviz_new_cplane(nv_data *, int)
Creates a clip plane object.
Definition: cplanes_obj.c:31
int Nviz_new_light(nv_data *)
Define new light.
Definition: lights.c:165
int GS_surf_exists(int)
Definition: gs2.c:194
int * GS_get_surf_list(int *)
Get surface list.
Definition: gs2.c:1530
void GS_set_Narrow(int *, int, float *)
Set decoration, north arrow ??
Definition: gs2.c:564
int GS_num_surfs(void)
Get number of surfaces.
Definition: gs2.c:1515
void GS_set_light_reset(int)
Definition: gs2.c:250
int gsd_north_arrow(float *, float, GLuint, unsigned long, unsigned long)
Draw North Arrow takes OpenGL coords and size.
Definition: gsd_objs.c:803
int gsd_scalebar_v2(float *, float, GLuint, unsigned long, unsigned long)
void GS_draw_fringe(int, unsigned long, float, int *)
Draw fringe around data (surface) at selected corners.
Definition: gs2.c:818
#define _(str)
Definition: glocale.h:10
int Nviz_get_bgcolor(nv_data *data)
Get background color.
Definition: nviz.c:113
void Nviz_delete_arrow(nv_data *data)
Deletes the North Arrow.
Definition: nviz.c:313
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:150
void Nviz_init_data(nv_data *data)
Initialize Nviz data.
Definition: nviz.c:26
int Nviz_draw_arrow(nv_data *data)
Draws the North Arrow.
Definition: nviz.c:296
void Nviz_delete_scalebar(nv_data *data, int bar_id)
Deletes scale bar.
Definition: nviz.c:434
void Nviz_draw_scalebar(nv_data *data)
Draws the Scale bar.
Definition: nviz.c:411
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:366
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:193
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
void Nviz_set_bgcolor(nv_data *data, int color)
Set background color.
Definition: nviz.c:99
int Nviz_color_from_str(const char *color_str)
Get color value from color string (name or RGB triplet)
Definition: nviz.c:125
void Nviz_destroy_data(nv_data *data)
Free allocated space by nv_data struct.
Definition: nviz.c:68
void Nviz_draw_fringe(nv_data *data)
Definition: nviz.c:232
struct scalebar_data * Nviz_new_scalebar(nv_data *data, int bar_id, float *coords, float size, unsigned int color)
Definition: nviz.c:332
#define MAX_CPLANES
Definition: ogsf.h:47
#define RED_MASK
Definition: ogsf.h:200
#define BLU_MASK
Definition: ogsf.h:202
#define GRN_MASK
Definition: ogsf.h:201
#define MAX_LIGHTS
Definition: ogsf.h:46
float where[3]
Definition: nviz.h:87
unsigned long color
Definition: nviz.h:85
float size
Definition: nviz.h:86
float elev
Definition: nviz.h:80
unsigned long color
Definition: nviz.h:79
int id
Definition: nviz.h:78
int where[4]
Definition: nviz.h:81
Definition: nviz.h:97
struct scalebar_data ** scalebar
Definition: nviz.h:120
int cur_cplane
Definition: nviz.h:103
int num_cplanes
Definition: nviz.h:102
int num_fringes
Definition: nviz.h:111
float xyrange
Definition: nviz.h:99
float zrange
Definition: nviz.h:99
struct fringe_data ** fringe
Definition: nviz.h:112
struct arrow_data * arrow
Definition: nviz.h:116
int bgcolor
Definition: nviz.h:123
int draw_arrow
Definition: nviz.h:115
int num_scalebars
Definition: nviz.h:119
float where[3]
Definition: nviz.h:94
int id
Definition: nviz.h:91
unsigned long color
Definition: nviz.h:92
float size
Definition: nviz.h:93