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