GRASS 8 Programmer's Manual 8.6.0dev(2026)-985dd2421b
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2008, 2010 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Updated/modified by Martin Landa <landa.martin gmail.com> (Google SoC
12 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 */
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
71 for (i = 0; i < data->num_fringes; i++) {
72 G_free(data->fringe[i]);
73 data->fringe[i] = NULL;
74 }
75 data->num_fringes = 0;
76 data->fringe = NULL;
77
78 if (data->arrow) {
79 G_free(data->arrow);
80 data->arrow = NULL;
81 data->draw_arrow = 0;
82 }
83
84 for (i = 0; i < data->num_scalebars; i++) {
85 G_free(data->scalebar[i]);
86 data->scalebar[i] = NULL;
87 }
88 data->num_scalebars = 0;
89 data->scalebar = NULL;
90}
91
92/*!
93 \brief Set background color
94
95 \param data nviz data
96 \param color color value
97 */
98void Nviz_set_bgcolor(nv_data *data, int color)
99{
100 data->bgcolor = color;
101
102 return;
103}
104
105/*!
106 \brief Get background color
107
108 \param data nviz data
109
110 \return color color value
111 */
113{
114 return data->bgcolor;
115}
116
117/*!
118 \brief Get color value from color string (name or RGB triplet)
119
120 \param color_str color string
121
122 \return color value
123 */
125{
126 int red, grn, blu;
127
128 if (G_str_to_color(color_str, &red, &grn, &blu) != 1) {
129 G_warning(_("Invalid color (%s), using \"white\" as default"),
130 color_str);
131 red = grn = blu = 255;
132 }
133
134 return (red & RED_MASK) + ((int)((grn) << 8) & GRN_MASK) +
135 ((int)((blu) << 16) & BLU_MASK);
136}
137
138/*! Add new fringe
139
140 \param data nviz data
141 \param id surface id
142 \param color color
143 \param elev fringe elevation
144 \param nw,ne,sw,se 1 (turn on) 0 (turn off)
145
146 \return pointer to allocated fringe_data structure
147 \return NULL on error
148 */
149struct fringe_data *Nviz_new_fringe(nv_data *data, int id, unsigned long color,
150 double elev, int nw, int ne, int sw, int se)
151{
152 int num;
153 int *surf = NULL;
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 (!surf) {
160 return NULL;
161 }
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 */
193struct 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 = NULL;
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 (!surf) {
204 return NULL;
205 }
206 id = surf[0];
207 G_free(surf);
208 }
209
210 for (i = 0; i < data->num_fringes; i++) {
211 f = data->fringe[i];
212 if (f->id == id) {
213 f->color = color;
214 f->elev = elev;
215 f->where[0] = nw;
216 f->where[1] = ne;
217 f->where[2] = sw;
218 f->where[3] = se;
219
220 return f;
221 }
222 }
223
224 f = Nviz_new_fringe(data, id, color, elev, nw, ne, sw, se);
225
226 return f;
227}
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/*!
245 \brief Sets the North Arrow position and return world coords
246
247 \param data nviz data
248 \param sx,sy screen coordinates
249 \param size arrow length
250 \param color arrow/text color
251 */
252int Nviz_set_arrow(nv_data *data, 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) {
262 id = surf_list[0];
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 \brief Draws the North Arrow
294
295 \param data nviz data
296 */
298{
299
300 struct arrow_data *arw = data->arrow;
301 GLuint FontBase = 0; /* don't know how to get fontbase */
302
303 data->draw_arrow = 1;
304 gsd_north_arrow(arw->where, arw->size, FontBase, arw->color, arw->color);
305
306 return 1;
307}
308
309/*!
310 \brief Deletes the North Arrow
311
312 \param data nviz data
313 */
315{
316 data->draw_arrow = 0;
317
318 return;
319}
320
321/*! Add new scalebar
322
323 \param data nviz data
324 \param bar_id scale bar id
325 \param coords real(?) coordinates
326 \param size scale bar length
327 \param color scalebar/text color
328
329 \return pointer to allocated scalebar_data structure
330 \return NULL on error
331 */
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 */
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) {
376 id = surf_list[0];
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
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 */
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:99
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
#define G_realloc(p, n)
Definition defs/gis.h:138
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:136
int Nviz_off_cplane(nv_data *, int)
Turn off (make inactive) the given clip plane.
Definition cplanes_obj.c:62
int Nviz_new_cplane(nv_data *, int)
Creates a clip plane object.
Definition cplanes_obj.c:30
int Nviz_new_light(nv_data *)
Define new light.
Definition lights.c:164
int GS_surf_exists(int)
Definition gs2.c:190
void GS_set_Narrow(int *, int, float *)
Set decoration, north arrow ??
Definition gs2.c:560
int GS_num_surfs(void)
Get number of surfaces.
Definition gs2.c:1513
int * GS_get_surf_list(int *)
Get surface list.
Definition gs2.c:1528
void GS_set_light_reset(int)
Definition gs2.c:246
int gsd_north_arrow(float *, float, GLuint, unsigned long, unsigned long)
Draw North Arrow.
Definition gsd_objs.c:847
int gsd_scalebar_v2(float *, float, GLuint, unsigned long, unsigned long)
Draw Scalebar (as lines)
Definition gsd_objs.c:1278
void GS_draw_fringe(int, unsigned long, float, int *)
Draw fringe around data (surface) at selected corners.
Definition gs2.c:816
#define _(str)
Definition glocale.h:10
int Nviz_get_bgcolor(nv_data *data)
Get background color.
Definition nviz.c:112
void Nviz_delete_arrow(nv_data *data)
Deletes the North Arrow.
Definition nviz.c:314
void Nviz_init_data(nv_data *data)
Initialize Nviz data.
Definition nviz.c:25
int Nviz_draw_arrow(nv_data *data)
Draws the North Arrow.
Definition nviz.c:297
void Nviz_delete_scalebar(nv_data *data, int bar_id)
Deletes scale bar.
Definition nviz.c:434
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
void Nviz_draw_scalebar(nv_data *data)
Draws the Scale bar.
Definition nviz.c:411
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:149
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:252
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
void Nviz_set_bgcolor(nv_data *data, int color)
Set background color.
Definition nviz.c:98
int Nviz_color_from_str(const char *color_str)
Get color value from color string (name or RGB triplet)
Definition nviz.c:124
void Nviz_destroy_data(nv_data *data)
Free allocated space by nv_data struct.
Definition nviz.c:67
struct scalebar_data * Nviz_new_scalebar(nv_data *data, int bar_id, float *coords, float size, unsigned int color)
Definition nviz.c:332
void Nviz_draw_fringe(nv_data *data)
Definition nviz.c:233
#define MAX_CPLANES
Definition ogsf.h:48
#define RED_MASK
Definition ogsf.h:201
#define BLU_MASK
Definition ogsf.h:203
#define GRN_MASK
Definition ogsf.h:202
#define MAX_LIGHTS
Definition ogsf.h:47
unsigned long color
Definition nviz.h:84
float size
Definition nviz.h:85
float elev
Definition nviz.h:79
unsigned long color
Definition nviz.h:78
int id
Definition nviz.h:77
int where[4]
Definition nviz.h:80
Definition nviz.h:96
struct scalebar_data ** scalebar
Definition nviz.h:119
int cur_cplane
Definition nviz.h:102
int num_cplanes
Definition nviz.h:101
int num_fringes
Definition nviz.h:110
float xyrange
Definition nviz.h:98
float zrange
Definition nviz.h:98
struct fringe_data ** fringe
Definition nviz.h:111
struct arrow_data * arrow
Definition nviz.h:115
int bgcolor
Definition nviz.h:122
int draw_arrow
Definition nviz.h:114
int num_scalebars
Definition nviz.h:118
float where[3]
Definition nviz.h:93
unsigned long color
Definition nviz.h:91
float size
Definition nviz.h:92