snap.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /****************************************************************
  2. *
  3. * MODULE: v.edit
  4. *
  5. * PURPOSE: Editing vector map.
  6. *
  7. * AUTHOR(S): GRASS Development Team
  8. * Wolf Bergenheim, Jachym Cepicky, Martin Landa
  9. *
  10. * COPYRIGHT: (C) 2006-2008 by the GRASS Development Team
  11. *
  12. * This program is free software under the
  13. * GNU General Public License (>=v2).
  14. * Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. * TODO: 3D support
  18. ****************************************************************/
  19. #include "global.h"
  20. /**
  21. \brief Global snapping function based on snapping library function.
  22. \param[in] Map vector map
  23. \param[in] List list of lines to be snapped
  24. \param[in] thresh threshold distance for snapping
  25. \return 1
  26. */
  27. int snap_lines(struct Map_info *Map, struct ilist *List, double thresh)
  28. {
  29. if (G_verbose() > G_verbose_min())
  30. G_important_message(SEP);
  31. Vect_snap_lines_list(Map, List, thresh, NULL);
  32. if (G_verbose() > G_verbose_min())
  33. G_important_message(SEP);
  34. return 1;
  35. }
  36. /**
  37. \brief Snap two selected lines
  38. \param[in] Map vector map
  39. \param[in] line1 reference line
  40. \param[in] line2 line to be snapped (to be modified)
  41. \param[in] thresh threshold distance for snapping (-1 for no limit)
  42. \return id of snapped line
  43. \return 0 lines not snapped
  44. \return -1 on error
  45. */
  46. int snap_line2(struct Map_info *Map, int line1, int line2, double thresh)
  47. {
  48. struct line_pnts *Points1, *Points2;
  49. struct line_cats *Cats2;
  50. int type1, type2;
  51. int newline;
  52. double mindist;
  53. int mindistidx;
  54. Points1 = Vect_new_line_struct();
  55. Points2 = Vect_new_line_struct();
  56. Cats2 = Vect_new_cats_struct();
  57. type1 = Vect_read_line(Map, Points1, NULL, line1);
  58. type2 = Vect_read_line(Map, Points2, Cats2, line2);
  59. /* find mininal distance and its indexes */
  60. mindist = Vedit_get_min_distance(Points1, Points2, 0, /* TODO 3D */
  61. &mindistidx);
  62. if (thresh > 0.0 && mindist > thresh) {
  63. Vect_destroy_line_struct(Points1);
  64. Vect_destroy_line_struct(Points2);
  65. Vect_destroy_cats_struct(Cats2);
  66. return 0;
  67. }
  68. switch (mindistidx) {
  69. case 0:
  70. Points2->x[0] = Points1->x[0];
  71. Points2->y[0] = Points1->y[0];
  72. Points2->z[0] = Points1->z[0];
  73. break;
  74. case 1:
  75. Points2->x[Points2->n_points - 1] = Points1->x[0];
  76. Points2->y[Points2->n_points - 1] = Points1->y[0];
  77. Points2->z[Points2->n_points - 1] = Points1->z[0];
  78. break;
  79. case 2:
  80. Points2->x[0] = Points1->x[Points1->n_points - 1];
  81. Points2->y[0] = Points1->y[Points1->n_points - 1];
  82. Points2->z[0] = Points1->z[Points1->n_points - 1];
  83. break;
  84. case 3:
  85. Points2->x[Points2->n_points - 1] = Points1->x[Points1->n_points - 1];
  86. Points2->y[Points2->n_points - 1] = Points1->y[Points1->n_points - 1];
  87. Points2->z[Points2->n_points - 1] = Points1->z[Points1->n_points - 1];
  88. break;
  89. default:
  90. break;
  91. }
  92. newline = Vect_rewrite_line(Map, line2, type2, Points2, Cats2);
  93. if (newline < 0) {
  94. G_warning(_("Unable to rewrite line %d"), line2);
  95. return -1;
  96. }
  97. /*
  98. G_message(_("Line %d snapped to line %d"),
  99. line2, line1);
  100. */
  101. Vect_destroy_line_struct(Points1);
  102. Vect_destroy_line_struct(Points2);
  103. Vect_destroy_cats_struct(Cats2);
  104. return newline;
  105. }