GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
gv.c
Go to the documentation of this file.
1 /*!
2  \file lib/ogsf/gv.c
3 
4  \brief OGSF library - loading and manipulating vector sets (lower level functions)
5 
6  (C) 1999-2008, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Bill Brown USACERL (November 1993)
12  \author Doxygenized by Martin Landa (June 2008)
13  */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 
18 #include <grass/ogsf.h>
19 #include "gsget.h"
20 
21 #define FIRST_VECT_ID 20656
22 
23 static geovect *Vect_top = NULL;
24 
25 /*!
26  \brief Get vector set
27 
28  \param id vector set id
29 
30  \return pointer to geovect struct
31  \return NULL on failure
32  */
34 {
35  geovect *gv;
36 
37  G_debug(5, "gv_get_vect() id=%d", id);
38 
39  for (gv = Vect_top; gv; gv = gv->next) {
40  if (gv->gvect_id == id) {
41  return gv;
42  }
43  }
44 
45  return NULL;
46 }
47 
48 /*!
49  \brief Get previous vector set
50 
51  \param id vector set id
52 
53  \return pointer to geovect struct
54  \return NULL on failure
55  */
57 {
58  geovect *pv;
59 
60  G_debug(5, "gv_get_prev_vect(): id=%d", id);
61 
62  for (pv = Vect_top; pv; pv = pv->next) {
63  if (pv->gvect_id == id - 1) {
64  return pv;
65  }
66  }
67 
68  return NULL;
69 }
70 
71 /*!
72  \brief Get number of loaded vector sets
73 
74  \return number of vector sets
75  */
76 int gv_num_vects(void)
77 {
78  geovect *gv;
79  int i;
80 
81  for (i = 0, gv = Vect_top; gv; gv = gv->next, i++) ;
82 
83  G_debug(5, "gv_num_vects(): num=%d", i);
84 
85  return i;
86 }
87 
88 /*!
89  \brief Get last loaded vector set
90 
91  \return pointer to geovect struct
92  \return NULL on failure (no vector set available)
93  */
95 {
96  geovect *lv;
97 
98  if (!Vect_top) {
99  return NULL;
100  }
101 
102  for (lv = Vect_top; lv->next; lv = lv->next) ;
103 
104  G_debug(5, "gv_get_last_vect(): id=%d", lv->gvect_id);
105 
106  return lv;
107 }
108 
109 /*!
110  \brief Allocate memory for new vector set
111 
112  \return pointer to geovect struct
113  \return NULL on failure
114  */
116 {
117  geovect *nv, *lv;
118 
119  nv = (geovect *) G_malloc(sizeof(geovect));
120  if (!nv) {
121  /* G_fatal_error */
122  return NULL;
123  }
124  G_zero(nv, sizeof(geovect));
125 
126  if ((lv = gv_get_last_vect())) {
127  lv->next = nv;
128  nv->gvect_id = lv->gvect_id + 1;
129  }
130  else {
131  Vect_top = nv;
132  nv->gvect_id = FIRST_VECT_ID;
133  }
134 
135  nv->style = (gvstyle *) G_malloc(sizeof(gvstyle));
136  if (NULL == nv->style)
137  return NULL;
138  G_zero(nv->style, sizeof (gvstyle));
139  nv->hstyle = (gvstyle *) G_malloc(sizeof(gvstyle));
140  if (NULL == nv->hstyle)
141  return NULL;
142  G_zero(nv->hstyle, sizeof (gvstyle));
143 
144  G_debug(5, "gv_get_new_vect() id=%d", nv->gvect_id);
145 
146  return nv;
147 }
148 
149 /*!
150  \brief Update drape surfaces
151 
152  Call after surface is deleted
153  */
155 {
156  geovect *gv;
157  int i, j;
158 
159  for (gv = Vect_top; gv; gv = gv->next) {
160  if (gv->n_surfs) {
161  for (i = 0; i < gv->n_surfs; i++) {
162  if (gv->drape_surf_id[i]) {
163  if (NULL == gs_get_surf(gv->drape_surf_id[i])) {
164  for (j = i; j < gv->n_surfs - 1; j++) {
165  gv->drape_surf_id[j] = gv->drape_surf_id[j + 1];
166  }
167 
168  gv->n_surfs = gv->n_surfs - 1;
169  }
170  }
171  }
172  }
173  }
174 }
175 
176 /*!
177  \brief Set attributes of vector set to default values
178 
179  \param gv pointer to geovect struct
180 
181  \return -1 on error
182  \return 0 on success
183  */
185 {
186  int i;
187 
188  if (!gv) {
189  return (-1);
190  }
191  G_debug(5, "gv_set_defaults() id=%d", gv->gvect_id);
192 
193  gv->filename = NULL;
194  gv->n_lines = gv->n_surfs = gv->use_mem = 0;
195  gv->x_trans = gv->y_trans = gv->z_trans = 0.0;
196  gv->lines = NULL;
197  gv->fastlines = NULL;
198  gv->use_z = 0;
199  gv->style->color = 0xF0F0F0;
200  gv->style->width = 1;
201  gv->style->next = NULL;
202  gv->hstyle->color = 0xFF0000;
203  gv->hstyle->width = 2;
204  gv->hstyle->next = NULL;
205  gv->tstyle = NULL;
206  gv->next = NULL;
207 
208  for (i = 0; i < MAX_SURFS; i++) {
209  gv->drape_surf_id[i] = 0;
210  }
211 
212  return 0;
213 }
214 
215 /*!
216  \brief Initialize geovect struct
217 
218  \param gv pointer to geovect struct
219 
220  \return -1 on failure
221  \return 0 on succcess
222  */
224 {
225  if (!gv) {
226  return -1;
227  }
228 
229  G_debug(5, "gv_init_vect() id=%d", gv->gvect_id);
230 
231  return 0;
232 }
233 
234 /*!
235  \brief Delete vector set (unload)
236 
237  \param id vector set id
238  */
239 void gv_delete_vect(int id)
240 {
241  geovect *fv;
242 
243  G_debug(5, "gv_delete_vect(): id=%d", id);
244 
245  fv = gv_get_vect(id);
246 
247  if (fv) {
248  gv_free_vect(fv);
249  }
250 
251  return;
252 }
253 
254 /*!
255  \brief Free allocated memory for geovect struct
256 
257  \param fv pointer to geovect struct
258 
259  \return -1 on failure
260  \return 1 on success
261  */
263 {
264  geovect *gv;
265  int found = 0;
266 
267  if (Vect_top) {
268  if (fv == Vect_top) {
269  if (Vect_top->next) {
270  /* can't free top if last */
271  found = 1;
272  Vect_top = fv->next;
273  }
274  else {
275  gv_free_vectmem(fv);
276  G_free(fv);
277  Vect_top = NULL;
278  }
279  }
280  else {
281  for (gv = Vect_top; gv && !found; gv = gv->next) {
282  /* can't free top */
283  if (gv->next) {
284  if (gv->next == fv) {
285  found = 1;
286  gv->next = fv->next;
287  }
288  }
289  }
290  }
291 
292  if (found) {
293  G_debug(5, "gv_free_vect(): id=%d", fv->gvect_id);
294  gv_free_vectmem(fv);
295  G_free(fv);
296  fv = NULL;
297  }
298 
299  return 1;
300  }
301 
302  return -1;
303 }
304 
305 /*!
306  \brief Free allocated memory
307 
308  \param fv pointer to geovect struct
309  */
311 {
312  geoline *gln, *tmpln;
313 
314  G_free((void *)fv->filename);
315  fv->filename = NULL;
316  if (fv->style)
317  G_free(fv->style);
318  if (fv->hstyle)
319  G_free(fv->hstyle);
320 
321  if (fv->lines) {
322  for (gln = fv->lines; gln;) {
323  if (gln->dims == 2) {
324  sub_Vectmem(gln->npts * sizeof(Point2));
325  G_free(gln->p2);
326  }
327 
328  if (gln->dims == 3) {
329  G_free(gln->p3);
330  }
331 
332  G_free(gln->cats);
333 
334  tmpln = gln;
335  gln = gln->next;
336  sub_Vectmem(sizeof(geoline));
337  G_free(tmpln);
338  }
339 
340  fv->n_lines = 0;
341  fv->lines = NULL;
342  }
343 
344  if (fv->tstyle) {
345  G_free(fv->tstyle->color_column);
347  G_free(fv->tstyle->size_column);
348  G_free(fv->tstyle->width_column);
349  }
350 
351  return;
352 }
353 
354 /*!
355  \brief Set drape surfaces for vector set
356 
357  \param gv pointer to geovect struct
358  \param hsurfs array of surfaces (id)
359  \param nsurfs number of surfaces
360  */
361 void gv_set_drapesurfs(geovect * gv, int *hsurfs, int nsurfs)
362 {
363  int i;
364 
365  for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
366  gv->drape_surf_id[i] = hsurfs[i];
367  }
368 
369  return;
370 }
float x_trans
Definition: ogsf.h:338
#define G_malloc(n)
Definition: defs/gis.h:112
geosurf * gs_get_surf(int)
Get geosurf struct.
Definition: gs.c:62
char * size_column
Definition: ogsf.h:309
float z_trans
Definition: ogsf.h:338
void gv_delete_vect(int id)
Delete vector set (unload)
Definition: gv.c:239
int use_z
Definition: ogsf.h:335
geovect * gv_get_prev_vect(int id)
Get previous vector set.
Definition: gv.c:56
int n_surfs
Definition: ogsf.h:336
#define FIRST_VECT_ID
Definition: gv.c:21
int gvect_id
Definition: ogsf.h:332
gvstyle_thematic * tstyle
Definition: ogsf.h:346
int color
Definition: ogsf.h:288
gvstyle * style
Definition: ogsf.h:347
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
float Point2[2]
Definition: ogsf.h:202
struct g_line * next
Definition: ogsf.h:326
geoline * lines
Definition: ogsf.h:340
#define NULL
Definition: ccmath.h:32
#define MAX_SURFS
Definition: ogsf.h:38
char * symbol_column
Definition: ogsf.h:308
char * color_column
Definition: ogsf.h:307
void sub_Vectmem(int)
Tracking memory.
Definition: gv3.c:292
int gv_free_vect(geovect *fv)
Free allocated memory for geovect struct.
Definition: gv.c:262
int drape_surf_id[MAX_SURFS]
Definition: ogsf.h:334
void gv_update_drapesurfs(void)
Update drape surfaces.
Definition: gv.c:154
int width
Definition: ogsf.h:291
int n_lines
Definition: ogsf.h:333
Definition: ogsf.h:330
int gv_set_defaults(geovect *gv)
Set attributes of vector set to default values.
Definition: gv.c:184
char * width_column
Definition: ogsf.h:310
gvstyle * hstyle
Definition: ogsf.h:348
int npts
Definition: ogsf.h:318
struct g_vect * next
Definition: ogsf.h:343
struct g_vect_style * next
Definition: ogsf.h:297
int use_mem
Definition: ogsf.h:333
geovect * gv_get_vect(int id)
Get vector set.
Definition: gv.c:33
geovect * gv_get_last_vect(void)
Get last loaded vector set.
Definition: gv.c:94
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
int dims
Definition: ogsf.h:318
Point3 * p3
Definition: ogsf.h:319
struct line_cats * cats
Definition: ogsf.h:322
float y_trans
Definition: ogsf.h:338
char * filename
Definition: ogsf.h:337
geovect * gv_get_new_vect(void)
Allocate memory for new vector set.
Definition: gv.c:115
geoline * fastlines
Definition: ogsf.h:341
int gv_num_vects(void)
Get number of loaded vector sets.
Definition: gv.c:76
int gv_init_vect(geovect *gv)
Initialize geovect struct.
Definition: gv.c:223
int G_debug(int, const char *,...) __attribute__((format(printf
void gv_free_vectmem(geovect *fv)
Free allocated memory.
Definition: gv.c:310
Definition: ogsf.h:314
Point2 * p2
Definition: ogsf.h:320
void gv_set_drapesurfs(geovect *gv, int *hsurfs, int nsurfs)
Set drape surfaces for vector set.
Definition: gv.c:361