File: vertex.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 (350 lines) | stat: -rw-r--r-- 8,429 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*!
  \file lib/vector/vedit/vertex.c
  
  \brief Vedit library - vertex manipulation
  
  (C) 2006-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 Jachym Cepicky <jachym.cepicky gmail.com>
  \author Martin Landa <landa.martin gmail.com>
*/

#include <grass/vedit.h>

/*!
  \brief Move all vertices in bounding box(es)
  
  \param Map pointer to Map_info
  \param BgMap, nbgmaps list of background vector maps for snapping
  \param List list of selected lines
  \param coord points location
  \param thresh_coords threshold value for selecting lines
  \param thresh_snap threshold value used for snapping
  \param move_x,move_y,move_z direction (move_z is used when map is 3D)
  \param move_first move only first vertex found in the bounding box
  \param snap snapping mode (see vedit.h)
  
  \return number of moved verteces
  \return -1 on error
*/
int Vedit_move_vertex(struct Map_info *Map, struct Map_info **BgMap,
		      int nbgmaps, struct ilist *List,
		      struct line_pnts *coord, double thresh_coords,
		      double thresh_snap, double move_x, double move_y,
		      double move_z, int move_first, int snap)
{
    int nvertices_moved, nlines_modified, nvertices_snapped;

    int i, j, k;
    int line, type, rewrite;
    int npoints;
    double east, north, dist;
    double *x, *y, *z;
    char *moved;

    struct line_pnts *Points, *Points_snap;
    struct line_cats *Cats;

    nlines_modified = 0;
    nvertices_moved = nvertices_snapped = 0;
    moved = NULL;

    Points = Vect_new_line_struct();
    Points_snap = Vect_new_line_struct();
    Cats = Vect_new_cats_struct();

    for (i = 0; i < List->n_values; i++) {
	line = List->value[i];

	if (!Vect_line_alive(Map, line))
	    continue;

	type = Vect_read_line(Map, Points, Cats, line);

	if (!(type & GV_LINES))
	    continue;

	npoints = Points->n_points;
	x = Points->x;
	y = Points->y;
	z = Points->z;

	/* vertex moved 
	   0 not moved
	   1 moved
	   2 moved and snapped
	 */
	moved =
	    (char *)G_realloc((void *)moved, Points->n_points * sizeof(char));
	G_zero((void *)moved, Points->n_points * sizeof(char));

	rewrite = 0;
	for (j = 0; j < coord->n_points; j++) {
	    east = coord->x[j];
	    north = coord->y[j];

	    /* move all vertices in the bounding box */
	    for (k = 0; k < Points->n_points; k++) {
		if (moved[k] == 0) {
		    dist = Vect_points_distance(east, north, 0.0,
						x[k], y[k], z[k], WITHOUT_Z);
		    if (dist <= thresh_coords) {
			G_debug(3,
				"Vedit_move_vertex(): line=%d; x=%f, y=%f -> x=%f, y=%f",
				line, x[k], y[k], x[k] + move_x,
				y[k] + move_y);
			x[k] += move_x;
			y[k] += move_y;
			if (Vect_is_3d(Map))
			    z[k] += move_z;

			moved[k] = 1;

			G_debug(3, "Vedit_move_vertex(): line=%d, point=%d",
				line, k);

			if (snap != NO_SNAP) {
			    if (Vedit_snap_point
				(Map, line, &x[k], &y[k], &z[k], thresh_snap,
				 (snap == SNAPVERTEX) ? 1 : 0) == 0) {
				/* check also background maps */
				int bgi;

				for (bgi = 0; bgi < nbgmaps; bgi++) {
				    if (Vedit_snap_point
					(BgMap[bgi], line, &x[k], &y[k],
					 &z[k], thresh_snap,
					 (snap == SNAPVERTEX) ? 1 : 0))
					moved[k] = 2;
				    break;	/* snapped, don't continue */
				}
			    }
			    else {
				moved[k] = 2;
			    }
			}

			rewrite = 1;
			nvertices_moved++;

			if (move_first)
			    break;
		    }
		}
	    }			/* for each line vertex */

	    /* close line or boundary */
	    if ((type & GV_LINES) &&
		Vect_points_distance(x[0], y[0], z[0],
				     x[npoints - 1], y[npoints - 1],
				     z[npoints - 1],
				     WITHOUT_Z) <= thresh_snap) {

		if (moved[0] == 1) {	/* first node moved */
		    x[0] = x[npoints - 1];
		    y[0] = y[npoints - 1];
		    if (Vect_is_3d(Map))
			z[0] = z[npoints - 1];
		}
		else if (moved[npoints - 1] == 1) {	/* last node moved */
		    x[npoints - 1] = x[0];
		    y[npoints - 1] = y[0];
		    if (Vect_is_3d(Map))
			z[npoints - 1] = z[0];
		}
	    }
	}			/* for each coord */

	if (rewrite) {
	    if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
		return -1;
	    }

	    nlines_modified++;
	}
    }				/* for each selected line */

    /* destroy structures */
    Vect_destroy_line_struct(Points);
    Vect_destroy_line_struct(Points_snap);
    Vect_destroy_cats_struct(Cats);
    /*     G_free ((void *) moved); */

    return nvertices_moved;
}

/*!
  \brief Add new vertex to line
  
  Shape of line is not changed.
  
  \todo 3D
  
  \param Map pointer to Map_info
  \param List list of lines
  \param coord points location
  \param thresh find line in given threshold
  
  \return number of add verteces
  \return -1 on error
*/
int Vedit_add_vertex(struct Map_info *Map, struct ilist *List,
		     struct line_pnts *coord, double thresh)
{
    int i, j;
    int type, line, seg;
    int nlines_modified, nvertices_added, rewrite;
    double east, north, dist;
    double *x, *y, *z;
    double px, py;

    struct line_pnts *Points;
    struct line_cats *Cats;

    nlines_modified = 0;
    nvertices_added = 0;
    Points = Vect_new_line_struct();
    Cats = Vect_new_cats_struct();

    for (i = 0; i < List->n_values; i++) {
	line = List->value[i];

	if (!Vect_line_alive(Map, line))
	    continue;

	type = Vect_read_line(Map, Points, Cats, line);

	if (!(type & GV_LINES))
	    continue;

	x = Points->x;
	y = Points->y;
	z = Points->z;
	rewrite = 0;
	for (j = 0; j < coord->n_points; j++) {
	    east = coord->x[j];
	    north = coord->y[j];

	    seg = Vect_line_distance(Points, east, north, 0.0,	/* standpoint */
				     WITHOUT_Z, &px, &py, NULL,	/* point on line */
				     &dist,	/* distance to line */
				     NULL, NULL);

	    if (dist <= thresh &&
		Vect_points_distance(px, py, 0.0, x[seg], y[seg], z[seg],
				     WITHOUT_Z) > 0 &&
		Vect_points_distance(px, py, 0.0, x[seg - 1], y[seg - 1],
				     z[seg - 1], WITHOUT_Z) > 0) {
		/* add new vertex */
		Vect_line_insert_point(Points, seg, px, py, 0.0);
		G_debug(3,
			"Vedit_add_vertex(): line=%d; x=%f, y=%f, index=%d",
			line, px, py, seg);
		rewrite = 1;
		nvertices_added++;
	    }
	}			/* for each point */

	/* rewrite the line */
	if (rewrite) {
	    Vect_line_prune(Points);
	    if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
		return -1;
	    }

	    nlines_modified++;
	}
    }				/* for each line */

    /* destroy structures */
    Vect_destroy_line_struct(Points);
    Vect_destroy_cats_struct(Cats);

    return nvertices_added;
}

/*!
  \brief Remove vertex from line
  
  \todo 3D
  
  \param Map pointer to Map_info
  \param List list of selected lines
  \param coord points location
  \param thresh threshold value to find a line
  
  \return number of removed vertices
  \return -1 on error
*/
int Vedit_remove_vertex(struct Map_info *Map, struct ilist *List,
			struct line_pnts *coord, double thresh)
{
    int i, j, k;
    int type, line;
    int nvertices_removed, rewrite, nlines_modified;
    double east, north;
    double dist;
    double *x, *y, *z;

    struct line_pnts *Points;
    struct line_cats *Cats;

    nvertices_removed = nlines_modified = 0;

    Points = Vect_new_line_struct();
    Cats = Vect_new_cats_struct();

    for (i = 0; i < List->n_values; i++) {
	line = List->value[i];

	if (!Vect_line_alive(Map, line))
	    continue;

	type = Vect_read_line(Map, Points, Cats, line);

	if (!(type & GV_LINES))
	    continue;

	x = Points->x;
	y = Points->y;
	z = Points->z;
	rewrite = 0;
	for (j = 0; j < coord->n_points; j++) {
	    east = coord->x[j];
	    north = coord->y[j];

	    for (k = 0; k < Points->n_points; k++) {
		dist = Vect_points_distance(east, north, 0.0,
					    x[k], y[k], z[k], WITHOUT_Z);
		if (dist <= thresh) {
		    /* remove vertex */
		    Vect_line_delete_point(Points, k);
		    G_debug(3,
			    "Vedit_remove_vertex(): line=%d; x=%f, y=%f, index=%d",
			    line, x[k], y[k], k);
		    k--;
		    nvertices_removed++;
		    rewrite = 1;
		}
	    }			/* for each point */
	}			/* for each bounding box */

	if (rewrite) {
	    /* rewrite the line */
	    if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
		return -1;
	    }

	    nlines_modified++;
	}
    }				/* for each line */

    /* destroy structures */
    Vect_destroy_line_struct(Points);
    Vect_destroy_cats_struct(Cats);

    return nvertices_removed;
}