GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 }
int GV_load_vector(int id, const char *filename)
Load vector set.
Definition: GV2.c:174
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int GS_unset_att(int id, int att)
Unset attribute.
Definition: GS2.c:1390
int GVL_delete_vol(int id)
Delete volume set from list.
Definition: GVL2.c:190
int GVL_new_vol(void)
Create new volume set.
Definition: GVL2.c:116
string name
Definition: render.py:1314
void GVL_get_dims(int id, int *rows, int *cols, int *depths)
Get volume dimensions.
Definition: GVL2.c:290
int GS_new_surface(void)
Add new surface.
Definition: GS2.c:224
int GVL_num_vols(void)
Get number of loaded volume sets.
Definition: GVL2.c:146
int Nviz_update_ranges(nv_data *dc)
Update ranges.
Definition: change_view.c:60
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:180
#define max(x, y)
Definition: draw2.c:69
void GS_set_att_defaults(float *defs, float *null_defs)
Set default attributes for map objects.
Definition: GS2.c:172
int GS_set_drawmode(int id, int mode)
Set draw mode.
Definition: GS2.c:2080
int Nviz_set_volume_attr_default(int id)
Set default volume attributes.
Definition: map_obj.c:322
int GP_set_sitemode(int id, int atmod, int color, int width, float size, int marker)
Set point set mode.
Definition: GP2.c:254
geosite * gp_get_site(int id)
Get geosite struct.
Definition: gp.c:36
int GVL_load_vol(int id, const char *filename)
Load 3d raster map to volume set.
Definition: GVL2.c:236
tuple data
int GVL_isosurf_set_drawmode(int id, int mode)
Set isosurface draw mode.
Definition: GVL2.c:593
int GP_new_site(void)
Create new point set.
Definition: GP2.c:63
int GV_delete_vector(int id)
Delete vector set from list.
Definition: GV2.c:131
int GVL_slice_set_drawmode(int id, int mode)
Set slice draw mode.
Definition: GVL2.c:1122
int GS_set_att_const(int id, int att, float constant)
Set attribute constant.
Definition: GS2.c:1409
int Nviz_unset_attr(int id, int type, int desc)
Definition: map_obj.c:362
int GS_set_drawres(int id, int xres, int yres, int xwire, int ywire)
Set draw resolution for surface.
Definition: GS2.c:2218
void GS_get_dims(int id, int *rows, int *cols)
Get dimension of surface.
Definition: GS2.c:2276
char * value
Definition: env.c:30
int Nviz_set_vpoint_attr_default(int id)
Set default vector point attributes.
Definition: map_obj.c:298
int GV_num_vects(void)
Get number of available vector sets.
Definition: GV2.c:86
int GS_num_surfs(void)
Get number of surfaces.
Definition: GS2.c:1521
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
tuple cols
void Nviz_set_surface_attr_default()
Set default surface attributes.
Definition: map_obj.c:274
int GP_delete_site(int id)
Delete registrated point set.
Definition: GP2.c:131
int GV_new_vector(void)
Register new vector set.
Definition: GV2.c:63
int * GS_get_surf_list(int *numsurfs)
Get surface list.
Definition: GS2.c:1536
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:44
int GVL_isosurf_set_drawres(int id, int xres, int yres, int zres)
Set isosurface draw resolution.
Definition: GVL2.c:532
int GP_num_sites(void)
Get number of loaded point sets.
Definition: GP2.c:86
int GS_load_att_map(int id, const char *filename, int att)
Load raster map as attribute.
Definition: GS2.c:1605
int GP_select_surf(int hp, int hs)
Select surface.
Definition: GP2.c:433
int GV_select_surf(int hv, int hs)
Select surface identified by hs to have vector identified by hv draped over it.
Definition: GV2.c:331
int GP_load_site(int id, const char *filename)
Load point set from file.
Definition: GP2.c:173
int GVL_slice_set_drawres(int id, int xres, int yres, int zres)
Set slice draw resolution.
Definition: GVL2.c:1061
int GV_set_vectmode(int id, int mem, int color, int width, int flat)
Set vector set mode.
Definition: GV2.c:231