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