rbbst.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /****************************************************************************
  2. *
  3. * MODULE: r.viewshed
  4. *
  5. * AUTHOR(S): Laura Toma, Bowdoin College - ltoma@bowdoin.edu
  6. * Yi Zhuang - yzhuang@bowdoin.edu
  7. * Ported to GRASS by William Richard -
  8. * wkrichar@bowdoin.edu or willster3021@gmail.com
  9. * Markus Metz: surface interpolation
  10. *
  11. * Date: april 2011
  12. *
  13. * PURPOSE: To calculate the viewshed (the visible cells in the
  14. * raster) for the given viewpoint (observer) location. The
  15. * visibility model is the following: Two points in the raster are
  16. * considered visible to each other if the cells where they belong are
  17. * visible to each other. Two cells are visible to each other if the
  18. * line-of-sight that connects their centers does not intersect the
  19. * terrain. The terrain is NOT viewed as a tesselation of flat cells,
  20. * i.e. if the line-of-sight does not pass through the cell center,
  21. * elevation is determined using bilinear interpolation.
  22. * The viewshed algorithm is efficient both in
  23. * terms of CPU operations and I/O operations. It has worst-case
  24. * complexity O(n lg n) in the RAM model and O(sort(n)) in the
  25. * I/O-model. For the algorithm and all the other details see the
  26. * paper: "Computing Visibility on * Terrains in External Memory" by
  27. * Herman Haverkort, Laura Toma and Yi Zhuang.
  28. *
  29. * COPYRIGHT: (C) 2008 by the GRASS Development Team
  30. *
  31. * This program is free software under the GNU General Public License
  32. * (>=v2). Read the file COPYING that comes with GRASS for details.
  33. *
  34. *****************************************************************************/
  35. #ifndef __RB_BINARY_SEARCH_TREE__
  36. #define __RB_BINARY_SEARCH_TREE__
  37. #define SMALLEST_GRADIENT (- 9999999999999999999999.0)
  38. /*this value is returned by findMaxValueWithinDist() is there is no
  39. key within that distance. The largest double value is 1.7 E 308 */
  40. #define RB_RED (0)
  41. #define RB_BLACK (1)
  42. /*<===========================================>
  43. //public:
  44. //The value that's stored in the tree
  45. //Change this structure to avoid type casting at run time */
  46. typedef struct tree_value_
  47. {
  48. /*this field is mandatory and cannot be removed.
  49. //the tree is indexed by this "key". */
  50. double key;
  51. /*anything else below this line is optional */
  52. double gradient[3];
  53. double angle[3];
  54. double maxGradient;
  55. } TreeValue;
  56. /*The node of a tree */
  57. typedef struct tree_node_
  58. {
  59. TreeValue value;
  60. char color;
  61. struct tree_node_ *left;
  62. struct tree_node_ *right;
  63. struct tree_node_ *parent;
  64. } TreeNode;
  65. typedef struct rbtree_
  66. {
  67. TreeNode *root;
  68. } RBTree;
  69. RBTree *create_tree(TreeValue tv);
  70. void delete_tree(RBTree * t);
  71. void destroy_sub_tree(TreeNode * node);
  72. void insert_into(RBTree * rbt, TreeValue value);
  73. void delete_from(RBTree * rbt, double key);
  74. TreeNode *search_for_node_with_key(RBTree * rbt, double key);
  75. /*------------The following is designed for kreveld's algorithm-------*/
  76. double find_max_gradient_within_key(RBTree * rbt, double key, double angle, double gradient);
  77. /*LT: not sure if this is correct */
  78. int is_empty(RBTree * t);
  79. /*<================================================>
  80. //private:
  81. //The below are private functions you should not
  82. //call directly when using the Tree
  83. //<--------------------------------->
  84. //for RB tree only
  85. //in RB TREE, used to replace NULL */
  86. void init_nil_node();
  87. /*Left and Right Rotation
  88. //the root of the tree may be modified during the rotations
  89. //so TreeNode** is passed into the functions */
  90. void left_rotate(TreeNode ** root, TreeNode * x);
  91. void right_rotate(TreeNode ** root, TreeNode * y);
  92. void rb_insert_fixup(TreeNode ** root, TreeNode * z);
  93. void rb_delete_fixup(TreeNode ** root, TreeNode * x);
  94. /*<------------------------------------> */
  95. /*compare function used by findMaxValue
  96. //-1: v1 < v2
  97. //0: v1 = v2
  98. //2: v1 > v2 */
  99. char compare_values(TreeValue * v1, TreeValue * v2);
  100. /*a function used to compare two doubles */
  101. char compare_double(double a, double b);
  102. /*create a tree node */
  103. TreeNode *create_tree_node(TreeValue value);
  104. /*create node with its value set to the value given
  105. //and insert the node into the tree */
  106. void insert_into_tree(TreeNode ** root, TreeValue value);
  107. /*delete the node out of the tree */
  108. void delete_from_tree(TreeNode ** root, double key);
  109. /*search for a node with the given key */
  110. TreeNode *search_for_node(TreeNode * root, double key);
  111. /*find the min value in the given tree value */
  112. double find_value_min_value(TreeValue * v);
  113. /*find the max value in the given tree
  114. //you need to provide a compare function to compare the nodes */
  115. double find_max_value(TreeNode * root);
  116. /*find max within the max key */
  117. double find_max_value_within_key(TreeNode * root, double maxKey, double angle, double gradient);
  118. #endif