GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_reassign.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <grass/cluster.h>
3 
4 int I_cluster_reassign(struct Cluster *C, int *interrupted)
5 {
6  double min, d, z;
7  double q;
8  int c, np;
9  int old;
10  int p, band, class;
11  int changes;
12  int first;
13 
14  changes = 0;
15  for (c = 0; c < C->nclasses; c++) {
16  C->countdiff[c] = 0;
17  for (band = 0; band < C->nbands; band++)
18  C->sumdiff[band][c] = 0;
19  }
20 
21  min = HUGE_VAL;
22  class = 0;
23  for (p = 0; p < C->npoints; p++) {
24  if (*interrupted)
25  return 0;
26  if (C->class[p] < 0) /* point to be ignored */
27  continue;
28 
29  /* find minimum distance to center of all classes */
30  first = 1;
31  for (c = 0; c < C->nclasses; c++) {
32  d = 0;
33  np = C->count[c];
34  if (np == 0)
35  continue;
36  for (band = 0; band < C->nbands; band++) {
37  z = C->points[band][p] * np - C->sum[band][c];
38  d += z * z;
39  }
40  d /= (np * np);
41 
42  if (first || (d < min)) {
43  class = c;
44  min = d;
45  first = 0;
46  }
47  }
48 
49  if (C->class[p] != class) {
50  old = C->class[p];
51  C->class[p] = class;
52  changes++;
53 
54  C->countdiff[class]++;
55  C->countdiff[old]--;
56 
57  for (band = 0; band < C->nbands; band++) {
58  q = C->points[band][p];
59  C->sumdiff[band][class] += q;
60  C->sumdiff[band][old] -= q;
61  }
62  }
63  }
64 
65  if (changes) {
66  for (c = 0; c < C->nclasses; c++) {
67  C->count[c] += C->countdiff[c];
68  for (band = 0; band < C->nbands; band++)
69  C->sum[band][c] += C->sumdiff[band][c];
70  }
71  }
72 
73  return changes;
74 }
tuple q
Definition: forms.py:2019
#define min(x, y)
Definition: draw2.c:68
#define C
Definition: intr_char.c:17
int first
Definition: form/open.c:25
#define HUGE_VAL
Values needed for Ray-Convex Polyhedron Intersection Test below originally by Eric Haines...
Definition: gs_query.c:29
int old
Definition: g3dcats.c:92
int I_cluster_reassign(struct Cluster *C, int *interrupted)
Definition: c_reassign.c:4