GRASS Programmer's Manual  6.4.2(2012)
vertex.c
Go to the documentation of this file.
00001 
00015 #include <grass/vedit.h>
00016 
00033 int Vedit_move_vertex(struct Map_info *Map, struct Map_info **BgMap,
00034                       int nbgmaps, struct ilist *List,
00035                       struct line_pnts *coord, double thresh_coords,
00036                       double thresh_snap, double move_x, double move_y,
00037                       double move_z, int move_first, int snap)
00038 {
00039     int nvertices_moved, nlines_modified, nvertices_snapped;
00040 
00041     int i, j, k;
00042     int line, type, rewrite;
00043     int npoints;
00044     double east, north, dist;
00045     double *x, *y, *z;
00046     char *moved;
00047 
00048     struct line_pnts *Points, *Points_snap;
00049     struct line_cats *Cats;
00050 
00051     nlines_modified = 0;
00052     nvertices_moved = nvertices_snapped = 0;
00053     moved = NULL;
00054 
00055     Points = Vect_new_line_struct();
00056     Points_snap = Vect_new_line_struct();
00057     Cats = Vect_new_cats_struct();
00058 
00059     for (i = 0; i < List->n_values; i++) {
00060         line = List->value[i];
00061 
00062         if (!Vect_line_alive(Map, line))
00063             continue;
00064 
00065         type = Vect_read_line(Map, Points, Cats, line);
00066 
00067         if (!(type & GV_LINES))
00068             continue;
00069 
00070         npoints = Points->n_points;
00071         x = Points->x;
00072         y = Points->y;
00073         z = Points->z;
00074 
00075         /* vertex moved 
00076            0 not moved
00077            1 moved
00078            2 moved and snapped
00079          */
00080         moved =
00081             (char *)G_realloc((void *)moved, Points->n_points * sizeof(char));
00082         G_zero((void *)moved, Points->n_points * sizeof(char));
00083 
00084         rewrite = 0;
00085         for (j = 0; j < coord->n_points; j++) {
00086             east = coord->x[j];
00087             north = coord->y[j];
00088 
00089             /* move all vertices in the bounding box */
00090             for (k = 0; k < Points->n_points; k++) {
00091                 if (moved[k] == 0) {
00092                     dist = Vect_points_distance(east, north, 0.0,
00093                                                 x[k], y[k], z[k], WITHOUT_Z);
00094                     if (dist <= thresh_coords) {
00095                         G_debug(3,
00096                                 "Vedit_move_vertex(): line=%d; x=%f, y=%f -> x=%f, y=%f",
00097                                 line, x[k], y[k], x[k] + move_x,
00098                                 y[k] + move_y);
00099                         x[k] += move_x;
00100                         y[k] += move_y;
00101                         if (Vect_is_3d(Map))
00102                             z[k] += move_z;
00103 
00104                         moved[k] = 1;
00105 
00106                         G_debug(3, "Vedit_move_vertex(): line=%d, point=%d",
00107                                 line, k);
00108 
00109                         if (snap != NO_SNAP) {
00110                             if (Vedit_snap_point
00111                                 (Map, line, &x[k], &y[k], &z[k], thresh_snap,
00112                                  (snap == SNAPVERTEX) ? 1 : 0) == 0) {
00113                                 /* check also background maps */
00114                                 int bgi;
00115 
00116                                 for (bgi = 0; bgi < nbgmaps; bgi++) {
00117                                     if (Vedit_snap_point
00118                                         (BgMap[bgi], line, &x[k], &y[k],
00119                                          &z[k], thresh_snap,
00120                                          (snap == SNAPVERTEX) ? 1 : 0))
00121                                         moved[k] = 2;
00122                                     break;      /* snapped, don't continue */
00123                                 }
00124                             }
00125                             else {
00126                                 moved[k] = 2;
00127                             }
00128                         }
00129 
00130                         rewrite = 1;
00131                         nvertices_moved++;
00132 
00133                         if (move_first)
00134                             break;
00135                     }
00136                 }
00137             }                   /* for each line vertex */
00138 
00139             /* close line or boundary */
00140             if ((type & GV_LINES) &&
00141                 Vect_points_distance(x[0], y[0], z[0],
00142                                      x[npoints - 1], y[npoints - 1],
00143                                      z[npoints - 1],
00144                                      WITHOUT_Z) <= thresh_snap) {
00145 
00146                 if (moved[0] == 1) {    /* first node moved */
00147                     x[0] = x[npoints - 1];
00148                     y[0] = y[npoints - 1];
00149                     if (Vect_is_3d(Map))
00150                         z[0] = z[npoints - 1];
00151                 }
00152                 else if (moved[npoints - 1] == 1) {     /* last node moved */
00153                     x[npoints - 1] = x[0];
00154                     y[npoints - 1] = y[0];
00155                     if (Vect_is_3d(Map))
00156                         z[npoints - 1] = z[0];
00157                 }
00158             }
00159         }                       /* for each coord */
00160 
00161         if (rewrite) {
00162             if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
00163                 return -1;
00164             }
00165 
00166             nlines_modified++;
00167         }
00168     }                           /* for each selected line */
00169 
00170     /* destroy structures */
00171     Vect_destroy_line_struct(Points);
00172     Vect_destroy_line_struct(Points_snap);
00173     Vect_destroy_cats_struct(Cats);
00174     /*     G_free ((void *) moved); */
00175 
00176     return nvertices_moved;
00177 }
00178 
00194 int Vedit_add_vertex(struct Map_info *Map, struct ilist *List,
00195                      struct line_pnts *coord, double thresh)
00196 {
00197     int i, j;
00198     int type, line, seg;
00199     int nlines_modified, nvertices_added, rewrite;
00200     double east, north, dist;
00201     double *x, *y, *z;
00202     double px, py;
00203 
00204     struct line_pnts *Points;
00205     struct line_cats *Cats;
00206 
00207     nlines_modified = 0;
00208     nvertices_added = 0;
00209     Points = Vect_new_line_struct();
00210     Cats = Vect_new_cats_struct();
00211 
00212     for (i = 0; i < List->n_values; i++) {
00213         line = List->value[i];
00214 
00215         if (!Vect_line_alive(Map, line))
00216             continue;
00217 
00218         type = Vect_read_line(Map, Points, Cats, line);
00219 
00220         if (!(type & GV_LINES))
00221             continue;
00222 
00223         x = Points->x;
00224         y = Points->y;
00225         z = Points->z;
00226         rewrite = 0;
00227         for (j = 0; j < coord->n_points; j++) {
00228             east = coord->x[j];
00229             north = coord->y[j];
00230 
00231             seg = Vect_line_distance(Points, east, north, 0.0,  /* standpoint */
00232                                      WITHOUT_Z, &px, &py, NULL, /* point on line */
00233                                      &dist,     /* distance to line */
00234                                      NULL, NULL);
00235 
00236             if (dist <= thresh &&
00237                 Vect_points_distance(px, py, 0.0, x[seg], y[seg], z[seg],
00238                                      WITHOUT_Z) > 0 &&
00239                 Vect_points_distance(px, py, 0.0, x[seg - 1], y[seg - 1],
00240                                      z[seg - 1], WITHOUT_Z) > 0) {
00241                 /* add new vertex */
00242                 Vect_line_insert_point(Points, seg, px, py, 0.0);
00243                 G_debug(3,
00244                         "Vedit_add_vertex(): line=%d; x=%f, y=%f, index=%d",
00245                         line, px, py, seg);
00246                 rewrite = 1;
00247                 nvertices_added++;
00248             }
00249         }                       /* for each point */
00250 
00251         /* rewrite the line */
00252         if (rewrite) {
00253             Vect_line_prune(Points);
00254             if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
00255                 return -1;
00256             }
00257 
00258             nlines_modified++;
00259         }
00260     }                           /* for each line */
00261 
00262     /* destroy structures */
00263     Vect_destroy_line_struct(Points);
00264     Vect_destroy_cats_struct(Cats);
00265 
00266     return nvertices_added;
00267 }
00268 
00282 int Vedit_remove_vertex(struct Map_info *Map, struct ilist *List,
00283                         struct line_pnts *coord, double thresh)
00284 {
00285     int i, j, k;
00286     int type, line;
00287     int nvertices_removed, rewrite, nlines_modified;
00288     double east, north;
00289     double dist;
00290     double *x, *y, *z;
00291 
00292     struct line_pnts *Points;
00293     struct line_cats *Cats;
00294 
00295     nvertices_removed = nlines_modified = 0;
00296 
00297     Points = Vect_new_line_struct();
00298     Cats = Vect_new_cats_struct();
00299 
00300     for (i = 0; i < List->n_values; i++) {
00301         line = List->value[i];
00302 
00303         if (!Vect_line_alive(Map, line))
00304             continue;
00305 
00306         type = Vect_read_line(Map, Points, Cats, line);
00307 
00308         if (!(type & GV_LINES))
00309             continue;
00310 
00311         x = Points->x;
00312         y = Points->y;
00313         z = Points->z;
00314         rewrite = 0;
00315         for (j = 0; j < coord->n_points; j++) {
00316             east = coord->x[j];
00317             north = coord->y[j];
00318 
00319             for (k = 0; k < Points->n_points; k++) {
00320                 dist = Vect_points_distance(east, north, 0.0,
00321                                             x[k], y[k], z[k], WITHOUT_Z);
00322                 if (dist <= thresh) {
00323                     /* remove vertex */
00324                     Vect_line_delete_point(Points, k);
00325                     G_debug(3,
00326                             "Vedit_remove_vertex(): line=%d; x=%f, y=%f, index=%d",
00327                             line, x[k], y[k], k);
00328                     k--;
00329                     nvertices_removed++;
00330                     rewrite = 1;
00331                 }
00332             }                   /* for each point */
00333         }                       /* for each bounding box */
00334 
00335         if (rewrite) {
00336             /* rewrite the line */
00337             if (Vect_rewrite_line(Map, line, type, Points, Cats) < 0) {
00338                 return -1;
00339             }
00340 
00341             nlines_modified++;
00342         }
00343     }                           /* for each line */
00344 
00345     /* destroy structures */
00346     Vect_destroy_line_struct(Points);
00347     Vect_destroy_cats_struct(Cats);
00348 
00349     return nvertices_removed;
00350 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines