extract.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <math.h>
  5. #include <grass/gis.h>
  6. #include <grass/display.h>
  7. #include <grass/colors.h>
  8. #include <grass/vector.h>
  9. #include <grass/glocale.h>
  10. #define WDTH 5
  11. #define M_START 1
  12. #define M_ADD 2
  13. #define M_DEL 3
  14. #define M_END 4
  15. int display(struct Map_info *Map, struct boxlist *List,
  16. const struct color_rgb *color);
  17. int extract(struct Map_info *In, struct Map_info *Out, int type,
  18. const struct color_rgb *color, const struct color_rgb *hcolor)
  19. {
  20. int i, button, mode, line;
  21. int screen_x, screen_y, cur_screen_x, cur_screen_y;
  22. double x1, y1, x2, y2;
  23. struct boxlist *List, *CList;
  24. struct bound_box box;
  25. struct line_pnts *Points;
  26. struct line_cats *Cats;
  27. Points = Vect_new_line_struct();
  28. Cats = Vect_new_cats_struct();
  29. List = Vect_new_boxlist(0);
  30. CList = Vect_new_boxlist(0);
  31. /* box.T = PORT_DOUBLE_MAX;
  32. box.B = -PORT_DOUBLE_MAX; */
  33. Vect_get_map_box(In, &box);
  34. mode = M_START;
  35. G_message(_("Select vector(s) with mouse"));
  36. G_message(_(" - L: draw box with left mouse button to select"));
  37. G_message(_(" - M: draw box with middle mouse button to remove from display"));
  38. G_message(_(" - R: quit and save selected vectors to new map\n"));
  39. while (1) {
  40. G_message(_("L: add M: remove R: quit and save\n"));
  41. if (mode == M_START) {
  42. R_get_location_with_pointer(&screen_x, &screen_y, &button);
  43. cur_screen_x = screen_x;
  44. cur_screen_y = screen_y;
  45. }
  46. else {
  47. R_get_location_with_box(cur_screen_x, cur_screen_y, &screen_x,
  48. &screen_y, &button);
  49. x1 = D_d_to_u_col((double)(cur_screen_x));
  50. y1 = D_d_to_u_row((double)(cur_screen_y));
  51. x2 = D_d_to_u_col((double)(screen_x));
  52. y2 = D_d_to_u_row((double)(screen_y));
  53. if (x1 < x2) {
  54. box.W = x1;
  55. box.E = x2;
  56. }
  57. else {
  58. box.W = x2;
  59. box.E = x1;
  60. }
  61. if (y1 < y2) {
  62. box.S = y1;
  63. box.N = y2;
  64. }
  65. else {
  66. box.S = y2;
  67. box.N = y1;
  68. }
  69. G_debug(1, "Box: N S E W = %f %f %f %f\n", box.N, box.S, box.E,
  70. box.W);
  71. }
  72. /* TODO: check if line really intersects box, not only box intersects box */
  73. switch (button) {
  74. case 1:
  75. if (mode == M_START) {
  76. mode = M_ADD;
  77. }
  78. else if (mode == M_ADD) {
  79. Vect_select_lines_by_box(In, &box, type, CList);
  80. Vect_boxlist_append_boxlist(List, CList);
  81. display(In, List, hcolor);
  82. mode = M_START;
  83. }
  84. break;
  85. case 2:
  86. if (mode == M_START) {
  87. mode = M_DEL;
  88. }
  89. else if (mode == M_DEL) {
  90. Vect_select_lines_by_box(In, &box, type, CList);
  91. Vect_boxlist_delete_boxlist(List, CList);
  92. display(In, CList, color);
  93. mode = M_START;
  94. }
  95. break;
  96. case 3:
  97. for (i = 0; i < List->n_values; i++) {
  98. line = List->value[i];
  99. type = Vect_read_line(In, Points, Cats, line);
  100. Vect_write_line(Out, type, Points, Cats);
  101. }
  102. display(In, List, color);
  103. return 1;
  104. break;
  105. }
  106. };
  107. Vect_destroy_boxlist(List);
  108. Vect_destroy_boxlist(CList);
  109. return 1;
  110. }
  111. int
  112. display(struct Map_info *Map, struct boxlist *List,
  113. const struct color_rgb *color)
  114. {
  115. int i, j, line, type;
  116. struct line_pnts *Points;
  117. double msize;
  118. msize = 10 * (D_d_to_u_col(2) - D_d_to_u_col(1)); /* do it better */
  119. G_debug(1, "msize = %f\n", msize);
  120. Points = Vect_new_line_struct();
  121. D_RGB_color(color->r, color->g, color->b);
  122. for (i = 0; i < List->n_values; i++) {
  123. line = abs(List->id[i]);
  124. type = Vect_read_line(Map, Points, NULL, line);
  125. if (type & GV_POINTS)
  126. D_plot_icon(Points->x[0], Points->y[0], G_ICON_CROSS, 0.0, msize);
  127. else
  128. for (j = 0; j < Points->n_points - 1; j++) {
  129. D_move(Points->x[j], Points->y[j]);
  130. D_cont(Points->x[j + 1], Points->y[j + 1]);
  131. }
  132. }
  133. R_flush();
  134. Vect_destroy_line_struct(Points);
  135. return 0;
  136. }