GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
map_obj.c
Go to the documentation of this file.
1 
18 #include <stdlib.h>
19 #include <time.h>
20 
21 #include <grass/glocale.h>
22 #include <grass/nviz.h>
23 
44 int Nviz_new_map_obj(int type, const char *name, double value, nv_data * data)
45 {
46  int new_id, i;
47  int num_surfs, *surf_list;
48 
49  /*
50  * For each type of map obj do the following --
51  * 1) Verify we havn't maxed out the number of
52  * allowed objects.
53  * 2) Call the internal library to generate a new
54  * map object of the specified type.
55  */
56  /* raster -> surface */
57  if (type == MAP_OBJ_SURF) {
58  if (GS_num_surfs() >= MAX_SURFS) {
59  G_warning(_("Maximum surfaces loaded!"));
60  return -1;
61  }
62 
63  new_id = GS_new_surface();
64 
65  if (new_id < 0) {
66  return -1;
67  }
68 
69  if (name) {
70  /* map */
71  if (!Nviz_set_attr(new_id, MAP_OBJ_SURF, ATT_TOPO,
72  MAP_ATT, name, -1.0, data)) {
73  return -1;
74  }
75  }
76  else {
77  /* constant */
78  if (!Nviz_set_attr(new_id, MAP_OBJ_SURF, ATT_TOPO,
79  CONST_ATT, NULL, value,
80  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 
92  new_id = GV_new_vector();
93 
94  if (name) {
95  if (GV_load_vector(new_id, name) < 0) {
96  GV_delete_vector(new_id);
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_vectmode(new_id, 1, 0xFF0000, 2, 0);
105  surf_list = GS_get_surf_list(&num_surfs);
106  if (num_surfs) {
107  for (i = 0; i < num_surfs; i++) {
108  GV_select_surf(new_id, surf_list[i]);
109  }
110  }
111  G_free(surf_list);
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 
120  new_id = GP_new_site();
121 
122  /* initizalize site attributes */
124 
125  /* load vector points */
126  if (0 > GP_load_site(new_id, name)) {
127  GP_delete_site(new_id);
128  G_warning(_("Error loading vector map <%s>"), name);
129  return -1;
130  }
131 
132  /* initialize display parameters */
133  GP_set_sitemode(new_id, ST_ATT_NONE, 0xFF0000, 2, 100, ST_X);
134  surf_list = GS_get_surf_list(&num_surfs);
135  for (i = 0; i < num_surfs; i++) {
136  GP_select_surf(new_id, surf_list[i]);
137  }
138  G_free(surf_list);
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 
147  new_id = GVL_new_vol();
148 
149  /* load volume */
150  if (0 > GVL_load_vol(new_id, name)) {
151  GVL_delete_vol(new_id);
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 
180 int Nviz_set_attr(int id, int type, int desc, int src,
181  const char *str_value, double num_value, nv_data * data)
182 {
183  int ret;
184  double value;
185 
186  switch (type) {
187  case (MAP_OBJ_SURF):{
188  /* Basically two cases, either we are setting to a constant field, or
189  * we are loading an actual file. Setting a constant is the easy part
190  * so we try and do that first.
191  */
192  if (src == CONST_ATT) {
193  /* Get the value for the constant
194  * Note that we require the constant to be an integer
195  */
196  if (str_value)
197  value = (double)atof(str_value);
198  else
199  value = num_value;
200 
201  /* Only special case is setting constant color.
202  * In this case we have to decode the constant Tcl
203  * returns so that the gsf library understands it.
204  */
205  if (desc == ATT_COLOR) {
206  /* TODO check this - sometimes gets reversed when save state
207  saves a surface with constant color
208 
209  int r, g, b;
210  r = (((int) value) & RED_MASK) >> 16;
211  g = (((int) value) & GRN_MASK) >> 8;
212  b = (((int) value) & BLU_MASK);
213  value = r + (g << 8) + (b << 16);
214  */
215  }
216 
217  /* Once the value is parsed, set it */
218  ret = GS_set_att_const(id, desc, value);
219  }
220  else if (src == MAP_ATT) {
221  ret = GS_load_att_map(id, str_value, desc);
222  }
223 
224  /* After we've loaded a constant map or a file,
225  * may need to adjust resolution if we are resetting
226  * topology (for example)
227  */
228  if (0 <= ret) {
229  if (desc == ATT_TOPO) {
230  int rows, cols, max;
231  int max2;
232 
233  /* If topology attribute is being set then need to set
234  * resolution of incoming map to some sensible value so we
235  * don't wait all day for drawing.
236  */
237  GS_get_dims(id, &rows, &cols);
238  max = (rows > cols) ? rows : cols;
239  max = max / 50;
240  if (max < 1)
241  max = 1;
242  max2 = max / 5;
243  if (max2 < 1)
244  max2 = 1;
245  /* reset max to finer for coarse surf drawing */
246  max = max2 + max2 / 2;
247  if (max < 1)
248  max = 1;
249 
250  GS_set_drawres(id, max2, max2, max, max);
251  GS_set_drawmode(id, DM_GOURAUD | DM_POLY | DM_GRID_SURF);
252  }
253 
254  /* Not sure about this next line, should probably just
255  * create separate routines to figure the Z range as well
256  * as the XYrange
257  */
258  Nviz_update_ranges(data);
259 
260  break;
261  }
262  default:{
263  return 0;
264  }
265  }
266  }
267 
268  return 1;
269 }
270 
275 {
276  float defs[MAX_ATTS];
277 
278  defs[ATT_TOPO] = 0;
279  defs[ATT_COLOR] = DEFAULT_SURF_COLOR;
280  defs[ATT_MASK] = 0;
281  defs[ATT_TRANSP] = 0;
282  defs[ATT_SHINE] = 60;
283  defs[ATT_EMIT] = 0;
284 
285  GS_set_att_defaults(defs, defs);
286 
287  return;
288 }
289 
299 {
300  int i;
301  geosite *gp;
302 
303  gp = gp_get_site(id);
304 
305  if (!gp)
306  return 0;
307 
308  for (i = 0; i < GPT_MAX_ATTR; i++)
309  gp->use_attr[i] = ST_ATT_NONE;
310 
311  return 1;
312 }
313 
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 */
342  GVL_isosurf_set_drawres(id, max, max, max);
343  GVL_isosurf_set_drawmode(id, DM_GOURAUD);
344 
345  /* set default drawres and drawmode for slices */
346  GVL_slice_set_drawres(id, 1, 1, 1);
347  GVL_slice_set_drawmode(id, DM_GOURAUD | DM_POLY);
348 
349  return 1;
350 }
351 
362 int 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 }