GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gv.c
Go to the documentation of this file.
1 
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include <grass/gstypes.h>
23 #include "gsget.h"
24 
25 #define FIRST_VECT_ID 20656
26 
27 static geovect *Vect_top = NULL;
28 
37 geovect *gv_get_vect(int id)
38 {
39  geovect *gv;
40 
41  G_debug(5, "gv_get_vect() id=%d", id);
42 
43  for (gv = Vect_top; gv; gv = gv->next) {
44  if (gv->gvect_id == id) {
45  return (gv);
46  }
47  }
48 
49  return (NULL);
50 }
51 
60 geovect *gv_get_prev_vect(int id)
61 {
62  geovect *pv;
63 
64  G_debug(5, "gv_get_prev_vect(): id=%d", id);
65 
66  for (pv = Vect_top; pv; pv = pv->next) {
67  if (pv->gvect_id == id - 1) {
68  return (pv);
69  }
70  }
71 
72  return (NULL);
73 }
74 
80 int gv_num_vects(void)
81 {
82  geovect *gv;
83  int i;
84 
85  for (i = 0, gv = Vect_top; gv; gv = gv->next, i++) ;
86 
87  G_debug(5, "gv_num_vects(): num=%d", i);
88 
89  return (i);
90 }
91 
98 geovect *gv_get_last_vect(void)
99 {
100  geovect *lv;
101 
102  if (!Vect_top) {
103  return (NULL);
104  }
105 
106  for (lv = Vect_top; lv->next; lv = lv->next) ;
107 
108  G_debug(5, "gv_get_last_vect(): id=%d", lv->gvect_id);
109 
110  return (lv);
111 }
112 
119 geovect *gv_get_new_vect(void)
120 {
121  geovect *nv, *lv;
122 
123  nv = (geovect *) G_malloc(sizeof(geovect));
124  if (!nv) {
125  /* G_fatal_error */
126  return (NULL);
127  }
128 
129  if ((lv = gv_get_last_vect())) {
130  lv->next = nv;
131  nv->gvect_id = lv->gvect_id + 1;
132  }
133  else {
134  Vect_top = nv;
135  nv->gvect_id = FIRST_VECT_ID;
136  }
137 
138  nv->next = NULL;
139 
140  G_debug(5, "gv_get_new_vect() id=%d", nv->gvect_id);
141 
142  return (nv);
143 }
144 
151 {
152  geovect *gv;
153  int i, j;
154 
155  for (gv = Vect_top; gv; gv = gv->next) {
156  if (gv->n_surfs) {
157  for (i = 0; i < gv->n_surfs; i++) {
158  if (gv->drape_surf_id[i]) {
159  if (NULL == gs_get_surf(gv->drape_surf_id[i])) {
160  for (j = i; j < gv->n_surfs - 1; j++) {
161  gv->drape_surf_id[j] = gv->drape_surf_id[j + 1];
162  }
163 
164  gv->n_surfs = gv->n_surfs - 1;
165  }
166  }
167  }
168  }
169  }
170 }
171 
180 int gv_set_defaults(geovect * gv)
181 {
182  int i;
183 
184  if (!gv) {
185  return (-1);
186  }
187 
188  G_debug(5, "gv_set_defaults() id=%d", gv->gvect_id);
189 
190  gv->filename = NULL;
191  gv->n_lines = gv->n_surfs = gv->use_mem = 0;
192  gv->x_trans = gv->y_trans = gv->z_trans = 0.0;
193  gv->lines = NULL;
194  gv->fastlines = NULL;
195  gv->width = 1;
196  gv->color = 0xFFFFFF;
197  gv->flat_val = 0;
198 
199  for (i = 0; i < MAX_SURFS; i++) {
200  gv->drape_surf_id[i] = 0;
201  }
202 
203  return (0);
204 }
205 
214 int gv_init_vect(geovect * gv)
215 {
216  if (!gv) {
217  return (-1);
218  }
219 
220  G_debug(5, "gv_init_vect() id=%d", gv->gvect_id);
221 
222  return (0);
223 }
224 
230 void gv_delete_vect(int id)
231 {
232  geovect *fv;
233 
234  G_debug(5, "gv_delete_vect(): id=%d", id);
235 
236  fv = gv_get_vect(id);
237 
238  if (fv) {
239  gv_free_vect(fv);
240  }
241 
242  return;
243 }
244 
253 int gv_free_vect(geovect * fv)
254 {
255  geovect *gv;
256  int found = 0;
257 
258  if (Vect_top) {
259  if (fv == Vect_top) {
260  if (Vect_top->next) {
261  /* can't free top if last */
262  found = 1;
263  Vect_top = fv->next;
264  }
265  else {
266  gv_free_vectmem(fv);
267  G_free(fv);
268  Vect_top = NULL;
269  }
270  }
271  else {
272  for (gv = Vect_top; gv && !found; gv = gv->next) {
273  /* can't free top */
274  if (gv->next) {
275  if (gv->next == fv) {
276  found = 1;
277  gv->next = fv->next;
278  }
279  }
280  }
281  }
282 
283  if (found) {
284  G_debug(5, "gv_free_vect(): id=%d", fv->gvect_id);
285  gv_free_vectmem(fv);
286  G_free(fv);
287  fv = NULL;
288  }
289 
290  return (1);
291  }
292 
293  return (-1);
294 }
295 
301 void gv_free_vectmem(geovect * fv)
302 {
303  geoline *gln, *tmpln;
304 
305  G_free((void *)fv->filename);
306  fv->filename = NULL;
307 
308  if (fv->lines) {
309  for (gln = fv->lines; gln;) {
310  if (gln->dims == 2) {
311  sub_Vectmem(gln->npts * sizeof(Point2));
312  G_free(gln->p2);
313  }
314 
315  if (gln->dims == 3) {
316  G_free(gln->p3);
317  }
318 
319  tmpln = gln;
320  gln = gln->next;
321  sub_Vectmem(sizeof(geoline));
322  G_free(tmpln);
323  }
324 
325  fv->n_lines = 0;
326  fv->lines = NULL;
327  }
328 
329  return;
330 }
331 
339 void gv_set_drapesurfs(geovect * gv, int *hsurfs, int nsurfs)
340 {
341  int i;
342 
343  for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
344  gv->drape_surf_id[i] = hsurfs[i];
345  }
346 
347  return;
348 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
void gv_delete_vect(int id)
Delete vector set (unload)
Definition: gv.c:230
geovect * gv_get_prev_vect(int id)
Get previous vector set.
Definition: gv.c:60
#define FIRST_VECT_ID
Definition: gv.c:25
int gv_free_vect(geovect *fv)
Free allocated memory for geovect struct.
Definition: gv.c:253
void gv_update_drapesurfs(void)
Update drape surfaces.
Definition: gv.c:150
int gv_set_defaults(geovect *gv)
Set attributes of vector set to default values.
Definition: gv.c:180
geosurf * gs_get_surf(int id)
Get geosurf struct.
Definition: gs.c:62
geovect * gv_get_vect(int id)
Get vector set.
Definition: gv.c:37
geovect * gv_get_last_vect(void)
Get last loaded vector set.
Definition: gv.c:98
return NULL
Definition: dbfopen.c:1394
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
geovect * gv_get_new_vect(void)
Allocate memory for new vector set.
Definition: gv.c:119
int gv_num_vects(void)
Get number of loaded vector sets.
Definition: gv.c:80
int gv_init_vect(geovect *gv)
Initialize geovect struct.
Definition: gv.c:214
void gv_free_vectmem(geovect *fv)
Free allocated memory.
Definition: gv.c:301
void gv_set_drapesurfs(geovect *gv, int *hsurfs, int nsurfs)
Set drape surfaces for vector set.
Definition: gv.c:339
void sub_Vectmem(int minus)
Tracking memory.
Definition: Gv3.c:272