GRASS GIS 7 Programmer's Manual  7.0.4(2016)-r00000
c_begin.c
Go to the documentation of this file.
1 
14 #include <stdlib.h>
15 #include <grass/glocale.h>
16 #include <grass/cluster.h>
17 
28 int I_cluster_begin(struct Cluster *C, int nbands)
29 {
30  int band;
31 
32  if (C->points != NULL) {
33  for (band = 0; band < C->nbands; band++)
34  if (C->points[band] != NULL)
35  free(C->points[band]);
36  free(C->points);
37  }
38  if (C->band_sum != NULL)
39  free(C->band_sum);
40  if (C->band_sum2 != NULL)
41  free(C->band_sum2);
42 
43  C->points = NULL;
44  C->band_sum = NULL;
45  C->band_sum2 = NULL;
46 
47  I_free_signatures(&C->S);
48 
49  /* record the number of bands */
50  C->nbands = nbands;
51  if (nbands <= 0)
52  return 1;
53 
54  /* prepare the signatures for nbands */
55 
56  I_init_signatures(&C->S, nbands);
57  sprintf(C->S.title, _("produced by i.cluster"));
58 
59  /* allocate the data (points) arrays */
60  C->points = (DCELL **) malloc(C->nbands * sizeof(DCELL *));
61  if (C->points == NULL)
62  return -1;
63  for (band = 0; band < C->nbands; band++)
64  C->points[band] = NULL;
65 
66  C->np = 128;
67  for (band = 0; band < C->nbands; band++) {
68  C->points[band] = (DCELL *) malloc(C->np * sizeof(DCELL));
69  if (C->points[band] == NULL)
70  return -1;
71  }
72 
73  /* initialize the count to zero */
74  C->npoints = 0;
75 
76  /* allocate the band sums and means */
77  C->band_sum = (double *)malloc(C->nbands * sizeof(double));
78  if (C->band_sum == NULL)
79  return -1;
80  C->band_sum2 = (double *)malloc(C->nbands * sizeof(double));
81  if (C->band_sum2 == NULL)
82  return -1;
83  for (band = 0; band < C->nbands; band++) {
84  C->band_sum[band] = 0;
85  C->band_sum2[band] = 0;
86  }
87 
88  return 0;
89 }
#define NULL
Definition: ccmath.h:32
int I_cluster_begin(struct Cluster *C, int nbands)
Initialize the cluster routines for nbands.
Definition: c_begin.c:28