secpar2d.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*!
  2. * \file secpar2d.c
  3. *
  4. * \author H. Mitasova, L. Mitas, I. Kosinovsky, D. Gerdes Fall 1994 (original authors)
  5. * \author modified by McCauley in August 1995
  6. * \author modified by Mitasova in August 1995
  7. * \author H. Mitasova (University of Illinois)
  8. * \author L. Mitas (University of Illinois)
  9. * \author I. Kosinovsky, (USA-CERL)
  10. * \author D.Gerdes (USA-CERL)
  11. *
  12. * \copyright
  13. * (C) 1994-1995 by Helena Mitasova and the GRASS Development Team
  14. *
  15. * \copyright
  16. * This program is free software under the
  17. * GNU General Public License (>=v2).
  18. * Read the file COPYING that comes with GRASS
  19. * for details.
  20. *
  21. */
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <unistd.h>
  25. #include <grass/gis.h>
  26. #include <grass/bitmap.h>
  27. #include <grass/interpf.h>
  28. /*!
  29. * Compute slope aspect and curvatures
  30. *
  31. * Computes slope, aspect and curvatures (depending on cond1, cond2) for
  32. * derivative arrays adx,...,adxy between columns ngstc and nszc.
  33. */
  34. int IL_secpar_loop_2d(struct interp_params *params,
  35. int ngstc, /*!< starting column */
  36. int nszc, /*!< ending column */
  37. int k, /*!< current row */
  38. struct BM *bitmask,
  39. double *gmin, double *gmax,
  40. double *c1min, double *c1max,
  41. double *c2min, double *c2max, /*!< min,max interp. values */
  42. int cond1,
  43. int cond2 /*!< determine if particular values need to be computed */
  44. )
  45. {
  46. double dnorm1, ro, /* rad to deg conv */
  47. dx2 = 0, dy2 = 0, grad2 = 0, /* gradient squared */
  48. slp = 0, grad, /* gradient */
  49. oor = 0, /* aspect (orientation) */
  50. curn = 0, /* profile curvature */
  51. curh = 0, /* tangential curvature */
  52. curm = 0, /* mean curvature */
  53. temp, /* temp variable */
  54. dxy2; /* temp variable square of part diriv. */
  55. double gradmin;
  56. int i, got, bmask = 1;
  57. static int first_time_g = 1;
  58. ro = M_R2D;
  59. gradmin = 0.001;
  60. for (i = ngstc; i <= nszc; i++) {
  61. if (bitmask != NULL) {
  62. bmask = BM_get(bitmask, i, k);
  63. }
  64. got = 0;
  65. if (bmask == 1) {
  66. while ((got == 0) && (cond1)) {
  67. dx2 = (double)(params->adx[i] * params->adx[i]);
  68. dy2 = (double)(params->ady[i] * params->ady[i]);
  69. grad2 = dx2 + dy2;
  70. grad = sqrt(grad2);
  71. /* slope in % slp = 100. * grad; */
  72. /* slope in degrees */
  73. slp = ro * atan(grad);
  74. if (grad <= gradmin) {
  75. oor = 0.;
  76. got = 3;
  77. if (cond2) {
  78. curn = 0.;
  79. curh = 0.;
  80. got = 3;
  81. break;
  82. }
  83. }
  84. if (got == 3)
  85. break;
  86. /***********aspect from r.slope.aspect, with adx, ady computed
  87. from interpol. function RST **************************/
  88. if (params->adx[i] == 0.) {
  89. if (params->ady[i] > 0.)
  90. oor = 90;
  91. else
  92. oor = 270;
  93. }
  94. else {
  95. oor = ro * atan2(params->ady[i], params->adx[i]);
  96. if (oor <= 0.)
  97. oor = 360. + oor;
  98. }
  99. got = 1;
  100. } /* while */
  101. if ((got != 3) && (cond2)) {
  102. dnorm1 = sqrt(grad2 + 1.);
  103. dxy2 =
  104. 2. * (double)(params->adxy[i] * params->adx[i] *
  105. params->ady[i]);
  106. curn =
  107. (double)(params->adxx[i] * dx2 + dxy2 +
  108. params->adyy[i] * dy2) / (grad2 * dnorm1 *
  109. dnorm1 * dnorm1);
  110. curh =
  111. (double)(params->adxx[i] * dy2 - dxy2 +
  112. params->adyy[i] * dx2) / (grad2 * dnorm1);
  113. temp = grad2 + 1.;
  114. curm =
  115. .5 * ((1. + dy2) * params->adxx[i] - dxy2 +
  116. (1. + dx2) * params->adyy[i]) / (temp * dnorm1);
  117. }
  118. if (first_time_g) {
  119. first_time_g = 0;
  120. *gmin = *gmax = slp;
  121. *c1min = *c1max = curn;
  122. *c2min = *c2max = curh;
  123. }
  124. *gmin = amin1(*gmin, slp);
  125. *gmax = amax1(*gmax, slp);
  126. *c1min = amin1(*c1min, curn);
  127. *c1max = amax1(*c1max, curn);
  128. *c2min = amin1(*c2min, curh);
  129. *c2max = amax1(*c2max, curh);
  130. if (cond1) {
  131. params->adx[i] = (FCELL) slp;
  132. params->ady[i] = (FCELL) oor;
  133. if (cond2) {
  134. params->adxx[i] = (FCELL) curn;
  135. params->adyy[i] = (FCELL) curh;
  136. params->adxy[i] = (FCELL) curm;
  137. }
  138. }
  139. } /* bmask == 1 */
  140. }
  141. return 1;
  142. }