GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-bea8435a9e
c_point.c
Go to the documentation of this file.
1 /*!
2  \file cluster/c_point.c
3 
4  \brief Cluster library - Add point
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 <grass/raster.h>
15 #include <grass/cluster.h>
16 
17 static int extend(struct Cluster *, int);
18 static int all_zero(struct Cluster *, int);
19 
20 /*!
21  \brief Adds the point x to the list of data points to be "clustered"
22 
23  The dimension of x must agree with the number of bands specified
24  in the initializing call to I_cluster_begin()
25 
26  Note: if all values in x are zero, the point is rejected
27 
28  \return 0 ok
29  \return -1 out of memory, point not added
30  \return 1 all values are null, point not added
31  */
32 int I_cluster_point(struct Cluster *C, DCELL *x)
33 {
34  int band;
35 
36  /* reject points which contain nulls in one of the bands */
37  for (band = 0; band < C->nbands; band++)
38  if (Rast_is_d_null_value(&x[band]))
39  return 1; /* fixed 11/99 Agus Carr */
40  /*
41  if (band >= C->nbands)
42  return 1;
43  */
44 
45  /* extend the arrays for each band, if necessary */
46  if (!extend(C, 1))
47  return -1;
48 
49  /* add the point to the points arrays */
50  for (band = 0; band < C->nbands; band++) {
51  register double z;
52 
53  /* if(Rast_is_d_null_value(&x[band])) continue; */
54  z = C->points[band][C->npoints] = x[band];
55  C->band_sum[band] += z;
56  C->band_sum2[band] += z * z;
57  }
58  C->npoints++;
59  return 0;
60 }
61 
62 /*!
63  \brief Begin point set
64 
65  \param C pointer to Cluster structure
66  \param n ?
67 
68  \return 0 on success
69  \return -1 on error
70  */
71 int I_cluster_begin_point_set(struct Cluster *C, int n)
72 {
73  return extend(C, n) ? 0 : -1;
74 }
75 
76 /*!
77  \brief ?
78 
79  \param C pointer to Cluster structure
80  \param x cell value
81  \param band band number
82  \param n ?
83 
84  \return 0 ok
85  \return -1 out of memory, point not added
86  \return 1 all values are null, point not added
87  */
88 int I_cluster_point_part(struct Cluster *C, DCELL x, int band, int n)
89 {
90  DCELL tmp = x;
91 
92  if (Rast_is_d_null_value(&tmp))
93  return 1;
94  C->points[band][C->npoints + n] = x;
95  C->band_sum[band] += x;
96  C->band_sum2[band] += x * x;
97 
98  return 0;
99 }
100 
101 /*!
102  \brief ?
103 
104  \param C pointer to Cluster structure
105  \param n ?
106 
107  \return number of points
108  */
109 int I_cluster_end_point_set(struct Cluster *C, int n)
110 {
111  int band;
112  int cur, next;
113 
114  cur = C->npoints;
115  n += C->npoints;
116  for (next = cur; next < n; next++) {
117  if (!all_zero(C, next)) {
118  if (cur != next)
119  for (band = 0; band < C->nbands; band++)
120  C->points[band][cur] = C->points[band][next];
121  cur++;
122  }
123  }
124  return C->npoints = cur;
125 }
126 
127 static int all_zero(struct Cluster *C, int i)
128 {
129  int band;
130 
131  for (band = 0; band < C->nbands; band++)
132  if (C->points[band][i])
133  return 0;
134  return 1;
135 }
136 
137 static int extend(struct Cluster *C, int n)
138 {
139  int band;
140 
141  while ((C->npoints + n) > C->np) {
142  C->np += 128;
143  for (band = 0; band < C->nbands; band++) {
144  C->points[band] =
145  (DCELL *)I_realloc(C->points[band], C->np * sizeof(DCELL));
146  if (C->points[band] == NULL)
147  return 0;
148  }
149  }
150  return 1;
151 }
int I_cluster_end_point_set(struct Cluster *C, int n)
?
Definition: c_point.c:109
int I_cluster_point_part(struct Cluster *C, DCELL x, int band, int n)
?
Definition: c_point.c:88
int I_cluster_begin_point_set(struct Cluster *C, int n)
Begin point set.
Definition: c_point.c:71
int I_cluster_point(struct Cluster *C, DCELL *x)
Adds the point x to the list of data points to be "clustered".
Definition: c_point.c:32
#define NULL
Definition: ccmath.h:32
void * I_realloc(void *, size_t)
Definition: imagery/alloc.c:14
#define Rast_is_d_null_value(dcellVal)
Definition: defs/raster.h:408
double DCELL
Definition: gis.h:629
Definition: cluster.h:7
int npoints
Definition: cluster.h:9
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
#define x