1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/*-
* Written by H. Mitasova, I. Kosinovsky, D. Gerdes Fall 1993
* University of Illinois
* US Army Construction Engineering Research Lab
* Copyright 1993, H. Mitasova (University of Illinois),
* I. Kosinovsky, (USA-CERL), and D.Gerdes (USA-CERL)
*
* Modified by H.Mitasova November 1996 to include variable smoothing
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <grass/dataquad.h>
/* sm added to point structure */
struct triple *quad_point_new(double x, double y, double z, double sm)
/* Initializes POINT structure with given arguments */
{
struct triple *point;
if (!(point = (struct triple *)malloc(sizeof(struct triple)))) {
return NULL;
}
point->x = x;
point->y = y;
point->z = z;
point->sm = sm;
return point;
}
struct quaddata *quad_data_new(double x_or, double y_or, double xmax,
double ymax, int rows, int cols, int n_points,
int kmax)
/* Initializes QUADDATA structure with given arguments */
{
struct quaddata *data;
int i;
if (!(data = (struct quaddata *)malloc(sizeof(struct quaddata)))) {
return NULL;
}
data->x_orig = x_or;
data->y_orig = y_or;
data->xmax = xmax;
data->ymax = ymax;
data->n_rows = rows;
data->n_cols = cols;
data->n_points = n_points;
data->points =
(struct triple *)malloc(sizeof(struct triple) * (kmax + 1));
if (!data->points)
return NULL;
for (i = 0; i <= kmax; i++) {
data->points[i].x = 0.;
data->points[i].y = 0.;
data->points[i].z = 0.;
data->points[i].sm = 0.;
}
return data;
}
int quad_compare(struct triple *point, struct quaddata *data)
/* returns the quadrant the point should be inserted in */
/* called by divide() */
{
int cond1, cond2, cond3, cond4, rows, cols;
double ew_res, ns_res;
ew_res = (data->xmax - data->x_orig) / data->n_cols;
ns_res = (data->ymax - data->y_orig) / data->n_rows;
if (data == NULL)
return -1;
if (data->n_rows % 2 == 0) {
rows = data->n_rows / 2;
}
else {
rows = (int)(data->n_rows / 2) + 1;
}
if (data->n_cols % 2 == 0) {
cols = data->n_cols / 2;
}
else {
cols = (int)(data->n_cols / 2) + 1;
}
cond1 = (point->x >= data->x_orig);
cond2 = (point->x >= data->x_orig + ew_res * cols);
cond3 = (point->y >= data->y_orig);
cond4 = (point->y >= data->y_orig + ns_res * rows);
if (cond1 && cond3) {
if (cond2 && cond4)
return NE;
if (cond2)
return SE;
if (cond4)
return NW;
return SW;
}
else
return 0;
}
int quad_add_data(struct triple *point, struct quaddata *data, double dmin)
/* Adds POINT to a given DATA . Called by tree function insert_quad() */
/* and by data function quad_divide_data() */
{
int n, i, cond;
double xx, yy, r;
cond = 1;
if (data == NULL) {
fprintf(stderr, "add_data: data is NULL \n");
return -5;
}
for (i = 0; i < data->n_points; i++) {
xx = data->points[i].x - point->x;
yy = data->points[i].y - point->y;
r = xx * xx + yy * yy;
if (r <= dmin) {
cond = 0;
break;
}
}
if (cond) {
n = (data->n_points)++;
data->points[n].x = point->x;
data->points[n].y = point->y;
data->points[n].z = point->z;
data->points[n].sm = point->sm;
}
return cond;
}
int quad_intersect(struct quaddata *data_inter, struct quaddata *data)
/* Checks if region defined by DATA intersects the region defined
by data_inter. Called by tree function MT_region_data() */
{
double xmin, xmax, ymin, ymax;
xmin = data_inter->x_orig;
xmax = data_inter->xmax;
ymin = data_inter->y_orig;
ymax = data_inter->ymax;
if (((data->x_orig >= xmin) && (data->x_orig <= xmax)
&& (((data->y_orig >= ymin) && (data->y_orig <= ymax))
|| ((ymin >= data->y_orig) && (ymin <= data->ymax))
)
)
|| ((xmin >= data->x_orig) && (xmin <= data->xmax)
&& (((ymin >= data->y_orig) && (ymin <= data->ymax))
|| ((data->y_orig >= ymin) && (data->y_orig <= ymax))
)
)
) {
return 1;
}
else
return 0;
}
int quad_division_check(struct quaddata *data, int kmax)
/* Checks if DATA needs to be divided. If data->points is empty,
returns -1; if its not empty but there aren't enough points
in DATA for division returns 0. Othervise (if its not empty and
there are too many points) returns 1. Called by MT_insert() */
{
if (data->points == NULL)
return -1;
if (data->n_points < kmax)
return 0;
else
return 1;
}
struct quaddata **quad_divide_data(struct quaddata *data, int kmax,
double dmin)
/* Divides DATA into 4 new datas reinserting data->points in
them by calling data function quad_compare() to detrmine
were to insert. Called by MT_divide(). Returns array of 4 new datas */
{
struct quaddata **datas;
int cols1, cols2, rows1, rows2, i; /*j1, j2, jmin = 0; */
double dx, dy; /* x2, y2, dist, mindist; */
double xr, xm, xl, yr, ym, yl; /* left, right, middle coord */
double ew_res, ns_res;
ew_res = (data->xmax - data->x_orig) / data->n_cols;
ns_res = (data->ymax - data->y_orig) / data->n_rows;
if ((data->n_cols <= 1) || (data->n_rows <= 1)) {
fprintf(stderr,
"Points are too concentrated -- please increase DMIN\n");
exit(0);
}
if (data->n_cols % 2 == 0) {
cols1 = data->n_cols / 2;
cols2 = cols1;
}
else {
cols2 = (int)(data->n_cols / 2);
cols1 = cols2 + 1;
}
if (data->n_rows % 2 == 0) {
rows1 = data->n_rows / 2;
rows2 = rows1;
}
else {
rows2 = (int)(data->n_rows / 2);
rows1 = rows2 + 1;
}
dx = cols1 * ew_res;
dy = rows1 * ns_res;
xl = data->x_orig;
xm = xl + dx;
xr = data->xmax;
yl = data->y_orig;
ym = yl + dy;
yr = data->ymax;
if (!(datas = (struct quaddata **)malloc(sizeof(struct quaddata *) * 5))) {
return NULL;
}
datas[NE] = quad_data_new(xm, ym, xr, yr, rows2, cols2, 0, kmax);
datas[SW] = quad_data_new(xl, yl, xm, ym, rows1, cols1, 0, kmax);
datas[SE] = quad_data_new(xm, yl, xr, ym, rows1, cols2, 0, kmax);
datas[NW] = quad_data_new(xl, ym, xm, yr, rows2, cols1, 0, kmax);
for (i = 0; i < data->n_points; i++) {
switch (quad_compare(data->points + i, data)) {
case SW:
{
quad_add_data(data->points + i, datas[SW], dmin);
break;
}
case SE:
{
quad_add_data(data->points + i, datas[SE], dmin);
break;
}
case NW:
{
quad_add_data(data->points + i, datas[NW], dmin);
break;
}
case NE:
{
quad_add_data(data->points + i, datas[NE], dmin);
break;
}
}
}
data->points = NULL;
return datas;
}
int
quad_get_points(struct quaddata *data_inter, struct quaddata *data, int MAX)
/* Gets such points from DATA that lie within region determined by
data_inter. Called by tree function region_data(). */
{
int i, ind;
int n = 0;
int l = 0;
double xmin, xmax, ymin, ymax;
struct triple *point;
xmin = data_inter->x_orig;
xmax = data_inter->xmax;
ymin = data_inter->y_orig;
ymax = data_inter->ymax;
for (i = 0; i < data->n_points; i++) {
point = data->points + i;
if (l >= MAX)
return MAX + 1;
if ((point->x > xmin) && (point->x < xmax)
&& (point->y > ymin) && (point->y < ymax)) {
ind = data_inter->n_points++;
data_inter->points[ind].x = point->x;
data_inter->points[ind].y = point->y;
data_inter->points[ind].z = point->z;
data_inter->points[ind].sm = point->sm;
l = l + 1;
}
}
n = l;
return (n);
}
|