*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.4 1997/04/25 18:40:25 scrappy Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.5 1997/05/06 07:27:51 thomas Exp $
*
*-------------------------------------------------------------------------
*/
char *path_encode( bool closed, int npts, Point *pt)
{
- char *result;
+ char *result = PALLOC(npts*(P_MAXLEN+3)+2);
char *cp;
int i;
- if (!PointerIsValid(result = (char *)PALLOC(npts*(P_MAXLEN+3)+2)))
- elog(WARN, "Memory allocation failed, can't output path", NULL);
-
cp = result;
switch (closed) {
case TRUE:
*/
BOX *box_in(char *str)
{
- BOX *box;
+ BOX *box = PALLOCTYPE(BOX);
int isopen;
char *s;
if (!PointerIsValid((char *)str))
elog (WARN," Bad (null) box external representation",NULL);
- if (!PointerIsValid(box = PALLOCTYPE(BOX)))
- elog(WARN, "Memory allocation failed, can't input box '%s'",str);
-
if ((! path_decode(FALSE, 2, str, &isopen, &s, &(box->high)))
|| (*s != '\0'))
elog (WARN, "Bad box external representation '%s'",str);
char *box_out(BOX *box)
{
#if OLD_FORMAT_OUT
- char *result;
+ char *result = PALLOC(2*(P_MAXLEN+1)+2);
char *cp;
#endif
return(NULL);
#if OLD_FORMAT_OUT
- if (!PointerIsValid(result = (char *)PALLOC(2*(P_MAXLEN+1)+2)))
- elog(WARN, "Memory allocation failed, can't output box", NULL);
-
cp = result;
*cp++ = LDELIM;
if (! pair_encode( box->high.x, box->high.y, cp))
*/
BOX *box_construct(double x1, double x2, double y1, double y2)
{
- BOX *result;
-
- result = PALLOCTYPE(BOX);
+ BOX *result = PALLOCTYPE(BOX);
+
return( box_fill(result, x1, x2, y1, y2) );
}
*/
BOX *box_copy(BOX *box)
{
- BOX *result;
-
- result = PALLOCTYPE(BOX);
+ BOX *result = PALLOCTYPE(BOX);
+
memmove((char *) result, (char *) box, sizeof(BOX));
return(result);
*/
double *box_area(BOX *box)
{
- double *result;
-
- result = PALLOCTYPE(double);
+ double *result = PALLOCTYPE(double);
+
*result = box_ln(box) * box_ht(box);
return(result);
*/
double *box_length(BOX *box)
{
- double *result;
-
- result = PALLOCTYPE(double);
+ double *result = PALLOCTYPE(double);
+
*result = box->high.x - box->low.x;
return(result);
*/
double *box_height(BOX *box)
{
- double *result;
-
- result = PALLOCTYPE(double);
+ double *result = PALLOCTYPE(double);
+
*result = box->high.y - box->low.y;
return(result);
*/
double *box_distance(BOX *box1, BOX *box2)
{
- double *result;
- Point *a, *b;
+ double *result = PALLOCTYPE(double);
+ Point *a, *b;
- result = PALLOCTYPE(double);
a = box_center(box1);
b = box_center(box2);
*result = HYPOT(a->x - b->x, a->y - b->y);
*/
Point *box_center(BOX *box)
{
- Point *result;
-
- result = PALLOCTYPE(Point);
+ Point *result = PALLOCTYPE(Point);
+
result->x = (box->high.x + box->low.x) / 2.0;
result->y = (box->high.y + box->low.y) / 2.0;
if (! box_overlap(box1,box2))
return(NULL);
+
result = PALLOCTYPE(BOX);
+
result->high.x = Min(box1->high.x, box2->high.x);
result->low.x = Max(box1->low.x, box2->low.x);
result->high.y = Min(box1->high.y, box2->high.y);
LINE * /* point-slope */
line_construct_pm(Point *pt, double m)
{
- LINE *result;
-
- result = PALLOCTYPE(LINE);
+ LINE *result = PALLOCTYPE(LINE);
+
/* use "mx - y + yinter = 0" */
result->A = m;
result->B = -1.0;
LINE * /* two points */
line_construct_pp(Point *pt1, Point *pt2)
{
- LINE *result;
-
- result = PALLOCTYPE(LINE);
+ LINE *result = PALLOCTYPE(LINE);
+
if (FPeq(pt1->x, pt2->x)) { /* vertical */
/* use "x = C" */
result->m = 0.0;
double * /* distance between l1, l2 */
line_distance(LINE *l1, LINE *l2)
{
- double *result;
- Point *tmp;
+ double *result = PALLOCTYPE(double);
+ Point *tmp;
- result = PALLOCTYPE(double);
if (line_intersect(l1, l2)) {
*result = 0.0;
return(result);
#endif
size = offsetof(PATH, p[0]) + (sizeof(path->p[0]) * npts);
- if (!PointerIsValid(path = PALLOC(size)))
- elog(WARN, "Memory allocation failed, can't input path '%s'",str);
+ path = PALLOC(size);
path->size = size;
path->npts = npts;
return NULL;
#if OLD_FORMAT_OUT
- if (!PointerIsValid(result = (char *)PALLOC(path->npts*(P_MAXLEN+3)+2)))
- elog(WARN, "Memory allocation failed, can't output path", NULL);
+ result = PALLOC(path->npts*(P_MAXLEN+3)+2);
cp = result;
*cp++ = LDELIM;
return NULL;
size = offsetof(PATH, p[0]) + (sizeof(path->p[0]) * path->npts);
- if (!PointerIsValid(result = PALLOC(size)))
- elog(WARN, "Memory allocation failed, can't copy path",NULL);
+ result = PALLOC(size);
memmove((char *) result, (char *) path, size);
return(result);
double *path_length(PATH *path)
{
- double *result;
+ double *result = PALLOCTYPE(double);
int ct, i;
- result = PALLOCTYPE(double);
ct = path->npts - 1;
for (i = 0; i < ct; i++)
*result += point_dt(&path->p[i], &path->p[i+1]);
if (! pair_decode( str, &x, &y, &s) || (strlen(s) > 0))
elog (WARN, "Bad point external representation '%s'",str);
- if (!PointerIsValid(point = PALLOCTYPE(Point)))
- elog (WARN, "Unable to allocate point storage for '%s'",str);
+ point = PALLOCTYPE(Point);
point->x = x;
point->y = y;
Point *point_construct(double x, double y)
{
- Point *result;
-
- result = PALLOCTYPE(Point);
+ Point *result = PALLOCTYPE(Point);
+
result->x = x;
result->y = y;
return(result);
Point *point_copy(Point *pt)
{
- Point *result;
-
- result = PALLOCTYPE(Point);
+ Point *result = PALLOCTYPE(Point);
+
result->x = pt->x;
result->y = pt->y;
return(result);
double *point_distance(Point *pt1, Point *pt2)
{
- double *result;
-
- result = PALLOCTYPE(double);
+ double *result = PALLOCTYPE(double);
+
*result = HYPOT( pt1->x - pt2->x, pt1->y - pt2->y );
return(result);
}
double *point_slope(Point *pt1, Point *pt2)
{
- double *result;
-
- result = PALLOCTYPE(double);
+ double *result = PALLOCTYPE(double);
+
if (point_vert(pt1, pt2))
*result = (double)DBL_MAX;
else
if (!PointerIsValid((char *)str))
elog (WARN," Bad (null) lseg external representation",NULL);
- if (!PointerIsValid(lseg = PALLOCTYPE(LSEG)))
- elog(WARN, "Memory allocation failed, can't input lseg '%s'",str);
+ lseg = PALLOCTYPE(LSEG);
if ((! path_decode(TRUE, 2, str, &isopen, &s, &(lseg->p[0])))
|| (*s != '\0'))
*/
LSEG *lseg_construct(Point *pt1, Point *pt2)
{
- LSEG *result;
-
- result = PALLOCTYPE(LSEG);
+ LSEG *result = PALLOCTYPE(LSEG);
+
result->p[0].x = pt1->x;
result->p[0].y = pt1->y;
result->p[1].x = pt2->x;
*/
double *lseg_distance(LSEG *l1, LSEG *l2)
{
- double *result;
-
- result = PALLOCTYPE(double);
+ double *result = PALLOCTYPE(double);
+
*result = lseg_dt( l1, l2);
return(result);
double *dist_pl(Point *pt, LINE *line)
{
- double *result;
-
- result = PALLOCTYPE(double);
+ double *result = PALLOCTYPE(double);
+
*result = (line->A * pt->x + line->B * pt->y + line->C) /
HYPOT(line->A, line->B);
elog(WARN, "Bad polygon external representation '%s'", str);
size = offsetof(POLYGON, p[0]) + (sizeof(poly->p[0]) * npts);
- if (!PointerIsValid(poly = (POLYGON *) PALLOC(size)))
- elog(WARN, "Memory allocation failed, can't input polygon '%s'",str);
+ poly = PALLOC(size);
memset((char *) poly, 0, size); /* zero any holes */
poly->size = size;
return NULL;
#if OLD_FORMAT_OUT
- if (!PointerIsValid(result = (char *)PALLOC(poly->npts*(P_MAXLEN+3)+2)))
- elog(WARN, "Memory allocation failed, can't output polygon", NULL);
+ result = PALLOC(poly->npts*(P_MAXLEN+3)+2);
cp = result;
*cp++ = LDELIM;
if (! (PointerIsValid(p1) && PointerIsValid(p2)))
return(NULL);
- if (!PointerIsValid(result = PALLOCTYPE(Point)))
- elog(WARN, "Memory allocation failed, can't add points",NULL);
+ result = PALLOCTYPE(Point);
result->x = (p1->x + p2->x);
result->y = (p1->y + p2->y);
if (! (PointerIsValid(p1) && PointerIsValid(p2)))
return(NULL);
- if (!PointerIsValid(result = PALLOCTYPE(Point)))
- elog(WARN, "Memory allocation failed, can't add points",NULL);
+ result = PALLOCTYPE(Point);
result->x = (p1->x - p2->x);
result->y = (p1->y - p2->y);
if (! (PointerIsValid(p1) && PointerIsValid(p2)))
return(NULL);
- if (!PointerIsValid(result = PALLOCTYPE(Point)))
- elog(WARN, "Memory allocation failed, can't multiply points",NULL);
+ result = PALLOCTYPE(Point);
result->x = (p1->x*p2->x) - (p1->y*p2->y);
result->y = (p1->x*p2->y) + (p1->y*p2->x);
if (! (PointerIsValid(p1) && PointerIsValid(p2)))
return(NULL);
- if (!PointerIsValid(result = PALLOCTYPE(Point)))
- elog(WARN, "Memory allocation failed, can't multiply path",NULL);
+ result = PALLOCTYPE(Point);
div = (p2->x*p2->x) + (p2->y*p2->y);
return(NULL);
size = offsetof(PATH, p[0]) + (sizeof(p1->p[0]) * (p1->npts+p2->npts));
- if (!PointerIsValid(result = PALLOC(size)))
- elog(WARN, "Memory allocation failed, can't add paths",NULL);
+ result = PALLOC(size);
result->size = size;
result->npts = (p1->npts+p2->npts);
elog(WARN, "Open path cannot be converted to polygon",NULL);
size = offsetof(POLYGON, p[0]) + (sizeof(poly->p[0]) * path->npts);
- if (!PointerIsValid(poly = PALLOC(size)))
- elog(WARN, "Memory allocation failed, can't convert path to polygon",NULL);
+ poly = PALLOC(size);
poly->size = size;
poly->npts = path->npts;
return(box);
} /* poly_box() */
+/* box_poly()
+ * Convert a box to a polygon.
+ */
POLYGON *
box_poly(BOX *box)
{
if (!PointerIsValid(box))
return(NULL);
+ /* map four corners of the box to a polygon */
size = offsetof(POLYGON, p[0]) + (sizeof(poly->p[0]) * 4);
- if (!PointerIsValid(poly = PALLOC(size)))
- elog(WARN, "Memory allocation failed, can't convert box to polygon",NULL);
+ poly = PALLOC(size);
poly->size = size;
poly->npts = 4;
return(NULL);
size = offsetof(PATH, p[0]) + (sizeof(path->p[0]) * poly->npts);
- if (!PointerIsValid(path = PALLOC(size)))
- elog(WARN, "Memory allocation failed, can't convert polygon to path",NULL);
+ path = PALLOC(size);
path->size = size;
path->npts = poly->npts;
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.4 1997/04/25 18:40:25 scrappy Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/geo_ops.c,v 1.5 1997/05/06 07:27:51 thomas Exp $
*
*-------------------------------------------------------------------------
*/
if (!PointerIsValid(str))
elog (WARN," Bad (null) circle external representation",NULL);
- if (!PointerIsValid(circle = PALLOCTYPE(CIRCLE)))
- elog(WARN, "Memory allocation failed, can't input circle '%s'",str);
+ circle = PALLOCTYPE(CIRCLE);
s = str;
while (isspace( *s)) s++;
if (!PointerIsValid(circle))
return(NULL);
- if (!PointerIsValid(result = (char *)PALLOC(3*(P_MAXLEN+1)+3)))
- elog(WARN, "Memory allocation failed, can't output circle", NULL);
+ result = PALLOC(3*(P_MAXLEN+1)+3);
cp = result;
*cp++ = LDELIM_C;
if (!PointerIsValid(circle))
return NULL;
- if (!PointerIsValid(result = PALLOCTYPE(CIRCLE)))
- elog(WARN, "Memory allocation failed, can't copy circle",NULL);
+ result = PALLOCTYPE(CIRCLE);
memmove((char *) result, (char *) circle, sizeof(CIRCLE));
return(result);
if (! (PointerIsValid(center) && PointerIsValid(radius)))
return(NULL);
- if (!PointerIsValid(result = PALLOCTYPE(CIRCLE)))
- elog(WARN, "Memory allocation failed, can't convert point to circle",NULL);
+ result = PALLOCTYPE(CIRCLE);
result->center.x = center->x;
result->center.y = center->y;
elog (WARN, "Unable to convert circle to polygon", NULL);
size = offsetof(POLYGON, p[0]) + (sizeof(poly->p[0]) * npts);
- if (!PointerIsValid(poly = (POLYGON *) PALLOC(size)))
- elog(WARN, "Memory allocation failed, can't convert circle to polygon",NULL);
+ poly = PALLOC(size);
memset((char *) poly, 0, size); /* zero any holes */
poly->size = size;
if (poly->npts <= 2)
elog (WARN, "Unable to convert polygon to circle", NULL);
- if (!PointerIsValid(circle = PALLOCTYPE(CIRCLE)))
- elog(WARN, "Memory allocation failed, can't convert polygon to circle",NULL);
+ circle = PALLOCTYPE(CIRCLE);
circle->center.x = 0;
circle->center.y = 0;