move.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. \file 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. \date 2007-2008
  10. */
  11. #include <grass/vedit.h>
  12. /**
  13. \brief Move selected primitives
  14. \param Map vector map
  15. \param BgMap, nbgmaps list of background vector maps used for snapping
  16. \param List list of primitives to be moved
  17. \param move_x,move_y,move_z direction (move_z used only if map is 3D)
  18. \param snap enable snapping (see globals.h)
  19. \return number of modified primitives
  20. \return -1 on error
  21. */
  22. int Vedit_move_lines(struct Map_info *Map, struct Map_info **BgMap,
  23. int nbgmaps, struct ilist *List, double move_x,
  24. double move_y, double move_z, int snap, double thresh)
  25. {
  26. struct line_pnts *Points;
  27. struct line_cats *Cats;
  28. int i, j;
  29. int type, newline, line;
  30. int nlines_moved;
  31. double *x, *y, *z;
  32. nlines_moved = 0;
  33. Points = Vect_new_line_struct();
  34. Cats = Vect_new_cats_struct();
  35. for (i = 0; i < List->n_values; i++) {
  36. line = List->value[i];
  37. if (!Vect_line_alive(Map, line))
  38. continue;
  39. type = Vect_read_line(Map, Points, Cats, line);
  40. G_debug(3, "Vedit_move_lines(): type=%d, line=%d", type, line);
  41. x = Points->x;
  42. y = Points->y;
  43. z = Points->z;
  44. /* move */
  45. for (j = 0; j < Points->n_points; j++) {
  46. x[j] += move_x;
  47. y[j] += move_y;
  48. if (Vect_is_3d(Map))
  49. z[j] += move_z;
  50. if (snap != NO_SNAP) {
  51. if (Vedit_snap_point(Map, line, &x[j], &y[j], &z[j], thresh,
  52. (snap == SNAPVERTEX) ? 1 : 0) == 0) {
  53. /* check also background maps */
  54. int bgi;
  55. for (bgi = 0; bgi < nbgmaps; bgi++) {
  56. if (Vedit_snap_point
  57. (BgMap[bgi], line, &x[j], &y[j], &z[j], thresh,
  58. (snap == SNAPVERTEX) ? 1 : 0))
  59. break; /* snapped, don't continue */
  60. }
  61. }
  62. }
  63. } /* for each point at line */
  64. newline = Vect_rewrite_line(Map, line, type, Points, Cats);
  65. if (newline < 0) {
  66. return -1;
  67. }
  68. nlines_moved++;
  69. }
  70. Vect_destroy_line_struct(Points);
  71. Vect_destroy_cats_struct(Cats);
  72. return nlines_moved;
  73. }