GRASS Programmer's Manual
6.4.2(2012)
|
00001 00002 /**************************************************************** 00003 * I_cluster_point (C,x) 00004 * struct Cluster *C; 00005 * DCELL *x; 00006 * 00007 * adds the point x to the list of data points to be "clustered" 00008 * 00009 * returns 00010 * 0 ok 00011 * -1 out of memory, point not added 00012 * 1 all values are zero, point not added 00013 * 00014 * the dimension of x must agree with the number of bands specified 00015 * in the initializing call to I_cluster_begin() 00016 * 00017 * note: if all values in x are zero, the point is rejected 00018 ***************************************************************/ 00019 00020 #include <grass/cluster.h> 00021 static int extend(struct Cluster *, int); 00022 static int all_zero(struct Cluster *, int); 00023 00024 int I_cluster_point(struct Cluster *C, DCELL * x) 00025 { 00026 int band; 00027 00028 /* reject points which contain nulls in one of the bands */ 00029 for (band = 0; band < C->nbands; band++) 00030 if (G_is_d_null_value(&x[band])) 00031 return 1; /* fixed 11/99 Agus Carr */ 00032 /* 00033 if (band >= C->nbands) 00034 return 1; 00035 */ 00036 00037 /* extend the arrays for each band, if necessary */ 00038 if (!extend(C, 1)) 00039 return -1; 00040 00041 /* add the point to the points arrays */ 00042 for (band = 0; band < C->nbands; band++) { 00043 register double z; 00044 00045 /* if(G_is_d_null_value(&x[band])) continue; */ 00046 z = C->points[band][C->npoints] = x[band]; 00047 C->band_sum[band] += z; 00048 C->band_sum2[band] += z * z; 00049 } 00050 C->npoints++; 00051 return 0; 00052 } 00053 00054 int I_cluster_begin_point_set(struct Cluster *C, int n) 00055 { 00056 return extend(C, n) ? 0 : -1; 00057 } 00058 00059 int I_cluster_point_part(struct Cluster *C, DCELL x, int band, int n) 00060 { 00061 DCELL tmp = x; 00062 00063 if (G_is_d_null_value(&tmp)) 00064 return 1; 00065 C->points[band][C->npoints + n] = x; 00066 C->band_sum[band] += x; 00067 C->band_sum2[band] += x * x; 00068 00069 return 0; 00070 } 00071 00072 int I_cluster_end_point_set(struct Cluster *C, int n) 00073 { 00074 int band; 00075 int cur, next; 00076 00077 cur = C->npoints; 00078 n += C->npoints; 00079 for (next = cur; next < n; next++) { 00080 if (!all_zero(C, next)) { 00081 if (cur != next) 00082 for (band = 0; band < C->nbands; band++) 00083 C->points[band][cur] = C->points[band][next]; 00084 cur++; 00085 } 00086 } 00087 return C->npoints = cur; 00088 } 00089 00090 static int all_zero(struct Cluster *C, int i) 00091 { 00092 int band; 00093 00094 for (band = 0; band < C->nbands; band++) 00095 if (C->points[band][i]) 00096 return 0; 00097 return 1; 00098 } 00099 00100 static int extend(struct Cluster *C, int n) 00101 { 00102 int band; 00103 00104 while ((C->npoints + n) > C->np) { 00105 C->np += 128; 00106 for (band = 0; band < C->nbands; band++) { 00107 C->points[band] = 00108 (DCELL *) I_realloc(C->points[band], C->np * sizeof(DCELL)); 00109 if (C->points[band] == NULL) 00110 return 0; 00111 } 00112 } 00113 return 1; 00114 }