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