vertex.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. \file vertex.cpp
  3. \brief Vertex manipulation
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. (C) 2008 by The GRASS development team
  8. \author Martin Landa <landa.martin gmail.com>
  9. \date 2008
  10. */
  11. extern "C" {
  12. #include <grass/vedit.h>
  13. }
  14. #include "driver.h"
  15. #include "digit.h"
  16. /**
  17. \brief Move vertex
  18. \param x,y,z coordinates (z is used only if map is 3d)
  19. \param move_x,move_y,move_z direction for moving vertex
  20. \param bgmap map of background map or NULL
  21. \param snap snap mode (see vector/v.edit/lib/vedit.h)
  22. \param thresh_coords threshold value to identify vertex position
  23. \param thresh_snap threshold value to snap moved vertex
  24. \param 1 vertex moved
  25. \param 0 nothing changed
  26. \param -1 error
  27. */
  28. int Digit::MoveVertex(double x, double y, double z,
  29. double move_x, double move_y, double move_z,
  30. const char *bgmap, int snap,
  31. double thresh_coords, double thresh_snap) {
  32. int ret;
  33. int changeset, nlines;
  34. struct line_pnts *point;
  35. struct Map_info **BgMap; /* backgroud vector maps */
  36. int nbgmaps; /* number of registrated background maps */
  37. if (!display->mapInfo) {
  38. display->DisplayMsg();
  39. return -1;
  40. }
  41. if (display->selected.values->n_values != 1)
  42. return 0;
  43. BgMap = NULL;
  44. nbgmaps = 0;
  45. if (bgmap && strlen(bgmap) > 0) {
  46. BgMap = OpenBackgroundVectorMap(bgmap);
  47. if (!BgMap) {
  48. display->BackgroundMapMsg(bgmap);
  49. return -1;
  50. }
  51. else {
  52. nbgmaps = 1;
  53. }
  54. }
  55. point = Vect_new_line_struct();
  56. Vect_append_point(point, x, y, z);
  57. nlines = Vect_get_num_lines(display->mapInfo);
  58. changeset = AddActionsBefore();
  59. /* move only first found vertex in bbox */
  60. ret = Vedit_move_vertex(display->mapInfo, BgMap, nbgmaps,
  61. display->selected.values,
  62. point, thresh_coords, thresh_snap,
  63. move_x, move_y, move_z,
  64. 1, snap);
  65. if (ret > 0) {
  66. AddActionsAfter(changeset, nlines);
  67. }
  68. else {
  69. changesets.erase(changeset);
  70. }
  71. if (ret > 0 && settings.breakLines) {
  72. BreakLineAtIntersection(Vect_get_num_lines(display->mapInfo), NULL, changeset);
  73. }
  74. if (BgMap && BgMap[0]) {
  75. Vect_close(BgMap[0]);
  76. }
  77. Vect_destroy_line_struct(point);
  78. return ret;
  79. }
  80. /**
  81. \brief Add or remove vertex
  82. Shape of line/boundary is not changed when adding new vertex.
  83. \param add add or remove vertex?
  84. \param x,y,z coordinates (z is used only if map is 3d
  85. \param thresh threshold value to identify vertex position
  86. \param 1 vertex added/removed
  87. \param 0 nothing changed
  88. \param -1 error
  89. */
  90. int Digit::ModifyLineVertex(int add, double x, double y, double z,
  91. double thresh)
  92. {
  93. int ret;
  94. int changeset, nlines;
  95. struct line_pnts *point;
  96. if (!display->mapInfo) {
  97. display->DisplayMsg();
  98. return -1;
  99. }
  100. if (display->selected.values->n_values != 1)
  101. return 0;
  102. point = Vect_new_line_struct();
  103. Vect_append_point(point, x, y, z);
  104. nlines = Vect_get_num_lines(display->mapInfo);
  105. changeset = AddActionsBefore();
  106. if (add) {
  107. ret = Vedit_add_vertex(display->mapInfo, display->selected.values,
  108. point, thresh);
  109. }
  110. else {
  111. ret = Vedit_remove_vertex(display->mapInfo, display->selected.values,
  112. point, thresh);
  113. }
  114. if (ret > 0) {
  115. AddActionsAfter(changeset, nlines);
  116. }
  117. else {
  118. changesets.erase(changeset);
  119. }
  120. if (!add && ret > 0 && settings.breakLines) {
  121. BreakLineAtIntersection(Vect_get_num_lines(display->mapInfo), NULL, changeset);
  122. }
  123. Vect_destroy_line_struct(point);
  124. return ret;
  125. }