GP2.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*!
  2. \file GP2.c
  3. \brief OGSF library - loading and manipulating point 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 (January 1994)
  11. \author Doxygenized by Martin landa <landa.martin gmail.com> (May 2008)
  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 Site_ID[MAX_SITES];
  19. static int Next_site = 0;
  20. /*!
  21. \brief Check if point set exists
  22. \param id point set id
  23. \return 1 found
  24. \return 0 not found
  25. */
  26. int GP_site_exists(int id)
  27. {
  28. int i, found = 0;
  29. G_debug(4, "GP_site_exists(%d)", id);
  30. if (NULL == gp_get_site(id)) {
  31. return (0);
  32. }
  33. for (i = 0; i < Next_site && !found; i++) {
  34. if (Site_ID[i] == id) {
  35. found = 1;
  36. }
  37. }
  38. G_debug(3, "GP_site_exists(): found=%d", found);
  39. return (found);
  40. }
  41. /*!
  42. \brief Create new point set
  43. \return point set id
  44. \return -1 on error (number of point sets exceeded)
  45. */
  46. int GP_new_site(void)
  47. {
  48. geosite *np;
  49. if (Next_site < MAX_SITES) {
  50. np = gp_get_new_site();
  51. gp_set_defaults(np);
  52. Site_ID[Next_site] = np->gsite_id;
  53. ++Next_site;
  54. G_debug(3, "GP_new_site() id=%d", np->gsite_id);
  55. return (np->gsite_id);
  56. }
  57. return (-1);
  58. }
  59. /*!
  60. \brief Get number of loaded point sets
  61. \return number of point sets
  62. */
  63. int GP_num_sites(void)
  64. {
  65. return (gp_num_sites());
  66. }
  67. /*!
  68. \brief Get list of point sets
  69. Must freed when no longer needed!
  70. \param numsites number of point sets
  71. \return pointer to list of points sets
  72. \return NULL on error
  73. */
  74. int *GP_get_site_list(int *numsites)
  75. {
  76. int i, *ret;
  77. *numsites = Next_site;
  78. if (Next_site) {
  79. ret = (int *)G_malloc(Next_site * sizeof(int)); /* G_fatal_error */
  80. if (!ret) {
  81. return (NULL);
  82. }
  83. for (i = 0; i < Next_site; i++) {
  84. ret[i] = Site_ID[i];
  85. }
  86. return (ret);
  87. }
  88. return (NULL);
  89. }
  90. /*!
  91. \brief Delete registrated point set
  92. \param id point set id
  93. \return 1 on success
  94. \return -1 on error (point sets not available)
  95. */
  96. int GP_delete_site(int id)
  97. {
  98. int i, j, found = 0;
  99. G_debug(4, "GP_delete_site(%d)", id);
  100. if (GP_site_exists(id)) {
  101. gp_delete_site(id);
  102. for (i = 0; i < Next_site && !found; i++) {
  103. if (Site_ID[i] == id) {
  104. found = 1;
  105. for (j = i; j < Next_site; j++) {
  106. Site_ID[j] = Site_ID[j + 1];
  107. }
  108. }
  109. }
  110. if (found) {
  111. --Next_site;
  112. return (1);
  113. }
  114. }
  115. return (-1);
  116. }
  117. /*!
  118. \brief Load point set from file
  119. Check to see if handle already loaded, if so - free before loading new
  120. for now, always load to memory
  121. \todo load file handle & ready for reading instead of using
  122. memory
  123. \param id point set id
  124. \param filename point set filename
  125. \return -1 on error
  126. \return 1 on success
  127. */
  128. int GP_load_site(int id, const char *filename)
  129. {
  130. geosite *gp;
  131. G_debug(3, "GP_load_site(%d, %s)", id, filename);
  132. if (NULL == (gp = gp_get_site(id))) {
  133. return (-1);
  134. }
  135. if (gp->points) {
  136. gp_free_sitemem(gp);
  137. }
  138. gp->filename = G_store(filename);
  139. gp->points = Gp_load_sites(filename, &(gp->n_sites), &(gp->has_z));
  140. if (gp->points) {
  141. return (1);
  142. }
  143. return (-1);
  144. }
  145. /*!
  146. \brief Get point set filename
  147. Note: char array is allocated by G_store()
  148. \param id point set id
  149. \param[out] &filename point set filename
  150. \return -1 on error (point set not found)
  151. \return 1 on success
  152. */
  153. int GP_get_sitename(int id, char **filename)
  154. {
  155. geosite *gp;
  156. G_debug(4, "GP_get_sitename(%d)", id);
  157. if (NULL == (gp = gp_get_site(id))) {
  158. return (-1);
  159. }
  160. *filename = G_store(gp->filename);
  161. return (1);
  162. }
  163. /*!
  164. \brief Get point set style
  165. \param id point set id
  166. \return -1 on error (point set not found)
  167. */
  168. int GP_get_style(int id, int *color, int *width, float *size, int *symbol)
  169. {
  170. geosite *gp;
  171. G_debug(4, "GP_get_style(%d)", id);
  172. if (NULL == (gp = gp_get_site(id))) {
  173. return (-1);
  174. }
  175. *color = gp->style->color;
  176. *width = gp->style->width;
  177. *symbol = gp->style->symbol;
  178. *size = gp->style->size;
  179. return (1);
  180. }
  181. /*!
  182. \brief Set point set style
  183. \param id point set id
  184. \param color icon color
  185. \param width icon line width
  186. \param size icon size
  187. \param symbol icon symbol
  188. \return -1 on error (point set not found)
  189. */
  190. int GP_set_style(int id, int color, int width, float size, int symbol)
  191. {
  192. geosite *gp;
  193. G_debug(4, "GP_set_style(%d, %d, %d, %f, %d)", id, color, width, size,
  194. symbol);
  195. if (NULL == (gp = gp_get_site(id))) {
  196. return (-1);
  197. }
  198. gp->style->color = color;
  199. gp->style->symbol = symbol;
  200. gp->style->size = size;
  201. gp->style->width = width;
  202. return (1);
  203. }
  204. /*!
  205. \brief Set z-mode
  206. \param id poin set id
  207. \param use_z use z ?
  208. \return 1 on success
  209. \return 0 no z
  210. \return -1 on error (invalid point set id)
  211. */
  212. /* I don't see who is using it? Why it's required? */
  213. int GP_set_zmode(int id, int use_z)
  214. {
  215. geosite *gp;
  216. G_debug(3, "GP_set_zmode(%d,%d)", id, use_z);
  217. if (NULL == (gp = gp_get_site(id))) {
  218. return (-1);
  219. }
  220. if (use_z) {
  221. if (gp->has_z) {
  222. gp->use_z = 1;
  223. return (1);
  224. }
  225. return (0);
  226. }
  227. gp->use_z = 0;
  228. return (1);
  229. }
  230. /*!
  231. \brief Get z-mode
  232. \param id point set id
  233. \param[out] use_z use z
  234. \return -1 on error (invalid point set id)
  235. \return 1 on success
  236. */
  237. /* Who's using this? */
  238. int GP_get_zmode(int id, int *use_z)
  239. {
  240. geosite *gp;
  241. G_debug(4, "GP_get_zmode(%d)", id);
  242. if (NULL == (gp = gp_get_site(id))) {
  243. return (-1);
  244. }
  245. *use_z = gp->use_z;
  246. return (1);
  247. }
  248. /*!
  249. \brief Set trans ?
  250. \param id point set id
  251. \param xtrans,ytrans,ztrans x/y/z trans values
  252. */
  253. void GP_set_trans(int id, float xtrans, float ytrans, float ztrans)
  254. {
  255. geosite *gp;
  256. G_debug(3, "GP_set_trans(): id=%d trans=%f,%f,%f",
  257. id, xtrans, ytrans, ztrans);
  258. gp = gp_get_site(id);
  259. if (gp) {
  260. gp->x_trans = xtrans;
  261. gp->y_trans = ytrans;
  262. gp->z_trans = ztrans;
  263. }
  264. return;
  265. }
  266. /*!
  267. \brief Get trans
  268. \param id point set id
  269. \param xtrans,ytrans,ztrans x/y/z trans values
  270. */
  271. void GP_get_trans(int id, float *xtrans, float *ytrans, float *ztrans)
  272. {
  273. geosite *gp;
  274. gp = gp_get_site(id);
  275. if (gp) {
  276. *xtrans = gp->x_trans;
  277. *ytrans = gp->y_trans;
  278. *ztrans = gp->z_trans;
  279. }
  280. G_debug(3, "GP_get_trans(): id=%d, trans=%f,%f,%f",
  281. id, *xtrans, *ytrans, *ztrans);
  282. return;
  283. }
  284. /*!
  285. \brief Select surface
  286. \param hp point set id
  287. \param hs surface id
  288. \return 1 surface selected
  289. \return -1 on error
  290. */
  291. int GP_select_surf(int hp, int hs)
  292. {
  293. geosite *gp;
  294. G_debug(3, "GP_select_surf(%d,%d)", hp, hs);
  295. if (GP_surf_is_selected(hp, hs)) {
  296. return (1);
  297. }
  298. gp = gp_get_site(hp);
  299. if (gp && GS_surf_exists(hs)) {
  300. gp->drape_surf_id[gp->n_surfs] = hs;
  301. gp->n_surfs += 1;
  302. return (1);
  303. }
  304. return (-1);
  305. }
  306. /*!
  307. \brief Unselect surface
  308. \param hp point set id
  309. \param hs surface id
  310. \return 1 surface unselected
  311. \return -1 on error
  312. */
  313. int GP_unselect_surf(int hp, int hs)
  314. {
  315. geosite *gp;
  316. int i, j;
  317. G_debug(3, "GP_unselect_surf(%d,%d)", hp, hs);
  318. if (!GP_surf_is_selected(hp, hs)) {
  319. return (1);
  320. }
  321. gp = gp_get_site(hp);
  322. if (gp) {
  323. for (i = 0; i < gp->n_surfs; i++) {
  324. if (gp->drape_surf_id[i] == hs) {
  325. for (j = i; j < gp->n_surfs - 1; j++) {
  326. gp->drape_surf_id[j] = gp->drape_surf_id[j + 1];
  327. }
  328. gp->n_surfs -= 1;
  329. return (1);
  330. }
  331. }
  332. }
  333. return (-1);
  334. }
  335. /*!
  336. \brief Check if surface is selected
  337. \param hp point set id
  338. \param hs surface id
  339. \return 1 selected
  340. \return 0 not selected
  341. */
  342. int GP_surf_is_selected(int hp, int hs)
  343. {
  344. int i;
  345. geosite *gp;
  346. G_debug(3, "GP_surf_is_selected(%d,%d)", hp, hs);
  347. gp = gp_get_site(hp);
  348. if (gp) {
  349. for (i = 0; i < gp->n_surfs; i++) {
  350. if (hs == gp->drape_surf_id[i]) {
  351. return (1);
  352. }
  353. }
  354. }
  355. return (0);
  356. }
  357. /*!
  358. \brief Draw point set
  359. \param id point set id
  360. */
  361. void GP_draw_site(int id)
  362. {
  363. geosurf *gs;
  364. geosite *gp;
  365. int i;
  366. float n, yo, xo, e;
  367. gp = gp_get_site(id);
  368. GS_get_region(&n, &yo, &xo, &e);
  369. /* kind of sloppy - maybe site files should have an origin, too */
  370. if (gp) {
  371. if (gp->use_z && gp->has_z) {
  372. gpd_3dsite(gp, xo, yo, 0);
  373. }
  374. else {
  375. for (i = 0; i < gp->n_surfs; i++) {
  376. gs = gs_get_surf(gp->drape_surf_id[i]);
  377. if (gs) {
  378. gpd_2dsite(gp, gs, 0);
  379. G_debug(5, "Drawing site %d on Surf %d", id,
  380. gp->drape_surf_id[i]);
  381. }
  382. }
  383. }
  384. }
  385. return;
  386. }
  387. /*!
  388. \brief Draw all available point sets
  389. */
  390. void GP_alldraw_site(void)
  391. {
  392. int id;
  393. for (id = 0; id < Next_site; id++) {
  394. GP_draw_site(Site_ID[id]);
  395. }
  396. return;
  397. }
  398. /*!
  399. \brief Set client data
  400. \param id point set id
  401. \param clientd client data
  402. \return 1 on success
  403. \return -1 on error (invalid point set id)
  404. */
  405. int GP_Set_ClientData(int id, void *clientd)
  406. {
  407. geosite *gp;
  408. gp = gp_get_site(id);
  409. if (gp) {
  410. gp->clientdata = clientd;
  411. return (1);
  412. }
  413. return (-1);
  414. }
  415. /*!
  416. \brief Get client data
  417. \param id point set id
  418. \return pointer to client data
  419. \return NULL on error
  420. */
  421. void *GP_Get_ClientData(int id)
  422. {
  423. geosite *gp;
  424. gp = gp_get_site(id);
  425. if (gp) {
  426. return (gp->clientdata);
  427. }
  428. return (NULL);
  429. }