rtree/test.c

Go to the documentation of this file.
00001 
00002 /****************************************************************************
00003 * MODULE:       R-Tree library 
00004 *              
00005 * AUTHOR(S):    Antonin Guttman - original code
00006 *               Daniel Green (green@superliminal.com) - major clean-up
00007 *                               and implementation of bounding spheres
00008 *               
00009 * PURPOSE:      Multidimensional index
00010 *
00011 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00012 *
00013 *               This program is free software under the GNU General Public
00014 *               License (>=v2). Read the file COPYING that comes with GRASS
00015 *               for details.
00016 *****************************************************************************/
00017 
00018 #include <stdio.h>
00019 #include "index.h"
00020 
00021 struct Rect rects[] = {
00022     {{0, 0, 0, 2, 2, 0}},       /* xmin, ymin, zmin, xmax, ymax, zmax (for 3 dimensional RTree) */
00023     {{5, 5, 0, 7, 7, 0}},
00024     {{8, 5, 0, 9, 6, 0}},
00025     {{7, 1, 0, 9, 2, 0}}
00026 };
00027 
00028 
00029 int nrects = sizeof(rects) / sizeof(rects[0]);
00030 struct Rect search_rect = {
00031     {6, 4, 0, 10, 6, 0}         /* search will find above rects that this one overlaps */
00032 };
00033 
00034 int MySearchCallback(int id, void *arg)
00035 {
00036     /* Note: -1 to make up for the +1 when data was inserted */
00037     fprintf(stdout, "Hit data rect %d\n", id - 1);
00038     return 1;                   /* keep going */
00039 }
00040 
00041 int main()
00042 {
00043     struct Node *root = RTreeNewIndex();
00044     int i, nhits;
00045 
00046     fprintf(stdout, "nrects = %d\n", nrects);
00047     /*
00048      * Insert all the data rects.
00049      * Notes about the arguments:
00050      * parameter 1 is the rect being inserted,
00051      * parameter 2 is its ID. NOTE: *** ID MUST NEVER BE ZERO ***, hence the +1,
00052      * parameter 3 is the root of the tree. Note: its address is passed
00053      * because it can change as a result of this call, therefore no other parts
00054      * of this code should stash its address since it could change undernieth.
00055      * parameter 4 is always zero which means to add from the root.
00056      */
00057     for (i = 0; i < nrects; i++)
00058         RTreeInsertRect(&rects[i], i + 1, &root, 0);    /* i+1 is rect ID. Note: root can change */
00059     nhits = RTreeSearch(root, &search_rect, MySearchCallback, 0);
00060     fprintf(stdout, "Search resulted in %d hits\n", nhits);
00061 
00062     return 0;
00063 }

Generated on Thu Jul 16 13:21:18 2009 for GRASS Programmer's Manual by  doxygen 1.5.6