File: gvd.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (314 lines) | stat: -rw-r--r-- 6,847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*!
   \file gvd.c

   \brief OGSF library - loading and manipulating vector sets (lower level functions)

   GRASS OpenGL gsurf OGSF Library 

   (C) 1999-2008 by the GRASS Development Team

   This program is free software under the 
   GNU General Public License (>=v2). 
   Read the file COPYING that comes with GRASS
   for details.

   \author Bill Brown USACERL (December 1993)
   \author Doxygenized by Martin Landa (June 2008)
 */

#include <stdio.h>
#include <stdlib.h>

#include <grass/gis.h>
#include <grass/gstypes.h>

#include "rowcol.h"

#define CHK_FREQ 5
/* check for cancel every CHK_FREQ lines */

/*!
   \brief Clip segment

   \todo to use fast clipping and move to gs.c

   \param gs surface
   \param bgn begin point
   \param end end point
   \param region region settings 

   \return 1 segment inside region
   \return 0 segment outside region
 */
int gs_clip_segment(geosurf * gs, float *bgn, float *end, float *region)
{
    float top, bottom, left, right;

    if (!region) {
	top = gs->yrange;
	bottom = VROW2Y(gs, VROWS(gs));
	left = 0.0;
	right = VCOL2X(gs, VCOLS(gs));
    }
    else {
	top = region[0];
	bottom = region[1];
	left = region[2];
	right = region[3];
    }

    /* for now, ignore any segments with an end outside */
    return (bgn[X] >= left && bgn[X] <= right &&
	    end[X] >= left && end[X] <= right &&
	    bgn[Y] >= bottom && bgn[Y] <= top &&
	    end[Y] >= bottom && end[Y] <= top);
}

/*!
   \brief Draw vector set

   Need to think about translations - If user translates surface,
   vector should automatically go with it, but translating vector should
   translate it relative to surface on which it's displayed?

   Handling mask checking here, but may be more appropriate to
   handle in get_drape_segments?

   \param gv vector set
   \param gs surface
   \param do_fast non-zero for fast mode

   \return
 */
int gvd_vect(geovect * gv, geosurf * gs, int do_fast)
{
    int i, j, k;
    float bgn[3], end[3], tx, ty, tz, konst;
    float zmin, zmax, fudge;
    Point3 *points;
    int npts, src, check;
    geoline *gln;

    G_debug(5, "gvd_vect(): id=%d", gv->gvect_id);

    if (GS_check_cancel()) {
	return (0);
    }

    gs_update_curmask(gs);

    src = gs_get_att_src(gs, ATT_TOPO);
    GS_get_scale(&tx, &ty, &tz, 1);
    gs_get_zrange(&zmin, &zmax);
    fudge = (zmax - zmin) / 500.;

    if (src == CONST_ATT) {
	konst = gs->att[ATT_TOPO].constant;
	bgn[Z] = end[Z] = konst + gv->z_trans;
    }

    gsd_pushmatrix();

    /* avoid scaling by zero */
    if (tz == 0.0) {
	src = CONST_ATT;
	konst = 0.0;
	bgn[Z] = end[Z] = konst;
	gsd_do_scale(0);
    }
    else {
	gsd_do_scale(1);
    }

    gsd_translate(gs->x_trans, gs->y_trans, gs->z_trans + fudge);

    gsd_colormode(CM_COLOR);
    gsd_color_func(gv->color);
    gsd_linewidth(gv->width);

    check = 0;
    if (do_fast) {
	if (!gv->fastlines) {
	    gv_decimate_lines(gv);
	}

	gln = gv->fastlines;
    }
    else {
	gln = gv->lines;
    }

    for (; gln; gln = gln->next) {
	G_debug(5, "gvd_vect(): type = %d dims = %d", gln->type, gln->dims);

	if (!(++check % CHK_FREQ)) {
	    if (GS_check_cancel()) {
		gsd_linewidth(1);
		gsd_popmatrix();

		return (0);
	    }
	}

	if (gln->type == OGSF_LINE) {	/* line */
	    if (gln->dims == 2) {	/* 2d line */
		G_debug(5, "gvd_vect(): 2D vector line");
		for (k = 0; k < gln->npts - 1; k++) {
		    bgn[X] = gln->p2[k][X] + gv->x_trans - gs->ox;
		    bgn[Y] = gln->p2[k][Y] + gv->y_trans - gs->oy;
		    end[X] = gln->p2[k + 1][X] + gv->x_trans - gs->ox;
		    end[Y] = gln->p2[k + 1][Y] + gv->y_trans - gs->oy;

		    if (src == MAP_ATT) {
			points = gsdrape_get_segments(gs, bgn, end, &npts);
			gsd_bgnline();

			for (i = 0, j = 0; i < npts; i++) {
			    if (gs_point_is_masked(gs, points[i])) {
				if (j) {
				    gsd_endline();
				    gsd_bgnline();
				    j = 0;
				}
				continue;
			    }
			    points[i][Z] += gv->z_trans;
			    gsd_vert_func(points[i]);
			    j++;
			    if (j > 250) {
				gsd_endline();
				gsd_bgnline();
				gsd_vert_func(points[i]);
				j = 1;
			    }
			}
			gsd_endline();
		    }
		    /* need to handle MASK! */
		    else if (src == CONST_ATT) {
			/* for now - but later, do seg intersect maskedge */
			if (gs_point_is_masked(gs, bgn) ||
			    gs_point_is_masked(gs, end))
			    continue;

			if (gs_clip_segment(gs, bgn, end, NULL)) {
			    gsd_bgnline();
			    gsd_vert_func(bgn);
			    gsd_vert_func(end);
			    gsd_endline();
			}
		    }
		}
	    }
	    else {		/* 3D line */

		G_debug(5, "gvd_vect(): 3D vector line");
		points = (Point3 *) malloc(sizeof(Point3));

		gsd_color_func(gv->color);
		gsd_bgnline();
		for (k = 0; k < gln->npts; k++) {
		    points[0][X] =
			(float)(gln->p3[k][X] + gv->x_trans - gs->ox);
		    points[0][Y] =
			(float)(gln->p3[k][Y] + gv->y_trans - gs->oy);
		    points[0][Z] = (float)(gln->p3[k][Z] + gv->z_trans);

		    gsd_vert_func(points[0]);
		}
		gsd_endline();
		free(points);
	    }
	}
	else if (gln->type == OGSF_POLYGON) {	/* polygon */
	    if (gln->dims == 3) {	/* 3D polygon */
		G_debug(5, "gvd_vect(): draw 3D polygon");

		/* We want at least 3 points */
		if (gln->npts >= 3) {
		    points = (Point3 *) malloc(2 * sizeof(Point3));
		    glEnable(GL_NORMALIZE);

		    glEnable(GL_COLOR_MATERIAL);
		    glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);

		    glEnable(GL_LIGHTING);
		    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

		    glShadeModel(GL_FLAT);

		    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

		    glBegin(GL_POLYGON);
		    glColor3f(1.0, 0, 0);
		    gsd_color_func(gv->color);
		    glNormal3fv(gln->norm);

		    for (k = 0; k < gln->npts; k++) {
			points[0][X] =
			    (float)(gln->p3[k][X] + gv->x_trans - gs->ox);
			points[0][Y] =
			    (float)(gln->p3[k][Y] + gv->y_trans - gs->oy);
			points[0][Z] = (float)(gln->p3[k][Z] + gv->z_trans);
			glVertex3fv(points[0]);
		    }
		    glEnd();
		    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
		    G_free(points);
		}
	    }
	    else {		/* 2D polygons */
		/* TODO */
	    }
	}
    }

    gsd_linewidth(1);
    gsd_popmatrix();

    return (1);
}

/*!
   \brief Draw line on surface

   \param gs surface
   \param bgn first line point
   \param end end line point
   \param color color value
 */
void gvd_draw_lineonsurf(geosurf * gs, float *bgn, float *end, int color)
{
    Point3 *points;
    int npts, i, j;

    gsd_color_func(color);
    points = gsdrape_get_segments(gs, bgn, end, &npts);
    gsd_bgnline();

    for (i = 0, j = 0; i < npts; i++) {
	if (gs_point_is_masked(gs, points[i])) {
	    if (j) {
		gsd_endline();
		gsd_bgnline();
		j = 0;
	    }

	    continue;
	}

	gsd_vert_func(points[i]);
	j++;

	if (j > 250) {
	    gsd_endline();
	    gsd_bgnline();
	    gsd_vert_func(points[i]);
	    j = 1;
	}
    }

    gsd_endline();

    return;
}