GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 */
11
12#include <stdlib.h>
13#include <grass/glocale.h>
14#include <grass/cluster.h>
15
16/*!
17 \brief Initialize the cluster routines for nbands
18
19 \param C pointer to Cluster structure
20 \param nbands number of bands
21
22 \return 0 ok
23 \return -1 out of memory
24 \return 1 illegal number of bands
25 */
26int I_cluster_begin(struct Cluster *C, int nbands)
27{
28 int band;
29
30 if (C->points != NULL) {
31 for (band = 0; band < C->nbands; band++)
32 if (C->points[band] != NULL)
33 free(C->points[band]);
34 free(C->points);
35 }
36 if (C->band_sum != NULL)
37 free(C->band_sum);
38 if (C->band_sum2 != NULL)
39 free(C->band_sum2);
40
41 C->points = NULL;
42 C->band_sum = NULL;
43 C->band_sum2 = NULL;
44
46
47 /* record the number of bands */
48 C->nbands = nbands;
49 if (nbands <= 0)
50 return 1;
51
52 /* prepare the signatures for nbands */
53
54 I_init_signatures(&C->S, nbands);
55 snprintf(C->S.title, sizeof(C->S.title), _("produced by i.cluster"));
56
57 /* allocate the data (points) arrays */
58 C->points = (DCELL **)malloc(C->nbands * sizeof(DCELL *));
59 if (C->points == NULL)
60 return -1;
61 for (band = 0; band < C->nbands; band++)
62 C->points[band] = NULL;
63
64 C->np = 128;
65 for (band = 0; band < C->nbands; band++) {
66 C->points[band] = (DCELL *)malloc(C->np * sizeof(DCELL));
67 if (C->points[band] == NULL)
68 return -1;
69 }
70
71 /* initialize the count to zero */
72 C->npoints = 0;
73
74 /* allocate the band sums and means */
75 C->band_sum = (double *)malloc(C->nbands * sizeof(double));
76 if (C->band_sum == NULL)
77 return -1;
78 C->band_sum2 = (double *)malloc(C->nbands * sizeof(double));
79 if (C->band_sum2 == NULL)
80 return -1;
81 for (band = 0; band < C->nbands; band++) {
82 C->band_sum[band] = 0;
83 C->band_sum2[band] = 0;
84 }
85
86 return 0;
87}
int I_cluster_begin(struct Cluster *C, int nbands)
Initialize the cluster routines for nbands.
Definition c_begin.c:26
#define NULL
Definition ccmath.h:32
int I_init_signatures(struct Signature *, int)
Initialize struct Signature before use.
Definition sig.c:14
int I_free_signatures(struct Signature *)
Free memory allocated for struct Signature.
Definition sig.c:60
double DCELL
Definition gis.h:632
#define _(str)
Definition glocale.h:10
void * malloc(unsigned)
void free(void *)
int npoints
Definition cluster.h:9
struct Signature S
Definition cluster.h:24
double * band_sum2
Definition cluster.h:14
DCELL ** points
Definition cluster.h:10
int np
Definition cluster.h:11
double * band_sum
Definition cluster.h:13
int nbands
Definition cluster.h:8