c_point.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*!
  2. \file cluster/c_point.c
  3. \brief Cluster library - Add point
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <grass/raster.h>
  10. #include <grass/cluster.h>
  11. static int extend(struct Cluster *, int);
  12. static int all_zero(struct Cluster *, int);
  13. /*!
  14. \brief Adds the point x to the list of data points to be "clustered"
  15. The dimension of x must agree with the number of bands specified
  16. in the initializing call to I_cluster_begin()
  17. Note: if all values in x are zero, the point is rejected
  18. \return 0 ok
  19. \return -1 out of memory, point not added
  20. \return 1 all values are null, point not added
  21. */
  22. int I_cluster_point(struct Cluster *C, DCELL * x)
  23. {
  24. int band;
  25. /* reject points which contain nulls in one of the bands */
  26. for (band = 0; band < C->nbands; band++)
  27. if (Rast_is_d_null_value(&x[band]))
  28. return 1; /* fixed 11/99 Agus Carr */
  29. /*
  30. if (band >= C->nbands)
  31. return 1;
  32. */
  33. /* extend the arrays for each band, if necessary */
  34. if (!extend(C, 1))
  35. return -1;
  36. /* add the point to the points arrays */
  37. for (band = 0; band < C->nbands; band++) {
  38. register double z;
  39. /* if(Rast_is_d_null_value(&x[band])) continue; */
  40. z = C->points[band][C->npoints] = x[band];
  41. C->band_sum[band] += z;
  42. C->band_sum2[band] += z * z;
  43. }
  44. C->npoints++;
  45. return 0;
  46. }
  47. /*!
  48. \brief Begin point set
  49. \param C pointer to Cluster structure
  50. \param n ?
  51. \return 0 on success
  52. \return -1 on error
  53. */
  54. int I_cluster_begin_point_set(struct Cluster *C, int n)
  55. {
  56. return extend(C, n) ? 0 : -1;
  57. }
  58. /*!
  59. \brief ?
  60. \param C pointer to Cluster structure
  61. \param x cell value
  62. \param band band number
  63. \param n ?
  64. \return 0 ok
  65. \return -1 out of memory, point not added
  66. \return 1 all values are null, point not added
  67. */
  68. int I_cluster_point_part(struct Cluster *C, DCELL x, int band, int n)
  69. {
  70. DCELL tmp = x;
  71. if (Rast_is_d_null_value(&tmp))
  72. return 1;
  73. C->points[band][C->npoints + n] = x;
  74. C->band_sum[band] += x;
  75. C->band_sum2[band] += x * x;
  76. return 0;
  77. }
  78. /*!
  79. \brief ?
  80. \param C pointer to Cluster structure
  81. \param n ?
  82. \return number of points
  83. */
  84. int I_cluster_end_point_set(struct Cluster *C, int n)
  85. {
  86. int band;
  87. int cur, next;
  88. cur = C->npoints;
  89. n += C->npoints;
  90. for (next = cur; next < n; next++) {
  91. if (!all_zero(C, next)) {
  92. if (cur != next)
  93. for (band = 0; band < C->nbands; band++)
  94. C->points[band][cur] = C->points[band][next];
  95. cur++;
  96. }
  97. }
  98. return C->npoints = cur;
  99. }
  100. static int all_zero(struct Cluster *C, int i)
  101. {
  102. int band;
  103. for (band = 0; band < C->nbands; band++)
  104. if (C->points[band][i])
  105. return 0;
  106. return 1;
  107. }
  108. static int extend(struct Cluster *C, int n)
  109. {
  110. int band;
  111. while ((C->npoints + n) > C->np) {
  112. C->np += 128;
  113. for (band = 0; band < C->nbands; band++) {
  114. C->points[band] =
  115. (DCELL *) I_realloc(C->points[band], C->np * sizeof(DCELL));
  116. if (C->points[band] == NULL)
  117. return 0;
  118. }
  119. }
  120. return 1;
  121. }