GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-602118adcc
c_assign.c
Go to the documentation of this file.
1 /*!
2  \file cluster/c_assign.c
3 
4  \brief Cluster library - Assign cluster
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 <math.h>
15 #include <grass/cluster.h>
16 
17 /*!
18  \brief Assign cluster
19 
20  \param C pointer to Cluster structure
21  \param interrupted ?
22 
23  \return -1 on interrupted
24  \return 0 on success
25  */
26 int I_cluster_assign(struct Cluster *C, int *interrupted)
27 {
28  int p, c;
29  int class, band;
30  double d, q;
31  double dmin;
32 
33  G_debug(3, "I_cluster_assign(npoints=%d,nclasses=%d,nbands=%d)", C->npoints,
34  C->nclasses, C->nbands);
35 
36  for (p = 0; p < C->npoints; p++) {
37  if (*interrupted)
38  return -1;
39 
40  dmin = HUGE_VAL;
41  class = 0;
42  for (c = 0; c < C->nclasses; c++) {
43  d = 0.0;
44  for (band = 0; band < C->nbands; band++) {
45  q = C->points[band][p];
46  q -= C->mean[band][c];
47  d += q * q;
48  }
49  if (c == 0 || d < dmin) {
50  class = c;
51  dmin = d;
52  }
53  }
54  C->class[p] = class;
55  C->count[class]++;
56  for (band = 0; band < C->nbands; band++)
57  C->sum[band][class] += C->points[band][p];
58  }
59 
60  return 0;
61 }
int I_cluster_assign(struct Cluster *C, int *interrupted)
Assign cluster.
Definition: c_assign.c:26
int G_debug(int, const char *,...) __attribute__((format(printf
#define HUGE_VAL
Values needed for Ray-Convex Polyhedron Intersection Test below originally by Eric Haines,...
Definition: gs_query.c:29
Definition: cluster.h:7
int * count
Definition: cluster.h:18
int npoints
Definition: cluster.h:9
int * class
Definition: cluster.h:16
int nclasses
Definition: cluster.h:26
double ** mean
Definition: cluster.h:23
DCELL ** points
Definition: cluster.h:10
double ** sum
Definition: cluster.h:20
int nbands
Definition: cluster.h:8