over_cells.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "Gwater.h"
  2. #define BIGNEG -9999999
  3. int
  4. overland_cells_recursive(int row, int col, CELL basin_num, CELL haf_num, CELL * hih_ele)
  5. {
  6. int r, rr, c, cc;
  7. CELL new_ele, new_max_ele, value;
  8. bas[SEG_INDEX(bas_seg, row, col)] = basin_num;
  9. haf[SEG_INDEX(haf_seg, row, col)] = haf_num;
  10. new_max_ele = BIGNEG;
  11. for (r = row - 1, rr = 0; r <= row + 1; r++, rr++) {
  12. for (c = col - 1, cc = 0; c <= col + 1; c++, cc++) {
  13. if (r >= 0 && c >= 0 && r < nrows && c < ncols) {
  14. if (r == row && c == col)
  15. continue;
  16. value = asp[SEG_INDEX(asp_seg, r, c)];
  17. if (value == drain[rr][cc]) {
  18. overland_cells(r, c, basin_num, haf_num, &new_ele);
  19. }
  20. }
  21. }
  22. }
  23. /*
  24. if (arm_flag) {
  25. if (new_max_ele == BIGNEG) {
  26. *hih_ele = alt[SEG_INDEX(alt_seg, row, col)];
  27. }
  28. else {
  29. *hih_ele = new_max_ele;
  30. }
  31. }
  32. */
  33. return 0;
  34. }
  35. /* non-recursive version */
  36. int
  37. overland_cells(int row, int col, CELL basin_num, CELL haf_num, CELL * hih_ele)
  38. {
  39. int r, c, rr, cc, next_r, next_c;
  40. int top = 0, idx;
  41. /* put root on stack */
  42. ocs[top].row = row;
  43. ocs[top].col = col;
  44. idx = SEG_INDEX(bas_seg, row, col);
  45. bas[idx] = basin_num;
  46. haf[idx] = haf_num;
  47. top++;
  48. while (top) {
  49. top--;
  50. next_r = ocs[top].row;
  51. next_c = ocs[top].col;
  52. for (r = next_r - 1, rr = 0; r <= next_r + 1; r++, rr++) {
  53. for (c = next_c - 1, cc = 0; c <= next_c + 1; c++, cc++) {
  54. if (r >= 0 && c >= 0 && r < nrows && c < ncols) {
  55. if (r == row && c == col)
  56. continue;
  57. idx = SEG_INDEX(bas_seg, r, c);
  58. if (asp[idx] == drain[rr][cc]) {
  59. if (top >= ocs_alloced) {
  60. ocs_alloced += bas_thres;
  61. ocs = (OC_STACK *)G_realloc(ocs, ocs_alloced * sizeof(OC_STACK));
  62. }
  63. ocs[top].row = r;
  64. ocs[top].col = c;
  65. bas[idx] = basin_num;
  66. haf[idx] = haf_num;
  67. top++;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. /*
  74. if (arm_flag) {
  75. if (new_max_ele == BIGNEG) {
  76. cseg_get(&alt, hih_ele, row, col);
  77. }
  78. else {
  79. *hih_ele = new_max_ele;
  80. }
  81. }
  82. */
  83. return 0;
  84. }