move.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*!
  2. \file lib/vector/vedit/move.c
  3. \brief Vedit library - move primitives
  4. (C) 2007-2008 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Martin Landa <landa.martin gmail.com>
  8. \author Jachym Cepicky <jachym.cepicky gmail.com>
  9. */
  10. #include <grass/vedit.h>
  11. /*!
  12. \brief Move selected primitives
  13. \param Map pointer to Map_info
  14. \param BgMap, nbgmaps list of background vector maps used for snapping
  15. \param List list of primitives to be moved
  16. \param move_x,move_y,move_z direction (move_z used only if map is 3D)
  17. \param snap enable snapping (see globals.h)
  18. \return number of modified primitives
  19. \return -1 on error
  20. */
  21. int Vedit_move_lines(struct Map_info *Map, struct Map_info **BgMap,
  22. int nbgmaps, struct ilist *List, double move_x,
  23. double move_y, double move_z, int snap, double thresh)
  24. {
  25. struct line_pnts *Points;
  26. struct line_cats *Cats;
  27. int i, j;
  28. int type, newline, line;
  29. int nlines_moved;
  30. double *x, *y, *z;
  31. nlines_moved = 0;
  32. Points = Vect_new_line_struct();
  33. Cats = Vect_new_cats_struct();
  34. for (i = 0; i < List->n_values; i++) {
  35. line = List->value[i];
  36. if (!Vect_line_alive(Map, line))
  37. continue;
  38. type = Vect_read_line(Map, Points, Cats, line);
  39. G_debug(3, "Vedit_move_lines(): type=%d, line=%d", type, line);
  40. x = Points->x;
  41. y = Points->y;
  42. z = Points->z;
  43. /* move */
  44. for (j = 0; j < Points->n_points; j++) {
  45. x[j] += move_x;
  46. y[j] += move_y;
  47. if (Vect_is_3d(Map))
  48. z[j] += move_z;
  49. if (snap != NO_SNAP) {
  50. if (Vedit_snap_point(Map, line, &x[j], &y[j], &z[j], thresh,
  51. (snap == SNAPVERTEX) ? 1 : 0) == 0) {
  52. /* check also background maps */
  53. int bgi;
  54. for (bgi = 0; bgi < nbgmaps; bgi++) {
  55. if (Vedit_snap_point
  56. (BgMap[bgi], -1, &x[j], &y[j], &z[j], thresh,
  57. (snap == SNAPVERTEX) ? 1 : 0))
  58. break; /* snapped, don't continue */
  59. }
  60. }
  61. }
  62. } /* for each point at line */
  63. newline = Vect_rewrite_line(Map, line, type, Points, Cats);
  64. if (newline < 0) {
  65. return -1;
  66. }
  67. nlines_moved++;
  68. }
  69. Vect_destroy_line_struct(Points);
  70. Vect_destroy_cats_struct(Cats);
  71. return nlines_moved;
  72. }