gp.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*!
  2. \file gp.c
  3. \brief OGSF library - loading and manipulating point sets
  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, GMSL/University of Illinois (January 1994)
  11. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  12. */
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/gstypes.h>
  16. #define FIRST_SITE_ID 21720
  17. static geosite *Site_top = NULL;
  18. /*!
  19. \brief Get geosite struct
  20. \param id point set id
  21. \return pointer to geosite struct
  22. \return NULL on failure
  23. */
  24. geosite *gp_get_site(int id)
  25. {
  26. geosite *gp;
  27. G_debug(5, "gp_get_site(%d)", id);
  28. for (gp = Site_top; gp; gp = gp->next) {
  29. if (gp->gsite_id == id) {
  30. return (gp);
  31. }
  32. }
  33. return (NULL);
  34. }
  35. /*!
  36. \brief Get previous geosite struct from list
  37. \param id point set id
  38. \return pointer to geosite struct
  39. \return NULL on failure
  40. */
  41. geosite *gp_get_prev_site(int id)
  42. {
  43. geosite *pp;
  44. G_debug(5, "gp_get_prev_site(%d)", id);
  45. for (pp = Site_top; pp; pp = pp->next) {
  46. if (pp->gsite_id == id - 1) {
  47. return (pp);
  48. }
  49. }
  50. return (NULL);
  51. }
  52. /*!
  53. \brief Get number of loaded point sets
  54. \return number of point sets
  55. */
  56. int gp_num_sites(void)
  57. {
  58. geosite *gp;
  59. int i;
  60. for (i = 0, gp = Site_top; gp; gp = gp->next, i++) ;
  61. G_debug(5, "gp_num_sites(): n=%d", i);
  62. return (i);
  63. }
  64. /*!
  65. \brief Get last point set
  66. \return pointer to geosite struct
  67. \return NULL if no point set is available
  68. */
  69. geosite *gp_get_last_site(void)
  70. {
  71. geosite *lp;
  72. G_debug(5, "gp_get_last_site");
  73. if (!Site_top) {
  74. return (NULL);
  75. }
  76. for (lp = Site_top; lp->next; lp = lp->next) ;
  77. G_debug(5, " last site id: %d", lp->gsite_id);
  78. return (lp);
  79. }
  80. /*!
  81. \brief Create new geosite instance and add it to list
  82. \return pointer to geosite struct
  83. \return NULL on error
  84. */
  85. geosite *gp_get_new_site(void)
  86. {
  87. geosite *np, *lp;
  88. G_debug(5, "gp_get_new_site");
  89. np = (geosite *) G_malloc(sizeof(geosite)); /* G_fatal_error */
  90. if (!np) {
  91. return (NULL);
  92. }
  93. lp = gp_get_last_site();
  94. if (lp) {
  95. lp->next = np;
  96. np->gsite_id = lp->gsite_id + 1;
  97. }
  98. else {
  99. Site_top = np;
  100. np->gsite_id = FIRST_SITE_ID;
  101. }
  102. np->next = NULL;
  103. np->style = (gvstyle *) G_malloc(sizeof(gvstyle));
  104. if (!np->style)
  105. return NULL;
  106. np->hstyle = (gvstyle *) G_malloc(sizeof(gvstyle));
  107. if (!np->hstyle)
  108. return NULL;
  109. return (np);
  110. }
  111. /*!
  112. \brief Update drape surfaces
  113. Call after surface is deleted
  114. */
  115. void gp_update_drapesurfs(void)
  116. {
  117. geosite *gp;
  118. int i, j;
  119. for (gp = Site_top; gp; gp = gp->next) {
  120. if (gp->n_surfs) {
  121. for (i = 0; i < gp->n_surfs; i++) {
  122. if (gp->drape_surf_id[i]) {
  123. if (NULL == gs_get_surf(gp->drape_surf_id[i])) {
  124. for (j = i; j < gp->n_surfs - 1; j++) {
  125. gp->drape_surf_id[j] = gp->drape_surf_id[j + 1];
  126. }
  127. gp->n_surfs = gp->n_surfs - 1;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. return;
  134. }
  135. /*!
  136. \brief Set default value for geosite struct
  137. \param gp pointer to geosite struct
  138. \return 1 on success
  139. \return -1 on failure
  140. */
  141. int gp_set_defaults(geosite * gp)
  142. {
  143. int i;
  144. float dim;
  145. G_debug(5, "gp_set_defaults");
  146. if (!gp) {
  147. return (-1);
  148. }
  149. GS_get_longdim(&dim);
  150. gp->filename = NULL;
  151. gp->n_sites = gp->use_z = gp->n_surfs = gp->use_mem = 0;
  152. gp->x_trans = gp->y_trans = gp->z_trans = 0.0;
  153. gp->points = NULL;
  154. gp->has_z = 0;
  155. gp->thematic_layer = -1;
  156. gp->style->color = 0xF0F0F0;
  157. gp->style->size = dim / 100.;
  158. gp->style->width = 1;
  159. gp->style->symbol = ST_X;
  160. gp->style->next = NULL;
  161. gp->hstyle->color = 0xFF0000;
  162. gp->hstyle->size = dim / 150.;
  163. gp->hstyle->symbol = ST_X;
  164. gp->hstyle->next = NULL;
  165. gp->next = NULL;
  166. for (i = 0; i < MAX_SURFS; i++) {
  167. gp->drape_surf_id[i] = 0;
  168. }
  169. return (1);
  170. }
  171. /*!
  172. \brief Initialize geosite struct
  173. \param gp pointer to geosite struct
  174. \return -1 on failure
  175. \return 0 on success
  176. */
  177. int gp_init_site(geosite * gp)
  178. {
  179. G_debug(5, "gp_init_site");
  180. if (!gp) {
  181. return (-1);
  182. }
  183. return (0);
  184. }
  185. /*!
  186. \brief Delete point set and remove from list
  187. \param id point set id
  188. */
  189. void gp_delete_site(int id)
  190. {
  191. geosite *fp;
  192. G_debug(5, "gp_delete_site");
  193. fp = gp_get_site(id);
  194. if (fp) {
  195. gp_free_site(fp);
  196. }
  197. return;
  198. }
  199. /*!
  200. \brief Free geosite struct
  201. \param fp pointer to geosite struct
  202. \return 1 on success
  203. \return -1 on failure
  204. */
  205. int gp_free_site(geosite * fp)
  206. {
  207. geosite *gp;
  208. int found = 0;
  209. G_debug(5, "gp_free_site");
  210. if (Site_top) {
  211. if (fp == Site_top) {
  212. if (Site_top->next) {
  213. /* can't free top if last */
  214. found = 1;
  215. Site_top = fp->next;
  216. }
  217. else {
  218. gp_free_sitemem(fp);
  219. G_free(fp);
  220. Site_top = NULL;
  221. }
  222. }
  223. else {
  224. for (gp = Site_top; gp && !found; gp = gp->next) {
  225. /* can't free top */
  226. if (gp->next) {
  227. if (gp->next == fp) {
  228. found = 1;
  229. gp->next = fp->next;
  230. }
  231. }
  232. }
  233. }
  234. if (found) {
  235. gp_free_sitemem(fp);
  236. G_free(fp);
  237. fp = NULL;
  238. }
  239. return (1);
  240. }
  241. return (-1);
  242. }
  243. /*!
  244. \brief Free geosite
  245. \param fp pointer to geosite struct
  246. */
  247. void gp_free_sitemem(geosite * fp)
  248. {
  249. geopoint *gpt, *tmp;
  250. gvstyle *gvs, *tmpstyle;
  251. G_free((void *)fp->filename);
  252. fp->filename = NULL;
  253. if (fp->style)
  254. G_free(fp->style);
  255. if (fp->hstyle)
  256. G_free(fp->hstyle);
  257. if (fp->points) {
  258. for (gpt = fp->points; gpt;) {
  259. G_free(gpt->cats);
  260. if (fp->thematic_layer > -1) { /* Style also exists for features */
  261. for (gvs = fp->style; gvs;) {
  262. tmpstyle = gvs;
  263. gvs = gvs->next;
  264. G_free(tmpstyle);
  265. }
  266. }
  267. tmp = gpt;
  268. gpt = gpt->next;
  269. G_free(tmp);
  270. }
  271. fp->n_sites = 0;
  272. fp->points = NULL;
  273. }
  274. return;
  275. }
  276. /*!
  277. \brief Set drape surfaces
  278. \param gp pointer to geosite struct
  279. \param hsurf list of surfaces (id)
  280. \param nsurf number of surfaces
  281. */
  282. void gp_set_drapesurfs(geosite * gp, int hsurfs[], int nsurfs)
  283. {
  284. int i;
  285. for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
  286. gp->drape_surf_id[i] = hsurfs[i];
  287. }
  288. return;
  289. }