gvl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*!
  2. \file lib/ogsf/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/ogsf.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->draw_wire = 0;
  161. gvl->n_isosurfs = 0;
  162. G_zero(gvl->isosurf, sizeof(geovol_isosurf *) * MAX_ISOSURFS);
  163. gvl->isosurf_x_mod = 1;
  164. gvl->isosurf_y_mod = 1;
  165. gvl->isosurf_z_mod = 1;
  166. gvl->isosurf_draw_mode = DM_GOURAUD;
  167. gvl->n_slices = 0;
  168. G_zero(gvl->slice, sizeof(geovol_slice *) * MAX_SLICES);
  169. gvl->slice_x_mod = 1;
  170. gvl->slice_y_mod = 1;
  171. gvl->slice_z_mod = 1;
  172. gvl->slice_draw_mode = DM_GOURAUD;
  173. gvl->hfile = -1;
  174. gvl->clientdata = NULL;
  175. return (1);
  176. }
  177. /*!
  178. \brief Remove volume set from list
  179. \param id volume set id
  180. */
  181. void gvl_delete_vol(int id)
  182. {
  183. geovol *fvl;
  184. G_debug(5, "gvl_delete_vol");
  185. fvl = gvl_get_vol(id);
  186. if (fvl) {
  187. gvl_free_vol(fvl);
  188. }
  189. return;
  190. }
  191. /*!
  192. \brief Free geovol struct
  193. \param fvl pointer to geovol struct
  194. \return -1 on failure
  195. \return 1 on success
  196. */
  197. int gvl_free_vol(geovol * fvl)
  198. {
  199. geovol *gvl;
  200. int found = 0;
  201. G_debug(5, "gvl_free_vol");
  202. if (Vol_top) {
  203. if (fvl == Vol_top) {
  204. if (Vol_top->next) {
  205. /* can't free top if last */
  206. found = 1;
  207. Vol_top = fvl->next;
  208. }
  209. else {
  210. gvl_free_volmem(fvl);
  211. G_free(fvl);
  212. Vol_top = NULL;
  213. }
  214. }
  215. else {
  216. for (gvl = Vol_top; gvl && !found; gvl = gvl->next) {
  217. /* can't free top */
  218. if (gvl->next) {
  219. if (gvl->next == fvl) {
  220. found = 1;
  221. gvl->next = fvl->next;
  222. }
  223. }
  224. }
  225. }
  226. if (found) {
  227. gvl_free_volmem(fvl);
  228. G_free(fvl);
  229. fvl = NULL;
  230. }
  231. return (1);
  232. }
  233. return (-1);
  234. }
  235. /*!
  236. \brief Free geovol struct memory
  237. \param fvl pointer to geovol struct
  238. */
  239. void gvl_free_volmem(geovol * fvl)
  240. {
  241. if (0 < fvl->hfile)
  242. gvl_file_free_datah(fvl->hfile);
  243. return;
  244. }
  245. /*!
  246. \brief Debug volume fields
  247. \param gvl pointer to geovol struct
  248. */
  249. void print_vol_fields(geovol * gvl)
  250. {
  251. G_debug(5, "ID: %d", gvl->gvol_id);
  252. G_debug(5, "cols: %d rows: %d depths: %d", gvl->cols, gvl->rows,
  253. gvl->depths);
  254. G_debug(5, "ox: %lf oy: %lf oz: %lf", gvl->ox, gvl->oy, gvl->oz);
  255. G_debug(5, "xres: %lf yres: %lf zres: %lf", gvl->xres, gvl->yres,
  256. gvl->zres);
  257. G_debug(5, "xmin: %f ymin: %f zmin: %f", gvl->xmin, gvl->ymin, gvl->zmin);
  258. G_debug(5, "xmax: %f ymax: %f zmax: %f", gvl->xmax, gvl->ymax, gvl->zmax);
  259. G_debug(5, "x_trans: %f y_trans: %f z_trans: %f", gvl->x_trans,
  260. gvl->y_trans, gvl->z_trans);
  261. return;
  262. }
  263. /*!
  264. \brief Get volume x-extent value
  265. \param gvl pointer to geovol struct
  266. \param[out] min x-min value
  267. \param[out] max y-max value
  268. \return 1
  269. */
  270. int gvl_get_xextents(geovol * gvl, float *min, float *max)
  271. {
  272. *min = gvl->xmin + gvl->x_trans;
  273. *max = gvl->xmax + gvl->x_trans;
  274. return (1);
  275. }
  276. /*!
  277. \brief Get volume y-extent value
  278. \param gvl pointer to geovol struct
  279. \param[out] min y-min value
  280. \param[out] max y-max value
  281. \return 1
  282. */
  283. int gvl_get_yextents(geovol * gvl, float *min, float *max)
  284. {
  285. *min = gvl->ymin + gvl->y_trans;
  286. *max = gvl->ymax + gvl->y_trans;
  287. return (1);
  288. }
  289. /*!
  290. \brief Get volume z-extent value
  291. \param gvl pointer to geovol struct
  292. \param[out] min z-min value
  293. \param[out] max z-max value
  294. \return 1
  295. */
  296. int gvl_get_zextents(geovol * gvl, float *min, float *max)
  297. {
  298. *min = gvl->zmin + gvl->z_trans;
  299. *max = gvl->zmax + gvl->z_trans;
  300. return (1);
  301. }
  302. /*!
  303. \brief Get volume x-range value
  304. \param[out] min x-min value
  305. \param[out] max x-max value
  306. \return 1
  307. */
  308. int gvl_get_xrange(float *min, float *max)
  309. {
  310. geovol *gvl;
  311. float tmin, tmax;
  312. if (Vol_top) {
  313. gvl_get_xextents(Vol_top, &tmin, &tmax);
  314. *min = tmin;
  315. *max = tmax;
  316. }
  317. else {
  318. return (-1);
  319. }
  320. for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
  321. gvl_get_xextents(gvl, &tmin, &tmax);
  322. if (tmin < *min) {
  323. *min = tmin;
  324. }
  325. if (tmax > *max) {
  326. *max = tmax;
  327. }
  328. }
  329. return (1);
  330. }
  331. /*!
  332. \brief Get volume y-range value
  333. \param[out] min y-min value
  334. \param[out] max y-max value
  335. \return 1
  336. */
  337. int gvl_get_yrange(float *min, float *max)
  338. {
  339. geovol *gvl;
  340. float tmin, tmax;
  341. if (Vol_top) {
  342. gvl_get_yextents(Vol_top, &tmin, &tmax);
  343. *min = tmin;
  344. *max = tmax;
  345. }
  346. else {
  347. return (-1);
  348. }
  349. for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
  350. gvl_get_yextents(gvl, &tmin, &tmax);
  351. if (tmin < *min) {
  352. *min = tmin;
  353. }
  354. if (tmax > *max) {
  355. *max = tmax;
  356. }
  357. }
  358. return (1);
  359. }
  360. /*!
  361. \brief Get volume z-range value
  362. \param[out] min z-min value
  363. \param[out] max z-max value
  364. \return 1
  365. */
  366. int gvl_get_zrange(float *min, float *max)
  367. {
  368. geovol *gvl;
  369. float tmin, tmax;
  370. if (Vol_top) {
  371. gvl_get_zextents(Vol_top, &tmin, &tmax);
  372. *min = tmin;
  373. *max = tmax;
  374. }
  375. else {
  376. return (-1);
  377. }
  378. for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
  379. gvl_get_zextents(gvl, &tmin, &tmax);
  380. if (tmin < *min) {
  381. *min = tmin;
  382. }
  383. if (tmax > *max) {
  384. *max = tmax;
  385. }
  386. }
  387. return (1);
  388. }
  389. /************************************************************************/
  390. /* ISOSURFACES */
  391. /************************************************************************/
  392. /*!
  393. \brief Initialize geovol_isosurf struct
  394. \param isosurf pointer to geovol_isosurf struct
  395. \return -1 on failure
  396. \return 1 on success
  397. */
  398. int gvl_isosurf_init(geovol_isosurf * isosurf)
  399. {
  400. int i;
  401. G_debug(5, "gvl_isosurf_init");
  402. if (!isosurf)
  403. return (-1);
  404. for (i = 0; i < MAX_ATTS; i++) {
  405. isosurf->att[i].att_src = NOTSET_ATT;
  406. isosurf->att[i].constant = 0.;
  407. isosurf->att[i].hfile = -1;
  408. isosurf->att[i].user_func = NULL;
  409. isosurf->att[i].att_data = NULL;
  410. isosurf->att[i].changed = 0;
  411. }
  412. isosurf->data = NULL;
  413. isosurf->data_desc = 0;
  414. isosurf->inout_mode = 0;
  415. return (1);
  416. }
  417. /*!
  418. \brief Free geovol_isosurf struct
  419. \param isosurf pointer to geovol_isosurf struct
  420. \return -1 on failure
  421. \return 1 on success
  422. */
  423. int gvl_isosurf_freemem(geovol_isosurf * isosurf)
  424. {
  425. int i;
  426. G_debug(5, "gvl_isosurf_freemem");
  427. if (!isosurf)
  428. return (-1);
  429. for (i = 0; i < MAX_ATTS; i++) {
  430. gvl_isosurf_set_att_src(isosurf, i, NOTSET_ATT);
  431. }
  432. G_free(isosurf->data);
  433. return (1);
  434. }
  435. /*!
  436. \brief Get isosurface of given volume set
  437. \param id volume set id
  438. \param isosurf_id isosurface id (0 - MAX_ISOSURFS)
  439. \return pointer to geovol_isosurf struct
  440. \return NULL on failure
  441. */
  442. geovol_isosurf *gvl_isosurf_get_isosurf(int id, int isosurf_id)
  443. {
  444. geovol *gvl;
  445. G_debug(5, "gvl_isosurf_get_isosurf(): id=%d isosurf=%d", id, isosurf_id);
  446. gvl = gvl_get_vol(id);
  447. if (gvl) {
  448. if ((isosurf_id < 0) || (isosurf_id > (gvl->n_isosurfs - 1)))
  449. return (NULL);
  450. return gvl->isosurf[isosurf_id];
  451. }
  452. return (NULL);
  453. }
  454. /*!
  455. \brief Get attribute source
  456. \param isosurf pointer to geovol_isosurf struct
  457. \param desc attribute id
  458. \return -1 on failure
  459. \return attribute value
  460. */
  461. int gvl_isosurf_get_att_src(geovol_isosurf * isosurf, int desc)
  462. {
  463. G_debug(5, "isosurf_get_att_src");
  464. if (!LEGAL_ATT(desc)) {
  465. return (-1);
  466. }
  467. if (isosurf) {
  468. return (isosurf->att[desc].att_src);
  469. }
  470. return (-1);
  471. }
  472. /*!
  473. \brief Set attribute source
  474. \param isosurf pointer to geovol_isosurf struct
  475. \param desc attribute id
  476. \param src attribute value
  477. \return -1 on failure
  478. \return 1 on success
  479. */
  480. int gvl_isosurf_set_att_src(geovol_isosurf * isosurf, int desc, int src)
  481. {
  482. G_debug(5, "gvl_isosurf_set_att_src");
  483. /* check if old source was MAP_ATT, deattach volfile */
  484. if (MAP_ATT == gvl_isosurf_get_att_src(isosurf, desc)) {
  485. gvl_file_free_datah(isosurf->att[desc].hfile);
  486. if (desc == ATT_COLOR) {
  487. Gvl_unload_colors_data(isosurf->att[desc].att_data);
  488. }
  489. }
  490. if (isosurf && LEGAL_SRC(src)) {
  491. isosurf->att[desc].att_src = src;
  492. gvl_isosurf_set_att_changed(isosurf, desc);
  493. return (1);
  494. }
  495. return (-1);
  496. }
  497. /*!
  498. \brief Set isosurface attribute constant
  499. \param isosurf pointer to geovol_isosurf struct
  500. \param desc attribute descriptor
  501. \param constant attribute value
  502. \return -1 on failure
  503. \return 1 on success
  504. */
  505. int gvl_isosurf_set_att_const(geovol_isosurf * isosurf, int desc,
  506. float constant)
  507. {
  508. G_debug(5, "gvl_isosurf_set_att_const(): att=%d, const=%f",
  509. desc, constant);
  510. if (isosurf) {
  511. isosurf->att[desc].constant = constant;
  512. gvl_isosurf_set_att_src(isosurf, desc, CONST_ATT);
  513. return (1);
  514. }
  515. return (-1);
  516. }
  517. /*!
  518. \brief Set attribute map
  519. \param isosurf pointer to geovol_isosurf struct
  520. \param desc attribute id
  521. \param filename filename
  522. \return -1 on failure
  523. \return 1 on success
  524. */
  525. int gvl_isosurf_set_att_map(geovol_isosurf * isosurf, int desc,
  526. const char *filename)
  527. {
  528. int hfile;
  529. G_debug(5, "gvl_isosurf_set_att_map(): att=%d map=%s", desc, filename);
  530. if (isosurf) {
  531. if (0 > (hfile = gvl_file_newh(filename, VOL_FTYPE_RASTER3D)))
  532. return (-1);
  533. gvl_isosurf_set_att_src(isosurf, desc, MAP_ATT);
  534. isosurf->att[desc].hfile = hfile;
  535. if (ATT_COLOR == desc) {
  536. Gvl_load_colors_data(&(isosurf->att[desc].att_data), filename);
  537. }
  538. return (1);
  539. }
  540. return (-1);
  541. }
  542. /*!
  543. \brief Set attribute changed
  544. \param isosurf pointer to geovol_isosurf struct
  545. \param desc attribute id
  546. \return -1 on failure
  547. \return 1 on success
  548. */
  549. int gvl_isosurf_set_att_changed(geovol_isosurf * isosurf, int desc)
  550. {
  551. int i;
  552. G_debug(5, "gvl_isosurf_set_att_changed");
  553. if (isosurf && LEGAL_ATT(desc)) {
  554. isosurf->att[desc].changed = 1;
  555. if ((desc == ATT_TOPO) || (desc == ATT_MASK)) {
  556. for (i = 1; i < MAX_ATTS; i++)
  557. isosurf->att[i].changed = 1;
  558. }
  559. return (1);
  560. }
  561. return (-1);
  562. }
  563. /************************************************************************/
  564. /* SLICES */
  565. /************************************************************************/
  566. /*!
  567. \brief Initialize geovol_slice struct
  568. \param slice pointer to geovol_slice struct
  569. \return -1 on failure
  570. \return 1 on success
  571. */
  572. int gvl_slice_init(geovol_slice * slice)
  573. {
  574. G_debug(5, "gvl_slice_init");
  575. if (!slice)
  576. return (-1);
  577. slice->data = NULL;
  578. slice->changed = 0;
  579. slice->mode = 1;
  580. slice->transp = 0;
  581. slice->z1 = 0;
  582. slice->z2 = 99;
  583. return (1);
  584. }
  585. /*!
  586. \brief Free geovol_slice struct
  587. \param slice pointer to geovol_slice struct
  588. \return -1 on failure
  589. \return 1 on success
  590. */
  591. int gvl_slice_freemem(geovol_slice * slice)
  592. {
  593. G_debug(5, "gvl_slice_freemem");
  594. if (!slice)
  595. return (-1);
  596. G_free(slice->data);
  597. return (1);
  598. }
  599. /*!
  600. \brief Get geovol_slice struct
  601. \param id volume set id
  602. \param slice_id slice id
  603. \return pointer to geovol_slice struct
  604. \return NULL on failure
  605. */
  606. geovol_slice *gvl_slice_get_slice(int id, int slice_id)
  607. {
  608. geovol *gvl;
  609. gvl = gvl_get_vol(id);
  610. if (gvl) {
  611. if ((slice_id < 0) || (slice_id > (gvl->n_slices - 1)))
  612. return (NULL);
  613. return gvl->slice[slice_id];
  614. }
  615. return (NULL);
  616. }