gv.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*!
  2. \file gv.c
  3. \brief OGSF library - loading and manipulating vector sets (lower level functions)
  4. GRASS OpenGL gsurf OGSF Library
  5. (C) 1999-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Bill Brown USACERL (November 1993)
  11. \author Doxygenized by Martin Landa (June 2008)
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <grass/gstypes.h>
  16. #include "gsget.h"
  17. #define FIRST_VECT_ID 20656
  18. static geovect *Vect_top = NULL;
  19. /*!
  20. \brief Get vector set
  21. \param id vector set id
  22. \return pointer to geovect struct
  23. \return NULL on failure
  24. */
  25. geovect *gv_get_vect(int id)
  26. {
  27. geovect *gv;
  28. G_debug(5, "gv_get_vect() id=%d", id);
  29. for (gv = Vect_top; gv; gv = gv->next) {
  30. if (gv->gvect_id == id) {
  31. return (gv);
  32. }
  33. }
  34. return (NULL);
  35. }
  36. /*!
  37. \brief Get previous vector set
  38. \param id vector set id
  39. \return pointer to geovect struct
  40. \return NULL on failure
  41. */
  42. geovect *gv_get_prev_vect(int id)
  43. {
  44. geovect *pv;
  45. G_debug(5, "gv_get_prev_vect(): id=%d", id);
  46. for (pv = Vect_top; pv; pv = pv->next) {
  47. if (pv->gvect_id == id - 1) {
  48. return (pv);
  49. }
  50. }
  51. return (NULL);
  52. }
  53. /*!
  54. \brief Get number of loaded vector sets
  55. \return number of vector sets
  56. */
  57. int gv_num_vects(void)
  58. {
  59. geovect *gv;
  60. int i;
  61. for (i = 0, gv = Vect_top; gv; gv = gv->next, i++) ;
  62. G_debug(5, "gv_num_vects(): num=%d", i);
  63. return (i);
  64. }
  65. /*!
  66. \brief Get last loaded vector set
  67. \return pointer to geovect struct
  68. \return NULL on failure (no vector set available)
  69. */
  70. geovect *gv_get_last_vect(void)
  71. {
  72. geovect *lv;
  73. if (!Vect_top) {
  74. return (NULL);
  75. }
  76. for (lv = Vect_top; lv->next; lv = lv->next) ;
  77. G_debug(5, "gv_get_last_vect(): id=%d", lv->gvect_id);
  78. return (lv);
  79. }
  80. /*!
  81. \brief Allocate memory for new vector set
  82. \return pointer to geovect struct
  83. \return NULL on failure
  84. */
  85. geovect *gv_get_new_vect(void)
  86. {
  87. geovect *nv, *lv;
  88. nv = (geovect *) G_malloc(sizeof(geovect));
  89. if (!nv) {
  90. /* G_fatal_error */
  91. return (NULL);
  92. }
  93. if ((lv = gv_get_last_vect())) {
  94. lv->next = nv;
  95. nv->gvect_id = lv->gvect_id + 1;
  96. }
  97. else {
  98. Vect_top = nv;
  99. nv->gvect_id = FIRST_VECT_ID;
  100. }
  101. nv->next = NULL;
  102. nv->style = (gvstyle *) G_malloc(sizeof(gvstyle));
  103. if (NULL == nv->style)
  104. return NULL;
  105. nv->hstyle = (gvstyle *) G_malloc(sizeof(gvstyle));
  106. if (NULL == nv->hstyle)
  107. return NULL;
  108. G_debug(5, "gv_get_new_vect() id=%d", nv->gvect_id);
  109. return (nv);
  110. }
  111. /*!
  112. \brief Update drape surfaces
  113. Call after surface is deleted
  114. */
  115. void gv_update_drapesurfs(void)
  116. {
  117. geovect *gv;
  118. int i, j;
  119. for (gv = Vect_top; gv; gv = gv->next) {
  120. if (gv->n_surfs) {
  121. for (i = 0; i < gv->n_surfs; i++) {
  122. if (gv->drape_surf_id[i]) {
  123. if (NULL == gs_get_surf(gv->drape_surf_id[i])) {
  124. for (j = i; j < gv->n_surfs - 1; j++) {
  125. gv->drape_surf_id[j] = gv->drape_surf_id[j + 1];
  126. }
  127. gv->n_surfs = gv->n_surfs - 1;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. /*!
  135. \brief Set attributes of vector set to default values
  136. \param gv pointer to geovect struct
  137. \return -1 on error
  138. \return 0 on success
  139. */
  140. int gv_set_defaults(geovect * gv)
  141. {
  142. int i;
  143. G_debug(5, "gv_set_defaults() id=%d", gv->gvect_id);
  144. if (!gv) {
  145. return (-1);
  146. }
  147. gv->filename = NULL;
  148. gv->n_lines = gv->n_surfs = gv->use_mem = 0;
  149. gv->x_trans = gv->y_trans = gv->z_trans = 0.0;
  150. gv->lines = NULL;
  151. gv->fastlines = NULL;
  152. gv->flat_val = 0;
  153. gv->thematic_layer = -1;
  154. gv->style->color = 0xF0F0F0;
  155. gv->style->width = 1;
  156. gv->style->next = NULL;
  157. gv->hstyle->color = 0xFF0000;
  158. gv->hstyle->width = 2;
  159. gv->hstyle->next = NULL;
  160. gv->next = NULL;
  161. for (i = 0; i < MAX_SURFS; i++) {
  162. gv->drape_surf_id[i] = 0;
  163. }
  164. return (0);
  165. }
  166. /*!
  167. \brief Initialize geovect struct
  168. \param gv pointer to geovect struct
  169. \return -1 on failure
  170. \return 0 on succcess
  171. */
  172. int gv_init_vect(geovect * gv)
  173. {
  174. if (!gv) {
  175. return (-1);
  176. }
  177. G_debug(5, "gv_init_vect() id=%d", gv->gvect_id);
  178. return (0);
  179. }
  180. /*!
  181. \brief Delete vector set (unload)
  182. \param id vector set id
  183. */
  184. void gv_delete_vect(int id)
  185. {
  186. geovect *fv;
  187. G_debug(5, "gv_delete_vect(): id=%d", id);
  188. fv = gv_get_vect(id);
  189. if (fv) {
  190. gv_free_vect(fv);
  191. }
  192. return;
  193. }
  194. /*!
  195. \brief Free allocated memory for geovect struct
  196. \param fv pointer to geovect struct
  197. \return -1 on failure
  198. \return 1 on success
  199. */
  200. int gv_free_vect(geovect * fv)
  201. {
  202. geovect *gv;
  203. int found = 0;
  204. if (Vect_top) {
  205. if (fv == Vect_top) {
  206. if (Vect_top->next) {
  207. /* can't free top if last */
  208. found = 1;
  209. Vect_top = fv->next;
  210. }
  211. else {
  212. gv_free_vectmem(fv);
  213. G_free(fv);
  214. Vect_top = NULL;
  215. }
  216. }
  217. else {
  218. for (gv = Vect_top; gv && !found; gv = gv->next) {
  219. /* can't free top */
  220. if (gv->next) {
  221. if (gv->next == fv) {
  222. found = 1;
  223. gv->next = fv->next;
  224. }
  225. }
  226. }
  227. }
  228. if (found) {
  229. G_debug(5, "gv_free_vect(): id=%d", fv->gvect_id);
  230. gv_free_vectmem(fv);
  231. G_free(fv);
  232. fv = NULL;
  233. }
  234. return (1);
  235. }
  236. return (-1);
  237. }
  238. /*!
  239. \brief Free allocated memory
  240. \param fv pointer to geovect struct
  241. */
  242. void gv_free_vectmem(geovect * fv)
  243. {
  244. geoline *gln, *tmpln;
  245. gvstyle *gvs, *tmpstyle;
  246. G_free((void *)fv->filename);
  247. fv->filename = NULL;
  248. if (fv->style)
  249. G_free(fv->style);
  250. if (fv->hstyle)
  251. G_free(fv->hstyle);
  252. if (fv->lines) {
  253. for (gln = fv->lines; gln;) {
  254. if (gln->dims == 2) {
  255. sub_Vectmem(gln->npts * sizeof(Point2));
  256. G_free(gln->p2);
  257. }
  258. if (gln->dims == 3) {
  259. G_free(gln->p3);
  260. }
  261. G_free(gln->cats);
  262. if (fv->thematic_layer > -1) { /* Style also exists for features */
  263. for (gvs = gln->style; gvs;) {
  264. tmpstyle = gvs;
  265. gvs = gvs->next;
  266. G_free(tmpstyle);
  267. }
  268. }
  269. tmpln = gln;
  270. gln = gln->next;
  271. sub_Vectmem(sizeof(geoline));
  272. G_free(tmpln);
  273. }
  274. fv->n_lines = 0;
  275. fv->lines = NULL;
  276. }
  277. return;
  278. }
  279. /*!
  280. \brief Set drape surfaces for vector set
  281. \param gv pointer to geovect struct
  282. \param hsurfs array of surfaces (id)
  283. \param nsurfs number of surfaces
  284. */
  285. void gv_set_drapesurfs(geovect * gv, int *hsurfs, int nsurfs)
  286. {
  287. int i;
  288. for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
  289. gv->drape_surf_id[i] = hsurfs[i];
  290. }
  291. return;
  292. }