init_search.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <grass/raster.h>
  2. #include <grass/glocale.h>
  3. #include "local_proto.h"
  4. int init_search(int depr_fd)
  5. {
  6. int r, c, r_nbr, c_nbr, ct_dir;
  7. CELL *depr_buf, ele_value;
  8. int nextdr[8] = { 1, -1, 0, 0, -1, 1, 1, -1 };
  9. int nextdc[8] = { 0, 0, -1, 1, 1, -1, 1, -1 };
  10. char asp_value, is_null;
  11. WAT_ALT wa;
  12. ASP_FLAG af, af_nbr;
  13. GW_LARGE_INT n_depr_cells = 0;
  14. nxt_avail_pt = heap_size = 0;
  15. /* load edge cells and real depressions to A* heap */
  16. if (depr_fd >= 0)
  17. depr_buf = Rast_allocate_buf(CELL_TYPE);
  18. else
  19. depr_buf = NULL;
  20. G_message(_("Initializing A* search..."));
  21. for (r = 0; r < nrows; r++) {
  22. G_percent(r, nrows, 2);
  23. if (depr_fd >= 0) {
  24. Rast_get_row(depr_fd, depr_buf, r, CELL_TYPE);
  25. }
  26. for (c = 0; c < ncols; c++) {
  27. seg_get(&aspflag, (char *)&af, r, c);
  28. is_null = FLAG_GET(af.flag, NULLFLAG);
  29. if (is_null)
  30. continue;
  31. asp_value = 0;
  32. if (r == 0 || r == nrows - 1 || c == 0 || c == ncols - 1) {
  33. if (r == 0 && c == 0)
  34. asp_value = -7;
  35. else if (r == 0 && c == ncols - 1)
  36. asp_value = -5;
  37. else if (r == nrows - 1 && c == 0)
  38. asp_value = -1;
  39. else if (r == nrows - 1 && c == ncols - 1)
  40. asp_value = -3;
  41. else if (r == 0)
  42. asp_value = -2;
  43. else if (c == 0)
  44. asp_value = -4;
  45. else if (r == nrows - 1)
  46. asp_value = -6;
  47. else if (c == ncols - 1)
  48. asp_value = -8;
  49. seg_get(&watalt, (char *)&wa, r, c);
  50. ele_value = wa.ele;
  51. heap_add(r, c, ele_value);
  52. FLAG_SET(af.flag, INLISTFLAG);
  53. FLAG_SET(af.flag, EDGEFLAG);
  54. af.asp = asp_value;
  55. seg_put(&aspflag, (char *)&af, r, c);
  56. continue;
  57. }
  58. /* any neighbour NULL ? */
  59. for (ct_dir = 0; ct_dir < sides; ct_dir++) {
  60. /* get r, c (r_nbr, c_nbr) for neighbours */
  61. r_nbr = r + nextdr[ct_dir];
  62. c_nbr = c + nextdc[ct_dir];
  63. seg_get(&aspflag, (char *)&af_nbr, r_nbr, c_nbr);
  64. is_null = FLAG_GET(af_nbr.flag, NULLFLAG);
  65. if (is_null) {
  66. asp_value = -1 * drain[r - r_nbr + 1][c - c_nbr + 1];
  67. seg_get(&watalt, (char *)&wa, r, c);
  68. ele_value = wa.ele;
  69. heap_add(r, c, ele_value);
  70. FLAG_SET(af.flag, INLISTFLAG);
  71. FLAG_SET(af.flag, EDGEFLAG);
  72. af.asp = asp_value;
  73. seg_put(&aspflag, (char *)&af, r, c);
  74. break;
  75. }
  76. }
  77. if (asp_value) /* some neighbour was NULL, point added to list */
  78. continue;
  79. /* real depression ? */
  80. if (depr_fd >= 0) {
  81. if (!Rast_is_c_null_value(&depr_buf[c]) && depr_buf[c] != 0) {
  82. seg_get(&watalt, (char *)&wa, r, c);
  83. ele_value = wa.ele;
  84. heap_add(r, c, ele_value);
  85. FLAG_SET(af.flag, INLISTFLAG);
  86. FLAG_SET(af.flag, DEPRFLAG);
  87. af.asp = asp_value;
  88. seg_put(&aspflag, (char *)&af, r, c);
  89. n_depr_cells++;
  90. }
  91. }
  92. }
  93. }
  94. G_percent(nrows, nrows, 2); /* finish it */
  95. if (depr_fd >= 0) {
  96. Rast_close(depr_fd);
  97. G_free(depr_buf);
  98. }
  99. G_debug(1, "%lld edge cells", heap_size - n_depr_cells);
  100. if (n_depr_cells)
  101. G_debug(1, "%lld cells in depressions", n_depr_cells);
  102. return 1;
  103. }