GV2.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*!
  2. \file GV2.c
  3. \brief OGSF library - loading and manipulating vector sets (higher 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, GMSL/University of Illinois
  11. \author Doxygenized by Martin Landa <landa.martin gmail.com>
  12. */
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <grass/gis.h>
  16. #include <grass/gstypes.h>
  17. #include "gsget.h"
  18. static int Vect_ID[MAX_VECTS];
  19. static int Next_vect = 0;
  20. /*!
  21. \brief Check if vector set exists
  22. \param id vector set id
  23. \return 0 not found
  24. \return 1 found
  25. */
  26. int GV_vect_exists(int id)
  27. {
  28. int i, found = 0;
  29. G_debug(3, "GV_vect_exists");
  30. if (NULL == gv_get_vect(id)) {
  31. return (0);
  32. }
  33. for (i = 0; i < Next_vect && !found; i++) {
  34. if (Vect_ID[i] == id) {
  35. found = 1;
  36. }
  37. }
  38. return (found);
  39. }
  40. /*!
  41. \brief Register new vector set
  42. \return vector set id
  43. \return -1 on error
  44. */
  45. int GV_new_vector(void)
  46. {
  47. geovect *nv;
  48. if (Next_vect < MAX_VECTS) {
  49. nv = gv_get_new_vect();
  50. gv_set_defaults(nv);
  51. Vect_ID[Next_vect] = nv->gvect_id;
  52. ++Next_vect;
  53. G_debug(3, "GV_new_vector(): id=%d", nv->gvect_id);
  54. return (nv->gvect_id);
  55. }
  56. return (-1);
  57. }
  58. /*!
  59. \brief Get number of available vector sets
  60. \return number of vector sets
  61. */
  62. int GV_num_vects(void)
  63. {
  64. return (gv_num_vects());
  65. }
  66. /*!
  67. \brief Get list of vector sets
  68. Must free when no longer needed!
  69. \param numvects number of vector sets
  70. \return pointer to list of point sets
  71. \return NULL on error
  72. */
  73. int *GV_get_vect_list(int *numvects)
  74. {
  75. int i, *ret;
  76. *numvects = Next_vect;
  77. if (Next_vect) {
  78. ret = (int *)G_malloc(Next_vect * sizeof(int));
  79. if (!ret) {
  80. return (NULL);
  81. }
  82. for (i = 0; i < Next_vect; i++) {
  83. ret[i] = Vect_ID[i];
  84. }
  85. return (ret);
  86. }
  87. return (NULL);
  88. }
  89. /*!
  90. \brief Delete vector set from list
  91. \param id vector set id
  92. \return 1 on success
  93. \return -1 on error
  94. */
  95. int GV_delete_vector(int id)
  96. {
  97. int i, j, found = 0;
  98. G_debug(3, "GV_delete_vect");
  99. if (GV_vect_exists(id)) {
  100. gv_delete_vect(id);
  101. for (i = 0; i < Next_vect && !found; i++) {
  102. if (Vect_ID[i] == id) {
  103. found = 1;
  104. for (j = i; j < Next_vect; j++) {
  105. Vect_ID[j] = Vect_ID[j + 1];
  106. }
  107. }
  108. }
  109. if (found) {
  110. --Next_vect;
  111. return (1);
  112. }
  113. }
  114. return (-1);
  115. }
  116. /*!
  117. \brief Load vector set
  118. Check to see if handle already loaded, if so - free before loading
  119. new for now, always load to memory
  120. \todo Load file handle & ready for reading instead of using
  121. memory
  122. \param id vector set id
  123. \param filename filename
  124. \return -1 on error (invalid vector set id)
  125. \return 1 on success
  126. */
  127. int GV_load_vector(int id, const char *filename)
  128. {
  129. geovect *gv;
  130. if (NULL == (gv = gv_get_vect(id))) {
  131. return (-1);
  132. }
  133. if (gv->lines) {
  134. gv_free_vectmem(gv);
  135. }
  136. gv->filename = G_store(filename);
  137. if ((gv->lines = Gv_load_vect(filename, &(gv->n_lines)))) {
  138. return (1);
  139. }
  140. return (-1);
  141. }
  142. /*!
  143. \brief Get vector map name
  144. Note: char array is allocated by G_store()
  145. \param id vector set id
  146. \param filename &filename
  147. \return -1 on error (invalid vector set id)
  148. \return 1 on success
  149. */
  150. int GV_get_vectname(int id, char **filename)
  151. {
  152. geovect *gv;
  153. if (NULL == (gv = gv_get_vect(id))) {
  154. return (-1);
  155. }
  156. *filename = G_store(gv->filename);
  157. return (1);
  158. }
  159. /*!
  160. \brief Set vector set mode
  161. \param id vector set id
  162. \param mem non-zero for use memory
  163. \param color color value
  164. \param width line width
  165. \param flat non-zero for flat mode
  166. \return -1 on error (invalid vector set id)
  167. \return 1 on success
  168. */
  169. int GV_set_vectmode(int id, int mem, int color, int width, int flat)
  170. {
  171. geovect *gv;
  172. if (NULL == (gv = gv_get_vect(id))) {
  173. return (-1);
  174. }
  175. gv->use_mem = mem;
  176. gv->flat_val = flat;
  177. gv->style->color = color;
  178. gv->style->width = width;
  179. return (1);
  180. }
  181. /*!
  182. \brief Get vector set mode
  183. \param id vector set id
  184. \param[out] mem
  185. \param[out] color color value
  186. \param[out] width
  187. \param[out] flat
  188. \return -1 on error (invalid vector set id)
  189. \return 1 on success
  190. */
  191. int GV_get_vectmode(int id, int *mem, int *color, int *width, int *flat)
  192. {
  193. geovect *gv;
  194. if (NULL == (gv = gv_get_vect(id))) {
  195. return (-1);
  196. }
  197. *mem = gv->use_mem;
  198. *color = gv->style->color;
  199. *width = gv->style->width;
  200. *flat = gv->flat_val;
  201. return (1);
  202. }
  203. /*!
  204. \brief Set trans ?
  205. \param id vector set id
  206. \param xtrans,ytrans,ztrans x/y/z trans values
  207. */
  208. void GV_set_trans(int id, float xtrans, float ytrans, float ztrans)
  209. {
  210. geovect *gv;
  211. G_debug(3, "GV_set_trans");
  212. gv = gv_get_vect(id);
  213. if (gv) {
  214. gv->x_trans = xtrans;
  215. gv->y_trans = ytrans;
  216. gv->z_trans = ztrans;
  217. }
  218. return;
  219. }
  220. /*!
  221. \brief Get trans ?
  222. \param id vector set id
  223. \param[out] xtrans,ytrans,ztrans x/y/z trans values
  224. */
  225. int GV_get_trans(int id, float *xtrans, float *ytrans, float *ztrans)
  226. {
  227. geovect *gv;
  228. gv = gv_get_vect(id);
  229. if (gv) {
  230. *xtrans = gv->x_trans;
  231. *ytrans = gv->y_trans;
  232. *ztrans = gv->z_trans;
  233. return (1);
  234. }
  235. return (-1);
  236. }
  237. /*!
  238. \brief Select surface identified by hs to have vector identified
  239. by hv draped over it
  240. \param hv vector set id
  241. \param hs surface id
  242. \return 1 on success
  243. \return -1 on error
  244. */
  245. int GV_select_surf(int hv, int hs)
  246. {
  247. geovect *gv;
  248. if (GV_surf_is_selected(hv, hs)) {
  249. return (1);
  250. }
  251. gv = gv_get_vect(hv);
  252. if (gv && GS_surf_exists(hs)) {
  253. gv->drape_surf_id[gv->n_surfs] = hs;
  254. gv->n_surfs += 1;
  255. return (1);
  256. }
  257. return (-1);
  258. }
  259. /*!
  260. \brief Unselect surface
  261. \param hv vector set id
  262. \param hs surface id
  263. \return 1 on success
  264. \return -1 on error
  265. */
  266. int GV_unselect_surf(int hv, int hs)
  267. {
  268. geovect *gv;
  269. int i, j;
  270. if (!GV_surf_is_selected(hv, hs)) {
  271. return (1);
  272. }
  273. gv = gv_get_vect(hv);
  274. if (gv) {
  275. for (i = 0; i < gv->n_surfs; i++) {
  276. if (gv->drape_surf_id[i] == hs) {
  277. for (j = i; j < gv->n_surfs - 1; j++) {
  278. gv->drape_surf_id[j] = gv->drape_surf_id[j + 1];
  279. }
  280. gv->n_surfs -= 1;
  281. return (1);
  282. }
  283. }
  284. }
  285. return (-1);
  286. }
  287. /*!
  288. \brief Check if surface is selected
  289. \param hv vector set id
  290. \param hs surface id
  291. \return 1 selected
  292. \return 0 not selected
  293. */
  294. int GV_surf_is_selected(int hv, int hs)
  295. {
  296. int i;
  297. geovect *gv;
  298. gv = gv_get_vect(hv);
  299. if (gv) {
  300. for (i = 0; i < gv->n_surfs; i++) {
  301. if (hs == gv->drape_surf_id[i]) {
  302. return (1);
  303. }
  304. }
  305. }
  306. return (0);
  307. }
  308. /*!
  309. \brief Draw vector set
  310. \param vid vector set id
  311. */
  312. void GV_draw_vect(int vid)
  313. {
  314. geosurf *gs;
  315. geovect *gv;
  316. int i;
  317. gv = gv_get_vect(vid);
  318. if (gv) {
  319. for (i = 0; i < gv->n_surfs; i++) {
  320. gs = gs_get_surf(gv->drape_surf_id[i]);
  321. if (gs) {
  322. gvd_vect(gv, gs, 0);
  323. }
  324. }
  325. }
  326. return;
  327. }
  328. /*!
  329. \brief Draw all loaded vector sets
  330. */
  331. void GV_alldraw_vect(void)
  332. {
  333. int id;
  334. for (id = 0; id < Next_vect; id++) {
  335. GV_draw_vect(Vect_ID[id]);
  336. }
  337. return;
  338. }
  339. /*!
  340. \brief Draw vector set (fast mode)
  341. \todo Seems to be broken, nothing is drawn
  342. \param vid vector set id
  343. */
  344. void GV_draw_fastvect(int vid)
  345. {
  346. geosurf *gs;
  347. geovect *gv;
  348. int i;
  349. gv = gv_get_vect(vid);
  350. if (gv) {
  351. for (i = 0; i < gv->n_surfs; i++) {
  352. gs = gs_get_surf(gv->drape_surf_id[i]);
  353. if (gs) {
  354. gvd_vect(gv, gs, 1);
  355. }
  356. }
  357. }
  358. return;
  359. }
  360. /*!
  361. \brief Draw all loaded vector sets (fast mode)
  362. */
  363. void GV_alldraw_fastvect(void)
  364. {
  365. int id;
  366. for (id = 0; id < Next_vect; id++) {
  367. GV_draw_fastvect(Vect_ID[id]);
  368. }
  369. return;
  370. }
  371. /*!
  372. \brief Set client data
  373. \param id vector set id
  374. \param clientd pointer to client data
  375. \return 1 on success
  376. \return -1 on error
  377. */
  378. int GV_Set_ClientData(int id, void *clientd)
  379. {
  380. geovect *gv;
  381. gv = gv_get_vect(id);
  382. if (gv) {
  383. gv->clientdata = clientd;
  384. return (1);
  385. }
  386. return (-1);
  387. }
  388. /*!
  389. \brief Get client data
  390. \param id vector set id
  391. \return pointer to client data
  392. \return NULL on error
  393. */
  394. void *GV_Get_ClientData(int id)
  395. {
  396. geovect *gv;
  397. gv = gv_get_vect(id);
  398. if (gv) {
  399. return (gv->clientdata);
  400. }
  401. return (NULL);
  402. }