test.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /****************************************************************************
  2. * MODULE: R-Tree library
  3. *
  4. * AUTHOR(S): Antonin Guttman - original code
  5. * Daniel Green (green@superliminal.com) - major clean-up
  6. * and implementation of bounding spheres
  7. *
  8. * PURPOSE: Multidimensional index
  9. *
  10. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include "index.h"
  18. struct Rect rects[] = {
  19. {{0, 0, 0, 2, 2, 0}}, /* xmin, ymin, zmin, xmax, ymax, zmax (for 3 dimensional RTree) */
  20. {{5, 5, 0, 7, 7, 0}},
  21. {{8, 5, 0, 9, 6, 0}},
  22. {{7, 1, 0, 9, 2, 0}}
  23. };
  24. int nrects = sizeof(rects) / sizeof(rects[0]);
  25. struct Rect search_rect = {
  26. {6, 4, 0, 10, 6, 0} /* search will find above rects that this one overlaps */
  27. };
  28. int MySearchCallback(int id, void *arg)
  29. {
  30. /* Note: -1 to make up for the +1 when data was inserted */
  31. fprintf(stdout, "Hit data rect %d\n", id - 1);
  32. return 1; /* keep going */
  33. }
  34. int main()
  35. {
  36. struct Node *root = RTreeNewIndex();
  37. int i, nhits;
  38. fprintf(stdout, "nrects = %d\n", nrects);
  39. /*
  40. * Insert all the data rects.
  41. * Notes about the arguments:
  42. * parameter 1 is the rect being inserted,
  43. * parameter 2 is its ID. NOTE: *** ID MUST NEVER BE ZERO ***, hence the +1,
  44. * parameter 3 is the root of the tree. Note: its address is passed
  45. * because it can change as a result of this call, therefore no other parts
  46. * of this code should stash its address since it could change undernieth.
  47. * parameter 4 is always zero which means to add from the root.
  48. */
  49. for (i = 0; i < nrects; i++)
  50. RTreeInsertRect(&rects[i], i + 1, &root, 0); /* i+1 is rect ID. Note: root can change */
  51. nhits = RTreeSearch(root, &search_rect, MySearchCallback, 0);
  52. fprintf(stdout, "Search resulted in %d hits\n", nhits);
  53. return 0;
  54. }