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