direction.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. using namespace std;
  27. #include <grass/iostream/ami.h>
  28. #include "types.h"
  29. #include "genericWindow.h"
  30. #include "nodata.h"
  31. /***************************************************************/
  32. /***************************************************************/
  33. class directionWindow: public genericWindow<bool> {
  34. public:
  35. int numdir;
  36. /***************************************************************/
  37. /* Modifies the window by setting to 1 only the neighbors to which
  38. direction point. This is the inverse function of encodeDirection().
  39. direction:
  40. 32 64 128
  41. 16 * 1
  42. 8 4 2 */
  43. directionWindow(direction_type dir)
  44. : genericWindow<bool>() {
  45. /* first set everything to 0 */
  46. numdir = 0;
  47. int i;
  48. for (i=0; i<9; i++) {
  49. set(i, false);
  50. }
  51. if (dir == 0 || dir == DIRECTION_UNDEF) {
  52. return;
  53. }
  54. assert(dir > 0 && dir < 256);
  55. if (dir & 1) {
  56. set(5, true); numdir++;
  57. }
  58. if (dir & 2) {
  59. set(8, true); numdir++;
  60. }
  61. if (dir & 4) {
  62. set(7, true); numdir++;
  63. }
  64. if (dir & 8) {
  65. set(6, true); numdir++;
  66. }
  67. if (dir & 16) {
  68. set(3, true); numdir++;
  69. }
  70. if (dir & 32) {
  71. set(0, true); numdir++;
  72. }
  73. if (dir & 64) {
  74. set(1, true); numdir++;
  75. }
  76. if (dir & 128) {
  77. set(2, true); numdir++;
  78. }
  79. }
  80. /***************************************************************/
  81. /* Check direction consistency. */
  82. void checkDirection(short di, short dj, int skipit,
  83. elevation_type el, elevation_type elneighb) const {
  84. #ifndef NDEBUG
  85. if (skipit == 1) {
  86. assert(get(di,dj) == false);
  87. } else {
  88. if ((el > elneighb) && !is_nodata(elneighb) && !is_nodata(el)) {
  89. assert(get(di,dj) == true);
  90. }
  91. }
  92. #endif
  93. }
  94. /***************************************************************/
  95. /* Correct direction (di,dj). If direction points to invalid
  96. neighbor which must be skipped, set it to 0; if direction does
  97. not point to valid downslope neighbor, set it to 1. */
  98. void correctDirection(short di, short dj, int skipit,
  99. dimension_type i, dimension_type j,
  100. elevation_type elev_crt, direction_type dir,
  101. elevation_type elev_neighb) {
  102. if (skipit && get(di,dj) == true) {
  103. cout << "WARNING: at ("
  104. << i << "," << j << " , h=" << elev_crt << ", dir=" << dir << ")"
  105. << "direction points to non-valid neighbor ("
  106. << i + di << ","
  107. << j + dj << ", h="
  108. << elev_crt - elev_neighb
  109. << ")\n";
  110. set(di,dj, false); /* correct it */
  111. }
  112. if (!skipit && elev_crt > elev_neighb && !is_nodata(elev_neighb)
  113. && get(di,dj) == 0) {
  114. set(di,dj, true); /* correct it */
  115. }
  116. }
  117. };
  118. direction_type encodeDirection(const genericWindow<elevation_type>& elevwin,
  119. const dimension_type nrows,
  120. const dimension_type ncols,
  121. dimension_type row, dimension_type col);
  122. direction_type encodeDirectionMFD(const genericWindow<elevation_type>& elevwin,
  123. const dimension_type nrows,
  124. const dimension_type ncols,
  125. dimension_type row, dimension_type col);
  126. direction_type encodeDirectionSFD(const genericWindow<elevation_type>& elevwin,
  127. const dimension_type nrows,
  128. const dimension_type ncols,
  129. dimension_type row, dimension_type col);
  130. direction_type findDominant(direction_type dir);
  131. char directionSymbol(direction_type dir);
  132. #endif