GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
vertex.c
Go to the documentation of this file.
1 
15 #include <grass/vedit.h>
16 
33 int Vedit_move_vertex(struct Map_info *Map, struct Map_info **BgMap,
34  int nbgmaps, struct ilist *List,
35  struct line_pnts *coord, double thresh_coords,
36  double thresh_snap, double move_x, double move_y,
37  double move_z, int move_first, int snap)
38 {
39  int nvertices_moved, nlines_modified, nvertices_snapped;
40 
41  int i, j, k;
42  int line, type, rewrite;
43  int npoints;
44  double east, north, dist;
45  double *x, *y, *z;
46  char *moved;
47 
48  struct line_pnts *Points, *Points_snap;
49  struct line_cats *Cats;
50 
51  nlines_modified = 0;
52  nvertices_moved = nvertices_snapped = 0;
53  moved = NULL;
54 
55  Points = Vect_new_line_struct();
56  Points_snap = Vect_new_line_struct();
57  Cats = Vect_new_cats_struct();
58 
59  for (i = 0; i < List->n_values; i++) {
60  line = List->value[i];
61 
62  if (!Vect_line_alive(Map, line))
63  continue;
64 
65  type = Vect_read_line(Map, Points, Cats, line);
66 
67  if (!(type & GV_LINES))
68  continue;
69 
70  npoints = Points->n_points;
71  x = Points->x;
72  y = Points->y;
73  z = Points->z;
74 
75  /* vertex moved
76  0 not moved
77  1 moved
78  2 moved and snapped
79  */
80  moved =
81  (char *)G_realloc((void *)moved, Points->n_points * sizeof(char));
82  G_zero((void *)moved, Points->n_points * sizeof(char));
83 
84  rewrite = 0;
85  for (j = 0; j < coord->n_points; j++) {
86  east = coord->x[j];
87  north = coord->y[j];
88 
89  /* move all vertices in the bounding box */
90  for (k = 0; k < Points->n_points; k++) {
91  if (moved[k] == 0) {
92  dist = Vect_points_distance(east, north, 0.0,
93  x[k], y[k], z[k], WITHOUT_Z);
94  if (dist <= thresh_coords) {
95  G_debug(3,
96  "Vedit_move_vertex(): line=%d; x=%f, y=%f -> x=%f, y=%f",
97  line, x[k], y[k], x[k] + move_x,
98  y[k] + move_y);
99  x[k] += move_x;
100  y[k] += move_y;
101  if (Vect_is_3d(Map))
102  z[k] += move_z;
103 
104  moved[k] = 1;
105 
106  G_debug(3, "Vedit_move_vertex(): line=%d, point=%d",
107  line, k);
108 
109  if (snap != NO_SNAP) {
110  if (Vedit_snap_point
111  (Map, line, &x[k], &y[k], &z[k], thresh_snap,
112  (snap == SNAPVERTEX) ? 1 : 0) == 0) {
113  /* check also background maps */
114  int bgi;
115 
116  for (bgi = 0; bgi < nbgmaps; bgi++) {
117  if (Vedit_snap_point
118  (BgMap[bgi], line, &x[k], &y[k],
119  &z[k], thresh_snap,
120  (snap == SNAPVERTEX) ? 1 : 0))
121  moved[k] = 2;
122  break; /* snapped, don't continue */
123  }
124  }
125  else {
126  moved[k] = 2;
127  }
128  }
129 
130  rewrite = 1;
131  nvertices_moved++;
132 
133  if (move_first)
134  break;
135  }
136  }
137  } /* for each line vertex */
138 
139  /* close line or boundary */
140  if ((type & GV_LINES) &&
141  Vect_points_distance(x[0], y[0], z[0],
142  x[npoints - 1], y[npoints - 1],
143  z[npoints - 1],
144  WITHOUT_Z) <= thresh_snap) {
145 
146  if (moved[0] == 1) { /* first node moved */
147  x[0] = x[npoints - 1];
148  y[0] = y[npoints - 1];
149  if (Vect_is_3d(Map))
150  z[0] = z[npoints - 1];
151  }
152  else if (moved[npoints - 1] == 1) { /* last node moved */
153  x[npoints - 1] = x[0];
154  y[npoints - 1] = y[0];
155  if (Vect_is_3d(Map))
156  z[npoints - 1] = z[0];
157  }
158  }
159  } /* for each coord */
160 
161  if (rewrite) {
162  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
163  return -1;
164  }
165 
166  nlines_modified++;
167  }
168  } /* for each selected line */
169 
170  /* destroy structures */
171  Vect_destroy_line_struct(Points);
172  Vect_destroy_line_struct(Points_snap);
174  /* G_free ((void *) moved); */
175 
176  return nvertices_moved;
177 }
178 
194 int Vedit_add_vertex(struct Map_info *Map, struct ilist *List,
195  struct line_pnts *coord, double thresh)
196 {
197  int i, j;
198  int type, line, seg;
199  int nlines_modified, nvertices_added, rewrite;
200  double east, north, dist;
201  double *x, *y, *z;
202  double px, py;
203 
204  struct line_pnts *Points;
205  struct line_cats *Cats;
206 
207  nlines_modified = 0;
208  nvertices_added = 0;
209  Points = Vect_new_line_struct();
210  Cats = Vect_new_cats_struct();
211 
212  for (i = 0; i < List->n_values; i++) {
213  line = List->value[i];
214 
215  if (!Vect_line_alive(Map, line))
216  continue;
217 
218  type = Vect_read_line(Map, Points, Cats, line);
219 
220  if (!(type & GV_LINES))
221  continue;
222 
223  x = Points->x;
224  y = Points->y;
225  z = Points->z;
226  rewrite = 0;
227  for (j = 0; j < coord->n_points; j++) {
228  east = coord->x[j];
229  north = coord->y[j];
230 
231  seg = Vect_line_distance(Points, east, north, 0.0, /* standpoint */
232  WITHOUT_Z, &px, &py, NULL, /* point on line */
233  &dist, /* distance to line */
234  NULL, NULL);
235 
236  if (dist <= thresh &&
237  Vect_points_distance(px, py, 0.0, x[seg], y[seg], z[seg],
238  WITHOUT_Z) > 0 &&
239  Vect_points_distance(px, py, 0.0, x[seg - 1], y[seg - 1],
240  z[seg - 1], WITHOUT_Z) > 0) {
241  /* add new vertex */
242  Vect_line_insert_point(Points, seg, px, py, 0.0);
243  G_debug(3,
244  "Vedit_add_vertex(): line=%d; x=%f, y=%f, index=%d",
245  line, px, py, seg);
246  rewrite = 1;
247  nvertices_added++;
248  }
249  } /* for each point */
250 
251  /* rewrite the line */
252  if (rewrite) {
253  Vect_line_prune(Points);
254  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
255  return -1;
256  }
257 
258  nlines_modified++;
259  }
260  } /* for each line */
261 
262  /* destroy structures */
263  Vect_destroy_line_struct(Points);
265 
266  return nvertices_added;
267 }
268 
282 int Vedit_remove_vertex(struct Map_info *Map, struct ilist *List,
283  struct line_pnts *coord, double thresh)
284 {
285  int i, j, k;
286  int type, line;
287  int nvertices_removed, rewrite, nlines_modified;
288  double east, north;
289  double dist;
290  double *x, *y, *z;
291 
292  struct line_pnts *Points;
293  struct line_cats *Cats;
294 
295  nvertices_removed = nlines_modified = 0;
296 
297  Points = Vect_new_line_struct();
298  Cats = Vect_new_cats_struct();
299 
300  for (i = 0; i < List->n_values; i++) {
301  line = List->value[i];
302 
303  if (!Vect_line_alive(Map, line))
304  continue;
305 
306  type = Vect_read_line(Map, Points, Cats, line);
307 
308  if (!(type & GV_LINES))
309  continue;
310 
311  x = Points->x;
312  y = Points->y;
313  z = Points->z;
314  rewrite = 0;
315  for (j = 0; j < coord->n_points; j++) {
316  east = coord->x[j];
317  north = coord->y[j];
318 
319  for (k = 0; k < Points->n_points; k++) {
320  dist = Vect_points_distance(east, north, 0.0,
321  x[k], y[k], z[k], WITHOUT_Z);
322  if (dist <= thresh) {
323  /* remove vertex */
324  Vect_line_delete_point(Points, k);
325  G_debug(3,
326  "Vedit_remove_vertex(): line=%d; x=%f, y=%f, index=%d",
327  line, x[k], y[k], k);
328  k--;
329  nvertices_removed++;
330  rewrite = 1;
331  }
332  } /* for each point */
333  } /* for each bounding box */
334 
335  if (rewrite) {
336  /* rewrite the line */
337  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
338  return -1;
339  }
340 
341  nlines_modified++;
342  }
343  } /* for each line */
344 
345  /* destroy structures */
346  Vect_destroy_line_struct(Points);
348 
349  return nvertices_removed;
350 }
struct line_pnts * Vect_new_line_struct()
Creates and initializes a struct line_pnts.
Definition: line.c:57
int y
Definition: plot.c:34
int Vedit_remove_vertex(struct Map_info *Map, struct ilist *List, struct line_pnts *coord, double thresh)
Remove vertex from line.
Definition: vertex.c:282
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:29
int Vect_is_3d(struct Map_info *Map)
Check if vector map is 3D (with z)
int Vedit_snap_point(struct Map_info *Map, int line, double *x, double *y, double *z, double thresh, int vertex)
Snap given point to the nearest primitive.
Definition: vedit/snap.c:28
int Vect_line_insert_point(struct line_pnts *Points, int index, double x, double y, double z)
Insert new point at index position and move all old points at that position and above up...
Definition: line.c:194
int Vect_rewrite_line(struct Map_info *Map, int line, int type, struct line_pnts *points, struct line_cats *cats)
Rewrites feature info at the given offset.
int GV_LINES
Definition: vdigit/main.py:24
int Vect_destroy_cats_struct(struct line_cats *p)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_line_alive(struct Map_info *Map, int line)
Check if feature is alive or dead.
struct line_cats * Vect_new_cats_struct()
Creates and initializes line_cats structure.
return NULL
Definition: dbfopen.c:1394
int Vect_line_prune(struct line_pnts *Points)
Remove duplicate points, i.e. zero length segments.
Definition: line.c:255
int Vedit_add_vertex(struct Map_info *Map, struct ilist *List, struct line_pnts *coord, double thresh)
Add new vertex to line.
Definition: vertex.c:194
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
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)
Move all vertices in bounding box(es)
Definition: vertex.c:33
double Vect_points_distance(double x1, double y1, double z1, double x2, double y2, double z2, int with_z)
Calculate distance of 2 points.
Definition: line.c:745
int Vect_line_delete_point(struct line_pnts *Points, int index)
Delete point at given index and move all points above down.
Definition: line.c:227
int Vect_line_distance(struct line_pnts *points, double ux, double uy, double uz, int with_z, double *px, double *py, double *pz, double *dist, double *spdist, double *lpdist)
calculate line distance.
Definition: line.c:631
int Vect_destroy_line_struct(struct line_pnts *p)
Frees all memory associated with a struct line_pnts, including the struct itself. ...
Definition: line.c:90
int Vect_read_line(struct Map_info *Map, struct line_pnts *line_p, struct line_cats *line_c, int line)
Read vector feature.