GRASS Programmer's Manual  6.4.2(2012)
c_begin.c
Go to the documentation of this file.
00001 #include <stdlib.h>
00002 #include <grass/cluster.h>
00003 
00004 /****************************************************************
00005  * I_cluster_begin (C,nbands)
00006  *
00007  * initialize the cluster routines for nbands
00008  *
00009  * returns 
00010  *  0 ok
00011  * -1 out of memory
00012  *  1 illegal number of bands
00013  *
00014  ***************************************************************/
00015 
00016 int I_cluster_begin(struct Cluster *C, int nbands)
00017 {
00018     int band;
00019 
00020     if (C->points != NULL) {
00021         for (band = 0; band < C->nbands; band++)
00022             if (C->points[band] != NULL)
00023                 free(C->points[band]);
00024         free(C->points);
00025     }
00026     if (C->band_sum != NULL)
00027         free(C->band_sum);
00028     if (C->band_sum2 != NULL)
00029         free(C->band_sum2);
00030 
00031     C->points = NULL;
00032     C->band_sum = NULL;
00033     C->band_sum2 = NULL;
00034 
00035     I_free_signatures(&C->S);
00036 
00037     /* record the number of bands */
00038     C->nbands = nbands;
00039     if (nbands <= 0)
00040         return 1;
00041 
00042     /* prepare the signatures for nbands */
00043 
00044     I_init_signatures(&C->S, nbands);
00045     sprintf(C->S.title, "produced by i.cluster");
00046 
00047     /* allocate the data (points) arrays */
00048     C->points = (DCELL **) malloc(C->nbands * sizeof(DCELL *));
00049     if (C->points == NULL)
00050         return -1;
00051     for (band = 0; band < C->nbands; band++)
00052         C->points[band] = NULL;
00053 
00054     C->np = 128;
00055     for (band = 0; band < C->nbands; band++) {
00056         C->points[band] = (DCELL *) malloc(C->np * sizeof(DCELL));
00057         if (C->points[band] == NULL)
00058             return -1;
00059     }
00060 
00061     /* initialize the count to zero */
00062     C->npoints = 0;
00063 
00064     /* allocate the band sums and means */
00065     C->band_sum = (double *)malloc(C->nbands * sizeof(double));
00066     if (C->band_sum == NULL)
00067         return -1;
00068     C->band_sum2 = (double *)malloc(C->nbands * sizeof(double));
00069     if (C->band_sum2 == NULL)
00070         return -1;
00071     for (band = 0; band < C->nbands; band++) {
00072         C->band_sum[band] = 0;
00073         C->band_sum2[band] = 0;
00074     }
00075 
00076     return 0;
00077 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines