c_begin.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*!
  2. \file cluster/c_begin.c
  3. \brief Cluster library - Begin clusterring
  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 <stdlib.h>
  10. #include <grass/glocale.h>
  11. #include <grass/cluster.h>
  12. /*!
  13. \brief Initialize the cluster routines for nbands
  14. \param C pointer to Cluster structure
  15. \param nbands number of bands
  16. \return 0 ok
  17. \return -1 out of memory
  18. \return 1 illegal number of bands
  19. */
  20. int I_cluster_begin(struct Cluster *C, int nbands)
  21. {
  22. int band;
  23. if (C->points != NULL) {
  24. for (band = 0; band < C->nbands; band++)
  25. if (C->points[band] != NULL)
  26. free(C->points[band]);
  27. free(C->points);
  28. }
  29. if (C->band_sum != NULL)
  30. free(C->band_sum);
  31. if (C->band_sum2 != NULL)
  32. free(C->band_sum2);
  33. C->points = NULL;
  34. C->band_sum = NULL;
  35. C->band_sum2 = NULL;
  36. I_free_signatures(&C->S);
  37. /* record the number of bands */
  38. C->nbands = nbands;
  39. if (nbands <= 0)
  40. return 1;
  41. /* prepare the signatures for nbands */
  42. I_init_signatures(&C->S, nbands);
  43. sprintf(C->S.title, _("produced by i.cluster"));
  44. /* allocate the data (points) arrays */
  45. C->points = (DCELL **) malloc(C->nbands * sizeof(DCELL *));
  46. if (C->points == NULL)
  47. return -1;
  48. for (band = 0; band < C->nbands; band++)
  49. C->points[band] = NULL;
  50. C->np = 128;
  51. for (band = 0; band < C->nbands; band++) {
  52. C->points[band] = (DCELL *) malloc(C->np * sizeof(DCELL));
  53. if (C->points[band] == NULL)
  54. return -1;
  55. }
  56. /* initialize the count to zero */
  57. C->npoints = 0;
  58. /* allocate the band sums and means */
  59. C->band_sum = (double *)malloc(C->nbands * sizeof(double));
  60. if (C->band_sum == NULL)
  61. return -1;
  62. C->band_sum2 = (double *)malloc(C->nbands * sizeof(double));
  63. if (C->band_sum2 == NULL)
  64. return -1;
  65. for (band = 0; band < C->nbands; band++) {
  66. C->band_sum[band] = 0;
  67. C->band_sum2[band] = 0;
  68. }
  69. return 0;
  70. }