GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
c_begin.c
Go to the documentation of this file.
1 /*!
2  \file cluster/c_begin.c
3 
4  \brief Cluster library - Begin clusterring
5 
6  (C) 2001-2009 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Original author CERL
12 */
13 
14 #include <stdlib.h>
15 #include <grass/glocale.h>
16 #include <grass/cluster.h>
17 
18 /*!
19  \brief Initialize the cluster routines for nbands
20 
21  \param C pointer to Cluster structure
22  \param nbands number of bands
23 
24  \return 0 ok
25  \return -1 out of memory
26  \return 1 illegal number of bands
27 */
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 }
int nbands
Definition: cluster.h:9
double * band_sum
Definition: cluster.h:14
double DCELL
Definition: gis.h:614
struct Signature S
Definition: cluster.h:25
void free(void *)
#define NULL
Definition: ccmath.h:32
Definition: cluster.h:7
char title[100]
Definition: imagery.h:68
int npoints
Definition: cluster.h:10
void * malloc(YYSIZE_T)
DCELL ** points
Definition: cluster.h:11
int np
Definition: cluster.h:12
int I_free_signatures(struct Signature *)
Free memory allocated for struct Signature.
Definition: sig.c:59
#define _(str)
Definition: glocale.h:10
int I_cluster_begin(struct Cluster *C, int nbands)
Initialize the cluster routines for nbands.
Definition: c_begin.c:28
double * band_sum2
Definition: cluster.h:15
int I_init_signatures(struct Signature *, int)
Initialize struct Signature before use.
Definition: sig.c:14