Inter support function

/*****************************************************************************
**
** Function name:
**
**      MyShapeInter
**
** Description:
**
**      This is an R-Tree support function which enables
**      the server to maintain an R-Tree index.  It computes
**      the intersection of two objects' bounding boxes.
**
** Special Comments:
**
**      Because MyShape and its subtypes are variable length opaque 
**      datatypes, the UDT instances are passed in from the server 
**      wrapped in mi_lvarchars.
**
** Parameters:
**
**      mi_lvarchar *in1, *in2   UDT instances to be intersected.
**      mi_lvarchar *out         Resulting intersection.
**      MI_FPARAM   *fp          UDR function parameter & state info.
**
** Return value:
**
**      mi_integer               MI_OK if success, MI_ERROR if problems.
**
*****************************************************************************/

UDREXPORT mi_integer
MyShapeInter (mi_lvarchar *shape_in1,
              mi_lvarchar *shape_in2,
              mi_lvarchar *shape_out,
              MI_FPARAM   *fp)
{
    MyShapeHdr *h1;
    MyShapeHdr *h2;
    MyShapeHdr *h3;

    SHAPE_TRACE_ENTER (MyShapeInter);

    h1 = (MyShapeHdr *) mi_get_vardata (shape_in1);
    h2 = (MyShapeHdr *) mi_get_vardata (shape_in2);
    h3 = (MyShapeHdr *) mi_get_vardata (shape_out);

    CheckVersion (h1->version);
    CheckVersion (h2->version);

    h3->version = SHAPE_VERSION;
    h3->tag     = MyHeaderTag;

    if (!((h1->xmin <= h2->xmax) &&
          (h1->xmax >= h2->xmin) &&
          (h1->ymin <= h2->ymax) &&
          (h1->ymax >= h2->ymin)))
    {
        /*
         * Bounding boxes of the two shapes do not intersect.
         * Indicate this by swapping xmin & xmax and ymin & ymax.
         * R-Tree will follow this Inter() call with a Size() call;
         * at that time we will return zero to indicate no intersection.
         * PROGRAMMING TIP:  There are several ways to indicate no
         * intersection.  You might also consider using a flag in
         * the header structure.
         */
        mi_double temp;
        temp = h1->xmax;
        h3->xmax = h1->xmin;
        h3->xmin = temp;
        temp = h1->ymax;
        h3->ymax = h1->ymin;
        h3->ymin = temp;
    }
    else
    {
        /* 
         * Bounding boxes of the two shapes do intersect.
         * Like MyShapeUnion, h1 and h3 may both reference the same
         * structure, or h2 and h3 may both reference the same structure.
         * This means we have to be careful not to prematurely overwrite
         * any elements of h1 or h2 as we assign values to h3.
         * The following algorithm is safe in this regard.
         */
        h3->xmin = (h1->xmin > h2->xmin) ? h1->xmin : h2->xmin;
        h3->ymin = (h1->ymin > h2->ymin) ? h1->ymin : h2->ymin;
        h3->xmax = (h1->xmax < h2->xmax) ? h1->xmax : h2->xmax;
        h3->ymax = (h1->ymax < h2->ymax) ? h1->ymax : h2->ymax;
    }

    SHAPE_TRACE_EXIT (MyShapeInter);

    return MI_OK;
}