c_assign.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*!
  2. \file cluster/c_assign.c
  3. \brief Cluster library - Assign cluster
  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 <math.h>
  10. #include <grass/cluster.h>
  11. /*!
  12. \brief Assign cluster
  13. \param C pointer to Cluster structure
  14. \param interrupted ?
  15. \return -1 on interrupted
  16. \return 0 on success
  17. */
  18. int I_cluster_assign(struct Cluster *C, int *interrupted)
  19. {
  20. int p, c;
  21. int class, band;
  22. double d, q;
  23. double dmin;
  24. G_debug(3, "I_cluster_assign(npoints=%d,nclasses=%d,nbands=%d)",
  25. C->npoints, C->nclasses, C->nbands);
  26. for (p = 0; p < C->npoints; p++) {
  27. if (*interrupted)
  28. return -1;
  29. dmin = HUGE_VAL;
  30. class = 0;
  31. for (c = 0; c < C->nclasses; c++) {
  32. d = 0.0;
  33. for (band = 0; band < C->nbands; band++) {
  34. q = C->points[band][p];
  35. q -= C->mean[band][c];
  36. d += q * q;
  37. }
  38. if (c == 0 || d < dmin) {
  39. class = c;
  40. dmin = d;
  41. }
  42. }
  43. C->class[p] = class;
  44. C->count[class]++;
  45. for (band = 0; band < C->nbands; band++)
  46. C->sum[band][class] += C->points[band][p];
  47. }
  48. return 0;
  49. }