direction.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #include "direction.h"
  19. #include "nodata.h"
  20. #include "common.h" /*for global opt->d8 flag*/
  21. #define TF_ROOTTWO 1.4142135623
  22. /* directions:
  23. 32 64 128
  24. 16 * 1
  25. 8 4 2
  26. */
  27. direction_type
  28. encodeDirection(const genericWindow<elevation_type>& elevwin,
  29. const dimension_type nrows, const dimension_type ncols,
  30. dimension_type row,
  31. dimension_type col) {
  32. /*check global opt flag and call appropriate model*/
  33. if(opt->d8){
  34. return encodeDirectionSFD(elevwin, nrows, ncols, row, col);
  35. }
  36. return encodeDirectionMFD(elevwin, nrows, ncols, row, col);
  37. }
  38. /***************************************************************/
  39. /* returns the direction corresponding to the window using MFD */
  40. direction_type
  41. encodeDirectionMFD(const genericWindow<elevation_type>& elevwin,
  42. const dimension_type nrows, const dimension_type ncols,
  43. dimension_type row,
  44. dimension_type col) {
  45. direction_type dir = DIRECTION_UNDEF;
  46. if(!is_nodata(elevwin.get())) {
  47. dir = 0;
  48. if (elevwin.get(5) < elevwin.get() && !is_void(elevwin.get(5))) dir |= 1;
  49. if (elevwin.get(3) < elevwin.get() && !is_void(elevwin.get(3))) dir |= 16;
  50. for(int i=0; i<3; i++) {
  51. if(elevwin.get(i) < elevwin.get() && !is_void(elevwin.get(i))) dir |= 32<<i;
  52. if(elevwin.get(i+6) < elevwin.get() && !is_void(elevwin.get(6+i))) dir |= 8>>i;
  53. }
  54. }
  55. /* if no direction, check for boundary */
  56. if(dir==0 || dir==DIRECTION_UNDEF) {
  57. if(row==0) {
  58. dir = 32 | 64 | 128;
  59. }
  60. if(row==nrows-1) {
  61. dir = 2 | 4 | 8;
  62. }
  63. if(col==0) {
  64. if(row==0) dir = 32;
  65. else if(row==nrows-1) dir = 8;
  66. else dir = 8 | 16 | 32;
  67. }
  68. if(col==ncols-1) {
  69. if(row==0) dir = 128;
  70. else if(row==nrows-1) dir = 2;
  71. else dir = 128 | 1 | 2;
  72. }
  73. }
  74. return dir;
  75. }
  76. /***************************************************************/
  77. /* returns the direction corresponding to the window using SFD */
  78. direction_type
  79. encodeDirectionSFD(const genericWindow<elevation_type>& elevwin,
  80. const dimension_type nrows, const dimension_type ncols,
  81. dimension_type row,
  82. dimension_type col) {
  83. direction_type dir = DIRECTION_UNDEF;
  84. float tdrop, max_drop;
  85. int max_indx;
  86. if(!is_nodata(elevwin.get())) {
  87. dir = 0;
  88. max_drop = 0;
  89. max_indx = -1;
  90. for(int i=0; i<9; i++){
  91. if(i%2==1){ /*cardinal direction*/
  92. tdrop=elevwin.get()-elevwin.get(i);
  93. if(tdrop>max_drop){ max_drop=tdrop; max_indx=i; }
  94. }
  95. else if(i!=4){ /*diagonal*/
  96. tdrop=(elevwin.get()-elevwin.get(i))/TF_ROOTTWO;
  97. if(tdrop>max_drop){ max_drop=tdrop; max_indx=i; }
  98. }
  99. }
  100. switch(max_indx){
  101. case 0:
  102. case 1:
  103. case 2:
  104. dir=32<<max_indx; break;
  105. case 3:
  106. dir=16; break;
  107. case 5:
  108. dir=1; break;
  109. case 6:
  110. case 7:
  111. case 8:
  112. dir=8>>(max_indx-6); break;
  113. default:
  114. dir=0; break;
  115. }
  116. }
  117. /* if no direction, check for boundary */
  118. if(dir==0 || dir==DIRECTION_UNDEF) {
  119. if(row==0) {
  120. dir = 64;
  121. }
  122. if(row==nrows-1) {
  123. dir = 48;
  124. }
  125. if(col==0) {
  126. if(row==0) dir = 32;
  127. else if(row==nrows-1) dir = 8;
  128. else dir = 16;
  129. }
  130. if(col==ncols-1) {
  131. if(row==0) dir = 128;
  132. else if(row==nrows-1) dir = 2;
  133. else dir = 1;
  134. }
  135. }
  136. return dir;
  137. }
  138. direction_type
  139. findDominant(direction_type dir) {
  140. switch(dir) {
  141. case 1:
  142. case 2:
  143. case 4:
  144. case 8:
  145. case 16:
  146. case 32:
  147. case 64:
  148. case 128:
  149. return dir;
  150. case 1+2:
  151. case 128+1:
  152. return 1;
  153. case 2+4:
  154. case 4+8:
  155. return 4;
  156. case 8+16:
  157. case 16+32:
  158. return 16;
  159. case 32+64:
  160. case 64+128:
  161. return 64;
  162. case 1+2+4:
  163. return 2;
  164. case 2+4+8:
  165. return 4;
  166. case 4+8+16:
  167. return 8;
  168. case 8+16+32:
  169. return 16;
  170. case 16+32+64:
  171. return 32;
  172. case 32+64+128:
  173. return 64;
  174. case 64+128+1:
  175. return 128;
  176. case 128+1+2:
  177. return 1;
  178. case 128+1+2+4:
  179. case 64+128+1+2:
  180. return 1;
  181. case 1+2+4+8:
  182. case 2+4+8+16:
  183. return 4;
  184. case 8+16+32+64:
  185. case 4+8+16+32:
  186. return 16;
  187. case 32+64+128+1:
  188. case 16+32+64+128:
  189. return 64;
  190. case 64+128+1+2+4:
  191. return 1;
  192. case 128+1+2+4+8:
  193. return 2;
  194. case 1+2+4+8+16:
  195. return 4;
  196. case 2+4+8+16+32:
  197. return 8;
  198. case 4+8+16+32+64:
  199. return 16;
  200. case 8+16+32+64+128:
  201. return 32;
  202. case 16+32+64+128+1:
  203. return 64;
  204. case 32+64+128+1+2:
  205. return 128;
  206. }
  207. /* Otherwise, there is no dominant direction.
  208. * SFD must output a single direction 1,2,4,8,16,32,64, or 128
  209. * pick the first matching one, with preference to cardinal direction
  210. */
  211. if(dir & 85 ){
  212. /* at least one cardinal direction (1+4+16+64=E+S+W+N) matches */
  213. if(dir & 1){ return 1;}
  214. if(dir & 4){ return 4;}
  215. if(dir & 16){ return 16;}
  216. if(dir & 64){ return 64;}
  217. }
  218. else{
  219. /* 2 8 32 128 = SE SW NW NE*/
  220. if(dir & 2){ return 2;}
  221. if(dir & 8){ return 8;}
  222. if(dir & 32){ return 32;}
  223. if(dir & 128){ return 128;}
  224. }
  225. return dir; /* shouldn't get here unless dir <= 0 */
  226. }
  227. char
  228. directionSymbol(direction_type dir) {
  229. char c='?';
  230. int cnt=0;
  231. char symbols[] = ">\\v/<\\^/";
  232. if(dir == 0) return '.';
  233. dir = findDominant(dir);
  234. for(int i=0; i<8; i++) {
  235. if(dir & (1<<i)) {
  236. cnt++;
  237. c = symbols[i];
  238. }
  239. }
  240. if(cnt>1) c = 'X';
  241. switch(dir) {
  242. case 1+16:
  243. case 128+1+2+8+16+32:
  244. c = '-';
  245. break;
  246. case 1+2+8+16+32:
  247. case 128+1+8+16+32:
  248. c = '<';
  249. break;
  250. case 128+1+2+16+32:
  251. case 128+1+2+8+16:
  252. c = '>';
  253. break;
  254. case 4+64:
  255. case 2+4+8+32+64+128:
  256. c = '|';
  257. break;
  258. case 4+8+32+64+128:
  259. case 2+4+32+64+128:
  260. c = '^';
  261. break;
  262. case 2+4+8+64+128:
  263. case 2+4+8+32+64:
  264. c = 'v';
  265. break;
  266. case 255:
  267. c = '*';
  268. break;
  269. default:
  270. break;
  271. }
  272. return c;
  273. }
  274. #undef TF_ROOTTWO