gs.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /*!
  2. \file lib/ogsf/gs.c
  3. \brief OGSF library - loading and manipulating surfaces (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 USACERL, GMSL/University of Illinois (January 1993)
  11. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <grass/ogsf.h>
  16. #include <grass/glocale.h>
  17. #include "gsget.h"
  18. #include "rowcol.h"
  19. #define FIRST_SURF_ID 110658
  20. static geosurf *Surf_top;
  21. static int Invertmask;
  22. /***********************************************************************/
  23. void gs_err(const char *msg)
  24. {
  25. G_warning("%s", msg);
  26. return;
  27. }
  28. /*!
  29. \brief Initialize library
  30. Still need to take care of library initialization,
  31. probably want to define a Surf_top of constant value (i.e., 0)
  32. */
  33. void gs_init(void)
  34. {
  35. Surf_top = NULL;
  36. return;
  37. }
  38. /*!
  39. \brief Get geosurf struct
  40. \param id surface id
  41. \return pointer to geosurf struct
  42. \return NULL if not found
  43. */
  44. geosurf *gs_get_surf(int id)
  45. {
  46. geosurf *gs;
  47. G_debug(5, "gs_get_surf():");
  48. for (gs = Surf_top; gs; gs = gs->next) {
  49. if (gs->gsurf_id == id) {
  50. G_debug(5, " id=%d", id);
  51. return (gs);
  52. }
  53. }
  54. return (NULL);
  55. }
  56. /*!
  57. \brief Get previous geosurf struct
  58. \param id current surface id
  59. \return pointer to geosurf struct
  60. \return NULL if not found
  61. */
  62. geosurf *gs_get_prev_surface(int id)
  63. {
  64. geosurf *ps;
  65. G_debug(5, "gs_get_prev_surface");
  66. for (ps = Surf_top; ps; ps = ps->next) {
  67. if (ps->gsurf_id == id - 1) {
  68. return (ps);
  69. }
  70. }
  71. return (NULL);
  72. }
  73. /*!
  74. \brief Get array of geosurf structs
  75. \param gsurfs pointer to array
  76. \return number of geosurfs
  77. */
  78. int gs_getall_surfaces(geosurf ** gsurfs)
  79. {
  80. geosurf *gs;
  81. int i;
  82. for (i = 0, gs = Surf_top; gs; gs = gs->next, i++) {
  83. gsurfs[i] = gs;
  84. }
  85. G_debug(5, "gs_num_surfaces(): num=%d", i);
  86. return (i);
  87. }
  88. /*!
  89. \brief Get number of surfaces
  90. \return number of surfaces
  91. */
  92. int gs_num_surfaces(void)
  93. {
  94. geosurf *gs;
  95. int i;
  96. for (i = 0, gs = Surf_top; gs; gs = gs->next, i++) ;
  97. G_debug(5, "gs_num_surfaces(): num=%d", i);
  98. return (i);
  99. }
  100. /*!
  101. \brief Check if attribute is set
  102. \param surf pointer to gsurf or NULL to look at all geosurfs
  103. \param att attribute id
  104. \return 1 attribute is set up
  105. \return 0 attribute is not set up
  106. */
  107. int gs_att_is_set(geosurf * surf, IFLAG att)
  108. {
  109. geosurf *gs;
  110. if (surf) {
  111. return (NOTSET_ATT != surf->att[att].att_src);
  112. }
  113. /* if surf == NULL, look at all surfs */
  114. for (gs = Surf_top; gs; gs = gs->next) {
  115. if (NOTSET_ATT != gs->att[att].att_src) {
  116. return (1);
  117. }
  118. }
  119. return (0);
  120. }
  121. /*!
  122. \brief Get last allocated geosurf struct from list
  123. \return pointer to geosurf struct
  124. */
  125. geosurf *gs_get_last_surface(void)
  126. {
  127. geosurf *ls;
  128. if (!Surf_top) {
  129. return (NULL);
  130. }
  131. for (ls = Surf_top; ls->next; ls = ls->next) ;
  132. G_debug(5, "gs_get_last_surface(): last surface id=%d", ls->gsurf_id);
  133. return (ls);
  134. }
  135. /*!
  136. \brief Allocate new geosurf struct
  137. \return pointer to geosurf struct
  138. */
  139. geosurf *gs_get_new_surface(void)
  140. {
  141. geosurf *ns, *ls;
  142. ns = (geosurf *) G_malloc(sizeof(geosurf)); /* G_fatal_error */
  143. if (!ns) {
  144. return (NULL);
  145. }
  146. if ((ls = gs_get_last_surface())) {
  147. ls->next = ns;
  148. ns->gsurf_id = ls->gsurf_id + 1;
  149. }
  150. else {
  151. Surf_top = ns;
  152. ns->gsurf_id = FIRST_SURF_ID;
  153. }
  154. ns->next = NULL;
  155. G_debug(5, "gs_get_new_surface(): id=%d", ns->gsurf_id);
  156. return (ns);
  157. }
  158. /*!
  159. \brief Initialize allocated geosurf struct
  160. \todo Now xmin & ox are the same, right? - get rid of ox, oy in geosurf struct?
  161. \param gs pointer to geosurf struct
  162. \param ox,oy x/y origin coordinates
  163. \param rows number of rows
  164. \param cols number of cols
  165. \param xres,yres x/y resolution value
  166. \return -1 on error
  167. \return 0 on success
  168. */
  169. int gs_init_surf(geosurf * gs, double ox, double oy, int rows, int cols,
  170. double xres, double yres)
  171. {
  172. geosurf *ps;
  173. int i;
  174. if (!gs) {
  175. return (-1);
  176. }
  177. G_debug(5, "gs_init_surf() id=%d", gs->gsurf_id);
  178. /* default attributes */
  179. for (i = 0; i < MAX_ATTS; i++) {
  180. gs->att[i].att_src = NOTSET_ATT;
  181. gs->att[i].att_type = ATTY_INT;
  182. gs->att[i].hdata = -1;
  183. gs->att[i].user_func = NULL;
  184. gs->att[i].constant = 0.;
  185. gs->att[i].lookup = NULL;
  186. gs->att[i].min_nz = gs->att[i].max_nz = gs->att[i].range_nz = 0;
  187. gs->att[i].default_null = 0.;
  188. }
  189. /* default values */
  190. gs->ox = ox;
  191. gs->oy = oy;
  192. gs->rows = rows;
  193. gs->cols = cols;
  194. gs->xres = xres;
  195. gs->yres = yres;
  196. gs->x_mod = 2;
  197. gs->y_mod = 2;
  198. gs->x_modw = rows / 30;
  199. gs->y_modw = rows / 30;
  200. gs->xmin = ox;
  201. gs->xmax = ox + (cols - 1) * xres;
  202. gs->xrange = gs->xmax - gs->xmin;
  203. gs->ymin = oy;
  204. gs->ymax = oy + (rows - 1) * yres;
  205. gs->yrange = gs->ymax - gs->ymin;
  206. gs->zmin = gs->zmin_nz = gs->zminmasked = 0;
  207. gs->zmax = gs->zmax_nz = 0;
  208. gs->zrange = gs->zrange_nz = 0;
  209. gs->wire_color = 0x00888888;
  210. gs->x_trans = gs->y_trans = gs->z_trans = 0.0;
  211. gs->nz_topo = gs->nz_color = 0;
  212. gs->norm_needupdate = 1;
  213. gs->mask_needupdate = 1;
  214. gs->curmask = NULL;
  215. gs->norms = NULL;
  216. gs->draw_mode = DM_GOURAUD;
  217. /* default z_exag value */
  218. if (gs->gsurf_id == FIRST_SURF_ID) {
  219. gs->z_exag = 1.0;
  220. }
  221. else {
  222. ps = gs_get_prev_surface(gs->gsurf_id);
  223. gs->z_exag = ps->z_exag;
  224. }
  225. return (0);
  226. }
  227. /*!
  228. \brief Init geosurf normbuff
  229. \param gs pointer to geosurf struct
  230. \return 0 on error
  231. \return 1 on success
  232. */
  233. int gs_init_normbuff(geosurf * gs)
  234. {
  235. long size;
  236. if (!gs) {
  237. return (0);
  238. }
  239. if (gs->norms) {
  240. G_free(gs->norms);
  241. }
  242. size = gs->rows * gs->cols * sizeof(unsigned long);
  243. gs->norms = (unsigned long *)G_malloc(size); /* G_fatal_error */
  244. if (!gs->norms) {
  245. return (-1);
  246. }
  247. gs->norm_needupdate = 1;
  248. return (1);
  249. }
  250. /*!
  251. \brief Debugging, print 'from/to' model coordinates to stderr
  252. \todo G_debug ?
  253. \param ft pointer to coordinates
  254. */
  255. void print_frto(float (*ft)[4])
  256. {
  257. fprintf(stderr, "FROM: %f, %f, %f\n", ft[FROM][X], ft[FROM][Y],
  258. ft[FROM][Z]);
  259. fprintf(stderr, "TO: %f, %f, %f\n", ft[TO][X], ft[TO][Y], ft[TO][Z]);
  260. return;
  261. }
  262. /*!
  263. \brief Debugging, print 'to' real coordinates to stderr
  264. \todo G_debug ?
  265. \param ft pointer to coordinates
  266. */
  267. void print_realto(float *rt)
  268. {
  269. fprintf(stderr, "REAL TO: %f, %f, %f\n", rt[X], rt[Y], rt[Z]);
  270. return;
  271. }
  272. /*!
  273. \brief Debugging, 256 integer values from buffer
  274. \todo G_debug ?
  275. \param ft pointer to buffer
  276. */
  277. void print_256lookup(int *buff)
  278. {
  279. int i;
  280. for (i = 0; i < 256; i++) {
  281. if (!(i % 8)) {
  282. fprintf(stderr, "\n");
  283. }
  284. fprintf(stderr, "%x ", buff[i]);
  285. }
  286. fprintf(stderr, "\n");
  287. return;
  288. }
  289. /*!
  290. \brief Debugging, print geosurf fields to stderr
  291. \todo G_debug ?
  292. \param s pointer to geosurf struct
  293. */
  294. void print_surf_fields(geosurf * s)
  295. {
  296. fprintf(stderr, "ID: %d\n", s->gsurf_id);
  297. fprintf(stderr, "rows: %d cols: %d\n", s->rows, s->cols);
  298. fprintf(stderr, "draw_mode: %x\n", s->draw_mode);
  299. fprintf(stderr, "wire_color: %lx\n", s->wire_color);
  300. fprintf(stderr, "ox: %lf oy: %lf\n", s->ox, s->oy);
  301. fprintf(stderr, "xres: %lf yres: %lf\n", s->xres, s->yres);
  302. fprintf(stderr, "z_exag: %f \n", s->z_exag);
  303. fprintf(stderr, "x_trans: %f y_trans: %f z_trans: %f\n",
  304. s->x_trans, s->y_trans, s->z_trans);
  305. fprintf(stderr, "xmin: %f ymin: %f zmin: %f\n",
  306. s->xmin, s->ymin, s->zmin);
  307. fprintf(stderr, "xmax: %f ymax: %f zmax: %f\n",
  308. s->xmax, s->ymax, s->zmax);
  309. fprintf(stderr, "x_mod: %d y_mod: %d x_modw: %d y_modw: %d\n",
  310. s->x_mod, s->y_mod, s->x_modw, s->y_modw);
  311. return;
  312. }
  313. /*!
  314. \brief Debugging, print geoview fields to stderr
  315. \todo G_debug ?
  316. \param gv pointer to geoview struct
  317. */
  318. void print_view_fields(geoview * gv)
  319. {
  320. fprintf(stderr, "coord_sys: %d\n", gv->coord_sys);
  321. fprintf(stderr, "view_proj: %d\n", gv->view_proj);
  322. fprintf(stderr, "infocus: %d\n", gv->infocus);
  323. print_frto(gv->from_to);
  324. fprintf(stderr, "twist: %d fov: %d\n", gv->twist, gv->fov);
  325. fprintf(stderr, "incl: %d look: %d\n", gv->incl, gv->look);
  326. fprintf(stderr, "real_to: %f %f %f\n",
  327. gv->real_to[X], gv->real_to[Y], gv->real_to[Z]);
  328. fprintf(stderr, "vert_exag: %f scale: %f \n", gv->vert_exag, gv->scale);
  329. return;
  330. }
  331. /*!
  332. \brief Set default attribute values
  333. \param gs pointer to geosurf struct
  334. \param defs array of default values (dim MAX_ATTRS)
  335. \param null_defs array of null default values (dim MAX_ATTRS)
  336. */
  337. void gs_set_defaults(geosurf * gs, float *defs, float *null_defs)
  338. {
  339. int i;
  340. G_debug(5, "gs_set_defaults(): id=%d", gs->gsurf_id);
  341. for (i = 0; i < MAX_ATTS; i++) {
  342. gs->att[i].constant = defs[i];
  343. gs->att[i].default_null = null_defs[i];
  344. gs->att[i].lookup = NULL;
  345. gs->att[i].hdata = -1;
  346. gs->att[i].att_src = NOTSET_ATT;
  347. }
  348. return;
  349. }
  350. /*!
  351. \brief Remove geosurf struct from list
  352. \param id surface id
  353. */
  354. void gs_delete_surf(int id)
  355. {
  356. geosurf *fs;
  357. G_debug(5, "gs_delete_surf");
  358. fs = gs_get_surf(id);
  359. if (fs) {
  360. gs_free_surf(fs);
  361. }
  362. return;
  363. }
  364. /*!
  365. \brief Free geosurf struct
  366. \param fs pointer to geosurf struct
  367. \return 1 found
  368. \return 0 not found
  369. \return -1 on error
  370. */
  371. int gs_free_surf(geosurf * fs)
  372. {
  373. geosurf *gs;
  374. int found = 0;
  375. G_debug(5, "gs_free_surf");
  376. if (Surf_top) {
  377. if (fs == Surf_top) {
  378. if (Surf_top->next) {
  379. /* can't free top if last */
  380. found = 1;
  381. Surf_top = fs->next;
  382. }
  383. else {
  384. gs_free_unshared_buffs(fs);
  385. if (fs->curmask) {
  386. G_free(fs->curmask);
  387. }
  388. if (fs->norms) {
  389. G_free(fs->norms);
  390. }
  391. G_free(fs);
  392. Surf_top = NULL;
  393. }
  394. }
  395. else {
  396. for (gs = Surf_top; gs && !found; gs = gs->next) {
  397. if (gs->next) {
  398. if (gs->next == fs) {
  399. found = 1;
  400. gs->next = fs->next;
  401. }
  402. }
  403. }
  404. }
  405. if (found) {
  406. gs_free_unshared_buffs(fs);
  407. if (fs->curmask) {
  408. G_free(fs->curmask);
  409. }
  410. if (fs->norms) {
  411. G_free(fs->norms);
  412. }
  413. G_free(fs);
  414. fs = NULL;
  415. }
  416. return (found);
  417. }
  418. return (-1);
  419. }
  420. /*!
  421. \brief Free unshared buffers of geosurf struct
  422. <i>fs</i> has already been taken out of the list
  423. This function is fairly revealing about how shared datsets work
  424. \param fs pointer to geosurf struct
  425. */
  426. void gs_free_unshared_buffs(geosurf * fs)
  427. {
  428. geosurf *gs;
  429. int i, j, same;
  430. int old_datah;
  431. G_debug(5, "gs_free_unshared_buffs");
  432. /* for each attribute
  433. if !same, free buff
  434. */
  435. for (i = 0; i < MAX_ATTS; i++) {
  436. same = 0;
  437. if (0 < (old_datah = fs->att[i].hdata)) {
  438. /* for ea att of all other surfs */
  439. for (gs = Surf_top; gs; gs = gs->next) {
  440. for (j = 0; j < MAX_ATTS; j++) {
  441. if ((old_datah == gs->att[j].hdata) && (fs != gs)) {
  442. same = 1;
  443. }
  444. }
  445. }
  446. if (!same) {
  447. gsds_free_datah(old_datah);
  448. }
  449. }
  450. }
  451. return;
  452. }
  453. /*!
  454. \brief Get number of reused values
  455. \param dh value
  456. \return number of reused values
  457. */
  458. int gs_num_datah_reused(int dh)
  459. {
  460. geosurf *gs;
  461. int ref, j;
  462. G_debug(5, "gs_num_datah_reused");
  463. /* for each attribute
  464. if same, ++reference
  465. */
  466. /* for ea att of all surfs */
  467. ref = 0;
  468. for (gs = Surf_top; gs; gs = gs->next) {
  469. for (j = 0; j < MAX_ATTS; j++) {
  470. if (dh == gs->att[j].hdata) {
  471. ref++;
  472. }
  473. }
  474. }
  475. return (ref);
  476. }
  477. /*!
  478. \brief Get attribute type
  479. \param gs pointer to geosurf struct
  480. \param desc attribute id
  481. \return -1 on error
  482. \return attribute type
  483. */
  484. int gs_get_att_type(geosurf * gs, int desc)
  485. {
  486. G_debug(5, "gs_get_att_type");
  487. if (!LEGAL_ATT(desc)) {
  488. return (-1);
  489. }
  490. if (gs) {
  491. if (gs->att[desc].att_src != NOTSET_ATT) {
  492. return (gs->att[desc].att_type);
  493. }
  494. }
  495. return (-1);
  496. }
  497. /*!
  498. \brief Get attribute source
  499. \param gs pointer to geosurf struct
  500. \param desc attribute id (descriptor)
  501. \return -1 on error
  502. \return attribute source id
  503. */
  504. int gs_get_att_src(geosurf * gs, int desc)
  505. {
  506. if (gs)
  507. G_debug(5, "gs_get_att_src(): id=%d, desc=%d", gs->gsurf_id, desc);
  508. if (!LEGAL_ATT(desc)) {
  509. return (-1);
  510. }
  511. if (gs) {
  512. return (gs->att[desc].att_src);
  513. }
  514. return (-1);
  515. }
  516. /*!
  517. \brief Get attribute data buffer
  518. \param gs pointer to geosurf struct
  519. \param desc attribute id (descriptor)
  520. \param to_write non-zero value for 'write'
  521. \return NULL on error
  522. \return pointer to typbuff
  523. */
  524. typbuff *gs_get_att_typbuff(geosurf * gs, int desc, int to_write)
  525. {
  526. typbuff *tb;
  527. geosurf *gsref;
  528. if (gs) {
  529. G_debug(5, "gs_get_att_typbuff(): id=%d desc=%d to_write=%d",
  530. gs->gsurf_id, desc, to_write);
  531. if ((tb = gsds_get_typbuff(gs->att[desc].hdata, to_write))) {
  532. tb->tfunc = NULL;
  533. if (desc == ATT_TOPO) {
  534. gsref = gsdiff_get_SDref();
  535. if (gsref && gsref != gs) {
  536. tb->tfunc = gsdiff_do_SD;
  537. }
  538. }
  539. return (tb);
  540. }
  541. }
  542. return (NULL);
  543. }
  544. /*!
  545. \brief Allocate attribute buffer
  546. \param gs pointer to geosurf struct
  547. \param desc attribute id (descriptor)
  548. \param type buffer type (based on raster map type)
  549. \return -1 on error
  550. \return amount of allocated memory
  551. */
  552. size_t gs_malloc_att_buff(geosurf * gs, int desc, int type)
  553. {
  554. int hdata, dims[2], ndims;
  555. G_debug(5, "gs_malloc_att_buff");
  556. if (gs) {
  557. if (0 < (hdata = gs->att[desc].hdata)) {
  558. dims[0] = gs->rows;
  559. dims[1] = gs->cols;
  560. ndims = 2;
  561. gs_set_att_type(gs, desc, type);
  562. return (gsds_alloc_typbuff(hdata, dims, ndims, type));
  563. }
  564. }
  565. return 0;
  566. }
  567. /*!
  568. \brief Allocate attribute lookup
  569. \param gs pointer to geosurf struct
  570. \param desc attribute id
  571. \return -1 on error
  572. \return pointer to typbuff (casted)
  573. */
  574. int gs_malloc_lookup(geosurf * gs, int desc)
  575. {
  576. int size;
  577. G_debug(5, "gs_malloc_lookup");
  578. if (gs) {
  579. if (gs->att[desc].lookup) {
  580. G_free(gs->att[desc].lookup);
  581. gs->att[desc].lookup = NULL;
  582. }
  583. switch (gs->att[desc].att_type) {
  584. case (ATTY_SHORT):
  585. size = 32768 * sizeof(int);
  586. /* positive integers only, because use as array index */
  587. gs->att[desc].lookup = (int *)G_malloc(size); /* G_fatal_error */
  588. if (!gs->att[desc].lookup) {
  589. return (-1);
  590. }
  591. break;
  592. case (ATTY_CHAR):
  593. size = 256 * sizeof(int);
  594. /* unsigned char */
  595. gs->att[desc].lookup = (int *)G_malloc(size);
  596. if (!gs->att[desc].lookup) {
  597. return (-1);
  598. }
  599. break;
  600. default:
  601. G_warning("bad type: gs_malloc_lookup");
  602. return (-1);
  603. }
  604. if (gs->att[desc].lookup) {
  605. return (0);
  606. }
  607. }
  608. return (-1);
  609. }
  610. /*!
  611. \brief Set attribute type
  612. \param gs pointer to geosurf struct
  613. \param desc attribute id
  614. \param type attribute type
  615. \return -1 on error
  616. \return 0 on success
  617. */
  618. int gs_set_att_type(geosurf * gs, int desc, int type)
  619. {
  620. G_debug(5, "gs_set_att_type(): desc=%d, type=%d", desc, type);
  621. if (gs && LEGAL_TYPE(type)) {
  622. gs->att[desc].att_type = type;
  623. return (0);
  624. }
  625. return (-1);
  626. }
  627. /*!
  628. \brief Set attribute source
  629. \param gs pointer to geosurf struct
  630. \param desc attribute id (descriptor)
  631. \param src source id
  632. \return -1 on error
  633. \return 0 on success
  634. */
  635. int gs_set_att_src(geosurf * gs, int desc, int src)
  636. {
  637. if (gs)
  638. G_debug(5, "gs_set_att_src(): id=%d desc=%d src=%d",
  639. gs->gsurf_id, desc, src);
  640. /* check if old source was MAP_ATT, free buff */
  641. if (MAP_ATT == gs_get_att_src(gs, desc)) {
  642. if (1 == gs_num_datah_reused(gs->att[desc].hdata)) {
  643. /* only reference */
  644. G_debug(5, "gs_set_att_src(): replacing existing map");
  645. gsds_free_datah(gs->att[desc].hdata);
  646. }
  647. if (ATT_TOPO == desc) {
  648. if (gs->norms) {
  649. G_free(gs->norms);
  650. }
  651. gs->norms = NULL;
  652. gs->norm_needupdate = 0;
  653. }
  654. }
  655. if (gs && LEGAL_SRC(src)) {
  656. gs->att[desc].att_src = src;
  657. return (0);
  658. }
  659. return (-1);
  660. }
  661. /*!
  662. \brief Set attribute constant value
  663. \todo set typbuf constant
  664. \param gs pointer to geosurf struct
  665. \param desc attribute id
  666. \param constant constant value
  667. \return 0 on success
  668. \return -1 on error
  669. */
  670. int gs_set_att_const(geosurf * gs, int desc, float constant)
  671. {
  672. if (gs) {
  673. G_debug(5, "gs_set_att_const(): id=%d, desc=%d, const=%f",
  674. gs->gsurf_id, desc, constant);
  675. gs->att[desc].constant = constant;
  676. if (ATT_MASK == desc) {
  677. gs->mask_needupdate = 1;
  678. }
  679. else {
  680. gs_set_att_src(gs, desc, CONST_ATT);
  681. }
  682. Gs_update_attrange(gs, desc);
  683. return (0);
  684. }
  685. return (-1);
  686. }
  687. /*!
  688. \brief Set geosurf mask mode
  689. \param invert invert mask
  690. */
  691. void gs_set_maskmode(int invert)
  692. {
  693. Invertmask = invert;
  694. return;
  695. }
  696. /*!
  697. \brief Check if mask is defined
  698. \param gs pointer to geosurf struct
  699. \return 1 if defined
  700. \return 0 not defined
  701. */
  702. int gs_mask_defined(geosurf * gs)
  703. {
  704. return (gs->att[ATT_MASK].att_src != NOTSET_ATT);
  705. }
  706. /*!
  707. \brief
  708. Should only be called when setting up the current mask (gs_bm.c)
  709. \param tb pointer to typbuff
  710. \param col number of cols
  711. \param row number of rows
  712. \param offset offset value
  713. \return 1
  714. \return 0
  715. */
  716. int gs_masked(typbuff * tb, int col, int row, int offset)
  717. {
  718. int ret;
  719. ret = 1;
  720. if (tb->bm) {
  721. ret = BM_get(tb->bm, col, row);
  722. }
  723. else if (tb->cb) {
  724. ret = tb->cb[offset];
  725. }
  726. else if (tb->sb) {
  727. ret = tb->sb[offset];
  728. }
  729. else if (tb->ib) {
  730. ret = tb->ib[offset];
  731. }
  732. else if (tb->fb) {
  733. ret = tb->fb[offset];
  734. }
  735. return (Invertmask ? ret : !ret);
  736. }
  737. /*!
  738. \brief
  739. Call this one when you already know att_src is MAP_ATT
  740. \param cobuff
  741. \param coloratt color attribute
  742. \param offset offset value
  743. \return packed color for catagory at offset
  744. */
  745. int gs_mapcolor(typbuff * cobuff, gsurf_att * coloratt, int offset)
  746. {
  747. if (coloratt->lookup) {
  748. /* for now, but may add larger color lookup capabilities later,
  749. so would have to use GET_MAPATT */
  750. return (coloratt->lookup[cobuff->cb[offset]]);
  751. }
  752. return (cobuff->ib[offset]);
  753. }
  754. /*
  755. In the following functions, "extents" refers to translated extents for
  756. a single surface, while "range" refers to accumulated extents of all
  757. loaded surfaces
  758. */
  759. /*!
  760. \brief Get z-extent values
  761. \todo pass flag to use zminmasked instead of zmin
  762. \param gs pointer to geosurf struct
  763. \param[out] min z-min value
  764. \param[out] max z-max value
  765. \param[out] mid z-middle value
  766. \return 1
  767. */
  768. int gs_get_zextents(geosurf * gs, float *min, float *max, float *mid)
  769. {
  770. *min = gs->zmin + gs->z_trans;
  771. *max = gs->zmax + gs->z_trans;
  772. *mid = (*max + *min) / 2.;
  773. return (1);
  774. }
  775. /*!
  776. \brief Get x-extent values
  777. \param gs pointer to geosurf struct
  778. \param[out] min x-min value
  779. \param[out] max x-max value
  780. \return 1
  781. */
  782. int gs_get_xextents(geosurf * gs, float *min, float *max)
  783. {
  784. *min = gs->xmin + gs->x_trans;
  785. *max = gs->xmax + gs->x_trans;
  786. return (1);
  787. }
  788. /*!
  789. \brief Get y-extent values
  790. \param gs pointer to geosurf struct
  791. \param[out] min y-min value
  792. \param[out] max y-max value
  793. \return 1
  794. */
  795. int gs_get_yextents(geosurf * gs, float *min, float *max)
  796. {
  797. *min = gs->ymin + gs->y_trans;
  798. *max = gs->ymax + gs->y_trans;
  799. return (1);
  800. }
  801. /*!
  802. \brief Get z-range
  803. \todo pass flag to use zminmasked instead of zmin
  804. could also have this return a weighted average for vertical "centroid"
  805. \param[out] min z-min value
  806. \param[out] max z-max value
  807. \return -1 on error (no surface)
  808. \return 1 on success
  809. */
  810. int gs_get_zrange0(float *min, float *max)
  811. {
  812. geosurf *gs;
  813. if (Surf_top) {
  814. *min = Surf_top->zmin;
  815. *max = Surf_top->zmax;
  816. }
  817. else {
  818. return (-1);
  819. }
  820. for (gs = Surf_top->next; gs; gs = gs->next) {
  821. if (gs->zmin < *min) {
  822. *min = gs->zmin;
  823. }
  824. if (gs->zmax > *max) {
  825. *max = gs->zmax;
  826. }
  827. }
  828. return (1);
  829. }
  830. /*!
  831. \brief Get z-range
  832. \param[out] min z-min value
  833. \param[out] max z-max value
  834. \return -1 on error (no surface)
  835. \return 1 on success
  836. */
  837. int gs_get_zrange(float *min, float *max)
  838. {
  839. geosurf *gs;
  840. float tmin, tmax, tmid;
  841. if (Surf_top) {
  842. gs_get_zextents(Surf_top, &tmin, &tmax, &tmid);
  843. *min = tmin;
  844. *max = tmax;
  845. }
  846. else {
  847. return (-1);
  848. }
  849. for (gs = Surf_top->next; gs; gs = gs->next) {
  850. gs_get_zextents(gs, &tmin, &tmax, &tmid);
  851. if (tmin < *min) {
  852. *min = tmin;
  853. }
  854. if (tmax > *max) {
  855. *max = tmax;
  856. }
  857. }
  858. return (1);
  859. }
  860. /*!
  861. \brief Get x-range
  862. \param[out] min x-min value
  863. \param[out] max x-max value
  864. \return -1 on error (no surface)
  865. \return 1 on success
  866. */
  867. int gs_get_xrange(float *min, float *max)
  868. {
  869. geosurf *gs;
  870. float tmin, tmax;
  871. if (Surf_top) {
  872. gs_get_xextents(Surf_top, &tmin, &tmax);
  873. *min = tmin;
  874. *max = tmax;
  875. }
  876. else {
  877. return (-1);
  878. }
  879. for (gs = Surf_top->next; gs; gs = gs->next) {
  880. gs_get_xextents(gs, &tmin, &tmax);
  881. if (tmin < *min) {
  882. *min = tmin;
  883. }
  884. if (tmax > *max) {
  885. *max = tmax;
  886. }
  887. }
  888. return (1);
  889. }
  890. /*!
  891. \brief Get y-range
  892. \param[out] min y-min value
  893. \param[out] max y-max value
  894. \return -1 on error (no surface)
  895. \return 1 on success
  896. */
  897. int gs_get_yrange(float *min, float *max)
  898. {
  899. geosurf *gs;
  900. float tmin, tmax;
  901. if (Surf_top) {
  902. gs_get_yextents(Surf_top, &tmin, &tmax);
  903. *min = tmin;
  904. *max = tmax;
  905. }
  906. else {
  907. return (-1);
  908. }
  909. for (gs = Surf_top->next; gs; gs = gs->next) {
  910. gs_get_yextents(gs, &tmin, &tmax);
  911. if (tmin < *min) {
  912. *min = tmin;
  913. }
  914. if (tmax > *max) {
  915. *max = tmax;
  916. }
  917. }
  918. return (1);
  919. }
  920. /*!
  921. \brief Get average z-max value
  922. Useful for setting position of cplane, lighting ball, etc.
  923. \param[out] azmax average z-max value
  924. \return -1 on error
  925. \return 1 on success
  926. */
  927. int gs_get_data_avg_zmax(float *azmax)
  928. {
  929. float zmax;
  930. int i;
  931. geosurf *gs;
  932. zmax = *azmax = 0.0;
  933. if (Surf_top) {
  934. for (i = 0, gs = Surf_top; gs; i++, gs = gs->next) {
  935. zmax += (gs->zmax + gs->z_trans);
  936. }
  937. *azmax = zmax / i;
  938. return (1);
  939. }
  940. return (-1);
  941. }
  942. /*!
  943. \brief Get data center point
  944. \param[out] center (array X,Y,Z)
  945. \return -1 on error
  946. \return 1 on success
  947. */
  948. int gs_get_datacenter(float *cen)
  949. {
  950. float zmin, zmax, ymin, ymax, xmin, xmax;
  951. geosurf *gs;
  952. if (Surf_top) {
  953. zmin = Surf_top->zmin;
  954. zmax = Surf_top->zmax;
  955. ymin = Surf_top->ymin;
  956. ymax = Surf_top->ymax;
  957. xmin = Surf_top->xmin;
  958. xmax = Surf_top->xmax;
  959. for (gs = Surf_top->next; gs; gs = gs->next) {
  960. if (gs->zmin < zmin) {
  961. zmin = gs->zmin;
  962. }
  963. if (gs->zmax > zmax) {
  964. zmax = gs->zmax;
  965. }
  966. if (gs->ymin < ymin) {
  967. ymin = gs->ymin;
  968. }
  969. if (gs->ymax > ymax) {
  970. ymax = gs->ymax;
  971. }
  972. if (gs->xmin < xmin) {
  973. xmin = gs->xmin;
  974. }
  975. if (gs->xmax > xmax) {
  976. xmax = gs->xmax;
  977. }
  978. }
  979. cen[X] = (xmin + xmax) / 2. - xmin;
  980. cen[Y] = (ymin + ymax) / 2. - ymin;
  981. cen[Z] = (zmin + zmax) / 2.;
  982. return (1);
  983. }
  984. cen[X] = cen[Y] = cen[Z] = 0.0;
  985. return (-1);
  986. }
  987. /*!
  988. \brief Set for geosurf need-to-update mark
  989. \return -1 no surface available
  990. \return 1 on success
  991. */
  992. int gs_setall_norm_needupdate(void)
  993. {
  994. geosurf *gs;
  995. if (Surf_top) {
  996. Surf_top->norm_needupdate = 1;
  997. }
  998. else {
  999. return (-1);
  1000. }
  1001. for (gs = Surf_top->next; gs; gs = gs->next) {
  1002. gs->norm_needupdate = 1;
  1003. }
  1004. return (1);
  1005. }
  1006. /*!
  1007. \brief Check if point is masked
  1008. \param gs pointer to geosurf struct
  1009. \param pt point coordinates (X,Y,Z)
  1010. \return 1 masked
  1011. \return 0 not masked
  1012. */
  1013. int gs_point_is_masked(geosurf * gs, float *pt)
  1014. {
  1015. int vrow, vcol, drow, dcol;
  1016. int retmask = 0, npts = 0;
  1017. float p2[2];
  1018. if (!gs->curmask) {
  1019. return (0);
  1020. }
  1021. vrow = Y2VROW(gs, pt[Y]);
  1022. vcol = X2VCOL(gs, pt[X]);
  1023. /* check right & bottom edges */
  1024. if (pt[X] == VCOL2X(gs, VCOLS(gs))) {
  1025. /* right edge */
  1026. vcol -= 1;
  1027. }
  1028. if (pt[Y] == VROW2Y(gs, VROWS(gs))) {
  1029. /* bottom edge */
  1030. vrow -= 1;
  1031. }
  1032. drow = VROW2DROW(gs, vrow);
  1033. dcol = VCOL2DCOL(gs, vcol);
  1034. if (BM_get(gs->curmask, dcol, drow)) {
  1035. retmask |= MASK_TL;
  1036. npts++;
  1037. }
  1038. dcol = VCOL2DCOL(gs, vcol + 1);
  1039. if (BM_get(gs->curmask, dcol, drow)) {
  1040. retmask |= MASK_TR;
  1041. npts++;
  1042. }
  1043. drow = VROW2DROW(gs, vrow + 1);
  1044. if (BM_get(gs->curmask, dcol, drow)) {
  1045. retmask |= MASK_BR;
  1046. npts++;
  1047. }
  1048. dcol = VCOL2DCOL(gs, vcol);
  1049. if (BM_get(gs->curmask, dcol, drow)) {
  1050. retmask |= MASK_BL;
  1051. npts++;
  1052. }
  1053. if (npts != 1) {
  1054. /* zero or masked */
  1055. return (retmask | npts);
  1056. }
  1057. p2[X] = VCOL2X(gs, vcol);
  1058. p2[Y] = VROW2Y(gs, vrow + 1);
  1059. switch (retmask) {
  1060. case MASK_TL:
  1061. if ((pt[X] - p2[X]) / VXRES(gs) > (pt[Y] - p2[Y]) / VYRES(gs)) {
  1062. /* lower triangle */
  1063. return (0);
  1064. }
  1065. return (retmask | npts);
  1066. case MASK_TR:
  1067. return (retmask | npts);
  1068. case MASK_BR:
  1069. if ((pt[X] - p2[X]) / VXRES(gs) <= (pt[Y] - p2[Y]) / VYRES(gs)) {
  1070. /* upper triangle */
  1071. return (0);
  1072. }
  1073. return (retmask | npts);
  1074. case MASK_BL:
  1075. return (retmask | npts);
  1076. }
  1077. /* Assume that if we get here it is an error */
  1078. return (0);
  1079. }
  1080. /*!
  1081. \brief Calculate distance on surface
  1082. \param gs pointer to geosurf struct
  1083. \param p1 from point
  1084. \param p2 to point
  1085. \param[out] dist distnace
  1086. \param use_exag use exag for calculation
  1087. \return 0 on error (points not in region)
  1088. \return 1 on success
  1089. */
  1090. int gs_distance_onsurf(geosurf * gs, float *p1, float *p2, float *dist,
  1091. int use_exag)
  1092. {
  1093. Point3 *tmp;
  1094. int np, i;
  1095. float exag, length;
  1096. if (in_vregion(gs, p1) && in_vregion(gs, p2)) {
  1097. if (NULL == (tmp = gsdrape_get_segments(gs, p1, p2, &np))) {
  1098. return (0);
  1099. }
  1100. length = 0.;
  1101. if (use_exag) {
  1102. exag = GS_global_exag();
  1103. tmp[0][Z] *= exag;
  1104. for (i = 0; i < (np - 1); i++) {
  1105. tmp[i + 1][Z] *= exag;
  1106. length += GS_distance(tmp[i], tmp[i + 1]);
  1107. }
  1108. }
  1109. else {
  1110. for (i = 0; i < (np - 1); i++) {
  1111. length += GS_distance(tmp[i], tmp[i + 1]);
  1112. }
  1113. }
  1114. *dist = length;
  1115. return (1);
  1116. }
  1117. return (0);
  1118. }