direction.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /****************************************************************************
  2. *
  3. * MODULE: r.terraflow
  4. *
  5. * COPYRIGHT (C) 2007 Laura Toma
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *****************************************************************************/
  18. #ifndef _direction_H
  19. #define _direction_H
  20. #include <stdio.h>
  21. #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
  22. #include <ostream>
  23. #else
  24. #include <ostream.h>
  25. #endif
  26. #include <grass/iostream/ami.h>
  27. #include "types.h"
  28. #include "genericWindow.h"
  29. #include "nodata.h"
  30. /***************************************************************/
  31. /***************************************************************/
  32. class directionWindow: public genericWindow<bool> {
  33. public:
  34. int numdir;
  35. /***************************************************************/
  36. /* Modifies the window by setting to 1 only the neighbors to which
  37. direction point. This is the inverse function of encodeDirection().
  38. direction:
  39. 32 64 128
  40. 16 * 1
  41. 8 4 2 */
  42. directionWindow(direction_type dir)
  43. : genericWindow<bool>() {
  44. /* first set everything to 0 */
  45. numdir = 0;
  46. int i;
  47. for (i=0; i<9; i++) {
  48. set(i, false);
  49. }
  50. if (dir == 0 || dir == DIRECTION_UNDEF) {
  51. return;
  52. }
  53. assert(dir > 0 && dir < 256);
  54. if (dir & 1) {
  55. set(5, true); numdir++;
  56. }
  57. if (dir & 2) {
  58. set(8, true); numdir++;
  59. }
  60. if (dir & 4) {
  61. set(7, true); numdir++;
  62. }
  63. if (dir & 8) {
  64. set(6, true); numdir++;
  65. }
  66. if (dir & 16) {
  67. set(3, true); numdir++;
  68. }
  69. if (dir & 32) {
  70. set(0, true); numdir++;
  71. }
  72. if (dir & 64) {
  73. set(1, true); numdir++;
  74. }
  75. if (dir & 128) {
  76. set(2, true); numdir++;
  77. }
  78. }
  79. /***************************************************************/
  80. /* Check direction consistency. */
  81. void checkDirection(short di, short dj, int skipit,
  82. elevation_type el, elevation_type elneighb) const {
  83. #ifndef NDEBUG
  84. if (skipit == 1) {
  85. assert(get(di,dj) == false);
  86. } else {
  87. if ((el > elneighb) && !is_nodata(elneighb) && !is_nodata(el)) {
  88. assert(get(di,dj) == true);
  89. }
  90. }
  91. #endif
  92. }
  93. /***************************************************************/
  94. /* Correct direction (di,dj). If direction points to invalid
  95. neighbor which must be skipped, set it to 0; if direction does
  96. not point to valid downslope neighbor, set it to 1. */
  97. void correctDirection(short di, short dj, int skipit,
  98. dimension_type i, dimension_type j,
  99. elevation_type elev_crt, direction_type dir,
  100. elevation_type elev_neighb) {
  101. if (skipit && get(di,dj) == true) {
  102. cout << "WARNING: at ("
  103. << i << "," << j << " , h=" << elev_crt << ", dir=" << dir << ")"
  104. << "direction points to non-valid neighbor ("
  105. << i + di << ","
  106. << j + dj << ", h="
  107. << elev_crt - elev_neighb
  108. << ")\n";
  109. set(di,dj, false); /* correct it */
  110. }
  111. if (!skipit && elev_crt > elev_neighb && !is_nodata(elev_neighb)
  112. && get(di,dj) == 0) {
  113. set(di,dj, true); /* correct it */
  114. }
  115. }
  116. };
  117. direction_type encodeDirection(const genericWindow<elevation_type>& elevwin,
  118. const dimension_type nrows,
  119. const dimension_type ncols,
  120. dimension_type row, dimension_type col);
  121. direction_type encodeDirectionMFD(const genericWindow<elevation_type>& elevwin,
  122. const dimension_type nrows,
  123. const dimension_type ncols,
  124. dimension_type row, dimension_type col);
  125. direction_type encodeDirectionSFD(const genericWindow<elevation_type>& elevwin,
  126. const dimension_type nrows,
  127. const dimension_type ncols,
  128. dimension_type row, dimension_type col);
  129. direction_type findDominant(direction_type dir);
  130. char directionSymbol(direction_type dir);
  131. #endif