stream_segment.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "local_proto.h"
  2. static int sector_cat = 0;
  3. float calc_dir(int rp, int cp, int rn, int cn)
  4. {
  5. return
  6. (cp - cn) == 0 ?
  7. (rp - rn) > 0 ? 0 : PI :
  8. (cp - cn) < 0 ?
  9. PI / 2 + atan((rp - rn) / (float)(cp - cn)) :
  10. 3 * PI / 2 + atan((rp - rn) / (float)(cp - cn));
  11. }
  12. float calc_length(double *distance, int start, int stop)
  13. {
  14. float cum_length = 0;
  15. int i;
  16. for (i = start; i < stop; ++i)
  17. cum_length += distance[i];
  18. return cum_length;
  19. }
  20. double calc_drop(float *elevation, int start, int stop)
  21. {
  22. float result;
  23. result = elevation[start] - elevation[stop];
  24. return result < 0 ? 0 : result;
  25. }
  26. double calc_stright(int rp, int cp, int rn, int cn)
  27. {
  28. double northing, easting, next_northing, next_easting;
  29. northing = window.north - (rp + .5) * window.ns_res;
  30. easting = window.west + (cp + .5) * window.ew_res;
  31. next_northing = window.north - (rn + .5) * window.ns_res;
  32. next_easting = window.west + (cn + .5) * window.ew_res;
  33. return G_distance(easting, northing, next_easting, next_northing);
  34. }
  35. int create_sectors(STREAM *cur_stream, int seg_length, int seg_skip,
  36. double seg_treshold)
  37. {
  38. DIRCELLS *streamline;
  39. unsigned long int *P; /* alias for points */
  40. int i, prev_i = 0;
  41. int number_of_cells;
  42. int cell_down, cell_up;
  43. int r, c, r_up, c_up, r_down, c_down;
  44. int seg_length_short = seg_length / 3;
  45. float dir_down, dir_up, dir_diff;
  46. float local_minimum = PI;
  47. int number_of_sectors = 0;
  48. int local_minimum_point = 0;
  49. int sector_index = 0;
  50. int in_loop = 0;
  51. int num_of_points = 0, num_of_breakpoints = 0;
  52. number_of_cells = cur_stream->number_of_cells - 1;
  53. P = cur_stream->points;
  54. streamline =
  55. (DIRCELLS *) G_malloc((number_of_cells + 1) * sizeof(DIRCELLS));
  56. /* init cells */
  57. for (i = 0; i < number_of_cells + 1; ++i) {
  58. streamline[i].long_dir_diff = 0;
  59. streamline[i].short_dir_diff = 0;
  60. streamline[i].long_break = 0;
  61. streamline[i].decision = 0;
  62. }
  63. /* upstream: to init, downstream: to outlet */
  64. for (i = seg_skip; i < number_of_cells - seg_skip; ++i) {
  65. cell_up = i < seg_length ? i : seg_length;
  66. cell_down = i > number_of_cells - 1 - seg_length ?
  67. number_of_cells - 1 - i : seg_length;
  68. r = (int)P[i] / ncols;
  69. c = (int)P[i] % ncols;
  70. r_up = (int)P[i - cell_up] / ncols;
  71. c_up = (int)P[i - cell_up] % ncols;
  72. r_down = (int)P[i + cell_down] / ncols;
  73. c_down = (int)P[i + cell_down] % ncols;
  74. dir_down = calc_dir(r, c, r_down, c_down);
  75. dir_up = calc_dir(r, c, r_up, c_up);
  76. dir_diff = fabs(dir_up - dir_down);
  77. streamline[i].long_dir_diff =
  78. dir_diff > PI ? PI * 2 - dir_diff : dir_diff;
  79. streamline[i].long_break =
  80. (streamline[i].long_dir_diff < seg_treshold) ? 1 : 0;
  81. cell_up = i < seg_length_short ? i : seg_length_short;
  82. cell_down = i > number_of_cells - 1 - seg_length_short ?
  83. number_of_cells - 1 - i : seg_length_short;
  84. r = (int)P[i] / ncols;
  85. c = (int)P[i] % ncols;
  86. r_up = (int)P[i - cell_up] / ncols;
  87. c_up = (int)P[i - cell_up] % ncols;
  88. r_down = (int)P[i + cell_down] / ncols;
  89. c_down = (int)P[i + cell_down] % ncols;
  90. dir_down = calc_dir(r, c, r_down, c_down);
  91. dir_up = calc_dir(r, c, r_up, c_up);
  92. dir_diff = fabs(dir_up - dir_down);
  93. streamline[i].short_dir_diff =
  94. dir_diff > PI ? PI * 2 - dir_diff : dir_diff;
  95. }
  96. /* look for breakpoints */
  97. for (i = 0; i < number_of_cells; ++i) {
  98. if (streamline[i].long_break) {
  99. num_of_breakpoints = 0;
  100. if (local_minimum > streamline[i].short_dir_diff) {
  101. local_minimum = streamline[i].short_dir_diff;
  102. local_minimum_point = i;
  103. in_loop = 1;
  104. } /* end local minimum */
  105. }
  106. else if (!streamline[i].long_break && in_loop) {
  107. num_of_breakpoints++;
  108. if (num_of_breakpoints == (seg_length / 5)) {
  109. streamline[local_minimum_point].decision = 1;
  110. local_minimum = PI;
  111. in_loop = 0;
  112. }
  113. }
  114. }
  115. /* cleaning breakpoints */
  116. for (i = 0, num_of_points = 0; i < number_of_cells; ++i, ++num_of_points) {
  117. if (streamline[i].decision) {
  118. //printf(" BEFORE %d %d\n",i,num_of_points);
  119. if (i < seg_skip || (i > seg_skip && num_of_points < seg_skip)) {
  120. streamline[i].decision = 0;
  121. i = local_minimum_point;
  122. }
  123. else {
  124. local_minimum_point = i;
  125. }
  126. num_of_points = 0;
  127. }
  128. }
  129. /* number of segment in streamline */
  130. for (i = 0; i < number_of_cells + 1; ++i)
  131. if (streamline[i].decision == 1 || i == (number_of_cells - 1))
  132. number_of_sectors++;
  133. cur_stream->number_of_sectors = number_of_sectors;
  134. cur_stream->sector_breakpoints =
  135. (int *)G_malloc(number_of_sectors * sizeof(int));
  136. cur_stream->sector_cats =
  137. (int *)G_malloc(number_of_sectors * sizeof(int));
  138. cur_stream->sector_directions =
  139. (float *)G_malloc(number_of_sectors * sizeof(float));
  140. cur_stream->sector_strights =
  141. (float *)G_malloc(number_of_sectors * sizeof(float));
  142. cur_stream->sector_lengths =
  143. (double *)G_malloc(number_of_sectors * sizeof(double));
  144. cur_stream->sector_drops =
  145. (float *)G_malloc(number_of_sectors * sizeof(float));
  146. /* add attributes */
  147. for (i = 0, prev_i = 0; i < number_of_cells + 1; ++i) {
  148. if (streamline[i].decision == 1 || i == (number_of_cells - 1)) {
  149. r = (int)P[i] / ncols;
  150. c = (int)P[i] % ncols;
  151. r_up = (int)P[prev_i] / ncols;
  152. c_up = (int)P[prev_i] % ncols;
  153. cur_stream->sector_breakpoints[sector_index] = i;
  154. cur_stream->sector_directions[sector_index] =
  155. calc_dir(r_up, c_up, r, c);
  156. cur_stream->sector_lengths[sector_index] =
  157. calc_length(cur_stream->distance, prev_i, i);
  158. cur_stream->sector_strights[sector_index] =
  159. calc_stright(r_up, c_up, r, c);
  160. cur_stream->sector_drops[sector_index] =
  161. calc_drop(cur_stream->elevation, prev_i, i);
  162. cur_stream->sector_cats[sector_index] = ++sector_cat;
  163. sector_index++;
  164. if (i < (number_of_cells - 1))
  165. prev_i = i;
  166. }
  167. }
  168. /*
  169. for (i = 0; i < number_of_cells; ++i)
  170. printf("%d | %f %f |break %d | Dec %d \n" ,i,
  171. streamline[i].long_dir_diff,
  172. streamline[i].short_dir_diff,
  173. streamline[i].long_break,
  174. streamline[i].decision);
  175. */
  176. G_free(streamline);
  177. return 0;
  178. }
  179. int calc_tangents(STREAM *cur_stream, int seg_length, int seg_skip,
  180. int number_streams)
  181. {
  182. int i;
  183. int cell_up, cell_down;
  184. int r, c, r_up, c_up, r_down, c_down;
  185. STREAM *SA = stream_attributes;
  186. unsigned long int *P = cur_stream->points;
  187. int next_stream = cur_stream->next_stream;
  188. unsigned int outlet = cur_stream->outlet;
  189. int last_cell = cur_stream->number_of_cells - 1;
  190. int reached_end = 1;
  191. G_debug(3, "calc_tangents(): number_streams=%d", number_streams);
  192. /*before calc tangents add rest of streamline attributes */
  193. r_up = (int)P[1] / ncols;
  194. c_up = (int)P[1] % ncols;
  195. r_down = (int)P[last_cell] / ncols;
  196. c_down = (int)P[last_cell] % ncols;
  197. cur_stream->direction = calc_dir(r_up, c_up, r_down, c_down);
  198. cur_stream->length = calc_length(cur_stream->distance, 1, last_cell);
  199. cur_stream->stright = calc_stright(r_up, c_up, r_down, c_down);
  200. cur_stream->drop = calc_drop(cur_stream->elevation, 1, last_cell);
  201. if (next_stream < 1) {
  202. cur_stream->tangent = -1;
  203. cur_stream->continuation = -1;
  204. return 0;
  205. }
  206. /* find location of outlet in next stream */
  207. for (i = 1; i < SA[next_stream].number_of_cells; ++i) {
  208. if (SA[next_stream].points[i] == outlet) {
  209. reached_end = 0;
  210. break;
  211. }
  212. }
  213. /* outlet not lies on the next stream */
  214. if (reached_end) {
  215. G_warning(_("Network topology error: cannot identify stream join for stream %d"),
  216. cur_stream->stream);
  217. cur_stream->tangent = -1;
  218. cur_stream->continuation = -1;
  219. return 0;
  220. }
  221. cell_up = i <= seg_length ? i - 1 : seg_length;
  222. cell_down = i >= (SA[next_stream].number_of_cells - seg_length) ?
  223. SA[next_stream].number_of_cells - seg_length - 1 : seg_length;
  224. r = (int)SA[next_stream].points[i] / ncols;
  225. c = (int)SA[next_stream].points[i] % ncols;
  226. r_up = (int)SA[next_stream].points[i - cell_up] / ncols;
  227. c_up = (int)SA[next_stream].points[i - cell_up] % ncols;
  228. r_down = (int)SA[next_stream].points[i + cell_down] / ncols;
  229. c_down = (int)SA[next_stream].points[i + cell_down] % ncols;
  230. cur_stream->continuation = calc_dir(r, c, r_down, c_down);
  231. cur_stream->tangent = i == 1 ? -1 :
  232. i < seg_skip ? cur_stream->continuation : calc_dir(r_up, c_up, r_down,
  233. c_down);
  234. return 0;
  235. }