vertex.c

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

Generated on Sat Oct 24 03:25:20 2009 for GRASS Programmer's Manual by  doxygen 1.6.1