gs.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  1. /*!
  2. \file 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/gstypes.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. G_debug(5, "gs_init_surf() id=%d", gs->gsurf_id);
  175. if (!gs) {
  176. return (-1);
  177. }
  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 interger 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) {
  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. G_debug(5, "gs_get_att_src(): id=%d desc=%d", gs->gsurf_id, desc);
  507. if (!LEGAL_ATT(desc)) {
  508. return (-1);
  509. }
  510. if (gs) {
  511. return (gs->att[desc].att_src);
  512. }
  513. return (-1);
  514. }
  515. /*!
  516. \brief Get attribute data buffer
  517. \param gs pointer to geosurf struct
  518. \param desc attribute id (descriptor)
  519. \param to_write non-zero value for 'write'
  520. \return NULL on error
  521. \return pointer to typbuff
  522. */
  523. typbuff *gs_get_att_typbuff(geosurf * gs, int desc, int to_write)
  524. {
  525. typbuff *tb;
  526. geosurf *gsref;
  527. G_debug(5, "gs_get_att_typbuff(): id=%d desc=%d to_write=%d",
  528. gs->gsurf_id, desc, to_write);
  529. if (gs) {
  530. if ((tb = gsds_get_typbuff(gs->att[desc].hdata, to_write))) {
  531. tb->tfunc = NULL;
  532. if (desc == ATT_TOPO) {
  533. gsref = gsdiff_get_SDref();
  534. if (gsref && gsref != gs) {
  535. tb->tfunc = gsdiff_do_SD;
  536. }
  537. }
  538. return (tb);
  539. }
  540. }
  541. return (NULL);
  542. }
  543. /*!
  544. \brief Allocate attribute buffer
  545. \param gs pointer to geosurf struct
  546. \param desc attribute id (descriptor)
  547. \param type buffer type (based on raster map type)
  548. \return -1 on error
  549. \return pointer to typbuff (casted)
  550. */
  551. int gs_malloc_att_buff(geosurf * gs, int desc, int type)
  552. {
  553. int hdata, dims[2], ndims;
  554. G_debug(5, "gs_malloc_att_buff");
  555. if (gs) {
  556. if (0 < (hdata = gs->att[desc].hdata)) {
  557. dims[0] = gs->rows;
  558. dims[1] = gs->cols;
  559. ndims = 2;
  560. gs_set_att_type(gs, desc, type);
  561. return (gsds_alloc_typbuff(hdata, dims, ndims, type));
  562. }
  563. }
  564. return (-1);
  565. }
  566. /*!
  567. \brief Allocate attribute lookup
  568. \param gs pointer to geosurf struct
  569. \param desc attribute id
  570. \return -1 on error
  571. \return pointer to typbuff (casted)
  572. */
  573. int gs_malloc_lookup(geosurf * gs, int desc)
  574. {
  575. int size;
  576. G_debug(5, "gs_malloc_lookup");
  577. if (gs) {
  578. if (gs->att[desc].lookup) {
  579. G_free(gs->att[desc].lookup);
  580. gs->att[desc].lookup = NULL;
  581. }
  582. switch (gs->att[desc].att_type) {
  583. case (ATTY_SHORT):
  584. size = 32768 * sizeof(int);
  585. /* positive integers only, because use as array index */
  586. gs->att[desc].lookup = (int *)G_malloc(size); /* G_fatal_error */
  587. if (!gs->att[desc].lookup) {
  588. return (-1);
  589. }
  590. break;
  591. case (ATTY_CHAR):
  592. size = 256 * sizeof(int);
  593. /* unsigned char */
  594. gs->att[desc].lookup = (int *)G_malloc(size);
  595. if (!gs->att[desc].lookup) {
  596. return (-1);
  597. }
  598. break;
  599. default:
  600. G_warning("bad type: gs_malloc_lookup");
  601. return (-1);
  602. }
  603. if (gs->att[desc].lookup) {
  604. return (0);
  605. }
  606. }
  607. return (-1);
  608. }
  609. /*!
  610. \brief Set attribute type
  611. \param gs pointer to geosurf struct
  612. \param desc attribute id
  613. \param type attribute type
  614. \return -1 on error
  615. \return 0 on success
  616. */
  617. int gs_set_att_type(geosurf * gs, int desc, int type)
  618. {
  619. G_debug(5, "gs_set_att_type(): desc=%d, type=%d", desc, type);
  620. if (gs && LEGAL_TYPE(type)) {
  621. gs->att[desc].att_type = type;
  622. return (0);
  623. }
  624. return (-1);
  625. }
  626. /*!
  627. \brief Set attribute source
  628. \param gs pointer to geosurf struct
  629. \param desc attribute id (descriptor)
  630. \param src source id
  631. \return -1 on error
  632. \return 0 on success
  633. */
  634. int gs_set_att_src(geosurf * gs, int desc, int src)
  635. {
  636. G_debug(5, "gs_set_att_src(): id=%d desc=%d src=%d",
  637. gs->gsurf_id, desc, src);
  638. /* check if old source was MAP_ATT, free buff */
  639. if (MAP_ATT == gs_get_att_src(gs, desc)) {
  640. if (1 == gs_num_datah_reused(gs->att[desc].hdata)) {
  641. /* only reference */
  642. G_debug(5, "gs_set_att_src(): replacing existing map");
  643. gsds_free_datah(gs->att[desc].hdata);
  644. }
  645. if (ATT_TOPO == desc) {
  646. if (gs->norms) {
  647. G_free(gs->norms);
  648. }
  649. gs->norms = NULL;
  650. gs->norm_needupdate = 0;
  651. }
  652. }
  653. if (gs && LEGAL_SRC(src)) {
  654. gs->att[desc].att_src = src;
  655. return (0);
  656. }
  657. return (-1);
  658. }
  659. /*!
  660. \brief Set attribute constant value
  661. \todo set typbuf constant
  662. \param gs pointer to geosurf struct
  663. \param desc attribute id
  664. \param constant constant value
  665. \return 0 on success
  666. \return -1 on error
  667. */
  668. int gs_set_att_const(geosurf * gs, int desc, float constant)
  669. {
  670. G_debug(5, "gs_set_att_const(): id=%d, desc=%d, const=%f",
  671. gs->gsurf_id, desc, constant);
  672. if (gs) {
  673. gs->att[desc].constant = constant;
  674. if (ATT_MASK == desc) {
  675. gs->mask_needupdate = 1;
  676. }
  677. else {
  678. gs_set_att_src(gs, desc, CONST_ATT);
  679. }
  680. Gs_update_attrange(gs, desc);
  681. return (0);
  682. }
  683. return (-1);
  684. }
  685. /*!
  686. \brief Set geosurf mask mode
  687. \param invert invert mask
  688. */
  689. void gs_set_maskmode(int invert)
  690. {
  691. Invertmask = invert;
  692. return;
  693. }
  694. /*!
  695. \brief Check if mask is defined
  696. \param gs pointer to geosurf struct
  697. \return 1 if defined
  698. \return 0 not defined
  699. */
  700. int gs_mask_defined(geosurf * gs)
  701. {
  702. return (gs->att[ATT_MASK].att_src != NOTSET_ATT);
  703. }
  704. /*!
  705. \brief
  706. Should only be called when setting up the current mask (gs_bm.c)
  707. \param tb pointer to typbuff
  708. \param col number of cols
  709. \param row number of rows
  710. \param offset offset value
  711. \return 1
  712. \return 0
  713. */
  714. int gs_masked(typbuff * tb, int col, int row, int offset)
  715. {
  716. int ret;
  717. ret = 1;
  718. if (tb->bm) {
  719. ret = BM_get(tb->bm, col, row);
  720. }
  721. else if (tb->cb) {
  722. ret = tb->cb[offset];
  723. }
  724. else if (tb->sb) {
  725. ret = tb->sb[offset];
  726. }
  727. else if (tb->ib) {
  728. ret = tb->ib[offset];
  729. }
  730. else if (tb->fb) {
  731. ret = tb->fb[offset];
  732. }
  733. return (Invertmask ? ret : !ret);
  734. }
  735. /*!
  736. \brief
  737. Call this one when you already know att_src is MAP_ATT
  738. \param cobuff
  739. \param coloratt color attribute
  740. \param offset offset value
  741. \return packed color for catagory at offset
  742. */
  743. int gs_mapcolor(typbuff * cobuff, gsurf_att * coloratt, int offset)
  744. {
  745. if (coloratt->lookup) {
  746. /* for now, but may add larger color lookup capabilities later,
  747. so would have to use GET_MAPATT */
  748. return (coloratt->lookup[cobuff->cb[offset]]);
  749. }
  750. return (cobuff->ib[offset]);
  751. }
  752. /*
  753. In the following functions, "extents" refers to translated extents for
  754. a single surface, while "range" refers to accumulated extents of all
  755. loaded surfaces
  756. */
  757. /*!
  758. \brief Get z-extent values
  759. \todo pass flag to use zminmasked instead of zmin
  760. \param gs pointer to geosurf struct
  761. \param[out] min z-min value
  762. \param[out] max z-max value
  763. \param[out] mid z-middle value
  764. \return 1
  765. */
  766. int gs_get_zextents(geosurf * gs, float *min, float *max, float *mid)
  767. {
  768. *min = gs->zmin + gs->z_trans;
  769. *max = gs->zmax + gs->z_trans;
  770. *mid = (*max + *min) / 2.;
  771. return (1);
  772. }
  773. /*!
  774. \brief Get x-extent values
  775. \param gs pointer to geosurf struct
  776. \param[out] min x-min value
  777. \param[out] max x-max value
  778. \return 1
  779. */
  780. int gs_get_xextents(geosurf * gs, float *min, float *max)
  781. {
  782. *min = gs->xmin + gs->x_trans;
  783. *max = gs->xmax + gs->x_trans;
  784. return (1);
  785. }
  786. /*!
  787. \brief Get y-extent values
  788. \param gs pointer to geosurf struct
  789. \param[out] min y-min value
  790. \param[out] max y-max value
  791. \return 1
  792. */
  793. int gs_get_yextents(geosurf * gs, float *min, float *max)
  794. {
  795. *min = gs->ymin + gs->y_trans;
  796. *max = gs->ymax + gs->y_trans;
  797. return (1);
  798. }
  799. /*!
  800. \brief Get z-range
  801. \todo pass flag to use zminmasked instead of zmin
  802. could also have this return a weighted average for vertical "centroid"
  803. \param[out] min z-min value
  804. \param[out] max z-max value
  805. \return -1 on error (no surface)
  806. \return 1 on success
  807. */
  808. int gs_get_zrange0(float *min, float *max)
  809. {
  810. geosurf *gs;
  811. if (Surf_top) {
  812. *min = Surf_top->zmin;
  813. *max = Surf_top->zmax;
  814. }
  815. else {
  816. return (-1);
  817. }
  818. for (gs = Surf_top->next; gs; gs = gs->next) {
  819. if (gs->zmin < *min) {
  820. *min = gs->zmin;
  821. }
  822. if (gs->zmax > *max) {
  823. *max = gs->zmax;
  824. }
  825. }
  826. return (1);
  827. }
  828. /*!
  829. \brief Get z-range
  830. \param[out] min z-min value
  831. \param[out] max z-max value
  832. \return -1 on error (no surface)
  833. \return 1 on success
  834. */
  835. int gs_get_zrange(float *min, float *max)
  836. {
  837. geosurf *gs;
  838. float tmin, tmax, tmid;
  839. if (Surf_top) {
  840. gs_get_zextents(Surf_top, &tmin, &tmax, &tmid);
  841. *min = tmin;
  842. *max = tmax;
  843. }
  844. else {
  845. return (-1);
  846. }
  847. for (gs = Surf_top->next; gs; gs = gs->next) {
  848. gs_get_zextents(gs, &tmin, &tmax, &tmid);
  849. if (tmin < *min) {
  850. *min = tmin;
  851. }
  852. if (tmax > *max) {
  853. *max = tmax;
  854. }
  855. }
  856. return (1);
  857. }
  858. /*!
  859. \brief Get x-range
  860. \param[out] min x-min value
  861. \param[out] max x-max value
  862. \return -1 on error (no surface)
  863. \return 1 on success
  864. */
  865. int gs_get_xrange(float *min, float *max)
  866. {
  867. geosurf *gs;
  868. float tmin, tmax;
  869. if (Surf_top) {
  870. gs_get_xextents(Surf_top, &tmin, &tmax);
  871. *min = tmin;
  872. *max = tmax;
  873. }
  874. else {
  875. return (-1);
  876. }
  877. for (gs = Surf_top->next; gs; gs = gs->next) {
  878. gs_get_xextents(gs, &tmin, &tmax);
  879. if (tmin < *min) {
  880. *min = tmin;
  881. }
  882. if (tmax > *max) {
  883. *max = tmax;
  884. }
  885. }
  886. return (1);
  887. }
  888. /*!
  889. \brief Get y-range
  890. \param[out] min y-min value
  891. \param[out] max y-max value
  892. \return -1 on error (no surface)
  893. \return 1 on success
  894. */
  895. int gs_get_yrange(float *min, float *max)
  896. {
  897. geosurf *gs;
  898. float tmin, tmax;
  899. if (Surf_top) {
  900. gs_get_yextents(Surf_top, &tmin, &tmax);
  901. *min = tmin;
  902. *max = tmax;
  903. }
  904. else {
  905. return (-1);
  906. }
  907. for (gs = Surf_top->next; gs; gs = gs->next) {
  908. gs_get_yextents(gs, &tmin, &tmax);
  909. if (tmin < *min) {
  910. *min = tmin;
  911. }
  912. if (tmax > *max) {
  913. *max = tmax;
  914. }
  915. }
  916. return (1);
  917. }
  918. /*!
  919. \brief Get average z-max value
  920. Useful for setting position of cplane, lighting ball, etc.
  921. \param[out] azmax average z-max value
  922. \return -1 on error
  923. \return 1 on success
  924. */
  925. int gs_get_data_avg_zmax(float *azmax)
  926. {
  927. float zmax;
  928. int i;
  929. geosurf *gs;
  930. zmax = *azmax = 0.0;
  931. if (Surf_top) {
  932. for (i = 0, gs = Surf_top; gs; i++, gs = gs->next) {
  933. zmax += (gs->zmax + gs->z_trans);
  934. }
  935. *azmax = zmax / i;
  936. return (1);
  937. }
  938. return (-1);
  939. }
  940. /*!
  941. \brief Get data center point
  942. \param[out] center (array X,Y,Z)
  943. \return -1 on error
  944. \return 1 on success
  945. */
  946. int gs_get_datacenter(float *cen)
  947. {
  948. float zmin, zmax, ymin, ymax, xmin, xmax;
  949. geosurf *gs;
  950. if (Surf_top) {
  951. zmin = Surf_top->zmin;
  952. zmax = Surf_top->zmax;
  953. ymin = Surf_top->ymin;
  954. ymax = Surf_top->ymax;
  955. xmin = Surf_top->xmin;
  956. xmax = Surf_top->xmax;
  957. for (gs = Surf_top->next; gs; gs = gs->next) {
  958. if (gs->zmin < zmin) {
  959. zmin = gs->zmin;
  960. }
  961. if (gs->zmax > zmax) {
  962. zmax = gs->zmax;
  963. }
  964. if (gs->ymin < ymin) {
  965. ymin = gs->ymin;
  966. }
  967. if (gs->ymax > ymax) {
  968. ymax = gs->ymax;
  969. }
  970. if (gs->xmin < xmin) {
  971. xmin = gs->xmin;
  972. }
  973. if (gs->xmax > xmax) {
  974. xmax = gs->xmax;
  975. }
  976. }
  977. cen[X] = (xmin + xmax) / 2. - xmin;
  978. cen[Y] = (ymin + ymax) / 2. - ymin;
  979. cen[Z] = (zmin + zmax) / 2.;
  980. return (1);
  981. }
  982. cen[X] = cen[Y] = cen[Z] = 0.0;
  983. return (-1);
  984. }
  985. /*!
  986. \brief Set for geosurf need-to-update mark
  987. \return -1 no surface available
  988. \return 1 on success
  989. */
  990. int gs_setall_norm_needupdate(void)
  991. {
  992. geosurf *gs;
  993. if (Surf_top) {
  994. Surf_top->norm_needupdate = 1;
  995. }
  996. else {
  997. return (-1);
  998. }
  999. for (gs = Surf_top->next; gs; gs = gs->next) {
  1000. gs->norm_needupdate = 1;
  1001. }
  1002. return (1);
  1003. }
  1004. /*!
  1005. \brief Check if point is masked
  1006. \param gs pointer to geosurf struct
  1007. \param pt point coordinates (X,Y,Z)
  1008. \return 1 masked
  1009. \return 0 not masked
  1010. */
  1011. int gs_point_is_masked(geosurf * gs, float *pt)
  1012. {
  1013. int vrow, vcol, drow, dcol;
  1014. int retmask = 0, npts = 0;
  1015. float p2[2];
  1016. if (!gs->curmask) {
  1017. return (0);
  1018. }
  1019. vrow = Y2VROW(gs, pt[Y]);
  1020. vcol = X2VCOL(gs, pt[X]);
  1021. /* check right & bottom edges */
  1022. if (pt[X] == VCOL2X(gs, VCOLS(gs))) {
  1023. /* right edge */
  1024. vcol -= 1;
  1025. }
  1026. if (pt[Y] == VROW2Y(gs, VROWS(gs))) {
  1027. /* bottom edge */
  1028. vrow -= 1;
  1029. }
  1030. drow = VROW2DROW(gs, vrow);
  1031. dcol = VCOL2DCOL(gs, vcol);
  1032. if (BM_get(gs->curmask, dcol, drow)) {
  1033. retmask |= MASK_TL;
  1034. npts++;
  1035. }
  1036. dcol = VCOL2DCOL(gs, vcol + 1);
  1037. if (BM_get(gs->curmask, dcol, drow)) {
  1038. retmask |= MASK_TR;
  1039. npts++;
  1040. }
  1041. drow = VROW2DROW(gs, vrow + 1);
  1042. if (BM_get(gs->curmask, dcol, drow)) {
  1043. retmask |= MASK_BR;
  1044. npts++;
  1045. }
  1046. dcol = VCOL2DCOL(gs, vcol);
  1047. if (BM_get(gs->curmask, dcol, drow)) {
  1048. retmask |= MASK_BL;
  1049. npts++;
  1050. }
  1051. if (npts != 1) {
  1052. /* zero or masked */
  1053. return (retmask | npts);
  1054. }
  1055. p2[X] = VCOL2X(gs, vcol);
  1056. p2[Y] = VROW2Y(gs, vrow + 1);
  1057. switch (retmask) {
  1058. case MASK_TL:
  1059. if ((pt[X] - p2[X]) / VXRES(gs) > (pt[Y] - p2[Y]) / VYRES(gs)) {
  1060. /* lower triangle */
  1061. return (0);
  1062. }
  1063. return (retmask | npts);
  1064. case MASK_TR:
  1065. return (retmask | npts);
  1066. case MASK_BR:
  1067. if ((pt[X] - p2[X]) / VXRES(gs) <= (pt[Y] - p2[Y]) / VYRES(gs)) {
  1068. /* upper triangle */
  1069. return (0);
  1070. }
  1071. return (retmask | npts);
  1072. case MASK_BL:
  1073. return (retmask | npts);
  1074. }
  1075. /* Assume that if we get here it is an error */
  1076. return (0);
  1077. }
  1078. /*!
  1079. \brief Calculate distance on surface
  1080. \param gs pointer to geosurf struct
  1081. \param p1 from point
  1082. \param p2 to point
  1083. \param[out] dist distnace
  1084. \param use_exag use exag for calculation
  1085. \return 0 on error (points not in region)
  1086. \return 1 on success
  1087. */
  1088. int gs_distance_onsurf(geosurf * gs, float *p1, float *p2, float *dist,
  1089. int use_exag)
  1090. {
  1091. Point3 *tmp;
  1092. int np, i;
  1093. float exag, length;
  1094. if (in_vregion(gs, p1) && in_vregion(gs, p2)) {
  1095. if (NULL == (tmp = gsdrape_get_segments(gs, p1, p2, &np))) {
  1096. return (0);
  1097. }
  1098. length = 0.;
  1099. if (use_exag) {
  1100. exag = GS_global_exag();
  1101. tmp[0][Z] *= exag;
  1102. for (i = 0; i < (np - 1); i++) {
  1103. tmp[i + 1][Z] *= exag;
  1104. length += GS_distance(tmp[i], tmp[i + 1]);
  1105. }
  1106. }
  1107. else {
  1108. for (i = 0; i < (np - 1); i++) {
  1109. length += GS_distance(tmp[i], tmp[i + 1]);
  1110. }
  1111. }
  1112. *dist = length;
  1113. return (1);
  1114. }
  1115. return (0);
  1116. }