File: Gv3.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (259 lines) | stat: -rw-r--r-- 6,077 bytes parent folder | download | duplicates (2)
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
/*
* $Id: Gv3.c,v 1.8 2005/02/03 14:28:13 markus Exp $
*/

/*  Gv.c 
    Bill Brown, USACERL  
    December 1993
    Uses GRASS routines!
*/
#include <stdlib.h>
#include "gis.h"
#include "Vect.h"

#include "gstypes.h"

#define TRAK_MEM

#ifdef TRAK_MEM
static int Tot_mem = 0;
#endif

/* This loads to memory.  
The other alternative may be to load to a tmp file. */
geoline *Gv_load_vect(char *grassname, int *nlines)
{
    struct Map_info map;
    struct line_pnts *points;
    geoline *top, *gln, *prev;
    int np, i, n, nareas, nl=0, area, type, is3d;
    struct Cell_head  wind;
    float vect[2][3];

    /* TODO: handle error messages */

    Vect_set_open_level (2); 
    Vect_open_old (&map, grassname, "");
    
    if (NULL == (top=gln=(geoline *)malloc(sizeof(geoline)))) {
	fprintf(stderr,"Can't malloc.\n");
	return(NULL);
    }
    prev = top;
    
    #ifdef TRAK_MEM
    	Tot_mem+=sizeof(geoline);
    #endif

    points = Vect_new_line_struct ();

    G_get_set_window (&wind) ;
    Vect_set_constraint_region(&map,wind.north,wind.south,wind.east,wind.west,
	                       PORT_DOUBLE_MAX, -PORT_DOUBLE_MAX);

    is3d = Vect_is_3d ( &map );
    
    /* Read areas */
    n = Vect_get_num_areas (&map);
    nareas = 0;
    G_debug(3, "Reading vector areas (nareas = %d)", n);
    for ( area = 1; area <= n ; area++ ) {
	G_debug(3, " area %d", area);

	Vect_get_area_points ( &map, area, points ); 
	if ( points->n_points < 3 ) continue;
	gln->type = OGSF_POLYGON;
	gln->npts = np = points->n_points;
        G_debug(3, "  np = %d", np);
		
	if ( is3d ) { 
	    gln->dims = 3; 
	    if (NULL == (gln->p3=(Point3 *)calloc(np, sizeof(Point3)))) {
		fprintf(stderr,"Can't calloc.\n"); /* CLEAN UP */
		return(NULL);
	    }
	    #ifdef TRAK_MEM
		Tot_mem+=(np*sizeof(Point3));
	    #endif
	} else {
	    gln->dims = 2;
	    if (NULL == (gln->p2=(Point2 *)calloc(np, sizeof(Point2)))) {
		fprintf(stderr,"Can't calloc.\n"); /* CLEAN UP */
		return(NULL);
	    }
	    #ifdef TRAK_MEM
		Tot_mem+=(np*sizeof(Point2));
	    #endif
	}
    
	for (i=0; i < np; i++) {
	    if ( is3d ) {
		gln->p3[i][X] = points->x[i];
		gln->p3[i][Y] = points->y[i];
		gln->p3[i][Z] = points->z[i];
	    } else {
		gln->p2[i][X] = points->x[i];
		gln->p2[i][Y] = points->y[i];
	    }
	}
	/* Calc normal (should be average) */
	if ( is3d ) {
	    vect[0][X] = (float) ( gln->p3[0][X] - gln->p3[1][X] );
	    vect[0][Y] = (float) ( gln->p3[0][Y] - gln->p3[1][Y] ); 
            vect[0][Z] = (float) ( gln->p3[0][Z] - gln->p3[1][Z] );
	    vect[1][X] = (float) ( gln->p3[2][X] - gln->p3[1][X] );
	    vect[1][Y] = (float) ( gln->p3[2][Y] - gln->p3[1][Y] );
	    vect[1][Z] = (float) ( gln->p3[2][Z] - gln->p3[1][Z] );
	    GS_v3cross( vect[1], vect[0], gln->norm );
	    
	}
    
	if (NULL == (gln->next=(geoline *)malloc (sizeof(geoline)))) {
	    fprintf(stderr,"Can't malloc.\n"); /* CLEAN UP */
	    return(NULL);
	}

	#ifdef TRAK_MEM
	    Tot_mem+=sizeof(geoline);
	#endif
    
	prev = gln;
	gln = gln->next;
	nareas++;
    }
    G_debug(3, "%d areas loaded", nareas);

    /* Read all lines */
    G_debug(3, "Reading vector lines ...");
    while (-1 < (type = Vect_read_next_line(&map, points, NULL))) {
	G_debug(3, "line type = %d", type);

	if ( type & ( GV_LINES | GV_FACE ) ) { 
	    if ( type & ( GV_LINES ) ) {
		gln->type = OGSF_LINE;
	    } else {
		gln->type = OGSF_POLYGON;
                /* Vect_append_point ( points, points->x[0], points->y[0], points->z[0] ); */
	    }

	    gln->npts = np = points->n_points;
            G_debug(3, "  np = %d", np);
		
	    if ( is3d ) { 
		gln->dims = 3; 
		if (NULL == (gln->p3=(Point3 *)calloc(np, sizeof(Point3)))) {
		    fprintf(stderr,"Can't calloc.\n"); /* CLEAN UP */
		    return(NULL);
		}
		#ifdef TRAK_MEM
		    Tot_mem+=(np*sizeof(Point3));
		#endif
	    } else {
		gln->dims = 2;
		if (NULL == (gln->p2=(Point2 *)calloc(np, sizeof(Point2)))) {
		    fprintf(stderr,"Can't calloc.\n"); /* CLEAN UP */
		    return(NULL);
		}
		#ifdef TRAK_MEM
		    Tot_mem+=(np*sizeof(Point2));
		#endif
	    }

	
	    for (i=0; i < np; i++) {
		if ( is3d ) {
		    gln->p3[i][X] = points->x[i];
		    gln->p3[i][Y] = points->y[i];
		    gln->p3[i][Z] = points->z[i];
		} else {
		    gln->p2[i][X] = points->x[i];
		    gln->p2[i][Y] = points->y[i];
		}
	    }
	    /* Calc normal (should be average) */
	    if ( is3d && gln->type == OGSF_POLYGON ) {
		vect[0][X] = (float) ( gln->p3[0][X] - gln->p3[1][X] );
		vect[0][Y] = (float) ( gln->p3[0][Y] - gln->p3[1][Y] ); 
		vect[0][Z] = (float) ( gln->p3[0][Z] - gln->p3[1][Z] );
		vect[1][X] = (float) ( gln->p3[2][X] - gln->p3[1][X] );
		vect[1][Y] = (float) ( gln->p3[2][Y] - gln->p3[1][Y] );
		vect[1][Z] = (float) ( gln->p3[2][Z] - gln->p3[1][Z] );
		GS_v3cross( vect[1], vect[0], gln->norm );
                G_debug ( 3, "norm %f %f %f", gln->norm[0], gln->norm[1], gln->norm[2] );
	    }
	
	    if (NULL == (gln->next=(geoline *)malloc (sizeof(geoline)))) {
		fprintf(stderr,"Can't malloc.\n"); /* CLEAN UP */
		return(NULL);
	    }

	    #ifdef TRAK_MEM
		Tot_mem+=sizeof(geoline);
	    #endif
	
	    prev = gln;
	    gln = gln->next;
	    nl++;
	}
    }
    G_debug(3, "%d lines loaded", nl);

    nl += nareas;
    
    prev->next = NULL;
    free(gln);
    
    #ifdef TRAK_MEM
    	Tot_mem-=sizeof(geoline);
    #endif
    
    Vect_close (&map);

    fprintf(stderr,"Vector file %s loaded.\n",grassname);
    if (!nl) {
	    fprintf(stderr, "Error: No lines from %s fall within current region\n", grassname);
	    return (NULL);
    }
    *nlines = nl;

    #ifdef TRAK_MEM
    	fprintf(stderr,"Total vect memory = %d Kbytes\n", Tot_mem/1000);
    #endif

    return(top);
}

void add_Vectmem(int plus)
{
    #ifdef TRAK_MEM
    {
    	Tot_mem+=plus;
    }
    #endif
    
    return;
}

void sub_Vectmem(int minus)
{
    #ifdef TRAK_MEM
    {
    	Tot_mem-=minus;
    }
    #endif
    
    return;
}

void show_Vectmem(void)
{
    #ifdef TRAK_MEM
    {
    	fprintf(stderr,"Total vect memory = %d Kbytes\n", Tot_mem/1000);
    }
    #endif
    
    return;
}