GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
c_reassign.c
Go to the documentation of this file.
1 /*!
2  \file cluster/c_reassign.c
3 
4  \brief Cluster library - Reassign 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 ?
19 
20  \param C pointer to Cluster structure
21  \param interrupted
22 
23  \return number of changes
24 */
25 int I_cluster_reassign(struct Cluster *C, int *interrupted)
26 {
27  double min, d, z;
28  double q;
29  int c, np;
30  int old;
31  int p, band, class;
32  int changes;
33  int first;
34 
35  changes = 0;
36  for (c = 0; c < C->nclasses; c++) {
37  C->countdiff[c] = 0;
38  for (band = 0; band < C->nbands; band++)
39  C->sumdiff[band][c] = 0;
40  }
41 
42  min = HUGE_VAL;
43  class = 0;
44  for (p = 0; p < C->npoints; p++) {
45  if (*interrupted)
46  return 0;
47  if (C->class[p] < 0) /* point to be ignored */
48  continue;
49 
50  /* find minimum distance to center of all classes */
51  first = 1;
52  for (c = 0; c < C->nclasses; c++) {
53  d = 0;
54  np = C->count[c];
55  if (np == 0)
56  continue;
57  for (band = 0; band < C->nbands; band++) {
58  z = C->points[band][p] * np - C->sum[band][c];
59  d += z * z;
60  }
61  d /= (np * np);
62 
63  if (first || (d < min)) {
64  class = c;
65  min = d;
66  first = 0;
67  }
68  }
69 
70  if (C->class[p] != class) {
71  old = C->class[p];
72  C->class[p] = class;
73  changes++;
74 
75  C->countdiff[class]++;
76  C->countdiff[old]--;
77 
78  for (band = 0; band < C->nbands; band++) {
79  q = C->points[band][p];
80  C->sumdiff[band][class] += q;
81  C->sumdiff[band][old] -= q;
82  }
83  }
84  }
85 
86  if (changes) {
87  for (c = 0; c < C->nclasses; c++) {
88  C->count[c] += C->countdiff[c];
89  for (band = 0; band < C->nbands; band++)
90  C->sum[band][c] += C->sumdiff[band][c];
91  }
92  }
93 
94  return changes;
95 }
int nbands
Definition: cluster.h:9
double ** sum
Definition: cluster.h:21
int nclasses
Definition: cluster.h:27
#define min(x, y)
Definition: draw2.c:31
Definition: cluster.h:7
int npoints
Definition: cluster.h:10
int * count
Definition: cluster.h:19
double ** sumdiff
Definition: cluster.h:22
int * class
Definition: cluster.h:17
DCELL ** points
Definition: cluster.h:11
#define HUGE_VAL
Values needed for Ray-Convex Polyhedron Intersection Test below originally by Eric Haines...
Definition: gs_query.c:29
int * countdiff
Definition: cluster.h:20
int I_cluster_reassign(struct Cluster *C, int *interrupted)
?
Definition: c_reassign.c:25