gvd.c 6.9 KB

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