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