home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / access / index-rtree / rtproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  3.3 KB  |  166 lines

  1. /*
  2.  *  rtproc.c -- pg_amproc entries for rtrees.
  3.  */
  4. #include <math.h>
  5.  
  6. #include "tmp/c.h"
  7. #include "tmp/postgres.h"
  8.  
  9. #include "utils/log.h"
  10. #include "utils/geo-decls.h"
  11.  
  12. RcsId("$Header: /private/postgres/src/access/index-rtree/RCS/rtproc.c,v 1.5 1991/05/09 13:38:56 mer Exp $");
  13.  
  14. BOX *
  15. rt_box_union(a, b)
  16.     BOX *a;
  17.     BOX *b;
  18. {
  19.     BOX *n;
  20.  
  21.     if ((n = (BOX *) palloc(sizeof (*n))) == (BOX *) NULL)
  22.         elog(WARN, "Cannot allocate box for union");
  23.  
  24.     n->xh = MAX(a->xh, b->xh);
  25.     n->yh = MAX(a->yh, b->yh);
  26.     n->xl = MIN(a->xl, b->xl);
  27.     n->yl = MIN(a->yl, b->yl);
  28.  
  29.     return (n);
  30. }
  31.  
  32. BOX *
  33. rt_box_inter(a, b)
  34.     BOX *a;
  35.     BOX *b;
  36. {
  37.     BOX *n;
  38.  
  39.     if ((n = (BOX *) palloc(sizeof (*n))) == (BOX *) NULL)
  40.         elog(WARN, "Cannot allocate box for union");
  41.  
  42.     n->xh = MIN(a->xh, b->xh);
  43.     n->yh = MIN(a->yh, b->yh);
  44.     n->xl = MAX(a->xl, b->xl);
  45.     n->yl = MAX(a->yl, b->yl);
  46.  
  47.     if (n->xh < n->xl || n->yh < n->yl) {
  48.         pfree (n);
  49.         return ((BOX *) NULL);
  50.     }
  51.  
  52.     return (n);
  53. }
  54.  
  55. int
  56. rt_box_size(a)
  57.     BOX *a;
  58. {
  59.     int size;
  60.  
  61.     if (a == (BOX *) NULL || a->xh <= a->xl || a->yh <= a->yl)
  62.         return (0);
  63.  
  64.     size = (int) (fabs(a->xh - a->xl) * fabs(a->yh - a->yl));
  65.  
  66.     return (size);
  67. }
  68.  
  69. /*
  70.  *  rt_bigbox_size() -- Compute a scaled size for big boxes.
  71.  *
  72.  *    If the field on which rectangles lie is large, or if the rectangles
  73.  *    themselves are large, then size computations on them can cause
  74.  *    arithmetic overflows.  On some architectures (eg, Sequent), the
  75.  *    overflow causes a core dump when we try to cast the double to an
  76.  *    int.  Rather than do non-portable arithmetic exception handling,
  77.  *    we declare an operator class "bigbox_ops" that's exactly like
  78.  *    "box_ops", except that sizes are scaled by four orders of decimal
  79.  *    magnitude.
  80.  */
  81.  
  82. int
  83. rt_bigbox_size(a)
  84.     BOX *a;
  85. {
  86.     int size;
  87.     double xdim, ydim;
  88.  
  89.     if (a == (BOX *) NULL || a->xh <= a->xl || a->yh <= a->yl)
  90.         return (0);
  91.  
  92.     /* scale boxes by 100 in both dimensions */
  93.     xdim = fabs(a->xh - a->xl) / 100.0;
  94.     ydim = fabs(a->yh - a->yl) / 100.0;
  95.  
  96.     size = (int) (xdim * ydim);
  97.  
  98.     return (size);
  99. }
  100.  
  101. POLYGON *
  102. rt_poly_union(a, b)
  103.     POLYGON *a;
  104.     POLYGON *b;
  105. {
  106.     POLYGON *p;
  107.  
  108.     p = (POLYGON *)PALLOCTYPE(POLYGON);
  109.  
  110.     if (!PointerIsValid(p))
  111.         elog(WARN, "Cannot allocate polygon for union");
  112.  
  113.     p->size = sizeof(POLYGON);
  114.     p->boundbox.xh = MAX(a->boundbox.xh, b->boundbox.xh);
  115.     p->boundbox.yh = MAX(a->boundbox.yh, b->boundbox.yh);
  116.     p->boundbox.xl = MIN(a->boundbox.xl, b->boundbox.xl);
  117.     p->boundbox.yl = MIN(a->boundbox.yl, b->boundbox.yl);
  118.     return p;
  119. }
  120.  
  121. int
  122. rt_poly_size(a)
  123.     POLYGON *a;
  124. {
  125.     int size;
  126.     double xdim, ydim;
  127.  
  128.     if (a == (POLYGON *) NULL || 
  129.         a->boundbox.xh <= a->boundbox.xl || 
  130.         a->boundbox.yh <= a->boundbox.yl)
  131.         return (0);
  132.  
  133.     xdim = fabs(a->boundbox.xh - a->boundbox.xl) / 100.0;
  134.     ydim = fabs(a->boundbox.yh - a->boundbox.yl) / 100.0;
  135.  
  136.     size = (int) (xdim * ydim);
  137.  
  138.     return (size);
  139. }
  140.  
  141. POLYGON *
  142. rt_poly_inter(a, b)
  143.     POLYGON *a;
  144.     POLYGON *b;
  145. {
  146.     POLYGON *p;
  147.  
  148.     p = (POLYGON *) PALLOCTYPE(POLYGON);
  149.  
  150.     if (!PointerIsValid(p))
  151.         elog(WARN, "Cannot allocate polygon for intersection");
  152.  
  153.     p->boundbox.xh = MIN(a->boundbox.xh, b->boundbox.xh);
  154.     p->boundbox.yh = MIN(a->boundbox.yh, b->boundbox.yh);
  155.     p->boundbox.xl = MAX(a->boundbox.xl, b->boundbox.xl);
  156.     p->boundbox.yl = MAX(a->boundbox.yl, b->boundbox.yl);
  157.  
  158.     if (p->boundbox.xh < p->boundbox.xl || p->boundbox.yh < p->boundbox.yl)
  159.     {
  160.         pfree (p);
  161.         return ((POLYGON *) NULL);
  162.     }
  163.  
  164.     return (p);
  165. }
  166.