GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_begin.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <grass/cluster.h>
3 
4 /****************************************************************
5  * I_cluster_begin (C,nbands)
6  *
7  * initialize the cluster routines for nbands
8  *
9  * returns
10  * 0 ok
11  * -1 out of memory
12  * 1 illegal number of bands
13  *
14  ***************************************************************/
15 
16 int I_cluster_begin(struct Cluster *C, int nbands)
17 {
18  int band;
19 
20  if (C->points != NULL) {
21  for (band = 0; band < C->nbands; band++)
22  if (C->points[band] != NULL)
23  free(C->points[band]);
24  free(C->points);
25  }
26  if (C->band_sum != NULL)
27  free(C->band_sum);
28  if (C->band_sum2 != NULL)
29  free(C->band_sum2);
30 
31  C->points = NULL;
32  C->band_sum = NULL;
33  C->band_sum2 = NULL;
34 
35  I_free_signatures(&C->S);
36 
37  /* record the number of bands */
38  C->nbands = nbands;
39  if (nbands <= 0)
40  return 1;
41 
42  /* prepare the signatures for nbands */
43 
44  I_init_signatures(&C->S, nbands);
45  sprintf(C->S.title, "produced by i.cluster");
46 
47  /* allocate the data (points) arrays */
48  C->points = (DCELL **) malloc(C->nbands * sizeof(DCELL *));
49  if (C->points == NULL)
50  return -1;
51  for (band = 0; band < C->nbands; band++)
52  C->points[band] = NULL;
53 
54  C->np = 128;
55  for (band = 0; band < C->nbands; band++) {
56  C->points[band] = (DCELL *) malloc(C->np * sizeof(DCELL));
57  if (C->points[band] == NULL)
58  return -1;
59  }
60 
61  /* initialize the count to zero */
62  C->npoints = 0;
63 
64  /* allocate the band sums and means */
65  C->band_sum = (double *)malloc(C->nbands * sizeof(double));
66  if (C->band_sum == NULL)
67  return -1;
68  C->band_sum2 = (double *)malloc(C->nbands * sizeof(double));
69  if (C->band_sum2 == NULL)
70  return -1;
71  for (band = 0; band < C->nbands; band++) {
72  C->band_sum[band] = 0;
73  C->band_sum2[band] = 0;
74  }
75 
76  return 0;
77 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
#define C
Definition: intr_char.c:17
void * malloc(YYSIZE_T)
int I_init_signatures(struct Signature *S, int nbands)
Definition: sig.c:4
int I_free_signatures(struct Signature *S)
Definition: sig.c:36
return NULL
Definition: dbfopen.c:1394
void free(void *)
for(cat=0;;cat++)
Definition: g3dcats.c:140
int I_cluster_begin(struct Cluster *C, int nbands)
Definition: c_begin.c:16