2 * $Header: /cvsroot/pgsql/src/test/regress/regress.c,v 1.4 1996/12/09 01:24:53 bryanh Exp $
5 #include <float.h> /* faked on sunos */
7 #include <string.h> /* for memset() */
11 #include "utils/geo-decls.h" /* includes <math.h> */
12 #include "executor/executor.h" /* For GetAttributeByName */
21 extern double *regress_dist_ptpath (Point *pt, PATH *path);
22 extern double *regress_path_dist (PATH *p1, PATH *p2);
23 extern PATH *poly2path (POLYGON *poly);
24 extern Point *interpt_pp (PATH *p1, PATH *p2);
25 extern void regress_lseg_construct (LSEG *lseg, Point *pt1, Point *pt2);
26 extern char overpaid (TUPLE tuple);
27 extern int boxarea (BOX *box);
28 extern char *reverse_c16 (char *string);
31 ** Distance from a point to a path
34 regress_dist_ptpath(pt, path)
45 result = PALLOCTYPE(double);
46 *result = Abs((double) DBL_MAX); /* +infinity */
49 result = point_distance(pt, &path->p[0]);
53 * the distance from a point to a path is the smallest distance
54 * from the point to any of its constituent segments.
56 Assert(path->npts > 1);
57 result = PALLOCTYPE(double);
58 for (i = 0; i < path->npts - 1; ++i) {
59 regress_lseg_construct(&lseg, &path->p[i], &path->p[i+1]);
60 tmp = dist_ps(pt, &lseg);
61 if (i == 0 || *tmp < *result)
71 /* this essentially does a cartesian product of the lsegs in the
72 two paths, and finds the min distance between any two lsegs */
74 regress_path_dist(p1, p2)
82 regress_lseg_construct(&seg1, &p1->p[0], &p1->p[1]);
83 regress_lseg_construct(&seg2, &p2->p[0], &p2->p[1]);
84 min = lseg_distance(&seg1, &seg2);
86 for (i = 0; i < p1->npts - 1; i++)
87 for (j = 0; j < p2->npts - 1; j++)
89 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i+1]);
90 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j+1]);
92 if (*min < *(tmp = lseg_distance(&seg1, &seg2)))
105 char *output = (char *)PALLOC(2*(P_MAXDIG + 1)*poly->npts + 64);
106 char *outptr = output;
109 sprintf(outptr, "(1, %*d", P_MAXDIG, poly->npts);
110 xp = (double *) poly->pts;
111 yp = (double *) (poly->pts + (poly->npts * sizeof(double *)));
113 for (i=1; i<poly->npts; i++,xp++,yp++)
115 sprintf(outptr, ",%*g,%*g", P_MAXDIG, *xp, P_MAXDIG, *yp);
116 outptr += 2*(P_MAXDIG + 1);
121 return(path_in(outptr));
124 /* return the point where two paths intersect. Assumes that they do. */
135 bool found; /* We've found the intersection */
137 found = false; /* Haven't found it yet */
139 for (i = 0; i < p1->npts - 1 && !found; i++)
140 for (j = 0; j < p2->npts - 1 && !found; j++)
142 regress_lseg_construct(&seg1, &p1->p[i], &p1->p[i+1]);
143 regress_lseg_construct(&seg2, &p2->p[j], &p2->p[j+1]);
144 if (lseg_intersect(&seg1, &seg2)) found = true;
147 ln = line_construct_pp(&seg2.p[0], &seg2.p[1]);
148 retval = interpt_sl(&seg1, ln);
154 /* like lseg_construct, but assume space already allocated */
156 regress_lseg_construct(lseg, pt1, pt2)
161 lseg->p[0].x = pt1->x;
162 lseg->p[0].y = pt1->y;
163 lseg->p[1].x = pt2->x;
164 lseg->p[1].y = pt2->y;
165 lseg->m = point_sl(pt1, pt2);
175 salary = (long)GetAttributeByName(tuple, "salary", &isnull);
176 return(salary > 699);
184 extern CIRCLE *circle_in (char *str);
185 extern char *circle_out (CIRCLE *circle);
186 extern int pt_in_circle (Point *point, CIRCLE *circle);
194 char *p, *coord[NARGS], buf2[1000];
200 for (i = 0, p = str; *p && i < NARGS && *p != RDELIM; p++)
201 if (*p == ',' || (*p == LDELIM && !i))
205 result = (CIRCLE *) palloc(sizeof(CIRCLE));
206 result->center.x = atof(coord[0]);
207 result->center.y = atof(coord[1]);
208 result->radius = atof(coord[2]);
210 sprintf(buf2, "circle_in: read (%f, %f, %f)\n", result->center.x,
211 result->center.y,result->radius);
224 result = (char *) palloc(60);
225 (void) sprintf(result, "(%g,%g,%g)",
226 circle->center.x, circle->center.y, circle->radius);
231 pt_in_circle(point, circle)
235 extern double point_dt();
237 return( point_dt(point, &circle->center) < circle->radius );
240 #define ABS(X) ((X) > 0 ? (X) : -(X))
250 width = ABS(box->xh - box->xl);
251 height = ABS(box->yh - box->yl);
252 return (width * height);
263 if (!(new_string = palloc(16))) {
264 fprintf(stderr, "reverse_c16: palloc failed\n");
267 memset(new_string, 0, 16);
268 for (i = 0; i < 16 && string[i]; ++i)
270 if (i == 16 || !string[i])
274 new_string[len-i] = string[i];