File: clean_nodes.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 (255 lines) | stat: -rw-r--r-- 6,433 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
/*!
   \file clean_nodes.c

   \brief Vector library - Clean boundaries at nodes

   Higher level functions for reading/writing/manipulating vectors.

   (C) 2001-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 Radim Blazek

   \date 2001-2008
 */

#include <stdlib.h>
#include <grass/gis.h>
#include <grass/Vect.h>
#include <grass/glocale.h>

/*!
   \brief Clean small angles at nodes.

   It may happen that even if the angle between 2 boundaries at node is
   very small, the calculated angle is 0 because of
   representation error.  The map must be built at least on level
   GV_BUILD_BASE

   \param Map input map
   \param Err vector map where error line segments are written

   \return number of line modifications
 */
int
Vect_clean_small_angles_at_nodes(struct Map_info *Map, int otype,
				 struct Map_info *Err)
{
    int node, nnodes;
    int nmodif = 0;
    struct line_pnts *Points;
    struct line_cats *SCats, *LCats, *OCats;

    Points = Vect_new_line_struct();
    SCats = Vect_new_cats_struct();
    LCats = Vect_new_cats_struct();
    OCats = Vect_new_cats_struct();

    nnodes = Vect_get_num_nodes(Map);
    for (node = 1; node <= nnodes; node++) {
	int i, nlines;

	G_percent(node, nnodes, 1);
	G_debug(3, "node = %d", node);
	if (!Vect_node_alive(Map, node))
	    continue;

	while (1) {
	    float angle1 = -100;
	    int line1 = -999;	/* value not important, just for debug */
	    int clean = 1;

	    nlines = Vect_get_node_n_lines(Map, node);
	    G_debug(3, "nlines = %d", nlines);

	    for (i = 0; i < nlines; i++) {
		P_LINE *Line;
		int line2;
		float angle2;

		line2 = Vect_get_node_line(Map, node, i);
		Line = Map->plus.Line[abs(line2)];
		if (!Line)
		    continue;
		G_debug(4, "  type = %d", Line->type);
		if (!(Line->type & (otype & GV_LINES)))
		    continue;

		angle2 = Vect_get_node_line_angle(Map, node, i);
		if (angle2 == -9.0)
		    continue;	/* Degenerated line */

		G_debug(4, "  line1 = %d angle1 = %e line2 = %d angle2 = %e",
			line1, angle1, line2, angle2);

		if (angle2 == angle1) {
		    int j;
		    double length1, length2;
		    int short_line;	/* line with shorter end segment */
		    int long_line;	/* line with longer end segment */
		    int new_short_line = 0;	/* line number of short line after rewrite */
		    int short_type, long_type, type;
		    double x, y, z, nx, ny, nz;

		    G_debug(4, "  identical angles -> clean");

		    /* Length of end segments for both lines */
		    Vect_read_line(Map, Points, NULL, abs(line1));
		    if (line1 > 0) {
			length1 =
			    Vect_points_distance(Points->x[0], Points->y[0],
						 0.0, Points->x[1],
						 Points->y[1], 0.0, 0);
		    }
		    else {
			int np;

			np = Points->n_points;
			length1 =
			    Vect_points_distance(Points->x[np - 1],
						 Points->y[np - 1], 0.0,
						 Points->x[np - 2],
						 Points->y[np - 2], 0.0, 0);
		    }

		    Vect_read_line(Map, Points, NULL, abs(line2));
		    if (line2 > 0) {
			length2 =
			    Vect_points_distance(Points->x[0], Points->y[0],
						 0.0, Points->x[1],
						 Points->y[1], 0.0, 0);
		    }
		    else {
			int np;

			np = Points->n_points;
			length2 =
			    Vect_points_distance(Points->x[np - 1],
						 Points->y[np - 1], 0.0,
						 Points->x[np - 2],
						 Points->y[np - 2], 0.0, 0);
		    }

		    G_debug(4, "  length1 = %f length2 = %f", length1,
			    length2);

		    if (length1 < length2) {
			short_line = line1;
			long_line = line2;
		    }
		    else {
			short_line = line2;
			long_line = line1;
		    }

		    /* Remove end segment from short_line */
		    short_type =
			Vect_read_line(Map, Points, SCats, abs(short_line));

		    if (short_line > 0) {
			x = Points->x[1];
			y = Points->y[1];
			z = Points->z[1];
			Vect_line_delete_point(Points, 0);	/* first */
		    }
		    else {
			x = Points->x[Points->n_points - 2];
			y = Points->y[Points->n_points - 2];
			z = Points->z[Points->n_points - 2];
			Vect_line_delete_point(Points, Points->n_points - 1);	/* last */
		    }

		    /* It may happen that it is one line: node could be deleted,
		     * in that case we have to read the node coords first */
		    Vect_get_node_coor(Map, node, &nx, &ny, &nz);

		    if (Points->n_points > 1) {
			new_short_line =
			    Vect_rewrite_line(Map, abs(short_line),
					      short_type, Points, SCats);
		    }
		    else {
			Vect_delete_line(Map, abs(short_line));
		    }

		    /* It may happen that it is one line, in that case we have to take the new
		     * short line as long line, orientation is not changed */
		    if (abs(line1) == abs(line2)) {
			if (long_line > 0)
			    long_line = new_short_line;
			else
			    long_line = -new_short_line;
		    }

		    /* Add new line (must be before rewrite of long_line otherwise node could be deleted) */
		    long_type =
			Vect_read_line(Map, NULL, LCats, abs(long_line));

		    Vect_reset_cats(OCats);
		    for (j = 0; j < SCats->n_cats; j++) {
			Vect_cat_set(OCats, SCats->field[j], SCats->cat[j]);
		    }
		    for (j = 0; j < LCats->n_cats; j++) {
			Vect_cat_set(OCats, LCats->field[j], LCats->cat[j]);
		    }

		    if (long_type == GV_BOUNDARY || short_type == GV_BOUNDARY) {
			type = GV_BOUNDARY;
		    }
		    else {
			type = GV_LINE;
		    }

		    Vect_reset_line(Points);
		    Vect_append_point(Points, nx, ny, nz);
		    Vect_append_point(Points, x, y, z);
		    Vect_write_line(Map, type, Points, OCats);

		    if (Err) {
			Vect_write_line(Err, type, Points, OCats);
		    }

		    /* Snap long_line to the new short_line end */
		    long_type =
			Vect_read_line(Map, Points, LCats, abs(long_line));
		    if (long_line > 0) {
			Points->x[0] = x;
			Points->y[0] = y;
			Points->z[0] = z;
		    }
		    else {
			Points->x[Points->n_points - 1] = x;
			Points->y[Points->n_points - 1] = y;
			Points->z[Points->n_points - 1] = z;
		    }
		    Vect_line_prune(Points);
		    if (Points->n_points > 1) {
			Vect_rewrite_line(Map, abs(long_line), long_type,
					  Points, LCats);
		    }
		    else {
			Vect_delete_line(Map, abs(long_line));
		    }

		    nmodif += 3;
		    clean = 0;

		    break;
		}

		line1 = line2;
		angle1 = angle2;
	    }

	    if (clean || !Vect_node_alive(Map, node))
		break;
	}
	nnodes = Vect_get_num_nodes(Map);
    }

    return (nmodif);
}