gvl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*!
  2. \file gvl.c
  3. \brief OGSF library - loading and manipulating volumes (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, UI-GMSL (May 1997)
  11. \author Tomas Paudits (February 2004)
  12. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  13. */
  14. #include <stdlib.h>
  15. #include <grass/gis.h>
  16. #include <grass/gstypes.h>
  17. #include "gsget.h"
  18. #define FIRST_VOL_ID 81721
  19. static geovol *Vol_top = NULL;
  20. /*!
  21. \brief Get volume set structure
  22. \param id volume set id
  23. \return pointer to geovol struct
  24. \return NULL on failure
  25. */
  26. geovol *gvl_get_vol(int id)
  27. {
  28. geovol *gvl;
  29. G_debug(5, "gvl_get_vol():");
  30. for (gvl = Vol_top; gvl; gvl = gvl->next) {
  31. if (gvl->gvol_id == id) {
  32. G_debug(5, " id=%d", id);
  33. return (gvl);
  34. }
  35. }
  36. return (NULL);
  37. }
  38. /*!
  39. \brief Get previous volume
  40. \param id current volume set id
  41. \return pointer to geovol struct
  42. \return NULL on failure
  43. */
  44. geovol *gvl_get_prev_vol(int id)
  45. {
  46. geovol *pv;
  47. G_debug(5, "gvl_get_prev_vol");
  48. for (pv = Vol_top; pv; pv = pv->next) {
  49. if (pv->gvol_id == id - 1) {
  50. return (pv);
  51. }
  52. }
  53. return (NULL);
  54. }
  55. /*!
  56. \brief Get all volumes
  57. \param[out] list of geovol structs
  58. \return number of available volume sets
  59. */
  60. int gvl_getall_vols(geovol ** gvols)
  61. {
  62. geovol *gvl;
  63. int i;
  64. G_debug(5, "gvl_getall_vols");
  65. for (i = 0, gvl = Vol_top; gvl; gvl = gvl->next, i++) {
  66. gvols[i] = gvl;
  67. }
  68. return (i);
  69. }
  70. /*!
  71. \brief Get number of loaded volume sets
  72. \return number of volumes
  73. */
  74. int gvl_num_vols(void)
  75. {
  76. geovol *gvl;
  77. int i;
  78. for (i = 0, gvl = Vol_top; gvl; gvl = gvl->next, i++) ;
  79. G_debug(5, "gvl_num_vols(): num=%d", i);
  80. return (i);
  81. }
  82. /*!
  83. \brief Get last volume set from the list
  84. \return pointer to geovol struct
  85. \return NULL on failure
  86. */
  87. geovol *gvl_get_last_vol(void)
  88. {
  89. geovol *lvl;
  90. G_debug(5, "gvl_get_last_vol");
  91. if (!Vol_top) {
  92. return (NULL);
  93. }
  94. for (lvl = Vol_top; lvl->next; lvl = lvl->next) ;
  95. G_debug(5, " last vol id: %d", lvl->gvol_id);
  96. return (lvl);
  97. }
  98. /*!
  99. \brief Allocate new volume set and add it to the list
  100. \return pointer to geovol struct
  101. \return NULL on failure
  102. */
  103. geovol *gvl_get_new_vol(void)
  104. {
  105. geovol *nvl, *lvl;
  106. G_debug(5, "gvl_get_new_vol()");
  107. nvl = (geovol *) G_malloc(sizeof(geovol)); /* G_fatal_error */
  108. if (!nvl) {
  109. return (NULL);
  110. }
  111. if ((lvl = gvl_get_last_vol())) {
  112. lvl->next = nvl;
  113. nvl->gvol_id = lvl->gvol_id + 1;
  114. }
  115. else {
  116. Vol_top = nvl;
  117. nvl->gvol_id = FIRST_VOL_ID;
  118. }
  119. nvl->next = NULL;
  120. G_debug(5, " id=%d", nvl->gvol_id);
  121. return (nvl);
  122. }
  123. /*!
  124. \brief Initialize geovol structure
  125. \param gvl pointer to geovol struct
  126. \param ox,oy,oz
  127. \param rows number of rows
  128. \param cols number of cols
  129. \param xres,yres,zres x/y/z resolution value
  130. \return -1 on failure
  131. \return 1 on success
  132. */
  133. int gvl_init_vol(geovol * gvl, double ox, double oy, double oz,
  134. int rows, int cols, int depths, double xres, double yres,
  135. double zres)
  136. {
  137. G_debug(5, "gvl_init_vol() id=%d", gvl->gvol_id);
  138. if (!gvl) {
  139. return (-1);
  140. }
  141. gvl->ox = ox;
  142. gvl->oy = oy;
  143. gvl->oz = oz;
  144. gvl->rows = rows;
  145. gvl->cols = cols;
  146. gvl->depths = depths;
  147. gvl->xres = xres;
  148. gvl->yres = yres;
  149. gvl->zres = zres;
  150. gvl->xmin = ox;
  151. gvl->xmax = ox + (cols - 1) * xres;
  152. gvl->xrange = gvl->xmax - gvl->xmin;
  153. gvl->ymin = oy;
  154. gvl->ymax = oy + (rows - 1) * yres;
  155. gvl->yrange = gvl->ymax - gvl->ymin;
  156. gvl->zmin = oz;
  157. gvl->zmax = oz + (depths - 1) * zres;
  158. gvl->zrange = gvl->zmax - gvl->zmin;
  159. gvl->x_trans = gvl->y_trans = gvl->z_trans = 0.0;
  160. gvl->n_isosurfs = 0;
  161. G_zero(gvl->isosurf, sizeof(geovol_isosurf *) * MAX_ISOSURFS);
  162. gvl->isosurf_x_mod = 1;
  163. gvl->isosurf_y_mod = 1;
  164. gvl->isosurf_z_mod = 1;
  165. gvl->isosurf_draw_mode = DM_GOURAUD;
  166. gvl->n_slices = 0;
  167. G_zero(gvl->slice, sizeof(geovol_slice *) * MAX_SLICES);
  168. gvl->slice_x_mod = 1;
  169. gvl->slice_y_mod = 1;
  170. gvl->slice_z_mod = 1;
  171. gvl->slice_draw_mode = DM_GOURAUD;
  172. gvl->hfile = -1;
  173. gvl->clientdata = NULL;
  174. return (1);
  175. }
  176. /*!
  177. \brief Remove volume set from list
  178. \param id volume set id
  179. */
  180. void gvl_delete_vol(int id)
  181. {
  182. geovol *fvl;
  183. G_debug(5, "gvl_delete_vol");
  184. fvl = gvl_get_vol(id);
  185. if (fvl) {
  186. gvl_free_vol(fvl);
  187. }
  188. return;
  189. }
  190. /*!
  191. \brief Free geovol struct
  192. \param fvl pointer to geovol struct
  193. \return -1 on failure
  194. \return 1 on success
  195. */
  196. int gvl_free_vol(geovol * fvl)
  197. {
  198. geovol *gvl;
  199. int found = 0;
  200. G_debug(5, "gvl_free_vol");
  201. if (Vol_top) {
  202. if (fvl == Vol_top) {
  203. if (Vol_top->next) {
  204. /* can't free top if last */
  205. found = 1;
  206. Vol_top = fvl->next;
  207. }
  208. else {
  209. gvl_free_volmem(fvl);
  210. G_free(fvl);
  211. Vol_top = NULL;
  212. }
  213. }
  214. else {
  215. for (gvl = Vol_top; gvl && !found; gvl = gvl->next) {
  216. /* can't free top */
  217. if (gvl->next) {
  218. if (gvl->next == fvl) {
  219. found = 1;
  220. gvl->next = fvl->next;
  221. }
  222. }
  223. }
  224. }
  225. if (found) {
  226. gvl_free_volmem(fvl);
  227. G_free(fvl);
  228. fvl = NULL;
  229. }
  230. return (1);
  231. }
  232. return (-1);
  233. }
  234. /*!
  235. \brief Free geovol struct memory
  236. \param fvl pointer to geovol struct
  237. */
  238. void gvl_free_volmem(geovol * fvl)
  239. {
  240. if (0 < fvl->hfile)
  241. gvl_file_free_datah(fvl->hfile);
  242. return;
  243. }
  244. /*!
  245. \brief Debug volume fields
  246. \param gvl pointer to geovol struct
  247. */
  248. void print_vol_fields(geovol * gvl)
  249. {
  250. G_debug(5, "ID: %d", gvl->gvol_id);
  251. G_debug(5, "cols: %d rows: %d depths: %d", gvl->cols, gvl->rows,
  252. gvl->depths);
  253. G_debug(5, "ox: %lf oy: %lf oz: %lf", gvl->ox, gvl->oy, gvl->oz);
  254. G_debug(5, "xres: %lf yres: %lf zres: %lf", gvl->xres, gvl->yres,
  255. gvl->zres);
  256. G_debug(5, "xmin: %f ymin: %f zmin: %f", gvl->xmin, gvl->ymin, gvl->zmin);
  257. G_debug(5, "xmax: %f ymax: %f zmax: %f", gvl->xmax, gvl->ymax, gvl->zmax);
  258. G_debug(5, "x_trans: %f y_trans: %f z_trans: %f", gvl->x_trans,
  259. gvl->y_trans, gvl->z_trans);
  260. return;
  261. }
  262. /*!
  263. \brief Get volume x-extent value
  264. \param gvl pointer to geovol struct
  265. \param[out] min x-min value
  266. \param[out] max y-max value
  267. \return 1
  268. */
  269. int gvl_get_xextents(geovol * gvl, float *min, float *max)
  270. {
  271. *min = gvl->xmin + gvl->x_trans;
  272. *max = gvl->xmax + gvl->x_trans;
  273. return (1);
  274. }
  275. /*!
  276. \brief Get volume y-extent value
  277. \param gvl pointer to geovol struct
  278. \param[out] min y-min value
  279. \param[out] max y-max value
  280. \return 1
  281. */
  282. int gvl_get_yextents(geovol * gvl, float *min, float *max)
  283. {
  284. *min = gvl->ymin + gvl->y_trans;
  285. *max = gvl->ymax + gvl->y_trans;
  286. return (1);
  287. }
  288. /*!
  289. \brief Get volume z-extent value
  290. \param gvl pointer to geovol struct
  291. \param[out] min z-min value
  292. \param[out] max z-max value
  293. \return 1
  294. */
  295. int gvl_get_zextents(geovol * gvl, float *min, float *max)
  296. {
  297. *min = gvl->zmin + gvl->z_trans;
  298. *max = gvl->zmax + gvl->z_trans;
  299. return (1);
  300. }
  301. /*!
  302. \brief Get volume x-range value
  303. \param[out] min x-min value
  304. \param[out] max x-max value
  305. \return 1
  306. */
  307. int gvl_get_xrange(float *min, float *max)
  308. {
  309. geovol *gvl;
  310. float tmin, tmax;
  311. if (Vol_top) {
  312. gvl_get_xextents(Vol_top, &tmin, &tmax);
  313. *min = tmin;
  314. *max = tmax;
  315. }
  316. else {
  317. return (-1);
  318. }
  319. for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
  320. gvl_get_xextents(gvl, &tmin, &tmax);
  321. if (tmin < *min) {
  322. *min = tmin;
  323. }
  324. if (tmax > *max) {
  325. *max = tmax;
  326. }
  327. }
  328. return (1);
  329. }
  330. /*!
  331. \brief Get volume y-range value
  332. \param[out] min y-min value
  333. \param[out] max y-max value
  334. \return 1
  335. */
  336. int gvl_get_yrange(float *min, float *max)
  337. {
  338. geovol *gvl;
  339. float tmin, tmax;
  340. if (Vol_top) {
  341. gvl_get_yextents(Vol_top, &tmin, &tmax);
  342. *min = tmin;
  343. *max = tmax;
  344. }
  345. else {
  346. return (-1);
  347. }
  348. for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
  349. gvl_get_yextents(gvl, &tmin, &tmax);
  350. if (tmin < *min) {
  351. *min = tmin;
  352. }
  353. if (tmax > *max) {
  354. *max = tmax;
  355. }
  356. }
  357. return (1);
  358. }
  359. /*!
  360. \brief Get volume z-range value
  361. \param[out] min z-min value
  362. \param[out] max z-max value
  363. \return 1
  364. */
  365. int gvl_get_zrange(float *min, float *max)
  366. {
  367. geovol *gvl;
  368. float tmin, tmax;
  369. if (Vol_top) {
  370. gvl_get_zextents(Vol_top, &tmin, &tmax);
  371. *min = tmin;
  372. *max = tmax;
  373. }
  374. else {
  375. return (-1);
  376. }
  377. for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
  378. gvl_get_zextents(gvl, &tmin, &tmax);
  379. if (tmin < *min) {
  380. *min = tmin;
  381. }
  382. if (tmax > *max) {
  383. *max = tmax;
  384. }
  385. }
  386. return (1);
  387. }
  388. /************************************************************************/
  389. /* ISOSURFACES */
  390. /************************************************************************/
  391. /*!
  392. \brief Initialize geovol_isosurf struct
  393. \param isosurf pointer to geovol_isosurf struct
  394. \return -1 on failure
  395. \return 1 on success
  396. */
  397. int gvl_isosurf_init(geovol_isosurf * isosurf)
  398. {
  399. int i;
  400. G_debug(5, "gvl_isosurf_init");
  401. if (!isosurf)
  402. return (-1);
  403. for (i = 0; i < MAX_ATTS; i++) {
  404. isosurf->att[i].att_src = NOTSET_ATT;
  405. isosurf->att[i].constant = 0.;
  406. isosurf->att[i].hfile = -1;
  407. isosurf->att[i].user_func = NULL;
  408. isosurf->att[i].att_data = NULL;
  409. isosurf->att[i].changed = 0;
  410. }
  411. isosurf->data = NULL;
  412. isosurf->data_desc = 0;
  413. isosurf->inout_mode = 0;
  414. return (1);
  415. }
  416. /*!
  417. \brief Free geovol_isosurf struct
  418. \param isosurf pointer to geovol_isosurf struct
  419. \return -1 on failure
  420. \return 1 on success
  421. */
  422. int gvl_isosurf_freemem(geovol_isosurf * isosurf)
  423. {
  424. int i;
  425. G_debug(5, "gvl_isosurf_freemem");
  426. if (!isosurf)
  427. return (-1);
  428. for (i = 0; i < MAX_ATTS; i++) {
  429. gvl_isosurf_set_att_src(isosurf, i, NOTSET_ATT);
  430. }
  431. G_free(isosurf->data);
  432. return (1);
  433. }
  434. /*!
  435. \brief Get isosurface of given volume set
  436. \param id volume set id
  437. \param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  438. \return pointer to geovol_isosurf struct
  439. \return NULL on failure
  440. */
  441. geovol_isosurf *gvl_isosurf_get_isosurf(int id, int isosurf_id)
  442. {
  443. geovol *gvl;
  444. G_debug(5, "gvl_isosurf_get_isosurf(): id=%d isosurf=%d", id, isosurf_id);
  445. gvl = gvl_get_vol(id);
  446. if (gvl) {
  447. if ((isosurf_id < 0) || (isosurf_id > (gvl->n_isosurfs - 1)))
  448. return (NULL);
  449. return gvl->isosurf[isosurf_id];
  450. }
  451. return (NULL);
  452. }
  453. /*!
  454. \brief Get attribute source
  455. \param isosurf pointer to geovol_isosurf struct
  456. \param desc attribute id
  457. \return -1 on failure
  458. \return attribute value
  459. */
  460. int gvl_isosurf_get_att_src(geovol_isosurf * isosurf, int desc)
  461. {
  462. G_debug(5, "isosurf_get_att_src");
  463. if (!LEGAL_ATT(desc)) {
  464. return (-1);
  465. }
  466. if (isosurf) {
  467. return (isosurf->att[desc].att_src);
  468. }
  469. return (-1);
  470. }
  471. /*!
  472. \brief Set attribute source
  473. \param isosurf pointer to geovol_isosurf struct
  474. \param desc attribute id
  475. \param src attribute value
  476. \return -1 on failure
  477. \return 1 on success
  478. */
  479. int gvl_isosurf_set_att_src(geovol_isosurf * isosurf, int desc, int src)
  480. {
  481. G_debug(5, "gvl_isosurf_set_att_src");
  482. /* check if old source was MAP_ATT, deattach volfile */
  483. if (MAP_ATT == gvl_isosurf_get_att_src(isosurf, desc)) {
  484. gvl_file_free_datah(isosurf->att[desc].hfile);
  485. if (desc == ATT_COLOR) {
  486. Gvl_unload_colors_data(isosurf->att[desc].att_data);
  487. }
  488. }
  489. if (isosurf && LEGAL_SRC(src)) {
  490. isosurf->att[desc].att_src = src;
  491. gvl_isosurf_set_att_changed(isosurf, desc);
  492. return (1);
  493. }
  494. return (-1);
  495. }
  496. /*!
  497. \brief Set isosurface attribute constant
  498. \param isosurf pointer to geovol_isosurf struct
  499. \param desc attribute descriptor
  500. \param constant attribute value
  501. \return -1 on failure
  502. \return 1 on success
  503. */
  504. int gvl_isosurf_set_att_const(geovol_isosurf * isosurf, int desc,
  505. float constant)
  506. {
  507. G_debug(5, "gvl_isosurf_set_att_const(): att=%d, const=%f",
  508. desc, constant);
  509. if (isosurf) {
  510. isosurf->att[desc].constant = constant;
  511. gvl_isosurf_set_att_src(isosurf, desc, CONST_ATT);
  512. return (1);
  513. }
  514. return (-1);
  515. }
  516. /*!
  517. \brief Set attribute map
  518. \param isosurf pointer to geovol_isosurf struct
  519. \param desc attribute id
  520. \param filename filename
  521. \return -1 on failure
  522. \return 1 on success
  523. */
  524. int gvl_isosurf_set_att_map(geovol_isosurf * isosurf, int desc,
  525. const char *filename)
  526. {
  527. int hfile;
  528. G_debug(5, "gvl_isosurf_set_att_map(): att=%d map=%s", desc, filename);
  529. if (isosurf) {
  530. if (0 > (hfile = gvl_file_newh(filename, VOL_FTYPE_G3D)))
  531. return (-1);
  532. gvl_isosurf_set_att_src(isosurf, desc, MAP_ATT);
  533. isosurf->att[desc].hfile = hfile;
  534. if (ATT_COLOR == desc) {
  535. Gvl_load_colors_data(&(isosurf->att[desc].att_data), filename);
  536. }
  537. return (1);
  538. }
  539. return (-1);
  540. }
  541. /*!
  542. \brief Set attribute changed
  543. \param isosurf pointer to geovol_isosurf struct
  544. \param desc attribute id
  545. \return -1 on failure
  546. \return 1 on success
  547. */
  548. int gvl_isosurf_set_att_changed(geovol_isosurf * isosurf, int desc)
  549. {
  550. int i;
  551. G_debug(5, "gvl_isosurf_set_att_changed");
  552. if (isosurf && LEGAL_ATT(desc)) {
  553. isosurf->att[desc].changed = 1;
  554. if ((desc == ATT_TOPO) || (desc == ATT_MASK)) {
  555. for (i = 1; i < MAX_ATTS; i++)
  556. isosurf->att[i].changed = 1;
  557. }
  558. return (1);
  559. }
  560. return (-1);
  561. }
  562. /************************************************************************/
  563. /* SLICES */
  564. /************************************************************************/
  565. /*!
  566. \brief Initialize geovol_slice struct
  567. \param slice pointer to geovol_slice struct
  568. \return -1 on failure
  569. \return 1 on success
  570. */
  571. int gvl_slice_init(geovol_slice * slice)
  572. {
  573. G_debug(5, "gvl_slice_init");
  574. if (!slice)
  575. return (-1);
  576. slice->data = NULL;
  577. slice->changed = 0;
  578. slice->mode = 1;
  579. slice->transp = 0;
  580. slice->z1 = 0;
  581. slice->z2 = 99;
  582. return (1);
  583. }
  584. /*!
  585. \brief Free geovol_slice struct
  586. \param slice pointer to geovol_slice struct
  587. \return -1 on failure
  588. \return 1 on success
  589. */
  590. int gvl_slice_freemem(geovol_slice * slice)
  591. {
  592. G_debug(5, "gvl_slice_freemem");
  593. if (!slice)
  594. return (-1);
  595. G_free(slice->data);
  596. return (1);
  597. }
  598. /*!
  599. \brief Get geovol_slice struct
  600. \param id volume set id
  601. \param slice_id slice id
  602. \return pointer to geovol_slice struct
  603. \return NULL on failure
  604. */
  605. geovol_slice *gvl_slice_get_slice(int id, int slice_id)
  606. {
  607. geovol *gvl;
  608. gvl = gvl_get_vol(id);
  609. if (gvl) {
  610. if ((slice_id < 0) || (slice_id > (gvl->n_slices - 1)))
  611. return (NULL);
  612. return gvl->slice[slice_id];
  613. }
  614. return (NULL);
  615. }