gvd.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*!
  2. \file gvd.c
  3. \brief OGSF library - loading and manipulating vector sets (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 (December 1993)
  11. \author Doxygenized by Martin Landa (June 2008)
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <grass/gis.h>
  16. #include <grass/gstypes.h>
  17. #include "rowcol.h"
  18. #define CHK_FREQ 5
  19. /* check for cancel every CHK_FREQ lines */
  20. /*!
  21. \brief Clip segment
  22. \todo to use fast clipping and move to gs.c
  23. \param gs surface
  24. \param bgn begin point
  25. \param end end point
  26. \param region region settings
  27. \return 1 segment inside region
  28. \return 0 segment outside region
  29. */
  30. int gs_clip_segment(geosurf * gs, float *bgn, float *end, float *region)
  31. {
  32. float top, bottom, left, right;
  33. if (!region) {
  34. top = gs->yrange;
  35. bottom = VROW2Y(gs, VROWS(gs));
  36. left = 0.0;
  37. right = VCOL2X(gs, VCOLS(gs));
  38. }
  39. else {
  40. top = region[0];
  41. bottom = region[1];
  42. left = region[2];
  43. right = region[3];
  44. }
  45. /* for now, ignore any segments with an end outside */
  46. return (bgn[X] >= left && bgn[X] <= right &&
  47. end[X] >= left && end[X] <= right &&
  48. bgn[Y] >= bottom && bgn[Y] <= top &&
  49. end[Y] >= bottom && end[Y] <= top);
  50. }
  51. /*!
  52. \brief Draw vector set
  53. Need to think about translations - If user translates surface,
  54. vector should automatically go with it, but translating vector should
  55. translate it relative to surface on which it's displayed?
  56. Handling mask checking here, but may be more appropriate to
  57. handle in get_drape_segments?
  58. \param gv vector set
  59. \param gs surface
  60. \param do_fast non-zero for fast mode
  61. \return
  62. */
  63. int gvd_vect(geovect * gv, geosurf * gs, int do_fast)
  64. {
  65. int i, j, k;
  66. float bgn[3], end[3], tx, ty, tz, konst;
  67. float zmin, zmax, fudge;
  68. Point3 *points;
  69. int npts, src, check;
  70. geoline *gln;
  71. G_debug(5, "gvd_vect(): id=%d", gv->gvect_id);
  72. if (GS_check_cancel()) {
  73. return (0);
  74. }
  75. gs_update_curmask(gs);
  76. src = gs_get_att_src(gs, ATT_TOPO);
  77. GS_get_scale(&tx, &ty, &tz, 1);
  78. gs_get_zrange(&zmin, &zmax);
  79. fudge = (zmax - zmin) / 500.;
  80. if (src == CONST_ATT) {
  81. konst = gs->att[ATT_TOPO].constant;
  82. bgn[Z] = end[Z] = konst;
  83. }
  84. gsd_pushmatrix();
  85. /* avoid scaling by zero */
  86. if (tz == 0.0) {
  87. src = CONST_ATT;
  88. konst = 0.0;
  89. bgn[Z] = end[Z] = konst;
  90. gsd_do_scale(0);
  91. }
  92. else {
  93. gsd_do_scale(1);
  94. }
  95. gsd_translate(gs->x_trans, gs->y_trans, gs->z_trans + fudge);
  96. gsd_colormode(CM_COLOR);
  97. gsd_color_func(gv->style->color);
  98. gsd_linewidth(gv->style->width);
  99. check = 0;
  100. if (do_fast) {
  101. if (!gv->fastlines) {
  102. gv_decimate_lines(gv);
  103. }
  104. gln = gv->fastlines;
  105. }
  106. else {
  107. gln = gv->lines;
  108. }
  109. for (; gln; gln = gln->next) {
  110. G_debug(5, "gvd_vect(): type = %d dims = %d", gln->type, gln->dims);
  111. if (!(++check % CHK_FREQ)) {
  112. if (GS_check_cancel()) {
  113. gsd_linewidth(1);
  114. gsd_popmatrix();
  115. return (0);
  116. }
  117. }
  118. if (gln->type == OGSF_LINE) { /* line */
  119. if (gln->dims == 2) { /* 2d line */
  120. G_debug(5, "gvd_vect(): 2D vector line");
  121. for (k = 0; k < gln->npts - 1; k++) {
  122. bgn[X] = gln->p2[k][X] + gv->x_trans - gs->ox;
  123. bgn[Y] = gln->p2[k][Y] + gv->y_trans - gs->oy;
  124. end[X] = gln->p2[k + 1][X] + gv->x_trans - gs->ox;
  125. end[Y] = gln->p2[k + 1][Y] + gv->y_trans - gs->oy;
  126. if (src == MAP_ATT) {
  127. points = gsdrape_get_segments(gs, bgn, end, &npts);
  128. gsd_bgnline();
  129. for (i = 0, j = 0; i < npts; i++) {
  130. if (gs_point_is_masked(gs, points[i])) {
  131. if (j) {
  132. gsd_endline();
  133. gsd_bgnline();
  134. j = 0;
  135. }
  136. continue;
  137. }
  138. points[i][Z] += gv->z_trans;
  139. gsd_vert_func(points[i]);
  140. j++;
  141. if (j > 250) {
  142. gsd_endline();
  143. gsd_bgnline();
  144. gsd_vert_func(points[i]);
  145. j = 1;
  146. }
  147. }
  148. gsd_endline();
  149. }
  150. /* need to handle MASK! */
  151. else if (src == CONST_ATT) {
  152. /* for now - but later, do seg intersect maskedge */
  153. if (gs_point_is_masked(gs, bgn) ||
  154. gs_point_is_masked(gs, end))
  155. continue;
  156. if (gs_clip_segment(gs, bgn, end, NULL)) {
  157. gsd_bgnline();
  158. gsd_vert_func(bgn);
  159. gsd_vert_func(end);
  160. gsd_endline();
  161. }
  162. }
  163. }
  164. }
  165. else { /* 3D line */
  166. G_debug(5, "gvd_vect(): 3D vector line");
  167. points = (Point3 *) malloc(sizeof(Point3));
  168. gsd_color_func(gv->style->color);
  169. gsd_bgnline();
  170. for (k = 0; k < gln->npts; k++) {
  171. points[0][X] =
  172. (float)(gln->p3[k][X] + gv->x_trans - gs->ox);
  173. points[0][Y] =
  174. (float)(gln->p3[k][Y] + gv->y_trans - gs->oy);
  175. points[0][Z] = (float)(gln->p3[k][Z] + gv->z_trans);
  176. gsd_vert_func(points[0]);
  177. }
  178. gsd_endline();
  179. free(points);
  180. }
  181. }
  182. else if (gln->type == OGSF_POLYGON) { /* polygon */
  183. if (gln->dims == 3) { /* 3D polygon */
  184. G_debug(5, "gvd_vect(): draw 3D polygon");
  185. /* We want at least 3 points */
  186. if (gln->npts >= 3) {
  187. points = (Point3 *) malloc(2 * sizeof(Point3));
  188. glEnable(GL_NORMALIZE);
  189. glEnable(GL_COLOR_MATERIAL);
  190. glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  191. glEnable(GL_LIGHTING);
  192. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  193. glShadeModel(GL_FLAT);
  194. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  195. glBegin(GL_POLYGON);
  196. glColor3f(1.0, 0, 0);
  197. gsd_color_func(gv->style->color);
  198. glNormal3fv(gln->norm);
  199. for (k = 0; k < gln->npts; k++) {
  200. points[0][X] =
  201. (float)(gln->p3[k][X] + gv->x_trans - gs->ox);
  202. points[0][Y] =
  203. (float)(gln->p3[k][Y] + gv->y_trans - gs->oy);
  204. points[0][Z] = (float)(gln->p3[k][Z] + gv->z_trans);
  205. glVertex3fv(points[0]);
  206. }
  207. glEnd();
  208. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  209. G_free(points);
  210. }
  211. }
  212. else { /* 2D polygons */
  213. /* TODO */
  214. }
  215. }
  216. }
  217. gsd_linewidth(1);
  218. gsd_popmatrix();
  219. return (1);
  220. }
  221. /*!
  222. \brief Draw line on surface
  223. \param gs surface
  224. \param bgn first line point
  225. \param end end line point
  226. \param color color value
  227. */
  228. void gvd_draw_lineonsurf(geosurf * gs, float *bgn, float *end, int color)
  229. {
  230. Point3 *points;
  231. int npts, i, j;
  232. gsd_color_func(color);
  233. points = gsdrape_get_segments(gs, bgn, end, &npts);
  234. gsd_bgnline();
  235. for (i = 0, j = 0; i < npts; i++) {
  236. if (gs_point_is_masked(gs, points[i])) {
  237. if (j) {
  238. gsd_endline();
  239. gsd_bgnline();
  240. j = 0;
  241. }
  242. continue;
  243. }
  244. gsd_vert_func(points[i]);
  245. j++;
  246. if (j > 250) {
  247. gsd_endline();
  248. gsd_bgnline();
  249. gsd_vert_func(points[i]);
  250. j = 1;
  251. }
  252. }
  253. gsd_endline();
  254. return;
  255. }