GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
map_obj.c
Go to the documentation of this file.
1/*!
2 \file lib/nviz/map_obj.c
3
4 \brief Nviz library -- Define creation and interface functions for map
5 objects.
6
7 Map objects are considered to be surfaces, vector plots, or site
8 files.
9
10 Based on visualization/nviz/src/map_obj.c
11
12 SPDX-FileCopyrightText: 2008, 2010 GRASS Development Team
13 SPDX-License-Identifier: GPL-2.0-or-later
14
15 \author Updated/modified by Martin Landa <landa.martin gmail.com> (Google SoC
16 2008/2010)
17 */
18
19#include <stdlib.h>
20#include <time.h>
21
22#include <grass/glocale.h>
23#include <grass/nviz.h>
24
25/*!
26 \brief Create a new map object which can be one of surf, vect, vol or site.
27
28 This routine creates the object internally in the gsf libraryb.
29 Optionally, a logical name may be specified for the new map object.
30 If no name is specified, a logical name is assigned to the new
31 object automatically. Note that maintaining unique logical names is
32 not the responsibility of the library (currently).
33
34 Initially map objects contain no data, use the attribute commands to
35 set attributes such as topology, color, etc.
36
37 \param type map object type
38 \param name map name (NULL for constant)
39 \param value constant (used if <i>name</i> is NULL)
40 \param data nviz data
41
42 \return map object id
43 \return -1 on error
44 */
45int Nviz_new_map_obj(int type, const char *name, double value, nv_data *data)
46{
47 int new_id, i;
48 int num_surfs, *surf_list;
49
50 /*
51 * For each type of map obj do the following --
52 * 1) Verify we haven't maxed out the number of
53 * allowed objects.
54 * 2) Call the internal library to generate a new
55 * map object of the specified type.
56 */
57 /* raster -> surface */
58 if (type == MAP_OBJ_SURF) {
59 if (GS_num_surfs() >= MAX_SURFS) {
60 G_warning(_("Maximum surfaces loaded!"));
61 return -1;
62 }
63
65
66 if (new_id < 0) {
67 return -1;
68 }
69
70 if (name) {
71 /* map */
73 -1.0, data)) {
74 return -1;
75 }
76 }
77 else {
78 /* constant */
80 value, data)) {
81 return -1;
82 }
83 }
84 }
85 /* vector overlay */
86 else if (type == MAP_OBJ_VECT) {
87 if (GV_num_vects() >= MAX_VECTS) {
88 G_warning(_("Maximum vector line maps loaded!"));
89 return -1;
90 }
91
93
94 if (name) {
95 if (GV_load_vector(new_id, name) < 0) {
97 G_warning(_("Error loading vector map <%s>"), name);
98 return -1;
99 }
100 }
101
102 /* initialize display parameters
103 automatically select all surfaces to draw vector */
104 GV_set_style(new_id, 1, 0x000000, 2, 0);
106 if (num_surfs) {
107 for (i = 0; i < num_surfs; i++) {
109 }
110 }
112 }
113 /* vector points overlay */
114 else if (type == MAP_OBJ_SITE) {
115 if (GP_num_sites() >= MAX_SITES) {
116 G_warning(_("Maximum vector point maps loaded!"));
117 return -1;
118 }
119
121
122 /* initizalize site attributes */
124
125 /* load vector points */
126 if (0 > GP_load_site(new_id, name)) {
128 G_warning(_("Error loading vector map <%s>"), name);
129 return -1;
130 }
131
132 /* initialize display parameters */
133 GP_set_style(new_id, 0x000000, 2, 100, ST_X);
135 for (i = 0; i < num_surfs; i++) {
137 }
139 }
140 /* 3d raster map -> volume */
141 else if (type == MAP_OBJ_VOL) {
142 if (GVL_num_vols() >= MAX_VOLS) {
143 G_warning(_("Maximum volumes loaded!"));
144 return -1;
145 }
146
148
149 /* load volume */
150 if (0 > GVL_load_vol(new_id, name)) {
152 G_warning(_("Error loading 3d raster map <%s>"), name);
153 return -1;
154 }
155
156 /* initilaze volume attributes */
158 }
159 else {
160 G_warning(_("Nviz_new_map_obj(): unsupported data type"));
161 return -1;
162 }
163
164 return new_id;
165}
166
167/*!
168 Set map object attribute
169
170 \param id map object id
171 \param type map object type (MAP_OBJ_SURF, MAP_OBJ_VECT, ...)
172 \param desc attribute descriptor
173 \param src attribute source
174 \param str_value attribute value as string (if NULL, check for
175 <i>num_value</i>)
176 \param num_value attribute value as double
177
178 \return 1 on success
179 \return 0 on failure
180 */
181int Nviz_set_attr(int id, int type, int desc, int src, const char *str_value,
182 double num_value, nv_data *data)
183{
184 int ret;
185 double value;
186
187 switch (type) {
188 case (MAP_OBJ_SURF): {
189 /* Basically two cases, either we are setting to a constant field, or
190 * we are loading an actual file. Setting a constant is the easy part
191 * so we try and do that first.
192 */
193 if (src == CONST_ATT) {
194 /* Get the value for the constant
195 * Note that we require the constant to be an integer
196 */
197 if (str_value)
198 value = (double)atof(str_value);
199 else
200 value = num_value;
201
202 /* Only special case is setting constant color.
203 * In this case we have to decode the constant Tcl
204 * returns so that the gsf library understands it.
205 */
206 if (desc == ATT_COLOR) {
207 /* TODO check this - sometimes gets reversed when save state
208 saves a surface with constant color
209
210 int r, g, b;
211 r = (((int) value) & RED_MASK) >> 16;
212 g = (((int) value) & GRN_MASK) >> 8;
213 b = (((int) value) & BLU_MASK);
214 value = r + (g << 8) + (b << 16);
215 */
216 }
217
218 /* Once the value is parsed, set it */
219 ret = GS_set_att_const(id, desc, value);
220 }
221 else if (src == MAP_ATT) {
222 ret = GS_load_att_map(id, str_value, desc);
223 }
224 else
225 ret = -1;
226
227 /* After we've loaded a constant map or a file,
228 * may need to adjust resolution if we are resetting
229 * topology (for example)
230 */
231 if (0 <= ret) {
232 if (desc == ATT_TOPO) {
233 int rows, cols, max;
234 int max2;
235
236 /* If topology attribute is being set then need to set
237 * resolution of incoming map to some sensible value so we
238 * don't wait all day for drawing.
239 */
240 GS_get_dims(id, &rows, &cols);
241 max = (rows > cols) ? rows : cols;
242 max = max / 50;
243 if (max < 1)
244 max = 1;
245 max2 = max / 5;
246 if (max2 < 1)
247 max2 = 1;
248 /* reset max to finer for coarse surf drawing */
249 max = max2 + max2 / 2;
250 if (max < 1)
251 max = 1;
252
255 }
256
257 /* Not sure about this next line, should probably just
258 * create separate routines to figure the Z range as well
259 * as the XYrange
260 */
261 Nviz_update_ranges(data);
262
263 break;
264 }
266 default: {
267 return 0;
268 }
269 }
270 }
271
272 return 1;
273}
274
275/*!
276 \brief Set default surface attributes
277 */
279{
280 float defs[MAX_ATTS];
281
282 defs[ATT_TOPO] = 0;
284 defs[ATT_MASK] = 0;
285 defs[ATT_TRANSP] = 0;
286 defs[ATT_SHINE] = 60;
287 defs[ATT_EMIT] = 0;
288
290
291 return;
292}
293
294/*!
295 \brief Set default vector point attributes
296
297 \param id vector point set id
298
299 \return 1 on success
300 \return 0 on failure
301 */
303{
304 geosite *gp;
305
306 gp = gp_get_site(id);
307
308 if (!gp)
309 return 0;
310
311 return 1;
312}
313
314/*!
315 \brief Set default volume attributes
316
317 \param id volume set id
318
319 \return 1 on success
320 \return 0 on failure
321 */
323{
324 int rows, cols, depths;
325 int max;
326
327 GVL_get_dims(id, &rows, &cols, &depths);
328 max = (rows > cols) ? rows : cols;
329 max = (depths > max) ? depths : max;
330 max = max / 35;
331 if (max < 1)
332 max = 1;
333
334 if (max > cols)
335 max = cols / 2;
336 if (max > rows)
337 max = rows / 2;
338 if (max > depths)
339 max = depths / 2;
340
341 /* set default drawres and drawmode for isosurfaces */
344
345 /* set default drawres and drawmode for slices */
346 GVL_slice_set_drawres(id, 1, 1, 1);
348
349 return 1;
350}
351
352/*!
353 Unset map object attribute
354
355 \param id map object id
356 \param type map object type (MAP_OBJ_SURF, MAP_OBJ_VECT, ...)
357 \param desc attribute descriptor
358
359 \return 1 on success
360 \return 0 on failure
361 */
362int Nviz_unset_attr(int id, int type, int desc)
363{
364 if (type == MAP_OBJ_SURF) {
365 return GS_unset_att(id, desc);
366 }
367
368 return 0;
369}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void G_warning(const char *,...) __attribute__((format(printf
int Nviz_update_ranges(nv_data *)
Update ranges.
Definition change_view.c:60
int GVL_delete_vol(int)
Delete volume set from list.
Definition gvl2.c:206
int GS_new_surface(void)
Add new surface.
Definition gs2.c:219
int GP_delete_site(int)
Delete registered point set.
Definition gp2.c:130
void GVL_get_dims(int, int *, int *, int *)
Get volume dimensions.
Definition gvl2.c:305
int GP_new_site(void)
Create new point set.
Definition gp2.c:62
int GV_new_vector(void)
Register new vector set.
Definition gv2.c:59
int GV_delete_vector(int)
Delete vector set from list.
Definition gv2.c:127
int GS_load_att_map(int, const char *, int)
Load raster map as attribute.
Definition gs2.c:1596
int GVL_load_vol(int, const char *)
Load 3d raster map to volume set.
Definition gvl2.c:252
int GP_load_site(int, const char *)
Load point set from file.
Definition gp2.c:171
int GP_select_surf(int, int)
Select surface for given point set.
Definition gp2.c:486
void GS_set_att_defaults(float *, float *)
Set default attributes for map objects.
Definition gs2.c:168
int GVL_isosurf_set_drawmode(int, int)
Set isosurface draw mode.
Definition gvl2.c:638
int GV_load_vector(int, const char *)
Load vector set.
Definition gv2.c:170
int GS_set_drawres(int, int, int, int, int)
Set draw resolution for surface.
Definition gs2.c:2218
int GVL_slice_set_drawmode(int, int)
Set slice draw mode.
Definition gvl2.c:1171
int GS_set_att_const(int, int, float)
Set attribute constant.
Definition gs2.c:1401
int GP_set_style(int, int, int, float, int)
Set point style.
Definition gp2.c:276
void GS_get_dims(int, int *, int *)
Get dimension of surface.
Definition gs2.c:2276
int GV_num_vects(void)
Get number of available vector sets.
Definition gv2.c:82
int GS_num_surfs(void)
Get number of surfaces.
Definition gs2.c:1513
int GVL_num_vols(void)
Get number of loaded volume sets.
Definition gvl2.c:162
int * GS_get_surf_list(int *)
Get surface list.
Definition gs2.c:1528
int GV_select_surf(int, int)
Select surface identified by hs to have vector identified by hv draped over it.
Definition gv2.c:391
int GVL_isosurf_set_drawres(int, int, int, int)
Set isosurface draw resolution.
Definition gvl2.c:577
int GS_unset_att(int, int)
Unset attribute.
Definition gs2.c:1382
int GVL_slice_set_drawres(int, int, int, int)
Set slice draw resolution.
Definition gvl2.c:1110
int GV_set_style(int, int, int, int, int)
Set vector style.
Definition gv2.c:227
int GP_num_sites(void)
Get number of loaded point sets.
Definition gp2.c:85
int GVL_new_vol(void)
Create new volume set.
Definition gvl2.c:132
geosite * gp_get_site(int)
Get geosite struct.
Definition gp.c:31
int GS_set_drawmode(int, int)
Set draw mode.
Definition gs2.c:2080
#define max(x, y)
Definition draw2.c:30
#define G_FALLTHROUGH
A macro for a fallthrough statement attribute.
Definition gis.h:56
#define _(str)
Definition glocale.h:10
int Nviz_set_attr(int id, int type, int desc, int src, const char *str_value, double num_value, nv_data *data)
Definition map_obj.c:181
int Nviz_set_vpoint_attr_default(int id)
Set default vector point attributes.
Definition map_obj.c:302
int Nviz_new_map_obj(int type, const char *name, double value, nv_data *data)
Create a new map object which can be one of surf, vect, vol or site.
Definition map_obj.c:45
int Nviz_set_volume_attr_default(int id)
Set default volume attributes.
Definition map_obj.c:322
void Nviz_set_surface_attr_default(void)
Set default surface attributes.
Definition map_obj.c:278
int Nviz_unset_attr(int id, int type, int desc)
Definition map_obj.c:362
const char * name
Definition named_colr.c:6
#define MAP_OBJ_SITE
Definition nviz.h:45
#define MAP_OBJ_VOL
Definition nviz.h:43
#define DEFAULT_SURF_COLOR
Definition nviz.h:62
#define MAP_OBJ_VECT
Definition nviz.h:44
#define MAP_OBJ_SURF
Definition nviz.h:42
#define DM_GRID_SURF
Definition ogsf.h:68
#define ATT_MASK
Definition ogsf.h:78
#define MAX_ATTS
Definition ogsf.h:46
#define ATT_TOPO
Definition ogsf.h:76
#define ATT_COLOR
Definition ogsf.h:77
#define MAX_SITES
Definition ogsf.h:43
#define ATT_EMIT
Definition ogsf.h:81
#define ATT_SHINE
Definition ogsf.h:80
#define MAP_ATT
Definition ogsf.h:86
#define MAX_SURFS
Definition ogsf.h:41
#define MAX_VOLS
Definition ogsf.h:44
#define DM_POLY
Definition ogsf.h:64
#define DM_GOURAUD
Definition ogsf.h:57
#define CONST_ATT
Definition ogsf.h:87
#define ATT_TRANSP
Definition ogsf.h:79
#define ST_X
Definition ogsf.h:93
#define MAX_VECTS
Definition ogsf.h:42
Vector map (points)
Definition ogsf.h:416
Definition nviz.h:96