GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
gvd.c
Go to the documentation of this file.
1 
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include <grass/gis.h>
23 #include <grass/gstypes.h>
24 
25 #include "rowcol.h"
26 
27 #define CHK_FREQ 5
28 /* check for cancel every CHK_FREQ lines */
29 
43 int gs_clip_segment(geosurf * gs, float *bgn, float *end, float *region)
44 {
45  float top, bottom, left, right;
46 
47  if (!region) {
48  top = gs->yrange;
49  bottom = VROW2Y(gs, VROWS(gs));
50  left = 0.0;
51  right = VCOL2X(gs, VCOLS(gs));
52  }
53  else {
54  top = region[0];
55  bottom = region[1];
56  left = region[2];
57  right = region[3];
58  }
59 
60  /* for now, ignore any segments with an end outside */
61  return (bgn[X] >= left && bgn[X] <= right &&
62  end[X] >= left && end[X] <= right &&
63  bgn[Y] >= bottom && bgn[Y] <= top &&
64  end[Y] >= bottom && end[Y] <= top);
65 }
66 
83 int gvd_vect(geovect * gv, geosurf * gs, int do_fast)
84 {
85  int i, j, k;
86  float bgn[3], end[3], tx, ty, tz, konst;
87  float zmin, zmax, fudge;
88  Point3 *points;
89  int npts, src, check;
90  geoline *gln;
91 
92  G_debug(5, "gvd_vect(): id=%d", gv->gvect_id);
93 
94  if (GS_check_cancel()) {
95  return (0);
96  }
97 
99 
100  src = gs_get_att_src(gs, ATT_TOPO);
101  GS_get_scale(&tx, &ty, &tz, 1);
102  gs_get_zrange(&zmin, &zmax);
103  fudge = (zmax - zmin) / 500.;
104 
105  if (src == CONST_ATT) {
106  konst = gs->att[ATT_TOPO].constant;
107  bgn[Z] = end[Z] = konst + gv->z_trans;
108  }
109 
110  gsd_pushmatrix();
111 
112  /* avoid scaling by zero */
113  if (tz == 0.0) {
114  src = CONST_ATT;
115  konst = 0.0;
116  bgn[Z] = end[Z] = konst;
117  gsd_do_scale(0);
118  }
119  else {
120  gsd_do_scale(1);
121  }
122 
123  gsd_translate(gs->x_trans, gs->y_trans, gs->z_trans + fudge);
124 
125  gsd_colormode(CM_COLOR);
126  gsd_color_func(gv->color);
127  gsd_linewidth(gv->width);
128 
129  check = 0;
130  if (do_fast) {
131  if (!gv->fastlines) {
132  gv_decimate_lines(gv);
133  }
134 
135  gln = gv->fastlines;
136  }
137  else {
138  gln = gv->lines;
139  }
140 
141  for (; gln; gln = gln->next) {
142  G_debug(5, "gvd_vect(): type = %d dims = %d", gln->type, gln->dims);
143 
144  if (!(++check % CHK_FREQ)) {
145  if (GS_check_cancel()) {
146  gsd_linewidth(1);
147  gsd_popmatrix();
148 
149  return (0);
150  }
151  }
152 
153  if (gln->type == OGSF_LINE) { /* line */
154  if (gln->dims == 2) { /* 2d line */
155  G_debug(5, "gvd_vect(): 2D vector line");
156  for (k = 0; k < gln->npts - 1; k++) {
157  bgn[X] = gln->p2[k][X] + gv->x_trans - gs->ox;
158  bgn[Y] = gln->p2[k][Y] + gv->y_trans - gs->oy;
159  end[X] = gln->p2[k + 1][X] + gv->x_trans - gs->ox;
160  end[Y] = gln->p2[k + 1][Y] + gv->y_trans - gs->oy;
161 
162  if (src == MAP_ATT) {
163  points = gsdrape_get_segments(gs, bgn, end, &npts);
164  gsd_bgnline();
165 
166  for (i = 0, j = 0; i < npts; i++) {
167  if (gs_point_is_masked(gs, points[i])) {
168  if (j) {
169  gsd_endline();
170  gsd_bgnline();
171  j = 0;
172  }
173  continue;
174  }
175  points[i][Z] += gv->z_trans;
176  gsd_vert_func(points[i]);
177  j++;
178  if (j > 250) {
179  gsd_endline();
180  gsd_bgnline();
181  gsd_vert_func(points[i]);
182  j = 1;
183  }
184  }
185  gsd_endline();
186  }
187  /* need to handle MASK! */
188  else if (src == CONST_ATT) {
189  /* for now - but later, do seg intersect maskedge */
190  if (gs_point_is_masked(gs, bgn) ||
191  gs_point_is_masked(gs, end))
192  continue;
193 
194  if (gs_clip_segment(gs, bgn, end, NULL)) {
195  gsd_bgnline();
196  gsd_vert_func(bgn);
197  gsd_vert_func(end);
198  gsd_endline();
199  }
200  }
201  }
202  }
203  else { /* 3D line */
204 
205  G_debug(5, "gvd_vect(): 3D vector line");
206  points = (Point3 *) malloc(sizeof(Point3));
207 
208  gsd_color_func(gv->color);
209  gsd_bgnline();
210  for (k = 0; k < gln->npts; k++) {
211  points[0][X] =
212  (float)(gln->p3[k][X] + gv->x_trans - gs->ox);
213  points[0][Y] =
214  (float)(gln->p3[k][Y] + gv->y_trans - gs->oy);
215  points[0][Z] = (float)(gln->p3[k][Z] + gv->z_trans);
216 
217  gsd_vert_func(points[0]);
218  }
219  gsd_endline();
220  free(points);
221  }
222  }
223  else if (gln->type == OGSF_POLYGON) { /* polygon */
224  if (gln->dims == 3) { /* 3D polygon */
225  G_debug(5, "gvd_vect(): draw 3D polygon");
226 
227  /* We want at least 3 points */
228  if (gln->npts >= 3) {
229  points = (Point3 *) malloc(2 * sizeof(Point3));
230  glEnable(GL_NORMALIZE);
231 
232  glEnable(GL_COLOR_MATERIAL);
233  glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
234 
235  glEnable(GL_LIGHTING);
236  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
237 
238  glShadeModel(GL_FLAT);
239 
240  glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
241 
242  glBegin(GL_POLYGON);
243  glColor3f(1.0, 0, 0);
244  gsd_color_func(gv->color);
245  glNormal3fv(gln->norm);
246 
247  for (k = 0; k < gln->npts; k++) {
248  points[0][X] =
249  (float)(gln->p3[k][X] + gv->x_trans - gs->ox);
250  points[0][Y] =
251  (float)(gln->p3[k][Y] + gv->y_trans - gs->oy);
252  points[0][Z] = (float)(gln->p3[k][Z] + gv->z_trans);
253  glVertex3fv(points[0]);
254  }
255  glEnd();
256  glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
257  G_free(points);
258  }
259  }
260  else { /* 2D polygons */
261  /* TODO */
262  }
263  }
264  }
265 
266  gsd_linewidth(1);
267  gsd_popmatrix();
268 
269  return (1);
270 }
271 
280 void gvd_draw_lineonsurf(geosurf * gs, float *bgn, float *end, int color)
281 {
282  Point3 *points;
283  int npts, i, j;
284 
285  gsd_color_func(color);
286  points = gsdrape_get_segments(gs, bgn, end, &npts);
287  gsd_bgnline();
288 
289  for (i = 0, j = 0; i < npts; i++) {
290  if (gs_point_is_masked(gs, points[i])) {
291  if (j) {
292  gsd_endline();
293  gsd_bgnline();
294  j = 0;
295  }
296 
297  continue;
298  }
299 
300  gsd_vert_func(points[i]);
301  j++;
302 
303  if (j > 250) {
304  gsd_endline();
305  gsd_bgnline();
306  gsd_vert_func(points[i]);
307  j = 1;
308  }
309  }
310 
311  gsd_endline();
312 
313  return;
314 }