GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_point.c
Go to the documentation of this file.
1 
2 /****************************************************************
3  * I_cluster_point (C,x)
4  * struct Cluster *C;
5  * DCELL *x;
6  *
7  * adds the point x to the list of data points to be "clustered"
8  *
9  * returns
10  * 0 ok
11  * -1 out of memory, point not added
12  * 1 all values are zero, point not added
13  *
14  * the dimension of x must agree with the number of bands specified
15  * in the initializing call to I_cluster_begin()
16  *
17  * note: if all values in x are zero, the point is rejected
18  ***************************************************************/
19 
20 #include <grass/cluster.h>
21 static int extend(struct Cluster *, int);
22 static int all_zero(struct Cluster *, int);
23 
24 int I_cluster_point(struct Cluster *C, DCELL * x)
25 {
26  int band;
27 
28  /* reject points which contain nulls in one of the bands */
29  for (band = 0; band < C->nbands; band++)
30  if (G_is_d_null_value(&x[band]))
31  return 1; /* fixed 11/99 Agus Carr */
32  /*
33  if (band >= C->nbands)
34  return 1;
35  */
36 
37  /* extend the arrays for each band, if necessary */
38  if (!extend(C, 1))
39  return -1;
40 
41  /* add the point to the points arrays */
42  for (band = 0; band < C->nbands; band++) {
43  register double z;
44 
45  /* if(G_is_d_null_value(&x[band])) continue; */
46  z = C->points[band][C->npoints] = x[band];
47  C->band_sum[band] += z;
48  C->band_sum2[band] += z * z;
49  }
50  C->npoints++;
51  return 0;
52 }
53 
54 int I_cluster_begin_point_set(struct Cluster *C, int n)
55 {
56  return extend(C, n) ? 0 : -1;
57 }
58 
59 int I_cluster_point_part(struct Cluster *C, DCELL x, int band, int n)
60 {
61  DCELL tmp = x;
62 
63  if (G_is_d_null_value(&tmp))
64  return 1;
65  C->points[band][C->npoints + n] = x;
66  C->band_sum[band] += x;
67  C->band_sum2[band] += x * x;
68 
69  return 0;
70 }
71 
72 int I_cluster_end_point_set(struct Cluster *C, int n)
73 {
74  int band;
75  int cur, next;
76 
77  cur = C->npoints;
78  n += C->npoints;
79  for (next = cur; next < n; next++) {
80  if (!all_zero(C, next)) {
81  if (cur != next)
82  for (band = 0; band < C->nbands; band++)
83  C->points[band][cur] = C->points[band][next];
84  cur++;
85  }
86  }
87  return C->npoints = cur;
88 }
89 
90 static int all_zero(struct Cluster *C, int i)
91 {
92  int band;
93 
94  for (band = 0; band < C->nbands; band++)
95  if (C->points[band][i])
96  return 0;
97  return 1;
98 }
99 
100 static int extend(struct Cluster *C, int n)
101 {
102  int band;
103 
104  while ((C->npoints + n) > C->np) {
105  C->np += 128;
106  for (band = 0; band < C->nbands; band++) {
107  C->points[band] =
108  (DCELL *) I_realloc(C->points[band], C->np * sizeof(DCELL));
109  if (C->points[band] == NULL)
110  return 0;
111  }
112  }
113  return 1;
114 }
#define C
Definition: intr_char.c:17
int G_is_d_null_value(const DCELL *dcellVal)
Returns 1 if dcell is NULL, 0 otherwise. This will test if the value dcell is a NaN. Same test as in G_is_f_null_value().
Definition: null_val.c:306
int I_cluster_begin_point_set(struct Cluster *C, int n)
Definition: c_point.c:54
int I_cluster_point_part(struct Cluster *C, DCELL x, int band, int n)
Definition: c_point.c:59
return NULL
Definition: dbfopen.c:1394
void * I_realloc(void *b, size_t n)
Definition: imagery/alloc.c:16
int I_cluster_end_point_set(struct Cluster *C, int n)
Definition: c_point.c:72
int n
Definition: dataquad.c:291
int I_cluster_point(struct Cluster *C, DCELL *x)
Definition: c_point.c:24