c_point.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /****************************************************************
  2. * I_cluster_point (C,x)
  3. * struct Cluster *C;
  4. * DCELL *x;
  5. *
  6. * adds the point x to the list of data points to be "clustered"
  7. *
  8. * returns
  9. * 0 ok
  10. * -1 out of memory, point not added
  11. * 1 all values are zero, point not added
  12. *
  13. * the dimension of x must agree with the number of bands specified
  14. * in the initializing call to I_cluster_begin()
  15. *
  16. * note: if all values in x are zero, the point is rejected
  17. ***************************************************************/
  18. #include <grass/cluster.h>
  19. static int extend(struct Cluster *, int);
  20. static int all_zero(struct Cluster *, int);
  21. int I_cluster_point(struct Cluster *C, DCELL * x)
  22. {
  23. int band;
  24. /* reject points which contain nulls in one of the bands */
  25. for (band = 0; band < C->nbands; band++)
  26. if (G_is_d_null_value(&x[band]))
  27. return 1; /* fixed 11/99 Agus Carr */
  28. /*
  29. if (band >= C->nbands)
  30. return 1;
  31. */
  32. /* extend the arrays for each band, if necessary */
  33. if (!extend(C, 1))
  34. return -1;
  35. /* add the point to the points arrays */
  36. for (band = 0; band < C->nbands; band++) {
  37. register double z;
  38. /* if(G_is_d_null_value(&x[band])) continue; */
  39. z = C->points[band][C->npoints] = x[band];
  40. C->band_sum[band] += z;
  41. C->band_sum2[band] += z * z;
  42. }
  43. C->npoints++;
  44. return 0;
  45. }
  46. int I_cluster_begin_point_set(struct Cluster *C, int n)
  47. {
  48. return extend(C, n) ? 0 : -1;
  49. }
  50. int I_cluster_point_part(struct Cluster *C, DCELL x, int band, int n)
  51. {
  52. DCELL tmp = x;
  53. if (G_is_d_null_value(&tmp))
  54. return 1;
  55. C->points[band][C->npoints + n] = x;
  56. C->band_sum[band] += x;
  57. C->band_sum2[band] += x * x;
  58. return 0;
  59. }
  60. int I_cluster_end_point_set(struct Cluster *C, int n)
  61. {
  62. int band;
  63. int cur, next;
  64. cur = C->npoints;
  65. n += C->npoints;
  66. for (next = cur; next < n; next++) {
  67. if (!all_zero(C, next)) {
  68. if (cur != next)
  69. for (band = 0; band < C->nbands; band++)
  70. C->points[band][cur] = C->points[band][next];
  71. cur++;
  72. }
  73. }
  74. return C->npoints = cur;
  75. }
  76. static int all_zero(struct Cluster *C, int i)
  77. {
  78. int band;
  79. for (band = 0; band < C->nbands; band++)
  80. if (C->points[band][i])
  81. return 0;
  82. return 1;
  83. }
  84. static int extend(struct Cluster *C, int n)
  85. {
  86. int band;
  87. while ((C->npoints + n) > C->np) {
  88. C->np += 128;
  89. for (band = 0; band < C->nbands; band++) {
  90. C->points[band] =
  91. (DCELL *) I_realloc(C->points[band], C->np * sizeof(DCELL));
  92. if (C->points[band] == NULL)
  93. return 0;
  94. }
  95. }
  96. return 1;
  97. }