GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-8cbe8fef7c
vertex.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/vedit/vertex.c
3 
4  \brief Vedit library - vertex manipulation
5 
6  (C) 2006-2008 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Jachym Cepicky <jachym.cepicky gmail.com>
12  \author Martin Landa <landa.martin gmail.com>
13  */
14 
15 #include <grass/vedit.h>
16 
17 /*!
18  \brief Move all vertices in bounding box(es)
19 
20  \param Map pointer to Map_info
21  \param BgMap, nbgmaps list of background vector maps for snapping
22  \param List list of selected lines
23  \param coord points location
24  \param thresh_coords threshold value for selecting lines
25  \param thresh_snap threshold value used for snapping
26  \param move_x,move_y,move_z direction (move_z is used when map is 3D)
27  \param move_first move only first vertex found in the bounding box
28  \param snap snapping mode (see vedit.h)
29 
30  \return number of moved vertices
31  \return -1 on error
32  */
33 int Vedit_move_vertex(struct Map_info *Map, struct Map_info **BgMap,
34  int nbgmaps, struct ilist *List, struct line_pnts *coord,
35  double thresh_coords, double thresh_snap, double move_x,
36  double move_y, double move_z, int move_first, int snap)
37 {
38  int nvertices_moved, nvertices_snapped;
39 
40  int i, j, k;
41  int line, type, rewrite;
42  int npoints;
43  double east, north, dist;
44  double *x, *y, *z;
45  char *moved;
46 
47  struct line_pnts *Points, *Points_snap;
48  struct line_cats *Cats;
49 
50  nvertices_moved = nvertices_snapped = 0;
51  moved = NULL;
52 
53  Points = Vect_new_line_struct();
54  Points_snap = Vect_new_line_struct();
55  Cats = Vect_new_cats_struct();
56 
57  for (i = 0; i < List->n_values; i++) {
58  line = List->value[i];
59 
60  if (!Vect_line_alive(Map, line))
61  continue;
62 
63  type = Vect_read_line(Map, Points, Cats, line);
64 
65  if (!(type & GV_LINES))
66  continue;
67 
68  npoints = Points->n_points;
69  x = Points->x;
70  y = Points->y;
71  z = Points->z;
72 
73  /* vertex moved
74  0 not moved
75  1 moved
76  2 moved and snapped
77  */
78  moved =
79  (char *)G_realloc((void *)moved, Points->n_points * sizeof(char));
80  G_zero((void *)moved, Points->n_points * sizeof(char));
81 
82  rewrite = 0;
83  for (j = 0; j < coord->n_points; j++) {
84  east = coord->x[j];
85  north = coord->y[j];
86 
87  /* move all vertices in the bounding box */
88  for (k = 0; k < Points->n_points; k++) {
89  if (moved[k] == 0) {
90  dist = Vect_points_distance(east, north, 0.0, x[k], y[k],
91  z[k], WITHOUT_Z);
92  if (dist <= thresh_coords) {
93  G_debug(3,
94  "Vedit_move_vertex(): line=%d; x=%f, y=%f -> "
95  "x=%f, y=%f",
96  line, x[k], y[k], x[k] + move_x, y[k] + move_y);
97  x[k] += move_x;
98  y[k] += move_y;
99  if (Vect_is_3d(Map))
100  z[k] += move_z;
101 
102  moved[k] = 1;
103 
104  G_debug(3, "Vedit_move_vertex(): line=%d, point=%d",
105  line, k);
106 
107  if (snap != NO_SNAP) {
108  if (Vedit_snap_point(
109  Map, line, &x[k], &y[k], &z[k], thresh_snap,
110  (snap == SNAPVERTEX) ? 1 : 0) == 0) {
111  /* check also background maps */
112  int bgi;
113 
114  for (bgi = 0; bgi < nbgmaps; bgi++) {
115  if (Vedit_snap_point(
116  BgMap[bgi], -1, &x[k], &y[k], &z[k],
117  thresh_snap,
118  (snap == SNAPVERTEX) ? 1 : 0))
119  moved[k] = 2;
120  break; /* snapped, don't continue */
121  }
122  }
123  else {
124  moved[k] = 2;
125  }
126  }
127 
128  rewrite = 1;
129  nvertices_moved++;
130 
131  if (move_first)
132  break;
133  }
134  }
135  } /* for each line vertex */
136 
137  /* close line or boundary */
138  if ((type & GV_LINES) &&
139  Vect_points_distance(x[0], y[0], z[0], x[npoints - 1],
140  y[npoints - 1], z[npoints - 1],
141  WITHOUT_Z) <= thresh_snap) {
142 
143  if (moved[0] == 1) { /* first node moved */
144  x[0] = x[npoints - 1];
145  y[0] = y[npoints - 1];
146  if (Vect_is_3d(Map))
147  z[0] = z[npoints - 1];
148  }
149  else if (moved[npoints - 1] == 1) { /* last node moved */
150  x[npoints - 1] = x[0];
151  y[npoints - 1] = y[0];
152  if (Vect_is_3d(Map))
153  z[npoints - 1] = z[0];
154  }
155  }
156  } /* for each coord */
157 
158  if (rewrite) {
159  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
160  return -1;
161  }
162  }
163  } /* for each selected line */
164 
165  /* destroy structures */
166  Vect_destroy_line_struct(Points);
167  Vect_destroy_line_struct(Points_snap);
169  /* G_free ((void *) moved); */
170 
171  return nvertices_moved;
172 }
173 
174 /*!
175  \brief Add new vertex to line
176 
177  Shape of line is not changed.
178 
179  \todo 3D
180 
181  \param Map pointer to Map_info
182  \param List list of lines
183  \param coord points location
184  \param thresh find line in given threshold
185 
186  \return number of add vertices
187  \return -1 on error
188  */
189 int Vedit_add_vertex(struct Map_info *Map, struct ilist *List,
190  struct line_pnts *coord, double thresh)
191 {
192  int i, j;
193  int type, line, seg;
194  int nvertices_added, rewrite;
195  double east, north, dist;
196  double *x, *y, *z;
197  double px, py;
198 
199  struct line_pnts *Points;
200  struct line_cats *Cats;
201 
202  nvertices_added = 0;
203  Points = Vect_new_line_struct();
204  Cats = Vect_new_cats_struct();
205 
206  for (i = 0; i < List->n_values; i++) {
207  line = List->value[i];
208 
209  if (!Vect_line_alive(Map, line))
210  continue;
211 
212  type = Vect_read_line(Map, Points, Cats, line);
213 
214  if (!(type & GV_LINES))
215  continue;
216 
217  G_debug(3, "Vedit_add_vertex(): line = %d, thresh = %f", line, thresh);
218 
219  x = Points->x;
220  y = Points->y;
221  z = Points->z;
222  rewrite = FALSE;
223  for (j = 0; j < coord->n_points; j++) {
224  east = coord->x[j];
225  north = coord->y[j];
226 
227  seg = Vect_line_distance(Points, east, north, 0.0, /* standpoint */
228  WITHOUT_Z, &px, &py,
229  NULL, /* point on line */
230  &dist, /* distance to line */
231  NULL, NULL);
232 
233  if (dist <= thresh &&
234  Vect_points_distance(px, py, 0.0, x[seg], y[seg], z[seg],
235  WITHOUT_Z) > 0 &&
236  Vect_points_distance(px, py, 0.0, x[seg - 1], y[seg - 1],
237  z[seg - 1], WITHOUT_Z) > 0) {
238  /* add new vertex */
239  Vect_line_insert_point(Points, seg, px, py, 0.0);
240  G_debug(3, "Vedit_add_vertex(): line=%d; x=%f, y=%f, index=%d",
241  line, px, py, seg);
242  rewrite = TRUE;
243  nvertices_added++;
244  }
245  } /* for each point */
246 
247  /* rewrite the line */
248  if (rewrite) {
249  Vect_line_prune(Points);
250  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
251  return -1;
252  }
253  }
254  } /* for each line */
255 
256  /* destroy structures */
257  Vect_destroy_line_struct(Points);
259 
260  return nvertices_added;
261 }
262 
263 /*!
264  \brief Remove vertex from line
265 
266  \todo 3D
267 
268  \param Map pointer to Map_info
269  \param List list of selected lines
270  \param coord points location
271  \param thresh threshold value to find a line
272 
273  \return number of removed vertices
274  \return -1 on error
275  */
276 int Vedit_remove_vertex(struct Map_info *Map, struct ilist *List,
277  struct line_pnts *coord, double thresh)
278 {
279  int i, j, k;
280  int type, line;
281  int nvertices_removed, rewrite, nlines_modified;
282  double east, north;
283  double dist;
284  double *x, *y, *z;
285 
286  struct line_pnts *Points;
287  struct line_cats *Cats;
288 
289  nvertices_removed = nlines_modified = 0;
290 
291  Points = Vect_new_line_struct();
292  Cats = Vect_new_cats_struct();
293 
294  for (i = 0; i < List->n_values; i++) {
295  line = List->value[i];
296 
297  if (!Vect_line_alive(Map, line))
298  continue;
299 
300  type = Vect_read_line(Map, Points, Cats, line);
301 
302  if (!(type & GV_LINES))
303  continue;
304 
305  x = Points->x;
306  y = Points->y;
307  z = Points->z;
308  rewrite = 0;
309  for (j = 0; j < coord->n_points; j++) {
310  east = coord->x[j];
311  north = coord->y[j];
312 
313  for (k = 0; k < Points->n_points; k++) {
314  dist = Vect_points_distance(east, north, 0.0, x[k], y[k], z[k],
315  WITHOUT_Z);
316  if (dist <= thresh) {
317  /* remove vertex */
318  Vect_line_delete_point(Points, k);
319  G_debug(
320  3,
321  "Vedit_remove_vertex(): line=%d; x=%f, y=%f, index=%d",
322  line, x[k], y[k], k);
323  k--;
324  nvertices_removed++;
325  rewrite = 1;
326  }
327  } /* for each point */
328  } /* for each bounding box */
329 
330  if (rewrite) {
331  /* rewrite the line */
332  if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
333  return -1;
334  }
335 
336  nlines_modified++;
337  }
338  } /* for each line */
339 
340  /* destroy structures */
341  Vect_destroy_line_struct(Points);
343 
344  return nvertices_removed;
345 }
#define NULL
Definition: ccmath.h:32
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
#define G_realloc(p, n)
Definition: defs/gis.h:96
int G_debug(int, const char *,...) __attribute__((format(printf
void Vect_destroy_line_struct(struct line_pnts *)
Frees all memory associated with a line_pnts structure, including the structure itself.
Definition: line.c:77
off_t Vect_rewrite_line(struct Map_info *, off_t, int, const struct line_pnts *, const struct line_cats *)
Rewrites existing feature (topological level required)
void Vect_destroy_cats_struct(struct line_cats *)
Frees all memory associated with line_cats structure, including the struct itself.
int Vect_read_line(struct Map_info *, struct line_pnts *, struct line_cats *, int)
Read vector feature (topological level required)
int Vect_line_distance(const struct line_pnts *, double, double, double, int, double *, double *, double *, double *, double *, double *)
Calculate distance of point to line.
Definition: line.c:648
double Vect_points_distance(double, double, double, double, double, double, int)
Calculate distance of 2 points.
Definition: line.c:866
int Vect_line_alive(struct Map_info *, int)
Check if feature is alive or dead (topological level required)
int Vect_line_insert_point(struct line_pnts *, int, double, double, double)
Insert new point at index position and move all old points at that position and above up.
Definition: line.c:176
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition: line.c:45
int Vect_line_delete_point(struct line_pnts *, int)
Delete point at given index and move all points above down.
Definition: line.c:210
int Vect_line_prune(struct line_pnts *)
Remove duplicate points, i.e. zero length segments.
Definition: line.c:279
struct line_cats * Vect_new_cats_struct(void)
Creates and initializes line_cats structure.
int Vect_is_3d(struct Map_info *)
Check if vector map is 3D.
int Vedit_snap_point(struct Map_info *, int, double *, double *, double *, double, int)
Snap given point to the nearest primitive.
Definition: vedit/snap.c:28
#define GV_LINES
Definition: dig_defines.h:193
#define WITHOUT_Z
2D/3D vector data
Definition: dig_defines.h:171
#define TRUE
Definition: gis.h:79
#define FALSE
Definition: gis.h:83
Vector map info.
Definition: dig_structs.h:1243
List of integers.
Definition: gis.h:706
int n_values
Number of values in the list.
Definition: gis.h:714
int * value
Array of values.
Definition: gis.h:710
Feature category info.
Definition: dig_structs.h:1677
Feature geometry info - coordinates.
Definition: dig_structs.h:1651
double * y
Array of Y coordinates.
Definition: dig_structs.h:1659
double * x
Array of X coordinates.
Definition: dig_structs.h:1655
int n_points
Number of points.
Definition: dig_structs.h:1667
double * z
Array of Z coordinates.
Definition: dig_structs.h:1663
#define NO_SNAP
Definition: vedit.h:7
#define SNAPVERTEX
Definition: vedit.h:9
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
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:189
int Vedit_remove_vertex(struct Map_info *Map, struct ilist *List, struct line_pnts *coord, double thresh)
Remove vertex from line.
Definition: vertex.c:276
#define x