weightWindow.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 _weight_H
  19. #define _weight_H
  20. #include <stdio.h>
  21. #include "types.h"
  22. #include "common.h"
  23. #include "genericWindow.h"
  24. #include "direction.h"
  25. class weightWindow {
  26. public:
  27. float cell_dx, cell_dy; /* dimension of cell in the grid */
  28. float celldiag; /* diagonal of a cell in the grid */
  29. float sumweight, sumcontour;
  30. genericWindow<float> weight; /* weights */
  31. protected:
  32. /* initialize all weights to 0 */
  33. void init();
  34. /* set weight of neighbor (di,dj) */
  35. void computeWeight(const short di, const short dj,
  36. const elevation_type elev_crt,
  37. const elevation_type elev_neighb);
  38. /* normalize weights */
  39. void normalize();
  40. /* computes the contour corresponding to this direction */
  41. double computeContour(const short di, const short dj);
  42. /* computes the distance corresponding to this direction */
  43. double computeDist(const short di, const short dj);
  44. /* compute the tanB corresponding to the elevation window and
  45. neighbor di,dj. */
  46. double computeTanB(const short di,const short dj,
  47. const genericWindow<elevation_type>& elevwin);
  48. public:
  49. weightWindow(const float gdx, const float gdy);
  50. ~weightWindow(){};
  51. /***************************************************************/
  52. /* Compute the weights of the neighbors of a cell given an elevation
  53. window and a direction window; if trustdir = 1 then trust
  54. directions; otherwise compute the downslope neighbors and use
  55. direction only for cells which do not have downslope neighbors */
  56. /***************************************************************/
  57. void compute(const dimension_type i, const dimension_type j,
  58. const genericWindow<elevation_type>& elevwin,
  59. const direction_type dir,
  60. const int trustdir);
  61. /* Find the dominant direction. Set corresponding weight to 1, and
  62. sets all other weights to 0. Set sumweight and sumcontour.*/
  63. void makeD8(const dimension_type i, const dimension_type j,
  64. const genericWindow<elevation_type>& elevwin,
  65. const direction_type dir,
  66. const bool trustdir);
  67. /* get specified weight di,dj in {-1,0,1} */
  68. float get(const short di, const short dj) const {
  69. return weight.get(di,dj);
  70. }
  71. /* get specified weight i in 0..8 */
  72. float get(const unsigned short i) const {
  73. return weight.get(i);
  74. }
  75. /* return the total contour */
  76. float totalContour() const {
  77. return sumcontour;
  78. }
  79. float totatWeight() const {
  80. return sumweight;
  81. }
  82. float dx() const {
  83. return cell_dx;
  84. }
  85. float dy() const {
  86. return cell_dy;
  87. }
  88. friend ostream& operator<<(ostream& s, const weightWindow &x) {
  89. return s << x.weight;
  90. }
  91. };
  92. #endif